diff options
| author | Ophestra <cat@gensokyo.uk> | 2026-05-07 15:40:47 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2026-05-07 15:55:19 +0900 |
| commit | 575ef307ad5cedb13a514cd038a4880ab8c91242 (patch) | |
| tree | 71c0bd20e32e08c56ce1be6824f8dbe0f1a091ed /container/binfmt.go | |
| parent | d4144fcf7f9217d2a2dcec21202421d5fa9d4928 (diff) | |
container: binfmt registration
This arranges for binfmt entries to be registered for the container.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'container/binfmt.go')
| -rw-r--r-- | container/binfmt.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/container/binfmt.go b/container/binfmt.go new file mode 100644 index 00000000..837e35b4 --- /dev/null +++ b/container/binfmt.go @@ -0,0 +1,46 @@ +package container + +import ( + "strings" + "unsafe" + + "hakurei.app/check" +) + +// escapeBinfmt escapes magic/mask sequences in a [BinfmtEntry]. +func escapeBinfmt(buf *strings.Builder, s string) string { + const lowerhex = "0123456789abcdef" + + buf.Reset() + for _, c := range unsafe.Slice(unsafe.StringData(s), len(s)) { + switch c { + case 0, '\\', ':': + buf.WriteString(`\x`) + buf.WriteByte(lowerhex[c>>4]) + buf.WriteByte(lowerhex[c&0xf]) + + default: + buf.WriteByte(c) + } + } + return buf.String() +} + +// BinfmtEntry is an entry to be registered by the init process. +type BinfmtEntry struct { + // The offset of the magic/mask in the file, counted in bytes. + Offset byte + // The byte sequence binfmt_misc is matching for. + Magic string + // An (optional, defaults to all 0xff) mask. + Mask string + // The program that should be invoked with the binary as first argument. + Interpreter *check.Absolute +} + +// Valid returns whether e can be registered into the kernel. +func (e *BinfmtEntry) Valid() bool { + return e != nil && + int(e.Offset)+max(len(e.Magic), len(e.Mask)) < 128 && + e.Interpreter != nil && len(e.Interpreter.String()) < 128 +} |
