]> gitweb.michael.orlitzky.com - apply-default-acl.git/blob - src/libadacl.c
Replace all tabs with spaces. We're not animals.
[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 else 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 || strlen(pathname) == 0 || pathname[0] == '\0') {
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 Remove all @c ACL_TYPE_ACCESS entries from the given file
556 * descriptor, leaving the UNIX permission bits.
557 *
558 * @param fd
559 * The file descriptor whose ACLs we want to wipe.
560 *
561 * @return
562 * - @c ACL_SUCCESS - The ACLs were wiped successfully, or none
563 * existed in the first place.
564 * - @c ACL_ERROR - Unexpected library error.
565 */
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);
569
570 if (empty_acl == (acl_t)NULL) {
571 perror("wipe_acls (acl_init)");
572 return ACL_ERROR;
573 }
574
575 if (acl_set_fd(fd, empty_acl) == ACL_ERROR) {
576 perror("wipe_acls (acl_set_fd)");
577 acl_free(empty_acl);
578 return ACL_ERROR;
579 }
580
581 acl_free(empty_acl);
582 return ACL_SUCCESS;
583 }
584
585
586 /**
587 * @brief Copy ACLs between file descriptors as xattrs, verbatim.
588 *
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.
593 *
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
599 * does.
600 *
601 * @param src_fd
602 * The file descriptor from which the ACL will be copied.
603 *
604 * @param src_type
605 * The type of ACL (either @c ACL_TYPE_ACCESS or @c ACL_TYPE_DEFAULT)
606 * to copy from @c src_fd.
607 *
608 * @param dst_fd
609 * The file descriptor whose ACL will be overwritten with the one
610 * from @c src_fd.
611 *
612 * @param dst_type
613 * The type of ACL (either @c ACL_TYPE_ACCESS or @c ACL_TYPE_DEFAULT)
614 * to replace on @c dst_fd.
615 *
616 * @return
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.
620 */
621 int acl_copy_xattr(int src_fd,
622 acl_type_t src_type,
623 int dst_fd,
624 acl_type_t dst_type) {
625
626 const char* src_name;
627 if (src_type == ACL_TYPE_ACCESS) {
628 src_name = XATTR_NAME_POSIX_ACL_ACCESS;
629 }
630 else if (src_type == ACL_TYPE_DEFAULT) {
631 src_name = XATTR_NAME_POSIX_ACL_DEFAULT;
632 }
633 else {
634 errno = EINVAL;
635 perror("acl_copy_xattr (src type)");
636 return ACL_ERROR;
637 }
638
639 const char* dst_name;
640 if (dst_type == ACL_TYPE_ACCESS) {
641 dst_name = XATTR_NAME_POSIX_ACL_ACCESS;
642 }
643 else if (dst_type == ACL_TYPE_DEFAULT) {
644 dst_name = XATTR_NAME_POSIX_ACL_DEFAULT;
645 }
646 else {
647 errno = EINVAL;
648 perror("acl_copy_xattr (dst type)");
649 return ACL_ERROR;
650 }
651
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. */
658 return ACL_FAILURE;
659 }
660 perror("acl_copy_xattr (fgetxattr size guess)");
661 return ACL_ERROR;
662 }
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. */
669 return ACL_FAILURE;
670 }
671 perror("acl_copy_xattr (fgetxattr)");
672 return ACL_ERROR;
673 }
674
675 if (fsetxattr(dst_fd, dst_name, src_acl_p, src_size, 0) == XATTR_ERROR) {
676 perror("acl_copy_xattr (fsetxattr)");
677 return ACL_ERROR;
678 }
679
680 return ACL_SUCCESS;
681 }
682
683
684 /**
685 * @brief Apply parent default ACL to a path.
686 *
687 * This overwrites any existing ACLs on @c path.
688 *
689 * @param path
690 * The path whose ACL we would like to reset to its default.
691 *
692 * @param sp
693 * A pointer to a stat structure for @c path, or @c NULL if you don't
694 * have one handy.
695 *
696 * @param no_exec_mask
697 * The value (either true or false) of the --no-exec-mask flag.
698 *
699 * @return
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.
703 */
704 int apply_default_acl_ex(const char* path,
705 const struct stat* sp,
706 bool no_exec_mask) {
707
708 if (path == NULL) {
709 errno = EINVAL;
710 perror("apply_default_acl_ex (args)");
711 return ACL_ERROR;
712 }
713
714 /* Define these next three variables here because we may have to
715 * jump to the cleanup routine which expects them to exist.
716 */
717
718 /* Our return value. */
719 int result = ACL_SUCCESS;
720
721 /* The new ACL for this path */
722 acl_t new_acl = (acl_t)NULL;
723
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;
727
728 /* The file descriptor corresponding to "path" */
729 int fd = 0;
730
731 /* The file descriptor for the directory containing "path" */
732 int parent_fd = 0;
733
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)");
739 return ACL_ERROR;
740 }
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;
748 goto cleanup;
749 }
750 else {
751 perror("apply_default_acl_ex (open parent fd)");
752 result = ACL_ERROR;
753 goto cleanup;
754 }
755 }
756
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;
763 goto cleanup;
764 }
765 else {
766 perror("apply_default_acl_ex (open fd)");
767 result = ACL_ERROR;
768 goto cleanup;
769 }
770 }
771
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
777 * fstat it.
778 *
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).
781 */
782 if (sp == NULL) {
783 struct stat s;
784 if (fstat(fd, &s) == STAT_ERROR) {
785 perror("apply_default_acl_ex (fstat)");
786 goto cleanup;
787 }
788
789 sp = &s;
790 }
791
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;
797 goto cleanup;
798 }
799 }
800
801
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;
807
808 if (!no_exec_mask) {
809 /* Never mask the execute bit on directories. */
810 int ace_result = any_can_execute(fd,sp) || S_ISDIR(sp->st_mode);
811
812 if (ace_result == ACL_ERROR) {
813 perror("apply_default_acl_ex (any_can_execute)");
814 result = ACL_ERROR;
815 goto cleanup;
816 }
817
818 allow_exec = (bool)ace_result;
819 }
820
821 if (wipe_acls(fd) == ACL_ERROR) {
822 perror("apply_default_acl_ex (wipe_acls)");
823 result = ACL_ERROR;
824 goto cleanup;
825 }
826
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,
830 ACL_TYPE_DEFAULT,
831 fd,
832 ACL_TYPE_DEFAULT) == ACL_ERROR) {
833 perror("apply_default_acl_ex (acl_copy_xattr default)");
834 result = ACL_ERROR;
835 goto cleanup;
836 }
837 }
838
839 /* If it's anything, _apply_ the parent's default. */
840 if (acl_copy_xattr(parent_fd,
841 ACL_TYPE_DEFAULT,
842 fd,
843 ACL_TYPE_ACCESS) == ACL_ERROR) {
844 perror("apply_default_acl_ex (acl_copy_xattr access)");
845 result = ACL_ERROR;
846 goto cleanup;
847 }
848
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.
858 */
859
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)");
865 result = ACL_ERROR;
866 goto cleanup;
867 }
868
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)");
875 result = ACL_ERROR;
876 goto cleanup;
877 }
878
879 acl_entry_t entry;
880 int ge_result = acl_get_entry(new_acl_unmasked, ACL_FIRST_ENTRY, &entry);
881
882 while (ge_result == ACL_SUCCESS) {
883 acl_tag_t tag = ACL_UNDEFINED_TAG;
884
885 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
886 perror("apply_default_acl_ex (acl_get_tag_type)");
887 result = ACL_ERROR;
888 goto cleanup;
889 }
890
891
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)");
896 result = ACL_ERROR;
897 goto cleanup;
898 }
899
900 if (tag == ACL_MASK ||
901 tag == ACL_USER_OBJ ||
902 tag == ACL_GROUP_OBJ ||
903 tag == ACL_OTHER) {
904
905 if (!allow_exec) {
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)");
911 result = ACL_ERROR;
912 goto cleanup;
913 }
914
915 if (acl_set_permset(entry, permset) == ACL_ERROR) {
916 perror("apply_default_acl_ex (acl_set_permset)");
917 result = ACL_ERROR;
918 goto cleanup;
919 }
920 }
921 }
922
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
929 * acl_free(acl).
930 *
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".
938 */
939
940 if (acl_set_entry(&new_acl, entry) == ACL_ERROR) {
941 perror("apply_default_acl_ex (acl_set_entry)");
942 result = ACL_ERROR;
943 goto cleanup;
944 }
945
946 ge_result = acl_get_entry(new_acl_unmasked, ACL_NEXT_ENTRY, &entry);
947 }
948
949 /* Catches the first acl_get_entry as well as the ones at the end of
950 the loop. */
951 if (ge_result == ACL_ERROR) {
952 perror("apply_default_acl_ex (acl_get_entry)");
953 result = ACL_ERROR;
954 goto cleanup;
955 }
956
957 if (acl_set_fd(fd, new_acl) == ACL_ERROR) {
958 perror("apply_default_acl_ex (acl_set_fd)");
959 result = ACL_ERROR;
960 goto cleanup;
961 }
962
963 cleanup:
964 free(path_copy);
965 if (new_acl != (acl_t)NULL) {
966 acl_free(new_acl);
967 }
968 if (new_acl_unmasked != (acl_t)NULL) {
969 acl_free(new_acl_unmasked);
970 }
971 if (fd >= 0 && close(fd) == CLOSE_ERROR) {
972 perror("apply_default_acl_ex (close fd)");
973 result = ACL_ERROR;
974 }
975 if (parent_fd >= 0 && close(parent_fd) == CLOSE_ERROR) {
976 perror("apply_default_acl_ex (close parent_fd)");
977 result = ACL_ERROR;
978 }
979 return result;
980 }
981
982
983
984 /**
985 * @brief The friendly interface to @c apply_default_acl_ex.
986 *
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.
991 *
992 * If you don't have a stat structure for your @c path, use this instead.
993 *
994 * @param path
995 * The path whose ACL we would like to reset to its default.
996 *
997 * @param no_exec_mask
998 * The value (either true or false) of the --no-exec-mask flag.
999 *
1000 * @return
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.
1005 */
1006 int apply_default_acl(const char* path, bool no_exec_mask) {
1007 return apply_default_acl_ex(path, NULL, no_exec_mask);
1008 }