utils: use safe procfs for /proc/self/fd loop code

From a safety perspective this might not be strictly required, but it
paves the way for us to remove utils.ProcThreadSelf.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
This commit is contained in:
Aleksa Sarai
2025-06-20 15:43:49 +10:00
parent fdcc9d3cad
commit ff6fe13246
4 changed files with 40 additions and 14 deletions
+6 -5
View File
@@ -14,6 +14,7 @@ import (
securejoin "github.com/cyphar/filepath-securejoin"
"github.com/opencontainers/runc/internal/linux"
"github.com/opencontainers/runc/internal/pathrs"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)
@@ -59,15 +60,15 @@ type fdFunc func(fd int)
// fdRangeFrom calls the passed fdFunc for each file descriptor that is open in
// the current process.
func fdRangeFrom(minFd int, fn fdFunc) error {
procSelfFd, closer := ProcThreadSelf("fd")
defer closer()
fdDir, err := os.Open(procSelfFd)
fdDir, closer, err := pathrs.ProcThreadSelfOpen("fd/", unix.O_DIRECTORY|unix.O_CLOEXEC)
if err != nil {
return err
return fmt.Errorf("get handle to /proc/thread-self/fd: %w", err)
}
defer closer()
defer fdDir.Close()
// NOTE: This is not really necessary since securejoin.ProcThreadSelf
// verifies this in a far stricter sense than EnsureProcHandle.
if err := EnsureProcHandle(fdDir); err != nil {
return err
}