| 1 |
// Testable examples. |
| 2 |
|
| 3 |
package cap_test |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"log" |
| 8 |
"syscall" |
| 9 |
|
| 10 |
"kernel.org/pub/linux/libs/security/libcap/cap" |
| 11 |
) |
| 12 |
|
| 13 |
func ExampleSet_Fill() { |
| 14 |
c, err := cap.FromText("cap_setfcap=p") |
| 15 |
if err != nil { |
| 16 |
log.Fatalf("failed to parse: %v", err) |
| 17 |
} |
| 18 |
c.Fill(cap.Effective, cap.Permitted) |
| 19 |
c.ClearFlag(cap.Permitted) |
| 20 |
c.Fill(cap.Inheritable, cap.Effective) |
| 21 |
c.ClearFlag(cap.Effective) |
| 22 |
fmt.Println(c) |
| 23 |
// Output: cap_setfcap=i |
| 24 |
} |
| 25 |
|
| 26 |
func ExampleGetProc() { |
| 27 |
c := cap.GetProc() |
| 28 |
fmt.Printf("current process has these capabilities: %q\n", c) |
| 29 |
} |
| 30 |
|
| 31 |
func ExampleNewSet() { |
| 32 |
c := cap.NewSet() |
| 33 |
fmt.Printf("empty capability Set is: %q\n", c) |
| 34 |
// Output: empty capability Set is: "=" |
| 35 |
} |
| 36 |
|
| 37 |
func ExampleMaxBits() { |
| 38 |
bits := cap.MaxBits() |
| 39 |
fmt.Printf("current kernel supports %d capabilities\n", bits) |
| 40 |
fmt.Printf("the most recently added is %d (%q)\n", bits-1, bits-1) |
| 41 |
fmt.Println("read documentation for it with commands like these:") |
| 42 |
fmt.Printf("\n capsh --explain=%d\n", bits-1) |
| 43 |
fmt.Printf(" capsh --explain=%q\n", bits-1) |
| 44 |
} |
| 45 |
|
| 46 |
func ExampleIABGetProc() { |
| 47 |
iab := cap.IABGetProc() |
| 48 |
fmt.Printf("process inheritable IAB tuple is: [%s]\n", iab) |
| 49 |
} |
| 50 |
|
| 51 |
func ExampleNewIAB() { |
| 52 |
iab := cap.NewIAB() |
| 53 |
fmt.Printf("empty IAB tuple is: [%v]\n", iab) |
| 54 |
// Output: empty IAB tuple is: [] |
| 55 |
} |
| 56 |
|
| 57 |
func ExampleSet_Export() { |
| 58 |
c, err := cap.FromText("cap_setuid=ep") |
| 59 |
if err != nil { |
| 60 |
log.Fatalf("failed to parse: %v", err) |
| 61 |
} |
| 62 |
b1, err := c.Export() |
| 63 |
if err != nil { |
| 64 |
log.Fatalf("export failed: %v", err) |
| 65 |
} |
| 66 |
fmt.Printf("default %q export: %02x\n", c, b1) |
| 67 |
cap.MinExtFlagSize = 0 |
| 68 |
b2, err := c.Export() |
| 69 |
if err != nil { |
| 70 |
log.Fatalf("export failed: %v", err) |
| 71 |
} |
| 72 |
fmt.Printf("minimal %q export: %02x\n", c, b2) |
| 73 |
// Output: |
| 74 |
// default "cap_setuid=ep" export: 90c2015108808000000000000000000000000000000000000000000000 |
| 75 |
// minimal "cap_setuid=ep" export: 90c2015101808000 |
| 76 |
} |
| 77 |
|
| 78 |
func ExampleImport() { |
| 79 |
d := []byte{0x90, 0xc2, 0x01, 0x51, 0x01, 0x80, 0x80, 0x00} |
| 80 |
c, err := cap.Import(d) |
| 81 |
if err != nil { |
| 82 |
log.Fatalf("failed to parse: %v", err) |
| 83 |
} |
| 84 |
fmt.Printf("import of %02x is %q\n", d, c) |
| 85 |
// Output: import of 90c2015101808000 is "cap_setuid=ep" |
| 86 |
} |
| 87 |
|
| 88 |
func ExampleSetUID() { |
| 89 |
c := cap.GetProc() |
| 90 |
if on, err := c.GetFlag(cap.Permitted, cap.SETUID); err != nil { |
| 91 |
fmt.Printf("unable to determine cap_setuid permitted flag value: %v\n", err) |
| 92 |
return |
| 93 |
} else if !on { |
| 94 |
fmt.Println("no permitted capability, try: sudo setcap cap_setuid=p program") |
| 95 |
return |
| 96 |
} |
| 97 |
pre := syscall.Getuid() |
| 98 |
const nobodyUID = 65534 |
| 99 |
if err := cap.SetUID(nobodyUID); err != nil { |
| 100 |
fmt.Printf("failed to set uid (%d): %v\n", nobodyUID, err) |
| 101 |
return |
| 102 |
} |
| 103 |
post := syscall.Getuid() |
| 104 |
fmt.Printf("UID was:%d, is now:%d (nobody=%v)\n", pre, post, post == nobodyUID) |
| 105 |
c = cap.GetProc() |
| 106 |
fmt.Printf("all effective capabilities should be lowered: %q\n", c) |
| 107 |
} |
| 108 |
|
| 109 |
func ExampleFromText() { |
| 110 |
ts := []string{"=p all+ei", "all=pie", "=pi all+e", "=eip"} |
| 111 |
expect, err := cap.FromText("=ie all+p") |
| 112 |
good := true |
| 113 |
if err != nil { |
| 114 |
fmt.Printf("failed to parse expected set: %v\n", err) |
| 115 |
good = false |
| 116 |
} |
| 117 |
for _, t := range ts { |
| 118 |
got, err := cap.FromText(t) |
| 119 |
if err != nil { |
| 120 |
fmt.Printf("failed to parse %q: %v\n", t, err) |
| 121 |
good = false |
| 122 |
} |
| 123 |
if cf, err := got.Cf(expect); err != nil || cf != 0 { |
| 124 |
fmt.Printf("comparison failed for %q got %q (want: %q): %v\n", t, got, expect, err) |
| 125 |
good = false |
| 126 |
} |
| 127 |
} |
| 128 |
fmt.Printf("all examples passed=%v\n", good) |
| 129 |
// Output: all examples passed=true |
| 130 |
} |
| 131 |
|
| 132 |
func ExampleFromName() { |
| 133 |
v := cap.SYS_CHROOT |
| 134 |
t, err := cap.FromName(v.String()) |
| 135 |
if err != nil { |
| 136 |
fmt.Printf("failed to parse %q: %v", v, err) |
| 137 |
return |
| 138 |
} |
| 139 |
fmt.Printf("capability value %q of numerical value=%d correctly named\n", v, t) |
| 140 |
// Output: capability value "cap_sys_chroot" of numerical value=18 correctly named |
| 141 |
} |