From ebb1144efc6c09dff8a732c5baf11db5b8f515b2 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Thu, 27 Aug 2026 15:35:55 +0900 Subject: container: explicitly order initialisation This avoids accessing uninitialised hostProc in some build modes. Signed-off-by: Ophestra --- container/init.go | 2 ++ container/mount.go | 4 ++-- container/path.go | 36 ++++++++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/container/init.go b/container/init.go index 510d9f59..07fff357 100644 --- a/container/init.go +++ b/container/init.go @@ -644,6 +644,8 @@ func initEntrypoint(k syscallDispatcher, msg message.Msg) { const initName = "init" var _ = func() struct{} { + _ = hostProc + for _, v := range []any{ (*AutoEtcOp)(nil), (*AutoRootOp)(nil), diff --git a/container/mount.go b/container/mount.go index 37a9445c..84415fa8 100644 --- a/container/mount.go +++ b/container/mount.go @@ -95,7 +95,7 @@ const ( ) // bindMount mounts source on target and recursively applies flags if MS_REC is set. -func (p *procPaths) bindMount(msg message.Msg, source, target string, flags uintptr) error { +func (p procPaths) bindMount(msg message.Msg, source, target string, flags uintptr) error { // syscallDispatcher.bindMount and procPaths.remount must not be called from this function if err := p.k.mount(source, target, FstypeNULL, MS_SILENT|MS_BIND|flags&MS_REC, zeroString); err != nil { @@ -105,7 +105,7 @@ func (p *procPaths) bindMount(msg message.Msg, source, target string, flags uint } // remount applies flags on target, recursively if MS_REC is set. -func (p *procPaths) remount(msg message.Msg, target string, flags uintptr) error { +func (p procPaths) remount(msg message.Msg, target string, flags uintptr) error { // syscallDispatcher methods bindMount, remount must not be called from this function var targetFinal string diff --git a/container/path.go b/container/path.go index 5ae52dee..8d91abd7 100644 --- a/container/path.go +++ b/container/path.go @@ -21,22 +21,29 @@ const ( // mounted at all. Neither configuration is supported by this package. Nonexistent = fhs.Proc + "nonexistent" - hostPath = fhs.Root + hostDir - hostDir = "host" + // hostPath is the pathname where the host root filesystem is mounted. + hostPath = fhs.Root + hostDir + // hostDir is the name of hostPath. + hostDir = "host" + // sysrootPath is the pathname where the new root filesystem is mounted. sysrootPath = fhs.Root + sysrootDir - sysrootDir = "sysroot" + // sysrootDir is the name of sysrootPath. + sysrootDir = "sysroot" ) +// toSysroot prefixes name with sysrootPath. func toSysroot(name string) string { name = strings.TrimLeftFunc(name, func(r rune) bool { return r == '/' }) return filepath.Join(sysrootPath, name) } +// toHost prefixes name with hostPath. func toHost(name string) string { name = strings.TrimLeftFunc(name, func(r rune) bool { return r == '/' }) return filepath.Join(hostPath, name) } +// createFile places a file at name. func createFile(name string, perm, pperm os.FileMode, content []byte) error { if err := os.MkdirAll(filepath.Dir(name), pperm); err != nil { return err @@ -51,6 +58,7 @@ func createFile(name string, perm, pperm os.FileMode, content []byte) error { return errors.Join(f.Close(), err) } +// ensureFile ensures the existence of a file at name. func ensureFile(name string, perm, pperm os.FileMode) error { fi, err := os.Stat(name) if err != nil { @@ -66,21 +74,26 @@ func ensureFile(name string, perm, pperm os.FileMode) error { return err } -var hostProc = newProcPaths(direct{}, hostPath) - -func newProcPaths(k syscallDispatcher, prefix string) *procPaths { - return &procPaths{k, prefix + "/proc", prefix + "/proc/self"} +// newProcPaths returns a populated procPaths. +func newProcPaths(k syscallDispatcher, prefix string) procPaths { + return procPaths{k, prefix + "/proc", prefix + "/proc/self"} } +// procPaths describes [fhs.Proc] paths. type procPaths struct { k syscallDispatcher prefix string self string } -func (p *procPaths) stdout() string { return p.self + "/fd/1" } -func (p *procPaths) fd(fd int) string { return p.self + "/fd/" + strconv.Itoa(fd) } -func (p *procPaths) mountinfo(f func(d *vfs.MountInfoDecoder) error) error { +// stdout returns the pathname of the magic symlink representing standard output. +func (p procPaths) stdout() string { return p.self + "/fd/1" } + +// fd returns the pathname of the magic symlink representing the specified file. +func (p procPaths) fd(fd int) string { return p.self + "/fd/" + strconv.Itoa(fd) } + +// mountinfo returns the pathname of the mountinfo file. +func (p procPaths) mountinfo(f func(d *vfs.MountInfoDecoder) error) error { if r, err := p.k.openNew(p.self + "/mountinfo"); err != nil { return err } else { @@ -94,3 +107,6 @@ func (p *procPaths) mountinfo(f func(d *vfs.MountInfoDecoder) error) error { return err0 } } + +// hostProc is a procPaths instance referring to [fhs.Proc] located in hostPath. +var hostProc = newProcPaths(direct{}, hostPath) -- cgit v1.3.1