Skip to content

Commit 57b9a1a

Browse files
committed
Internal change.
PiperOrigin-RevId: 914702616
1 parent 8bae410 commit 57b9a1a

7 files changed

Lines changed: 84 additions & 6 deletions

File tree

pkg/sentry/fsimpl/proc/filesystem.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ type InternalData struct {
164164
// should exist in the procfs.
165165
GVisorMarkerFile bool
166166

167+
// OverrideProcs is a list of proc files to override with stubs. The
168+
// /proc/ prefix is removed (e.g., this contains filenames like
169+
// "kallsyms").
170+
OverrideProcs []string
171+
167172
// Cgroups-internal data.
168173
Cgroups map[string]string
169174
}

pkg/sentry/fsimpl/proc/tasks.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ func (fs *filesystem) newTasksInode(ctx context.Context, k *kernel.Kernel, pidns
103103
}
104104
fs.addNvproxyFiles(ctx, root, k, contents)
105105

106+
for _, name := range internalData.OverrideProcs {
107+
contents[name] = fs.newInode(ctx, root, 0444, newStaticFile(""))
108+
}
109+
106110
fs.newTasksInodeExtra(ctx, root, internalData, k, contents)
107111

108112
inode := &tasksInode{

pkg/sentry/fsimpl/proc/tasks_test.go

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package proc
1616

1717
import (
1818
"fmt"
19+
"io"
1920
"math"
2021
"path"
2122
"strconv"
@@ -105,6 +106,15 @@ var (
105106
)
106107

107108
func setup(t *testing.T) *testutil.System {
109+
return setupWithData(t, &InternalData{
110+
Cgroups: map[string]string{
111+
"cpuset": "/foo/cpuset",
112+
"memory": "/foo/memory",
113+
},
114+
})
115+
}
116+
117+
func setupWithData(t *testing.T, data *InternalData) *testutil.System {
108118
k, err := testutil.Boot()
109119
if err != nil {
110120
t.Fatalf("Error creating kernel: %v", err)
@@ -139,12 +149,7 @@ func setup(t *testing.T) *testutil.System {
139149
}
140150
mntOpts := &vfs.MountOptions{
141151
GetFilesystemOptions: vfs.GetFilesystemOptions{
142-
InternalData: &InternalData{
143-
Cgroups: map[string]string{
144-
"cpuset": "/foo/cpuset",
145-
"memory": "/foo/memory",
146-
},
147-
},
152+
InternalData: data,
148153
},
149154
}
150155
if _, err := k.VFS().MountAt(ctx, creds, "", pop, Name, mntOpts); err != nil {
@@ -162,6 +167,52 @@ func TestTasksEmpty(t *testing.T) {
162167
s.AssertDirentOffsets(collector, tasksStaticFilesNextOffs)
163168
}
164169

170+
func TestTasksWithOverrideProc(t *testing.T) {
171+
s := setupWithData(t, &InternalData{
172+
Cgroups: map[string]string{
173+
"cpuset": "/foo/cpuset",
174+
"memory": "/foo/memory",
175+
},
176+
OverrideProcs: []string{"kallsyms", "arbitrary_file"},
177+
})
178+
defer s.Destroy()
179+
180+
expected := make(map[string]testutil.DirentType)
181+
for k, v := range tasksStaticFiles {
182+
expected[k] = v
183+
}
184+
expected["kallsyms"] = linux.DT_REG
185+
expected["arbitrary_file"] = linux.DT_REG
186+
187+
collector := s.ListDirents(s.PathOpAtRoot("/proc"))
188+
s.AssertAllDirentTypes(collector, expected)
189+
190+
// Verify file contents are empty.
191+
for _, name := range []string{"kallsyms", "arbitrary_file"} {
192+
filePath := path.Join("/proc", name)
193+
fd, err := s.VFS.OpenAt(
194+
s.Ctx,
195+
s.Creds,
196+
s.PathOpAtRoot(filePath),
197+
&vfs.OpenOptions{},
198+
)
199+
if err != nil {
200+
t.Fatalf("vfs.OpenAt(%q) failed: %v", filePath, err)
201+
}
202+
defer fd.DecRef(s.Ctx)
203+
204+
buf := make([]byte, 1024)
205+
bufIOSeq := usermem.BytesIOSequence(buf)
206+
n, err := fd.Read(s.Ctx, bufIOSeq, vfs.ReadOptions{})
207+
if err != nil && err != io.EOF {
208+
t.Errorf("read %q failed: %v", filePath, err)
209+
}
210+
if n != 0 {
211+
t.Errorf("expected empty file %q, got %d bytes: %q", filePath, n, buf[:n])
212+
}
213+
}
214+
}
215+
165216
func TestTasks(t *testing.T) {
166217
s := setup(t)
167218
defer s.Destroy()

runsc/boot/restore.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"io"
2121
"os"
2222
"strconv"
23+
"strings"
2324
time2 "time"
2425

2526
specs "github.com/opencontainers/runtime-spec/specs-go"
@@ -524,3 +525,15 @@ func (l *Loader) saveWithOpts(saveOpts *state.SaveOpts, execOpts *control.SaveRe
524525
}
525526
return state.SaveWithOpts(saveOpts, execOpts)
526527
}
528+
529+
func procFiles(conf *config.Config) []string {
530+
var files []string
531+
532+
if conf.OverrideProcs != "" {
533+
for _, val := range strings.Split(conf.OverrideProcs, ",") {
534+
files = append(files, strings.TrimPrefix(val, "/proc/"))
535+
}
536+
}
537+
538+
return files
539+
}

runsc/boot/restore_impl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
func newProcInternalData(conf *config.Config, _ *specs.Spec) *proc.InternalData {
2929
return &proc.InternalData{
3030
GVisorMarkerFile: conf.GVisorMarkerFile,
31+
OverrideProcs: procFiles(conf),
3132
}
3233
}
3334

runsc/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,9 @@ type Config struct {
406406
// GVisorMarkerFile enables the /proc/gvisor/kernel_is_gvisor marker file.
407407
GVisorMarkerFile bool `flag:"gvisor-marker-file"`
408408

409+
// OverrideProcs is a comma-separated list of proc files to override with stubs.
410+
OverrideProcs string `flag:"override-procs"`
411+
409412
// SystrapDisableSyscallPatching disables syscall patching in Systrap.
410413
SystrapDisableSyscallPatching bool `flag:"systrap-disable-syscall-patching"`
411414

runsc/config/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ func RegisterFlags(flagSet *flag.FlagSet) {
139139
flagSet.Var(hostUDSPtr(HostUDSNone), flagHostUDS, "controls permission to access host Unix-domain sockets. Values: none|open|create|all, default: none")
140140
flagSet.Var(hostFifoPtr(HostFifoNone), "host-fifo", "controls permission to access host FIFOs (or named pipes). Values: none|open, default: none")
141141
flagSet.Bool("gvisor-marker-file", false, "enable the presence of the /proc/gvisor/kernel_is_gvisor file that can be used by applications to detect that gVisor is in use")
142+
flagSet.String("override-procs", "", "comma-separated list of proc files to override with stubs (e.g. kallsyms)")
142143

143144
flagSet.Bool("ignore-cgroups", false, "don't configure cgroups.")
144145
flagSet.Int("fdlimit", -1, "Specifies a limit on the number of host file descriptors that can be open. Applies separately to the sentry and gofer. Note: each file in the sandbox holds more than one host FD open.")

0 commit comments

Comments
 (0)