4 * @brief The adacl (apply default acl) shared library.
8 /* Enables get_current_dir_name() in unistd.h, the O_PATH flag, and
9 * the asprintf() function.
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 <limits.h> /* PATH_MAX */
18 #include <stdbool.h> /* the "bool" type */
19 #include <stdio.h> /* perror(), asprintf() */
20 #include <stdlib.h> /* free() */
21 #include <string.h> /* strdup() */
22 #include <sys/stat.h> /* fstat() */
23 #include <sys/xattr.h> /* fgetxattr(), fsetxattr() */
24 #include <unistd.h> /* get_current_dir_name() */
27 #include <acl/libacl.h> /* acl_get_perm, not portable */
28 #include <sys/acl.h> /* all other acl_foo functions */
30 /* XATTR_NAME_POSIX_ACL_ACCESS and XATTR_NAME_POSIX_ACL_DEFAULT */
31 #include <linux/xattr.h>
36 /* Even though most other library functions reliably return -1 for
37 * error, it feels a little wrong to re-use the ACL_ERROR constant.
39 #define CLOSE_ERROR -1
41 #define ASPRINTF_ERROR -1
43 #define XATTR_ERROR -1
47 int safe_open_ex(int at_fd
, char* pathname
, int flags
);
48 int safe_open(const char* pathname
, int flags
);
49 int acl_update_entry(acl_t aclp
, acl_entry_t updated_entry
);
50 int acl_entry_count(acl_t acl
);
51 int acl_is_minimal(acl_t acl
);
52 int acl_execute_masked(acl_t acl
);
53 int any_can_execute(int fd
, const struct stat
* sp
);
54 int acl_copy_xattr(int src_fd
,
58 int has_default_acl_fd(int fd
);
59 int apply_default_acl_fds(int parent_fd
, int fd
, bool recursive
);
60 int apply_default_acl(const char* path
, bool recursive
);
65 * @brief The recursive portion of the @c safe_open function, used to
66 * open a file descriptor in a symlink-safe way when combined with
67 * the @c O_NOFOLLOW flag.
70 * A file descriptor relative to which @c pathname will be opened.
73 * The path to the file/directory/whatever whose descriptor you want.
76 * File status flags to be passed to @c openat.
78 * @return a file descriptor for @c pathname if everything goes well,
79 * and @c OPEN_ERROR if not.
81 int safe_open_ex(int at_fd
, char* pathname
, int flags
) {
82 if (pathname
== NULL
) {
84 perror("safe_open_ex (args)");
88 char* firstslash
= strchr(pathname
, '/');
89 if (firstslash
== NULL
) {
90 /* No more slashes, this is the base case. */
91 return openat(at_fd
, pathname
, flags
);
93 if (firstslash
[1] == '\0') {
94 /* The first slash is the last character; ensure that we open
97 return openat(at_fd
, pathname
, flags
| O_DIRECTORY
);
100 /* The first slash exists and isn't the last character in the path,
101 so we can split the path wherever that first slash lies and
104 int fd
= openat(at_fd
, pathname
, flags
| O_DIRECTORY
| O_PATH
);
105 if (fd
== OPEN_ERROR
) {
106 if (errno
!= ENOTDIR
) {
107 /* Don't output anything if we ignore a symlink */
108 perror("safe_open_ex (safe_open_ex)");
113 /* The +1 is safe because there needs to be at least one character
114 after the first slash (we checked this above). */
115 int result
= safe_open_ex(fd
, firstslash
+1, flags
);
116 if (close(fd
) == CLOSE_ERROR
) {
117 perror("safe_open_ex (close)");
125 * @brief A version of @c open that is completely symlink-safe when
126 * used with the @c O_NOFOLLOW flag.
128 * The @c openat function exists to ensure that you can anchor one
129 * path to a particular directory while opening it; however, if you
130 * open "b/c/d" relative to "/a", then even the @c openat function will
131 * still follow symlinks in the "b" component. This can be exploited
132 * by an attacker to make you open the wrong path.
134 * To avoid that problem, this function uses a recursive
135 * implementation that opens every path from the root, one level at a
136 * time. So "a" is opened relative to "/", and then "b" is opened
137 * relative to "/a", and then "c" is opened relative to "/a/b",
138 * etc. When the @c O_NOFOLLOW flag is used, this approach ensures
139 * that no symlinks in any component are followed.
142 * The path to the file/directory/whatever whose descriptor you want.
145 * File status flags to be passed to @c openat.
147 * @return a file descriptor for @c pathname if everything goes well,
148 * and @c OPEN_ERROR if not.
150 int safe_open(const char* pathname
, int flags
) {
151 if (pathname
== NULL
) {
153 perror("safe_open (args)");
157 char* abspath
= NULL
;
158 int asprintf_result
= 0;
159 if (strchr(pathname
, '/') == pathname
) {
160 /* pathname is already absolute; just copy it. */
161 asprintf_result
= asprintf(&abspath
, "%s", pathname
);
164 /* Concatenate the current working directory and pathname into an
165 * absolute path. We use realpath() ONLY on the cwd part, and not
166 * on the pathname part, because realpath() resolves symlinks. And
167 * the whole point of all this crap is to avoid following symlinks
170 * Using realpath() on the cwd lets us operate on relative paths
171 * while we're sitting in a directory that happens to have a
172 * symlink in it; for example: cd /var/run && apply-default-acl foo.
174 char* cwd
= get_current_dir_name();
176 perror("safe_open (get_current_dir_name)");
180 char abs_cwd
[PATH_MAX
];
181 if (realpath(cwd
, abs_cwd
) == NULL
) {
182 perror("safe_open (realpath)");
186 asprintf_result
= asprintf(&abspath
, "%s/%s", abs_cwd
, pathname
);
189 if (asprintf_result
== ASPRINTF_ERROR
) {
190 perror("safe_open (asprintf)");
194 /* Beyond here, asprintf() worked, and we need to free abspath. */
195 int result
= OPEN_ERROR
;
197 bool abspath_is_root
= (strcmp(abspath
, "/") == 0);
198 int rootflags
= flags
| O_DIRECTORY
;
199 if (!abspath_is_root
) {
200 /* Use O_PATH for some added safety if "/" is not our target */
203 int rootfd
= open("/", rootflags
);
204 if (rootfd
== OPEN_ERROR
) {
205 perror("safe_open (open)");
210 if (abspath_is_root
) {
215 result
= safe_open_ex(rootfd
, abspath
+1, flags
);
216 if (close(rootfd
) == CLOSE_ERROR
) {
217 perror("safe_open (close)");
231 * @brief Update an entry in an @b minimal ACL.
234 * A pointer to the acl_t structure whose entry we want to update.
236 * @param updated_entry
237 * An updated copy of an existing entry in @c aclp.
240 * - @c ACL_SUCCESS - If we update an existing entry.
241 * - @c ACL_FAILURE - If we don't find an entry to update.
242 * - @c ACL_ERROR - Unexpected library error.
244 int acl_update_entry(acl_t aclp
, acl_entry_t updated_entry
) {
245 if (aclp
== NULL
|| updated_entry
== NULL
) {
247 perror("acl_update_entry (args)");
251 acl_tag_t updated_tag
;
252 if (acl_get_tag_type(updated_entry
, &updated_tag
) == ACL_ERROR
) {
253 perror("acl_update_entry (acl_get_tag_type)");
257 acl_permset_t updated_permset
;
258 if (acl_get_permset(updated_entry
, &updated_permset
) == ACL_ERROR
) {
259 perror("acl_update_entry (acl_get_permset)");
263 /* This can allocate memory, so from here on out we have to jump to
264 the "cleanup" label to exit. */
265 void* updated_qualifier
= acl_get_qualifier(updated_entry
);
266 if (updated_qualifier
== NULL
&&
267 (updated_tag
== ACL_USER
|| updated_tag
== ACL_GROUP
)) {
268 /* acl_get_qualifier() can return NULL, but it shouldn't for
269 ACL_USER or ACL_GROUP entries. */
270 perror("acl_update_entry (acl_get_qualifier)");
274 /* Our return value. Default to failure, and change to success if we
275 actually update something. */
276 int result
= ACL_FAILURE
;
278 acl_entry_t existing_entry
;
279 /* Loop through the given ACL looking for matching entries. */
280 int get_entry_result
= acl_get_entry(aclp
, ACL_FIRST_ENTRY
, &existing_entry
);
282 while (get_entry_result
== ACL_SUCCESS
) {
283 acl_tag_t existing_tag
= ACL_UNDEFINED_TAG
;
285 if (acl_get_tag_type(existing_entry
, &existing_tag
) == ACL_ERROR
) {
286 perror("set_acl_tag_permset (acl_get_tag_type)");
291 if (existing_tag
== updated_tag
) {
292 /* Our tag types match, but if we have a named user or group
293 entry, then we need to check that the user/group (that is,
294 the qualifier) matches too. */
295 bool qualifiers_match
= false;
297 /* There are three ways the qualifiers can match... */
298 void* existing_qualifier
= acl_get_qualifier(existing_entry
);
299 if (existing_qualifier
== NULL
) {
300 if (existing_tag
== ACL_USER
|| existing_tag
== ACL_GROUP
) {
301 perror("acl_update_entry (acl_get_qualifier)");
306 /* First, we could be dealing with an entry that isn't a
307 named user or group, in which case they "match
309 qualifiers_match
= true;
313 /* Second, they could have matching UIDs. We don't really need to
314 check both tags here, since we know that they're equal. However,
315 clang-tidy can't figure that out, and the redundant equality
316 check prevents it from complaining about a potential null pointer
318 if (updated_tag
== ACL_USER
&& existing_tag
== ACL_USER
) {
319 qualifiers_match
= ( *((uid_t
*)existing_qualifier
)
321 *((uid_t
*)updated_qualifier
) );
324 /* Third, they could have matching GIDs. See above for why
325 we check the redundant condition existing_tag == ACL_GROUP. */
326 if (updated_tag
== ACL_GROUP
&& existing_tag
== ACL_GROUP
) {
327 qualifiers_match
= ( *((gid_t
*)existing_qualifier
)
329 *((gid_t
*)updated_qualifier
) );
332 /* Be sure to free this inside the loop, where memory is allocated. */
333 acl_free(existing_qualifier
);
335 if (qualifiers_match
) {
336 /* If we update something, we're done and return ACL_SUCCESS */
337 if (acl_set_permset(existing_entry
, updated_permset
) == ACL_ERROR
) {
338 perror("acl_update_entry (acl_set_permset)");
343 result
= ACL_SUCCESS
;
348 get_entry_result
= acl_get_entry(aclp
, ACL_NEXT_ENTRY
, &existing_entry
);
351 /* This catches both the initial acl_get_entry and the ones at the
353 if (get_entry_result
== ACL_ERROR
) {
354 perror("acl_update_entry (acl_get_entry)");
359 acl_free(updated_qualifier
);
366 * @brief Determine the number of entries in the given ACL.
369 * The ACL to inspect.
371 * @return Either the non-negative number of entries in @c acl, or
372 * @c ACL_ERROR on error.
374 int acl_entry_count(acl_t acl
) {
378 int result
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &entry
);
380 while (result
== ACL_SUCCESS
) {
382 result
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
385 if (result
== ACL_ERROR
) {
386 perror("acl_entry_count (acl_get_entry)");
396 * @brief Determine whether or not the given ACL is minimal.
398 * An ACL is minimal if it has fewer than four entries.
401 * The ACL whose minimality is in question.
404 * - @c ACL_SUCCESS - @c acl is minimal
405 * - @c ACL_FAILURE - @c acl is not minimal
406 * - @c ACL_ERROR - Unexpected library error
408 int acl_is_minimal(acl_t acl
) {
411 perror("acl_is_minimal (args)");
415 int ec
= acl_entry_count(acl
);
417 if (ec
== ACL_ERROR
) {
418 perror("acl_is_minimal (acl_entry_count)");
433 * @brief Determine whether the given ACL's mask denies execute.
436 * The ACL whose mask we want to check.
439 * - @c ACL_SUCCESS - The @c acl has a mask which denies execute.
440 * - @c ACL_FAILURE - The @c acl has a mask which does not deny execute.
441 * - @c ACL_ERROR - Unexpected library error.
443 int acl_execute_masked(acl_t acl
) {
446 perror("acl_execute_masked (args)");
451 int ge_result
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &entry
);
453 while (ge_result
== ACL_SUCCESS
) {
454 acl_tag_t tag
= ACL_UNDEFINED_TAG
;
456 if (acl_get_tag_type(entry
, &tag
) == ACL_ERROR
) {
457 perror("acl_execute_masked (acl_get_tag_type)");
461 if (tag
== ACL_MASK
) {
462 /* This is the mask entry, get its permissions, and see if
463 execute is specified. */
464 acl_permset_t permset
;
466 if (acl_get_permset(entry
, &permset
) == ACL_ERROR
) {
467 perror("acl_execute_masked (acl_get_permset)");
471 int gp_result
= acl_get_perm(permset
, ACL_EXECUTE
);
472 if (gp_result
== ACL_ERROR
) {
473 perror("acl_execute_masked (acl_get_perm)");
477 if (gp_result
== ACL_FAILURE
) {
478 /* No execute bit set in the mask; execute not allowed. */
483 ge_result
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
492 * @brief Determine whether @c fd is executable by anyone.
495 * This is used as part of the heuristic to determine whether or not
496 * we should mask the execute bit when inheriting an ACL. If @c fd
497 * describes a file, we check the @a effective permissions, contrary
498 * to what setfacl does.
501 * The file descriptor to check.
504 * A pointer to a stat structure for @c fd.
507 * - @c ACL_SUCCESS - Someone has effective execute permissions on @c fd.
508 * - @c ACL_FAILURE - Nobody can execute @c fd.
509 * - @c ACL_ERROR - Unexpected library error.
511 int any_can_execute(int fd
, const struct stat
* sp
) {
514 perror("any_can_execute (args)");
518 acl_t acl
= acl_get_fd(fd
);
520 if (acl
== (acl_t
)NULL
) {
521 perror("any_can_execute (acl_get_fd)");
525 /* Our return value. */
526 int result
= ACL_FAILURE
;
528 if (acl_is_minimal(acl
)) {
529 if (sp
->st_mode
& (S_IXUSR
| S_IXOTH
| S_IXGRP
)) {
530 result
= ACL_SUCCESS
;
534 result
= ACL_FAILURE
;
540 int ge_result
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &entry
);
542 while (ge_result
== ACL_SUCCESS
) {
543 /* The first thing we do is check to see if this is a mask
544 entry. If it is, we skip it entirely. */
545 acl_tag_t tag
= ACL_UNDEFINED_TAG
;
547 if (acl_get_tag_type(entry
, &tag
) == ACL_ERROR
) {
548 perror("any_can_execute_or (acl_get_tag_type)");
553 if (tag
== ACL_MASK
) {
554 ge_result
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
558 /* Ok, so it's not a mask entry. Check the execute perms. */
559 acl_permset_t permset
;
561 if (acl_get_permset(entry
, &permset
) == ACL_ERROR
) {
562 perror("any_can_execute_or (acl_get_permset)");
567 int gp_result
= acl_get_perm(permset
, ACL_EXECUTE
);
568 if (gp_result
== ACL_ERROR
) {
569 perror("any_can_execute (acl_get_perm)");
574 if (gp_result
== ACL_SUCCESS
) {
575 /* Only return ACL_SUCCESS if this execute bit is not masked. */
576 if (acl_execute_masked(acl
) != ACL_SUCCESS
) {
577 result
= ACL_SUCCESS
;
582 ge_result
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
585 if (ge_result
== ACL_ERROR
) {
586 perror("any_can_execute (acl_get_entry)");
599 * @brief Copy ACLs between file descriptors as xattrs, verbatim.
601 * There is a small deficiency in libacl, namely that there is no way
602 * to get or set default ACLs through file descriptors. The @c
603 * acl_get_file and @c acl_set_file functions can do it, but they use
604 * paths, and are vulnerable to symlink attacks.
606 * Fortunately, when inheriting an ACL, we don't really need to look
607 * at what it contains. That means that we can copy the on-disk xattrs
608 * from the source directory to the destination file/directory without
609 * passing through libacl, and this can be done with file descriptors
610 * through @c fgetxattr and @c fsetxattr. That's what this function
614 * The file descriptor from which the ACL will be copied.
617 * The type of ACL (either @c ACL_TYPE_ACCESS or @c ACL_TYPE_DEFAULT)
618 * to copy from @c src_fd.
621 * The file descriptor whose ACL will be overwritten with the one
625 * The type of ACL (either @c ACL_TYPE_ACCESS or @c ACL_TYPE_DEFAULT)
626 * to replace on @c dst_fd.
629 * - @c ACL_SUCCESS - The ACL was copied successfully.
630 * - @c ACL_FAILURE - There was no ACL on @c src_fd.
631 * - @c ACL_ERROR - Unexpected library error.
633 int acl_copy_xattr(int src_fd
,
636 acl_type_t dst_type
) {
638 const char* src_name
;
639 if (src_type
== ACL_TYPE_ACCESS
) {
640 src_name
= XATTR_NAME_POSIX_ACL_ACCESS
;
642 else if (src_type
== ACL_TYPE_DEFAULT
) {
643 src_name
= XATTR_NAME_POSIX_ACL_DEFAULT
;
647 perror("acl_copy_xattr (src type)");
651 const char* dst_name
;
652 if (dst_type
== ACL_TYPE_ACCESS
) {
653 dst_name
= XATTR_NAME_POSIX_ACL_ACCESS
;
655 else if (dst_type
== ACL_TYPE_DEFAULT
) {
656 dst_name
= XATTR_NAME_POSIX_ACL_DEFAULT
;
660 perror("acl_copy_xattr (dst type)");
664 ssize_t src_size_guess
= fgetxattr(src_fd
, src_name
, NULL
, 0);
665 if (src_size_guess
== XATTR_ERROR
) {
666 if (errno
== ENODATA
) {
667 /* A missing ACL isn't really an error. ENOATTR and ENODATA are
668 synonyms, but using ENODATA here lets us avoid another
669 "include" directive. */
672 perror("acl_copy_xattr (fgetxattr size guess)");
675 char* src_acl_p
= alloca(src_size_guess
);
676 /* The actual size may be smaller than our guess? I don't know. The
677 return value from fgetxattr() will either be nonnegative, or
678 XATTR_ERROR (which we've already ruled out), so it's safe to cast
679 it to an unsigned size_t here to avoid a compiler warning. */
680 ssize_t src_size
= fgetxattr(src_fd
,
683 (size_t)src_size_guess
);
684 if (src_size
== XATTR_ERROR
) {
685 if (errno
== ENODATA
) {
686 /* A missing ACL isn't an error. */
689 perror("acl_copy_xattr (fgetxattr)");
693 /* See above: src_size must be nonnegative at this point,so we cast
694 it to size_t to avoid a compiler warning. */
695 if (fsetxattr(dst_fd
,
701 perror("acl_copy_xattr (fsetxattr)");
710 * @brief Determine if a file descriptor has a default ACL.
713 * The file descriptor whose default ACL is in question.
716 * - @c ACL_SUCCESS - If @c fd has a default ACL.
717 * - @c ACL_FAILURE - If @c fd does not have a default ACL.
718 * - @c ACL_ERROR - Unexpected library error.
720 int has_default_acl_fd(int fd
) {
721 if (fgetxattr(fd
, XATTR_NAME_POSIX_ACL_DEFAULT
, NULL
, 0) == XATTR_ERROR
) {
722 if (errno
== ENODATA
) {
725 perror("has_default_acl_fd (fgetxattr)");
735 * @brief The recursive portion of @c apply_default_acl.
737 * The @c apply_default_acl function takes a path, but then opens file
738 * descriptors for the path and its parent. Afterwards, everything is
739 * done using file descriptors, including the recursive application on
740 * the path's children. This function encapsulates the portion of @c
741 * apply_default_acl that uses only file descriptors; for the
742 * recursion, this function ultimately calls itself.
744 * This overwrites any existing ACLs on @c fd and, if @c recursive is
745 * @c true, its children. When @c recursive is @c true, the "worst"
746 * result encountered is returned as the overall result.
749 * A file descriptor for the parent directory of @c fd.
752 * The file descriptor that should inherit its parent's default ACL.
755 * Should we recurse into subdirectories?
758 * - @c ACL_SUCCESS - The parent default ACLs were inherited successfully.
759 * - @c ACL_FAILURE - If symlinks or hard links are encountered.
760 * - @c ACL_ERROR - Unexpected library error.
762 int apply_default_acl_fds(int parent_fd
, int fd
, bool recursive
) {
763 int result
= ACL_SUCCESS
;
765 /* The new ACL for this path */
766 acl_t new_acl
= (acl_t
)NULL
;
768 /* A copy of new_acl, to be made before we begin mangling new_acl in
769 order to mask the execute bit. */
770 acl_t new_acl_unmasked
= (acl_t
)NULL
;
772 /* Refuse to operate on hard links, which can be abused by an
773 * attacker to trick us into changing the ACL on a file we didn't
774 * intend to; namely the "target" of the hard link. There is TOCTOU
775 * race condition here, but the window is as small as possible
776 * between when we open the file descriptor (look above) and when we
780 if (fstat(fd
, &s
) == STAT_ERROR
) {
781 perror("apply_default_acl_fds (fstat)");
782 /* We can't recurse without the stat struct for fd */
787 /* Check to make sure the parent descriptor actually has a default
788 ACL. If it doesn't, then we can "succeed" immediately, saving a
789 little work, particularly in any_can_execute(). Note that we
790 can't skip the fstat() above, because we need it in case we
792 if (has_default_acl_fd(parent_fd
) == ACL_FAILURE
) {
793 result
= ACL_SUCCESS
;
794 /* Just because this target can't inherit anything doesn't mean
795 that one of it's children can't. For example, if there's a
796 default on "c" in "a/b/c/d", then we don't want to skip all
802 if (!S_ISDIR(s
.st_mode
)) {
803 /* If it's not a directory, make sure it's a regular,
804 non-hard-linked file. */
805 if (!S_ISREG(s
.st_mode
) || s
.st_nlink
!= 1) {
806 result
= ACL_FAILURE
;
807 goto cleanup
; /* It's not a directory, so we can skip the recursion. */
812 /* Next We try to guess whether or not to strip the execute bits.
813 * This behavior is modeled after the capital 'X' perms of setfacl.
815 int ace_result
= any_can_execute(fd
, &s
);
817 if (ace_result
== ACL_ERROR
) {
818 perror("apply_default_acl_fds (any_can_execute)");
823 /* Never mask the execute bit on directories. */
824 bool allow_exec
= (bool)ace_result
|| S_ISDIR(s
.st_mode
);
827 /* If it's a directory, inherit the parent's default. */
828 if (S_ISDIR(s
.st_mode
)) {
829 if (acl_copy_xattr(parent_fd
,
832 ACL_TYPE_DEFAULT
) == ACL_ERROR
) {
833 perror("apply_default_acl_fds (acl_copy_xattr default)");
839 /* If it's anything, _apply_ the parent's default. */
840 if (acl_copy_xattr(parent_fd
,
843 ACL_TYPE_ACCESS
) == ACL_ERROR
) {
844 perror("apply_default_acl_fds (acl_copy_xattr access)");
849 /* There's a good reason why we saved the ACL above, even though
850 * we're about to read it back into memory and mess with it on the
851 * next line. The acl_copy_xattr() function is already a hack to let
852 * us copy default ACLs without resorting to path names; we simply
853 * have no way to read the parent's default ACL into memory using
854 * parent_fd. We can, however, copy the parent's ACL to a file (with
855 * acl_copy_xattr), and then read the ACL from a file using
856 * "fd". It's quite the circus, but it works and should be safe from
857 * sym/hardlink attacks.
860 /* Now we potentially need to mask the execute permissions in the
861 ACL on fd; or maybe not. */
863 /* Skip the mask code for this target, but don't skip its children! */
867 /* OK, we need to mask some execute permissions. First obtain the
869 new_acl
= acl_get_fd(fd
);
870 if (new_acl
== (acl_t
)NULL
) {
871 perror("apply_default_acl_fds (acl_get_fd)");
876 /* ...and now make a copy of it, because otherwise when we loop
877 below, some shit gets stuck (modifying the structure while
878 looping over it no worky). */
879 new_acl_unmasked
= acl_dup(new_acl
);
880 if (new_acl_unmasked
== (acl_t
)NULL
) {
881 perror("apply_default_acl_fds (acl_dup)");
887 int ge_result
= acl_get_entry(new_acl_unmasked
, ACL_FIRST_ENTRY
, &entry
);
889 while (ge_result
== ACL_SUCCESS
) {
890 acl_tag_t tag
= ACL_UNDEFINED_TAG
;
892 if (acl_get_tag_type(entry
, &tag
) == ACL_ERROR
) {
893 perror("apply_default_acl_fds (acl_get_tag_type)");
899 /* We've got an entry/tag from the default ACL. Get its permset. */
900 acl_permset_t permset
;
901 if (acl_get_permset(entry
, &permset
) == ACL_ERROR
) {
902 perror("apply_default_acl_fds (acl_get_permset)");
907 /* To mimic what the kernel does, I think we could drop
908 ACL_GROUP_OBJ from the list below? */
909 if (tag
== ACL_MASK
||
910 tag
== ACL_USER_OBJ
||
911 tag
== ACL_GROUP_OBJ
||
914 /* The mask doesn't affect acl_user_obj, acl_group_obj (in
915 minimal ACLs) or acl_other entries, so if execute should be
916 masked, we have to do it manually. */
917 if (acl_delete_perm(permset
, ACL_EXECUTE
) == ACL_ERROR
) {
918 perror("apply_default_acl_fds (acl_delete_perm)");
923 if (acl_set_permset(entry
, permset
) == ACL_ERROR
) {
924 perror("apply_default_acl_fds (acl_set_permset)");
930 if (acl_update_entry(new_acl
, entry
) == ACL_ERROR
) {
931 perror("apply_default_acl_fds (acl_update_entry)");
936 ge_result
= acl_get_entry(new_acl_unmasked
, ACL_NEXT_ENTRY
, &entry
);
939 /* Catches the first acl_get_entry as well as the ones at the end of
941 if (ge_result
== ACL_ERROR
) {
942 perror("apply_default_acl_fds (acl_get_entry)");
947 if (acl_set_fd(fd
, new_acl
) == ACL_ERROR
) {
948 perror("apply_default_acl_fds (acl_set_fd)");
954 if (recursive
&& S_ISDIR(s
.st_mode
)) {
955 /* Recurse into subdirectories. Don't call closedir() on d! It
956 closes the open file descriptor as well, and subsequent calls
957 to close() then throw errors. */
958 DIR* d
= fdopendir(fd
);
960 perror("apply_default_acl_fds (fdopendir)");
967 while ((de
= readdir(d
)) != NULL
) {
968 if (de
->d_type
!= DT_DIR
&& de
->d_type
!= DT_REG
) {
969 /* Hit a symlink or whatever. */
970 result
= ACL_FAILURE
;
973 if (strcmp(de
->d_name
, ".") == 0) { continue; }
974 if (strcmp(de
->d_name
, "..") == 0) { continue; }
976 /* Be careful not to "return" out of this loop and leave the
978 new_fd
= openat(fd
, de
->d_name
, O_NOFOLLOW
);
979 if (new_fd
== OPEN_ERROR
) {
980 if (errno
== ELOOP
|| errno
== ENOTDIR
) {
981 /* We hit a symlink, either in the last path component (ELOOP)
982 or higher up (ENOTDIR). */
983 if (result
== ACL_SUCCESS
) {
984 /* Don't overwrite an error result with success/failure. */
985 result
= ACL_FAILURE
;
990 perror("apply_default_acl_fds (openat)");
995 switch (apply_default_acl_fds(fd
, new_fd
, recursive
)) {
996 /* Don't overwrite an error result with success/failure. */
998 if (result
== ACL_SUCCESS
) {
999 result
= ACL_FAILURE
;
1005 if (close(new_fd
) == CLOSE_ERROR
) {
1006 perror("apply_default_acl_fds (close)");
1015 acl_free(new_acl_unmasked
);
1021 * @brief Apply parent default ACL to a path and optionally its children.
1023 * This overwrites any existing ACLs on the target, and, if @c
1024 * recursive is @c true, its children. When @c recursive is @c true,
1025 * the "worst" result encountered is returned as the overall result.
1028 * The path whose ACL we would like to reset to its default.
1031 * Should we recurse into subdirectories?
1034 * - @c ACL_SUCCESS - The parent default ACLs were inherited successfully.
1035 * - @c ACL_FAILURE - If symlinks or hard links are encountered.
1036 * - @c ACL_ERROR - Unexpected library error.
1038 int apply_default_acl(const char* path
, bool recursive
) {
1042 perror("apply_default_acl (args)");
1046 /* Define these next three variables here because we may have to
1047 * jump to the cleanup routine which expects them to exist.
1050 /* Our return value. */
1051 int result
= ACL_SUCCESS
;
1053 /* The file descriptor corresponding to "path" */
1056 /* The file descriptor for the directory containing "path" */
1059 /* dirname() and basename() mangle their arguments, so we need
1060 to make copies of "path" before using them. */
1061 char* dirname_path_copy
= NULL
;
1062 char* basename_path_copy
= NULL
;
1064 /* Get the parent directory of "path" with dirname(), which happens
1065 * to murder its argument and necessitates a path_copy. */
1066 dirname_path_copy
= strdup(path
);
1067 if (dirname_path_copy
== NULL
) {
1068 perror("apply_default_acl (strdup)");
1071 char* parent
= dirname(dirname_path_copy
);
1073 basename_path_copy
= strdup(path
);
1074 if (basename_path_copy
== NULL
) {
1075 perror("apply_default_acl (strdup)");
1079 char* child
= basename(basename_path_copy
);
1081 /* Just kidding, if the path is "." or "..", then dirname will do
1082 * the wrong thing and give us "." as its parent, too. So, we handle
1083 * those as special cases. We use "child" instead of "path" here to
1084 * catch things like "./" and "../"
1086 bool path_is_dots
= strcmp(child
, ".") == 0 || strcmp(child
, "..") == 0;
1087 char dots_parent
[6] = "../";
1089 /* We know that "child" contains no more than two characters here, and
1090 using strncat to enforce that belief keeps clang-tidy happy. */
1091 parent
= strncat(dots_parent
, child
, 2);
1094 parent_fd
= safe_open(parent
, O_DIRECTORY
| O_NOFOLLOW
);
1096 if (parent_fd
== OPEN_ERROR
) {
1097 if (errno
== ELOOP
|| errno
== ENOTDIR
) {
1098 /* We hit a symlink, either in the last path component (ELOOP)
1099 or higher up (ENOTDIR). */
1100 result
= ACL_FAILURE
;
1104 perror("apply_default_acl (open parent fd)");
1110 /* We already obtained the parent fd safely, so if we use the
1111 * basename of path here instead of the full thing, then we can get
1112 * away with using openat() and spare ourselves the slowness of
1113 * another safe_open().
1115 * Note that if the basename is "." or "..", then we don't want to
1116 * open it relative to the parent_fd, so we need another special
1117 * case for those paths here.
1120 fd
= open(child
, O_NOFOLLOW
);
1123 fd
= openat(parent_fd
, child
, O_NOFOLLOW
);
1125 if (fd
== OPEN_ERROR
) {
1126 if (errno
== ELOOP
|| errno
== ENOTDIR
) {
1127 /* We hit a symlink, either in the last path component (ELOOP)
1128 or higher up (ENOTDIR). */
1129 result
= ACL_FAILURE
;
1133 perror("apply_default_acl (open fd)");
1139 result
= apply_default_acl_fds(parent_fd
, fd
, recursive
);
1142 free(dirname_path_copy
);
1143 free(basename_path_copy
);
1145 if (parent_fd
> 0 && close(parent_fd
) == CLOSE_ERROR
) {
1146 perror("apply_default_acl (close parent_fd)");
1149 if (fd
> 0 && close(fd
) == CLOSE_ERROR
) {
1150 perror("apply_default_acl (close fd)");