aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/sandbox/sequential.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-03-14 02:18:44 +0900
committerOphestra <cat@gensokyo.uk>2025-03-14 02:18:44 +0900
commitf332200ca4a18ae098e4c42509ba1256d8562cf9 (patch)
tree7668eeb8d4c1672cf2f31cd7fdd49ddeed8ddcc7 /internal/sandbox/sequential.go
parent2eff47009102d53844906aa90fb2b8eddae78c1b (diff)
sandbox: mount container /dev
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/sandbox/sequential.go')
-rw-r--r--internal/sandbox/sequential.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/internal/sandbox/sequential.go b/internal/sandbox/sequential.go
index 719af5f1..93cc74c3 100644
--- a/internal/sandbox/sequential.go
+++ b/internal/sandbox/sequential.go
@@ -7,6 +7,7 @@ import (
"os"
"path"
"syscall"
+ "unsafe"
"git.gensokyo.uk/security/fortify/internal/fmsg"
)
@@ -62,6 +63,89 @@ func (p *MountProc) apply(*InitParams) error {
fmt.Sprintf("cannot mount proc on %q:", p.Path))
}
+func init() { gob.Register(new(MountDev)) }
+
+// MountDev mounts dev on container Path.
+type MountDev struct {
+ Path string
+}
+
+func (d *MountDev) apply(params *InitParams) error {
+ if !path.IsAbs(d.Path) {
+ return fmsg.WrapError(syscall.EBADE,
+ fmt.Sprintf("path %q is not absolute", d.Path))
+ }
+ target := toSysroot(d.Path)
+
+ if err := mountTmpfs("devtmpfs", d.Path, 0, 0755); err != nil {
+ return err
+ }
+
+ for _, name := range []string{"null", "zero", "full", "random", "urandom", "tty"} {
+ if err := bindMount(
+ "/dev/"+name, path.Join(d.Path, name),
+ BindSource|BindDevices,
+ ); err != nil {
+ return err
+ }
+ }
+ for i, name := range []string{"stdin", "stdout", "stderr"} {
+ if err := os.Symlink(
+ "/proc/self/fd/"+string(rune(i+'0')),
+ path.Join(target, name),
+ ); err != nil {
+ return fmsg.WrapError(err, err.Error())
+ }
+ }
+ for _, pair := range [][2]string{
+ {"/proc/self/fd", "fd"},
+ {"/proc/kcore", "core"},
+ {"pts/ptmx", "ptmx"},
+ } {
+ if err := os.Symlink(pair[0], path.Join(target, pair[1])); err != nil {
+ return fmsg.WrapError(err, err.Error())
+ }
+ }
+
+ devPtsPath := path.Join(target, "pts")
+ for _, name := range []string{path.Join(target, "shm"), devPtsPath} {
+ if err := os.Mkdir(name, 0755); err != nil {
+ return fmsg.WrapError(err, err.Error())
+ }
+ }
+
+ if err := syscall.Mount("devpts", devPtsPath, "devpts",
+ syscall.MS_NOSUID|syscall.MS_NOEXEC,
+ "newinstance,ptmxmode=0666,mode=620"); err != nil {
+ return fmsg.WrapErrorSuffix(err,
+ fmt.Sprintf("cannot mount devpts on %q:", devPtsPath))
+ }
+
+ if params.Flags&FAllowTTY != 0 {
+ var buf [8]byte
+ if _, _, errno := syscall.Syscall(
+ syscall.SYS_IOCTL, 1, syscall.TIOCGWINSZ,
+ uintptr(unsafe.Pointer(&buf[0])),
+ ); errno == 0 {
+ if err := bindMount(
+ "/proc/self/fd/1", path.Join(d.Path, "console"),
+ BindDevices,
+ ); err != nil {
+ return err
+ }
+ }
+ }
+
+ return nil
+}
+
+func (d *MountDev) Is(op Op) bool { vd, ok := op.(*MountDev); return ok && *d == *vd }
+func (d *MountDev) String() string { return fmt.Sprintf("dev on %q", d.Path) }
+func (f *Ops) Dev(dest string) *Ops {
+ *f = append(*f, &MountDev{dest})
+ return f
+}
+
func (p *MountProc) Is(op Op) bool { vp, ok := op.(*MountProc); return ok && *p == *vp }
func (p *MountProc) String() string { return fmt.Sprintf("proc on %q", p.Path) }
func (f *Ops) Proc(dest string) *Ops {