Debian Codegraph @

daemon/droppriv.go

raw symbols SCIP
1 // +build !windows
2
3 package daemon
4
5 import (
6 "errors"
7 "fmt"
8 "gopkg.in/hlandau/svcutils.v1/caps"
9 "gopkg.in/hlandau/svcutils.v1/chroot"
10 "gopkg.in/hlandau/svcutils.v1/passwd"
11 "gopkg.in/hlandau/svcutils.v1/setuid"
12 "net"
13 "runtime"
14 "sync"
15 "syscall"
16 )
17
18 // Drops privileges to the specified UID and GID.
19 // This function does nothing and returns no error if all E?[UG]IDs are nonzero.
20 //
21 // If chrootDir is not empty, the process is chrooted into it. The directory
22 // must exist. The function tests that privilege dropping has been successful
23 // by attempting to setuid(0), which must fail.
24 //
25 // The current directory is set to / inside the chroot.
26 //
27 // The function ensures that /etc/hosts and /etc/resolv.conf are loaded before
28 // chrooting, so name service should continue to be available.
29 func DropPrivileges(UID, GID int, chrootDir string) (chrootErr error, err error) {
30 // chroot and set UID and GIDs
31 chrootErr, err = dropPrivileges(UID, GID, chrootDir)
32 if err != nil {
33 err = fmt.Errorf("dropPrivileges failed: %v", err)
34 return
35 }
36
37 err = syscall.Chdir("/")
38 if err != nil {
39 return
40 }
41
42 err = ensureNoPrivs()
43 if err != nil {
44 err = fmt.Errorf("ensure no privs failed: %v", err)
45 return
46 }
47
48 return
49 }
50
51 func dropPrivileges(UID, GID int, chrootDir string) (chrootErr error, err error) {
52 if (UID <= 0) != (GID <= 0) {
53 return nil, errors.New("either both or neither UID and GID must be set to positive (i.e. valid, non-root) values")
54 }
55
56 var gids []int
57 if UID > 0 {
58 gids, err = passwd.GetExtraGIDs(GID)
59 if err != nil {
60 return nil, err
61 }
62
63 gids = append(gids, GID)
64 }
65
66 chrootErr = tryChroot(chrootDir)
67
68 if UID > 0 {
69 err = tryDropPrivileges(UID, GID, gids)
70 if err != nil {
71 return
72 }
73 }
74
75 return
76 }
77
78 var warnOnce sync.Once
79
80 func tryDropPrivileges(UID, GID int, gids []int) error {
81 if UID <= 0 || GID <= 0 {
82 return errors.New("invalid UID/GID specified so cannot setuid/setgid")
83 }
84
85 if runtime.GOOS == "linux" {
86 ver := runtime.Version()
87 if ver == "go1.5" || ver == "go1.5.1" {
88 return errors.New("It is not possible to drop privileges on Linux using Go 1.5 or 1.5.1 (Go bug #12498: <https://github.com/golang/go/issues/12498>); either use Go1.4, 1.5.2 or a development branch of Go, or do not use privilege dropping by running services only as non-root users with no capabilities set")
89 }
90 }
91
92 err := setuid.Setgroups(gids)
93 if err != nil {
94 return err
95 }
96
97 err = setuid.Setresgid(GID, GID, GID)
98 if err != nil {
99 return err
100 }
101
102 err = setuid.Setresuid(UID, UID, UID)
103 if err != nil {
104 return err
105 }
106
107 return nil
108 }
109
110 func tryChroot(path string) error {
111 if path == "/" {
112 path = ""
113 }
114
115 if path == "" {
116 return nil
117 }
118
119 ensureResolverConfigIsLoaded()
120
121 err := chroot.Chroot(path)
122 if err != nil {
123 return err
124 }
125
126 return nil
127 }
128
129 func ensureResolverConfigIsLoaded() {
130 c, err := net.Dial("udp", "un_localhost:1")
131 if err == nil {
132 c.Close()
133 }
134 }
135
136 func ensureNoPrivs() error {
137 if IsRoot() {
138 return errors.New("still have non-zero UID or GID or capabilities")
139 }
140
141 err := setuid.Setuid(0)
142 if err == nil {
143 return errors.New("Can't drop privileges - setuid(0) still succeeded")
144 }
145
146 err = setuid.Setgid(0)
147 if err == nil {
148 return errors.New("Can't drop privileges - setgid(0) still succeeded")
149 }
150
151 return nil
152 }
153
154 // Returns true if either or both of the following are true:
155 //
156 // Any of the UID, EUID, GID or EGID are zero.
157 //
158 // On supported platforms which support capabilities (currently Linux), any
159 // capabilities are present.
160 func IsRoot() bool {
161 return caps.HaveAny() || isRoot()
162 }
163
164 func isRoot() bool {
165 return syscall.Getuid() == 0 || syscall.Geteuid() == 0 ||
166 syscall.Getgid() == 0 || syscall.Getegid() == 0
167 }
168
169 // This is set to a path which should be empty on the target platform.
170 //
171 // On Linux, the FHS provides that "/var/empty" should always be empty.
172 var EmptyChrootPath = "/var/empty"