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
This commit is contained in:
Andrei Vagin
2023-11-03 16:00:15 -07:00
committed by gVisor bot
parent aeaee71669
commit 1c2b646c26
@@ -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,