-
Notifications
You must be signed in to change notification settings - Fork 184
/
unix_permission.go
51 lines (41 loc) · 1.57 KB
/
unix_permission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
// go:build !windows
//go:build !windows
// +build !windows
package security
import (
"fmt"
"os/user"
"syscall"
)
// CheckFileRights check that the given file path has been protected by the owner.
// If the owner is changed, they need at least the sudo permission to override the owner.
func CheckFileRights(filePath string) error {
var stat syscall.Stat_t
if err := syscall.Stat(filePath, &stat); err != nil {
return fmt.Errorf("Cannot get file's stat %s: %v", filePath, err)
}
// Check the owner of binary has read, write, exec.
if !(stat.Mode&(syscall.S_IXUSR) == 0 || stat.Mode&(syscall.S_IRUSR) == 0 || stat.Mode&(syscall.S_IWUSR) == 0) {
return nil
}
// Check the owner of file has read, write
if !(stat.Mode&(syscall.S_IRUSR) == 0 || stat.Mode&(syscall.S_IWUSR) == 0) {
return nil
}
return fmt.Errorf("File's owner does not have enough permission at path %s", filePath)
}
// CheckFileOwnerRights check that the given owner is the same owner of the given filepath
func CheckFileOwnerRights(filePath, requiredOwner string) error {
var stat syscall.Stat_t
if err := syscall.Stat(filePath, &stat); err != nil {
return fmt.Errorf("Cannot get file's stat %s: %v", filePath, err)
}
if owner, err := user.LookupId(fmt.Sprintf("%d", stat.Uid)); err != nil {
return fmt.Errorf("Cannot look up file owner's name %s: %v", filePath, err)
} else if owner.Name != requiredOwner {
return fmt.Errorf("Agent does not have permission to protect file %s", filePath)
}
return nil
}