Skip to content

Commit 45e8c8e

Browse files
authored
Merge pull request moby#3742 from tonistiigi/git-umask-refactor
git: set umask without reexec
2 parents b85b5ab + e34d10d commit 45e8c8e

File tree

4 files changed

+23
-67
lines changed

4 files changed

+23
-67
lines changed

source/git/gitsource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ func git(ctx context.Context, dir, sshAuthSock, knownHosts string, args ...strin
678678
}
679679
// remote git commands spawn helper processes that inherit FDs and don't
680680
// handle parent death signal so exec.CommandContext can't be used
681-
err := runProcessGroup(ctx, cmd)
681+
err := runWithStandardUmask(ctx, cmd)
682682
if err != nil {
683683
if strings.Contains(errbuf.String(), "--depth") || strings.Contains(errbuf.String(), "shallow") {
684684
if newArgs := argsNoDepth(args); len(args) > len(newArgs) {

source/git/gitsource_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/containerd/containerd/namespaces"
2222
"github.com/containerd/containerd/snapshots"
2323
"github.com/containerd/containerd/snapshots/native"
24-
"github.com/docker/docker/pkg/reexec"
2524
"github.com/moby/buildkit/cache"
2625
"github.com/moby/buildkit/cache/metadata"
2726
"github.com/moby/buildkit/client"
@@ -36,12 +35,6 @@ import (
3635
bolt "go.etcd.io/bbolt"
3736
)
3837

39-
func init() {
40-
if reexec.Init() {
41-
os.Exit(0)
42-
}
43-
}
44-
4538
func TestRepeatedFetch(t *testing.T) {
4639
testRepeatedFetch(t, false)
4740
}

source/git/gitsource_unix.go

Lines changed: 21 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,80 +5,43 @@ package git
55

66
import (
77
"context"
8-
"os"
98
"os/exec"
10-
"os/signal"
9+
"runtime"
1110
"syscall"
1211
"time"
1312

14-
"github.com/docker/docker/pkg/reexec"
1513
"golang.org/x/sys/unix"
1614
)
1715

18-
const (
19-
gitCmd = "umask-git"
20-
)
21-
22-
func init() {
23-
reexec.Register(gitCmd, gitMain)
24-
}
25-
26-
func gitMain() {
27-
// Need standard user umask for git process.
28-
unix.Umask(0022)
29-
30-
// Reexec git command
31-
cmd := exec.Command(os.Args[1], os.Args[2:]...) //nolint:gosec // reexec
32-
cmd.SysProcAttr = &unix.SysProcAttr{
33-
Setpgid: true,
34-
Pdeathsig: unix.SIGTERM,
35-
}
36-
cmd.Stdout = os.Stdout
37-
cmd.Stderr = os.Stderr
38-
cmd.Stdin = os.Stdin
16+
func runWithStandardUmask(ctx context.Context, cmd *exec.Cmd) error {
17+
errCh := make(chan error)
3918

40-
// Forward all signals
41-
sigc := make(chan os.Signal, 1)
42-
done := make(chan struct{})
43-
signal.Notify(sigc)
4419
go func() {
45-
for {
46-
select {
47-
case sig := <-sigc:
48-
if cmd.Process == nil {
49-
continue
50-
}
51-
switch sig {
52-
case unix.SIGINT, unix.SIGTERM, unix.SIGKILL:
53-
_ = unix.Kill(-cmd.Process.Pid, sig.(unix.Signal))
54-
default:
55-
_ = cmd.Process.Signal(sig)
56-
}
57-
case <-done:
58-
return
59-
}
20+
defer close(errCh)
21+
runtime.LockOSThread()
22+
23+
if err := unshareAndRun(ctx, cmd); err != nil {
24+
errCh <- err
6025
}
6126
}()
6227

63-
err := cmd.Run()
64-
close(done)
65-
if err != nil {
66-
if exiterr, ok := err.(*exec.ExitError); ok {
67-
switch status := exiterr.Sys().(type) {
68-
case unix.WaitStatus:
69-
os.Exit(status.ExitStatus())
70-
case syscall.WaitStatus:
71-
os.Exit(status.ExitStatus())
72-
}
73-
}
74-
os.Exit(1)
28+
return <-errCh
29+
}
30+
31+
// unshareAndRun needs to be called in a locked thread.
32+
func unshareAndRun(ctx context.Context, cmd *exec.Cmd) error {
33+
if err := syscall.Unshare(syscall.CLONE_FS); err != nil {
34+
return err
7535
}
76-
os.Exit(0)
36+
syscall.Umask(0022)
37+
return runProcessGroup(ctx, cmd)
7738
}
7839

7940
func runProcessGroup(ctx context.Context, cmd *exec.Cmd) error {
80-
cmd.Path = reexec.Self()
81-
cmd.Args = append([]string{gitCmd}, cmd.Args...)
41+
cmd.SysProcAttr = &unix.SysProcAttr{
42+
Setpgid: true,
43+
Pdeathsig: unix.SIGTERM,
44+
}
8245
if err := cmd.Start(); err != nil {
8346
return err
8447
}

source/git/gitsource_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"os/exec"
99
)
1010

11-
func runProcessGroup(ctx context.Context, cmd *exec.Cmd) error {
11+
func runWithStandardUmask(ctx context.Context, cmd *exec.Cmd) error {
1212
if err := cmd.Start(); err != nil {
1313
return err
1414
}

0 commit comments

Comments
 (0)