Debian Codegraph @

capability/capability.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 provides utilities for manipulating POSIX capabilities.
9 package capability
10
11 type Capabilities interface {
12 // Get check whether a capability present in the given
13 // capabilities set. The 'which' value should be one of EFFECTIVE,
14 // PERMITTED, INHERITABLE, BOUNDING or AMBIENT.
15 Get(which CapType, what Cap) bool
16
17 // Empty check whether all capability bits of the given capabilities
18 // set are zero. The 'which' value should be one of EFFECTIVE,
19 // PERMITTED, INHERITABLE, BOUNDING or AMBIENT.
20 Empty(which CapType) bool
21
22 // Full check whether all capability bits of the given capabilities
23 // set are one. The 'which' value should be one of EFFECTIVE,
24 // PERMITTED, INHERITABLE, BOUNDING or AMBIENT.
25 Full(which CapType) bool
26
27 // Set sets capabilities of the given capabilities sets. The
28 // 'which' value should be one or combination (OR'ed) of EFFECTIVE,
29 // PERMITTED, INHERITABLE, BOUNDING or AMBIENT.
30 Set(which CapType, caps ...Cap)
31
32 // Unset unsets capabilities of the given capabilities sets. The
33 // 'which' value should be one or combination (OR'ed) of EFFECTIVE,
34 // PERMITTED, INHERITABLE, BOUNDING or AMBIENT.
35 Unset(which CapType, caps ...Cap)
36
37 // Fill sets all bits of the given capabilities kind to one. The
38 // 'kind' value should be one or combination (OR'ed) of CAPS,
39 // BOUNDS or AMBS.
40 Fill(kind CapType)
41
42 // Clear sets all bits of the given capabilities kind to zero. The
43 // 'kind' value should be one or combination (OR'ed) of CAPS,
44 // BOUNDS or AMBS.
45 Clear(kind CapType)
46
47 // String return current capabilities state of the given capabilities
48 // set as string. The 'which' value should be one of EFFECTIVE,
49 // PERMITTED, INHERITABLE BOUNDING or AMBIENT
50 StringCap(which CapType) string
51
52 // String return current capabilities state as string.
53 String() string
54
55 // Load load actual capabilities value. This will overwrite all
56 // outstanding changes.
57 Load() error
58
59 // Apply apply the capabilities settings, so all changes made by
60 // [Set], [Unset], [Fill], or [Clear] will take effect.
61 Apply(kind CapType) error
62 }
63
64 // NewPid initializes a new [Capabilities] object for given pid when
65 // it is nonzero, or for the current process if pid is 0.
66 //
67 // Deprecated: replace with [NewPid2] followed by optional [Capabilities.Load]
68 // (only if needed). For example, replace:
69 //
70 // c, err := NewPid(0)
71 // if err != nil {
72 // return err
73 // }
74 //
75 // with:
76 //
77 // c, err := NewPid2(0)
78 // if err != nil {
79 // return err
80 // }
81 // err = c.Load()
82 // if err != nil {
83 // return err
84 // }
85 func NewPid(pid int) (Capabilities, error) {
86 c, err := newPid(pid)
87 if err != nil {
88 return c, err
89 }
90 err = c.Load()
91 return c, err
92 }
93
94 // NewPid2 initializes a new [Capabilities] object for given pid when
95 // it is nonzero, or for the current process if pid is 0. This
96 // does not load the process's current capabilities; if needed,
97 // call [Capabilities.Load].
98 func NewPid2(pid int) (Capabilities, error) {
99 return newPid(pid)
100 }
101
102 // NewFile initializes a new Capabilities object for given file path.
103 //
104 // Deprecated: replace with [NewFile2] followed by optional [Capabilities.Load]
105 // (only if needed). For example, replace:
106 //
107 // c, err := NewFile(path)
108 // if err != nil {
109 // return err
110 // }
111 //
112 // with:
113 //
114 // c, err := NewFile2(path)
115 // if err != nil {
116 // return err
117 // }
118 // err = c.Load()
119 // if err != nil {
120 // return err
121 // }
122 func NewFile(path string) (Capabilities, error) {
123 c, err := newFile(path)
124 if err != nil {
125 return c, err
126 }
127 err = c.Load()
128 return c, err
129 }
130
131 // NewFile2 creates a new initialized [Capabilities] object for given
132 // file path. This does not load the process's current capabilities;
133 // if needed, call [Capabilities.Load].
134 func NewFile2(path string) (Capabilities, error) {
135 return newFile(path)
136 }
137
138 // LastCap returns highest valid capability of the running kernel,
139 // or an error if it can not be obtained.
140 //
141 // See also: [ListSupported].
142 func LastCap() (Cap, error) {
143 return lastCap()
144 }
145
146 // GetAmbient determines if a specific ambient capability is raised in the
147 // calling thread.
148 func GetAmbient(c Cap) (bool, error) {
149 return getAmbient(c)
150 }
151
152 // SetAmbient raises or lowers specified ambient capabilities for the calling
153 // thread. To complete successfully, the prevailing effective capability set
154 // must have a raised CAP_SETPCAP. Further, to raise a specific ambient
155 // capability the inheritable and permitted sets of the calling thread must
156 // already contain the specified capability.
157 func SetAmbient(raise bool, caps ...Cap) error {
158 return setAmbient(raise, caps...)
159 }
160
161 // ResetAmbient resets all of the ambient capabilities for the calling thread
162 // to their lowered value.
163 func ResetAmbient() error {
164 return resetAmbient()
165 }
166
167 // GetBound determines if a specific bounding capability is raised in the
168 // calling thread.
169 func GetBound(c Cap) (bool, error) {
170 return getBound(c)
171 }
172
173 // DropBound lowers the specified bounding set capability.
174 func DropBound(caps ...Cap) error {
175 return dropBound(caps...)
176 }