/
caps_linux.go
222 lines (196 loc) · 4.19 KB
/
caps_linux.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package caps
import (
"fmt"
"os"
"os/user"
"strconv"
"strings"
"syscall"
"unsafe"
)
const (
LINUX_CAPABILITY_VERSION_3 = 0x20080522
LINUX_CAPABILITY_U32S_3 = 2
)
type Capability int
const (
CapChown Capability = iota
CapDacOverride
CapDacReadSearch
CapFowner
CapFsetid
CapKill
CapSetgid
CapSetuid
CapSetpcap
CapLinuxImmutable
CapNetBindService
CapNetBroadcast
CapNetAdmin
CapNetRaw
CapIPCLock
CapIPCOwner
CapSysModule
CapSysRawIO
CapSysChroot
CapSysPtrace
CapSysPacct
CapSysAdmin
CapSysBoot
CapSysNice
CapSysResource
CapSysTime
CapSysTTYConfig
CapMknod
CapLease
CapAuditWrite
CapAuditControl
CapSetfcap
CapMACOverride
CapMACAdmin
CapSyslog
CapWakeAlarm
CapBlockSuspend
)
var (
CapMin Capability = 0
CapMax Capability // determined on process init
)
func init() {
capMax, err := lastCap()
if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: lastCap: %s\n", err)
return
}
CapMax = Capability(capMax)
}
type capHeader struct {
Version uint32
Pid int32
}
type capData struct {
Effective uint32
Permitted uint32
Inheritable uint32
}
type Cap struct {
Header capHeader
Data [LINUX_CAPABILITY_U32S_3]capData
}
// DropRoot is a convienence function around DropRootTo, calling
// caps.Lookup on the username provided, and passing the result to
// DropRootTo.
func DropRoot(username string) (err error) {
var u *user.User
if u, err = Lookup(username); err != nil {
return
}
return DropRootTo(u)
}
// DropRootTo calls setresgid(2) and setresuid(2) to permenently
// revoke root privileges. The USER, LOGNAME and HOME environmental
// variables are updated to match.
func DropRootTo(u *user.User) (err error) {
var uid, gid int
if gid, err = strconv.Atoi(u.Gid); err != nil {
return
}
if uid, err = strconv.Atoi(u.Uid); err != nil {
return
}
if err = syscall.Setresgid(gid, gid, gid); err != nil {
return
}
if err = syscall.Setresuid(uid, uid, uid); err != nil {
return
}
// based on what Avahi does.
os.Setenv("USER", u.Name)
os.Setenv("LOGNAME", u.Name)
os.Setenv("HOME", u.HomeDir)
return nil
}
// gets the capabilities of the current process
func capget(pid int) (*Cap, error) {
caps := new(Cap)
caps.Header.Version = LINUX_CAPABILITY_VERSION_3
caps.Header.Pid = int32(pid)
_, _, e1 := syscall.Syscall(syscall.SYS_CAPGET,
uintptr(unsafe.Pointer(&caps.Header)),
uintptr(unsafe.Pointer(&caps.Data[0])),
0)
if e1 != 0 {
return nil, e1
}
return caps, nil
}
func capset(caps ...int) (err error) {
var capabilities Cap
capabilities.Header.Version = LINUX_CAPABILITY_VERSION_3
capabilities.Header.Pid = int32(os.Getpid())
// TODO: actually support anything except dropping all caps.
_, _, e1 := syscall.Syscall(syscall.SYS_CAPSET,
uintptr(unsafe.Pointer(&capabilities.Header)),
uintptr(unsafe.Pointer(&capabilities.Data[0])),
0)
if e1 != 0 {
err = e1
}
return
}
// prctl(2) syscall
func prctl(option, arg2, arg3, arg4, arg5 int) (err error) {
_, _, e1 := syscall.Syscall6(syscall.SYS_PRCTL,
uintptr(option),
uintptr(arg2),
uintptr(arg3),
uintptr(arg4),
uintptr(arg5),
uintptr(0))
if e1 != 0 {
err = e1
}
return
}
// lastCap returns the integer corresponding to the highest known
// capability on this system.
func lastCap() (last int, err error) {
// contains a single integer followed by a newline
f, err := os.Open("/proc/sys/kernel/cap_last_cap")
if err != nil {
return
}
defer f.Close()
buf := make([]byte, 16)
l, err := f.Read(buf)
if err != nil {
return
}
if l >= 16 {
return 0, fmt.Errorf("cap_last_cap too long: %d", l)
}
return strconv.Atoi(strings.TrimSpace(string(buf[:l])))
}
// drop a capability from the current processes bounding set
func BoundingSetDrop(c Capability) error {
if err := prctl(syscall.PR_CAPBSET_DROP, int(c), 0, 0, 0); err != nil {
return err
}
return nil
}
func BoundingSetEmpty() (err error) {
// don't bother trying to empty the capability bounding set if
// we're not root, as we won't have the permissions.
// FIXME: instead of checking for root, we should be checking
// for the permission
if syscall.Geteuid() != 0 {
return
}
for i := CapMin; i <= CapMax; i++ {
BoundingSetDrop(i)
}
return nil
}
func DropAllCapabilities() error {
return capset()
}