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
use super::{Cap, CapSet};
#[inline]
pub fn raise(cap: Cap) -> crate::Result<()> {
unsafe {
crate::raw_prctl(
libc::PR_CAP_AMBIENT,
libc::PR_CAP_AMBIENT_RAISE as libc::c_ulong,
cap as libc::c_ulong,
0,
0,
)?;
}
Ok(())
}
#[inline]
pub fn lower(cap: Cap) -> crate::Result<()> {
unsafe {
crate::raw_prctl(
libc::PR_CAP_AMBIENT,
libc::PR_CAP_AMBIENT_LOWER as libc::c_ulong,
cap as libc::c_ulong,
0,
0,
)?;
}
Ok(())
}
#[inline]
pub fn is_set(cap: Cap) -> Option<bool> {
match unsafe {
crate::raw_prctl_opt(
libc::PR_CAP_AMBIENT,
libc::PR_CAP_AMBIENT_IS_SET as libc::c_ulong,
cap as libc::c_ulong,
0,
0,
)
} {
Some(res) => Some(res != 0),
None => {
#[cfg(not(feature = "sc"))]
debug_assert_eq!(unsafe { *libc::__errno_location() }, libc::EINVAL);
None
}
}
}
#[inline]
pub fn clear() -> crate::Result<()> {
unsafe {
crate::raw_prctl(
libc::PR_CAP_AMBIENT,
libc::PR_CAP_AMBIENT_CLEAR_ALL as libc::c_ulong,
0,
0,
0,
)?;
}
Ok(())
}
#[inline]
pub fn is_supported() -> bool {
is_set(Cap::CHOWN).is_some()
}
pub fn probe() -> Option<CapSet> {
let mut set = CapSet::empty();
for cap in Cap::iter() {
match is_set(cap) {
Some(true) => set.add(cap),
Some(false) => (),
None => {
if cap as u8 == 0 {
return None;
} else {
break;
}
}
}
}
Some(set)
}
pub fn clear_unknown() -> crate::Result<()> {
for cap in (super::CAP_MAX as libc::c_ulong + 1)..(super::CAP_MAX as libc::c_ulong * 2) {
match unsafe {
crate::raw_prctl(
libc::PR_CAP_AMBIENT,
libc::PR_CAP_AMBIENT_LOWER as libc::c_ulong,
cap,
0,
0,
)
} {
Ok(_) => (),
Err(e) if e.code() == libc::EINVAL => return Ok(()),
Err(e) => return Err(e),
}
}
Err(crate::Error::from_code(libc::E2BIG))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ambient_supported() {
if is_supported() {
let orig_caps = probe().unwrap();
let supported_caps = Cap::probe_supported();
for cap in Cap::iter() {
if supported_caps.has(cap) {
assert_eq!(is_set(cap), Some(orig_caps.has(cap)), "{:?}", cap);
} else {
assert_eq!(is_set(cap), None, "{:?}", cap);
}
}
clear_unknown().unwrap();
assert_eq!(probe().unwrap(), orig_caps);
clear().unwrap();
assert_eq!(probe().unwrap(), CapSet::empty());
let orig_state = crate::caps::CapState::get_current().unwrap();
let mut state = orig_state;
state.inheritable = state.permitted;
state.set_current().unwrap();
for cap in state.inheritable {
raise(cap).unwrap();
}
state.inheritable.clear();
state.set_current().unwrap();
assert_eq!(probe().unwrap(), CapSet::empty());
for cap in supported_caps {
assert_eq!(raise(cap).unwrap_err().code(), libc::EPERM);
}
orig_state.set_current().unwrap();
for cap in orig_caps.iter() {
raise(cap).unwrap();
}
for cap in (supported_caps - orig_caps).iter() {
lower(cap).unwrap();
}
for cap in !supported_caps {
assert_eq!(raise(cap).unwrap_err().code(), libc::EINVAL);
assert_eq!(lower(cap).unwrap_err().code(), libc::EINVAL);
}
} else {
assert_eq!(probe(), None);
assert_eq!(raise(Cap::CHOWN).unwrap_err().code(), libc::EINVAL);
assert_eq!(lower(Cap::CHOWN).unwrap_err().code(), libc::EINVAL);
assert_eq!(clear().unwrap_err().code(), libc::EINVAL);
assert_eq!(clear_unknown().unwrap_err().code(), libc::EINVAL);
}
}
}