Debian Codegraph @

t_acl.c

raw symbols SCIP
1 /*
2 * Unit test for lib/acl.c.
3 *
4 * Validates the fd- and at-based POSIX ACL get/set/delete in lib/acl.c against
5 * the system libacl, used here as an oracle: we set an ACL via one and read it
6 * back via the other (both directions), round-trip through lib/acl.c, and check
7 * the default-ACL and delete paths. We deliberately do NOT try to reproduce
8 * libacl's symlink-following races -- we only compare functional behaviour.
9 *
10 * Not linked into rsync itself. Exits 0 if all checks pass, 1 on any failure,
11 * 77 to skip (built without SUPPORT_ACL_FD, no libacl, or a scratch filesystem
12 * without ACL support).
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18
19 #include "rsync.h"
20 #include "lib/acl.h"
21
22 #include <stdio.h>
23
24 #ifndef SUPPORT_ACL_FD
25
26 int main(int argc, char *argv[])
27 {
28 (void)argc;
29 (void)argv;
30 fprintf(stderr, "t_acl: built without SUPPORT_ACL_FD -- skipping\n");
31 return 77;
32 }
33
34 #else
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/acl.h>
44 #ifdef HAVE_ACL_LIBACL_H
45 #include <acl/libacl.h> /* acl_get_perm() */
46 #endif
47
48 #define MAX_ENT 16
49
50 static int errs = 0;
51 static const char *scratch;
52
53 static void ok(int cond, const char *label)
54 {
55 if (cond)
56 fprintf(stderr, "OK %s\n", label);
57 else {
58 fprintf(stderr, "FAIL %s\n", label);
59 errs++;
60 }
61 }
62
63 static void dump_entries(const char *pfx, const rsync_acl_ent *e, int n)
64 {
65 int i;
66 for (i = 0; i < n; i++)
67 fprintf(stderr, " %s tag=0x%02x perm=%o id=%u\n",
68 pfx, e[i].tag, e[i].perm,
69 e[i].id == RACL_UNDEFINED_ID ? (unsigned)-1 : e[i].id);
70 }
71
72 static int ent_cmp(const void *a, const void *b)
73 {
74 const rsync_acl_ent *x = a, *y = b;
75 if (x->tag != y->tag)
76 return x->tag < y->tag ? -1 : 1;
77 if (x->id != y->id)
78 return x->id < y->id ? -1 : 1;
79 return 0;
80 }
81
82 static void canon(rsync_acl_ent *e, int n)
83 {
84 if (n > 1)
85 qsort(e, n, sizeof e[0], ent_cmp);
86 }
87
88 static int entries_equal(rsync_acl_ent *a, int na, rsync_acl_ent *b, int nb)
89 {
90 int i;
91 canon(a, na);
92 canon(b, nb);
93 if (na != nb)
94 return 0;
95 for (i = 0; i < na; i++) {
96 if (a[i].tag != b[i].tag || a[i].perm != b[i].perm)
97 return 0;
98 if ((a[i].tag == RACL_USER || a[i].tag == RACL_GROUP)
99 && a[i].id != b[i].id)
100 return 0;
101 }
102 return 1;
103 }
104
105 /* === libacl oracle helpers === */
106
107 static uint16_t racl_from_tag(acl_tag_t tag)
108 {
109 switch (tag) {
110 case ACL_USER_OBJ: return RACL_USER_OBJ;
111 case ACL_USER: return RACL_USER;
112 case ACL_GROUP_OBJ: return RACL_GROUP_OBJ;
113 case ACL_GROUP: return RACL_GROUP;
114 case ACL_MASK: return RACL_MASK;
115 case ACL_OTHER: return RACL_OTHER;
116 }
117 return 0;
118 }
119
120 static acl_tag_t tag_from_racl(uint16_t tag)
121 {
122 switch (tag) {
123 case RACL_USER_OBJ: return ACL_USER_OBJ;
124 case RACL_USER: return ACL_USER;
125 case RACL_GROUP_OBJ: return ACL_GROUP_OBJ;
126 case RACL_GROUP: return ACL_GROUP;
127 case RACL_MASK: return ACL_MASK;
128 case RACL_OTHER: return ACL_OTHER;
129 }
130 return 0;
131 }
132
133 /* Convert a libacl acl_t to our neutral entry array. Returns count or -1. */
134 static int libacl_to_entries(acl_t acl, rsync_acl_ent *out, int max)
135 {
136 acl_entry_t e;
137 int n = 0, r;
138
139 for (r = acl_get_entry(acl, ACL_FIRST_ENTRY, &e); r == 1;
140 r = acl_get_entry(acl, ACL_NEXT_ENTRY, &e)) {
141 acl_tag_t tag;
142 acl_permset_t ps;
143 uint16_t perm = 0;
144
145 if (n >= max)
146 return -1;
147 if (acl_get_tag_type(e, &tag) != 0 || acl_get_permset(e, &ps) != 0)
148 return -1;
149 if (acl_get_perm(ps, ACL_READ) > 0)
150 perm |= 4;
151 if (acl_get_perm(ps, ACL_WRITE) > 0)
152 perm |= 2;
153 if (acl_get_perm(ps, ACL_EXECUTE) > 0)
154 perm |= 1;
155 out[n].tag = racl_from_tag(tag);
156 out[n].perm = perm;
157 out[n].id = RACL_UNDEFINED_ID;
158 if (tag == ACL_USER || tag == ACL_GROUP) {
159 void *q = acl_get_qualifier(e);
160 if (q) {
161 out[n].id = *(id_t *)q;
162 acl_free(q);
163 }
164 }
165 n++;
166 }
167 if (r < 0)
168 return -1;
169 canon(out, n);
170 return n;
171 }
172
173 /* Build a libacl acl_t from our neutral entries. */
174 static acl_t entries_to_libacl(const rsync_acl_ent *ents, int n)
175 {
176 acl_t acl = acl_init(n);
177 int i;
178
179 if (!acl)
180 return NULL;
181 for (i = 0; i < n; i++) {
182 acl_entry_t e;
183 acl_permset_t ps;
184 if (acl_create_entry(&acl, &e) != 0)
185 goto fail;
186 if (acl_set_tag_type(e, tag_from_racl(ents[i].tag)) != 0)
187 goto fail;
188 if (acl_get_permset(e, &ps) != 0 || acl_clear_perms(ps) != 0)
189 goto fail;
190 if ((ents[i].perm & 4) && acl_add_perm(ps, ACL_READ) != 0)
191 goto fail;
192 if ((ents[i].perm & 2) && acl_add_perm(ps, ACL_WRITE) != 0)
193 goto fail;
194 if ((ents[i].perm & 1) && acl_add_perm(ps, ACL_EXECUTE) != 0)
195 goto fail;
196 if (acl_set_permset(e, ps) != 0)
197 goto fail;
198 if (ents[i].tag == RACL_USER || ents[i].tag == RACL_GROUP) {
199 id_t id = ents[i].id;
200 if (acl_set_qualifier(e, &id) != 0)
201 goto fail;
202 }
203 }
204 return acl;
205 fail:
206 acl_free(acl);
207 return NULL;
208 }
209
210 static int libacl_get(const char *path, acl_type_t type, rsync_acl_ent *out, int max)
211 {
212 acl_t acl = acl_get_file(path, type);
213 int n;
214 if (!acl)
215 return -1;
216 n = libacl_to_entries(acl, out, max);
217 acl_free(acl);
218 return n;
219 }
220
221 static int libacl_set(const char *path, acl_type_t type, const rsync_acl_ent *ents, int n)
222 {
223 acl_t acl = entries_to_libacl(ents, n);
224 int rc;
225 if (!acl)
226 return -1;
227 rc = acl_set_file(path, type, acl);
228 acl_free(acl);
229 return rc;
230 }
231
232 /* === scratch helpers === */
233
234 static char *acl_path(const char *name)
235 {
236 static char buf[4096];
237 snprintf(buf, sizeof buf, "%s/%s", scratch, name);
238 return buf;
239 }
240
241 static int acl_mkfile(const char *name)
242 {
243 char *p = acl_path(name);
244 int fd = open(p, O_WRONLY | O_CREAT | O_TRUNC, 0644);
245 if (fd >= 0)
246 close(fd);
247 return fd < 0 ? -1 : 0;
248 }
249
250 static int acl_mkdir(const char *name)
251 {
252 return mkdir(acl_path(name), 0755);
253 }
254
255 /* Open a held fd the way set_file_attrs does (REG/DIR, NOFOLLOW). */
256 static int open_held(const char *name)
257 {
258 return open(acl_path(name), O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC);
259 }
260
261 /* === comparison tests for one ACL shape === */
262
263 static void cmp_via_fd(const char *name, int want_default,
264 const rsync_acl_ent *ents, int n, const char *tag)
265 {
266 char label[256];
267 char *path = acl_path(name);
268 acl_type_t type = want_default ? ACL_TYPE_DEFAULT : ACL_TYPE_ACCESS;
269 rsync_acl_ent got[MAX_ENT], oracle[MAX_ENT];
270 rsync_acl_ent *lib_ents = NULL;
271 int gc = 0, oc, fd;
272
273 /* 1. set via lib (fd) -> read via libacl */
274 fd = open_held(name);
275 snprintf(label, sizeof label, "%s: open held fd", tag);
276 ok(fd >= 0, label);
277 if (fd < 0)
278 return;
279 snprintf(label, sizeof label, "%s: xacl_set_fd", tag);
280 ok(xacl_set_fd(fd, want_default, ents, n) == 0, label);
281 oc = libacl_get(path, type, oracle, MAX_ENT);
282 memcpy(got, ents, n * sizeof ents[0]);
283 snprintf(label, sizeof label, "%s: libacl reads back what xacl_set_fd wrote", tag);
284 if (!entries_equal(got, n, oracle, oc)) {
285 dump_entries("set ", got, n);
286 dump_entries("got ", oracle, oc < 0 ? 0 : oc);
287 }
288 ok(oc == n && entries_equal(got, n, oracle, oc), label);
289
290 /* 2. set via libacl -> read via lib (fd) */
291 snprintf(label, sizeof label, "%s: libacl_set", tag);
292 ok(libacl_set(path, type, ents, n) == 0, label);
293 snprintf(label, sizeof label, "%s: xacl_get_fd reads back what libacl wrote", tag);
294 if (xacl_get_fd(fd, want_default, &lib_ents, &gc) == 0) {
295 memcpy(got, ents, n * sizeof ents[0]);
296 if (!entries_equal(got, n, lib_ents, gc)) {
297 dump_entries("set ", got, n);
298 dump_entries("got ", lib_ents, gc);
299 }
300 ok(gc == n && entries_equal(got, n, lib_ents, gc), label);
301 } else
302 ok(0, label);
303 if (lib_ents)
304 free(lib_ents);
305 lib_ents = NULL;
306
307 /* 3. round-trip lib set -> lib get */
308 snprintf(label, sizeof label, "%s: xacl_set_fd/xacl_get_fd round-trip", tag);
309 if (xacl_set_fd(fd, want_default, ents, n) == 0
310 && xacl_get_fd(fd, want_default, &lib_ents, &gc) == 0) {
311 memcpy(got, ents, n * sizeof ents[0]);
312 ok(gc == n && entries_equal(got, n, lib_ents, gc), label);
313 } else
314 ok(0, label);
315 if (lib_ents)
316 free(lib_ents);
317
318 close(fd);
319 }
320
321 static void cmp_via_at(const char *name, int want_default,
322 const rsync_acl_ent *ents, int n, const char *tag)
323 {
324 char label[256];
325 char *path = acl_path(name);
326 acl_type_t type = want_default ? ACL_TYPE_DEFAULT : ACL_TYPE_ACCESS;
327 rsync_acl_ent got[MAX_ENT], oracle[MAX_ENT];
328 rsync_acl_ent *lib_ents = NULL;
329 int gc = 0, oc, dirfd;
330
331 dirfd = open(scratch, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
332 snprintf(label, sizeof label, "%s: open scratch dirfd", tag);
333 ok(dirfd >= 0, label);
334 if (dirfd < 0)
335 return;
336
337 snprintf(label, sizeof label, "%s: xacl_set_at", tag);
338 ok(xacl_set_at(dirfd, name, want_default, ents, n) == 0, label);
339 oc = libacl_get(path, type, oracle, MAX_ENT);
340 memcpy(got, ents, n * sizeof ents[0]);
341 snprintf(label, sizeof label, "%s: libacl reads back what xacl_set_at wrote", tag);
342 ok(oc == n && entries_equal(got, n, oracle, oc), label);
343
344 snprintf(label, sizeof label, "%s: libacl_set", tag);
345 ok(libacl_set(path, type, ents, n) == 0, label);
346 snprintf(label, sizeof label, "%s: xacl_get_at reads back what libacl wrote", tag);
347 if (xacl_get_at(dirfd, name, want_default, &lib_ents, &gc) == 0) {
348 memcpy(got, ents, n * sizeof ents[0]);
349 ok(gc == n && entries_equal(got, n, lib_ents, gc), label);
350 } else
351 ok(0, label);
352 if (lib_ents)
353 free(lib_ents);
354
355 close(dirfd);
356 }
357
358 /* === scenarios === */
359
360 #define U(p) { RACL_USER_OBJ, p, RACL_UNDEFINED_ID }
361 #define G(p) { RACL_GROUP_OBJ, p, RACL_UNDEFINED_ID }
362 #define M(p) { RACL_MASK, p, RACL_UNDEFINED_ID }
363 #define O(p) { RACL_OTHER, p, RACL_UNDEFINED_ID }
364 #define NU(i,p) { RACL_USER, p, i }
365 #define NG(i,p) { RACL_GROUP, p, i }
366
367 static const rsync_acl_ent A1[] = { U(6), NU(12345,4), G(4), M(6), O(4) };
368 static const rsync_acl_ent A2[] = { U(7), NU(1000,6), NU(2000,5), G(5), NG(100,4), NG(200,1), M(7), O(0) };
369 static const rsync_acl_ent A3[] = { U(7), NU(4000000000U,5), G(5), M(7), O(4) };
370 static const rsync_acl_ent A4[] = { U(7), NU(0,0), G(0), M(4), O(0) };
371
372 static const rsync_acl_ent D1[] = { U(7), G(5), O(5) };
373 static const rsync_acl_ent D2[] = { U(7), NU(1000,6), G(5), NG(100,4), M(6), O(0) };
374
375 #define NELEM(a) ((int)(sizeof (a) / sizeof (a)[0]))
376
377 static void run_access(const char *base, const rsync_acl_ent *e, int n, const char *tag)
378 {
379 char fdname[128], atname[128];
380 char fdtag[160], attag[160];
381
382 snprintf(fdname, sizeof fdname, "%s_fd", base);
383 snprintf(atname, sizeof atname, "%s_at", base);
384 snprintf(fdtag, sizeof fdtag, "access %s [fd]", tag);
385 snprintf(attag, sizeof attag, "access %s [at]", tag);
386
387 if (acl_mkfile(fdname) == 0)
388 cmp_via_fd(fdname, 0, e, n, fdtag);
389 if (xacl_at_available()) {
390 if (acl_mkfile(atname) == 0)
391 cmp_via_at(atname, 0, e, n, attag);
392 }
393 }
394
395 static void run_default(const char *base, const rsync_acl_ent *e, int n, const char *tag)
396 {
397 char fdname[128], atname[128];
398 char fdtag[160], attag[160];
399
400 snprintf(fdname, sizeof fdname, "%s_fd", base);
401 snprintf(atname, sizeof atname, "%s_at", base);
402 snprintf(fdtag, sizeof fdtag, "default %s [fd]", tag);
403 snprintf(attag, sizeof attag, "default %s [at]", tag);
404
405 if (acl_mkdir(fdname) == 0)
406 cmp_via_fd(fdname, 1, e, n, fdtag);
407 if (xacl_at_available()) {
408 if (acl_mkdir(atname) == 0)
409 cmp_via_at(atname, 1, e, n, attag);
410 }
411 }
412
413 /* delete of a default ACL + the "no explicit ACL" / errno behaviour */
414 static void run_misc(void)
415 {
416 rsync_acl_ent *ents = NULL;
417 int n = 0, fd;
418 char *p;
419
420 /* default-ACL delete */
421 if (acl_mkdir("deldir") == 0) {
422 fd = open_held("deldir");
423 ok(fd >= 0, "misc: open deldir fd");
424 if (fd >= 0) {
425 ok(xacl_set_fd(fd, 1, D2, NELEM(D2)) == 0, "misc: set default to delete");
426 ok(xacl_del_default_fd(fd) == 0, "misc: xacl_del_default_fd");
427 /* libacl returns an empty (0-entry) default ACL after delete */
428 {
429 acl_t a = acl_get_file(acl_path("deldir"), ACL_TYPE_DEFAULT);
430 int cnt = -1;
431 if (a) {
432 acl_entry_t e;
433 cnt = acl_get_entry(a, ACL_FIRST_ENTRY, &e);
434 acl_free(a);
435 }
436 ok(cnt == 0, "misc: default ACL is empty after delete");
437 }
438 /* deleting again is still success */
439 ok(xacl_del_default_fd(fd) == 0, "misc: xacl_del_default_fd idempotent");
440 close(fd);
441 }
442 }
443
444 /* a freshly-created file has no explicit access ACL xattr -> count 0 */
445 if (acl_mkfile("plainfile") == 0) {
446 fd = open_held("plainfile");
447 ok(fd >= 0, "misc: open plainfile fd");
448 if (fd >= 0) {
449 int rc = xacl_get_fd(fd, 0, &ents, &n);
450 ok(rc == 0 && n == 0, "misc: xacl_get_fd on mode-only file -> no entries");
451 if (ents)
452 free(ents);
453 ents = NULL;
454 /* no default ACL on a regular file -> empty */
455 rc = xacl_get_fd(fd, 1, &ents, &n);
456 ok(rc == 0 && n == 0, "misc: xacl_get_fd default on regular file -> no entries");
457 if (ents)
458 free(ents);
459 close(fd);
460 }
461 }
462
463 /* at-variant: leaf NOFOLLOW must not touch a symlink target */
464 if (xacl_at_available()) {
465 int dirfd;
466 acl_mkfile("nofollow_target");
467 p = acl_path("nofollow_link");
468 unlink(p);
469 if (symlink("nofollow_target", p) == 0
470 && (dirfd = open(scratch, O_RDONLY | O_DIRECTORY | O_CLOEXEC)) >= 0) {
471 rsync_acl_ent before[MAX_ENT], after[MAX_ENT];
472 int bc, ac;
473 /* give the target a distinctive ACL via libacl */
474 libacl_set(acl_path("nofollow_target"), ACL_TYPE_ACCESS, A1, NELEM(A1));
475 bc = libacl_get(acl_path("nofollow_target"), ACL_TYPE_ACCESS, before, MAX_ENT);
476 /* attempt to set through the symlink leaf with NOFOLLOW: must fail */
477 errno = 0;
478 ok(xacl_set_at(dirfd, "nofollow_link", 0, A2, NELEM(A2)) != 0,
479 "misc: xacl_set_at on symlink leaf is refused");
480 ac = libacl_get(acl_path("nofollow_target"), ACL_TYPE_ACCESS, after, MAX_ENT);
481 ok(bc == ac && entries_equal(before, bc, after, ac),
482 "misc: symlink target ACL unchanged by NOFOLLOW set");
483 close(dirfd);
484 }
485 }
486 }
487
488 int main(int argc, char *argv[])
489 {
490 char *p;
491
492 if (argc > 1)
493 scratch = argv[1];
494 else
495 scratch = "/tmp";
496
497 /* Probe filesystem ACL support: set a trivial ACL via libacl. */
498 if (acl_mkfile(".acl_probe") != 0) {
499 fprintf(stderr, "t_acl: cannot create scratch file in %s\n", scratch);
500 return 77;
501 }
502 p = acl_path(".acl_probe");
503 if (libacl_set(p, ACL_TYPE_ACCESS, A1, NELEM(A1)) != 0) {
504 if (errno == EOPNOTSUPP || errno == ENOTSUP || errno == ENOSYS) {
505 fprintf(stderr, "t_acl: %s has no ACL support -- skipping\n", scratch);
506 unlink(p);
507 return 77;
508 }
509 fprintf(stderr, "t_acl: probe acl_set_file failed: %s\n", strerror(errno));
510 unlink(p);
511 return 77;
512 }
513 unlink(p);
514
515 fprintf(stderr, "t_acl: scratch=%s, setxattrat=%s\n",
516 scratch, xacl_at_available() ? "yes" : "no");
517
518 run_access("a1", A1, NELEM(A1), "named-user+mask");
519 run_access("a2", A2, NELEM(A2), "multi-named+mask");
520 run_access("a3", A3, NELEM(A3), "large-uid");
521 run_access("a4", A4, NELEM(A4), "zero-perms");
522
523 run_default("d1", D1, NELEM(D1), "minimal");
524 run_default("d2", D2, NELEM(D2), "named+mask");
525
526 run_misc();
527
528 fprintf(stderr, "t_acl: %d failure(s)\n", errs);
529 return errs ? 1 : 0;
530 }
531
532 #endif /* SUPPORT_ACL_FD */