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 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 Determine if a file descriptor has a default ACL.
688 * The file descriptor whose default ACL is in question.
691 * - @c ACL_SUCCESS - If @c fd has a default ACL.
692 * - @c ACL_FAILURE - If @c fd does not have a default ACL.
693 * - @c ACL_ERROR - Unexpected library error.
695 int has_default_acl_fd(int fd
) {
696 if (fgetxattr(fd
, XATTR_NAME_POSIX_ACL_DEFAULT
, NULL
, 0) == XATTR_ERROR
) {
697 if (errno
== ENODATA
) {
700 perror("has_default_acl_fd (fgetxattr)");
709 * @brief Apply parent default ACL to a path.
711 * This overwrites any existing ACLs on @c path.
714 * The path whose ACL we would like to reset to its default.
717 * A pointer to a stat structure for @c path, or @c NULL if you don't
720 * @param no_exec_mask
721 * The value (either true or false) of the --no-exec-mask flag.
724 * - @c ACL_SUCCESS - The parent default ACL was inherited successfully.
725 * - @c ACL_FAILURE - If symlinks or hard links are encountered.
726 * - @c ACL_ERROR - Unexpected library error.
728 int apply_default_acl_ex(const char* path
,
729 const struct stat
* sp
,
734 perror("apply_default_acl_ex (args)");
738 /* Define these next three variables here because we may have to
739 * jump to the cleanup routine which expects them to exist.
742 /* Our return value. */
743 int result
= ACL_SUCCESS
;
745 /* The new ACL for this path */
746 acl_t new_acl
= (acl_t
)NULL
;
748 /* A copy of new_acl, to be made before we begin mangling new_acl in
749 order to mask the execute bit. */
750 acl_t new_acl_unmasked
= (acl_t
)NULL
;
752 /* The file descriptor corresponding to "path" */
755 /* The file descriptor for the directory containing "path" */
758 /* Get the parent directory of "path" with dirname(), which happens
759 * to murder its argument and necessitates a path_copy. */
760 char* path_copy
= strdup(path
);
761 if (path_copy
== NULL
) {
762 perror("apply_default_acl_ex (strdup)");
765 char* parent
= dirname(path_copy
);
766 parent_fd
= safe_open(parent
, O_DIRECTORY
| O_NOFOLLOW
);
767 if (parent_fd
== OPEN_ERROR
) {
768 if (errno
== ELOOP
|| errno
== ENOTDIR
) {
769 /* We hit a symlink, either in the last path component (ELOOP)
770 or higher up (ENOTDIR). */
771 result
= ACL_FAILURE
;
775 perror("apply_default_acl_ex (open parent fd)");
781 /* Check to make sure the parent descriptor actually has a default
782 ACL. If it doesn't, then we can "succeed" immediately. */
783 if (has_default_acl_fd(parent_fd
) == ACL_FAILURE
) {
784 result
= ACL_SUCCESS
;
788 fd
= safe_open(path
, O_NOFOLLOW
);
789 if (fd
== OPEN_ERROR
) {
790 if (errno
== ELOOP
|| errno
== ENOTDIR
) {
791 /* We hit a symlink, either in the last path component (ELOOP)
792 or higher up (ENOTDIR). */
793 result
= ACL_FAILURE
;
797 perror("apply_default_acl_ex (open fd)");
803 /* Refuse to operate on hard links, which can be abused by an
804 * attacker to trick us into changing the ACL on a file we didn't
805 * intend to; namely the "target" of the hard link. There is TOCTOU
806 * race condition here, but the window is as small as possible
807 * between when we open the file descriptor (look above) and when we
810 * Note: we only need to call fstat ourselves if we weren't passed a
811 * valid pointer to a stat structure (nftw does that).
815 if (fstat(fd
, &s
) == STAT_ERROR
) {
816 perror("apply_default_acl_ex (fstat)");
823 if (!S_ISDIR(sp
->st_mode
)) {
824 /* If it's not a directory, make sure it's a regular,
825 non-hard-linked file. */
826 if (!S_ISREG(sp
->st_mode
) || sp
->st_nlink
!= 1) {
827 result
= ACL_FAILURE
;
833 /* Default to not masking the exec bit; i.e. applying the default
834 ACL literally. If --no-exec-mask was not specified, then we try
835 to "guess" whether or not to mask the exec bit. This behavior
836 is modeled after the capital 'X' perms of setfacl. */
837 bool allow_exec
= true;
840 /* Never mask the execute bit on directories. */
841 int ace_result
= any_can_execute(fd
,sp
) || S_ISDIR(sp
->st_mode
);
843 if (ace_result
== ACL_ERROR
) {
844 perror("apply_default_acl_ex (any_can_execute)");
849 allow_exec
= (bool)ace_result
;
852 if (wipe_acls(fd
) == ACL_ERROR
) {
853 perror("apply_default_acl_ex (wipe_acls)");
858 /* If it's a directory, inherit the parent's default. */
859 if (S_ISDIR(sp
->st_mode
)) {
860 if (acl_copy_xattr(parent_fd
,
863 ACL_TYPE_DEFAULT
) == ACL_ERROR
) {
864 perror("apply_default_acl_ex (acl_copy_xattr default)");
870 /* If it's anything, _apply_ the parent's default. */
871 if (acl_copy_xattr(parent_fd
,
874 ACL_TYPE_ACCESS
) == ACL_ERROR
) {
875 perror("apply_default_acl_ex (acl_copy_xattr access)");
880 /* There's a good reason why we saved the ACL above, even though
881 * we're about tto read it back into memory and mess with it on the
882 * next line. The acl_copy_xattr() function is already a hack to let
883 * us copy default ACLs without resorting to path names; we simply
884 * have no way to read the parent's default ACL into memory using
885 * parent_fd. We can, however, copy the parent's ACL to a file (with
886 * acl_copy_xattr), and then read the ACL from a file using
887 * "fd". It's quite the circus, but it works and should be safe from
888 * sym/hardlink attacks.
891 /* Now we potentially need to mask the execute permissions in the
892 ACL on fd; or maybe now. */
897 /* OK, we need to mask some execute permissions. First obtain the
899 new_acl
= acl_get_fd(fd
);
900 if (new_acl
== (acl_t
)NULL
) {
901 perror("apply_default_acl_ex (acl_get_fd)");
906 /* ...and now make a copy of it, because otherwise when we loop
907 below, some shit gets stuck (modifying the structure while
908 looping over it no worky). */
909 new_acl_unmasked
= acl_dup(new_acl
);
910 if (new_acl_unmasked
== (acl_t
)NULL
) {
911 perror("apply_default_acl_ex (acl_dup)");
917 int ge_result
= acl_get_entry(new_acl_unmasked
, ACL_FIRST_ENTRY
, &entry
);
919 while (ge_result
== ACL_SUCCESS
) {
920 acl_tag_t tag
= ACL_UNDEFINED_TAG
;
922 if (acl_get_tag_type(entry
, &tag
) == ACL_ERROR
) {
923 perror("apply_default_acl_ex (acl_get_tag_type)");
929 /* We've got an entry/tag from the default ACL. Get its permset. */
930 acl_permset_t permset
;
931 if (acl_get_permset(entry
, &permset
) == ACL_ERROR
) {
932 perror("apply_default_acl_ex (acl_get_permset)");
937 if (tag
== ACL_MASK
||
938 tag
== ACL_USER_OBJ
||
939 tag
== ACL_GROUP_OBJ
||
942 /* The mask doesn't affect acl_user_obj, acl_group_obj (in
943 minimal ACLs) or acl_other entries, so if execute should be
944 masked, we have to do it manually. */
945 if (acl_delete_perm(permset
, ACL_EXECUTE
) == ACL_ERROR
) {
946 perror("apply_default_acl_ex (acl_delete_perm)");
951 if (acl_set_permset(entry
, permset
) == ACL_ERROR
) {
952 perror("apply_default_acl_ex (acl_set_permset)");
958 /* Finally, add the permset to the access ACL. It's actually
959 * important that we pass in the address of "new_acl" here, and not
960 * "new_acl" itself. Why? The call to acl_create_entry() within
961 * acl_set_entry() can allocate new memory for the entry.
962 * Sometimes that can be done in-place, in which case everything
963 * is cool and the new memory gets released when we call
966 * But occasionally, the whole ACL structure will have to be moved
967 * in order to allocate the extra space. When that happens,
968 * acl_create_entry() modifies the pointer it was passed (in this
969 * case, &acl) to point to the new location. We want to call
970 * acl_free() on the new location, and since acl_free() gets
971 * called right here, we need acl_create_entry() to update the
972 * value of "new_acl". To do that, it needs the address of "new_acl".
975 if (acl_set_entry(&new_acl
, entry
) == ACL_ERROR
) {
976 perror("apply_default_acl_ex (acl_set_entry)");
981 ge_result
= acl_get_entry(new_acl_unmasked
, ACL_NEXT_ENTRY
, &entry
);
984 /* Catches the first acl_get_entry as well as the ones at the end of
986 if (ge_result
== ACL_ERROR
) {
987 perror("apply_default_acl_ex (acl_get_entry)");
992 if (acl_set_fd(fd
, new_acl
) == ACL_ERROR
) {
993 perror("apply_default_acl_ex (acl_set_fd)");
1000 if (new_acl
!= (acl_t
)NULL
) {
1003 if (new_acl_unmasked
!= (acl_t
)NULL
) {
1004 acl_free(new_acl_unmasked
);
1006 if (fd
> 0 && close(fd
) == CLOSE_ERROR
) {
1007 perror("apply_default_acl_ex (close fd)");
1010 if (parent_fd
> 0 && close(parent_fd
) == CLOSE_ERROR
) {
1011 perror("apply_default_acl_ex (close parent_fd)");
1020 * @brief The friendly interface to @c apply_default_acl_ex.
1022 * The @c apply_default_acl_ex function holds the real implementation
1023 * of this function, but it takes a weird second argument that most
1024 * people won't care about (a stat structure). But, we use that
1025 * argument for the recursive mode of the CLI, so it's there.
1027 * If you don't have a stat structure for your @c path, use this instead.
1030 * The path whose ACL we would like to reset to its default.
1032 * @param no_exec_mask
1033 * The value (either true or false) of the --no-exec-mask flag.
1036 * - @c ACL_SUCCESS - The parent default ACL was inherited successfully.
1037 * - @c ACL_FAILURE - If symlinks or hard links are encountered.
1038 * or the parent of @c path is not a directory.
1039 * - @c ACL_ERROR - Unexpected library error.
1041 int apply_default_acl(const char* path
, bool no_exec_mask
) {
1042 return apply_default_acl_ex(path
, NULL
, no_exec_mask
);