Debian Codegraph @

capability/capability_linux.go

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