Skip to content

Commit f684675

Browse files
committed
[engine] Skip mounting filesystem root if in passthrough_env
Change-Id: I6d5e4bae06f0b2462b4e88806af24c4e1dde02b0
1 parent 9d338a1 commit f684675

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

internal/engine/runtime_ctx_os.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,27 @@ func ctxOsExec(ctx context.Context, s *shacState, name string, args starlark.Tup
252252
continue
253253
}
254254
env[pte.Name] = val
255-
if pte.IsPath {
256-
passthroughMounts = append(passthroughMounts, sandbox.Mount{
257-
Path: val,
258-
Writable: pte.Writeable,
255+
if !pte.IsPath {
256+
continue
257+
}
258+
259+
dest := val
260+
// Mount at a temporary directory.
261+
if val == filepath.Dir(val) {
262+
if dest, err = s.newTempDir(); err != nil {
263+
return nil, err
264+
}
265+
cleanupFuncs = append(cleanupFuncs, func() error {
266+
return os.RemoveAll(tempDir)
259267
})
260268
}
269+
270+
passthroughMounts = append(passthroughMounts, sandbox.Mount{
271+
Path: val,
272+
Writable: pte.Writeable,
273+
Dest: dest,
274+
Why: "passthrough_env: " + pte.Name,
275+
})
261276
}
262277

263278
for _, item := range argenv.Items() {
@@ -343,7 +358,7 @@ func ctxOsExec(ctx context.Context, s *shacState, name string, args starlark.Tup
343358
if runtime.GOOS != "windows" {
344359
config.Mounts = []sandbox.Mount{
345360
// TODO(olivernewman): Mount the checkout read-only unconditionally.
346-
{Path: s.root, Writable: s.writableRoot},
361+
{Path: s.root, Writable: s.writableRoot, Why: "checkout_root"},
347362
// OS-provided utilities.
348363
{Path: "/dev/null", Writable: true},
349364
{Path: "/dev/urandom"},
@@ -363,7 +378,7 @@ func ctxOsExec(ctx context.Context, s *shacState, name string, args starlark.Tup
363378
// Make the parent directory of tempDir available, since it is the root
364379
// of all ctx.os.tempdir() calls, which can be used as scratch pads for
365380
// this executable.
366-
{Path: filepath.Dir(tempDir), Writable: true},
381+
{Path: filepath.Dir(tempDir), Writable: true, Why: "tempdir_parent"},
367382
}
368383
config.Mounts = append(config.Mounts, passthroughMounts...)
369384

@@ -372,7 +387,7 @@ func ctxOsExec(ctx context.Context, s *shacState, name string, args starlark.Tup
372387
// installs Go in the checkout directory, and stop explicitly mounting
373388
// $GOROOT and adding it to $PATH.
374389
if runtime.GOROOT() != "" {
375-
config.Mounts = append(config.Mounts, sandbox.Mount{Path: runtime.GOROOT()})
390+
config.Mounts = append(config.Mounts, sandbox.Mount{Path: runtime.GOROOT(), Why: "GOROOT"})
376391
}
377392

378393
// Mount all directories listed in $PATH.
@@ -382,13 +397,18 @@ func ctxOsExec(ctx context.Context, s *shacState, name string, args starlark.Tup
382397
// Relative paths in $PATH are not allowed.
383398
continue
384399
}
400+
if p == filepath.Dir(p) {
401+
// Skip trying to mount the filesystem root if it happens to be
402+
// included in $PATH, since that will cause lots of issues.
403+
continue
404+
}
385405
var fi os.FileInfo
386406
if fi, err = os.Stat(p); err != nil || !fi.IsDir() {
387407
// Skip $PATH elements that don't exist or point to
388408
// non-directories.
389409
continue
390410
}
391-
config.Mounts = append(config.Mounts, sandbox.Mount{Path: p})
411+
config.Mounts = append(config.Mounts, sandbox.Mount{Path: p, Why: "PATH_element"})
392412
}
393413
}
394414

internal/sandbox/sandbox.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Mount struct {
4242
// Writable controls whether the mount is writable by processes within the
4343
// nsjail.
4444
Writable bool
45+
Why string
4546
}
4647

4748
// Config represents the configuration for a sandboxed subprocess.
@@ -135,6 +136,7 @@ func (s nsjailSandbox) Command(ctx context.Context, config *Config) *exec.Cmd {
135136
val = fmt.Sprintf("%s:%s", mnt.Path, mnt.Dest)
136137
}
137138
args = append(args, flag, val)
139+
fmt.Println("-", val, mnt.Why)
138140
}
139141
args = append(args, "--")
140142
args = append(args, config.Cmd...)

0 commit comments

Comments
 (0)