Commit c8ba796
libselinux: do not return the cached prev_current value when using getpidcon()
libselinux implements a cache mechanism for get*con() functions, such
that when a thread calls setcon(...) then getcon(...), the context is
directly returned. Unfortunately, getpidcon(pid, &context) uses the same
cached variable, so when a program uses setcon("something"), all later
calls to getpidcon(pid, ...) returns "something". This is a bug.
Here is a program which illustrates this bug:
#include <stdio.h>
#include <selinux/selinux.h>
int main() {
char *context = "";
if (getpidcon(1, &context) < 0) {
perror("getpidcon(1)");
}
printf("getpidcon(1) = %s\n", context);
if (getcon(&context) < 0) {
perror("getcon()");
}
printf("getcon() = %s\n", context);
if (setcon(context) < 0) {
perror("setcon()");
}
if (getpidcon(1, &context) < 0) {
perror("getpidcon(1)");
}
printf("getpidcon(1) = %s\n", context);
return 0;
}
On an Arch Linux system using unconfined user, this program displays:
getpidcon(1) = system_u:system_r:init_t
getcon() = unconfined_u:unconfined_r:unconfined_t
getpidcon(1) = unconfined_u:unconfined_r:unconfined_t
With this commit, this program displays:
getpidcon(1) = system_u:system_r:init_t
getcon() = unconfined_u:unconfined_r:unconfined_t
getpidcon(1) = system_u:system_r:init_t
This bug was present in the first commit of
https://github.com/SELinuxProject/selinux git history. It was reported
in https://lore.kernel.org/selinux/20220121084012.GS7643@suse.com/ and a
patch to fix it was sent in
https://patchwork.kernel.org/project/selinux/patch/20220127130741.31940-1-jsegitz@suse.de/
without a clear explanation. This patch added pid checks, which made
sense but were difficult to read. Instead, it is possible to change the
way the functions are called so that they directly know which cache
variable to use.
Moreover, as the code is not clear at all (I spent too much time trying
to understand what the switch did and what the thread-local variable
contained), this commit also reworks libselinux/src/procattr.c to:
- not use hard-to-understand switch/case constructions on strings (they
are replaced by a new argument filled by macros)
- remove getpidattr_def macro (it was only used once, for pidcon, and
the code is clearer with one less macro)
- remove the pid parameter of setprocattrcon() and setprocattrcon_raw()
(it is always zero)
Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
Cc: Johannes Segitz <jsegitz@suse.de>1 parent de28525 commit c8ba796
1 file changed
+50
-97
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
14 | 17 | | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
19 | 22 | | |
20 | 23 | | |
21 | 24 | | |
| |||
111 | 114 | | |
112 | 115 | | |
113 | 116 | | |
114 | | - | |
115 | | - | |
| 117 | + | |
| 118 | + | |
116 | 119 | | |
117 | 120 | | |
118 | 121 | | |
119 | 122 | | |
120 | 123 | | |
121 | 124 | | |
122 | | - | |
123 | 125 | | |
124 | 126 | | |
125 | 127 | | |
126 | 128 | | |
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 | 129 | | |
152 | 130 | | |
153 | 131 | | |
| |||
194 | 172 | | |
195 | 173 | | |
196 | 174 | | |
197 | | - | |
198 | | - | |
| 175 | + | |
| 176 | + | |
199 | 177 | | |
200 | 178 | | |
201 | 179 | | |
202 | 180 | | |
203 | | - | |
| 181 | + | |
204 | 182 | | |
205 | 183 | | |
206 | 184 | | |
| |||
210 | 188 | | |
211 | 189 | | |
212 | 190 | | |
213 | | - | |
214 | | - | |
| 191 | + | |
| 192 | + | |
215 | 193 | | |
216 | 194 | | |
217 | 195 | | |
218 | 196 | | |
219 | | - | |
| 197 | + | |
220 | 198 | | |
221 | 199 | | |
222 | 200 | | |
223 | 201 | | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | 202 | | |
246 | 203 | | |
247 | 204 | | |
248 | 205 | | |
249 | 206 | | |
250 | 207 | | |
251 | | - | |
| 208 | + | |
252 | 209 | | |
253 | 210 | | |
254 | 211 | | |
| |||
279 | 236 | | |
280 | 237 | | |
281 | 238 | | |
282 | | - | |
283 | | - | |
| 239 | + | |
| 240 | + | |
284 | 241 | | |
285 | 242 | | |
286 | 243 | | |
287 | 244 | | |
288 | 245 | | |
289 | 246 | | |
290 | 247 | | |
291 | | - | |
| 248 | + | |
292 | 249 | | |
293 | 250 | | |
294 | 251 | | |
295 | 252 | | |
296 | 253 | | |
297 | 254 | | |
298 | | - | |
| 255 | + | |
299 | 256 | | |
300 | 257 | | |
301 | | - | |
| 258 | + | |
302 | 259 | | |
303 | 260 | | |
304 | 261 | | |
305 | | - | |
| 262 | + | |
306 | 263 | | |
307 | 264 | | |
308 | | - | |
| 265 | + | |
309 | 266 | | |
310 | 267 | | |
311 | | - | |
| 268 | + | |
312 | 269 | | |
313 | 270 | | |
314 | 271 | | |
315 | | - | |
| 272 | + | |
316 | 273 | | |
317 | 274 | | |
318 | | - | |
319 | | - | |
320 | | - | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
321 | 278 | | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
341 | 285 | | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | | - | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
349 | 294 | | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
0 commit comments