Debian Codegraph @

capability/capability_linux.go

raw symbols SCIP
1 // Copyright 2023 The Capability Authors.
2 // Copyright 2013 Suryandaru Triandana <[email protected]>
3 // All rights reserved.
4 //
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
7
8 package capability
9
10 import (
11 "bufio"
12 "errors"
13 "fmt"
14 "io"
15 "os"
16 "strconv"
17 "strings"
18 "sync"
19 "syscall"
20 )
21
22 const (
23 linuxCapVer1 = 0x19980330 // No longer supported.
24 linuxCapVer2 = 0x20071026 // No longer supported.
25 linuxCapVer3 = 0x20080522
26 )
27
28 var lastCap = sync.OnceValues(func() (Cap, error) {
29 f, err := os.Open("/proc/sys/kernel/cap_last_cap")
30 if err != nil {
31 return 0, err
32 }
33
34 buf := make([]byte, 11)
35 l, err := f.Read(buf)
36 f.Close()
37 if err != nil {
38 return 0, err
39 }
40 buf = buf[:l]
41
42 last, err := strconv.Atoi(strings.TrimSpace(string(buf)))
43 if err != nil {
44 return 0, err
45 }
46 return Cap(last), nil
47 })
48
49 func capUpperMask() uint32 {
50 last, err := lastCap()
51 if err != nil || last < 32 {
52 return 0
53 }
54 return (uint32(1) << (uint(last) - 31)) - 1
55 }
56
57 func mkStringCap(c Capabilities, which CapType) (ret string) {
58 last, err := lastCap()
59 if err != nil {
60 return ""
61 }
62 for i, first := Cap(0), true; i <= last; i++ {
63 if !c.Get(which, i) {
64 continue
65 }
66 if first {
67 first = false
68 } else {
69 ret += ", "
70 }
71 ret += i.String()
72 }
73 return
74 }
75
76 func mkString(c Capabilities, max CapType) (ret string) {
77 ret = "{"
78 for i := CapType(1); i <= max; i <<= 1 {
79 ret += " " + i.String() + "=\""
80 if c.Empty(i) {
81 ret += "empty"
82 } else if c.Full(i) {
83 ret += "full"
84 } else {
85 ret += c.StringCap(i)
86 }
87 ret += "\""
88 }
89 ret += " }"
90 return
91 }
92
93 var capVersion = sync.OnceValues(func() (uint32, error) {
94 var hdr capHeader
95 err := capget(&hdr, nil)
96 return hdr.version, err
97 })
98
99 func newPid(pid int) (c Capabilities, retErr error) {
100 ver, err := capVersion()
101 if err != nil {
102 retErr = fmt.Errorf("unable to get capability version from the kernel: %w", err)
103 return
104 }
105 switch ver {
106 case linuxCapVer1, linuxCapVer2:
107 retErr = errors.New("old/unsupported capability version (kernel older than 2.6.26?)")
108 default:
109 // Either linuxCapVer3, or an unknown/future version (such as v4).
110 // In the latter case, we fall back to v3 as the latest version known
111 // to this package, as kernel should be backward-compatible to v3.
112 p := new(capsV3)
113 p.hdr.version = linuxCapVer3
114 p.hdr.pid = int32(pid)
115 c = p
116 }
117 return
118 }
119
120 func ignoreEINVAL(err error) error {
121 if errors.Is(err, syscall.EINVAL) {
122 err = nil
123 }
124 return err
125 }
126
127 type capsV3 struct {
128 hdr capHeader
129 data [2]capData
130 bounds [2]uint32
131 ambient [2]uint32
132 }
133
134 func (c *capsV3) Get(which CapType, what Cap) bool {
135 var i uint
136 if what > 31 {
137 i = uint(what) >> 5
138 what %= 32
139 }
140
141 switch which {
142 case EFFECTIVE:
143 return (1<<uint(what))&c.data[i].effective != 0
144 case PERMITTED:
145 return (1<<uint(what))&c.data[i].permitted != 0
146 case INHERITABLE:
147 return (1<<uint(what))&c.data[i].inheritable != 0
148 case BOUNDING:
149 return (1<<uint(what))&c.bounds[i] != 0
150 case AMBIENT:
151 return (1<<uint(what))&c.ambient[i] != 0
152 }
153
154 return false
155 }
156
157 func (c *capsV3) getData(which CapType, dest []uint32) {
158 switch which {
159 case EFFECTIVE:
160 dest[0] = c.data[0].effective
161 dest[1] = c.data[1].effective
162 case PERMITTED:
163 dest[0] = c.data[0].permitted
164 dest[1] = c.data[1].permitted
165 case INHERITABLE:
166 dest[0] = c.data[0].inheritable
167 dest[1] = c.data[1].inheritable
168 case BOUNDING:
169 dest[0] = c.bounds[0]
170 dest[1] = c.bounds[1]
171 case AMBIENT:
172 dest[0] = c.ambient[0]
173 dest[1] = c.ambient[1]
174 }
175 }
176
177 func (c *capsV3) Empty(which CapType) bool {
178 var data [2]uint32
179 c.getData(which, data[:])
180 return data[0] == 0 && data[1] == 0
181 }
182
183 func (c *capsV3) Full(which CapType) bool {
184 var data [2]uint32
185 c.getData(which, data[:])
186 if (data[0] & 0xffffffff) != 0xffffffff {
187 return false
188 }
189 mask := capUpperMask()
190 return (data[1] & mask) == mask
191 }
192
193 func (c *capsV3) Set(which CapType, caps ...Cap) {
194 for _, what := range caps {
195 var i uint
196 if what > 31 {
197 i = uint(what) >> 5
198 what %= 32
199 }
200
201 if which&EFFECTIVE != 0 {
202 c.data[i].effective |= 1 << uint(what)
203 }
204 if which&PERMITTED != 0 {
205 c.data[i].permitted |= 1 << uint(what)
206 }
207 if which&INHERITABLE != 0 {
208 c.data[i].inheritable |= 1 << uint(what)
209 }
210 if which&BOUNDING != 0 {
211 c.bounds[i] |= 1 << uint(what)
212 }
213 if which&AMBIENT != 0 {
214 c.ambient[i] |= 1 << uint(what)
215 }
216 }
217 }
218
219 func (c *capsV3) Unset(which CapType, caps ...Cap) {
220 for _, what := range caps {
221 var i uint
222 if what > 31 {
223 i = uint(what) >> 5
224 what %= 32
225 }
226
227 if which&EFFECTIVE != 0 {
228 c.data[i].effective &= ^(1 << uint(what))
229 }
230 if which&PERMITTED != 0 {
231 c.data[i].permitted &= ^(1 << uint(what))
232 }
233 if which&INHERITABLE != 0 {
234 c.data[i].inheritable &= ^(1 << uint(what))
235 }
236 if which&BOUNDING != 0 {
237 c.bounds[i] &= ^(1 << uint(what))
238 }
239 if which&AMBIENT != 0 {
240 c.ambient[i] &= ^(1 << uint(what))
241 }
242 }
243 }
244
245 func (c *capsV3) Fill(kind CapType) {
246 if kind&CAPS == CAPS {
247 c.data[0].effective = 0xffffffff
248 c.data[0].permitted = 0xffffffff
249 c.data[0].inheritable = 0
250 c.data[1].effective = 0xffffffff
251 c.data[1].permitted = 0xffffffff
252 c.data[1].inheritable = 0
253 }
254
255 if kind&BOUNDS == BOUNDS {
256 c.bounds[0] = 0xffffffff
257 c.bounds[1] = 0xffffffff
258 }
259 if kind&AMBS == AMBS {
260 c.ambient[0] = 0xffffffff
261 c.ambient[1] = 0xffffffff
262 }
263 }
264
265 func (c *capsV3) Clear(kind CapType) {
266 if kind&CAPS == CAPS {
267 c.data[0].effective = 0
268 c.data[0].permitted = 0
269 c.data[0].inheritable = 0
270 c.data[1].effective = 0
271 c.data[1].permitted = 0
272 c.data[1].inheritable = 0
273 }
274
275 if kind&BOUNDS == BOUNDS {
276 c.bounds[0] = 0
277 c.bounds[1] = 0
278 }
279 if kind&AMBS == AMBS {
280 c.ambient[0] = 0
281 c.ambient[1] = 0
282 }
283 }
284
285 func (c *capsV3) StringCap(which CapType) (ret string) {
286 return mkStringCap(c, which)
287 }
288
289 func (c *capsV3) String() (ret string) {
290 return mkString(c, BOUNDING)
291 }
292
293 func (c *capsV3) Load() (err error) {
294 err = capget(&c.hdr, &c.data[0])
295 if err != nil {
296 return
297 }
298
299 path := "/proc/self/status"
300 if c.hdr.pid != 0 {
301 path = fmt.Sprintf("/proc/%d/status", c.hdr.pid)
302 }
303
304 f, err := os.Open(path)
305 if err != nil {
306 return
307 }
308 b := bufio.NewReader(f)
309 for {
310 line, e := b.ReadString('\n')
311 if e != nil {
312 if e != io.EOF {
313 err = e
314 }
315 break
316 }
317 if val, ok := strings.CutPrefix(line, "CapBnd:\t"); ok {
318 _, err = fmt.Sscanf(val, "%08x%08x", &c.bounds[1], &c.bounds[0])
319 if err != nil {
320 break
321 }
322 continue
323 }
324 if val, ok := strings.CutPrefix(line, "CapAmb:\t"); ok {
325 _, err = fmt.Sscanf(val, "%08x%08x", &c.ambient[1], &c.ambient[0])
326 if err != nil {
327 break
328 }
329 continue
330 }
331 }
332 f.Close()
333
334 return
335 }
336
337 func (c *capsV3) Apply(kind CapType) error {
338 if c.hdr.pid != 0 {
339 return errors.New("unable to modify capabilities of another process")
340 }
341 last, err := LastCap()
342 if err != nil {
343 return err
344 }
345 if kind&BOUNDS == BOUNDS {
346 var data [2]capData
347 err = capget(&c.hdr, &data[0])
348 if err != nil {
349 return err
350 }
351 if (1<<uint(CAP_SETPCAP))&data[0].effective != 0 {
352 for i := Cap(0); i <= last; i++ {
353 if c.Get(BOUNDING, i) {
354 continue
355 }
356 // Ignore EINVAL since the capability may not be supported in this system.
357 err = ignoreEINVAL(dropBound(i))
358 if err != nil {
359 return err
360 }
361 }
362 }
363 }
364
365 if kind&CAPS == CAPS {
366 err = capset(&c.hdr, &c.data[0])
367 if err != nil {
368 return err
369 }
370 }
371
372 if kind&AMBS == AMBS {
373 // Ignore EINVAL as not supported on kernels before 4.3
374 err = ignoreEINVAL(resetAmbient())
375 if err != nil {
376 return err
377 }
378 for i := Cap(0); i <= last; i++ {
379 if !c.Get(AMBIENT, i) {
380 continue
381 }
382 // Ignore EINVAL as not supported on kernels before 4.3
383 err = ignoreEINVAL(setAmbient(true, i))
384 if err != nil {
385 return err
386 }
387 }
388 }
389
390 return nil
391 }
392
393 func getAmbient(c Cap) (bool, error) {
394 res, err := prctlRetInt(pr_CAP_AMBIENT, pr_CAP_AMBIENT_IS_SET, uintptr(c))
395 if err != nil {
396 return false, err
397 }
398 return res > 0, nil
399 }
400
401 func setAmbient(raise bool, caps ...Cap) error {
402 op := pr_CAP_AMBIENT_RAISE
403 if !raise {
404 op = pr_CAP_AMBIENT_LOWER
405 }
406 for _, val := range caps {
407 err := prctl(pr_CAP_AMBIENT, op, uintptr(val))
408 if err != nil {
409 return err
410 }
411 }
412 return nil
413 }
414
415 func resetAmbient() error {
416 return prctl(pr_CAP_AMBIENT, pr_CAP_AMBIENT_CLEAR_ALL, 0)
417 }
418
419 func getBound(c Cap) (bool, error) {
420 res, err := prctlRetInt(syscall.PR_CAPBSET_READ, uintptr(c), 0)
421 if err != nil {
422 return false, err
423 }
424 return res > 0, nil
425 }
426
427 func dropBound(caps ...Cap) error {
428 for _, val := range caps {
429 err := prctl(syscall.PR_CAPBSET_DROP, uintptr(val), 0)
430 if err != nil {
431 return err
432 }
433 }
434 return nil
435 }
436
437 func newFile(path string) (c Capabilities, err error) {
438 c = &capsFile{path: path}
439 return
440 }
441
442 type capsFile struct {
443 path string
444 data vfscapData
445 }
446
447 func (c *capsFile) Get(which CapType, what Cap) bool {
448 var i uint
449 if what > 31 {
450 if c.data.version == 1 {
451 return false
452 }
453 i = uint(what) >> 5
454 what %= 32
455 }
456
457 switch which {
458 case EFFECTIVE:
459 return (1<<uint(what))&c.data.effective[i] != 0
460 case PERMITTED:
461 return (1<<uint(what))&c.data.data[i].permitted != 0
462 case INHERITABLE:
463 return (1<<uint(what))&c.data.data[i].inheritable != 0
464 }
465
466 return false
467 }
468
469 func (c *capsFile) getData(which CapType, dest []uint32) {
470 switch which {
471 case EFFECTIVE:
472 dest[0] = c.data.effective[0]
473 dest[1] = c.data.effective[1]
474 case PERMITTED:
475 dest[0] = c.data.data[0].permitted
476 dest[1] = c.data.data[1].permitted
477 case INHERITABLE:
478 dest[0] = c.data.data[0].inheritable
479 dest[1] = c.data.data[1].inheritable
480 }
481 }
482
483 func (c *capsFile) Empty(which CapType) bool {
484 var data [2]uint32
485 c.getData(which, data[:])
486 return data[0] == 0 && data[1] == 0
487 }
488
489 func (c *capsFile) Full(which CapType) bool {
490 var data [2]uint32
491 c.getData(which, data[:])
492 if c.data.version == 0 {
493 return (data[0] & 0x7fffffff) == 0x7fffffff
494 }
495 if (data[0] & 0xffffffff) != 0xffffffff {
496 return false
497 }
498 mask := capUpperMask()
499 return (data[1] & mask) == mask
500 }
501
502 func (c *capsFile) Set(which CapType, caps ...Cap) {
503 for _, what := range caps {
504 var i uint
505 if what > 31 {
506 if c.data.version == 1 {
507 continue
508 }
509 i = uint(what) >> 5
510 what %= 32
511 }
512
513 if which&EFFECTIVE != 0 {
514 c.data.effective[i] |= 1 << uint(what)
515 }
516 if which&PERMITTED != 0 {
517 c.data.data[i].permitted |= 1 << uint(what)
518 }
519 if which&INHERITABLE != 0 {
520 c.data.data[i].inheritable |= 1 << uint(what)
521 }
522 }
523 }
524
525 func (c *capsFile) Unset(which CapType, caps ...Cap) {
526 for _, what := range caps {
527 var i uint
528 if what > 31 {
529 if c.data.version == 1 {
530 continue
531 }
532 i = uint(what) >> 5
533 what %= 32
534 }
535
536 if which&EFFECTIVE != 0 {
537 c.data.effective[i] &= ^(1 << uint(what))
538 }
539 if which&PERMITTED != 0 {
540 c.data.data[i].permitted &= ^(1 << uint(what))
541 }
542 if which&INHERITABLE != 0 {
543 c.data.data[i].inheritable &= ^(1 << uint(what))
544 }
545 }
546 }
547
548 func (c *capsFile) Fill(kind CapType) {
549 if kind&CAPS == CAPS {
550 c.data.effective[0] = 0xffffffff
551 c.data.data[0].permitted = 0xffffffff
552 c.data.data[0].inheritable = 0
553 if c.data.version == 2 {
554 c.data.effective[1] = 0xffffffff
555 c.data.data[1].permitted = 0xffffffff
556 c.data.data[1].inheritable = 0
557 }
558 }
559 }
560
561 func (c *capsFile) Clear(kind CapType) {
562 if kind&CAPS == CAPS {
563 c.data.effective[0] = 0
564 c.data.data[0].permitted = 0
565 c.data.data[0].inheritable = 0
566 if c.data.version == 2 {
567 c.data.effective[1] = 0
568 c.data.data[1].permitted = 0
569 c.data.data[1].inheritable = 0
570 }
571 }
572 }
573
574 func (c *capsFile) StringCap(which CapType) (ret string) {
575 return mkStringCap(c, which)
576 }
577
578 func (c *capsFile) String() (ret string) {
579 return mkString(c, INHERITABLE)
580 }
581
582 func (c *capsFile) Load() (err error) {
583 return getVfsCap(c.path, &c.data)
584 }
585
586 func (c *capsFile) Apply(kind CapType) (err error) {
587 if kind&CAPS == CAPS {
588 return setVfsCap(c.path, &c.data)
589 }
590 return
591 }