Sandbox the OUT_DIR environment variable

Currently, OUT_DIR is inherited from the parent process, leading to
scripts being able to find the output directory when the enviornment
variable is set to an absolute path. When sandboxing a command,
also rewrite the OUT_DIR environment variable to the sandboxed one,
so that scripts can't find the real out dir.

Bug: 307824623
Test: Presubmits
Change-Id: I325071121a60bddc4105df680fbdfe3d11dc94e2
This commit is contained in:
Cole Faust
2024-08-23 14:41:51 -07:00
parent 079871cd55
commit 1ead86c1a5
4 changed files with 295 additions and 47 deletions

View File

@@ -580,6 +580,16 @@ func (r *RuleBuilder) build(name string, desc string, ninjaEscapeCommandString b
})
}
// Set OUT_DIR to the relative path of the sandboxed out directory.
// Otherwise, OUT_DIR will be inherited from the rest of the build,
// which will allow scripts to escape the sandbox if OUT_DIR is an
// absolute path.
command.Env = append(command.Env, &sbox_proto.EnvironmentVariable{
Name: proto.String("OUT_DIR"),
State: &sbox_proto.EnvironmentVariable_Value{
Value: sboxOutSubDir,
},
})
command.Chdir = proto.Bool(true)
}