4 * @brief The adacl (apply default acl) shared library.
8 /* Enables get_current_dir_name() in unistd.h and the O_PATH flag. */
11 #include <errno.h> /* EINVAL, ELOOP, ENOTDIR, etc. */
12 #include <fcntl.h> /* openat() */
13 #include <libgen.h> /* basename(), dirname() */
14 #include <limits.h> /* PATH_MAX */
15 #include <stdbool.h> /* the "bool" type */
16 #include <stdio.h> /* perror(), snprintf() */
17 #include <stdlib.h> /* free() */
18 #include <string.h> /* strdup() */
19 #include <sys/stat.h> /* fstat() */
20 #include <sys/xattr.h> /* fgetxattr(), fsetxattr() */
21 #include <unistd.h> /* get_current_dir_name() */
24 #include <acl/libacl.h> /* acl_get_perm, not portable */
25 #include <sys/acl.h> /* all other acl_foo functions */
27 /* XATTR_NAME_POSIX_ACL_ACCESS and XATTR_NAME_POSIX_ACL_DEFAULT */
28 #include <linux/xattr.h>
33 /* Even though most other library functions reliably return -1 for
34 * error, it feels a little wrong to re-use the ACL_ERROR constant.
36 #define CLOSE_ERROR -1
38 #define SNPRINTF_ERROR -1
40 #define XATTR_ERROR -1
44 * @brief The recursive portion of the @c safe_open function, used to
45 * open a file descriptor in a symlink-safe way when combined with
46 * the @c O_NOFOLLOW flag.
49 * A file descriptor relative to which @c pathname will be opened.
52 * The path to the file/directory/whatever whose descriptor you want.
55 * File status flags to be passed to @c openat.
57 * @return a file descriptor for @c pathname if everything goes well,
58 * and @c OPEN_ERROR if not.
60 int safe_open_ex(int at_fd
, char* pathname
, int flags
) {
61 if (pathname
== NULL
) {
63 perror("safe_open_ex (args)");
67 char* firstslash
= strchr(pathname
, '/');
68 if (firstslash
== NULL
) {
69 /* No more slashes, this is the base case. */
70 return openat(at_fd
, pathname
, flags
);
72 else if (firstslash
[1] == '\0') {
73 /* The first slash is the last character; ensure that we open
76 return openat(at_fd
, pathname
, flags
| O_DIRECTORY
);
79 /* The first slash exists and isn't the last character in the path,
80 so we can split the path wherever that first slash lies and
83 int fd
= openat(at_fd
, pathname
, flags
| O_DIRECTORY
| O_PATH
);
84 if (fd
== OPEN_ERROR
) {
85 if (errno
!= ENOTDIR
) {
86 /* Don't output anything if we ignore a symlink */
87 perror("safe_open_ex (safe_open_ex)");
92 /* The +1 is safe because there needs to be at least one character
93 after the first slash (we checked this above). */
94 int result
= safe_open_ex(fd
, firstslash
+1, flags
);
95 if (close(fd
) == CLOSE_ERROR
) {
96 perror("safe_open_ex (close)");
104 * @brief A version of @c open that is completely symlink-safe when
105 * used with the @c O_NOFOLLOW flag.
107 * The @c openat function exists to ensure that you can anchor one
108 * path to a particular directory while opening it; however, if you
109 * open "b/c/d" relative to "/a", then even the @c openat function will
110 * still follow symlinks in the "b" component. This can be exploited
111 * by an attacker to make you open the wrong path.
113 * To avoid that problem, this function uses a recursive
114 * implementation that opens every path from the root, one level at a
115 * time. So "a" is opened relative to "/", and then "b" is opened
116 * relative to "/a", and then "c" is opened relative to "/a/b",
117 * etc. When the @c O_NOFOLLOW flag is used, this approach ensures
118 * that no symlinks in any component are followed.
121 * The path to the file/directory/whatever whose descriptor you want.
124 * File status flags to be passed to @c openat.
126 * @return a file descriptor for @c pathname if everything goes well,
127 * and @c OPEN_ERROR if not.
129 int safe_open(const char* pathname
, int flags
) {
130 if (pathname
== NULL
|| strlen(pathname
) == 0 || pathname
[0] == '\0') {
132 perror("safe_open (args)");
136 char abspath
[PATH_MAX
];
137 int snprintf_result
= 0;
138 if (strchr(pathname
, '/') == pathname
) {
139 /* pathname is already absolute; just copy it. */
140 snprintf_result
= snprintf(abspath
, PATH_MAX
, "%s", pathname
);
143 /* Concatenate the current working directory and pathname into an
144 * absolute path. We use realpath() ONLY on the cwd part, and not
145 * on the pathname part, because realpath() resolves symlinks. And
146 * the whole point of all this crap is to avoid following symlinks
149 * Using realpath() on the cwd lets us operate on relative paths
150 * while we're sitting in a directory that happens to have a
151 * symlink in it; for example: cd /var/run && apply-default-acl foo.
153 char* cwd
= get_current_dir_name();
155 perror("safe_open (get_current_dir_name)");
159 char abs_cwd
[PATH_MAX
];
160 if (realpath(cwd
, abs_cwd
) == NULL
) {
161 perror("safe_open (realpath)");
165 snprintf_result
= snprintf(abspath
, PATH_MAX
, "%s/%s", abs_cwd
, pathname
);
168 if (snprintf_result
== SNPRINTF_ERROR
|| snprintf_result
> PATH_MAX
) {
169 perror("safe_open (snprintf)");
174 if (strcmp(abspath
, "/") == 0) {
175 fd
= open("/", flags
| O_DIRECTORY
);
178 /* Use O_PATH for some added safety if "/" is not our target */
179 fd
= open("/", flags
| O_DIRECTORY
| O_PATH
);
181 if (fd
== OPEN_ERROR
) {
182 perror("safe_open (open)");
186 if (strcmp(abspath
, "/") == 0) {
190 int result
= safe_open_ex(fd
, abspath
+1, flags
);
191 if (close(fd
) == CLOSE_ERROR
) {
192 perror("safe_open (close)");
202 * @brief Update (or create) an entry in an @b minimal ACL.
204 * This function will not work if @c aclp contains extended
205 * entries. This is fine for our purposes, since we call @c wipe_acls
206 * on each path before applying the default to it.
208 * The assumption that there are no extended entries makes things much
209 * simpler. For example, we only have to update the @c ACL_USER_OBJ,
210 * @c ACL_GROUP_OBJ, and @c ACL_OTHER entries -- all others can simply
211 * be created anew. This means we don't have to fool around comparing
212 * named-user/group entries.
215 * A pointer to the acl_t structure whose entry we want to modify.
218 * The new entry. If @c entry contains a user/group/other entry, we
219 * update the existing one. Otherwise we create a new entry.
221 * @return If there is an unexpected library error, @c ACL_ERROR is
222 * returned. Otherwise, @c ACL_SUCCESS.
225 int acl_set_entry(acl_t
* aclp
, acl_entry_t entry
) {
226 if (aclp
== NULL
|| entry
== NULL
) {
228 perror("acl_set_entry (args)");
233 if (acl_get_tag_type(entry
, &entry_tag
) == ACL_ERROR
) {
234 perror("acl_set_entry (acl_get_tag_type)");
238 acl_permset_t entry_permset
;
239 if (acl_get_permset(entry
, &entry_permset
) == ACL_ERROR
) {
240 perror("acl_set_entry (acl_get_permset)");
244 acl_entry_t existing_entry
;
245 /* Loop through the given ACL looking for matching entries. */
246 int result
= acl_get_entry(*aclp
, ACL_FIRST_ENTRY
, &existing_entry
);
248 while (result
== ACL_SUCCESS
) {
249 acl_tag_t existing_tag
= ACL_UNDEFINED_TAG
;
251 if (acl_get_tag_type(existing_entry
, &existing_tag
) == ACL_ERROR
) {
252 perror("set_acl_tag_permset (acl_get_tag_type)");
256 if (existing_tag
== entry_tag
) {
257 /* If we update something, we're done and return ACL_SUCCESS */
258 if (acl_set_permset(existing_entry
, entry_permset
) == ACL_ERROR
) {
259 perror("acl_set_entry (acl_set_permset)");
266 result
= acl_get_entry(*aclp
, ACL_NEXT_ENTRY
, &existing_entry
);
269 /* This catches both the initial acl_get_entry and the ones at the
271 if (result
== ACL_ERROR
) {
272 perror("acl_set_entry (acl_get_entry)");
276 /* If we've made it this far, we need to add a new entry to the
278 acl_entry_t new_entry
;
280 /* The acl_create_entry() function can allocate new memory and/or
281 * change the location of the ACL structure entirely. When that
282 * happens, the value pointed to by aclp is updated, which means
283 * that a new acl_t gets "passed out" to our caller, eventually to
284 * be fed to acl_free(). In other words, we should still be freeing
285 * the right thing, even if the value pointed to by aclp changes.
287 if (acl_create_entry(aclp
, &new_entry
) == ACL_ERROR
) {
288 perror("acl_set_entry (acl_create_entry)");
292 if (acl_set_tag_type(new_entry
, entry_tag
) == ACL_ERROR
) {
293 perror("acl_set_entry (acl_set_tag_type)");
297 if (acl_set_permset(new_entry
, entry_permset
) == ACL_ERROR
) {
298 perror("acl_set_entry (acl_set_permset)");
302 if (entry_tag
== ACL_USER
|| entry_tag
== ACL_GROUP
) {
303 /* We need to set the qualifier too. */
304 void* entry_qual
= acl_get_qualifier(entry
);
305 if (entry_qual
== (void*)NULL
) {
306 perror("acl_set_entry (acl_get_qualifier)");
310 if (acl_set_qualifier(new_entry
, entry_qual
) == ACL_ERROR
) {
311 perror("acl_set_entry (acl_set_qualifier)");
322 * @brief Determine the number of entries in the given ACL.
325 * The ACL to inspect.
327 * @return Either the non-negative number of entries in @c acl, or
328 * @c ACL_ERROR on error.
330 int acl_entry_count(acl_t acl
) {
334 int result
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &entry
);
336 while (result
== ACL_SUCCESS
) {
338 result
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
341 if (result
== ACL_ERROR
) {
342 perror("acl_entry_count (acl_get_entry)");
352 * @brief Determine whether or not the given ACL is minimal.
354 * An ACL is minimal if it has fewer than four entries.
357 * The ACL whose minimality is in question.
360 * - @c ACL_SUCCESS - @c acl is minimal
361 * - @c ACL_FAILURE - @c acl is not minimal
362 * - @c ACL_ERROR - Unexpected library error
364 int acl_is_minimal(acl_t acl
) {
367 perror("acl_is_minimal (args)");
371 int ec
= acl_entry_count(acl
);
373 if (ec
== ACL_ERROR
) {
374 perror("acl_is_minimal (acl_entry_count)");
389 * @brief Determine whether the given ACL's mask denies execute.
392 * The ACL whose mask we want to check.
395 * - @c ACL_SUCCESS - The @c acl has a mask which denies execute.
396 * - @c ACL_FAILURE - The @c acl has a mask which does not deny execute.
397 * - @c ACL_ERROR - Unexpected library error.
399 int acl_execute_masked(acl_t acl
) {
402 perror("acl_execute_masked (args)");
407 int ge_result
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &entry
);
409 while (ge_result
== ACL_SUCCESS
) {
410 acl_tag_t tag
= ACL_UNDEFINED_TAG
;
412 if (acl_get_tag_type(entry
, &tag
) == ACL_ERROR
) {
413 perror("acl_execute_masked (acl_get_tag_type)");
417 if (tag
== ACL_MASK
) {
418 /* This is the mask entry, get its permissions, and see if
419 execute is specified. */
420 acl_permset_t permset
;
422 if (acl_get_permset(entry
, &permset
) == ACL_ERROR
) {
423 perror("acl_execute_masked (acl_get_permset)");
427 int gp_result
= acl_get_perm(permset
, ACL_EXECUTE
);
428 if (gp_result
== ACL_ERROR
) {
429 perror("acl_execute_masked (acl_get_perm)");
433 if (gp_result
== ACL_FAILURE
) {
434 /* No execute bit set in the mask; execute not allowed. */
439 ge_result
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
448 * @brief Determine whether @c fd is executable by anyone.
451 * This is used as part of the heuristic to determine whether or not
452 * we should mask the execute bit when inheriting an ACL. If @c fd
453 * describes a file, we check the @a effective permissions, contrary
454 * to what setfacl does.
457 * The file descriptor to check.
460 * A pointer to a stat structure for @c fd.
463 * - @c ACL_SUCCESS - Someone has effective execute permissions on @c fd.
464 * - @c ACL_FAILURE - Nobody can execute @c fd.
465 * - @c ACL_ERROR - Unexpected library error.
467 int any_can_execute(int fd
, const struct stat
* sp
) {
470 perror("any_can_execute (args)");
474 acl_t acl
= acl_get_fd(fd
);
476 if (acl
== (acl_t
)NULL
) {
477 perror("any_can_execute (acl_get_fd)");
481 /* Our return value. */
482 int result
= ACL_FAILURE
;
484 if (acl_is_minimal(acl
)) {
485 if (sp
->st_mode
& (S_IXUSR
| S_IXOTH
| S_IXGRP
)) {
486 result
= ACL_SUCCESS
;
490 result
= ACL_FAILURE
;
496 int ge_result
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &entry
);
498 while (ge_result
== ACL_SUCCESS
) {
499 /* The first thing we do is check to see if this is a mask
500 entry. If it is, we skip it entirely. */
501 acl_tag_t tag
= ACL_UNDEFINED_TAG
;
503 if (acl_get_tag_type(entry
, &tag
) == ACL_ERROR
) {
504 perror("any_can_execute_or (acl_get_tag_type)");
509 if (tag
== ACL_MASK
) {
510 ge_result
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
514 /* Ok, so it's not a mask entry. Check the execute perms. */
515 acl_permset_t permset
;
517 if (acl_get_permset(entry
, &permset
) == ACL_ERROR
) {
518 perror("any_can_execute_or (acl_get_permset)");
523 int gp_result
= acl_get_perm(permset
, ACL_EXECUTE
);
524 if (gp_result
== ACL_ERROR
) {
525 perror("any_can_execute (acl_get_perm)");
530 if (gp_result
== ACL_SUCCESS
) {
531 /* Only return ACL_SUCCESS if this execute bit is not masked. */
532 if (acl_execute_masked(acl
) != ACL_SUCCESS
) {
533 result
= ACL_SUCCESS
;
538 ge_result
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
541 if (ge_result
== ACL_ERROR
) {
542 perror("any_can_execute (acl_get_entry)");
555 * @brief Remove all @c ACL_TYPE_ACCESS entries from the given file
556 * descriptor, leaving the UNIX permission bits.
559 * The file descriptor whose ACLs we want to wipe.
562 * - @c ACL_SUCCESS - The ACLs were wiped successfully, or none
563 * existed in the first place.
564 * - @c ACL_ERROR - Unexpected library error.
566 int wipe_acls(int fd
) {
567 /* Initialize an empty ACL, and then overwrite the one on "fd" with it. */
568 acl_t empty_acl
= acl_init(0);
570 if (empty_acl
== (acl_t
)NULL
) {
571 perror("wipe_acls (acl_init)");
575 if (acl_set_fd(fd
, empty_acl
) == ACL_ERROR
) {
576 perror("wipe_acls (acl_set_fd)");
587 * @brief Copy ACLs between file descriptors as xattrs, verbatim.
589 * There is a small deficiency in libacl, namely that there is no way
590 * to get or set default ACLs through file descriptors. The @c
591 * acl_get_file and @c acl_set_file functions can do it, but they use
592 * paths, and are vulnerable to symlink attacks.
594 * Fortunately, when inheriting an ACL, we don't really need to look
595 * at what it contains. That means that we can copy the on-disk xattrs
596 * from the source directory to the destination file/directory without
597 * passing through libacl, and this can be done with file descriptors
598 * through @c fgetxattr and @c fsetxattr. That's what this function
602 * The file descriptor from which the ACL will be copied.
605 * The type of ACL (either @c ACL_TYPE_ACCESS or @c ACL_TYPE_DEFAULT)
606 * to copy from @c src_fd.
609 * The file descriptor whose ACL will be overwritten with the one
613 * The type of ACL (either @c ACL_TYPE_ACCESS or @c ACL_TYPE_DEFAULT)
614 * to replace on @c dst_fd.
617 * - @c ACL_SUCCESS - The ACL was copied successfully.
618 * - @c ACL_FAILURE - There was no ACL on @c src_fd.
619 * - @c ACL_ERROR - Unexpected library error.
621 int acl_copy_xattr(int src_fd
,
624 acl_type_t dst_type
) {
626 const char* src_name
;
627 if (src_type
== ACL_TYPE_ACCESS
) {
628 src_name
= XATTR_NAME_POSIX_ACL_ACCESS
;
630 else if (src_type
== ACL_TYPE_DEFAULT
) {
631 src_name
= XATTR_NAME_POSIX_ACL_DEFAULT
;
635 perror("acl_copy_xattr (src type)");
639 const char* dst_name
;
640 if (dst_type
== ACL_TYPE_ACCESS
) {
641 dst_name
= XATTR_NAME_POSIX_ACL_ACCESS
;
643 else if (dst_type
== ACL_TYPE_DEFAULT
) {
644 dst_name
= XATTR_NAME_POSIX_ACL_DEFAULT
;
648 perror("acl_copy_xattr (dst type)");
652 size_t src_size_guess
= fgetxattr(src_fd
, src_name
, NULL
, 0);
653 if (src_size_guess
== XATTR_ERROR
) {
654 if (errno
== ENODATA
) {
655 /* A missing ACL isn't really an error. ENOATTR and ENODATA are
656 synonyms, but using ENODATA here lets us avoid another
657 "include" directive. */
660 perror("acl_copy_xattr (fgetxattr size guess)");
663 char* src_acl_p
= alloca(src_size_guess
);
664 /* The actual size may be smaller than our guess? I don't know. */
665 size_t src_size
= fgetxattr(src_fd
, src_name
, src_acl_p
, (int)src_size_guess
);
666 if (src_size
== XATTR_ERROR
) {
667 if (errno
== ENODATA
) {
668 /* A missing ACL isn't an error. */
671 perror("acl_copy_xattr (fgetxattr)");
675 if (fsetxattr(dst_fd
, dst_name
, src_acl_p
, src_size
, 0) == XATTR_ERROR
) {
676 perror("acl_copy_xattr (fsetxattr)");
685 * @brief Apply parent default ACL to a path.
687 * This overwrites any existing ACLs on @c path.
690 * The path whose ACL we would like to reset to its default.
693 * A pointer to a stat structure for @c path, or @c NULL if you don't
696 * @param no_exec_mask
697 * The value (either true or false) of the --no-exec-mask flag.
700 * - @c ACL_SUCCESS - The parent default ACL was inherited successfully.
701 * - @c ACL_FAILURE - If symlinks or hard links are encountered.
702 * - @c ACL_ERROR - Unexpected library error.
704 int apply_default_acl_ex(const char* path
,
705 const struct stat
* sp
,
710 perror("apply_default_acl_ex (args)");
714 /* Define these next three variables here because we may have to
715 * jump to the cleanup routine which expects them to exist.
718 /* Our return value. */
719 int result
= ACL_SUCCESS
;
721 /* The new ACL for this path */
722 acl_t new_acl
= (acl_t
)NULL
;
724 /* A copy of new_acl, to be made before we begin mangling new_acl in
725 order to mask the execute bit. */
726 acl_t new_acl_unmasked
= (acl_t
)NULL
;
728 /* The file descriptor corresponding to "path" */
731 /* The file descriptor for the directory containing "path" */
734 /* Get the parent directory of "path" with dirname(), which happens
735 * to murder its argument and necessitates a path_copy. */
736 char* path_copy
= strdup(path
);
737 if (path_copy
== NULL
) {
738 perror("apply_default_acl_ex (strdup)");
741 char* parent
= dirname(path_copy
);
742 parent_fd
= safe_open(parent
, O_DIRECTORY
| O_NOFOLLOW
);
743 if (parent_fd
== OPEN_ERROR
) {
744 if (errno
== ELOOP
|| errno
== ENOTDIR
) {
745 /* We hit a symlink, either in the last path component (ELOOP)
746 or higher up (ENOTDIR). */
747 result
= ACL_FAILURE
;
751 perror("apply_default_acl_ex (open parent fd)");
757 fd
= safe_open(path
, O_NOFOLLOW
);
758 if (fd
== OPEN_ERROR
) {
759 if (errno
== ELOOP
|| errno
== ENOTDIR
) {
760 /* We hit a symlink, either in the last path component (ELOOP)
761 or higher up (ENOTDIR). */
762 result
= ACL_FAILURE
;
766 perror("apply_default_acl_ex (open fd)");
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
779 * Note: we only need to call fstat ourselves if we weren't passed a
780 * valid pointer to a stat structure (nftw does that).
784 if (fstat(fd
, &s
) == STAT_ERROR
) {
785 perror("apply_default_acl_ex (fstat)");
792 if (!S_ISDIR(sp
->st_mode
)) {
793 /* If it's not a directory, make sure it's a regular,
794 non-hard-linked file. */
795 if (!S_ISREG(sp
->st_mode
) || sp
->st_nlink
!= 1) {
796 result
= ACL_FAILURE
;
802 /* Default to not masking the exec bit; i.e. applying the default
803 ACL literally. If --no-exec-mask was not specified, then we try
804 to "guess" whether or not to mask the exec bit. This behavior
805 is modeled after the capital 'X' perms of setfacl. */
806 bool allow_exec
= true;
809 /* Never mask the execute bit on directories. */
810 int ace_result
= any_can_execute(fd
,sp
) || S_ISDIR(sp
->st_mode
);
812 if (ace_result
== ACL_ERROR
) {
813 perror("apply_default_acl_ex (any_can_execute)");
818 allow_exec
= (bool)ace_result
;
821 if (wipe_acls(fd
) == ACL_ERROR
) {
822 perror("apply_default_acl_ex (wipe_acls)");
827 /* If it's a directory, inherit the parent's default. */
828 if (S_ISDIR(sp
->st_mode
)) {
829 if (acl_copy_xattr(parent_fd
,
832 ACL_TYPE_DEFAULT
) == ACL_ERROR
) {
833 perror("apply_default_acl_ex (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_ex (acl_copy_xattr access)");
849 /* There's a good reason why we saved the ACL above, even though
850 * we're about tto 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. First obtain the current one... */
862 new_acl
= acl_get_fd(fd
);
863 if (new_acl
== (acl_t
)NULL
) {
864 perror("apply_default_acl_ex (acl_get_fd)");
869 /* ...and now make a copy of it, because otherwise when we loop
870 below, some shit gets stuck (modifying the structure while
871 looping over it no worky). */
872 new_acl_unmasked
= acl_dup(new_acl
);
873 if (new_acl_unmasked
== (acl_t
)NULL
) {
874 perror("apply_default_acl_ex (acl_dup)");
880 int ge_result
= acl_get_entry(new_acl_unmasked
, ACL_FIRST_ENTRY
, &entry
);
882 while (ge_result
== ACL_SUCCESS
) {
883 acl_tag_t tag
= ACL_UNDEFINED_TAG
;
885 if (acl_get_tag_type(entry
, &tag
) == ACL_ERROR
) {
886 perror("apply_default_acl_ex (acl_get_tag_type)");
892 /* We've got an entry/tag from the default ACL. Get its permset. */
893 acl_permset_t permset
;
894 if (acl_get_permset(entry
, &permset
) == ACL_ERROR
) {
895 perror("apply_default_acl_ex (acl_get_permset)");
900 if (tag
== ACL_MASK
||
901 tag
== ACL_USER_OBJ
||
902 tag
== ACL_GROUP_OBJ
||
906 /* The mask doesn't affect acl_user_obj, acl_group_obj (in
907 minimal ACLs) or acl_other entries, so if execute should be
908 masked, we have to do it manually. */
909 if (acl_delete_perm(permset
, ACL_EXECUTE
) == ACL_ERROR
) {
910 perror("apply_default_acl_ex (acl_delete_perm)");
915 if (acl_set_permset(entry
, permset
) == ACL_ERROR
) {
916 perror("apply_default_acl_ex (acl_set_permset)");
923 /* Finally, add the permset to the access ACL. It's actually
924 * important that we pass in the address of "new_acl" here, and not
925 * "new_acl" itself. Why? The call to acl_create_entry() within
926 * acl_set_entry() can allocate new memory for the entry.
927 * Sometimes that can be done in-place, in which case everything
928 * is cool and the new memory gets released when we call
931 * But occasionally, the whole ACL structure will have to be moved
932 * in order to allocate the extra space. When that happens,
933 * acl_create_entry() modifies the pointer it was passed (in this
934 * case, &acl) to point to the new location. We want to call
935 * acl_free() on the new location, and since acl_free() gets
936 * called right here, we need acl_create_entry() to update the
937 * value of "new_acl". To do that, it needs the address of "new_acl".
940 if (acl_set_entry(&new_acl
, entry
) == ACL_ERROR
) {
941 perror("apply_default_acl_ex (acl_set_entry)");
946 ge_result
= acl_get_entry(new_acl_unmasked
, ACL_NEXT_ENTRY
, &entry
);
949 /* Catches the first acl_get_entry as well as the ones at the end of
951 if (ge_result
== ACL_ERROR
) {
952 perror("apply_default_acl_ex (acl_get_entry)");
957 if (acl_set_fd(fd
, new_acl
) == ACL_ERROR
) {
958 perror("apply_default_acl_ex (acl_set_fd)");
965 if (new_acl
!= (acl_t
)NULL
) {
968 if (new_acl_unmasked
!= (acl_t
)NULL
) {
969 acl_free(new_acl_unmasked
);
971 if (fd
>= 0 && close(fd
) == CLOSE_ERROR
) {
972 perror("apply_default_acl_ex (close fd)");
975 if (parent_fd
>= 0 && close(parent_fd
) == CLOSE_ERROR
) {
976 perror("apply_default_acl_ex (close parent_fd)");
985 * @brief The friendly interface to @c apply_default_acl_ex.
987 * The @c apply_default_acl_ex function holds the real implementation
988 * of this function, but it takes a weird second argument that most
989 * people won't care about (a stat structure). But, we use that
990 * argument for the recursive mode of the CLI, so it's there.
992 * If you don't have a stat structure for your @c path, use this instead.
995 * The path whose ACL we would like to reset to its default.
997 * @param no_exec_mask
998 * The value (either true or false) of the --no-exec-mask flag.
1001 * - @c ACL_SUCCESS - The parent default ACL was inherited successfully.
1002 * - @c ACL_FAILURE - If symlinks or hard links are encountered.
1003 * or the parent of @c path is not a directory.
1004 * - @c ACL_ERROR - Unexpected library error.
1006 int apply_default_acl(const char* path
, bool no_exec_mask
) {
1007 return apply_default_acl_ex(path
, NULL
, no_exec_mask
);