Skip to content

Commit 4e8cf1b

Browse files
committed
separate functions for ownership check to improve readability
1 parent 7bc72a1 commit 4e8cf1b

File tree

5 files changed

+40
-15
lines changed

5 files changed

+40
-15
lines changed

internal/pkg/agent/cmd/run_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func checkCapabilitiesPerms(agentCapabilitiesPath string, userName string, uid i
4848
} else {
4949
capabilitiesUID = os.Getuid()
5050
}
51-
if err := utils.HasStrictExecPerms(agentCapabilitiesPath, capabilitiesUID, true); err != nil && !os.IsNotExist(err) {
51+
if err := utils.HasStrictExecPermsAndOwnership(agentCapabilitiesPath, capabilitiesUID); err != nil && !os.IsNotExist(err) {
5252
// capabilities are corrupted, we should not proceed
5353
return fmt.Errorf("invalid capabilities file permissions: %w", err)
5454
}

internal/pkg/agent/cmd/run_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func checkCapabilitiesPerms(agentCapabilitiesPath string, userName string, uid i
5050
} else {
5151
capabilitiesUID = os.Getuid()
5252
}
53-
if err := utils.HasStrictExecPerms(agentCapabilitiesPath, capabilitiesUID, true); err != nil && !os.IsNotExist(err) {
53+
if err := utils.HasStrictExecPermsAndOwnership(agentCapabilitiesPath, capabilitiesUID); err != nil && !os.IsNotExist(err) {
5454
// capabilities are corrupted, we should not proceed
5555
return fmt.Errorf("invalid capabilities file permissions: %w", err)
5656
}

pkg/component/runtime/command.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,12 @@ func (c *commandRuntime) start(comm Communicator) error {
363363
}
364364
env = append(env, fmt.Sprintf("%s=%s", envAgentComponentID, c.current.ID))
365365
env = append(env, fmt.Sprintf("%s=%s", envAgentComponentType, c.getSpecType()))
366-
uid := os.Geteuid()
367366
workDir := c.current.WorkDirPath(paths.Run())
368367
path, err := filepath.Abs(c.getSpecBinaryPath())
369368
if err != nil {
370369
return fmt.Errorf("failed to determine absolute path: %w", err)
371370
}
372-
err = utils.HasStrictExecPerms(path, uid, false)
371+
err = utils.HasStrictExecPerms(path)
373372
if err != nil {
374373
return fmt.Errorf("execution of component prevented: %w", err)
375374
}

pkg/utils/perm_unix.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,49 @@ func CurrentFileOwner() (FileOwner, error) {
2929

3030
// HasStrictExecPerms ensures that the path is executable by the owner, cannot be written by anyone other than the
3131
// owner of the file and that the owner of the file is the same as the UID or root.
32-
func HasStrictExecPerms(path string, uid int, checkUID bool) error {
32+
func HasStrictExecPerms(path string) error {
3333
info, err := file.Stat(path)
3434
if err != nil {
3535
return err
3636
}
37-
if info.IsDir() {
38-
return errors.New("is a directory")
39-
}
40-
if info.Mode()&0022 != 0 {
41-
return errors.New("cannot be writeable by group or other")
37+
38+
return hasStrictExecPerms(info)
39+
}
40+
41+
// HasStrictExecPermsAndOwnership ensures that the path is executable by the owner and that the owner of the file
42+
// is the same as the UID or root.
43+
func HasStrictExecPermsAndOwnership(path string, uid int) error {
44+
info, err := file.Stat(path)
45+
if err != nil {
46+
return err
4247
}
43-
if info.Mode()&0100 == 0 {
44-
return errors.New("not executable by owner")
48+
49+
if err := hasStrictExecPerms(info); err != nil {
50+
return err
4551
}
4652

4753
fileUID, err := info.UID()
4854
if err != nil {
4955
return err
5056
}
5157

52-
if checkUID && fileUID != 0 && fileUID != uid {
58+
if fileUID != 0 && fileUID != uid {
5359
return errors.New("file owner does not match expected UID or root")
5460
}
5561

5662
return nil
5763
}
64+
65+
func hasStrictExecPerms(info file.FileInfo) error {
66+
if info.IsDir() {
67+
return errors.New("is a directory")
68+
}
69+
if info.Mode()&0022 != 0 {
70+
return errors.New("cannot be writeable by group or other")
71+
}
72+
if info.Mode()&0100 == 0 {
73+
return errors.New("not executable by owner")
74+
}
75+
76+
return nil
77+
}

pkg/utils/perm_windows.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,15 @@ func CurrentFileOwner() (FileOwner, error) {
6161
}, nil
6262
}
6363

64-
// HasStrictExecPerms ensures that the path is executable by the owner and that the owner of the file
64+
// HasStrictExecPerms ensures that the path is executable by the owner.
65+
func HasStrictExecPerms(path string) error {
66+
// TODO: Need to add check on Windows to ensure that the ACL are correct for the binary before execution.
67+
return nil
68+
}
69+
70+
// HasStrictExecPermsAndOwnership ensures that the path is executable by the owner and that the owner of the file
6571
// is the same as the UID or root.
66-
func HasStrictExecPerms(path string, uid int, _ bool) error {
72+
func HasStrictExecPermsAndOwnership(path string, uid int) error {
6773
// TODO: Need to add check on Windows to ensure that the ACL are correct for the binary before execution.
6874
return nil
6975
}

0 commit comments

Comments
 (0)