From 7aa1c5a28bea47117c6198caffa03f2bda85275b Mon Sep 17 00:00:00 2001 From: criyle Date: Sun, 25 May 2025 22:02:23 -0400 Subject: [PATCH] refactor: normalize error message & replace path with filepath --- cmd/runprog/main_darwin.go | 2 +- cmd/runprog/main_linux.go | 12 ++++---- container/container_cmd_linux.go | 6 +++- container/container_exec_linux.go | 10 +++--- container/container_init_linux.go | 32 ++++++++++---------- container/environment_linux.go | 16 +++++----- container/host_cmd_linux.go | 16 +++++----- container/socket_linux.go | 12 ++++---- container/utils.go | 8 ++--- pkg/cgroup/cgroup_info_linux.go | 4 +-- pkg/cgroup/cgroup_linux.go | 16 +++++----- pkg/cgroup/utils_linux.go | 14 ++++----- pkg/cgroup/v1_linux.go | 7 ++--- pkg/cgroup/v1controller_linux.go | 8 ++--- pkg/cgroup/v2_linux.go | 22 +++++++------- pkg/memfd/memfd_other.go | 2 +- pkg/seccomp/libseccomp/syscall_name_linux.go | 2 +- pkg/unixsocket/socket_linux.go | 12 ++++---- runner/ptrace/handle_linux.go | 8 ++--- 19 files changed, 106 insertions(+), 103 deletions(-) diff --git a/cmd/runprog/main_darwin.go b/cmd/runprog/main_darwin.go index 3f5476b..03e522e 100644 --- a/cmd/runprog/main_darwin.go +++ b/cmd/runprog/main_darwin.go @@ -117,7 +117,7 @@ func start() (*runner.Result, error) { if profilePath != "" { c, err := os.ReadFile(profilePath) if err != nil { - return nil, fmt.Errorf("profile: %v", err) + return nil, fmt.Errorf("profile: %w", err) } profile = string(c) } diff --git a/cmd/runprog/main_linux.go b/cmd/runprog/main_linux.go index a1d4593..da73938 100644 --- a/cmd/runprog/main_linux.go +++ b/cmd/runprog/main_linux.go @@ -237,11 +237,11 @@ func start() (*runner.Result, error) { if memfile { fin, err := os.Open(args[0]) if err != nil { - return nil, fmt.Errorf("failed to open args[0]: %v", err) + return nil, fmt.Errorf("failed to open args[0]: %w", err) } execf, err := memfd.DupToMemfd("run_program", fin) if err != nil { - return nil, fmt.Errorf("dup to memfd failed: %v", err) + return nil, fmt.Errorf("dup to memfd failed: %w", err) } fin.Close() defer execf.Close() @@ -252,7 +252,7 @@ func start() (*runner.Result, error) { // open input / output / err files files, err := prepareFiles(inputFileName, outputFileName, errorFileName) if err != nil { - return nil, fmt.Errorf("failed to prepare files: %v", err) + return nil, fmt.Errorf("failed to prepare files: %w", err) } defer closeFiles(files) @@ -295,7 +295,7 @@ func start() (*runner.Result, error) { if !unsafe || runt != "container" { filter, err = builder.Build() if err != nil { - return nil, fmt.Errorf("failed to create seccomp filter %v", err) + return nil, fmt.Errorf("failed to create seccomp filter: %w", err) } } @@ -329,12 +329,12 @@ func start() (*runner.Result, error) { m, err := b.Build() if err != nil { - return nil, fmt.Errorf("failed to new container: %v", err) + return nil, fmt.Errorf("failed to new container: %w", err) } defer m.Destroy() err = m.Ping() if err != nil { - return nil, fmt.Errorf("failed to ping container: %v", err) + return nil, fmt.Errorf("failed to ping container: %w", err) } if unsafe { filter = nil diff --git a/container/container_cmd_linux.go b/container/container_cmd_linux.go index aac8700..ac500b6 100644 --- a/container/container_cmd_linux.go +++ b/container/container_cmd_linux.go @@ -47,6 +47,9 @@ func (c *containerServer) handleOpen(open []OpenCmd) error { for _, o := range open { outFile, err := os.OpenFile(o.Path, o.Flag, o.Perm) if err != nil { + for _, f := range fileToClose { + f.Close() + } return c.sendErrorReply("open: %v", err) } fileToClose = append(fileToClose, outFile) @@ -85,6 +88,7 @@ func readDotEnv() ([]string, error) { if errors.Is(err, os.ErrNotExist) { return nil, nil } + return nil, fmt.Errorf("dotenv: open /.env: %w", err) } defer f.Close() @@ -96,7 +100,7 @@ func readDotEnv() ([]string, error) { continue } if !strings.Contains(line, "=") { - return nil, fmt.Errorf("dotenv: invalid line %s", line) + return nil, fmt.Errorf("dotenv: invalid line: %s", line) } ret = append(ret, line) } diff --git a/container/container_exec_linux.go b/container/container_exec_linux.go index fad6dc9..3d31765 100644 --- a/container/container_exec_linux.go +++ b/container/container_exec_linux.go @@ -66,14 +66,14 @@ func (c *containerServer) handleExecve(cmd *execCmd, msg unixsocket.Msg) error { }, } if err := c.sendReply(reply{}, msg); err != nil { - return fmt.Errorf("syncFunc: sendReply %v", err) + return fmt.Errorf("sync func: send reply: %w", err) } cmd, _, err := c.recvCmd() if err != nil { - return fmt.Errorf("syncFunc: recvCmd %v", err) + return fmt.Errorf("sync func: recv cmd: %w", err) } if cmd.Cmd == cmdKill { - return fmt.Errorf("syncFunc: received kill") + return fmt.Errorf("sync func: received kill") } return nil } @@ -179,7 +179,7 @@ func convertReply(ret waitPidResult) reply { if ret.Err != nil { return reply{ Error: &errorReply{ - Msg: fmt.Sprintf("execve: wait4 %v", ret.Err), + Msg: fmt.Sprintf("execve: wait4: %v", ret.Err), }, } } @@ -229,7 +229,7 @@ func convertReply(ret waitPidResult) reply { default: return reply{ Error: &errorReply{ - Msg: fmt.Sprintf("execve: unknown status %v", waitStatus), + Msg: fmt.Sprintf("execve: unknown status: %v", waitStatus), }, } } diff --git a/container/container_init_linux.go b/container/container_init_linux.go index dc438d3..d0dd70a 100644 --- a/container/container_init_linux.go +++ b/container/container_init_linux.go @@ -88,14 +88,14 @@ func Init() (err error) { // ensure there's no fd leak to child process (e.g. VSCode leaks ptmx fd) if err := closeOnExecAllFds(); err != nil { - return fmt.Errorf("container_init: failed to close_on_exec all fd %v", err) + return fmt.Errorf("container_init: failed to close on exec all fds: %w", err) } // new_container environment shared the socket at fd 3 (marked close_exec) const defaultFd = 3 soc, err := unixsocket.NewSocket(defaultFd) if err != nil { - return fmt.Errorf("container_init: failed to new socket %v", err) + return fmt.Errorf("container_init: failed to create new socket: %w", err) } // serve forever @@ -197,10 +197,10 @@ func (c *containerServer) serve() error { for { cmd, msg, err := c.recvCmd() if err != nil { - return fmt.Errorf("serve: recvCmd %v", err) + return fmt.Errorf("serve: recvCmd: %w", err) } if err := c.handleCmd(cmd, msg); err != nil { - return fmt.Errorf("serve: failed to execute cmd %v", err) + return fmt.Errorf("serve: failed to execute cmd: %w", err) } } } @@ -255,53 +255,53 @@ func initFileSystem(c containerConfig) error { // mount tmpfs as root const tmpfs = "tmpfs" if err := syscall.Mount(tmpfs, c.ContainerRoot, tmpfs, 0, ""); err != nil { - return fmt.Errorf("init_fs: mount / %v", err) + return fmt.Errorf("init_fs: mount /: %w", err) } // change dir to container root if err := syscall.Chdir(c.ContainerRoot); err != nil { - return fmt.Errorf("init_fs: chdir %v", err) + return fmt.Errorf("init_fs: chdir: %w", err) } // performing mounts for _, m := range c.Mounts { if err := m.Mount(); err != nil { - return fmt.Errorf("init_fs: mount %v %v", m, err) + return fmt.Errorf("init_fs: mount %v: %w", m, err) } } // pivot root const oldRoot = "old_root" if err := os.Mkdir(oldRoot, 0755); err != nil { - return fmt.Errorf("init_fs: mkdir(old_root) %v", err) + return fmt.Errorf("init_fs: mkdir old_root: %w", err) } if err := syscall.PivotRoot(c.ContainerRoot, oldRoot); err != nil { - return fmt.Errorf("init_fs: pivot_root(%s, %s) %v", c.ContainerRoot, oldRoot, err) + return fmt.Errorf("init_fs: pivot_root(%s, %s): %w", c.ContainerRoot, oldRoot, err) } if err := syscall.Unmount(oldRoot, syscall.MNT_DETACH); err != nil { - return fmt.Errorf("init_fs: unmount(old_root) %v", err) + return fmt.Errorf("init_fs: unmount old_root: %w", err) } if err := os.Remove(oldRoot); err != nil { - return fmt.Errorf("init_fs: unlink(old_root) %v", err) + return fmt.Errorf("init_fs: unlink old_root: %w", err) } // create symlinks for _, l := range c.SymbolicLinks { // ensure dir exists dir := filepath.Dir(l.LinkPath) if err := os.MkdirAll(dir, 0755); err != nil { - return fmt.Errorf("init_fs: mkdir_all(%s) %v", dir, err) + return fmt.Errorf("init_fs: mkdir_all(%s): %w", dir, err) } if err := os.Symlink(l.Target, l.LinkPath); err != nil { - return fmt.Errorf("init_fs: symlink %v", err) + return fmt.Errorf("init_fs: symlink: %w", err) } } // mask paths for _, p := range c.MaskPaths { if err := maskPath(p); err != nil { - return fmt.Errorf("init_fs: mask path %v", err) + return fmt.Errorf("init_fs: mask path: %w", err) } } // readonly root const remountFlag = syscall.MS_BIND | syscall.MS_REMOUNT | syscall.MS_RDONLY | syscall.MS_NOATIME | syscall.MS_NOSUID if err := syscall.Mount(tmpfs, "/", tmpfs, remountFlag, ""); err != nil { - return fmt.Errorf("init_fs: readonly remount / %v", err) + return fmt.Errorf("init_fs: readonly remount /: %w", err) } return nil } @@ -368,7 +368,7 @@ func maskPath(path string) error { // otherwise, mount tmpfs to mask it return syscall.Mount("tmpfs", path, "tmpfs", syscall.MS_RDONLY, "") } - return err + return fmt.Errorf("mask path: %w", err) } return nil } diff --git a/container/environment_linux.go b/container/environment_linux.go index cd4aa83..234e571 100644 --- a/container/environment_linux.go +++ b/container/environment_linux.go @@ -127,7 +127,7 @@ func (b *Builder) Build() (Environment, error) { // avoid non cinit enabled executable running as container init process if err = c.Ping(); err != nil { c.Destroy() - return nil, fmt.Errorf("container: container init not responding to ping %v", err) + return nil, fmt.Errorf("container: init not responding to ping: %w", err) } // container mount points @@ -154,13 +154,13 @@ func (b *Builder) Build() (Environment, error) { root := b.Root if b.TmpRoot != "" { if root, err = os.MkdirTemp(b.Root, b.TmpRoot); err != nil { - return nil, fmt.Errorf("container: failed to make tmp container root at %s %v", b.Root, err) + return nil, fmt.Errorf("container: failed to make tmp container root at %s: %w", b.Root, err) } defer os.Remove(root) } if root == "" { if root, err = os.Getwd(); err != nil { - return nil, fmt.Errorf("container: failed to get work directory %v", err) + return nil, fmt.Errorf("container: failed to get work directory: %w", err) } } workDir := containerWD @@ -206,14 +206,14 @@ func (b *Builder) startContainer() (*container, error) { // prepare host <-> container unix socket ins, outs, err := newPassCredSocketPair() if err != nil { - return nil, fmt.Errorf("container: failed to create socket: %v", err) + return nil, fmt.Errorf("container: failed to create socket: %w", err) } defer outs.Close() outf, err := outs.File() if err != nil { ins.Close() - return nil, fmt.Errorf("container: failed to dup container socket fd %v", err) + return nil, fmt.Errorf("container: failed to dup container socket fd: %w", err) } defer outf.Close() @@ -258,7 +258,7 @@ func (b *Builder) startContainer() (*container, error) { } if err = r.Start(); err != nil { ins.Close() - return nil, fmt.Errorf("container: failed to start container %v", err) + return nil, fmt.Errorf("container: failed to start container: %w", err) } c := &container{ process: r.Process, @@ -386,10 +386,10 @@ func (b *Builder) getIDMapping(cred *syscall.Credential) ([]syscall.SysProcIDMap func (c *container) recvAckReply(name string) error { reply, _, err := c.recvReply() if err != nil { - return fmt.Errorf("%v: recvAck %v", name, err) + return fmt.Errorf("%s: recv ack: %w", name, err) } if reply.Error != nil { - return fmt.Errorf("%v: container error %v", name, reply.Error) + return fmt.Errorf("%s: container error: %v", name, reply.Error) } return nil } diff --git a/container/host_cmd_linux.go b/container/host_cmd_linux.go index 8990cb4..62a14d1 100644 --- a/container/host_cmd_linux.go +++ b/container/host_cmd_linux.go @@ -24,7 +24,7 @@ func (c *container) Ping() error { Cmd: cmdPing, } if err := c.sendCmd(cmd, unixsocket.Msg{}); err != nil { - return fmt.Errorf("ping: %v", err) + return fmt.Errorf("ping: %w", err) } // receive no error return c.recvAckReply("ping") @@ -40,7 +40,7 @@ func (c *container) conf(conf *containerConfig) error { ConfCmd: &confCmd{Conf: *conf}, } if err := c.sendCmd(cmd, unixsocket.Msg{}); err != nil { - return fmt.Errorf("conf: %v", err) + return fmt.Errorf("conf: %w", err) } return c.recvAckReply("conf") } @@ -59,18 +59,18 @@ func (c *container) Open(p []OpenCmd) ([]*os.File, error) { OpenCmd: p, } if err := c.sendCmd(cmd, unixsocket.Msg{}); err != nil { - return nil, fmt.Errorf("open: %v", err) + return nil, fmt.Errorf("open: %w", err) } reply, msg, err := c.recvReply() if err != nil { - return nil, fmt.Errorf("open: %v", err) + return nil, fmt.Errorf("open: %w", err) } if reply.Error != nil { return nil, fmt.Errorf("open: %v", reply.Error) } if len(msg.Fds) != len(p) { closeFds(msg.Fds) - return nil, fmt.Errorf("open: unexpected number of fd %v / %v", len(msg.Fds), len(p)) + return nil, fmt.Errorf("open: unexpected number of fds: got %d, want %d", len(msg.Fds), len(p)) } ret := make([]*os.File, 0, len(p)) @@ -79,7 +79,7 @@ func (c *container) Open(p []OpenCmd) ([]*os.File, error) { f := os.NewFile(uintptr(fd), p[i].Path) if f == nil { closeFds(msg.Fds) - return nil, fmt.Errorf("open: failed NewFile %v", fd) + return nil, fmt.Errorf("open: failed to create file for fd: %d", fd) } ret = append(ret, f) } @@ -96,7 +96,7 @@ func (c *container) Delete(p string) error { DeleteCmd: &deleteCmd{Path: p}, } if err := c.sendCmd(cmd, unixsocket.Msg{}); err != nil { - return fmt.Errorf("delete: %v", err) + return fmt.Errorf("delete: %w", err) } return c.recvAckReply("delete") } @@ -110,7 +110,7 @@ func (c *container) Reset() error { Cmd: cmdReset, } if err := c.sendCmd(cmd, unixsocket.Msg{}); err != nil { - return fmt.Errorf("reset: %v", err) + return fmt.Errorf("reset: %w", err) } return c.recvAckReply("reset") } diff --git a/container/socket_linux.go b/container/socket_linux.go index a9e5ed1..483b66b 100644 --- a/container/socket_linux.go +++ b/container/socket_linux.go @@ -43,27 +43,27 @@ func newSocket(s *unixsocket.Socket) *socket { return &soc } -func (s *socket) RecvMsg(e interface{}) (msg unixsocket.Msg, err error) { +func (s *socket) RecvMsg(e any) (msg unixsocket.Msg, err error) { n, msg, err := s.Socket.RecvMsg(s.buff) if err != nil { - return msg, fmt.Errorf("RecvMsg: %v", err) + return msg, fmt.Errorf("recv msg: %w", err) } s.recvBuff.Rotate(bytes.NewBuffer(s.buff[:n])) if err := s.decoder.Decode(e); err != nil { - return msg, fmt.Errorf("RecvMsg: failed to decode %v", err) + return msg, fmt.Errorf("recv msg: decode: %w", err) } return msg, nil } -func (s *socket) SendMsg(e interface{}, msg unixsocket.Msg) error { +func (s *socket) SendMsg(e any, msg unixsocket.Msg) error { s.sendBuff.Reset() if err := s.encoder.Encode(e); err != nil { - return fmt.Errorf("SendMsg: failed to encode %v", err) + return fmt.Errorf("send msg: encode: %w", err) } if err := s.Socket.SendMsg(s.sendBuff.Bytes(), msg); err != nil { - return fmt.Errorf("SendMsg: failed to SendMsg %v", err) + return fmt.Errorf("send msg: %w", err) } return nil } diff --git a/container/utils.go b/container/utils.go index 92502cb..3e81728 100644 --- a/container/utils.go +++ b/container/utils.go @@ -2,7 +2,7 @@ package container import ( "os" - "path" + "path/filepath" "syscall" ) @@ -54,10 +54,10 @@ func removeContents(dir string) error { } for _, name := range names { - err = os.RemoveAll(path.Join(dir, name)) + err1 := os.RemoveAll(filepath.Join(dir, name)) if err != nil { - return err + err = err1 } } - return nil + return err } diff --git a/pkg/cgroup/cgroup_info_linux.go b/pkg/cgroup/cgroup_info_linux.go index e8805ed..64abd47 100644 --- a/pkg/cgroup/cgroup_info_linux.go +++ b/pkg/cgroup/cgroup_info_linux.go @@ -4,7 +4,7 @@ import ( "bufio" "fmt" "os" - "path" + "path/filepath" "strconv" "strings" ) @@ -178,7 +178,7 @@ func GetAvailableControllerV2() (*Controllers, error) { } func getAvailableControllerV2(prefix string) (*Controllers, error) { - return getAvailableControllerV2path(path.Join(basePath, prefix, cgroupControllers)) + return getAvailableControllerV2path(filepath.Join(basePath, prefix, cgroupControllers)) } func getAvailableControllerV2path(p string) (*Controllers, error) { diff --git a/pkg/cgroup/cgroup_linux.go b/pkg/cgroup/cgroup_linux.go index f751ed6..5fdee5c 100644 --- a/pkg/cgroup/cgroup_linux.go +++ b/pkg/cgroup/cgroup_linux.go @@ -4,7 +4,7 @@ import ( "errors" "fmt" "os" - "path" + "path/filepath" "strings" ) @@ -136,7 +136,7 @@ func newV1(prefix string, ct *Controllers) (cg Cgroup, err error) { func newV2(prefix string, ct *Controllers) (cg Cgroup, err error) { v2 := &V2{ - path: path.Join(basePath, prefix), + path: filepath.Join(basePath, prefix), control: ct, } if _, err := os.Stat(v2.path); err == nil { @@ -159,8 +159,8 @@ func newV2(prefix string, ct *Controllers) (cg Cgroup, err error) { parent := current current = current + "/" + e // try mkdir if not exists - if _, err := os.Stat(path.Join(basePath, current)); os.IsNotExist(err) { - if err := os.Mkdir(path.Join(basePath, current), dirPerm); err != nil { + if _, err := os.Stat(filepath.Join(basePath, current)); os.IsNotExist(err) { + if err := os.Mkdir(filepath.Join(basePath, current), dirPerm); err != nil { return nil, err } } else if err != nil { @@ -175,7 +175,7 @@ func newV2(prefix string, ct *Controllers) (cg Cgroup, err error) { if ect.Contains(ct) { continue } - if err := writeFile(path.Join(basePath, parent, cgroupSubtreeControl), controlMsg, filePerm); err != nil { + if err := writeFile(filepath.Join(basePath, parent, cgroupSubtreeControl), controlMsg, filePerm); err != nil { return nil, err } } @@ -197,7 +197,7 @@ func openExistingV1(prefix string, ct *Controllers) (cg Cgroup, err error) { } if err = loopV1Controllers(ct, v1, func(name string, cg **v1controller) error { - p := path.Join(basePath, name, prefix) + p := filepath.Join(basePath, name, prefix) *cg = newV1Controller(p) // os.IsNotExist if _, err := os.Stat(p); err != nil { @@ -224,10 +224,10 @@ func openExistingV2(prefix string, ct *Controllers) (cg Cgroup, err error) { return nil, err } if !ect.Contains(ct) { - return nil, fmt.Errorf("openCgroupV2: requesting %v controllers but %v found", ct, ect) + return nil, fmt.Errorf("open cgroup v2: requesting %v controllers but %v found", ct, ect) } return &V2{ - path: path.Join(basePath, prefix), + path: filepath.Join(basePath, prefix), control: ect, existing: true, }, nil diff --git a/pkg/cgroup/utils_linux.go b/pkg/cgroup/utils_linux.go index 69511e7..65e3b58 100644 --- a/pkg/cgroup/utils_linux.go +++ b/pkg/cgroup/utils_linux.go @@ -6,7 +6,7 @@ import ( "io/fs" "math/rand/v2" "os" - "path" + "path/filepath" "strconv" "strings" "syscall" @@ -24,7 +24,7 @@ func EnsureDirExists(path string) error { // CreateV1ControllerPath create path for controller with given group, prefix func CreateV1ControllerPath(controller, prefix string) (string, error) { - p := path.Join(basePath, controller, prefix) + p := filepath.Join(basePath, controller, prefix) return p, EnsureDirExists(p) } @@ -37,7 +37,7 @@ func EnableV2Nesting() error { return nil } - p, err := readFile(path.Join(basePath, cgroupProcs)) + p, err := readFile(filepath.Join(basePath, cgroupProcs)) if err != nil { return err } @@ -47,11 +47,11 @@ func EnableV2Nesting() error { } // mkdir init - if err := os.Mkdir(path.Join(basePath, initPath), dirPerm); err != nil && !errors.Is(err, os.ErrExist) { + if err := os.Mkdir(filepath.Join(basePath, initPath), dirPerm); err != nil && !errors.Is(err, os.ErrExist) { return err } // move all process into init cgroup - procFile, err := os.OpenFile(path.Join(basePath, initPath, cgroupProcs), os.O_RDWR, filePerm) + procFile, err := os.OpenFile(filepath.Join(basePath, initPath, cgroupProcs), os.O_RDWR, filePerm) if err != nil { return err } @@ -165,7 +165,7 @@ func nextRandom() string { func randomBuild(pattern string, build func(string) (Cgroup, error)) (Cgroup, error) { prefix, suffix, err := prefixAndSuffix(pattern) if err != nil { - return nil, fmt.Errorf("cgroup.builder: random %v", err) + return nil, fmt.Errorf("cgroup.builder: random %w", err) } try := 0 @@ -181,6 +181,6 @@ func randomBuild(pattern string, build func(string) (Cgroup, error)) (Cgroup, er } return nil, fmt.Errorf("cgroup.builder: tried 10000 times but failed") } - return nil, fmt.Errorf("cgroup.builder: random %v", err) + return nil, fmt.Errorf("cgroup.builder: random %w", err) } } diff --git a/pkg/cgroup/v1_linux.go b/pkg/cgroup/v1_linux.go index 04b14e2..f742d1e 100644 --- a/pkg/cgroup/v1_linux.go +++ b/pkg/cgroup/v1_linux.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "os" - "path" "path/filepath" "strings" ) @@ -65,13 +64,13 @@ func (c *V1) Processes() ([]int, error) { if len(c.all) == 0 { return nil, os.ErrInvalid } - return ReadProcesses(path.Join(c.all[0].path, cgroupProcs)) + return ReadProcesses(filepath.Join(c.all[0].path, cgroupProcs)) } // New creates a sub-cgroup based on the existing one func (c *V1) New(name string) (cg Cgroup, err error) { v1 := &V1{ - prefix: path.Join(c.prefix, name), + prefix: filepath.Join(c.prefix, name), } defer func() { if err != nil { @@ -93,7 +92,7 @@ func (c *V1) New(name string) (cg Cgroup, err error) { if v.now == nil { continue } - p := path.Join(v.now.path, name) + p := filepath.Join(v.now.path, name) *v.new = &v1controller{path: p} err = EnsureDirExists(p) if os.IsExist(err) { diff --git a/pkg/cgroup/v1controller_linux.go b/pkg/cgroup/v1controller_linux.go index 592ac00..5341fe7 100644 --- a/pkg/cgroup/v1controller_linux.go +++ b/pkg/cgroup/v1controller_linux.go @@ -2,7 +2,7 @@ package cgroup import ( "errors" - "path" + "path/filepath" "strconv" "strings" ) @@ -50,7 +50,7 @@ func (c *v1controller) WriteFile(name string, content []byte) error { if c == nil || c.path == "" { return ErrNotInitialized } - p := path.Join(c.path, name) + p := filepath.Join(c.path, name) return writeFile(p, content, filePerm) } @@ -60,10 +60,10 @@ func (c *v1controller) ReadFile(name string) ([]byte, error) { if c == nil || c.path == "" { return nil, nil } - p := path.Join(c.path, name) + p := filepath.Join(c.path, name) return readFile(p) } func (c *v1controller) AddProc(pids ...int) error { - return AddProcesses(path.Join(c.path, cgroupProcs), pids) + return AddProcesses(filepath.Join(c.path, cgroupProcs), pids) } diff --git a/pkg/cgroup/v2_linux.go b/pkg/cgroup/v2_linux.go index 5785dc1..5042b36 100644 --- a/pkg/cgroup/v2_linux.go +++ b/pkg/cgroup/v2_linux.go @@ -4,7 +4,7 @@ import ( "bufio" "bytes" "os" - "path" + "path/filepath" "strconv" "strings" "sync" @@ -26,18 +26,18 @@ func (c *V2) Open() (*os.File, error) { } func (c *V2) String() string { - ct, _ := getAvailableControllerV2path(path.Join(c.path, cgroupControllers)) + ct, _ := getAvailableControllerV2path(filepath.Join(c.path, cgroupControllers)) return "v2(" + c.path + ")" + ct.String() } // AddProc adds processes into the cgroup func (c *V2) AddProc(pids ...int) error { - return AddProcesses(path.Join(c.path, cgroupProcs), pids) + return AddProcesses(filepath.Join(c.path, cgroupProcs), pids) } // Processes returns all processes within the cgroup func (c *V2) Processes() ([]int, error) { - return ReadProcesses(path.Join(c.path, cgroupProcs)) + return ReadProcesses(filepath.Join(c.path, cgroupProcs)) } // New creates a sub-cgroup based on the existing one @@ -46,7 +46,7 @@ func (c *V2) New(name string) (Cgroup, error) { return nil, err } v2 := &V2{ - path: path.Join(c.path, name), + path: filepath.Join(c.path, name), control: c.control, } if err := os.Mkdir(v2.path, dirPerm); err != nil { @@ -61,7 +61,7 @@ func (c *V2) New(name string) (Cgroup, error) { // Nest creates a sub-cgroup, moves current process into that cgroup func (c *V2) Nest(name string) (Cgroup, error) { v2 := &V2{ - path: path.Join(c.path, name), + path: filepath.Join(c.path, name), control: c.control, } if err := os.Mkdir(v2.path, dirPerm); err != nil { @@ -85,12 +85,12 @@ func (c *V2) Nest(name string) (Cgroup, error) { func (c *V2) enableSubtreeControl() error { c.subtreeOnce.Do(func() { - ct, err := getAvailableControllerV2path(path.Join(c.path, cgroupControllers)) + ct, err := getAvailableControllerV2path(filepath.Join(c.path, cgroupControllers)) if err != nil { c.subtreeErr = err return } - ect, err := getAvailableControllerV2path(path.Join(c.path, cgroupSubtreeControl)) + ect, err := getAvailableControllerV2path(filepath.Join(c.path, cgroupSubtreeControl)) if err != nil { c.subtreeErr = err return @@ -100,7 +100,7 @@ func (c *V2) enableSubtreeControl() error { } s := ct.Names() controlMsg := []byte("+" + strings.Join(s, " +")) - c.subtreeErr = writeFile(path.Join(c.path, cgroupSubtreeControl), controlMsg, filePerm) + c.subtreeErr = writeFile(filepath.Join(c.path, cgroupSubtreeControl), controlMsg, filePerm) }) return c.subtreeErr } @@ -221,13 +221,13 @@ func (c *V2) ReadUint(filename string) (uint64, error) { // WriteFile writes cgroup file and handles potential EINTR error while writes to // the slow device (cgroup) func (c *V2) WriteFile(name string, content []byte) error { - p := path.Join(c.path, name) + p := filepath.Join(c.path, name) return writeFile(p, content, filePerm) } // ReadFile reads cgroup file and handles potential EINTR error while read to // the slow device (cgroup) func (c *V2) ReadFile(name string) ([]byte, error) { - p := path.Join(c.path, name) + p := filepath.Join(c.path, name) return readFile(p) } diff --git a/pkg/memfd/memfd_other.go b/pkg/memfd/memfd_other.go index 7021868..2c13f22 100644 --- a/pkg/memfd/memfd_other.go +++ b/pkg/memfd/memfd_other.go @@ -9,7 +9,7 @@ import ( "runtime" ) -var errNotImplemented = fmt.Errorf("memfd: unsupported on platform %s", runtime.GOOS) +var errNotImplemented = fmt.Errorf("memfd: unsupported on platform: %s", runtime.GOOS) func New(name string) (*os.File, error) { return nil, errNotImplemented diff --git a/pkg/seccomp/libseccomp/syscall_name_linux.go b/pkg/seccomp/libseccomp/syscall_name_linux.go index 07e2d2b..f0d3bf9 100644 --- a/pkg/seccomp/libseccomp/syscall_name_linux.go +++ b/pkg/seccomp/libseccomp/syscall_name_linux.go @@ -15,7 +15,7 @@ func ToSyscallName(sysno uint) (string, error) { } n, ok := info.SyscallNumbers[int(sysno)] if !ok { - return "", fmt.Errorf("syscall no %d does not exits", sysno) + return "", fmt.Errorf("syscall number does not exist: %d", sysno) } return n, nil } diff --git a/pkg/unixsocket/socket_linux.go b/pkg/unixsocket/socket_linux.go index 43cb4ea..b93f435 100644 --- a/pkg/unixsocket/socket_linux.go +++ b/pkg/unixsocket/socket_linux.go @@ -45,19 +45,19 @@ func NewSocket(fd int) (*Socket, error) { file := os.NewFile(uintptr(fd), "unix-socket") if file == nil { - return nil, fmt.Errorf("NewSocket: %d is not a valid fd", fd) + return nil, fmt.Errorf("new socket: %d is not a valid fd", fd) } defer file.Close() conn, err := net.FileConn(file) if err != nil { - return nil, err + return nil, fmt.Errorf("new socket: fileconn: %w", err) } unixConn, ok := conn.(*net.UnixConn) if !ok { conn.Close() - return nil, fmt.Errorf("NewSocket: %d is not a valid unix socket connection", fd) + return nil, fmt.Errorf("new socket: %d is not a valid unix socket connection", fd) } return newSocket(unixConn), nil } @@ -66,21 +66,21 @@ func NewSocket(fd int) (*Socket, error) { func NewSocketPair() (*Socket, *Socket, error) { fd, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_SEQPACKET|syscall.SOCK_CLOEXEC, 0) if err != nil { - return nil, nil, fmt.Errorf("NewSocketPair: failed to call socketpair %v", err) + return nil, nil, fmt.Errorf("new socket pair: socketpair: %w", err) } ins, err := NewSocket(fd[0]) if err != nil { syscall.Close(fd[0]) syscall.Close(fd[1]) - return nil, nil, fmt.Errorf("NewSocketPair: failed to call NewSocket on sender %v", err) + return nil, nil, fmt.Errorf("new socket pair: sender: %w", err) } outs, err := NewSocket(fd[1]) if err != nil { ins.Close() syscall.Close(fd[1]) - return nil, nil, fmt.Errorf("NewSocketPair: failed to call NewSocket receiver %v", err) + return nil, nil, fmt.Errorf("new socket pair: receiver: %w", err) } return ins, outs, nil diff --git a/runner/ptrace/handle_linux.go b/runner/ptrace/handle_linux.go index cd83afc..8c47060 100644 --- a/runner/ptrace/handle_linux.go +++ b/runner/ptrace/handle_linux.go @@ -3,7 +3,7 @@ package ptrace import ( "fmt" "os" - "path" + "path/filepath" "syscall" "github.com/criyle/go-sandbox/pkg/seccomp/libseccomp" @@ -156,8 +156,8 @@ func getProcCwd(pid int) string { // built-in function did the dirty works to resolve relative paths func absPath(pid int, p string) string { // if relative path - if !path.IsAbs(p) { - return path.Join(getProcCwd(pid), p) + if !filepath.IsAbs(p) { + return filepath.Join(getProcCwd(pid), p) } - return path.Clean(p) + return filepath.Clean(p) }