@@ -9,11 +9,11 @@ import (
9
9
"fmt"
10
10
"io/fs"
11
11
"os"
12
- "os/exec"
13
12
"path/filepath"
14
13
"strings"
15
14
"syscall"
16
15
16
+ "golang.org/x/sys/unix"
17
17
"kernel.org/pub/linux/libs/security/libcap/cap"
18
18
19
19
"github.com/elastic/elastic-agent/internal/pkg/cli"
@@ -33,42 +33,38 @@ var (
33
33
// - chown all agent-related paths if DAC_OVERRIDE capability is not in the Effective set
34
34
// If new binary capabilities are set then the returned cmd will be not nil. Note that it is up to caller to invoke
35
35
// the returned cmd and spawn an agent instance with all the capabilities.
36
- func initContainer (streams * cli.IOStreams ) (* exec. Cmd , error ) {
36
+ func initContainer (streams * cli.IOStreams ) (shouldExit bool , err error ) {
37
37
isRoot , _ := utils .HasRoot ()
38
38
if ! skipFileCapabilities && ! isRoot {
39
39
executable , err := os .Executable ()
40
40
if err != nil {
41
- return nil , err
41
+ return true , err
42
42
}
43
43
44
44
logInfo (streams , "agent container initialisation - file capabilities" )
45
45
updated , err := updateFileCapsFromBoundingSet (executable )
46
46
if err != nil {
47
- return nil , err
47
+ return true , err
48
48
}
49
49
50
50
if updated {
51
51
// new capabilities were added thus we need to re-exec agent to pick them up
52
- var args []string
52
+ args := []string { filepath . Base ( executable )}
53
53
if len (os .Args ) > 1 {
54
54
args = append (args , os .Args [1 :]... )
55
55
}
56
56
// add skipFileCapabilitiesFlag flag to skip reapplying the file capabilities
57
57
args = append (args , fmt .Sprintf ("--%s" , skipFileCapabilitiesFlag ))
58
58
59
- cmd := exec .Command (executable , args ... )
60
- cmd .Stdout = os .Stdout
61
- cmd .Stderr = os .Stderr
62
- cmd .Env = os .Environ ()
63
- return cmd , nil
59
+ return true , unix .Exec (executable , args , os .Environ ())
64
60
}
65
61
}
66
62
67
63
if ! isRoot {
68
64
// if we are not root, we need to raise the ambient capabilities
69
65
logInfo (streams , "agent container initialisation - ambient capabilities" )
70
66
if err := raiseAmbientCapabilities (); err != nil {
71
- return nil , err
67
+ return true , err
72
68
}
73
69
}
74
70
@@ -78,18 +74,18 @@ func initContainer(streams *cli.IOStreams) (*exec.Cmd, error) {
78
74
procSet := capProc ()
79
75
hasOverride , err := procSet .GetFlag (cap .Effective , cap .DAC_OVERRIDE )
80
76
if err != nil {
81
- return nil , err
77
+ return true , err
82
78
}
83
79
if ! hasOverride {
84
80
// we need to chown all paths
85
81
logInfo (streams , "agent container initialisation - chown paths" )
86
82
87
83
if err = chownPaths (); err != nil {
88
- return nil , err
84
+ return true , err
89
85
}
90
86
}
91
87
92
- return nil , nil
88
+ return false , nil
93
89
}
94
90
95
91
// raiseAmbientCapabilities will attempt to raise all capabilities present in the Effective set of the running process
0 commit comments