From 5deab709e7e8c1a98396bf09aabba73b88924c97 Mon Sep 17 00:00:00 2001 From: Fabricio Voznika Date: Tue, 21 Jun 2022 09:54:32 -0700 Subject: [PATCH] Add mode to procfs dump Updates #4805 PiperOrigin-RevId: 456284312 --- runsc/boot/procfs/BUILD | 1 + runsc/boot/procfs/dump.go | 11 ++++++++++- runsc/container/trace_test.go | 7 ++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/runsc/boot/procfs/BUILD b/runsc/boot/procfs/BUILD index cf06480bd..bc1ecf773 100644 --- a/runsc/boot/procfs/BUILD +++ b/runsc/boot/procfs/BUILD @@ -7,6 +7,7 @@ go_library( srcs = ["dump.go"], visibility = ["//runsc:__subpackages__"], deps = [ + "//pkg/abi/linux", "//pkg/context", "//pkg/log", "//pkg/sentry/fsimpl/proc", diff --git a/runsc/boot/procfs/dump.go b/runsc/boot/procfs/dump.go index 54214e21d..e8b288850 100644 --- a/runsc/boot/procfs/dump.go +++ b/runsc/boot/procfs/dump.go @@ -21,6 +21,7 @@ import ( "fmt" "strings" + "gvisor.dev/gvisor/pkg/abi/linux" "gvisor.dev/gvisor/pkg/context" "gvisor.dev/gvisor/pkg/log" "gvisor.dev/gvisor/pkg/sentry/fsimpl/proc" @@ -36,6 +37,8 @@ type FDInfo struct { Number int32 `json:"number,omitempty"` // Path is the path of the file that FD represents. Path string `json:"path,omitempty"` + // Mode is the file mode. + Mode uint16 `json:"mode,omitempty"` } // UIDGID contains information for /proc/[pid]/status/{uid,gid}. @@ -185,7 +188,13 @@ func getFDs(ctx context.Context, t *kernel.Task, pid kernel.ThreadID) []FDInfo { log.Warningf("PathnameWithDeleted failed to find path for fd %d in PID %s: %v", fd.no, pid, err) path = "" } - res = append(res, FDInfo{Number: fd.no, Path: path}) + mode := uint16(0) + if statx, err := fd.fd.Stat(ctx, vfs.StatOptions{Mask: linux.STATX_MODE}); err != nil { + log.Warningf("Stat(STATX_MODE) failed for fd %d in PID %s: %v", fd.no, pid, err) + } else { + mode = statx.Mode + } + res = append(res, FDInfo{Number: fd.no, Path: path, Mode: mode}) } return res } diff --git a/runsc/container/trace_test.go b/runsc/container/trace_test.go index a2df2d5a5..8a75236bb 100644 --- a/runsc/container/trace_test.go +++ b/runsc/container/trace_test.go @@ -22,6 +22,7 @@ import ( "time" specs "github.com/opencontainers/runtime-spec/specs-go" + "golang.org/x/sys/unix" "google.golang.org/protobuf/proto" "gvisor.dev/gvisor/pkg/sentry/kernel" "gvisor.dev/gvisor/pkg/sentry/limits" @@ -374,12 +375,16 @@ func TestProcfsDump(t *testing.T) { if len(procfsDump[0].FDs) < 3 { t.Errorf("expected at least 3 FDs for the sleep process, got %+v", procfsDump[0].FDs) } else { + modes := []uint16{unix.S_IFCHR, unix.S_IFIFO, unix.S_IFREG} for i, fd := range procfsDump[0].FDs[:3] { if want := int32(i); fd.Number != want { t.Errorf("expected FD number %d, got %d", want, fd.Number) } if wantSubStr := "host"; !strings.Contains(fd.Path, wantSubStr) { - t.Errorf("expected FD path to contain %q, got %q", wantSubStr, fd.Path) + t.Errorf("expected FD %d path to contain %q, got %q", fd.Number, wantSubStr, fd.Path) + } + if want, got := modes[i], fd.Mode&unix.S_IFMT; uint16(want) != got { + t.Errorf("wrong mode FD %d, want: %#o, got: %#o", fd.Number, want, got) } } }