Skip to content

Commit 2edd36f

Browse files
committed
libcontainer: create Cwd when it does not exist
The benefit for doing this within runc is that it works well with userns. Actually, runc already does the same thing for mount points. Signed-off-by: Akihiro Suda <[email protected]>
1 parent 0351df1 commit 2edd36f

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

libcontainer/rootfs_linux.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ func needsSetupDev(config *configs.Config) bool {
4040
// prepareRootfs sets up the devices, mount points, and filesystems for use
4141
// inside a new mount namespace. It doesn't set anything as ro. You must call
4242
// finalizeRootfs after this function to finish setting up the rootfs.
43-
func prepareRootfs(pipe io.ReadWriter, config *configs.Config) (err error) {
43+
func prepareRootfs(pipe io.ReadWriter, iConfig *initConfig) (err error) {
44+
config := iConfig.Config
4445
if err := prepareRoot(config); err != nil {
4546
return newSystemErrorWithCause(err, "preparing rootfs")
4647
}
@@ -80,6 +81,7 @@ func prepareRootfs(pipe io.ReadWriter, config *configs.Config) (err error) {
8081
// The hooks are run after the mounts are setup, but before we switch to the new
8182
// root, so that the old root is still available in the hooks for any mount
8283
// manipulations.
84+
// Note that iConfig.Cwd is not guaranteed to exist here.
8385
if err := syncParentHooks(pipe); err != nil {
8486
return err
8587
}
@@ -111,6 +113,14 @@ func prepareRootfs(pipe io.ReadWriter, config *configs.Config) (err error) {
111113
}
112114
}
113115

116+
if cwd := iConfig.Cwd; cwd != "" {
117+
// Note that spec.Process.Cwd can contain unclean value like "../../../../foo/bar...".
118+
// However, we are safe to call MkDirAll directly because we are in the jail here.
119+
if err := os.MkdirAll(cwd, 0755); err != nil {
120+
return err
121+
}
122+
}
123+
114124
return nil
115125
}
116126

libcontainer/standard_init_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (l *linuxStandardInit) Init() error {
6868

6969
// prepareRootfs() can be executed only for a new mount namespace.
7070
if l.config.Config.Namespaces.Contains(configs.NEWNS) {
71-
if err := prepareRootfs(l.pipe, l.config.Config); err != nil {
71+
if err := prepareRootfs(l.pipe, l.config); err != nil {
7272
return err
7373
}
7474
}

0 commit comments

Comments
 (0)