A desktop coding agent with free access to frontier models.
TypeScript
fix: boxrun init stays inside the box root
ogsamrat4fc18adba151
diff --git a/apps/desktop/box/go/cmd/boxrun/init.go b/apps/desktop/box/go/cmd/boxrun/init.goindex 02aa0fe8..06ab85e1 100644--- a/apps/desktop/box/go/cmd/boxrun/init.go+++ b/apps/desktop/box/go/cmd/boxrun/init.go@@ -8,7 +8,6 @@ import ("fmt""os""os/exec"- "path/filepath""strings""syscall""time"@@ -61,45 +60,6 @@ func ensureDir(p string) error {return os.MkdirAll(p, 0o755)}-func ensureFile(p string) error {- if fi, err := os.Lstat(p); err == nil {- if fi.Mode().IsRegular() {- return nil- }- return fmt.Errorf("%s is not a file", p)- }- if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {- return err- }- f, err := os.OpenFile(p, os.O_CREATE|os.O_WRONLY, 0o644)- if err != nil {- return err- }- return f.Close()-}--func bind(src, dst string, ro bool, rec bool) error {- flags := uintptr(syscall.MS_BIND)- if rec {- flags |= syscall.MS_REC- }- if err := syscall.Mount(src, dst, "", flags, ""); err != nil {- return fmt.Errorf("bind %s: %w", dst, err)- }- if ro {- // Read-only needs a remount of the bind; flags locked by the parent namespace must be kept.- var st syscall.Statfs_t- extra := uintptr(0)- if syscall.Statfs(dst, &st) == nil {- extra = uintptr(st.Flags) & (syscall.MS_NOSUID | syscall.MS_NODEV | syscall.MS_NOEXEC | syscall.MS_NOATIME | syscall.MS_RELATIME)- }- if err := syscall.Mount("", dst, "", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|extra, ""); err != nil {- return fmt.Errorf("read-only %s: %w", dst, err)- }- }- return nil-}-// stageInit runs inside the namespaces as root: overlay root, the project// and run folders, /proc /dev /sys, DNS, then the root switch and boxd.func stageInit(args []string) error {@@ -130,36 +90,79 @@ func stageInit(args []string) error {if err != nil {return err}- in := func(p string) string { return filepath.Join(m, p) }+ shadowsOn, shadowsMade, err := prepareRoot(sp, m)+ if err != nil {+ return err+ }+ if len(shadowsOn) > 0 {+ list, _ := json.Marshal(shadowsOn)+ sp.Env = append(sp.Env, "BOXD_SHADOWS="+string(list))+ }+ if sp.InitOut != "" {+ info, _ := json.Marshal(map[string]any{"overlay": overlay, "ms": time.Since(t0).Milliseconds(), "shadows": nonNil(shadowsOn), "made": nonNil(shadowsMade)})+ _ = os.WriteFile(sp.InitOut, info, 0o600)+ }+ // Switch root: pivot_root(".", ".") stacks the old root on the new one;+ // detaching it leaves only the box.+ if err := os.Chdir(m); err != nil {+ return err+ }+ if err := syscall.PivotRoot(".", "."); err != nil {+ return fmt.Errorf("pivot_root: %w", err)+ }+ if err := syscall.Unmount(".", syscall.MNT_DETACH); err != nil {+ return fmt.Errorf("detach old root: %w", err)+ }+ if err := os.Chdir("/"); err != nil {+ return err+ }+ return syscall.Exec("/opt/omnirush/bin/boxd", []string{"boxd"}, sp.Env)+}++// prepareRoot fills the mounted root m (inroot.go: nothing here follows a link the box left+// out of it): the command folders, /proc /dev /sys, boxd, DNS, the binds and the shadows.+func prepareRoot(sp spec, m string) (shadowsOn, shadowsMade []string, err error) {+ r, err := openRoot(m)+ if err != nil {+ return nil, nil, err+ }+ defer r.Close()// The command folders boxd and the launcher use. They are made once, in// the upper layer, before boxd's first index, so they are no change.for _, d := range []string{"proc", "dev", "sys", "tmp", "run", "opt/omnirush/bin"} {- if err := ensureDir(in(d)); err != nil {- return err+ if err := r.ensureDirIn(d); err != nil {+ return nil, nil, err}}- _ = os.Chmod(in("tmp"), 0o1777)- if err := syscall.Mount("proc", in("proc"), "proc", syscall.MS_NOSUID|syscall.MS_NODEV|syscall.MS_NOEXEC, ""); err != nil {+ _ = r.chmodIn("tmp", 0o1777)+ err = r.mountIn("proc", true, "", func(target string) error {+ return syscall.Mount("proc", target, "proc", syscall.MS_NOSUID|syscall.MS_NODEV|syscall.MS_NOEXEC, "")+ })+ if err != nil {// Some kernels refuse a fresh proc while the host's is partly covered: bind the namespace's own.- if err2 := bind("/proc", in("proc"), false, true); err2 != nil {- return fmt.Errorf("proc: %v; %v", err, err2)+ if err2 := r.bindIn("/proc", "proc", true, false, true); err2 != nil {+ return nil, nil, fmt.Errorf("proc: %v; %v", err, err2)}}- if err := bind("/dev", in("dev"), false, true); err != nil {- return err+ r.special["proc"] = true+ if err := r.bindIn("/dev", "dev", true, false, true); err != nil {+ return nil, nil, err}- if fi, err := os.Stat(in("dev/shm")); err == nil && fi.IsDir() {- _ = syscall.Mount("tmpfs", in("dev/shm"), "tmpfs", syscall.MS_NOSUID|syscall.MS_NODEV, "mode=1777")+ r.special["dev"] = true+ if fd, names, err := r.open("dev/shm", "dev"); err == nil {+ var st syscall.Stat_t+ if syscall.Fstat(fd, &st) == nil && st.Mode&syscall.S_IFMT == syscall.S_IFDIR && len(names) > 0 && names[0] == "dev" {+ _ = syscall.Mount("tmpfs", r.target(fd, names), "tmpfs", syscall.MS_NOSUID|syscall.MS_NODEV, "mode=1777")+ }+ _ = syscall.Close(fd)}- if err := bind("/sys", in("sys"), true, true); err != nil {+ if err := r.bindIn("/sys", "sys", true, true, true); err != nil {// /sys is only read by a few tools: go on without it.fmt.Fprintf(os.Stderr, "boxrun: %v\n", err)}- if err := ensureFile(in("opt/omnirush/bin/boxd")); err != nil {- return err- }- if err := bind(sp.Boxd, in("opt/omnirush/bin/boxd"), true, false); err != nil {- return err+ r.special["sys"] = true+ if err := r.bindIn(sp.Boxd, "opt/omnirush/bin/boxd", false, true, false); err != nil {+ return nil, nil, err}for _, f := range []struct{ src, dst string }{{sp.Resolv, "etc/resolv.conf"}, {sp.Hosts, "etc/hosts"}} {if f.src == "" {@@ -168,65 +171,57 @@ func stageInit(args []string) error {if _, err := os.Stat(f.src); err != nil {continue}- target := in(f.dst)- if fi, err := os.Lstat(target); err == nil && fi.Mode()&os.ModeSymlink != 0 {- continue // the image links it somewhere: leave it+ if imageLinksFile(r, f.dst) {+ continue // the image links it to a file of its own: leave it}- if ensureFile(target) == nil {- _ = bind(f.src, target, true, false)+ if err := r.bindIn(f.src, f.dst, false, true, false); err != nil {+ fmt.Fprintf(os.Stderr, "boxrun: %v\n", err)}}for _, b := range sp.Binds {- target := in(b.Dst)fi, err := os.Stat(b.Src)if err != nil {- return fmt.Errorf("bind source %s: %w", b.Dst, err)- }- if fi.IsDir() {- err = ensureDir(target)- } else {- err = ensureFile(target)- }- if err != nil {- return err+ return nil, nil, fmt.Errorf("bind source %s: %w", b.Dst, err)}- if err := bind(b.Src, target, b.RO, true); err != nil {- return err+ if err := r.bindIn(b.Src, b.Dst, fi.IsDir(), b.RO, true); err != nil {+ return nil, nil, err}}// The box's own build folders, over the project's (after its bind: they are inside it).- shadowsOn, shadowsMade := bindShadows(sp, in)- if len(shadowsOn) > 0 {- list, _ := json.Marshal(shadowsOn)- sp.Env = append(sp.Env, "BOXD_SHADOWS="+string(list))- }+ shadowsOn, shadowsMade = bindShadows(sp, r)if sp.AptRoot {// One uid is mapped (the user's, as root): apt cannot drop to _apt.- conf := in("etc/apt/apt.conf.d")- if fi, err := os.Stat(conf); err == nil && fi.IsDir() {- file := filepath.Join(conf, "00omnirush-box")- if _, err := os.Stat(file); err != nil {- _ = os.WriteFile(file, []byte("APT::Sandbox::User \"root\";\n"), 0o644)+ if fd, _, err := r.open("etc/apt/apt.conf.d", ""); err == nil {+ if mode, err := statAt(fd, "."); err == nil && mode == syscall.S_IFDIR {+ if f, err := syscall.Openat(fd, "00omnirush-box", syscall.O_CREAT|syscall.O_EXCL|syscall.O_WRONLY|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, 0o644); err == nil {+ _, _ = syscall.Write(f, []byte("APT::Sandbox::User \"root\";\n"))+ _ = syscall.Close(f)+ }}+ _ = syscall.Close(fd)}}- if sp.InitOut != "" {- info, _ := json.Marshal(map[string]any{"overlay": overlay, "ms": time.Since(t0).Milliseconds(), "shadows": nonNil(shadowsOn), "made": nonNil(shadowsMade)})- _ = os.WriteFile(sp.InitOut, info, 0o600)- }- // Switch root: pivot_root(".", ".") stacks the old root on the new one;- // detaching it leaves only the box.- if err := os.Chdir(m); err != nil {- return err- }- if err := syscall.PivotRoot(".", "."); err != nil {- return fmt.Errorf("pivot_root: %w", err)+ return shadowsOn, shadowsMade, nil+}++// imageLinksFile: rel is a link (the image's, or the box's) to a file in the root, followed+// inside it as the box will: left alone. A link to nothing, a folder or init's mounts is+// replaced by the real file.+func imageLinksFile(r *rootFS, rel string) bool {+ pfd, _, name, err := r.parent(rel, "")+ if err != nil {+ return false}- if err := syscall.Unmount(".", syscall.MNT_DETACH); err != nil {- return fmt.Errorf("detach old root: %w", err)+ mode, err := statAt(pfd, name)+ _ = syscall.Close(pfd)+ if err != nil || mode != syscall.S_IFLNK {+ return false}- if err := os.Chdir("/"); err != nil {- return err+ fd, _, err := r.open(rel, "")+ if err != nil {+ return false}- return syscall.Exec("/opt/omnirush/bin/boxd", []string{"boxd"}, sp.Env)+ defer syscall.Close(fd)+ var st syscall.Stat_t+ return syscall.Fstat(fd, &st) == nil && st.Mode&syscall.S_IFMT == syscall.S_IFREG}diff --git a/apps/desktop/box/go/cmd/boxrun/inroot.go b/apps/desktop/box/go/cmd/boxrun/inroot.gonew file mode 100644index 00000000..0f594501--- /dev/null+++ b/apps/desktop/box/go/cmd/boxrun/inroot.go@@ -0,0 +1,440 @@+//go:build linux++package main++// Paths inside the box's root before the root switch. init runs as root+// (real root under unshare in the WSL distro / Lima VM), and the overlay's+// upper layer is the box's from earlier runs: any folder on the way to a+// path init makes, changes or mounts on may be a link the box left there.+// So nothing here goes by path: every path is walked a component at a time+// from the root's own descriptor, never following a link by the kernel. A+// link on the way is read and followed inside the root, as the box will+// see it after the root switch (an absolute target starts again at the+// root, ".." stops there), so it never leads out of it. The kernel's own+// openat2(RESOLVE_IN_ROOT) re-checks every result where it exists (5.6+).+// The mounts init puts in the root (/proc, /dev, /sys) are never walked+// into unless asked for, and a link where init makes a folder or a file of+// its own is removed (it is in the root: the overlay keeps a whiteout) and+// the real one made instead. Mounts go through /proc/self/fd/N, the very+// folder or file the walk opened.++import (+ "errors"+ "fmt"+ "os"+ "path/filepath"+ "strings"+ "syscall"+ "unsafe"+)++const (+ maxLinks = 40++ sysOpenat2 = 437 // the same number on every Linux architecture+ resolveNoSymlinks = 0x04+ resolveNoMagiclinks = 0x02+ resolveInRoot = 0x10+)++// forceNoOpenat2: tests of the walk alone.+var forceNoOpenat2 bool++type rootFS struct {+ path string+ fd int+ special map[string]bool // first components walks stay out of (init's /proc, /dev, /sys mounts)+ // noOpenat2: the kernel has no openat2 (or it is filtered): the walk alone.+ noOpenat2 bool+ // procFD: mount through /proc/self/fd (false: by the walked path, no links in it).+ procFD bool+}++func openRoot(path string) (*rootFS, error) {+ fd, err := syscall.Open(path, oPath|syscall.O_DIRECTORY|syscall.O_CLOEXEC, 0)+ if err != nil {+ return nil, fmt.Errorf("box root %s: %w", path, err)+ }+ r := &rootFS{path: path, fd: fd, special: map[string]bool{}, noOpenat2: forceNoOpenat2}+ if fi, err := os.Stat("/proc/self/fd"); err == nil && fi.IsDir() {+ r.procFD = true+ }+ return r, nil+}++func (r *rootFS) Close() { _ = syscall.Close(r.fd) }++// pathError: a path init uses goes where it must not (init's own mounts, a file on the way).+type pathError struct {+ rel string+ why string+}++func (e *pathError) Error() string { return fmt.Sprintf("box root: /%s: %s", e.rel, e.why) }++func splitPath(p string) []string {+ var out []string+ for _, c := range strings.Split(p, "/") {+ if c != "" && c != "." {+ out = append(out, c)+ }+ }+ return out+}++func readlinkFD(fd int) (string, error) {+ empty := []byte{0}+ for size := 256; size <= 1<<16; size *= 4 {+ buf := make([]byte, size)+ n, _, e := syscall.Syscall6(syscall.SYS_READLINKAT, uintptr(fd), uintptr(unsafe.Pointer(&empty[0])), uintptr(unsafe.Pointer(&buf[0])), uintptr(size), 0, 0)+ if e != 0 {+ return "", e+ }+ if int(n) < size {+ return string(buf[:n]), nil+ }+ }+ return "", syscall.ENAMETOOLONG+}++type openHow struct {+ Flags uint64+ Mode uint64+ Resolve uint64+}++func openat2(dirfd int, path string, flags int, resolve uint64) (int, error) {+ p, err := syscall.BytePtrFromString(path)+ if err != nil {+ return -1, err+ }+ how := openHow{Flags: uint64(flags), Resolve: resolve}+ fd, _, e := syscall.Syscall6(sysOpenat2, uintptr(dirfd), uintptr(unsafe.Pointer(p)), uintptr(unsafe.Pointer(&how)), unsafe.Sizeof(how), 0, 0)+ if e != 0 {+ return -1, e+ }+ return int(fd), nil+}++func sameFile(a, b int) bool {+ var sa, sb syscall.Stat_t+ if syscall.Fstat(a, &sa) != nil || syscall.Fstat(b, &sb) != nil {+ return false+ }+ return sa.Dev == sb.Dev && sa.Ino == sb.Ino+}++// walk opens rel in the root (O_PATH), following links inside it, and returns the descriptor+// and the path it really is at (no link in it). mkdirs: missing folders are made on the way+// (the last one too). allow: a first component of r.special this walk may go into.+func (r *rootFS) walk(rel string, mkdirs bool, allow string) (int, []string, error) {+ todo := splitPath(rel)+ var fds []int+ var names []string+ cur := func() int {+ if len(fds) == 0 {+ return r.fd+ }+ return fds[len(fds)-1]+ }+ closeAll := func() {+ for _, fd := range fds {+ _ = syscall.Close(fd)+ }+ fds, names = nil, nil+ }+ links := 0+ for len(todo) > 0 {+ c := todo[0]+ todo = todo[1:]+ if c == ".." {+ if len(fds) > 0 {+ _ = syscall.Close(fds[len(fds)-1])+ fds, names = fds[:len(fds)-1], names[:len(names)-1]+ }+ continue+ }+ if len(names) == 0 && r.special[c] && c != allow {+ closeAll()+ return -1, nil, &pathError{rel, "goes into /" + c}+ }+ fd, err := syscall.Openat(cur(), c, oPath|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, 0)+ if err == syscall.ENOENT && mkdirs {+ if err = syscall.Mkdirat(cur(), c, 0o755); err == nil || err == syscall.EEXIST {+ fd, err = syscall.Openat(cur(), c, oPath|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, 0)+ }+ }+ if err != nil {+ closeAll()+ return -1, nil, &os.PathError{Op: "open", Path: "/" + strings.Join(append(names, c), "/") + " (in the box)", Err: err}+ }+ var st syscall.Stat_t+ if err := syscall.Fstat(fd, &st); err != nil {+ _ = syscall.Close(fd)+ closeAll()+ return -1, nil, err+ }+ switch st.Mode & syscall.S_IFMT {+ case syscall.S_IFLNK:+ links+++ target, err := readlinkFD(fd)+ _ = syscall.Close(fd)+ if err == nil && links > maxLinks {+ err = syscall.ELOOP+ }+ if err == nil && target == "" {+ err = syscall.ENOENT+ }+ if err != nil {+ closeAll()+ return -1, nil, &os.PathError{Op: "link", Path: "/" + strings.Join(append(names, c), "/") + " (in the box)", Err: err}+ }+ if strings.HasPrefix(target, "/") {+ closeAll()+ }+ todo = append(splitPath(target), todo...)+ continue+ case syscall.S_IFDIR:+ default:+ if len(todo) > 0 {+ _ = syscall.Close(fd)+ closeAll()+ return -1, nil, &pathError{rel, "/" + strings.Join(append(names, c), "/") + " is not a folder"}+ }+ }+ fds, names = append(fds, fd), append(names, c)+ }+ var fd int+ if len(fds) == 0 {+ d, err := syscall.Dup(r.fd)+ if err != nil {+ return -1, nil, err+ }+ syscall.CloseOnExec(d)+ fd = d+ } else {+ fd = fds[len(fds)-1]+ for _, f := range fds[:len(fds)-1] {+ _ = syscall.Close(f)+ }+ }+ if err := r.check(fd, names); err != nil {+ _ = syscall.Close(fd)+ return -1, nil, err+ }+ return fd, names, nil+}++// check: the kernel opens the walked path in the root, with no link allowed, and it must be+// the same file (openat2, 5.6+; where it is missing or filtered the walk alone counts).+func (r *rootFS) check(fd int, names []string) error {+ if r.noOpenat2 {+ return nil+ }+ p := strings.Join(names, "/")+ if p == "" {+ p = "."+ }+ fd2, err := openat2(r.fd, p, oPath|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, resolveInRoot|resolveNoMagiclinks|resolveNoSymlinks)+ for i := 0; err == syscall.EAGAIN && i < 8; i++ {+ fd2, err = openat2(r.fd, p, oPath|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, resolveInRoot|resolveNoMagiclinks|resolveNoSymlinks)+ }+ if err == syscall.ENOSYS || err == syscall.EPERM || err == syscall.EINVAL || err == syscall.E2BIG {+ r.noOpenat2 = true+ return nil+ }+ if err != nil {+ return &pathError{p, "changed while it was opened: " + err.Error()}+ }+ defer syscall.Close(fd2)+ if !sameFile(fd, fd2) {+ return &pathError{p, "changed while it was opened"}+ }+ return nil+}++// parent: the folder rel is in (made when missing) and rel's last name.+func (r *rootFS) parent(rel string, allow string) (int, []string, string, error) {+ parts := splitPath(rel)+ if len(parts) == 0 || parts[len(parts)-1] == ".." {+ return -1, nil, "", &pathError{rel, "not a name"}+ }+ dir := strings.Join(parts[:len(parts)-1], "/")+ fd, names, err := r.walk(dir, true, allow)+ if err != nil {+ return -1, nil, "", err+ }+ name := parts[len(parts)-1]+ if len(names) == 0 && r.special[name] && name != allow {+ _ = syscall.Close(fd)+ return -1, nil, "", &pathError{rel, "goes into /" + name}+ }+ return fd, names, name, nil+}++// open: rel in the root, an existing one (links followed inside the root).+func (r *rootFS) open(rel string, allow string) (int, []string, error) {+ return r.walk(rel, false, allow)+}++// ensure makes rel a folder (dir) or a file in the root: a link there is removed first.+// Returns its descriptor (O_PATH) and where it is.+func (r *rootFS) ensure(rel string, dir bool, allow string) (int, []string, error) {+ pfd, names, name, err := r.parent(rel, allow)+ if err != nil {+ return -1, nil, err+ }+ defer syscall.Close(pfd)+ where := append(append([]string{}, names...), name)+ mode, err := statAt(pfd, name)+ if err == nil && mode == syscall.S_IFLNK {+ // The box's link (it is in the root: the parent was walked there): the real one goes in its place.+ if err := syscall.Unlinkat(pfd, name); err != nil {+ return -1, nil, &pathError{strings.Join(where, "/"), "a link that could not be removed: " + err.Error()}+ }+ err = syscall.ENOENT+ }+ switch {+ case err == syscall.ENOENT && dir:+ if err := syscall.Mkdirat(pfd, name, 0o755); err != nil && err != syscall.EEXIST {+ return -1, nil, &os.PathError{Op: "mkdir", Path: "/" + strings.Join(where, "/") + " (in the box)", Err: err}+ }+ case err == syscall.ENOENT:+ f, err := syscall.Openat(pfd, name, syscall.O_CREAT|syscall.O_EXCL|syscall.O_WRONLY|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, 0o644)+ if err != nil {+ return -1, nil, &os.PathError{Op: "create", Path: "/" + strings.Join(where, "/") + " (in the box)", Err: err}+ }+ _ = syscall.Close(f)+ case err != nil:+ return -1, nil, err+ case dir && mode != syscall.S_IFDIR:+ return -1, nil, &pathError{strings.Join(where, "/"), "is not a folder"}+ case !dir && mode != syscall.S_IFREG:+ return -1, nil, &pathError{strings.Join(where, "/"), "is not a file"}+ }+ fd, err := syscall.Openat(pfd, name, oPath|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, 0)+ if err != nil {+ return -1, nil, err+ }+ var st syscall.Stat_t+ if err := syscall.Fstat(fd, &st); err != nil || (dir && st.Mode&syscall.S_IFMT != syscall.S_IFDIR) || (!dir && st.Mode&syscall.S_IFMT != syscall.S_IFREG) {+ _ = syscall.Close(fd)+ return -1, nil, &pathError{strings.Join(where, "/"), "changed while it was made"}+ }+ if err := r.check(fd, where); err != nil {+ _ = syscall.Close(fd)+ return -1, nil, err+ }+ return fd, where, nil+}++// target: the path a mount call uses for fd: the descriptor itself, or (no /proc) the walked+// path, which has no link in it.+func (r *rootFS) target(fd int, names []string) string {+ if r.procFD {+ return fmt.Sprintf("/proc/self/fd/%d", fd)+ }+ return filepath.Join(append([]string{r.path}, names...)...)+}++// ensureDirIn / ensureFileIn: a folder or file of init's own at rel (see ensure).+func (r *rootFS) ensureDirIn(rel string) error {+ fd, _, err := r.ensure(rel, true, "")+ if err == nil {+ _ = syscall.Close(fd)+ }+ return err+}++func (r *rootFS) ensureFileIn(rel string) error {+ fd, _, err := r.ensure(rel, false, "")+ if err == nil {+ _ = syscall.Close(fd)+ }+ return err+}++// chmodIn: the mode of init's own folder or file at rel (a link there is not followed).+func (r *rootFS) chmodIn(rel string, mode uint32) error {+ pfd, _, name, err := r.parent(rel, "")+ if err != nil {+ return err+ }+ defer syscall.Close(pfd)+ fd, err := syscall.Openat(pfd, name, syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_CLOEXEC|syscall.O_NONBLOCK, 0)+ if err != nil {+ return err+ }+ defer syscall.Close(fd)+ return syscall.Fchmod(fd, mode)+}++// mountIn mounts on the folder or file at rel, made (dir/file) as init's own (see ensure).+func (r *rootFS) mountIn(rel string, dir bool, allow string, mount func(target string) error) error {+ fd, names, err := r.ensure(rel, dir, allow)+ if err != nil {+ return err+ }+ defer syscall.Close(fd)+ return mount(r.target(fd, names))+}++// bindIn binds src over the folder (dir) or file at rel in the root.+func (r *rootFS) bindIn(src, rel string, dir, ro, rec bool) error {+ fd, names, err := r.ensure(rel, dir, "")+ if err != nil {+ return err+ }+ err = r.bindAt(src, fd, names, ro, rec)+ _ = syscall.Close(fd)+ if err != nil {+ return fmt.Errorf("bind /%s: %w", strings.Join(names, "/"), err)+ }+ return nil+}++// bindAt binds src over fd (at names); read-only needs a remount of the new mount, opened again.+func (r *rootFS) bindAt(src string, fd int, names []string, ro, rec bool) error {+ flags := uintptr(syscall.MS_BIND)+ if rec {+ flags |= syscall.MS_REC+ }+ if err := syscall.Mount(src, r.target(fd, names), "", flags, ""); err != nil {+ return err+ }+ if !ro {+ return nil+ }+ // The mount just made: walked to again (the descriptor is the folder under it).+ top, err := r.reopen(names)+ if err != nil {+ return err+ }+ defer syscall.Close(top)+ // Flags locked by the parent namespace must be kept.+ var st syscall.Statfs_t+ extra := uintptr(0)+ if syscall.Fstatfs(top, &st) == nil {+ extra = uintptr(st.Flags) & (syscall.MS_NOSUID | syscall.MS_NODEV | syscall.MS_NOEXEC | syscall.MS_NOATIME | syscall.MS_RELATIME)+ }+ if err := syscall.Mount("", r.target(top, names), "", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|extra, ""); err != nil {+ return fmt.Errorf("read-only: %w", err)+ }+ return nil+}++// reopen: names again from the root (no link on the way: it was just walked), now the mount on it.+func (r *rootFS) reopen(names []string) (int, error) {+ if len(names) == 0 {+ return -1, errors.New("not the root itself")+ }+ fd, got, err := r.walk(strings.Join(names, "/"), false, names[0])+ if err != nil {+ return -1, err+ }+ if strings.Join(got, "/") != strings.Join(names, "/") {+ _ = syscall.Close(fd)+ return -1, &pathError{strings.Join(names, "/"), "changed while it was mounted"}+ }+ return fd, nil+}diff --git a/apps/desktop/box/go/cmd/boxrun/inroot_test.go b/apps/desktop/box/go/cmd/boxrun/inroot_test.gonew file mode 100644index 00000000..1320e715--- /dev/null+++ b/apps/desktop/box/go/cmd/boxrun/inroot_test.go@@ -0,0 +1,320 @@+//go:build linux++package main++import (+ "fmt"+ "os"+ "path/filepath"+ "reflect"+ "runtime"+ "sort"+ "strings"+ "syscall"+ "testing"+)++// treeOf lists a folder tree (kind, mode, size, link target, content) to prove it unchanged.+func treeOf(t *testing.T, dir string) []string {+ t.Helper()+ var out []string+ _ = filepath.Walk(dir, func(p string, fi os.FileInfo, err error) error {+ if err != nil {+ out = append(out, "err "+p)+ return nil+ }+ rel, _ := filepath.Rel(dir, p)+ line := fmt.Sprintf("%s %v %d", rel, fi.Mode(), fi.Size())+ if fi.Mode()&os.ModeSymlink != 0 {+ target, _ := os.Readlink(p)+ line += " -> " + target+ } else if fi.Mode().IsRegular() {+ data, _ := os.ReadFile(p)+ line += " " + string(data)+ }+ out = append(out, line)+ return nil+ })+ sort.Strings(out)+ return out+}++// Both ways of walking: the kernel's openat2 check where it exists, and the walk alone.+func eachWalk(t *testing.T, fn func(t *testing.T, noOpenat2 bool)) {+ for _, no := range []bool{false, true} {+ t.Run(fmt.Sprintf("noOpenat2=%v", no), func(t *testing.T) { fn(t, no) })+ }+}++func testRoot(t *testing.T, path string, noOpenat2 bool) *rootFS {+ t.Helper()+ r, err := openRoot(path)+ if err != nil {+ t.Fatal(err)+ }+ r.noOpenat2 = noOpenat2+ t.Cleanup(r.Close)+ return r+}++func TestInRootLinksNeverLeaveTheRoot(t *testing.T) {+ eachWalk(t, func(t *testing.T, noOpenat2 bool) {+ root, outside := t.TempDir(), t.TempDir()+ victimFile := filepath.Join(outside, "boxd")+ _ = os.WriteFile(victimFile, []byte("host file"), 0o644)+ _ = os.MkdirAll(filepath.Join(root, "etc"), 0o755)+ _ = os.MkdirAll(filepath.Join(root, "opt", "omnirush"), 0o755)+ // The box's links, each to the host: absolute, relative with "..", through a chain.+ _ = os.Symlink(filepath.Join(outside, "run"), filepath.Join(root, "run"))+ _ = os.Symlink(outside, filepath.Join(root, "tmp"))+ _ = os.Symlink(outside, filepath.Join(root, "opt", "omnirush", "bin"))+ _ = os.Symlink("../../../../../../../../.."+outside, filepath.Join(root, "proj"))+ _ = os.Symlink("hop2", filepath.Join(root, "hop1"))+ _ = os.Symlink(outside+"/chain", filepath.Join(root, "hop2"))+ _ = os.Symlink(victimFile, filepath.Join(root, "etc", "hosts"))+ _ = os.Symlink("loop", filepath.Join(root, "loop"))+ before := treeOf(t, outside)+ r := testRoot(t, root, noOpenat2)++ for _, d := range []string{"run", "tmp", "proj/sub/dir", "hop1/x", "opt/omnirush/bin"} {+ if err := r.ensureDirIn(d); err != nil {+ t.Errorf("ensureDirIn(%s): %v", d, err)+ }+ }+ if err := r.chmodIn("tmp", 0o1777); err != nil {+ t.Errorf("chmodIn(tmp): %v", err)+ }+ if err := r.ensureFileIn("opt/omnirush/bin/boxd"); err != nil {+ t.Errorf("ensureFileIn(boxd): %v", err)+ }+ if err := r.ensureFileIn("etc/hosts"); err != nil {+ t.Errorf("ensureFileIn(etc/hosts): %v", err)+ }+ if err := r.ensureDirIn("loop/x"); err == nil {+ t.Error("a link loop was walked")+ }+ if got := treeOf(t, outside); !reflect.DeepEqual(got, before) {+ t.Errorf("outside the root changed:\n%v\nwant\n%v", got, before)+ }+ if !noOpenat2 && r.noOpenat2 && kernelAtLeast(kernelRelease(), 5, 6) {+ t.Error("openat2 was not used on a 5.6+ kernel")+ }+ // Leaf links are replaced by real ones; links on the way are followed inside the root.+ for _, p := range []string{"run", "tmp", "opt/omnirush/bin", "etc/hosts"} {+ if fi, err := os.Lstat(filepath.Join(root, p)); err != nil || fi.Mode()&os.ModeSymlink != 0 {+ t.Errorf("%s: still a link or missing (%v)", p, err)+ }+ }+ if fi, err := os.Stat(filepath.Join(root, "tmp")); err != nil || fi.Mode().Perm() != 0o777 || fi.Mode()&os.ModeSticky == 0 {+ t.Errorf("tmp mode: %v %v", fi, err)+ }+ if fi, err := os.Stat(filepath.Join(root, "opt/omnirush/bin/boxd")); err != nil || !fi.Mode().IsRegular() {+ t.Errorf("boxd file: %v", err)+ }+ for _, p := range []string{outside + "/sub/dir", outside + "/chain/x"} {+ if fi, err := os.Stat(filepath.Join(root, p)); err != nil || !fi.IsDir() {+ t.Errorf("%s not made inside the root: %v", p, err)+ }+ }+ })+}++func TestInRootRefusals(t *testing.T) {+ eachWalk(t, func(t *testing.T, noOpenat2 bool) {+ root, outside := t.TempDir(), t.TempDir()+ _ = os.MkdirAll(filepath.Join(root, "proc", "1"), 0o755)+ _ = os.WriteFile(filepath.Join(root, "file"), nil, 0o644)+ _ = os.Symlink("/proc/1", filepath.Join(root, "toproc"))+ _ = os.Symlink("../proc", filepath.Join(root, "file2"))+ r := testRoot(t, root, noOpenat2)+ r.special["proc"] = true+ for _, p := range []string{"proc/1/x", "toproc/x", "file2/1", "proc", "file/x", "/", ".", "a/.."} {+ if err := r.ensureDirIn(p); err == nil {+ t.Errorf("ensureDirIn(%q) went on", p)+ }+ }+ if err := r.ensureDirIn("file"); err == nil || !strings.Contains(err.Error(), "not a folder") {+ t.Errorf("a file where a folder goes: %v", err)+ }+ // ".." stops at the root.+ if fd, names, err := r.open("../../..", ""); err != nil || len(names) != 0 || !sameFile(fd, r.fd) {+ t.Errorf("..: %v %v", names, err)+ } else {+ syscall.Close(fd)+ }+ if entries, _ := os.ReadDir(outside); len(entries) != 0 {+ t.Errorf("outside: %v", entries)+ }+ })+}++// imageLinksFile: an image's link to its own file is left; a dangling one, or one to a folder, is not.+func TestImageLinksFile(t *testing.T) {+ root, outside := t.TempDir(), t.TempDir()+ _ = os.MkdirAll(filepath.Join(root, "etc"), 0o755)+ _ = os.MkdirAll(filepath.Join(root, "run", "systemd"), 0o755)+ _ = os.WriteFile(filepath.Join(root, "run", "systemd", "stub"), []byte("nameserver 1.1.1.1\n"), 0o644)+ _ = os.WriteFile(filepath.Join(outside, "resolv"), []byte("host"), 0o644)+ r := testRoot(t, root, false)+ cases := map[string]bool{"/run/systemd/stub": true, "../run/systemd/stub": true, "/run/nothing": false, "/run": false, filepath.Join(outside, "resolv"): false}+ for target, want := range cases {+ link := filepath.Join(root, "etc", "resolv.conf")+ _ = os.Remove(link)+ _ = os.Symlink(target, link)+ if got := imageLinksFile(r, "etc/resolv.conf"); got != want {+ t.Errorf("link to %s: %v, want %v", target, got, want)+ }+ }+}++// prepareRoot as root on a real overlay whose upper layer (the box's, from earlier runs) has a+// link at every path init uses: nothing outside the root changes and the box still gets its mounts.+func TestPrepareRootWithPlantedLinks(t *testing.T) {+ if os.Geteuid() != 0 {+ t.Skip("needs root (mounts)")+ }+ eachWalk(t, func(t *testing.T, noOpenat2 bool) {+ dir, outside, src := t.TempDir(), t.TempDir(), t.TempDir()+ lower, upper, work, merged := filepath.Join(dir, "lower"), filepath.Join(dir, "upper"), filepath.Join(dir, "work"), filepath.Join(dir, "merged")+ for _, d := range []string{lower + "/etc/apt/apt.conf.d", lower + "/var/lib", upper, work, merged, src + "/proj/pkg", src + "/store/aaaaaaaaaaaa", src + "/store/bbbbbbbbbbbb", outside + "/v"} {+ _ = os.MkdirAll(d, 0o755)+ }+ _ = os.WriteFile(src+"/boxd", []byte("boxd binary"), 0o755)+ _ = os.WriteFile(src+"/resolv", []byte("nameserver 9.9.9.9\n"), 0o644)+ _ = os.WriteFile(src+"/hosts", []byte("127.0.0.1 x\n"), 0o644)+ _ = os.WriteFile(outside+"/v/file", []byte("keep"), 0o644)+ _ = os.WriteFile(src+"/proj/package.json", []byte("{}"), 0o644)+ // The box's links in the upper layer.+ plant := map[string]string{+ "run": outside + "/v",+ "tmp": outside + "/v",+ "sys": outside + "/v",+ "opt": outside + "/v",+ "etc": outside + "/v",+ "var/lib/omnirush": outside + "/v",+ "c": "../../../../../../.." + outside + "/v",+ "proc": outside + "/v/proc",+ }+ for p, target := range plant {+ _ = os.MkdirAll(filepath.Dir(filepath.Join(upper, p)), 0o755)+ if err := os.Symlink(target, filepath.Join(upper, p)); err != nil {+ t.Fatal(err)+ }+ }+ before := treeOf(t, outside)+ sp := spec{+ Boxd: src + "/boxd", Resolv: src + "/resolv", Hosts: src + "/hosts", AptRoot: true,+ Binds: []bindSpec{{Src: src + "/proj", Dst: "/c/Users/u/proj"}},+ ShadowStore: src + "/store", ShadowProject: "/c/Users/u/proj",+ Shadows: []bindSpec{{Src: src + "/store/aaaaaaaaaaaa", Dst: "/c/Users/u/proj/node_modules"}, {Src: src + "/store/bbbbbbbbbbbb", Dst: "/c/Users/u/proj/pkg/node_modules"}},+ }+ type result struct {+ on, made []string+ mounts string+ files map[string]string+ err error+ }+ done := make(chan result)+ go func() {+ runtime.LockOSThread() // never unlocked: the thread ends with its namespace+ if err := syscall.Unshare(syscall.CLONE_NEWNS); err != nil {+ done <- result{err: err}+ return+ }+ if err := syscall.Mount("", "/", "", syscall.MS_REC|syscall.MS_PRIVATE, ""); err != nil {+ done <- result{err: err}+ return+ }+ if _, err := mountOverlay(lower, upper, work, merged, ""); err != nil {+ done <- result{err: err}+ return+ }+ saved := forceNoOpenat2+ forceNoOpenat2 = noOpenat2+ on, made, err := prepareRoot(sp, merged)+ forceNoOpenat2 = saved+ res := result{on: on, made: made, err: err, files: map[string]string{}}+ if err == nil {+ // Read as the box will (in the root): a link there is the box's own business.+ rfs, _ := openRoot(merged)+ for _, p := range []string{"opt/omnirush/bin/boxd", "etc/resolv.conf", "etc/hosts", "c/Users/u/proj/package.json", "c/Users/u/proj/node_modules", "c/Users/u/proj/pkg/node_modules", "proc/self/status"} {+ fd, _, err := rfs.open(p, "proc")+ if err != nil {+ res.files[p] = err.Error()+ continue+ }+ var st syscall.Stat_t+ _ = syscall.Fstat(fd, &st)+ if st.Mode&syscall.S_IFMT == syscall.S_IFDIR {+ entries, _ := os.ReadDir(fmt.Sprintf("/proc/self/fd/%d", fd))+ res.files[p] = fmt.Sprintf("dir %d", len(entries))+ } else {+ data, err := os.ReadFile(fmt.Sprintf("/proc/self/fd/%d", fd))+ res.files[p] = fmt.Sprint(len(data) > 0, " ", strings.SplitN(string(data), "\n", 2)[0], err)+ }+ syscall.Close(fd)+ }+ rfs.Close()+ }+ data, _ := os.ReadFile("/proc/thread-self/mountinfo") // this thread's namespace+ res.mounts = string(data)+ done <- res+ }()+ r := <-done+ if r.err != nil {+ t.Fatalf("prepareRoot: %v", r.err)+ }+ if got := treeOf(t, outside); !reflect.DeepEqual(got, before) {+ t.Errorf("outside the root changed:\n%v\nwant\n%v", got, before)+ }+ want := map[string]string{+ "opt/omnirush/bin/boxd": "true boxd binary<nil>",+ "etc/resolv.conf": "true nameserver 9.9.9.9<nil>",+ "etc/hosts": "true 127.0.0.1 x<nil>",+ "c/Users/u/proj/package.json": "true {}<nil>",+ "c/Users/u/proj/node_modules": "dir 0",+ "c/Users/u/proj/pkg/node_modules": "dir 0",+ "proc/self/status": "true Name:\tgo-test-binary<nil>",+ }+ for k, v := range want {+ if k == "proc/self/status" {+ if !strings.HasPrefix(r.files[k], "true Name:") {+ t.Errorf("%s in the box: %q", k, r.files[k])+ }+ continue+ }+ if r.files[k] != v {+ t.Errorf("%s in the box: %q, want %q", k, r.files[k], v)+ }+ }+ // Every mount init made is in the root; none is on the host's folder.+ n := 0+ for _, line := range strings.Split(r.mounts, "\n") {+ f := strings.Fields(line)+ if len(f) < 5 {+ continue+ }+ if strings.HasPrefix(f[4], outside) {+ t.Errorf("a mount on the host's folder: %s", line)+ }+ if strings.HasPrefix(f[4], merged+"/") {+ n+++ }+ }+ if n < 8 {+ t.Errorf("only %d mounts in the root:\n%s", n, r.mounts)+ }+ if !strings.Contains(r.mounts, " "+merged+"/proc ") || !strings.Contains(r.mounts, " "+merged+"/sys ") || !strings.Contains(r.mounts, " "+merged+"/dev ") {+ t.Errorf("proc/dev/sys not on the real folders")+ }+ if !reflect.DeepEqual(r.on, []string{"/c/Users/u/proj/node_modules", "/c/Users/u/proj/pkg/node_modules"}) {+ t.Errorf("shadows: %v made %v", r.on, r.made)+ }+ // Init's own folders: the box's links there are gone (a link on the way is followed in the root).+ for _, p := range []string{"run", "tmp", "proc", "sys"} {+ if fi, err := os.Lstat(filepath.Join(upper, p)); err != nil || fi.Mode()&os.ModeSymlink != 0 || !fi.IsDir() {+ t.Errorf("upper %s: not a real folder (%v)", p, err)+ }+ }+ })+}diff --git a/apps/desktop/box/go/cmd/boxrun/shadows.go b/apps/desktop/box/go/cmd/boxrun/shadows.goindex 55323e81..d7dcbc98 100644--- a/apps/desktop/box/go/cmd/boxrun/shadows.go+++ b/apps/desktop/box/go/cmd/boxrun/shadows.go@@ -213,58 +213,72 @@ func union(a, b []string) []string {// bindShadows (init, as root, before the root switch): the store at shadowStore, then each// shadow over its folder. A folder that cannot be shadowed is left as it is (never fails the box).-// Returns the inside paths shadowed and the ones made here as mount points.-func bindShadows(sp spec, in func(string) string) (on, made []string) {+// Every path is walked in the box's root (inroot.go), and a shadow's folder must really be in the+// project's. Returns the inside paths shadowed and the ones made here as mount points.+func bindShadows(sp spec, r *rootFS) (on, made []string) {if sp.ShadowStore == "" || len(sp.Shadows) == 0 || sp.ShadowProject == "" {return nil, nil}- if err := ensureDir(in(shadowStore)); err != nil {+ if err := r.bindIn(sp.ShadowStore, shadowStore, true, false, false); err != nil {fmt.Fprintf(os.Stderr, "boxrun: shadows: %v\n", err)return nil, nil}- if err := bind(sp.ShadowStore, in(shadowStore), false, false); err != nil {- fmt.Fprintf(os.Stderr, "boxrun: shadows: %v\n", err)- return nil, nil- }- projReal, err := filepath.EvalSymlinks(in(sp.ShadowProject))+ pfd, projNames, err := r.open(sp.ShadowProject, "")if err != nil {fmt.Fprintf(os.Stderr, "boxrun: shadows: %v\n", err)return nil, nil}+ _ = syscall.Close(pfd)+ projReal := "/" + strings.Join(projNames, "/")for _, b := range sp.Shadows {if b.Dst == sp.ShadowProject || !within(b.Dst, sp.ShadowProject) {continue}- target := in(b.Dst)// A link in the project (written by the box) must not take the folder elsewhere.- parentReal, err := filepath.EvalSymlinks(filepath.Dir(target))- if err != nil || !within(parentReal, projReal) {+ dfd, dirNames, err := r.open(filepath.Dir(b.Dst), "")+ if err != nil {+ continue+ }+ parentReal := "/" + strings.Join(dirNames, "/")+ name := filepath.Base(b.Dst)+ if !within(parentReal, projReal) || name == "." || name == ".." || name == "/" {+ _ = syscall.Close(dfd)continue}- target = filepath.Join(parentReal, filepath.Base(target))+ names := append(append([]string{}, dirNames...), name)madeHere := false- if fi, err := os.Lstat(target); err != nil {+ mode, err := statAt(dfd, name)+ if err == syscall.ENOENT {// Only the leaf is made (its parent is the project's folder, e.g. next to package.json).- if parent, err := os.Lstat(parentReal); err != nil || !parent.IsDir() {- continue- }- if os.Mkdir(target, 0o755) != nil {+ if syscall.Mkdirat(dfd, name, 0o755) != nil {+ _ = syscall.Close(dfd)continue}madeHere = true- } else if !fi.IsDir() {+ } else if err != nil || mode != syscall.S_IFDIR {+ _ = syscall.Close(dfd)continue}- if err := bind(b.Src, target, false, false); err != nil {+ fd, err := syscall.Openat(dfd, name, oPath|syscall.O_NOFOLLOW|syscall.O_DIRECTORY|syscall.O_CLOEXEC, 0)+ if err == nil {+ err = r.check(fd, names)+ if err == nil {+ err = r.bindAt(b.Src, fd, names, false, false)+ }+ _ = syscall.Close(fd)+ }+ if err != nil {fmt.Fprintf(os.Stderr, "boxrun: shadow %s: %v\n", b.Dst, err)if madeHere {- _ = os.Remove(target)+ _ = rmdirAt(dfd, name)}+ _ = syscall.Close(dfd)continue}+ _ = syscall.Close(dfd)if madeHere {// Recorded where it really is (a link inside the project may have led there).- if rel, err := filepath.Rel(projReal, target); err == nil && rel != "." && !hasDotDot(rel) {+ if rel, err := filepath.Rel(projReal, "/"+strings.Join(names, "/")); err == nil && rel != "." && !hasDotDot(rel) {made = append(made, filepath.Join(sp.ShadowProject, rel))}}diff --git a/apps/desktop/box/go/cmd/boxrun/shadows_test.go b/apps/desktop/box/go/cmd/boxrun/shadows_test.goindex ae66b85f..abb57595 100644--- a/apps/desktop/box/go/cmd/boxrun/shadows_test.go+++ b/apps/desktop/box/go/cmd/boxrun/shadows_test.go@@ -297,7 +297,6 @@ func TestBindShadowsStaysInTheProject(t *testing.T) {{Src: filepath.Join(store, "bbbbbbbbbbbb"), Dst: "/c/p/evil/node_modules"},{Src: filepath.Join(store, "cccccccccccc"), Dst: "/elsewhere/node_modules"},}}- in := func(p string) string { return filepath.Join(rootfs, p) }type result struct {on, made []stringerr error@@ -313,7 +312,13 @@ func TestBindShadowsStaysInTheProject(t *testing.T) {done <- result{err: err}return}- on, made := bindShadows(sp, in)+ r, err := openRoot(rootfs)+ if err != nil {+ done <- result{err: err}+ return+ }+ defer r.Close()+ on, made := bindShadows(sp, r)done <- result{on: on, made: made}}()r := <-done