2 * @file apply-default-acl.c
4 * @brief The entire implementation.
8 /* On Linux, ftw.h needs this special voodoo to work. */
9 #define _XOPEN_SOURCE 500
13 #include <fcntl.h> /* AT_FOO constants */
14 #include <ftw.h> /* nftw() et al. */
16 #include <libgen.h> /* basename(), dirname() */
25 #include <acl/libacl.h> /* acl_get_perm, not portable */
26 #include <sys/types.h>
29 /* Most of the libacl functions return 1 for success, 0 for failure,
39 * @brief Determine whether or not the given path is accessible.
44 * @return true if @c path is accessible to the current effective
45 * user/group, false otherwise.
47 bool path_accessible(const char* path
) {
52 /* Test for access using the effective user and group rather than
54 int flags
= AT_EACCESS
;
56 /* Don't follow symlinks when checking for a path's existence,
57 since we won't follow them to set its ACLs either. */
58 flags
|= AT_SYMLINK_NOFOLLOW
;
60 /* If the path is relative, interpret it relative to the current
61 working directory (just like the access() system call). */
62 if (faccessat(AT_FDCWD
, path
, F_OK
, flags
) == 0) {
73 * @brief Determine whether or not the given path is a directory.
78 * @return true if @c path is a directory, false otherwise.
80 bool is_path_directory(const char* path
) {
86 if (lstat(path
, &s
) == 0) {
87 return S_ISDIR(s
.st_mode
);
96 * @brief Determine whether or not the given file descriptor is for
100 * The file descriptor whose directoryness is in question.
102 * @return true if @c fd describes a directory, and false otherwise.
104 bool is_directory(int fd
) {
110 if (fstat(fd
, &s
) == 0) {
111 return S_ISDIR(s
.st_mode
);
121 * @brief Update (or create) an entry in an @b minimal ACL.
123 * This function will not work if @c aclp contains extended
124 * entries. This is fine for our purposes, since we call @c wipe_acls
125 * on each path before applying the default to it.
127 * The assumption that there are no extended entries makes things much
128 * simpler. For example, we only have to update the @c ACL_USER_OBJ,
129 * @c ACL_GROUP_OBJ, and @c ACL_OTHER entries -- all others can simply
130 * be created anew. This means we don't have to fool around comparing
131 * named-user/group entries.
134 * A pointer to the acl_t structure whose entry we want to modify.
137 * The new entry. If @c entry contains a user/group/other entry, we
138 * update the existing one. Otherwise we create a new entry.
140 * @return If there is an unexpected library error, @c ACL_ERROR is
141 * returned. Otherwise, @c ACL_SUCCESS.
144 int acl_set_entry(acl_t
* aclp
, acl_entry_t entry
) {
147 if (acl_get_tag_type(entry
, &entry_tag
) == ACL_ERROR
) {
148 perror("acl_set_entry (acl_get_tag_type)");
152 acl_permset_t entry_permset
;
153 if (acl_get_permset(entry
, &entry_permset
) == ACL_ERROR
) {
154 perror("acl_set_entry (acl_get_permset)");
158 acl_entry_t existing_entry
;
159 /* Loop through the given ACL looking for matching entries. */
160 int result
= acl_get_entry(*aclp
, ACL_FIRST_ENTRY
, &existing_entry
);
162 while (result
== ACL_SUCCESS
) {
163 acl_tag_t existing_tag
= ACL_UNDEFINED_TAG
;
165 if (acl_get_tag_type(existing_entry
, &existing_tag
) == ACL_ERROR
) {
166 perror("set_acl_tag_permset (acl_get_tag_type)");
170 if (existing_tag
== entry_tag
) {
171 if (entry_tag
== ACL_USER_OBJ
||
172 entry_tag
== ACL_GROUP_OBJ
||
173 entry_tag
== ACL_OTHER
) {
174 /* Only update for these three since all other tags will have
175 been wiped. These three are guaranteed to exist, so if we
176 match one of them, we're allowed to return ACL_SUCCESS
177 below and bypass the rest of the function. */
178 acl_permset_t existing_permset
;
179 if (acl_get_permset(existing_entry
, &existing_permset
) == ACL_ERROR
) {
180 perror("acl_set_entry (acl_get_permset)");
184 if (acl_set_permset(existing_entry
, entry_permset
) == ACL_ERROR
) {
185 perror("acl_set_entry (acl_set_permset)");
194 result
= acl_get_entry(*aclp
, ACL_NEXT_ENTRY
, &existing_entry
);
197 /* This catches both the initial acl_get_entry and the ones at the
199 if (result
== ACL_ERROR
) {
200 perror("acl_set_entry (acl_get_entry)");
204 /* If we've made it this far, we need to add a new entry to the
206 acl_entry_t new_entry
;
208 /* The acl_create_entry() function can allocate new memory and/or
209 * change the location of the ACL structure entirely. When that
210 * happens, the value pointed to by aclp is updated, which means
211 * that a new acl_t gets "passed out" to our caller, eventually to
212 * be fed to acl_free(). In other words, we should still be freeing
213 * the right thing, even if the value pointed to by aclp changes.
215 if (acl_create_entry(aclp
, &new_entry
) == ACL_ERROR
) {
216 perror("acl_set_entry (acl_create_entry)");
220 if (acl_set_tag_type(new_entry
, entry_tag
) == ACL_ERROR
) {
221 perror("acl_set_entry (acl_set_tag_type)");
225 if (acl_set_permset(new_entry
, entry_permset
) == ACL_ERROR
) {
226 perror("acl_set_entry (acl_set_permset)");
230 if (entry_tag
== ACL_USER
|| entry_tag
== ACL_GROUP
) {
231 /* We need to set the qualifier too. */
232 void* entry_qual
= acl_get_qualifier(entry
);
233 if (entry_qual
== (void*)NULL
) {
234 perror("acl_set_entry (acl_get_qualifier)");
238 if (acl_set_qualifier(new_entry
, entry_qual
) == ACL_ERROR
) {
239 perror("acl_set_entry (acl_set_qualifier)");
250 * @brief Determine the number of entries in the given ACL.
253 * The ACL to inspect.
255 * @return Either the non-negative number of entries in @c acl, or
256 * @c ACL_ERROR on error.
258 int acl_entry_count(acl_t acl
) {
262 int result
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &entry
);
264 while (result
== ACL_SUCCESS
) {
266 result
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
269 if (result
== ACL_ERROR
) {
270 perror("acl_entry_count (acl_get_entry)");
280 * @brief Determine whether or not the given ACL is minimal.
282 * An ACL is minimal if it has fewer than four entries.
285 * The ACL whose minimality is in question.
288 * - @c ACL_SUCCESS - @c acl is minimal
289 * - @c ACL_FAILURE - @c acl is not minimal
290 * - @c ACL_ERROR - Unexpected library error
292 int acl_is_minimal(acl_t acl
) {
294 int ec
= acl_entry_count(acl
);
296 if (ec
== ACL_ERROR
) {
297 perror("acl_is_minimal (acl_entry_count)");
312 * @brief Determine whether the given ACL's mask denies execute.
315 * The ACL whose mask we want to check.
318 * - @c ACL_SUCCESS - The @c acl has a mask which denies execute.
319 * - @c ACL_FAILURE - The @c acl has a mask which does not deny execute.
320 * - @c ACL_ERROR - Unexpected library error.
322 int acl_execute_masked(acl_t acl
) {
325 int ge_result
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &entry
);
327 while (ge_result
== ACL_SUCCESS
) {
328 acl_tag_t tag
= ACL_UNDEFINED_TAG
;
330 if (acl_get_tag_type(entry
, &tag
) == ACL_ERROR
) {
331 perror("acl_execute_masked (acl_get_tag_type)");
335 if (tag
== ACL_MASK
) {
336 /* This is the mask entry, get its permissions, and see if
337 execute is specified. */
338 acl_permset_t permset
;
340 if (acl_get_permset(entry
, &permset
) == ACL_ERROR
) {
341 perror("acl_execute_masked (acl_get_permset)");
345 int gp_result
= acl_get_perm(permset
, ACL_EXECUTE
);
346 if (gp_result
== ACL_ERROR
) {
347 perror("acl_execute_masked (acl_get_perm)");
351 if (gp_result
== ACL_FAILURE
) {
352 /* No execute bit set in the mask; execute not allowed. */
357 ge_result
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
366 * @brief Determine whether @c fd is executable by anyone.
369 * This is used as part of the heuristic to determine whether or not
370 * we should mask the execute bit when inheriting an ACL. If @c fd
371 * describes a file, we check the @a effective permissions, contrary
372 * to what setfacl does.
375 * The file descriptor to check.
378 * - @c ACL_SUCCESS - Someone has effective execute permissions on @c fd.
379 * - @c ACL_FAILURE - Nobody can execute @c fd.
380 * - @c ACL_ERROR - Unexpected library error.
382 int any_can_execute(int fd
) {
383 acl_t acl
= acl_get_fd(fd
);
385 if (acl
== (acl_t
)NULL
) {
386 perror("any_can_execute (acl_get_file)");
390 /* Our return value. */
391 int result
= ACL_FAILURE
;
393 if (acl_is_minimal(acl
)) {
395 if (fstat(fd
, &s
) == -1) {
396 perror("any_can_execute (fstat)");
400 if (s
.st_mode
& (S_IXUSR
| S_IXOTH
| S_IXGRP
)) {
401 result
= ACL_SUCCESS
;
405 result
= ACL_FAILURE
;
411 int ge_result
= acl_get_entry(acl
, ACL_FIRST_ENTRY
, &entry
);
413 while (ge_result
== ACL_SUCCESS
) {
414 /* The first thing we do is check to see if this is a mask
415 entry. If it is, we skip it entirely. */
416 acl_tag_t tag
= ACL_UNDEFINED_TAG
;
418 if (acl_get_tag_type(entry
, &tag
) == ACL_ERROR
) {
419 perror("any_can_execute_or (acl_get_tag_type)");
424 if (tag
== ACL_MASK
) {
425 ge_result
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
429 /* Ok, so it's not a mask entry. Check the execute perms. */
430 acl_permset_t permset
;
432 if (acl_get_permset(entry
, &permset
) == ACL_ERROR
) {
433 perror("any_can_execute_or (acl_get_permset)");
438 int gp_result
= acl_get_perm(permset
, ACL_EXECUTE
);
439 if (gp_result
== ACL_ERROR
) {
440 perror("any_can_execute (acl_get_perm)");
445 if (gp_result
== ACL_SUCCESS
) {
446 /* Only return ACL_SUCCESS if this execute bit is not masked. */
447 if (acl_execute_masked(acl
) != ACL_SUCCESS
) {
448 result
= ACL_SUCCESS
;
453 ge_result
= acl_get_entry(acl
, ACL_NEXT_ENTRY
, &entry
);
456 if (ge_result
== ACL_ERROR
) {
457 perror("any_can_execute (acl_get_entry)");
470 * @brief Set @c acl as the default ACL on @c path if it's a directory.
472 * This overwrites any existing default ACL on @c path. If no default
473 * ACL exists, then one is created. If @c path is not a directory, we
474 * return ACL_FAILURE but no error is raised.
477 * The target directory whose ACL we wish to replace or create.
480 * The ACL to set as default on @c path.
483 * - @c ACL_SUCCESS - The default ACL was assigned successfully.
484 * - @c ACL_FAILURE - If @c path is not a directory.
485 * - @c ACL_ERROR - Unexpected library error.
487 int assign_default_acl(const char* path
, acl_t acl
) {
494 if (!is_path_directory(path
)) {
498 /* Our return value; success unless something bad happens. */
499 int result
= ACL_SUCCESS
;
500 acl_t path_acl
= acl_dup(acl
);
502 if (path_acl
== (acl_t
)NULL
) {
503 perror("assign_default_acl (acl_dup)");
504 return ACL_ERROR
; /* Nothing to clean up in this case. */
507 if (acl_set_file(path
, ACL_TYPE_DEFAULT
, path_acl
) == ACL_ERROR
) {
508 perror("assign_default_acl (acl_set_file)");
519 * @brief Remove all @c ACL_TYPE_ACCESS entries from the given file
520 * descriptor, leaving the UNIX permission bits.
523 * The file descriptor whose ACLs we want to wipe.
526 * - @c ACL_SUCCESS - The ACLs were wiped successfully, or none
527 * existed in the first place.
528 * - @c ACL_ERROR - Unexpected library error.
530 int wipe_acls(int fd
) {
531 /* Initialize an empty ACL, and then overwrite the one on "fd" with it. */
532 acl_t empty_acl
= acl_init(0);
534 if (empty_acl
== (acl_t
)NULL
) {
535 perror("wipe_acls (acl_init)");
539 if (acl_set_fd(fd
, empty_acl
) == ACL_ERROR
) {
540 perror("wipe_acls (acl_set_fd)");
552 * @brief Apply parent default ACL to a path.
554 * This overwrites any existing ACLs on @c path.
557 * The path whose ACL we would like to reset to its default.
559 * @param no_exec_mask
560 * The value (either true or false) of the --no-exec-mask flag.
563 * - @c ACL_SUCCESS - The parent default ACL was inherited successfully.
564 * - @c ACL_FAILURE - The target path is not a regular file/directory,
565 * or the parent of @c path is not a directory.
566 * - @c ACL_ERROR - Unexpected library error.
568 int apply_default_acl(const char* path
, bool no_exec_mask
) {
572 perror("apply_default_acl (args)");
576 /* Define these next three variables here because we may have to
577 * jump to the cleanup routine which expects them to exist.
580 /* Our return value. */
581 int result
= ACL_SUCCESS
;
583 /* The default ACL on path's parent directory */
584 acl_t defacl
= (acl_t
)NULL
;
586 /* The file descriptor corresponding to "path" */
589 /* Split "path" into base/dirname parts to be used with openat().
590 * We duplicate the strings involved because dirname/basename mangle
593 char* path_copy
= strdup(path
);
594 if (path_copy
== NULL
) {
595 perror("apply_default_acl (strdup)");
598 char* parent
= dirname(path_copy
);
600 fd
= open(path
, O_NOFOLLOW
);
602 if (errno
== ELOOP
) {
603 result
= ACL_FAILURE
; /* hit a symlink */
607 perror("apply_default_acl (open fd)");
614 /* Refuse to operate on hard links, which can be abused by an
615 * attacker to trick us into changing the ACL on a file we didn't
616 * intend to; namely the "target" of the hard link. There is TOCTOU
617 * race condition here, but the window is as small as possible
618 * between when we open the file descriptor (look above) and when we
622 if (fstat(fd
, &s
) == -1) {
623 perror("apply_default_acl (fstat)");
626 if (!S_ISDIR(s
.st_mode
)) {
627 /* If it's not a directory, make sure it's a regular,
628 non-hard-linked file. */
629 if (!S_ISREG(s
.st_mode
) || s
.st_nlink
!= 1) {
630 result
= ACL_FAILURE
;
636 /* Default to not masking the exec bit; i.e. applying the default
637 ACL literally. If --no-exec-mask was not specified, then we try
638 to "guess" whether or not to mask the exec bit. This behavior
639 is modeled after the capital 'X' perms of setfacl. */
640 bool allow_exec
= true;
643 /* Never mask the execute bit on directories. */
644 int ace_result
= any_can_execute(fd
) || is_directory(fd
);
646 if (ace_result
== ACL_ERROR
) {
647 perror("apply_default_acl (any_can_execute)");
652 allow_exec
= (bool)ace_result
;
655 defacl
= acl_get_file(parent
, ACL_TYPE_DEFAULT
);
657 if (defacl
== (acl_t
)NULL
) {
658 perror("apply_default_acl (acl_get_file)");
663 if (wipe_acls(fd
) == ACL_ERROR
) {
664 perror("apply_default_acl (wipe_acls)");
669 /* Do this after wipe_acls(), otherwise we'll overwrite the wiped
670 ACL with this one. */
671 acl_t acl
= acl_get_fd(fd
);
672 if (acl
== (acl_t
)NULL
) {
673 perror("apply_default_acl (acl_get_fd)");
678 /* If it's a directory, inherit the parent's default. */
679 if (assign_default_acl(path
, defacl
) == ACL_ERROR
) {
680 perror("apply_default_acl (assign_default_acl)");
686 int ge_result
= acl_get_entry(defacl
, ACL_FIRST_ENTRY
, &entry
);
688 while (ge_result
== ACL_SUCCESS
) {
689 acl_tag_t tag
= ACL_UNDEFINED_TAG
;
691 if (acl_get_tag_type(entry
, &tag
) == ACL_ERROR
) {
692 perror("apply_default_acl (acl_get_tag_type)");
698 /* We've got an entry/tag from the default ACL. Get its permset. */
699 acl_permset_t permset
;
700 if (acl_get_permset(entry
, &permset
) == ACL_ERROR
) {
701 perror("apply_default_acl (acl_get_permset)");
706 /* If this is a default mask, fix it up. */
707 if (tag
== ACL_MASK
||
708 tag
== ACL_USER_OBJ
||
709 tag
== ACL_GROUP_OBJ
||
713 /* The mask doesn't affect acl_user_obj, acl_group_obj (in
714 minimal ACLs) or acl_other entries, so if execute should be
715 masked, we have to do it manually. */
716 if (acl_delete_perm(permset
, ACL_EXECUTE
) == ACL_ERROR
) {
717 perror("apply_default_acl (acl_delete_perm)");
722 if (acl_set_permset(entry
, permset
) == ACL_ERROR
) {
723 perror("apply_default_acl (acl_set_permset)");
730 /* Finally, add the permset to the access ACL. It's actually
731 * important that we pass in the address of "acl" here, and not
732 * "acl" itself. Why? The call to acl_create_entry() within
733 * acl_set_entry() can allocate new memory for the entry.
734 * Sometimes that can be done in-place, in which case everything
735 * is cool and the new memory gets released when we call
738 * But occasionally, the whole ACL structure will have to be moved
739 * in order to allocate the extra space. When that happens,
740 * acl_create_entry() modifies the pointer it was passed (in this
741 * case, &acl) to point to the new location. We want to call
742 * acl_free() on the new location, and since acl_free() gets
743 * called right here, we need acl_create_entry() to update the
744 * value of "acl". To do that, it needs the address of "acl".
746 if (acl_set_entry(&acl
, entry
) == ACL_ERROR
) {
747 perror("apply_default_acl (acl_set_entry)");
752 ge_result
= acl_get_entry(defacl
, ACL_NEXT_ENTRY
, &entry
);
755 /* Catches the first acl_get_entry as well as the ones at the end of
757 if (ge_result
== ACL_ERROR
) {
758 perror("apply_default_acl (acl_get_entry)");
763 if (acl_set_fd(fd
, acl
) == ACL_ERROR
) {
764 perror("apply_default_acl (acl_set_fd)");
771 if (defacl
!= (acl_t
)NULL
) {
774 if (fd
>= 0 && close(fd
) == -1) {
775 perror("apply_default_acl (close)");
784 * @brief Display program usage information.
786 * @param program_name
787 * The program name to use in the output.
790 void usage(const char* program_name
) {
791 printf("Apply any applicable default ACLs to the given files or "
793 printf("Usage: %s [flags] <target1> [<target2> [ <target3>...]]\n\n",
796 printf(" -h, --help Print this help message\n");
797 printf(" -r, --recursive Act on any given directories recursively\n");
798 printf(" -x, --no-exec-mask Apply execute permissions unconditionally\n");
805 * @brief Wrapper around @c apply_default_acl() for use with @c nftw().
807 * For parameter information, see the @c nftw man page.
809 * @return If the ACL was applied to @c target successfully, we return
810 * @c FTW_CONTINUE to signal to @ nftw() that we should proceed onto
811 * the next file or directory. Otherwise, we return @c FTW_STOP to
815 int apply_default_acl_nftw(const char *target
,
816 const struct stat
*s
,
820 if (apply_default_acl(target
, false)) {
831 * @brief Wrapper around @c apply_default_acl() for use with @c nftw().
833 * This is identical to @c apply_default_acl_nftw(), except it passes
834 * @c true to @c apply_default_acl() as its no_exec_mask argument.
837 int apply_default_acl_nftw_x(const char *target
,
838 const struct stat
*s
,
842 if (apply_default_acl(target
, true)) {
853 * @brief Recursive version of @c apply_default_acl().
855 * If @c target is a directory, we use @c nftw() to call @c
856 * apply_default_acl() recursively on all of its children. Otherwise,
857 * we just delegate to @c apply_default_acl().
859 * We ignore symlinks for consistency with chmod -r.
862 * The root (path) of the recursive application.
864 * @param no_exec_mask
865 * The value (either true or false) of the --no-exec-mask flag.
868 * If @c target is not a directory, we return the result of
869 * calling @c apply_default_acl() on @c target. Otherwise, we convert
870 * the return value of @c nftw(). If @c nftw() succeeds (returns 0),
871 * then we return @c true. Otherwise, we return @c false.
873 * If there is an error, it will be reported via @c perror, but
874 * we still return @c false.
876 bool apply_default_acl_recursive(const char *target
, bool no_exec_mask
) {
878 if (!is_path_directory(target
)) {
879 return apply_default_acl(target
, no_exec_mask
);
882 int max_levels
= 256;
883 int flags
= FTW_PHYS
; /* Don't follow links. */
885 /* There are two separate functions that could be passed to
886 nftw(). One passes no_exec_mask = true to apply_default_acl(),
887 and the other passes no_exec_mask = false. Since the function we
888 pass to nftw() cannot have parameters, we have to create separate
889 options and make the decision here. */
890 int (*fn
)(const char *, const struct stat
*, int, struct FTW
*) = NULL
;
891 fn
= no_exec_mask
? apply_default_acl_nftw_x
: apply_default_acl_nftw
;
893 int nftw_result
= nftw(target
, fn
, max_levels
, flags
);
895 if (nftw_result
== 0) {
900 /* nftw will return -1 on error, or if the supplied function
901 * (apply_default_acl_nftw) returns a non-zero result, nftw will
904 if (nftw_result
== -1) {
905 perror("apply_default_acl_recursive (nftw)");
914 * @brief Call apply_default_acl (possibly recursively) on each
915 * command-line argument.
917 * @return Either @c EXIT_FAILURE or @c EXIT_SUCCESS. If everything
918 * goes as expected, we return @c EXIT_SUCCESS. Otherwise, we return
921 int main(int argc
, char* argv
[]) {
928 bool recursive
= false;
929 bool no_exec_mask
= false;
931 struct option long_options
[] = {
932 /* These options set a flag. */
933 {"help", no_argument
, NULL
, 'h'},
934 {"recursive", no_argument
, NULL
, 'r'},
935 {"no-exec-mask", no_argument
, NULL
, 'x'},
941 while ((opt
= getopt_long(argc
, argv
, "hrx", long_options
, NULL
)) != -1) {
958 int result
= EXIT_SUCCESS
;
961 for (arg_index
= optind
; arg_index
< argc
; arg_index
++) {
962 const char* target
= argv
[arg_index
];
963 bool reapp_result
= false;
965 /* Make sure we can access the given path before we go out of our
966 * way to please it. Doing this check outside of
967 * apply_default_acl() lets us spit out a better error message for
970 if (!path_accessible(target
)) {
971 fprintf(stderr
, "%s: %s: No such file or directory\n", argv
[0], target
);
972 result
= EXIT_FAILURE
;
977 reapp_result
= apply_default_acl_recursive(target
, no_exec_mask
);
980 /* It's either a normal file, or we're not operating recursively. */
981 reapp_result
= apply_default_acl(target
, no_exec_mask
);
985 result
= EXIT_FAILURE
;