fortify/internal/sandbox/tmpfs.go
Ophestra 9f43c2a263
All checks were successful
Test / Create distribution (push) Successful in 25s
Test / Fortify (push) Successful in 2m31s
Test / Fpkg (push) Successful in 3m27s
Test / Data race detector (push) Successful in 3m32s
Test / Flake checks (push) Successful in 51s
sandbox: native container tooling
This should eventually replace bwrap.

Signed-off-by: Ophestra <cat@gensokyo.uk>
2025-03-13 20:59:03 +09:00

50 lines
1.2 KiB
Go

package sandbox
import (
"encoding/gob"
"fmt"
"math"
"os"
"path"
"syscall"
"git.gensokyo.uk/security/fortify/internal/fmsg"
)
func init() { gob.Register(new(MountTmpfs)) }
type MountTmpfs struct {
Path string
Size int
Mode os.FileMode
}
func (t *MountTmpfs) apply() error {
if !path.IsAbs(t.Path) {
return fmsg.WrapError(syscall.EBADE,
fmt.Sprintf("path %q is not absolute", t.Path))
}
if t.Size < 0 || t.Size > math.MaxUint>>1 {
return fmsg.WrapError(syscall.EBADE,
fmt.Sprintf("size %d out of bounds", t.Size))
}
target := toSysroot(t.Path)
if err := os.MkdirAll(target, 0755); err != nil {
return err
}
opt := fmt.Sprintf("mode=%#o", t.Mode)
if t.Size > 0 {
opt += fmt.Sprintf(",size=%d", t.Mode)
}
return fmsg.WrapErrorSuffix(syscall.Mount("tmpfs", target, "tmpfs",
syscall.MS_NOSUID|syscall.MS_NODEV, opt),
fmt.Sprintf("cannot mount tmpfs on %q:", t.Path))
}
func (t *MountTmpfs) Is(op FSOp) bool { vt, ok := op.(*MountTmpfs); return ok && *t == *vt }
func (t *MountTmpfs) String() string { return fmt.Sprintf("tmpfs on %q size %d", t.Path, t.Size) }
func (f *Filesystem) Tmpfs(dest string, size int, mode os.FileMode) *Filesystem {
*f = append(*f, &MountTmpfs{dest, size, mode})
return f
}