I have been testing binfmt_misc feature of Linux on Debian 10, and have found that setting the flags to "OC", to use the credentials of the binary instead of interpreter, causes execution to fail silently.
In the POC below, /tmp/test.sh is the interpreter, while qux.go is the binary. Why is /tmp/test.sh executed successfully without flags, when it fails silently with flags "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
Also:
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)
Bonus:
Some resources claim that binfmt_misc could be used for container-to-host escapes. However, as I see it, the interpreter path is evaluated within the chroot'd filesystem of the container, and execution of the interpreter is happening within the container, i.e. ls -la / shows the container root (not the host root).
Resource:
https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html