binfmt_miscがフラグなしで動作している間に「OC」フラグを使用しないのはなぜですか?

binfmt_miscがフラグなしで動作している間に「OC」フラグを使用しないのはなぜですか?

Debian 10でLinux機能をテストしたbinfmt_misc結果、フラグを「OC」に設定すると(インタプリタの代わりにバイナリ資格情報を使用)、実行が自動的に失敗することがわかりました。

以下のPOCでは、/tmp/test.shインタプリタとqux.goバイナリファイルです。/tmp/test.shフラグなしでは正常に実行されますが、「OC」フラグがあると静かに失敗するのはなぜですか?

POC:

$ touch qux.go
$ chmod +x qux.go
$ cat <<EOF >/tmp/test.sh                                                                                                                                                                                                          
> #!/bin/sh                                                                                                                                                                                                                                                                       
> echo Golang                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
> EOF      
$ chmod +x /tmp/test.sh 
$ echo ':golang:E::go::/tmp/test.sh:' | sudo tee /proc/sys/fs/binfmt_misc/register 
:golang:E::go::/tmp/test.sh:
$ ./qux.go 
Golang
$ echo -1 | sudo tee /proc/sys/fs/binfmt_misc/golang 
-1
$ echo ':golang:E::go::/tmp/test.sh:OC' | sudo tee /proc/sys/fs/binfmt_misc/register 
:golang:E::go::/tmp/test.sh:OC
$ ./qux.go # no output

返品:

mount | grep binfmt_misc
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=28,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=658)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,nosuid,nodev,noexec,relatime)

ボーナス:

一部のリソースは、binfmt_miscコンテナからホストへのエスケープに使用できると主張しています。しかし、私が見ることができるように、インタプリタパスはchrootコンテナのファイルシステム内で評価され、インタプリタの実行はコンテナ内で行われます。つまり、ls -la /コンテナルート(ホストルートではない)が表示されます。

リソース:

https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html

ベストアンサー1

2つの機能のために問題が発生しました。

1つ目は、exec失敗した場合にシェルが実行しようとしているファイルの内容を見て、シェルスクリプトのように見える場合はそれ自体を解釈することです。空のファイルはシェルスクリプトのように見えます。次のコマンドを実行してstrace -f ./qux.go(表示を表示)、変更してexec確認できますqux.go

$ echo echo Failed Golang > qux.go
$ ./qux.go
Failed Golang

もう一つの特徴はOロゴです。カスケードインタプリタでは動作しません。:あなたの場合はqux.goインタプリタが必要ですが、インタプリタ自体にはインタプリタが必要なので、解釈するファイルは2つあり/bin/shます。以下はうまくいきます:test.shqux.goO

$ cat <<EOF > /tmp/test.c
#include <stdio.h>

int main(int argc, char **argv) {
  puts("Golang");
  return 0;
}
EOF
$ make /tmp/test
cc     /tmp/test.c   -o /tmp/test
$ echo ':golang:E::go::/tmp/test:OC' | sudo tee /proc/sys/fs/binfmt_misc/register
:golang:E::go::/tmp/test:OC
$ ./qux.go
Golang

おすすめ記事