]> gitweb.michael.orlitzky.com - apply-default-acl.git/blob - src/libadacl.c
Eliminate the wipe_acls() function that is apparently not needed.
[apply-default-acl.git] / src / libadacl.c
1 /**
2 * @file libadacl.c
3 *
4 * @brief The adacl (apply default acl) shared library.
5 *
6 */
7
8 /* Enables get_current_dir_name() in unistd.h and the O_PATH flag. */
9 #define _GNU_SOURCE
10
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() */
22
23 /* ACLs */
24 #include <acl/libacl.h> /* acl_get_perm, not portable */
25 #include <sys/acl.h> /* all other acl_foo functions */
26
27 /* XATTR_NAME_POSIX_ACL_ACCESS and XATTR_NAME_POSIX_ACL_DEFAULT */
28 #include <linux/xattr.h>
29
30 #include "libadacl.h"
31
32
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.
35 */
36 #define CLOSE_ERROR -1
37 #define OPEN_ERROR -1
38 #define SNPRINTF_ERROR -1
39 #define STAT_ERROR -1
40 #define XATTR_ERROR -1
41
42
43 /**
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.
47 *
48 * @param at_fd
49 * A file descriptor relative to which @c pathname will be opened.
50 *
51 * @param pathname
52 * The path to the file/directory/whatever whose descriptor you want.
53 *
54 * @param flags
55 * File status flags to be passed to @c openat.
56 *
57 * @return a file descriptor for @c pathname if everything goes well,
58 * and @c OPEN_ERROR if not.
59 */
60 int safe_open_ex(int at_fd, char* pathname, int flags) {
61 if (pathname == NULL) {
62 errno = EINVAL;
63 perror("safe_open_ex (args)");
64 return OPEN_ERROR;
65 }
66
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);
71 }
72 if (firstslash[1] == '\0') {
73 /* The first slash is the last character; ensure that we open
74 a directory. */
75 firstslash[0] = '\0';
76 return openat(at_fd, pathname, flags | O_DIRECTORY);
77 }
78
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
81 recurse. */
82 *firstslash = '\0';
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)");
88 }
89 return OPEN_ERROR;
90 }
91
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)");
97 return OPEN_ERROR;
98 }
99 return result;
100 }
101
102
103 /**
104 * @brief A version of @c open that is completely symlink-safe when
105 * used with the @c O_NOFOLLOW flag.
106 *
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.
112 *
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.
119 *
120 * @param pathname
121 * The path to the file/directory/whatever whose descriptor you want.
122 *
123 * @param flags
124 * File status flags to be passed to @c openat.
125 *
126 * @return a file descriptor for @c pathname if everything goes well,
127 * and @c OPEN_ERROR if not.
128 */
129 int safe_open(const char* pathname, int flags) {
130 if (pathname == NULL) {
131 errno = EINVAL;
132 perror("safe_open (args)");
133 return OPEN_ERROR;
134 }
135
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);
141 }
142 else {
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
147 * in the pathname.
148 *
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.
152 */
153 char* cwd = get_current_dir_name();
154 if (cwd == NULL) {
155 perror("safe_open (get_current_dir_name)");
156 return OPEN_ERROR;
157 }
158
159 char abs_cwd[PATH_MAX];
160 if (realpath(cwd, abs_cwd) == NULL) {
161 perror("safe_open (realpath)");
162 free(cwd);
163 return OPEN_ERROR;
164 }
165 snprintf_result = snprintf(abspath, PATH_MAX, "%s/%s", abs_cwd, pathname);
166 free(cwd);
167 }
168 if (snprintf_result == SNPRINTF_ERROR || snprintf_result > PATH_MAX) {
169 perror("safe_open (snprintf)");
170 return OPEN_ERROR;
171 }
172
173 int fd = 0;
174 if (strcmp(abspath, "/") == 0) {
175 fd = open("/", flags | O_DIRECTORY);
176 }
177 else {
178 /* Use O_PATH for some added safety if "/" is not our target */
179 fd = open("/", flags | O_DIRECTORY | O_PATH);
180 }
181 if (fd == OPEN_ERROR) {
182 perror("safe_open (open)");
183 return OPEN_ERROR;
184 }
185
186 if (strcmp(abspath, "/") == 0) {
187 return fd;
188 }
189
190 int result = safe_open_ex(fd, abspath+1, flags);
191 if (close(fd) == CLOSE_ERROR) {
192 perror("safe_open (close)");
193 return OPEN_ERROR;
194 }
195 return result;
196 }
197
198
199
200
201 /**
202 * @brief Update (or create) an entry in an @b minimal ACL.
203 *
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.
207 *
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.
213 *
214 * @param aclp
215 * A pointer to the acl_t structure whose entry we want to modify.
216 *
217 * @param entry
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.
220 *
221 * @return If there is an unexpected library error, @c ACL_ERROR is
222 * returned. Otherwise, @c ACL_SUCCESS.
223 *
224 */
225 int acl_set_entry(acl_t* aclp, acl_entry_t entry) {
226 if (aclp == NULL || entry == NULL) {
227 errno = EINVAL;
228 perror("acl_set_entry (args)");
229 return ACL_ERROR;
230 }
231
232 acl_tag_t entry_tag;
233 if (acl_get_tag_type(entry, &entry_tag) == ACL_ERROR) {
234 perror("acl_set_entry (acl_get_tag_type)");
235 return ACL_ERROR;
236 }
237
238 acl_permset_t entry_permset;
239 if (acl_get_permset(entry, &entry_permset) == ACL_ERROR) {
240 perror("acl_set_entry (acl_get_permset)");
241 return ACL_ERROR;
242 }
243
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);
247
248 while (result == ACL_SUCCESS) {
249 acl_tag_t existing_tag = ACL_UNDEFINED_TAG;
250
251 if (acl_get_tag_type(existing_entry, &existing_tag) == ACL_ERROR) {
252 perror("set_acl_tag_permset (acl_get_tag_type)");
253 return ACL_ERROR;
254 }
255
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)");
260 return ACL_ERROR;
261 }
262
263 return ACL_SUCCESS;
264 }
265
266 result = acl_get_entry(*aclp, ACL_NEXT_ENTRY, &existing_entry);
267 }
268
269 /* This catches both the initial acl_get_entry and the ones at the
270 end of the loop. */
271 if (result == ACL_ERROR) {
272 perror("acl_set_entry (acl_get_entry)");
273 return ACL_ERROR;
274 }
275
276 /* If we've made it this far, we need to add a new entry to the
277 ACL. */
278 acl_entry_t new_entry;
279
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.
286 */
287 if (acl_create_entry(aclp, &new_entry) == ACL_ERROR) {
288 perror("acl_set_entry (acl_create_entry)");
289 return ACL_ERROR;
290 }
291
292 if (acl_set_tag_type(new_entry, entry_tag) == ACL_ERROR) {
293 perror("acl_set_entry (acl_set_tag_type)");
294 return ACL_ERROR;
295 }
296
297 if (acl_set_permset(new_entry, entry_permset) == ACL_ERROR) {
298 perror("acl_set_entry (acl_set_permset)");
299 return ACL_ERROR;
300 }
301
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)");
307 return ACL_ERROR;
308 }
309
310 if (acl_set_qualifier(new_entry, entry_qual) == ACL_ERROR) {
311 perror("acl_set_entry (acl_set_qualifier)");
312 return ACL_ERROR;
313 }
314 }
315
316 return ACL_SUCCESS;
317 }
318
319
320
321 /**
322 * @brief Determine the number of entries in the given ACL.
323 *
324 * @param acl
325 * The ACL to inspect.
326 *
327 * @return Either the non-negative number of entries in @c acl, or
328 * @c ACL_ERROR on error.
329 */
330 int acl_entry_count(acl_t acl) {
331
332 acl_entry_t entry;
333 int entry_count = 0;
334 int result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
335
336 while (result == ACL_SUCCESS) {
337 entry_count++;
338 result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
339 }
340
341 if (result == ACL_ERROR) {
342 perror("acl_entry_count (acl_get_entry)");
343 return ACL_ERROR;
344 }
345
346 return entry_count;
347 }
348
349
350
351 /**
352 * @brief Determine whether or not the given ACL is minimal.
353 *
354 * An ACL is minimal if it has fewer than four entries.
355 *
356 * @param acl
357 * The ACL whose minimality is in question.
358 *
359 * @return
360 * - @c ACL_SUCCESS - @c acl is minimal
361 * - @c ACL_FAILURE - @c acl is not minimal
362 * - @c ACL_ERROR - Unexpected library error
363 */
364 int acl_is_minimal(acl_t acl) {
365 if (acl == NULL) {
366 errno = EINVAL;
367 perror("acl_is_minimal (args)");
368 return ACL_ERROR;
369 }
370
371 int ec = acl_entry_count(acl);
372
373 if (ec == ACL_ERROR) {
374 perror("acl_is_minimal (acl_entry_count)");
375 return ACL_ERROR;
376 }
377
378 if (ec < 4) {
379 return ACL_SUCCESS;
380 }
381 else {
382 return ACL_FAILURE;
383 }
384 }
385
386
387
388 /**
389 * @brief Determine whether the given ACL's mask denies execute.
390 *
391 * @param acl
392 * The ACL whose mask we want to check.
393 *
394 * @return
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.
398 */
399 int acl_execute_masked(acl_t acl) {
400 if (acl == NULL) {
401 errno = EINVAL;
402 perror("acl_execute_masked (args)");
403 return ACL_ERROR;
404 }
405
406 acl_entry_t entry;
407 int ge_result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
408
409 while (ge_result == ACL_SUCCESS) {
410 acl_tag_t tag = ACL_UNDEFINED_TAG;
411
412 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
413 perror("acl_execute_masked (acl_get_tag_type)");
414 return ACL_ERROR;
415 }
416
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;
421
422 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
423 perror("acl_execute_masked (acl_get_permset)");
424 return ACL_ERROR;
425 }
426
427 int gp_result = acl_get_perm(permset, ACL_EXECUTE);
428 if (gp_result == ACL_ERROR) {
429 perror("acl_execute_masked (acl_get_perm)");
430 return ACL_ERROR;
431 }
432
433 if (gp_result == ACL_FAILURE) {
434 /* No execute bit set in the mask; execute not allowed. */
435 return ACL_SUCCESS;
436 }
437 }
438
439 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
440 }
441
442 return ACL_FAILURE;
443 }
444
445
446
447 /**
448 * @brief Determine whether @c fd is executable by anyone.
449 *
450 *
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.
455 *
456 * @param fd
457 * The file descriptor to check.
458 *
459 * @param sp
460 * A pointer to a stat structure for @c fd.
461 *
462 * @return
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.
466 */
467 int any_can_execute(int fd, const struct stat* sp) {
468 if (sp == NULL) {
469 errno = EINVAL;
470 perror("any_can_execute (args)");
471 return ACL_ERROR;
472 }
473
474 acl_t acl = acl_get_fd(fd);
475
476 if (acl == (acl_t)NULL) {
477 perror("any_can_execute (acl_get_fd)");
478 return ACL_ERROR;
479 }
480
481 /* Our return value. */
482 int result = ACL_FAILURE;
483
484 if (acl_is_minimal(acl)) {
485 if (sp->st_mode & (S_IXUSR | S_IXOTH | S_IXGRP)) {
486 result = ACL_SUCCESS;
487 goto cleanup;
488 }
489 else {
490 result = ACL_FAILURE;
491 goto cleanup;
492 }
493 }
494
495 acl_entry_t entry;
496 int ge_result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
497
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;
502
503 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
504 perror("any_can_execute_or (acl_get_tag_type)");
505 result = ACL_ERROR;
506 goto cleanup;
507 }
508
509 if (tag == ACL_MASK) {
510 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
511 continue;
512 }
513
514 /* Ok, so it's not a mask entry. Check the execute perms. */
515 acl_permset_t permset;
516
517 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
518 perror("any_can_execute_or (acl_get_permset)");
519 result = ACL_ERROR;
520 goto cleanup;
521 }
522
523 int gp_result = acl_get_perm(permset, ACL_EXECUTE);
524 if (gp_result == ACL_ERROR) {
525 perror("any_can_execute (acl_get_perm)");
526 result = ACL_ERROR;
527 goto cleanup;
528 }
529
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;
534 goto cleanup;
535 }
536 }
537
538 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
539 }
540
541 if (ge_result == ACL_ERROR) {
542 perror("any_can_execute (acl_get_entry)");
543 result = ACL_ERROR;
544 goto cleanup;
545 }
546
547 cleanup:
548 acl_free(acl);
549 return result;
550 }
551
552
553
554 /**
555 * @brief Copy ACLs between file descriptors as xattrs, verbatim.
556 *
557 * There is a small deficiency in libacl, namely that there is no way
558 * to get or set default ACLs through file descriptors. The @c
559 * acl_get_file and @c acl_set_file functions can do it, but they use
560 * paths, and are vulnerable to symlink attacks.
561 *
562 * Fortunately, when inheriting an ACL, we don't really need to look
563 * at what it contains. That means that we can copy the on-disk xattrs
564 * from the source directory to the destination file/directory without
565 * passing through libacl, and this can be done with file descriptors
566 * through @c fgetxattr and @c fsetxattr. That's what this function
567 * does.
568 *
569 * @param src_fd
570 * The file descriptor from which the ACL will be copied.
571 *
572 * @param src_type
573 * The type of ACL (either @c ACL_TYPE_ACCESS or @c ACL_TYPE_DEFAULT)
574 * to copy from @c src_fd.
575 *
576 * @param dst_fd
577 * The file descriptor whose ACL will be overwritten with the one
578 * from @c src_fd.
579 *
580 * @param dst_type
581 * The type of ACL (either @c ACL_TYPE_ACCESS or @c ACL_TYPE_DEFAULT)
582 * to replace on @c dst_fd.
583 *
584 * @return
585 * - @c ACL_SUCCESS - The ACL was copied successfully.
586 * - @c ACL_FAILURE - There was no ACL on @c src_fd.
587 * - @c ACL_ERROR - Unexpected library error.
588 */
589 int acl_copy_xattr(int src_fd,
590 acl_type_t src_type,
591 int dst_fd,
592 acl_type_t dst_type) {
593
594 const char* src_name;
595 if (src_type == ACL_TYPE_ACCESS) {
596 src_name = XATTR_NAME_POSIX_ACL_ACCESS;
597 }
598 else if (src_type == ACL_TYPE_DEFAULT) {
599 src_name = XATTR_NAME_POSIX_ACL_DEFAULT;
600 }
601 else {
602 errno = EINVAL;
603 perror("acl_copy_xattr (src type)");
604 return ACL_ERROR;
605 }
606
607 const char* dst_name;
608 if (dst_type == ACL_TYPE_ACCESS) {
609 dst_name = XATTR_NAME_POSIX_ACL_ACCESS;
610 }
611 else if (dst_type == ACL_TYPE_DEFAULT) {
612 dst_name = XATTR_NAME_POSIX_ACL_DEFAULT;
613 }
614 else {
615 errno = EINVAL;
616 perror("acl_copy_xattr (dst type)");
617 return ACL_ERROR;
618 }
619
620 size_t src_size_guess = fgetxattr(src_fd, src_name, NULL, 0);
621 if (src_size_guess == XATTR_ERROR) {
622 if (errno == ENODATA) {
623 /* A missing ACL isn't really an error. ENOATTR and ENODATA are
624 synonyms, but using ENODATA here lets us avoid another
625 "include" directive. */
626 return ACL_FAILURE;
627 }
628 perror("acl_copy_xattr (fgetxattr size guess)");
629 return ACL_ERROR;
630 }
631 char* src_acl_p = alloca(src_size_guess);
632 /* The actual size may be smaller than our guess? I don't know. */
633 size_t src_size = fgetxattr(src_fd, src_name, src_acl_p, (int)src_size_guess);
634 if (src_size == XATTR_ERROR) {
635 if (errno == ENODATA) {
636 /* A missing ACL isn't an error. */
637 return ACL_FAILURE;
638 }
639 perror("acl_copy_xattr (fgetxattr)");
640 return ACL_ERROR;
641 }
642
643 if (fsetxattr(dst_fd, dst_name, src_acl_p, src_size, 0) == XATTR_ERROR) {
644 perror("acl_copy_xattr (fsetxattr)");
645 return ACL_ERROR;
646 }
647
648 return ACL_SUCCESS;
649 }
650
651
652 /**
653 * @brief Determine if a file descriptor has a default ACL.
654 *
655 * @param fd
656 * The file descriptor whose default ACL is in question.
657 *
658 * @return
659 * - @c ACL_SUCCESS - If @c fd has a default ACL.
660 * - @c ACL_FAILURE - If @c fd does not have a default ACL.
661 * - @c ACL_ERROR - Unexpected library error.
662 */
663 int has_default_acl_fd(int fd) {
664 if (fgetxattr(fd, XATTR_NAME_POSIX_ACL_DEFAULT, NULL, 0) == XATTR_ERROR) {
665 if (errno == ENODATA) {
666 return ACL_FAILURE;
667 }
668 perror("has_default_acl_fd (fgetxattr)");
669 return ACL_ERROR;
670 }
671
672 return ACL_SUCCESS;
673 }
674
675
676 /**
677 * @brief Apply parent default ACL to a path.
678 *
679 * This overwrites any existing ACLs on @c path.
680 *
681 * @param path
682 * The path whose ACL we would like to reset to its default.
683 *
684 * @param sp
685 * A pointer to a stat structure for @c path, or @c NULL if you don't
686 * have one handy.
687 *
688 * @param no_exec_mask
689 * The value (either true or false) of the --no-exec-mask flag.
690 *
691 * @return
692 * - @c ACL_SUCCESS - The parent default ACL was inherited successfully.
693 * - @c ACL_FAILURE - If symlinks or hard links are encountered.
694 * - @c ACL_ERROR - Unexpected library error.
695 */
696 int apply_default_acl_ex(const char* path,
697 const struct stat* sp,
698 bool no_exec_mask) {
699
700 if (path == NULL) {
701 errno = EINVAL;
702 perror("apply_default_acl_ex (args)");
703 return ACL_ERROR;
704 }
705
706 /* Define these next three variables here because we may have to
707 * jump to the cleanup routine which expects them to exist.
708 */
709
710 /* Our return value. */
711 int result = ACL_SUCCESS;
712
713 /* The new ACL for this path */
714 acl_t new_acl = (acl_t)NULL;
715
716 /* A copy of new_acl, to be made before we begin mangling new_acl in
717 order to mask the execute bit. */
718 acl_t new_acl_unmasked = (acl_t)NULL;
719
720 /* The file descriptor corresponding to "path" */
721 int fd = 0;
722
723 /* The file descriptor for the directory containing "path" */
724 int parent_fd = 0;
725
726 /* Get the parent directory of "path" with dirname(), which happens
727 * to murder its argument and necessitates a path_copy. */
728 char* path_copy = strdup(path);
729 if (path_copy == NULL) {
730 perror("apply_default_acl_ex (strdup)");
731 return ACL_ERROR;
732 }
733 char* parent = dirname(path_copy);
734 parent_fd = safe_open(parent, O_DIRECTORY | O_NOFOLLOW);
735 if (parent_fd == OPEN_ERROR) {
736 if (errno == ELOOP || errno == ENOTDIR) {
737 /* We hit a symlink, either in the last path component (ELOOP)
738 or higher up (ENOTDIR). */
739 result = ACL_FAILURE;
740 goto cleanup;
741 }
742 else {
743 perror("apply_default_acl_ex (open parent fd)");
744 result = ACL_ERROR;
745 goto cleanup;
746 }
747 }
748
749 /* Check to make sure the parent descriptor actually has a default
750 ACL. If it doesn't, then we can "succeed" immediately. */
751 if (has_default_acl_fd(parent_fd) == ACL_FAILURE) {
752 result = ACL_SUCCESS;
753 goto cleanup;
754 }
755
756 fd = safe_open(path, O_NOFOLLOW);
757 if (fd == OPEN_ERROR) {
758 if (errno == ELOOP || errno == ENOTDIR) {
759 /* We hit a symlink, either in the last path component (ELOOP)
760 or higher up (ENOTDIR). */
761 result = ACL_FAILURE;
762 goto cleanup;
763 }
764 else {
765 perror("apply_default_acl_ex (open fd)");
766 result = ACL_ERROR;
767 goto cleanup;
768 }
769 }
770
771 /* Refuse to operate on hard links, which can be abused by an
772 * attacker to trick us into changing the ACL on a file we didn't
773 * intend to; namely the "target" of the hard link. There is TOCTOU
774 * race condition here, but the window is as small as possible
775 * between when we open the file descriptor (look above) and when we
776 * fstat it.
777 *
778 * Note: we only need to call fstat ourselves if we weren't passed a
779 * valid pointer to a stat structure (nftw does that).
780 */
781 if (sp == NULL) {
782 struct stat s;
783 if (fstat(fd, &s) == STAT_ERROR) {
784 perror("apply_default_acl_ex (fstat)");
785 goto cleanup;
786 }
787
788 sp = &s;
789 }
790
791 if (!S_ISDIR(sp->st_mode)) {
792 /* If it's not a directory, make sure it's a regular,
793 non-hard-linked file. */
794 if (!S_ISREG(sp->st_mode) || sp->st_nlink != 1) {
795 result = ACL_FAILURE;
796 goto cleanup;
797 }
798 }
799
800
801 /* Default to not masking the exec bit; i.e. applying the default
802 ACL literally. If --no-exec-mask was not specified, then we try
803 to "guess" whether or not to mask the exec bit. This behavior
804 is modeled after the capital 'X' perms of setfacl. */
805 bool allow_exec = true;
806
807 if (!no_exec_mask) {
808 /* Never mask the execute bit on directories. */
809 int ace_result = any_can_execute(fd,sp) || S_ISDIR(sp->st_mode);
810
811 if (ace_result == ACL_ERROR) {
812 perror("apply_default_acl_ex (any_can_execute)");
813 result = ACL_ERROR;
814 goto cleanup;
815 }
816
817 allow_exec = (bool)ace_result;
818 }
819
820 /* If it's a directory, inherit the parent's default. */
821 if (S_ISDIR(sp->st_mode)) {
822 if (acl_copy_xattr(parent_fd,
823 ACL_TYPE_DEFAULT,
824 fd,
825 ACL_TYPE_DEFAULT) == ACL_ERROR) {
826 perror("apply_default_acl_ex (acl_copy_xattr default)");
827 result = ACL_ERROR;
828 goto cleanup;
829 }
830 }
831
832 /* If it's anything, _apply_ the parent's default. */
833 if (acl_copy_xattr(parent_fd,
834 ACL_TYPE_DEFAULT,
835 fd,
836 ACL_TYPE_ACCESS) == ACL_ERROR) {
837 perror("apply_default_acl_ex (acl_copy_xattr access)");
838 result = ACL_ERROR;
839 goto cleanup;
840 }
841
842 /* There's a good reason why we saved the ACL above, even though
843 * we're about tto read it back into memory and mess with it on the
844 * next line. The acl_copy_xattr() function is already a hack to let
845 * us copy default ACLs without resorting to path names; we simply
846 * have no way to read the parent's default ACL into memory using
847 * parent_fd. We can, however, copy the parent's ACL to a file (with
848 * acl_copy_xattr), and then read the ACL from a file using
849 * "fd". It's quite the circus, but it works and should be safe from
850 * sym/hardlink attacks.
851 */
852
853 /* Now we potentially need to mask the execute permissions in the
854 ACL on fd; or maybe now. */
855 if (allow_exec) {
856 goto cleanup;
857 }
858
859 /* OK, we need to mask some execute permissions. First obtain the
860 current ACL... */
861 new_acl = acl_get_fd(fd);
862 if (new_acl == (acl_t)NULL) {
863 perror("apply_default_acl_ex (acl_get_fd)");
864 result = ACL_ERROR;
865 goto cleanup;
866 }
867
868 /* ...and now make a copy of it, because otherwise when we loop
869 below, some shit gets stuck (modifying the structure while
870 looping over it no worky). */
871 new_acl_unmasked = acl_dup(new_acl);
872 if (new_acl_unmasked == (acl_t)NULL) {
873 perror("apply_default_acl_ex (acl_dup)");
874 result = ACL_ERROR;
875 goto cleanup;
876 }
877
878 acl_entry_t entry;
879 int ge_result = acl_get_entry(new_acl_unmasked, ACL_FIRST_ENTRY, &entry);
880
881 while (ge_result == ACL_SUCCESS) {
882 acl_tag_t tag = ACL_UNDEFINED_TAG;
883
884 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
885 perror("apply_default_acl_ex (acl_get_tag_type)");
886 result = ACL_ERROR;
887 goto cleanup;
888 }
889
890
891 /* We've got an entry/tag from the default ACL. Get its permset. */
892 acl_permset_t permset;
893 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
894 perror("apply_default_acl_ex (acl_get_permset)");
895 result = ACL_ERROR;
896 goto cleanup;
897 }
898
899 if (tag == ACL_MASK ||
900 tag == ACL_USER_OBJ ||
901 tag == ACL_GROUP_OBJ ||
902 tag == ACL_OTHER) {
903
904 /* The mask doesn't affect acl_user_obj, acl_group_obj (in
905 minimal ACLs) or acl_other entries, so if execute should be
906 masked, we have to do it manually. */
907 if (acl_delete_perm(permset, ACL_EXECUTE) == ACL_ERROR) {
908 perror("apply_default_acl_ex (acl_delete_perm)");
909 result = ACL_ERROR;
910 goto cleanup;
911 }
912
913 if (acl_set_permset(entry, permset) == ACL_ERROR) {
914 perror("apply_default_acl_ex (acl_set_permset)");
915 result = ACL_ERROR;
916 goto cleanup;
917 }
918 }
919
920 /* Finally, add the permset to the access ACL. It's actually
921 * important that we pass in the address of "new_acl" here, and not
922 * "new_acl" itself. Why? The call to acl_create_entry() within
923 * acl_set_entry() can allocate new memory for the entry.
924 * Sometimes that can be done in-place, in which case everything
925 * is cool and the new memory gets released when we call
926 * acl_free(acl).
927 *
928 * But occasionally, the whole ACL structure will have to be moved
929 * in order to allocate the extra space. When that happens,
930 * acl_create_entry() modifies the pointer it was passed (in this
931 * case, &acl) to point to the new location. We want to call
932 * acl_free() on the new location, and since acl_free() gets
933 * called right here, we need acl_create_entry() to update the
934 * value of "new_acl". To do that, it needs the address of "new_acl".
935 */
936
937 if (acl_set_entry(&new_acl, entry) == ACL_ERROR) {
938 perror("apply_default_acl_ex (acl_set_entry)");
939 result = ACL_ERROR;
940 goto cleanup;
941 }
942
943 ge_result = acl_get_entry(new_acl_unmasked, ACL_NEXT_ENTRY, &entry);
944 }
945
946 /* Catches the first acl_get_entry as well as the ones at the end of
947 the loop. */
948 if (ge_result == ACL_ERROR) {
949 perror("apply_default_acl_ex (acl_get_entry)");
950 result = ACL_ERROR;
951 goto cleanup;
952 }
953
954 if (acl_set_fd(fd, new_acl) == ACL_ERROR) {
955 perror("apply_default_acl_ex (acl_set_fd)");
956 result = ACL_ERROR;
957 goto cleanup;
958 }
959
960 cleanup:
961 free(path_copy);
962 if (new_acl != (acl_t)NULL) {
963 acl_free(new_acl);
964 }
965 if (new_acl_unmasked != (acl_t)NULL) {
966 acl_free(new_acl_unmasked);
967 }
968 if (fd > 0 && close(fd) == CLOSE_ERROR) {
969 perror("apply_default_acl_ex (close fd)");
970 result = ACL_ERROR;
971 }
972 if (parent_fd > 0 && close(parent_fd) == CLOSE_ERROR) {
973 perror("apply_default_acl_ex (close parent_fd)");
974 result = ACL_ERROR;
975 }
976 return result;
977 }
978
979
980
981 /**
982 * @brief The friendly interface to @c apply_default_acl_ex.
983 *
984 * The @c apply_default_acl_ex function holds the real implementation
985 * of this function, but it takes a weird second argument that most
986 * people won't care about (a stat structure). But, we use that
987 * argument for the recursive mode of the CLI, so it's there.
988 *
989 * If you don't have a stat structure for your @c path, use this instead.
990 *
991 * @param path
992 * The path whose ACL we would like to reset to its default.
993 *
994 * @param no_exec_mask
995 * The value (either true or false) of the --no-exec-mask flag.
996 *
997 * @return
998 * - @c ACL_SUCCESS - The parent default ACL was inherited successfully.
999 * - @c ACL_FAILURE - If symlinks or hard links are encountered.
1000 * or the parent of @c path is not a directory.
1001 * - @c ACL_ERROR - Unexpected library error.
1002 */
1003 int apply_default_acl(const char* path, bool no_exec_mask) {
1004 return apply_default_acl_ex(path, NULL, no_exec_mask);
1005 }