]> gitweb.michael.orlitzky.com - apply-default-acl.git/blob - src/libadacl.c
src/libadacl.c: fix a clang-tidy warning by adding a redundant check.
[apply-default-acl.git] / src / libadacl.c
1 /**
2 * @file libadacl.c
3 *
4 * @brief The adacl (apply default acl) shared library.
5 *
6 */
7
8 /* Enables get_current_dir_name() in unistd.h, the O_PATH flag, and
9 * the asprintf() function.
10 */
11 #define _GNU_SOURCE
12
13 #include <dirent.h> /* readdir(), etc. */
14 #include <errno.h> /* EINVAL, ELOOP, ENOTDIR, etc. */
15 #include <fcntl.h> /* openat() */
16 #include <libgen.h> /* basename(), dirname() */
17 #include <stdbool.h> /* the "bool" type */
18 #include <stdio.h> /* perror(), asprintf() */
19 #include <stdlib.h> /* free() */
20 #include <string.h> /* strdup() */
21 #include <sys/stat.h> /* fstat() */
22 #include <sys/xattr.h> /* fgetxattr(), fsetxattr() */
23 #include <unistd.h> /* get_current_dir_name() */
24
25 /* ACLs */
26 #include <acl/libacl.h> /* acl_get_perm, not portable */
27 #include <sys/acl.h> /* all other acl_foo functions */
28
29 /* XATTR_NAME_POSIX_ACL_ACCESS and XATTR_NAME_POSIX_ACL_DEFAULT */
30 #include <linux/xattr.h>
31
32 #include "libadacl.h"
33
34
35 /* Even though most other library functions reliably return -1 for
36 * error, it feels a little wrong to re-use the ACL_ERROR constant.
37 */
38 #define CLOSE_ERROR -1
39 #define OPEN_ERROR -1
40 #define ASPRINTF_ERROR -1
41 #define STAT_ERROR -1
42 #define XATTR_ERROR -1
43
44
45 /* Prototypes */
46 int safe_open_ex(int at_fd, char* pathname, int flags);
47 int safe_open(const char* pathname, int flags);
48 int acl_update_entry(acl_t aclp, acl_entry_t updated_entry);
49 int acl_entry_count(acl_t acl);
50 int acl_is_minimal(acl_t acl);
51 int acl_execute_masked(acl_t acl);
52 int any_can_execute(int fd, const struct stat* sp);
53 int acl_copy_xattr(int src_fd,
54 acl_type_t src_type,
55 int dst_fd,
56 acl_type_t dst_type);
57 int has_default_acl_fd(int fd);
58 int apply_default_acl_fds(int parent_fd, int fd, bool recursive);
59 int apply_default_acl(const char* path, bool recursive);
60
61
62
63 /**
64 * @brief The recursive portion of the @c safe_open function, used to
65 * open a file descriptor in a symlink-safe way when combined with
66 * the @c O_NOFOLLOW flag.
67 *
68 * @param at_fd
69 * A file descriptor relative to which @c pathname will be opened.
70 *
71 * @param pathname
72 * The path to the file/directory/whatever whose descriptor you want.
73 *
74 * @param flags
75 * File status flags to be passed to @c openat.
76 *
77 * @return a file descriptor for @c pathname if everything goes well,
78 * and @c OPEN_ERROR if not.
79 */
80 int safe_open_ex(int at_fd, char* pathname, int flags) {
81 if (pathname == NULL) {
82 errno = EINVAL;
83 perror("safe_open_ex (args)");
84 return OPEN_ERROR;
85 }
86
87 char* firstslash = strchr(pathname, '/');
88 if (firstslash == NULL) {
89 /* No more slashes, this is the base case. */
90 return openat(at_fd, pathname, flags);
91 }
92 if (firstslash[1] == '\0') {
93 /* The first slash is the last character; ensure that we open
94 a directory. */
95 firstslash[0] = '\0';
96 return openat(at_fd, pathname, flags | O_DIRECTORY);
97 }
98
99 /* The first slash exists and isn't the last character in the path,
100 so we can split the path wherever that first slash lies and
101 recurse. */
102 *firstslash = '\0';
103 int fd = openat(at_fd, pathname, flags | O_DIRECTORY | O_PATH);
104 if (fd == OPEN_ERROR) {
105 if (errno != ENOTDIR) {
106 /* Don't output anything if we ignore a symlink */
107 perror("safe_open_ex (safe_open_ex)");
108 }
109 return OPEN_ERROR;
110 }
111
112 /* The +1 is safe because there needs to be at least one character
113 after the first slash (we checked this above). */
114 int result = safe_open_ex(fd, firstslash+1, flags);
115 if (close(fd) == CLOSE_ERROR) {
116 perror("safe_open_ex (close)");
117 return OPEN_ERROR;
118 }
119 return result;
120 }
121
122
123 /**
124 * @brief A version of @c open that is completely symlink-safe when
125 * used with the @c O_NOFOLLOW flag.
126 *
127 * The @c openat function exists to ensure that you can anchor one
128 * path to a particular directory while opening it; however, if you
129 * open "b/c/d" relative to "/a", then even the @c openat function will
130 * still follow symlinks in the "b" component. This can be exploited
131 * by an attacker to make you open the wrong path.
132 *
133 * To avoid that problem, this function uses a recursive
134 * implementation that opens every path from the root, one level at a
135 * time. So "a" is opened relative to "/", and then "b" is opened
136 * relative to "/a", and then "c" is opened relative to "/a/b",
137 * etc. When the @c O_NOFOLLOW flag is used, this approach ensures
138 * that no symlinks in any component are followed.
139 *
140 * @param pathname
141 * The path to the file/directory/whatever whose descriptor you want.
142 *
143 * @param flags
144 * File status flags to be passed to @c openat.
145 *
146 * @return a file descriptor for @c pathname if everything goes well,
147 * and @c OPEN_ERROR if not.
148 */
149 int safe_open(const char* pathname, int flags) {
150 if (pathname == NULL) {
151 errno = EINVAL;
152 perror("safe_open (args)");
153 return OPEN_ERROR;
154 }
155
156 char* abspath = NULL;
157 int asprintf_result = 0;
158 if (strchr(pathname, '/') == pathname) {
159 /* pathname is already absolute; just copy it. */
160 asprintf_result = asprintf(&abspath, "%s", pathname);
161 }
162 else {
163 /* Concatenate the current working directory and pathname into an
164 * absolute path. We use realpath() ONLY on the cwd part, and not
165 * on the pathname part, because realpath() resolves symlinks. And
166 * the whole point of all this crap is to avoid following symlinks
167 * in the pathname.
168 *
169 * Using realpath() on the cwd lets us operate on relative paths
170 * while we're sitting in a directory that happens to have a
171 * symlink in it; for example: cd /var/run && apply-default-acl foo.
172 */
173 char* cwd = get_current_dir_name();
174 if (cwd == NULL) {
175 perror("safe_open (get_current_dir_name)");
176 return OPEN_ERROR;
177 }
178
179 char abs_cwd[PATH_MAX];
180 if (realpath(cwd, abs_cwd) == NULL) {
181 perror("safe_open (realpath)");
182 free(cwd);
183 return OPEN_ERROR;
184 }
185 asprintf_result = asprintf(&abspath, "%s/%s", abs_cwd, pathname);
186 free(cwd);
187 }
188 if (asprintf_result == ASPRINTF_ERROR) {
189 perror("safe_open (asprintf)");
190 return OPEN_ERROR;
191 }
192
193 /* Beyond here, asprintf() worked, and we need to free abspath. */
194 int result = OPEN_ERROR;
195
196 bool abspath_is_root = (strcmp(abspath, "/") == 0);
197 int rootflags = flags | O_DIRECTORY;
198 if (!abspath_is_root) {
199 /* Use O_PATH for some added safety if "/" is not our target */
200 rootflags |= O_PATH;
201 }
202 int rootfd = open("/", rootflags);
203 if (rootfd == OPEN_ERROR) {
204 perror("safe_open (open)");
205 result = OPEN_ERROR;
206 goto cleanup;
207 }
208
209 if (abspath_is_root) {
210 result = rootfd;
211 goto cleanup;
212 }
213
214 result = safe_open_ex(rootfd, abspath+1, flags);
215 if (close(rootfd) == CLOSE_ERROR) {
216 perror("safe_open (close)");
217 result = OPEN_ERROR;
218 goto cleanup;
219 }
220
221 cleanup:
222 free(abspath);
223 return result;
224 }
225
226
227
228
229 /**
230 * @brief Update an entry in an @b minimal ACL.
231 *
232 * @param aclp
233 * A pointer to the acl_t structure whose entry we want to update.
234 *
235 * @param updated_entry
236 * An updated copy of an existing entry in @c aclp.
237 *
238 * @return
239 * - @c ACL_SUCCESS - If we update an existing entry.
240 * - @c ACL_FAILURE - If we don't find an entry to update.
241 * - @c ACL_ERROR - Unexpected library error.
242 */
243 int acl_update_entry(acl_t aclp, acl_entry_t updated_entry) {
244 if (aclp == NULL || updated_entry == NULL) {
245 errno = EINVAL;
246 perror("acl_update_entry (args)");
247 return ACL_ERROR;
248 }
249
250 acl_tag_t updated_tag;
251 if (acl_get_tag_type(updated_entry, &updated_tag) == ACL_ERROR) {
252 perror("acl_update_entry (acl_get_tag_type)");
253 return ACL_ERROR;
254 }
255
256 acl_permset_t updated_permset;
257 if (acl_get_permset(updated_entry, &updated_permset) == ACL_ERROR) {
258 perror("acl_update_entry (acl_get_permset)");
259 return ACL_ERROR;
260 }
261
262 /* This can allocate memory, so from here on out we have to jump to
263 the "cleanup" label to exit. */
264 void* updated_qualifier = acl_get_qualifier(updated_entry);
265 if (updated_qualifier == NULL &&
266 (updated_tag == ACL_USER || updated_tag == ACL_GROUP)) {
267 /* acl_get_qualifier() can return NULL, but it shouldn't for
268 ACL_USER or ACL_GROUP entries. */
269 perror("acl_update_entry (acl_get_qualifier)");
270 return ACL_ERROR;
271 }
272
273 /* Our return value. Default to failure, and change to success if we
274 actually update something. */
275 int result = ACL_FAILURE;
276
277 acl_entry_t existing_entry;
278 /* Loop through the given ACL looking for matching entries. */
279 int get_entry_result = acl_get_entry(aclp, ACL_FIRST_ENTRY, &existing_entry);
280
281 while (get_entry_result == ACL_SUCCESS) {
282 acl_tag_t existing_tag = ACL_UNDEFINED_TAG;
283
284 if (acl_get_tag_type(existing_entry, &existing_tag) == ACL_ERROR) {
285 perror("set_acl_tag_permset (acl_get_tag_type)");
286 result = ACL_ERROR;
287 goto cleanup;
288 }
289
290 if (existing_tag == updated_tag) {
291 /* Our tag types match, but if we have a named user or group
292 entry, then we need to check that the user/group (that is,
293 the qualifier) matches too. */
294 bool qualifiers_match = false;
295
296 /* There are three ways the qualifiers can match... */
297 void* existing_qualifier = acl_get_qualifier(existing_entry);
298 if (existing_qualifier == NULL) {
299 if (existing_tag == ACL_USER || existing_tag == ACL_GROUP) {
300 perror("acl_update_entry (acl_get_qualifier)");
301 result = ACL_ERROR;
302 goto cleanup;
303 }
304 else {
305 /* First, we could be dealing with an entry that isn't a
306 named user or group, in which case they "match
307 vacuously." */
308 qualifiers_match = true;
309 }
310 }
311
312 /* Second, they could have matching UIDs. We don't really need to
313 check both tags here, since we know that they're equal. However,
314 clang-tidy can't figure that out, and the redundant equality
315 check prevents it from complaining about a potential null pointer
316 dereference. */
317 if (updated_tag == ACL_USER && existing_tag == ACL_USER) {
318 qualifiers_match = ( *((uid_t*)existing_qualifier)
319 ==
320 *((uid_t*)updated_qualifier) );
321 }
322
323 /* Third, they could have matching GIDs. See above for why
324 we check the redundant condition existing_tag == ACL_GROUP. */
325 if (updated_tag == ACL_GROUP && existing_tag == ACL_GROUP) {
326 qualifiers_match = ( *((gid_t*)existing_qualifier)
327 ==
328 *((gid_t*)updated_qualifier) );
329 }
330
331 /* Be sure to free this inside the loop, where memory is allocated. */
332 acl_free(existing_qualifier);
333
334 if (qualifiers_match) {
335 /* If we update something, we're done and return ACL_SUCCESS */
336 if (acl_set_permset(existing_entry, updated_permset) == ACL_ERROR) {
337 perror("acl_update_entry (acl_set_permset)");
338 result = ACL_ERROR;
339 goto cleanup;
340 }
341
342 result = ACL_SUCCESS;
343 goto cleanup;
344 }
345 }
346
347 get_entry_result = acl_get_entry(aclp, ACL_NEXT_ENTRY, &existing_entry);
348 }
349
350 /* This catches both the initial acl_get_entry and the ones at the
351 end of the loop. */
352 if (get_entry_result == ACL_ERROR) {
353 perror("acl_update_entry (acl_get_entry)");
354 result = ACL_ERROR;
355 }
356
357 cleanup:
358 acl_free(updated_qualifier);
359 return result;
360 }
361
362
363
364 /**
365 * @brief Determine the number of entries in the given ACL.
366 *
367 * @param acl
368 * The ACL to inspect.
369 *
370 * @return Either the non-negative number of entries in @c acl, or
371 * @c ACL_ERROR on error.
372 */
373 int acl_entry_count(acl_t acl) {
374
375 acl_entry_t entry;
376 int entry_count = 0;
377 int result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
378
379 while (result == ACL_SUCCESS) {
380 entry_count++;
381 result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
382 }
383
384 if (result == ACL_ERROR) {
385 perror("acl_entry_count (acl_get_entry)");
386 return ACL_ERROR;
387 }
388
389 return entry_count;
390 }
391
392
393
394 /**
395 * @brief Determine whether or not the given ACL is minimal.
396 *
397 * An ACL is minimal if it has fewer than four entries.
398 *
399 * @param acl
400 * The ACL whose minimality is in question.
401 *
402 * @return
403 * - @c ACL_SUCCESS - @c acl is minimal
404 * - @c ACL_FAILURE - @c acl is not minimal
405 * - @c ACL_ERROR - Unexpected library error
406 */
407 int acl_is_minimal(acl_t acl) {
408 if (acl == NULL) {
409 errno = EINVAL;
410 perror("acl_is_minimal (args)");
411 return ACL_ERROR;
412 }
413
414 int ec = acl_entry_count(acl);
415
416 if (ec == ACL_ERROR) {
417 perror("acl_is_minimal (acl_entry_count)");
418 return ACL_ERROR;
419 }
420
421 if (ec < 4) {
422 return ACL_SUCCESS;
423 }
424 else {
425 return ACL_FAILURE;
426 }
427 }
428
429
430
431 /**
432 * @brief Determine whether the given ACL's mask denies execute.
433 *
434 * @param acl
435 * The ACL whose mask we want to check.
436 *
437 * @return
438 * - @c ACL_SUCCESS - The @c acl has a mask which denies execute.
439 * - @c ACL_FAILURE - The @c acl has a mask which does not deny execute.
440 * - @c ACL_ERROR - Unexpected library error.
441 */
442 int acl_execute_masked(acl_t acl) {
443 if (acl == NULL) {
444 errno = EINVAL;
445 perror("acl_execute_masked (args)");
446 return ACL_ERROR;
447 }
448
449 acl_entry_t entry;
450 int ge_result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
451
452 while (ge_result == ACL_SUCCESS) {
453 acl_tag_t tag = ACL_UNDEFINED_TAG;
454
455 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
456 perror("acl_execute_masked (acl_get_tag_type)");
457 return ACL_ERROR;
458 }
459
460 if (tag == ACL_MASK) {
461 /* This is the mask entry, get its permissions, and see if
462 execute is specified. */
463 acl_permset_t permset;
464
465 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
466 perror("acl_execute_masked (acl_get_permset)");
467 return ACL_ERROR;
468 }
469
470 int gp_result = acl_get_perm(permset, ACL_EXECUTE);
471 if (gp_result == ACL_ERROR) {
472 perror("acl_execute_masked (acl_get_perm)");
473 return ACL_ERROR;
474 }
475
476 if (gp_result == ACL_FAILURE) {
477 /* No execute bit set in the mask; execute not allowed. */
478 return ACL_SUCCESS;
479 }
480 }
481
482 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
483 }
484
485 return ACL_FAILURE;
486 }
487
488
489
490 /**
491 * @brief Determine whether @c fd is executable by anyone.
492 *
493 *
494 * This is used as part of the heuristic to determine whether or not
495 * we should mask the execute bit when inheriting an ACL. If @c fd
496 * describes a file, we check the @a effective permissions, contrary
497 * to what setfacl does.
498 *
499 * @param fd
500 * The file descriptor to check.
501 *
502 * @param sp
503 * A pointer to a stat structure for @c fd.
504 *
505 * @return
506 * - @c ACL_SUCCESS - Someone has effective execute permissions on @c fd.
507 * - @c ACL_FAILURE - Nobody can execute @c fd.
508 * - @c ACL_ERROR - Unexpected library error.
509 */
510 int any_can_execute(int fd, const struct stat* sp) {
511 if (sp == NULL) {
512 errno = EINVAL;
513 perror("any_can_execute (args)");
514 return ACL_ERROR;
515 }
516
517 acl_t acl = acl_get_fd(fd);
518
519 if (acl == (acl_t)NULL) {
520 perror("any_can_execute (acl_get_fd)");
521 return ACL_ERROR;
522 }
523
524 /* Our return value. */
525 int result = ACL_FAILURE;
526
527 if (acl_is_minimal(acl)) {
528 if (sp->st_mode & (S_IXUSR | S_IXOTH | S_IXGRP)) {
529 result = ACL_SUCCESS;
530 goto cleanup;
531 }
532 else {
533 result = ACL_FAILURE;
534 goto cleanup;
535 }
536 }
537
538 acl_entry_t entry;
539 int ge_result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
540
541 while (ge_result == ACL_SUCCESS) {
542 /* The first thing we do is check to see if this is a mask
543 entry. If it is, we skip it entirely. */
544 acl_tag_t tag = ACL_UNDEFINED_TAG;
545
546 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
547 perror("any_can_execute_or (acl_get_tag_type)");
548 result = ACL_ERROR;
549 goto cleanup;
550 }
551
552 if (tag == ACL_MASK) {
553 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
554 continue;
555 }
556
557 /* Ok, so it's not a mask entry. Check the execute perms. */
558 acl_permset_t permset;
559
560 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
561 perror("any_can_execute_or (acl_get_permset)");
562 result = ACL_ERROR;
563 goto cleanup;
564 }
565
566 int gp_result = acl_get_perm(permset, ACL_EXECUTE);
567 if (gp_result == ACL_ERROR) {
568 perror("any_can_execute (acl_get_perm)");
569 result = ACL_ERROR;
570 goto cleanup;
571 }
572
573 if (gp_result == ACL_SUCCESS) {
574 /* Only return ACL_SUCCESS if this execute bit is not masked. */
575 if (acl_execute_masked(acl) != ACL_SUCCESS) {
576 result = ACL_SUCCESS;
577 goto cleanup;
578 }
579 }
580
581 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
582 }
583
584 if (ge_result == ACL_ERROR) {
585 perror("any_can_execute (acl_get_entry)");
586 result = ACL_ERROR;
587 goto cleanup;
588 }
589
590 cleanup:
591 acl_free(acl);
592 return result;
593 }
594
595
596
597 /**
598 * @brief Copy ACLs between file descriptors as xattrs, verbatim.
599 *
600 * There is a small deficiency in libacl, namely that there is no way
601 * to get or set default ACLs through file descriptors. The @c
602 * acl_get_file and @c acl_set_file functions can do it, but they use
603 * paths, and are vulnerable to symlink attacks.
604 *
605 * Fortunately, when inheriting an ACL, we don't really need to look
606 * at what it contains. That means that we can copy the on-disk xattrs
607 * from the source directory to the destination file/directory without
608 * passing through libacl, and this can be done with file descriptors
609 * through @c fgetxattr and @c fsetxattr. That's what this function
610 * does.
611 *
612 * @param src_fd
613 * The file descriptor from which the ACL will be copied.
614 *
615 * @param src_type
616 * The type of ACL (either @c ACL_TYPE_ACCESS or @c ACL_TYPE_DEFAULT)
617 * to copy from @c src_fd.
618 *
619 * @param dst_fd
620 * The file descriptor whose ACL will be overwritten with the one
621 * from @c src_fd.
622 *
623 * @param dst_type
624 * The type of ACL (either @c ACL_TYPE_ACCESS or @c ACL_TYPE_DEFAULT)
625 * to replace on @c dst_fd.
626 *
627 * @return
628 * - @c ACL_SUCCESS - The ACL was copied successfully.
629 * - @c ACL_FAILURE - There was no ACL on @c src_fd.
630 * - @c ACL_ERROR - Unexpected library error.
631 */
632 int acl_copy_xattr(int src_fd,
633 acl_type_t src_type,
634 int dst_fd,
635 acl_type_t dst_type) {
636
637 const char* src_name;
638 if (src_type == ACL_TYPE_ACCESS) {
639 src_name = XATTR_NAME_POSIX_ACL_ACCESS;
640 }
641 else if (src_type == ACL_TYPE_DEFAULT) {
642 src_name = XATTR_NAME_POSIX_ACL_DEFAULT;
643 }
644 else {
645 errno = EINVAL;
646 perror("acl_copy_xattr (src type)");
647 return ACL_ERROR;
648 }
649
650 const char* dst_name;
651 if (dst_type == ACL_TYPE_ACCESS) {
652 dst_name = XATTR_NAME_POSIX_ACL_ACCESS;
653 }
654 else if (dst_type == ACL_TYPE_DEFAULT) {
655 dst_name = XATTR_NAME_POSIX_ACL_DEFAULT;
656 }
657 else {
658 errno = EINVAL;
659 perror("acl_copy_xattr (dst type)");
660 return ACL_ERROR;
661 }
662
663 ssize_t src_size_guess = fgetxattr(src_fd, src_name, NULL, 0);
664 if (src_size_guess == XATTR_ERROR) {
665 if (errno == ENODATA) {
666 /* A missing ACL isn't really an error. ENOATTR and ENODATA are
667 synonyms, but using ENODATA here lets us avoid another
668 "include" directive. */
669 return ACL_FAILURE;
670 }
671 perror("acl_copy_xattr (fgetxattr size guess)");
672 return ACL_ERROR;
673 }
674 char* src_acl_p = alloca(src_size_guess);
675 /* The actual size may be smaller than our guess? I don't know. The
676 return value from fgetxattr() will either be nonnegative, or
677 XATTR_ERROR (which we've already ruled out), so it's safe to cast
678 it to an unsigned size_t here to avoid a compiler warning. */
679 ssize_t src_size = fgetxattr(src_fd,
680 src_name,
681 src_acl_p,
682 (size_t)src_size_guess);
683 if (src_size == XATTR_ERROR) {
684 if (errno == ENODATA) {
685 /* A missing ACL isn't an error. */
686 return ACL_FAILURE;
687 }
688 perror("acl_copy_xattr (fgetxattr)");
689 return ACL_ERROR;
690 }
691
692 /* See above: src_size must be nonnegative at this point,so we cast
693 it to size_t to avoid a compiler warning. */
694 if (fsetxattr(dst_fd,
695 dst_name,
696 src_acl_p,
697 (size_t)src_size,
698 0)
699 == XATTR_ERROR) {
700 perror("acl_copy_xattr (fsetxattr)");
701 return ACL_ERROR;
702 }
703
704 return ACL_SUCCESS;
705 }
706
707
708 /**
709 * @brief Determine if a file descriptor has a default ACL.
710 *
711 * @param fd
712 * The file descriptor whose default ACL is in question.
713 *
714 * @return
715 * - @c ACL_SUCCESS - If @c fd has a default ACL.
716 * - @c ACL_FAILURE - If @c fd does not have a default ACL.
717 * - @c ACL_ERROR - Unexpected library error.
718 */
719 int has_default_acl_fd(int fd) {
720 if (fgetxattr(fd, XATTR_NAME_POSIX_ACL_DEFAULT, NULL, 0) == XATTR_ERROR) {
721 if (errno == ENODATA) {
722 return ACL_FAILURE;
723 }
724 perror("has_default_acl_fd (fgetxattr)");
725 return ACL_ERROR;
726 }
727
728 return ACL_SUCCESS;
729 }
730
731
732
733 /**
734 * @brief The recursive portion of @c apply_default_acl.
735 *
736 * The @c apply_default_acl function takes a path, but then opens file
737 * descriptors for the path and its parent. Afterwards, everything is
738 * done using file descriptors, including the recursive application on
739 * the path's children. This function encapsulates the portion of @c
740 * apply_default_acl that uses only file descriptors; for the
741 * recursion, this function ultimately calls itself.
742 *
743 * This overwrites any existing ACLs on @c fd and, if @c recursive is
744 * @c true, its children. When @c recursive is @c true, the "worst"
745 * result encountered is returned as the overall result.
746 *
747 * @param parent_fd
748 * A file descriptor for the parent directory of @c fd.
749 *
750 * @param fd
751 * The file descriptor that should inherit its parent's default ACL.
752 *
753 * @param recursive
754 * Should we recurse into subdirectories?
755 *
756 * @return
757 * - @c ACL_SUCCESS - The parent default ACLs were inherited successfully.
758 * - @c ACL_FAILURE - If symlinks or hard links are encountered.
759 * - @c ACL_ERROR - Unexpected library error.
760 */
761 int apply_default_acl_fds(int parent_fd, int fd, bool recursive) {
762 int result = ACL_SUCCESS;
763
764 /* The new ACL for this path */
765 acl_t new_acl = (acl_t)NULL;
766
767 /* A copy of new_acl, to be made before we begin mangling new_acl in
768 order to mask the execute bit. */
769 acl_t new_acl_unmasked = (acl_t)NULL;
770
771 /* Refuse to operate on hard links, which can be abused by an
772 * attacker to trick us into changing the ACL on a file we didn't
773 * intend to; namely the "target" of the hard link. There is TOCTOU
774 * race condition here, but the window is as small as possible
775 * between when we open the file descriptor (look above) and when we
776 * fstat it.
777 */
778 struct stat s;
779 if (fstat(fd, &s) == STAT_ERROR) {
780 perror("apply_default_acl_fds (fstat)");
781 /* We can't recurse without the stat struct for fd */
782 goto cleanup;
783 }
784
785
786 /* Check to make sure the parent descriptor actually has a default
787 ACL. If it doesn't, then we can "succeed" immediately, saving a
788 little work, particularly in any_can_execute(). Note that we
789 can't skip the fstat() above, because we need it in case we
790 recurse. */
791 if (has_default_acl_fd(parent_fd) == ACL_FAILURE) {
792 result = ACL_SUCCESS;
793 /* Just because this target can't inherit anything doesn't mean
794 that one of it's children can't. For example, if there's a
795 default on "c" in "a/b/c/d", then we don't want to skip all
796 children of "a"! */
797 goto recurse;
798 }
799
800
801 if (!S_ISDIR(s.st_mode)) {
802 /* If it's not a directory, make sure it's a regular,
803 non-hard-linked file. */
804 if (!S_ISREG(s.st_mode) || s.st_nlink != 1) {
805 result = ACL_FAILURE;
806 goto cleanup; /* It's not a directory, so we can skip the recursion. */
807 }
808 }
809
810
811 /* Next We try to guess whether or not to strip the execute bits.
812 * This behavior is modeled after the capital 'X' perms of setfacl.
813 */
814 int ace_result = any_can_execute(fd, &s);
815
816 if (ace_result == ACL_ERROR) {
817 perror("apply_default_acl_fds (any_can_execute)");
818 result = ACL_ERROR;
819 goto cleanup;
820 }
821
822 /* Never mask the execute bit on directories. */
823 bool allow_exec = (bool)ace_result || S_ISDIR(s.st_mode);
824
825
826 /* If it's a directory, inherit the parent's default. */
827 if (S_ISDIR(s.st_mode)) {
828 if (acl_copy_xattr(parent_fd,
829 ACL_TYPE_DEFAULT,
830 fd,
831 ACL_TYPE_DEFAULT) == ACL_ERROR) {
832 perror("apply_default_acl_fds (acl_copy_xattr default)");
833 result = ACL_ERROR;
834 goto cleanup;
835 }
836 }
837
838 /* If it's anything, _apply_ the parent's default. */
839 if (acl_copy_xattr(parent_fd,
840 ACL_TYPE_DEFAULT,
841 fd,
842 ACL_TYPE_ACCESS) == ACL_ERROR) {
843 perror("apply_default_acl_fds (acl_copy_xattr access)");
844 result = ACL_ERROR;
845 goto cleanup;
846 }
847
848 /* There's a good reason why we saved the ACL above, even though
849 * we're about to read it back into memory and mess with it on the
850 * next line. The acl_copy_xattr() function is already a hack to let
851 * us copy default ACLs without resorting to path names; we simply
852 * have no way to read the parent's default ACL into memory using
853 * parent_fd. We can, however, copy the parent's ACL to a file (with
854 * acl_copy_xattr), and then read the ACL from a file using
855 * "fd". It's quite the circus, but it works and should be safe from
856 * sym/hardlink attacks.
857 */
858
859 /* Now we potentially need to mask the execute permissions in the
860 ACL on fd; or maybe not. */
861 if (allow_exec) {
862 /* Skip the mask code for this target, but don't skip its children! */
863 goto recurse;
864 }
865
866 /* OK, we need to mask some execute permissions. First obtain the
867 current ACL... */
868 new_acl = acl_get_fd(fd);
869 if (new_acl == (acl_t)NULL) {
870 perror("apply_default_acl_fds (acl_get_fd)");
871 result = ACL_ERROR;
872 goto cleanup;
873 }
874
875 /* ...and now make a copy of it, because otherwise when we loop
876 below, some shit gets stuck (modifying the structure while
877 looping over it no worky). */
878 new_acl_unmasked = acl_dup(new_acl);
879 if (new_acl_unmasked == (acl_t)NULL) {
880 perror("apply_default_acl_fds (acl_dup)");
881 result = ACL_ERROR;
882 goto cleanup;
883 }
884
885 acl_entry_t entry;
886 int ge_result = acl_get_entry(new_acl_unmasked, ACL_FIRST_ENTRY, &entry);
887
888 while (ge_result == ACL_SUCCESS) {
889 acl_tag_t tag = ACL_UNDEFINED_TAG;
890
891 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
892 perror("apply_default_acl_fds (acl_get_tag_type)");
893 result = ACL_ERROR;
894 goto cleanup;
895 }
896
897
898 /* We've got an entry/tag from the default ACL. Get its permset. */
899 acl_permset_t permset;
900 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
901 perror("apply_default_acl_fds (acl_get_permset)");
902 result = ACL_ERROR;
903 goto cleanup;
904 }
905
906 /* To mimic what the kernel does, I think we could drop
907 ACL_GROUP_OBJ from the list below? */
908 if (tag == ACL_MASK ||
909 tag == ACL_USER_OBJ ||
910 tag == ACL_GROUP_OBJ ||
911 tag == ACL_OTHER) {
912
913 /* The mask doesn't affect acl_user_obj, acl_group_obj (in
914 minimal ACLs) or acl_other entries, so if execute should be
915 masked, we have to do it manually. */
916 if (acl_delete_perm(permset, ACL_EXECUTE) == ACL_ERROR) {
917 perror("apply_default_acl_fds (acl_delete_perm)");
918 result = ACL_ERROR;
919 goto cleanup;
920 }
921
922 if (acl_set_permset(entry, permset) == ACL_ERROR) {
923 perror("apply_default_acl_fds (acl_set_permset)");
924 result = ACL_ERROR;
925 goto cleanup;
926 }
927 }
928
929 if (acl_update_entry(new_acl, entry) == ACL_ERROR) {
930 perror("apply_default_acl_fds (acl_update_entry)");
931 result = ACL_ERROR;
932 goto cleanup;
933 }
934
935 ge_result = acl_get_entry(new_acl_unmasked, ACL_NEXT_ENTRY, &entry);
936 }
937
938 /* Catches the first acl_get_entry as well as the ones at the end of
939 the loop. */
940 if (ge_result == ACL_ERROR) {
941 perror("apply_default_acl_fds (acl_get_entry)");
942 result = ACL_ERROR;
943 goto cleanup;
944 }
945
946 if (acl_set_fd(fd, new_acl) == ACL_ERROR) {
947 perror("apply_default_acl_fds (acl_set_fd)");
948 result = ACL_ERROR;
949 goto cleanup;
950 }
951
952 recurse:
953 if (recursive && S_ISDIR(s.st_mode)) {
954 /* Recurse into subdirectories. Don't call closedir() on d! It
955 closes the open file descriptor as well, and subsequent calls
956 to close() then throw errors. */
957 DIR* d = fdopendir(fd);
958 if (d == NULL) {
959 perror("apply_default_acl_fds (fdopendir)");
960 result = ACL_ERROR;
961 goto cleanup;
962 }
963
964 struct dirent* de;
965 int new_fd = 0;
966 while ((de = readdir(d)) != NULL) {
967 if (de->d_type != DT_DIR && de->d_type != DT_REG) {
968 /* Hit a symlink or whatever. */
969 result = ACL_FAILURE;
970 continue;
971 }
972 if (strcmp(de->d_name, ".") == 0) { continue; }
973 if (strcmp(de->d_name, "..") == 0) { continue; }
974
975 /* Be careful not to "return" out of this loop and leave the
976 new_fd open! */
977 new_fd = openat(fd, de->d_name, O_NOFOLLOW);
978 if (new_fd == OPEN_ERROR) {
979 if (errno == ELOOP || errno == ENOTDIR) {
980 /* We hit a symlink, either in the last path component (ELOOP)
981 or higher up (ENOTDIR). */
982 if (result == ACL_SUCCESS) {
983 /* Don't overwrite an error result with success/failure. */
984 result = ACL_FAILURE;
985 }
986 continue;
987 }
988 else {
989 perror("apply_default_acl_fds (openat)");
990 result = ACL_ERROR;
991 continue;
992 }
993 }
994 switch (apply_default_acl_fds(fd, new_fd, recursive)) {
995 /* Don't overwrite an error result with success/failure. */
996 case ACL_FAILURE:
997 if (result == ACL_SUCCESS) {
998 result = ACL_FAILURE;
999 }
1000 break;
1001 case ACL_ERROR:
1002 result = ACL_ERROR;
1003 default:
1004 if (close(new_fd) == CLOSE_ERROR) {
1005 perror("apply_default_acl_fds (close)");
1006 result = ACL_ERROR;
1007 }
1008 }
1009 }
1010 }
1011
1012 cleanup:
1013 acl_free(new_acl);
1014 acl_free(new_acl_unmasked);
1015 return result;
1016 }
1017
1018
1019 /**
1020 * @brief Apply parent default ACL to a path and optionally its children.
1021 *
1022 * This overwrites any existing ACLs on the target, and, if @c
1023 * recursive is @c true, its children. When @c recursive is @c true,
1024 * the "worst" result encountered is returned as the overall result.
1025 *
1026 * @param path
1027 * The path whose ACL we would like to reset to its default.
1028 *
1029 * @param recursive
1030 * Should we recurse into subdirectories?
1031 *
1032 * @return
1033 * - @c ACL_SUCCESS - The parent default ACLs were inherited successfully.
1034 * - @c ACL_FAILURE - If symlinks or hard links are encountered.
1035 * - @c ACL_ERROR - Unexpected library error.
1036 */
1037 int apply_default_acl(const char* path, bool recursive) {
1038
1039 if (path == NULL) {
1040 errno = EINVAL;
1041 perror("apply_default_acl (args)");
1042 return ACL_ERROR;
1043 }
1044
1045 /* Define these next three variables here because we may have to
1046 * jump to the cleanup routine which expects them to exist.
1047 */
1048
1049 /* Our return value. */
1050 int result = ACL_SUCCESS;
1051
1052 /* The file descriptor corresponding to "path" */
1053 int fd = 0;
1054
1055 /* The file descriptor for the directory containing "path" */
1056 int parent_fd = 0;
1057
1058 /* dirname() and basename() mangle their arguments, so we need
1059 to make copies of "path" before using them. */
1060 char* dirname_path_copy = NULL;
1061 char* basename_path_copy = NULL;
1062
1063 /* Get the parent directory of "path" with dirname(), which happens
1064 * to murder its argument and necessitates a path_copy. */
1065 dirname_path_copy = strdup(path);
1066 if (dirname_path_copy == NULL) {
1067 perror("apply_default_acl (strdup)");
1068 return ACL_ERROR;
1069 }
1070 char* parent = dirname(dirname_path_copy);
1071
1072 basename_path_copy = strdup(path);
1073 if (basename_path_copy == NULL) {
1074 perror("apply_default_acl (strdup)");
1075 result = ACL_ERROR;
1076 goto cleanup;
1077 }
1078 char* child = basename(basename_path_copy);
1079
1080 /* Just kidding, if the path is "." or "..", then dirname will do
1081 * the wrong thing and give us "." as its parent, too. So, we handle
1082 * those as special cases. We use "child" instead of "path" here to
1083 * catch things like "./" and "../"
1084 */
1085 bool path_is_dots = strcmp(child, ".") == 0 || strcmp(child, "..") == 0;
1086 char dots_parent[6] = "../";
1087 if (path_is_dots) {
1088 /* We know that "child" contains no more than two characters here, and
1089 using strncat to enforce that belief keeps clang-tidy happy. */
1090 parent = strncat(dots_parent, child, 2);
1091 }
1092
1093 parent_fd = safe_open(parent, O_DIRECTORY | O_NOFOLLOW);
1094
1095 if (parent_fd == OPEN_ERROR) {
1096 if (errno == ELOOP || errno == ENOTDIR) {
1097 /* We hit a symlink, either in the last path component (ELOOP)
1098 or higher up (ENOTDIR). */
1099 result = ACL_FAILURE;
1100 goto cleanup;
1101 }
1102 else {
1103 perror("apply_default_acl (open parent fd)");
1104 result = ACL_ERROR;
1105 goto cleanup;
1106 }
1107 }
1108
1109 /* We already obtained the parent fd safely, so if we use the
1110 * basename of path here instead of the full thing, then we can get
1111 * away with using openat() and spare ourselves the slowness of
1112 * another safe_open().
1113 *
1114 * Note that if the basename is "." or "..", then we don't want to
1115 * open it relative to the parent_fd, so we need another special
1116 * case for those paths here.
1117 */
1118 if (path_is_dots) {
1119 fd = open(child, O_NOFOLLOW);
1120 }
1121 else {
1122 fd = openat(parent_fd, child, O_NOFOLLOW);
1123 }
1124 if (fd == OPEN_ERROR) {
1125 if (errno == ELOOP || errno == ENOTDIR) {
1126 /* We hit a symlink, either in the last path component (ELOOP)
1127 or higher up (ENOTDIR). */
1128 result = ACL_FAILURE;
1129 goto cleanup;
1130 }
1131 else {
1132 perror("apply_default_acl (open fd)");
1133 result = ACL_ERROR;
1134 goto cleanup;
1135 }
1136 }
1137
1138 result = apply_default_acl_fds(parent_fd, fd, recursive);
1139
1140 cleanup:
1141 free(dirname_path_copy);
1142 free(basename_path_copy);
1143
1144 if (parent_fd > 0 && close(parent_fd) == CLOSE_ERROR) {
1145 perror("apply_default_acl (close parent_fd)");
1146 result = ACL_ERROR;
1147 }
1148 if (fd > 0 && close(fd) == CLOSE_ERROR) {
1149 perror("apply_default_acl (close fd)");
1150 result = ACL_ERROR;
1151 }
1152 return result;
1153 }