-
Notifications
You must be signed in to change notification settings - Fork 0
/
priviledges.go
52 lines (46 loc) · 1.06 KB
/
priviledges.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
52
package service
import (
"errors"
"fmt"
"os"
"strconv"
"syscall"
)
func DropPrivs() error {
uid, gid := GetUserEnvInt()
if err := syscall.Setregid(-1, gid); err != nil {
return errors.New(fmt.Sprintf("Can't drop gid: %s", err))
}
if err := syscall.Setreuid(-1, uid); err != nil {
return errors.New(fmt.Sprintf("Can't drop uid: %s", err))
}
return nil
}
func EscalatePrivs() error {
err := syscall.Setreuid(-1, 0)
return err
}
// GetUserEnv checks if the process can drop priviledges by checking if either
// SUDO_UID and SUDO_GID or PKEXEC_UID are set, it returns the corresponding
// uid and gid set in these or 0 otherwise.
func GetUserEnv() (uid, gid string) {
if v := os.Getenv("SUDO_UID"); v != "" {
uid = v
} else if v := os.Getenv("PKEXEC_UID"); v != "" {
uid = v
} else {
uid = "0"
}
if v := os.Getenv("SUDO_GID"); v != "" {
gid = v
} else {
gid = "0"
}
return uid, gid
}
func GetUserEnvInt() (uid, gid int) {
uidString, gidString := GetUserEnv()
uid, _ = strconv.Atoi(uidString)
gid, _ = strconv.Atoi(gidString)
return uid, gid
}