Skip to content

Commit c98a217

Browse files
committed
fix: respect user namespace on rootfs creation
Signed-off-by: Luca Di Maio <luca.dimaio1@gmail.com>
1 parent 3acc6bc commit c98a217

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

pkg/dockerless/create.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func (p *DockerlessProvider) Create(ctx context.Context, workspaceId string, run
8585
p.Log.Debugf("unpacking layer %d of %d", index+1, len(manifest.Layers))
8686

8787
err = UntarFile(
88+
workspaceId,
8889
filepath.Join(imageDir, layerDigest),
8990
containerDIR,
9091
)

pkg/dockerless/exec.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ func (p *DockerlessProvider) ExecuteCommand(ctx context.Context, workspaceId, us
2727

2828
pid = bytes.TrimSpace(pid)
2929

30-
p.Log.Debugf("found process %s", string(pid))
31-
3230
nsenter := "nsenter"
3331
args := []string{
3432
"-m",
@@ -57,8 +55,6 @@ func (p *DockerlessProvider) ExecuteCommand(ctx context.Context, workspaceId, us
5755
args = append(args, []string{"su", "-l", uid, "-c", command}...)
5856
}
5957

60-
p.Log.Debugf("executing: %s %s", nsenter, strings.Join(args, " "))
61-
6258
cmd := exec.Command(nsenter, args...)
6359
environB, err := os.ReadFile(filepath.Join("/proc", string(pid), "environ"))
6460
if err == nil {

pkg/dockerless/fileutils.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,53 @@ import (
66
"io"
77
"os"
88
"os/exec"
9+
"path/filepath"
910
"syscall"
1011
)
1112

1213
// UntarFile will untar target file to target directory.
1314
// If userns is specified and it is keep-id, it will perform the
1415
// untarring in a new user namespace with user id maps set, in order to prevent
1516
// permission errors.
16-
func UntarFile(path string, target string) error {
17+
func UntarFile(workspaceId, path, target string) error {
1718
// first ensure we can write
1819
err := syscall.Access(path, 2)
1920
if err != nil {
2021
return err
2122
}
2223

23-
cmd := exec.Command("tar", "--exclude=dev/*", "-xpf", path, "-C", target)
24+
command := ""
25+
var args []string
26+
27+
if os.Getuid() > 0 {
28+
command = "rootlesskit"
29+
args = []string{
30+
"--pidns",
31+
"--cgroupns",
32+
"--utsns",
33+
"--ipcns",
34+
"--net",
35+
"host",
36+
"--state-dir",
37+
filepath.Join("/tmp", "dockerless", workspaceId),
38+
}
39+
} else {
40+
command = "unshare"
41+
args = []string{
42+
"-m",
43+
"-p",
44+
"-u",
45+
"-f",
46+
"--mount-proc",
47+
}
48+
}
49+
50+
args = append(args, []string{
51+
"tar", "--exclude=dev/*", "-xpf", path, "-C", target,
52+
}...,
53+
)
54+
55+
cmd := exec.Command(command, args...)
2456

2557
out, err := cmd.CombinedOutput()
2658
if err != nil {

0 commit comments

Comments
 (0)