From 1c2b646c2655fc3bdfdbb4efadb42bfc40398525 Mon Sep 17 00:00:00 2001 From: Andrei Vagin Date: Fri, 3 Nov 2023 15:58:13 -0700 Subject: [PATCH] embeddedbinary: access proc files of the current process via /proc/self/ /proc/self is always points on the current process entry, but /proc/pid/ can points to another process if a process is in another pid namespace than /proc. PiperOrigin-RevId: 579328968 --- tools/embeddedbinary/embeddedbinary_template.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/embeddedbinary/embeddedbinary_template.go b/tools/embeddedbinary/embeddedbinary_template.go index 942272039..06ff9a435 100644 --- a/tools/embeddedbinary/embeddedbinary_template.go +++ b/tools/embeddedbinary/embeddedbinary_template.go @@ -63,7 +63,6 @@ func run(options Options, fork bool) (int, error) { decompressed := flate.NewReader(bytes.NewReader(compressedBinary)) runtime.LockOSThread() defer runtime.UnlockOSThread() - myPID := os.Getpid() oldMask := unix.Umask(0077) defer unix.Umask(oldMask) tmpDir, err := os.MkdirTemp("", "gvisor.*.tmp") @@ -89,7 +88,7 @@ func run(options Options, fork bool) (int, error) { return 0, fmt.Errorf("cannot decompress embedded binary or write it to temporary file: %w", err) } // Reopen the file for reading. - tmpFileReadOnly, err := os.OpenFile(fmt.Sprintf("/proc/%d/fd/%d", myPID, tmpFile.Fd()), os.O_RDONLY, 0700) + tmpFileReadOnly, err := os.OpenFile(fmt.Sprintf("/proc/self/fd/%d", tmpFile.Fd()), os.O_RDONLY, 0700) if err != nil { tmpFile.Close() return 0, fmt.Errorf("cannot re-open temp file for reading: %w", err) @@ -102,7 +101,7 @@ func run(options Options, fork bool) (int, error) { if _, err := unix.Seek(int(tmpFD), 0, unix.SEEK_SET); err != nil { return 0, fmt.Errorf("cannot seek temp file back to 0: %w", err) } - fdPath := fmt.Sprintf("/proc/%d/fd/%d", myPID, tmpFD) + fdPath := fmt.Sprintf("/proc/self/fd/%d", tmpFD) if fork { return syscall.ForkExec(fdPath, options.Argv, &syscall.ProcAttr{ Env: options.Envv,