Debian Codegraph @

src/test/change_id_test.c

raw
1 /* change_id_test.c -- capng_change_id additional group tests
2 * Copyright 2026 Red Hat Inc.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; see the file COPYING.LIB. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
18 * Boston, MA 02110-1335, USA.
19 */
20
21 #include "config.h"
22 #include "../cap-ng.h"
23 #include <grp.h>
24 #include <pwd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/prctl.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31
32 static void fail(const char *msg)
33 {
34 fprintf(stderr, "%s\n", msg);
35 _exit(EXIT_FAILURE);
36 }
37
38 static int gid_in_list(const gid_t *gids, size_t count, gid_t gid)
39 {
40 size_t i;
41
42 for (i = 0; i < count; i++) {
43 if (gids[i] == gid)
44 return 1;
45 }
46 return 0;
47 }
48
49 static int rc_in_list(int rc, const int *allowed, size_t count)
50 {
51 size_t i;
52
53 for (i = 0; i < count; i++) {
54 if (rc == allowed[i])
55 return 1;
56 }
57 return 0;
58 }
59
60 static int get_current_groups(gid_t **gids, size_t *count)
61 {
62 int ngroups;
63
64 *gids = NULL;
65 *count = 0;
66
67 ngroups = getgroups(0, NULL);
68 if (ngroups < 0)
69 return -1;
70 if (ngroups == 0)
71 return 0;
72
73 *gids = malloc(sizeof(gid_t) * ngroups);
74 if (*gids == NULL)
75 return -1;
76 ngroups = getgroups(ngroups, *gids);
77 if (ngroups < 0) {
78 free(*gids);
79 *gids = NULL;
80 return -1;
81 }
82 *count = ngroups;
83 return 0;
84 }
85
86 static int get_natural_groups(uid_t uid, gid_t gid, gid_t **gids, size_t *count)
87 {
88 struct passwd *pw;
89 gid_t *list;
90 int ngroups = 1;
91 int rc;
92
93 *gids = NULL;
94 *count = 0;
95
96 pw = getpwuid(uid);
97 if (pw == NULL)
98 return -1;
99
100 list = malloc(sizeof(gid_t));
101 if (list == NULL)
102 return -1;
103
104 rc = getgrouplist(pw->pw_name, gid, list, &ngroups);
105 if (rc == -1) {
106 gid_t *tmp;
107
108 tmp = realloc(list, sizeof(gid_t) * ngroups);
109 if (tmp == NULL) {
110 free(list);
111 return -1;
112 }
113 list = tmp;
114 rc = getgrouplist(pw->pw_name, gid, list, &ngroups);
115 }
116 if (rc == -1) {
117 free(list);
118 return -1;
119 }
120
121 *gids = list;
122 *count = ngroups;
123 return 0;
124 }
125
126 static int merge_groups(const gid_t *base, size_t base_cnt,
127 const gid_t *extra, size_t extra_cnt, gid_t **merged,
128 size_t *merged_cnt)
129 {
130 gid_t *list;
131 size_t i, count = 0, total = base_cnt + extra_cnt;
132
133 *merged = NULL;
134 *merged_cnt = 0;
135 if (total == 0)
136 return 0;
137
138 list = malloc(sizeof(gid_t) * total);
139 if (list == NULL)
140 return -1;
141
142 for (i = 0; i < base_cnt; i++) {
143 if (gid_in_list(list, count, base[i]) == 0)
144 list[count++] = base[i];
145 }
146 for (i = 0; i < extra_cnt; i++) {
147 if (gid_in_list(list, count, extra[i]) == 0)
148 list[count++] = extra[i];
149 }
150
151 *merged = list;
152 *merged_cnt = count;
153 return 0;
154 }
155
156 static void check_groups(const gid_t *expected, size_t expected_cnt)
157 {
158 gid_t *actual;
159 size_t actual_cnt;
160 size_t i;
161
162 if (get_current_groups(&actual, &actual_cnt))
163 fail("Failed to get current additional groups");
164 if (actual_cnt != expected_cnt)
165 fail("Unexpected additional group count");
166 for (i = 0; i < expected_cnt; i++) {
167 if (actual[i] != expected[i])
168 fail("Unexpected additional group value");
169 }
170 free(actual);
171 }
172
173 static int choose_extra_gid(const gid_t *skip, size_t skip_cnt, gid_t *gid)
174 {
175 struct group *gr;
176
177 setgrent();
178 while ((gr = getgrent()) != NULL) {
179 if (gid_in_list(skip, skip_cnt, gr->gr_gid) == 0) {
180 *gid = gr->gr_gid;
181 endgrent();
182 return 0;
183 }
184 }
185 endgrent();
186 return -1;
187 }
188
189 static int read_bounding_cap(unsigned int cap)
190 {
191 #ifdef PR_CAPBSET_READ
192 return prctl(PR_CAPBSET_READ, cap, 0, 0, 0);
193 #else
194 (void)cap;
195 return -1;
196 #endif
197 }
198
199 static int find_drop_test_bounding_cap(unsigned int *cap)
200 {
201 unsigned int i;
202
203 for (i = 0; cap_valid(i); i++) {
204 int rc;
205
206 if (i == CAP_SETPCAP || i == CAP_SETUID || i == CAP_SETGID)
207 continue;
208 rc = read_bounding_cap(i);
209 if (rc < 0)
210 return -1;
211 if (rc == 1) {
212 *cap = i;
213 return 0;
214 }
215 }
216 for (i = 0; cap_valid(i); i++) {
217 int rc;
218
219 rc = read_bounding_cap(i);
220 if (rc < 0)
221 return -1;
222 if (rc == 1) {
223 *cap = i;
224 return 0;
225 }
226 }
227 return -1;
228 }
229
230 static void test_staged_only(void)
231 {
232 static const int allowed[] = { -2, -3, -14 };
233 gid_t staged[2];
234 gid_t *natural = NULL;
235 size_t natural_cnt = 0;
236 size_t staged_cnt = 1;
237 int rc;
238
239 if (capng_get_caps_process())
240 fail("Failed to initialize libcap-ng state");
241 if (get_natural_groups(getuid(), getgid(), &natural, &natural_cnt))
242 fail("Failed to resolve natural groups");
243
244 staged[0] = getgid();
245 if (choose_extra_gid(natural, natural_cnt, &staged[1]) == 0)
246 staged_cnt = 2;
247 free(natural);
248
249 rc = capng_stage_additional_groups(staged, staged_cnt);
250 if (rc)
251 fail("Failed to stage additional groups");
252 rc = capng_change_id(-1, -1, CAPNG_APPLY_STAGED_GROUPS);
253 if (rc == 0)
254 check_groups(staged, staged_cnt);
255 else if (rc_in_list(rc, allowed, sizeof(allowed) / sizeof(int)) == 0)
256 fail("Unexpected staged-only return code");
257 }
258
259 static void test_init_only(void)
260 {
261 static const int allowed[] = { -2, -3, -5 };
262 gid_t *natural = NULL;
263 size_t natural_cnt = 0;
264 int rc;
265
266 if (capng_get_caps_process())
267 fail("Failed to initialize libcap-ng state");
268 if (get_natural_groups(getuid(), getgid(), &natural, &natural_cnt))
269 fail("Failed to resolve natural groups");
270
271 rc = capng_change_id(getuid(), -1, CAPNG_INIT_SUPP_GRP);
272 if (rc == 0)
273 check_groups(natural, natural_cnt);
274 else if (rc_in_list(rc, allowed, sizeof(allowed) / sizeof(int)) == 0)
275 fail("Unexpected init-only return code");
276 free(natural);
277 }
278
279 static void test_init_and_staged(void)
280 {
281 static const int allowed[] = { -2, -3, -14 };
282 gid_t staged[2];
283 gid_t *natural = NULL, *merged = NULL;
284 size_t natural_cnt = 0, merged_cnt = 0;
285 size_t staged_cnt = 1;
286 int rc;
287
288 if (capng_get_caps_process())
289 fail("Failed to initialize libcap-ng state");
290 if (get_natural_groups(getuid(), getgid(), &natural, &natural_cnt))
291 fail("Failed to resolve natural groups");
292
293 staged[0] = getgid();
294 if (choose_extra_gid(natural, natural_cnt, &staged[1]) == 0)
295 staged_cnt = 2;
296 if (merge_groups(natural, natural_cnt, staged, staged_cnt,
297 &merged, &merged_cnt))
298 fail("Failed to merge expected additional groups");
299
300 rc = capng_stage_additional_groups(staged, staged_cnt);
301 if (rc)
302 fail("Failed to stage additional groups");
303 rc = capng_change_id(getuid(), -1,
304 CAPNG_INIT_SUPP_GRP |
305 CAPNG_APPLY_STAGED_GROUPS);
306 if (rc == 0)
307 check_groups(merged, merged_cnt);
308 else if (rc_in_list(rc, allowed, sizeof(allowed) / sizeof(int)) == 0)
309 fail("Unexpected init+staged return code");
310
311 free(natural);
312 free(merged);
313 }
314
315 static void test_invalid_drop_and_staged(void)
316 {
317 gid_t gid = getgid();
318 int rc;
319
320 if (capng_get_caps_process())
321 fail("Failed to initialize libcap-ng state");
322 rc = capng_stage_additional_groups(&gid, 1);
323 if (rc)
324 fail("Failed to stage additional groups");
325 rc = capng_change_id(-1, -1,
326 CAPNG_DROP_SUPP_GRP |
327 CAPNG_APPLY_STAGED_GROUPS);
328 if (rc != -12)
329 fail("Unexpected drop+staged return code");
330 }
331
332 static void test_staged_ignored_without_flag(void)
333 {
334 static const int allowed[] = { -2, -3 };
335 gid_t gid = getgid();
336 int rc;
337
338 if (capng_get_caps_process())
339 fail("Failed to initialize libcap-ng state");
340 rc = capng_stage_additional_groups(&gid, 1);
341 if (rc)
342 fail("Failed to stage additional groups");
343 rc = capng_change_id(-1, -1, CAPNG_NO_FLAG);
344 if (rc && rc_in_list(rc, allowed, sizeof(allowed) / sizeof(int)) == 0)
345 fail("Unexpected return code when staged groups are ignored");
346 rc = capng_change_id(-1, -1, CAPNG_APPLY_STAGED_GROUPS);
347 if (rc != -13)
348 fail("Staged groups were not cleared when ignored");
349 }
350
351 static void test_staged_cleared_after_use(void)
352 {
353 static const int allowed[] = { -2, -3, -14 };
354 gid_t gid = getgid();
355 int rc;
356
357 if (capng_get_caps_process())
358 fail("Failed to initialize libcap-ng state");
359 rc = capng_stage_additional_groups(&gid, 1);
360 if (rc)
361 fail("Failed to stage additional groups");
362 rc = capng_change_id(-1, -1, CAPNG_APPLY_STAGED_GROUPS);
363 if (rc && rc_in_list(rc, allowed, sizeof(allowed) / sizeof(int)) == 0)
364 fail("Unexpected return code when consuming staged groups");
365 rc = capng_change_id(-1, -1, CAPNG_APPLY_STAGED_GROUPS);
366 if (rc != -13)
367 fail("Staged groups were not cleared after use");
368 }
369
370 static void test_apply_bounding_during_change_id(void)
371 {
372 static const int allowed[] = { -2, -3, -8, -9 };
373 unsigned int cap;
374 int rc;
375
376 if (capng_get_caps_process())
377 fail("Failed to initialize libcap-ng state");
378 if (find_drop_test_bounding_cap(&cap))
379 return;
380 if (capng_update(CAPNG_DROP, CAPNG_BOUNDING_SET, cap))
381 fail("Failed to prepare bounding set change");
382
383 rc = capng_change_id(-1, -1, CAPNG_APPLY_BOUNDING);
384 if (rc == 0) {
385 if (read_bounding_cap(cap) != 0)
386 fail("Prepared bounding set was not applied");
387 } else if (rc_in_list(rc, allowed, sizeof(allowed) / sizeof(int)) == 0)
388 fail("Unexpected apply-bounding return code");
389 }
390
391 static void test_apply_bounding_noop_without_state(void)
392 {
393 static const int allowed[] = { -2, -3 };
394 unsigned int cap;
395 int before, rc;
396
397 if (capng_get_caps_process())
398 fail("Failed to initialize libcap-ng state");
399 if (find_drop_test_bounding_cap(&cap))
400 return;
401 before = read_bounding_cap(cap);
402 if (before != 1)
403 fail("Expected selected bounding capability to be present");
404
405 rc = capng_change_id(-1, -1, CAPNG_APPLY_BOUNDING);
406 if (rc == 0) {
407 if (read_bounding_cap(cap) != before)
408 fail("Unprepared bounding set should be a no-op");
409 } else if (rc_in_list(rc, allowed, sizeof(allowed) / sizeof(int)) == 0)
410 fail("Unexpected no-op apply-bounding return code");
411 }
412
413 static void test_invalid_apply_and_clear_bounding(void)
414 {
415 if (capng_get_caps_process())
416 fail("Failed to initialize libcap-ng state");
417 if (capng_change_id(-1, -1,
418 CAPNG_APPLY_BOUNDING | CAPNG_CLEAR_BOUNDING) != -17)
419 fail("Unexpected apply+clear bounding return code");
420 }
421
422 static void test_apply_bounding_preserves_requested_helper_cap(void)
423 {
424 static const int allowed[] = { -2, -3, -8, -9 };
425 unsigned int cap;
426 int rc;
427
428 if (capng_get_caps_process())
429 fail("Failed to initialize libcap-ng state");
430 if (find_drop_test_bounding_cap(&cap))
431 return;
432 if (capng_have_capability(CAPNG_PERMITTED, CAP_SETPCAP) == 0)
433 return;
434 if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED,
435 CAP_SETPCAP))
436 fail("Failed to request CAP_SETPCAP");
437 if (capng_update(CAPNG_DROP, CAPNG_BOUNDING_SET, cap))
438 fail("Failed to prepare bounding set change");
439
440 rc = capng_change_id(-1, -1, CAPNG_APPLY_BOUNDING);
441 if (rc == 0) {
442 if (capng_have_capability(CAPNG_EFFECTIVE, CAP_SETPCAP) == 0)
443 fail("Requested CAP_SETPCAP was removed from effective");
444 if (capng_have_capability(CAPNG_PERMITTED, CAP_SETPCAP) == 0)
445 fail("Requested CAP_SETPCAP was removed from permitted");
446 } else if (rc_in_list(rc, allowed, sizeof(allowed) / sizeof(int)) == 0)
447 fail("Unexpected helper capability cleanup return code");
448 }
449
450 static void test_apply_bounding_preserves_permitted_only_helper_cap(void)
451 {
452 static const int allowed[] = { -2, -3, -8, -9 };
453 unsigned int cap;
454 int rc;
455
456 if (capng_get_caps_process())
457 fail("Failed to initialize libcap-ng state");
458 if (find_drop_test_bounding_cap(&cap))
459 return;
460 if (capng_have_capability(CAPNG_PERMITTED, CAP_SETPCAP) == 0)
461 return;
462 if (capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_SETPCAP))
463 fail("Failed to clear effective CAP_SETPCAP request");
464 if (capng_update(CAPNG_ADD, CAPNG_PERMITTED, CAP_SETPCAP))
465 fail("Failed to request permitted CAP_SETPCAP");
466 if (capng_update(CAPNG_DROP, CAPNG_BOUNDING_SET, cap))
467 fail("Failed to prepare bounding set change");
468
469 rc = capng_change_id(-1, -1, CAPNG_APPLY_BOUNDING);
470 if (rc == 0) {
471 if (capng_have_capability(CAPNG_EFFECTIVE, CAP_SETPCAP))
472 fail("Permitted-only CAP_SETPCAP became effective");
473 if (capng_have_capability(CAPNG_PERMITTED, CAP_SETPCAP) == 0)
474 fail("Permitted-only CAP_SETPCAP was removed");
475 } else if (rc_in_list(rc, allowed, sizeof(allowed) / sizeof(int)) == 0)
476 fail("Unexpected permitted-only helper cleanup return code");
477 }
478
479 static void test_bounding_state_ignored_without_flag(void)
480 {
481 static const int allowed[] = { -2, -3 };
482 unsigned int cap;
483 int rc;
484
485 if (capng_get_caps_process())
486 fail("Failed to initialize libcap-ng state");
487 if (find_drop_test_bounding_cap(&cap))
488 return;
489 if (capng_update(CAPNG_DROP, CAPNG_BOUNDING_SET, cap))
490 fail("Failed to prepare bounding set change");
491
492 rc = capng_change_id(-1, -1, CAPNG_NO_FLAG);
493 if (rc == 0) {
494 if (read_bounding_cap(cap) != 1)
495 fail("Bounding set changed without CAPNG_APPLY_BOUNDING");
496 } else if (rc_in_list(rc, allowed, sizeof(allowed) / sizeof(int)) == 0)
497 fail("Unexpected backward compatibility return code");
498 }
499
500 static void run_test(const char *name, void (*test)(void))
501 {
502 pid_t pid;
503 int status;
504
505 fflush(NULL);
506 pid = fork();
507 if (pid < 0) {
508 perror("fork");
509 abort();
510 }
511 if (pid == 0) {
512 test();
513 _exit(EXIT_SUCCESS);
514 }
515 if (waitpid(pid, &status, 0) < 0) {
516 perror("waitpid");
517 abort();
518 }
519 if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != EXIT_SUCCESS) {
520 printf("Failed %s test\n", name);
521 abort();
522 }
523 }
524
525 int main(void)
526 {
527 puts("Doing capng_change_id additional group tests...");
528 run_test("staged-only additional groups", test_staged_only);
529 run_test("init-only additional groups", test_init_only);
530 run_test("init+staged additional groups", test_init_and_staged);
531 run_test("invalid drop+staged combination",
532 test_invalid_drop_and_staged);
533 run_test("staged groups ignored without flag",
534 test_staged_ignored_without_flag);
535 run_test("staged state cleared after use",
536 test_staged_cleared_after_use);
537 run_test("apply prepared bounding set",
538 test_apply_bounding_during_change_id);
539 run_test("apply bounding is no-op without prepared state",
540 test_apply_bounding_noop_without_state);
541 run_test("invalid apply+clear bounding combination",
542 test_invalid_apply_and_clear_bounding);
543 run_test("helper cleanup keeps requested capability",
544 test_apply_bounding_preserves_requested_helper_cap);
545 run_test("helper cleanup keeps permitted-only capability",
546 test_apply_bounding_preserves_permitted_only_helper_cap);
547 run_test("prepared bounding state ignored without flag",
548 test_bounding_state_ignored_without_flag);
549 return EXIT_SUCCESS;
550 }