]> gitweb.michael.orlitzky.com - apply-default-acl.git/blob - src/libadacl.c
stc/libadacl.c: use a "cleanup" routine in acl_update_entry().
[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, the O_PATH flag, and
9 * the asprintf() function.
10 */
11 #define _GNU_SOURCE
12
13 #include <dirent.h> /* readdir(), etc. */
14 #include <errno.h> /* EINVAL, ELOOP, ENOTDIR, etc. */
15 #include <fcntl.h> /* openat() */
16 #include <libgen.h> /* basename(), dirname() */
17 #include <stdbool.h> /* the "bool" type */
18 #include <stdio.h> /* perror(), asprintf() */
19 #include <stdlib.h> /* free() */
20 #include <string.h> /* strdup() */
21 #include <sys/stat.h> /* fstat() */
22 #include <sys/xattr.h> /* fgetxattr(), fsetxattr() */
23 #include <unistd.h> /* get_current_dir_name() */
24
25 /* ACLs */
26 #include <acl/libacl.h> /* acl_get_perm, not portable */
27 #include <sys/acl.h> /* all other acl_foo functions */
28
29 /* XATTR_NAME_POSIX_ACL_ACCESS and XATTR_NAME_POSIX_ACL_DEFAULT */
30 #include <linux/xattr.h>
31
32 #include "libadacl.h"
33
34
35 /* Even though most other library functions reliably return -1 for
36 * error, it feels a little wrong to re-use the ACL_ERROR constant.
37 */
38 #define CLOSE_ERROR -1
39 #define OPEN_ERROR -1
40 #define ASPRINTF_ERROR -1
41 #define STAT_ERROR -1
42 #define XATTR_ERROR -1
43
44
45 /* Prototypes */
46 int safe_open_ex(int at_fd, char* pathname, int flags);
47 int safe_open(const char* pathname, int flags);
48 int acl_update_entry(acl_t aclp, acl_entry_t entry);
49 int acl_entry_count(acl_t acl);
50 int acl_is_minimal(acl_t acl);
51 int acl_execute_masked(acl_t acl);
52 int any_can_execute(int fd, const struct stat* sp);
53 int acl_copy_xattr(int src_fd,
54 acl_type_t src_type,
55 int dst_fd,
56 acl_type_t dst_type);
57 int has_default_acl_fd(int fd);
58 int apply_default_acl_fds(int parent_fd, int fd, bool recursive);
59 int apply_default_acl(const char* path, bool recursive);
60
61
62
63 /**
64 * @brief The recursive portion of the @c safe_open function, used to
65 * open a file descriptor in a symlink-safe way when combined with
66 * the @c O_NOFOLLOW flag.
67 *
68 * @param at_fd
69 * A file descriptor relative to which @c pathname will be opened.
70 *
71 * @param pathname
72 * The path to the file/directory/whatever whose descriptor you want.
73 *
74 * @param flags
75 * File status flags to be passed to @c openat.
76 *
77 * @return a file descriptor for @c pathname if everything goes well,
78 * and @c OPEN_ERROR if not.
79 */
80 int safe_open_ex(int at_fd, char* pathname, int flags) {
81 if (pathname == NULL) {
82 errno = EINVAL;
83 perror("safe_open_ex (args)");
84 return OPEN_ERROR;
85 }
86
87 char* firstslash = strchr(pathname, '/');
88 if (firstslash == NULL) {
89 /* No more slashes, this is the base case. */
90 return openat(at_fd, pathname, flags);
91 }
92 if (firstslash[1] == '\0') {
93 /* The first slash is the last character; ensure that we open
94 a directory. */
95 firstslash[0] = '\0';
96 return openat(at_fd, pathname, flags | O_DIRECTORY);
97 }
98
99 /* The first slash exists and isn't the last character in the path,
100 so we can split the path wherever that first slash lies and
101 recurse. */
102 *firstslash = '\0';
103 int fd = openat(at_fd, pathname, flags | O_DIRECTORY | O_PATH);
104 if (fd == OPEN_ERROR) {
105 if (errno != ENOTDIR) {
106 /* Don't output anything if we ignore a symlink */
107 perror("safe_open_ex (safe_open_ex)");
108 }
109 return OPEN_ERROR;
110 }
111
112 /* The +1 is safe because there needs to be at least one character
113 after the first slash (we checked this above). */
114 int result = safe_open_ex(fd, firstslash+1, flags);
115 if (close(fd) == CLOSE_ERROR) {
116 perror("safe_open_ex (close)");
117 return OPEN_ERROR;
118 }
119 return result;
120 }
121
122
123 /**
124 * @brief A version of @c open that is completely symlink-safe when
125 * used with the @c O_NOFOLLOW flag.
126 *
127 * The @c openat function exists to ensure that you can anchor one
128 * path to a particular directory while opening it; however, if you
129 * open "b/c/d" relative to "/a", then even the @c openat function will
130 * still follow symlinks in the "b" component. This can be exploited
131 * by an attacker to make you open the wrong path.
132 *
133 * To avoid that problem, this function uses a recursive
134 * implementation that opens every path from the root, one level at a
135 * time. So "a" is opened relative to "/", and then "b" is opened
136 * relative to "/a", and then "c" is opened relative to "/a/b",
137 * etc. When the @c O_NOFOLLOW flag is used, this approach ensures
138 * that no symlinks in any component are followed.
139 *
140 * @param pathname
141 * The path to the file/directory/whatever whose descriptor you want.
142 *
143 * @param flags
144 * File status flags to be passed to @c openat.
145 *
146 * @return a file descriptor for @c pathname if everything goes well,
147 * and @c OPEN_ERROR if not.
148 */
149 int safe_open(const char* pathname, int flags) {
150 if (pathname == NULL) {
151 errno = EINVAL;
152 perror("safe_open (args)");
153 return OPEN_ERROR;
154 }
155
156 char* abspath = NULL;
157 int asprintf_result = 0;
158 if (strchr(pathname, '/') == pathname) {
159 /* pathname is already absolute; just copy it. */
160 asprintf_result = asprintf(&abspath, "%s", pathname);
161 }
162 else {
163 /* Concatenate the current working directory and pathname into an
164 * absolute path. We use realpath() ONLY on the cwd part, and not
165 * on the pathname part, because realpath() resolves symlinks. And
166 * the whole point of all this crap is to avoid following symlinks
167 * in the pathname.
168 *
169 * Using realpath() on the cwd lets us operate on relative paths
170 * while we're sitting in a directory that happens to have a
171 * symlink in it; for example: cd /var/run && apply-default-acl foo.
172 */
173 char* cwd = get_current_dir_name();
174 if (cwd == NULL) {
175 perror("safe_open (get_current_dir_name)");
176 return OPEN_ERROR;
177 }
178
179 char abs_cwd[PATH_MAX];
180 if (realpath(cwd, abs_cwd) == NULL) {
181 perror("safe_open (realpath)");
182 free(cwd);
183 return OPEN_ERROR;
184 }
185 asprintf_result = asprintf(&abspath, "%s/%s", abs_cwd, pathname);
186 free(cwd);
187 }
188 if (asprintf_result == ASPRINTF_ERROR) {
189 perror("safe_open (asprintf)");
190 return OPEN_ERROR;
191 }
192
193 /* Beyond here, asprintf() worked, and we need to free abspath. */
194 int result = OPEN_ERROR;
195
196 bool abspath_is_root = (strcmp(abspath, "/") == 0);
197 int rootflags = flags | O_DIRECTORY;
198 if (!abspath_is_root) {
199 /* Use O_PATH for some added safety if "/" is not our target */
200 rootflags |= O_PATH;
201 }
202 int rootfd = open("/", rootflags);
203 if (rootfd == OPEN_ERROR) {
204 perror("safe_open (open)");
205 result = OPEN_ERROR;
206 goto cleanup;
207 }
208
209 if (abspath_is_root) {
210 result = rootfd;
211 goto cleanup;
212 }
213
214 result = safe_open_ex(rootfd, abspath+1, flags);
215 if (close(rootfd) == CLOSE_ERROR) {
216 perror("safe_open (close)");
217 result = OPEN_ERROR;
218 goto cleanup;
219 }
220
221 cleanup:
222 free(abspath);
223 return result;
224 }
225
226
227
228
229 /**
230 * @brief Update an entry in an @b minimal ACL.
231 *
232 * @param aclp
233 * A pointer to the acl_t structure whose entry we want to update.
234 *
235 * @param entry
236 * The new entry.
237 *
238 * @return
239 * - @c ACL_SUCCESS - If we update an existing entry.
240 * - @c ACL_FAILURE - If we don't find an entry to update.
241 * - @c ACL_ERROR - Unexpected library error.
242 */
243 int acl_update_entry(acl_t aclp, acl_entry_t entry) {
244 if (aclp == NULL || entry == NULL) {
245 errno = EINVAL;
246 perror("acl_update_entry (args)");
247 return ACL_ERROR;
248 }
249
250 acl_tag_t entry_tag;
251 if (acl_get_tag_type(entry, &entry_tag) == ACL_ERROR) {
252 perror("acl_update_entry (acl_get_tag_type)");
253 return ACL_ERROR;
254 }
255
256 acl_permset_t entry_permset;
257 if (acl_get_permset(entry, &entry_permset) == ACL_ERROR) {
258 perror("acl_update_entry (acl_get_permset)");
259 return ACL_ERROR;
260 }
261
262 /* Our return value. Default to failure, and change to success if we
263 actually update something. */
264 int result = ACL_FAILURE;
265
266 acl_entry_t existing_entry;
267 /* Loop through the given ACL looking for matching entries. */
268 int get_entry_result = acl_get_entry(aclp, ACL_FIRST_ENTRY, &existing_entry);
269
270 while (get_entry_result == ACL_SUCCESS) {
271 acl_tag_t existing_tag = ACL_UNDEFINED_TAG;
272
273 if (acl_get_tag_type(existing_entry, &existing_tag) == ACL_ERROR) {
274 perror("set_acl_tag_permset (acl_get_tag_type)");
275 result = ACL_ERROR;
276 goto cleanup;
277 }
278
279 if (existing_tag == entry_tag) {
280 /* If we update something, we're done and return ACL_SUCCESS */
281 if (acl_set_permset(existing_entry, entry_permset) == ACL_ERROR) {
282 perror("acl_update_entry (acl_set_permset)");
283 result = ACL_ERROR;
284 goto cleanup;
285 }
286
287 result = ACL_SUCCESS;
288 goto cleanup;
289 }
290
291 get_entry_result = acl_get_entry(aclp, ACL_NEXT_ENTRY, &existing_entry);
292 }
293
294 /* This catches both the initial acl_get_entry and the ones at the
295 end of the loop. */
296 if (get_entry_result == ACL_ERROR) {
297 perror("acl_update_entry (acl_get_entry)");
298 result = ACL_ERROR;
299 }
300
301 cleanup:
302 return result;
303 }
304
305
306
307 /**
308 * @brief Determine the number of entries in the given ACL.
309 *
310 * @param acl
311 * The ACL to inspect.
312 *
313 * @return Either the non-negative number of entries in @c acl, or
314 * @c ACL_ERROR on error.
315 */
316 int acl_entry_count(acl_t acl) {
317
318 acl_entry_t entry;
319 int entry_count = 0;
320 int result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
321
322 while (result == ACL_SUCCESS) {
323 entry_count++;
324 result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
325 }
326
327 if (result == ACL_ERROR) {
328 perror("acl_entry_count (acl_get_entry)");
329 return ACL_ERROR;
330 }
331
332 return entry_count;
333 }
334
335
336
337 /**
338 * @brief Determine whether or not the given ACL is minimal.
339 *
340 * An ACL is minimal if it has fewer than four entries.
341 *
342 * @param acl
343 * The ACL whose minimality is in question.
344 *
345 * @return
346 * - @c ACL_SUCCESS - @c acl is minimal
347 * - @c ACL_FAILURE - @c acl is not minimal
348 * - @c ACL_ERROR - Unexpected library error
349 */
350 int acl_is_minimal(acl_t acl) {
351 if (acl == NULL) {
352 errno = EINVAL;
353 perror("acl_is_minimal (args)");
354 return ACL_ERROR;
355 }
356
357 int ec = acl_entry_count(acl);
358
359 if (ec == ACL_ERROR) {
360 perror("acl_is_minimal (acl_entry_count)");
361 return ACL_ERROR;
362 }
363
364 if (ec < 4) {
365 return ACL_SUCCESS;
366 }
367 else {
368 return ACL_FAILURE;
369 }
370 }
371
372
373
374 /**
375 * @brief Determine whether the given ACL's mask denies execute.
376 *
377 * @param acl
378 * The ACL whose mask we want to check.
379 *
380 * @return
381 * - @c ACL_SUCCESS - The @c acl has a mask which denies execute.
382 * - @c ACL_FAILURE - The @c acl has a mask which does not deny execute.
383 * - @c ACL_ERROR - Unexpected library error.
384 */
385 int acl_execute_masked(acl_t acl) {
386 if (acl == NULL) {
387 errno = EINVAL;
388 perror("acl_execute_masked (args)");
389 return ACL_ERROR;
390 }
391
392 acl_entry_t entry;
393 int ge_result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
394
395 while (ge_result == ACL_SUCCESS) {
396 acl_tag_t tag = ACL_UNDEFINED_TAG;
397
398 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
399 perror("acl_execute_masked (acl_get_tag_type)");
400 return ACL_ERROR;
401 }
402
403 if (tag == ACL_MASK) {
404 /* This is the mask entry, get its permissions, and see if
405 execute is specified. */
406 acl_permset_t permset;
407
408 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
409 perror("acl_execute_masked (acl_get_permset)");
410 return ACL_ERROR;
411 }
412
413 int gp_result = acl_get_perm(permset, ACL_EXECUTE);
414 if (gp_result == ACL_ERROR) {
415 perror("acl_execute_masked (acl_get_perm)");
416 return ACL_ERROR;
417 }
418
419 if (gp_result == ACL_FAILURE) {
420 /* No execute bit set in the mask; execute not allowed. */
421 return ACL_SUCCESS;
422 }
423 }
424
425 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
426 }
427
428 return ACL_FAILURE;
429 }
430
431
432
433 /**
434 * @brief Determine whether @c fd is executable by anyone.
435 *
436 *
437 * This is used as part of the heuristic to determine whether or not
438 * we should mask the execute bit when inheriting an ACL. If @c fd
439 * describes a file, we check the @a effective permissions, contrary
440 * to what setfacl does.
441 *
442 * @param fd
443 * The file descriptor to check.
444 *
445 * @param sp
446 * A pointer to a stat structure for @c fd.
447 *
448 * @return
449 * - @c ACL_SUCCESS - Someone has effective execute permissions on @c fd.
450 * - @c ACL_FAILURE - Nobody can execute @c fd.
451 * - @c ACL_ERROR - Unexpected library error.
452 */
453 int any_can_execute(int fd, const struct stat* sp) {
454 if (sp == NULL) {
455 errno = EINVAL;
456 perror("any_can_execute (args)");
457 return ACL_ERROR;
458 }
459
460 acl_t acl = acl_get_fd(fd);
461
462 if (acl == (acl_t)NULL) {
463 perror("any_can_execute (acl_get_fd)");
464 return ACL_ERROR;
465 }
466
467 /* Our return value. */
468 int result = ACL_FAILURE;
469
470 if (acl_is_minimal(acl)) {
471 if (sp->st_mode & (S_IXUSR | S_IXOTH | S_IXGRP)) {
472 result = ACL_SUCCESS;
473 goto cleanup;
474 }
475 else {
476 result = ACL_FAILURE;
477 goto cleanup;
478 }
479 }
480
481 acl_entry_t entry;
482 int ge_result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
483
484 while (ge_result == ACL_SUCCESS) {
485 /* The first thing we do is check to see if this is a mask
486 entry. If it is, we skip it entirely. */
487 acl_tag_t tag = ACL_UNDEFINED_TAG;
488
489 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
490 perror("any_can_execute_or (acl_get_tag_type)");
491 result = ACL_ERROR;
492 goto cleanup;
493 }
494
495 if (tag == ACL_MASK) {
496 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
497 continue;
498 }
499
500 /* Ok, so it's not a mask entry. Check the execute perms. */
501 acl_permset_t permset;
502
503 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
504 perror("any_can_execute_or (acl_get_permset)");
505 result = ACL_ERROR;
506 goto cleanup;
507 }
508
509 int gp_result = acl_get_perm(permset, ACL_EXECUTE);
510 if (gp_result == ACL_ERROR) {
511 perror("any_can_execute (acl_get_perm)");
512 result = ACL_ERROR;
513 goto cleanup;
514 }
515
516 if (gp_result == ACL_SUCCESS) {
517 /* Only return ACL_SUCCESS if this execute bit is not masked. */
518 if (acl_execute_masked(acl) != ACL_SUCCESS) {
519 result = ACL_SUCCESS;
520 goto cleanup;
521 }
522 }
523
524 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
525 }
526
527 if (ge_result == ACL_ERROR) {
528 perror("any_can_execute (acl_get_entry)");
529 result = ACL_ERROR;
530 goto cleanup;
531 }
532
533 cleanup:
534 acl_free(acl);
535 return result;
536 }
537
538
539
540 /**
541 * @brief Copy ACLs between file descriptors as xattrs, verbatim.
542 *
543 * There is a small deficiency in libacl, namely that there is no way
544 * to get or set default ACLs through file descriptors. The @c
545 * acl_get_file and @c acl_set_file functions can do it, but they use
546 * paths, and are vulnerable to symlink attacks.
547 *
548 * Fortunately, when inheriting an ACL, we don't really need to look
549 * at what it contains. That means that we can copy the on-disk xattrs
550 * from the source directory to the destination file/directory without
551 * passing through libacl, and this can be done with file descriptors
552 * through @c fgetxattr and @c fsetxattr. That's what this function
553 * does.
554 *
555 * @param src_fd
556 * The file descriptor from which the ACL will be copied.
557 *
558 * @param src_type
559 * The type of ACL (either @c ACL_TYPE_ACCESS or @c ACL_TYPE_DEFAULT)
560 * to copy from @c src_fd.
561 *
562 * @param dst_fd
563 * The file descriptor whose ACL will be overwritten with the one
564 * from @c src_fd.
565 *
566 * @param dst_type
567 * The type of ACL (either @c ACL_TYPE_ACCESS or @c ACL_TYPE_DEFAULT)
568 * to replace on @c dst_fd.
569 *
570 * @return
571 * - @c ACL_SUCCESS - The ACL was copied successfully.
572 * - @c ACL_FAILURE - There was no ACL on @c src_fd.
573 * - @c ACL_ERROR - Unexpected library error.
574 */
575 int acl_copy_xattr(int src_fd,
576 acl_type_t src_type,
577 int dst_fd,
578 acl_type_t dst_type) {
579
580 const char* src_name;
581 if (src_type == ACL_TYPE_ACCESS) {
582 src_name = XATTR_NAME_POSIX_ACL_ACCESS;
583 }
584 else if (src_type == ACL_TYPE_DEFAULT) {
585 src_name = XATTR_NAME_POSIX_ACL_DEFAULT;
586 }
587 else {
588 errno = EINVAL;
589 perror("acl_copy_xattr (src type)");
590 return ACL_ERROR;
591 }
592
593 const char* dst_name;
594 if (dst_type == ACL_TYPE_ACCESS) {
595 dst_name = XATTR_NAME_POSIX_ACL_ACCESS;
596 }
597 else if (dst_type == ACL_TYPE_DEFAULT) {
598 dst_name = XATTR_NAME_POSIX_ACL_DEFAULT;
599 }
600 else {
601 errno = EINVAL;
602 perror("acl_copy_xattr (dst type)");
603 return ACL_ERROR;
604 }
605
606 ssize_t src_size_guess = fgetxattr(src_fd, src_name, NULL, 0);
607 if (src_size_guess == XATTR_ERROR) {
608 if (errno == ENODATA) {
609 /* A missing ACL isn't really an error. ENOATTR and ENODATA are
610 synonyms, but using ENODATA here lets us avoid another
611 "include" directive. */
612 return ACL_FAILURE;
613 }
614 perror("acl_copy_xattr (fgetxattr size guess)");
615 return ACL_ERROR;
616 }
617 char* src_acl_p = alloca(src_size_guess);
618 /* The actual size may be smaller than our guess? I don't know. The
619 return value from fgetxattr() will either be nonnegative, or
620 XATTR_ERROR (which we've already ruled out), so it's safe to cast
621 it to an unsigned size_t here to avoid a compiler warning. */
622 ssize_t src_size = fgetxattr(src_fd,
623 src_name,
624 src_acl_p,
625 (size_t)src_size_guess);
626 if (src_size == XATTR_ERROR) {
627 if (errno == ENODATA) {
628 /* A missing ACL isn't an error. */
629 return ACL_FAILURE;
630 }
631 perror("acl_copy_xattr (fgetxattr)");
632 return ACL_ERROR;
633 }
634
635 /* See above: src_size must be nonnegative at this point,so we cast
636 it to size_t to avoid a compiler warning. */
637 if (fsetxattr(dst_fd,
638 dst_name,
639 src_acl_p,
640 (size_t)src_size,
641 0)
642 == XATTR_ERROR) {
643 perror("acl_copy_xattr (fsetxattr)");
644 return ACL_ERROR;
645 }
646
647 return ACL_SUCCESS;
648 }
649
650
651 /**
652 * @brief Determine if a file descriptor has a default ACL.
653 *
654 * @param fd
655 * The file descriptor whose default ACL is in question.
656 *
657 * @return
658 * - @c ACL_SUCCESS - If @c fd has a default ACL.
659 * - @c ACL_FAILURE - If @c fd does not have a default ACL.
660 * - @c ACL_ERROR - Unexpected library error.
661 */
662 int has_default_acl_fd(int fd) {
663 if (fgetxattr(fd, XATTR_NAME_POSIX_ACL_DEFAULT, NULL, 0) == XATTR_ERROR) {
664 if (errno == ENODATA) {
665 return ACL_FAILURE;
666 }
667 perror("has_default_acl_fd (fgetxattr)");
668 return ACL_ERROR;
669 }
670
671 return ACL_SUCCESS;
672 }
673
674
675
676 /**
677 * @brief The recursive portion of @c apply_default_acl.
678 *
679 * The @c apply_default_acl function takes a path, but then opens file
680 * descriptors for the path and its parent. Afterwards, everything is
681 * done using file descriptors, including the recursive application on
682 * the path's children. This function encapsulates the portion of @c
683 * apply_default_acl that uses only file descriptors; for the
684 * recursion, this function ultimately calls itself.
685 *
686 * This overwrites any existing ACLs on @c fd and, if @c recursive is
687 * @c true, its children. When @c recursive is @c true, the "worst"
688 * result encountered is returned as the overall result.
689 *
690 * @param parent_fd
691 * A file descriptor for the parent directory of @c fd.
692 *
693 * @param fd
694 * The file descriptor that should inherit its parent's default ACL.
695 *
696 * @param recursive
697 * Should we recurse into subdirectories?
698 *
699 * @return
700 * - @c ACL_SUCCESS - The parent default ACLs were 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_fds(int parent_fd, int fd, bool recursive) {
705 int result = ACL_SUCCESS;
706
707 /* The new ACL for this path */
708 acl_t new_acl = (acl_t)NULL;
709
710 /* A copy of new_acl, to be made before we begin mangling new_acl in
711 order to mask the execute bit. */
712 acl_t new_acl_unmasked = (acl_t)NULL;
713
714 /* Refuse to operate on hard links, which can be abused by an
715 * attacker to trick us into changing the ACL on a file we didn't
716 * intend to; namely the "target" of the hard link. There is TOCTOU
717 * race condition here, but the window is as small as possible
718 * between when we open the file descriptor (look above) and when we
719 * fstat it.
720 */
721 struct stat s;
722 if (fstat(fd, &s) == STAT_ERROR) {
723 perror("apply_default_acl_fds (fstat)");
724 /* We can't recurse without the stat struct for fd */
725 goto cleanup;
726 }
727
728
729 /* Check to make sure the parent descriptor actually has a default
730 ACL. If it doesn't, then we can "succeed" immediately, saving a
731 little work, particularly in any_can_execute(). Note that we
732 can't skip the fstat() above, because we need it in case we
733 recurse. */
734 if (has_default_acl_fd(parent_fd) == ACL_FAILURE) {
735 result = ACL_SUCCESS;
736 /* Just because this target can't inherit anything doesn't mean
737 that one of it's children can't. For example, if there's a
738 default on "c" in "a/b/c/d", then we don't want to skip all
739 children of "a"! */
740 goto recurse;
741 }
742
743
744 if (!S_ISDIR(s.st_mode)) {
745 /* If it's not a directory, make sure it's a regular,
746 non-hard-linked file. */
747 if (!S_ISREG(s.st_mode) || s.st_nlink != 1) {
748 result = ACL_FAILURE;
749 goto cleanup; /* It's not a directory, so we can skip the recursion. */
750 }
751 }
752
753
754 /* Next We try to guess whether or not to strip the execute bits.
755 * This behavior is modeled after the capital 'X' perms of setfacl.
756 */
757 int ace_result = any_can_execute(fd, &s);
758
759 if (ace_result == ACL_ERROR) {
760 perror("apply_default_acl_fds (any_can_execute)");
761 result = ACL_ERROR;
762 goto cleanup;
763 }
764
765 /* Never mask the execute bit on directories. */
766 bool allow_exec = (bool)ace_result || S_ISDIR(s.st_mode);
767
768
769 /* If it's a directory, inherit the parent's default. */
770 if (S_ISDIR(s.st_mode)) {
771 if (acl_copy_xattr(parent_fd,
772 ACL_TYPE_DEFAULT,
773 fd,
774 ACL_TYPE_DEFAULT) == ACL_ERROR) {
775 perror("apply_default_acl_fds (acl_copy_xattr default)");
776 result = ACL_ERROR;
777 goto cleanup;
778 }
779 }
780
781 /* If it's anything, _apply_ the parent's default. */
782 if (acl_copy_xattr(parent_fd,
783 ACL_TYPE_DEFAULT,
784 fd,
785 ACL_TYPE_ACCESS) == ACL_ERROR) {
786 perror("apply_default_acl_fds (acl_copy_xattr access)");
787 result = ACL_ERROR;
788 goto cleanup;
789 }
790
791 /* There's a good reason why we saved the ACL above, even though
792 * we're about to read it back into memory and mess with it on the
793 * next line. The acl_copy_xattr() function is already a hack to let
794 * us copy default ACLs without resorting to path names; we simply
795 * have no way to read the parent's default ACL into memory using
796 * parent_fd. We can, however, copy the parent's ACL to a file (with
797 * acl_copy_xattr), and then read the ACL from a file using
798 * "fd". It's quite the circus, but it works and should be safe from
799 * sym/hardlink attacks.
800 */
801
802 /* Now we potentially need to mask the execute permissions in the
803 ACL on fd; or maybe not. */
804 if (allow_exec) {
805 /* Skip the mask code for this target, but don't skip its children! */
806 goto recurse;
807 }
808
809 /* OK, we need to mask some execute permissions. First obtain the
810 current ACL... */
811 new_acl = acl_get_fd(fd);
812 if (new_acl == (acl_t)NULL) {
813 perror("apply_default_acl_fds (acl_get_fd)");
814 result = ACL_ERROR;
815 goto cleanup;
816 }
817
818 /* ...and now make a copy of it, because otherwise when we loop
819 below, some shit gets stuck (modifying the structure while
820 looping over it no worky). */
821 new_acl_unmasked = acl_dup(new_acl);
822 if (new_acl_unmasked == (acl_t)NULL) {
823 perror("apply_default_acl_fds (acl_dup)");
824 result = ACL_ERROR;
825 goto cleanup;
826 }
827
828 acl_entry_t entry;
829 int ge_result = acl_get_entry(new_acl_unmasked, ACL_FIRST_ENTRY, &entry);
830
831 while (ge_result == ACL_SUCCESS) {
832 acl_tag_t tag = ACL_UNDEFINED_TAG;
833
834 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
835 perror("apply_default_acl_fds (acl_get_tag_type)");
836 result = ACL_ERROR;
837 goto cleanup;
838 }
839
840
841 /* We've got an entry/tag from the default ACL. Get its permset. */
842 acl_permset_t permset;
843 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
844 perror("apply_default_acl_fds (acl_get_permset)");
845 result = ACL_ERROR;
846 goto cleanup;
847 }
848
849 /* To mimic what the kernel does, I think we could drop
850 ACL_GROUP_OBJ from the list below? */
851 if (tag == ACL_MASK ||
852 tag == ACL_USER_OBJ ||
853 tag == ACL_GROUP_OBJ ||
854 tag == ACL_OTHER) {
855
856 /* The mask doesn't affect acl_user_obj, acl_group_obj (in
857 minimal ACLs) or acl_other entries, so if execute should be
858 masked, we have to do it manually. */
859 if (acl_delete_perm(permset, ACL_EXECUTE) == ACL_ERROR) {
860 perror("apply_default_acl_fds (acl_delete_perm)");
861 result = ACL_ERROR;
862 goto cleanup;
863 }
864
865 if (acl_set_permset(entry, permset) == ACL_ERROR) {
866 perror("apply_default_acl_fds (acl_set_permset)");
867 result = ACL_ERROR;
868 goto cleanup;
869 }
870 }
871
872 if (acl_update_entry(new_acl, entry) == ACL_ERROR) {
873 perror("apply_default_acl_fds (acl_update_entry)");
874 result = ACL_ERROR;
875 goto cleanup;
876 }
877
878 ge_result = acl_get_entry(new_acl_unmasked, ACL_NEXT_ENTRY, &entry);
879 }
880
881 /* Catches the first acl_get_entry as well as the ones at the end of
882 the loop. */
883 if (ge_result == ACL_ERROR) {
884 perror("apply_default_acl_fds (acl_get_entry)");
885 result = ACL_ERROR;
886 goto cleanup;
887 }
888
889 if (acl_set_fd(fd, new_acl) == ACL_ERROR) {
890 perror("apply_default_acl_fds (acl_set_fd)");
891 result = ACL_ERROR;
892 goto cleanup;
893 }
894
895 recurse:
896 if (recursive && S_ISDIR(s.st_mode)) {
897 /* Recurse into subdirectories. Don't call closedir() on d! It
898 closes the open file descriptor as well, and subsequent calls
899 to close() then throw errors. */
900 DIR* d = fdopendir(fd);
901 if (d == NULL) {
902 perror("apply_default_acl_fds (fdopendir)");
903 result = ACL_ERROR;
904 goto cleanup;
905 }
906
907 struct dirent* de;
908 int new_fd = 0;
909 while ((de = readdir(d)) != NULL) {
910 if (de->d_type != DT_DIR && de->d_type != DT_REG) {
911 /* Hit a symlink or whatever. */
912 result = ACL_FAILURE;
913 continue;
914 }
915 if (strcmp(de->d_name, ".") == 0) { continue; }
916 if (strcmp(de->d_name, "..") == 0) { continue; }
917
918 /* Be careful not to "return" out of this loop and leave the
919 new_fd open! */
920 new_fd = openat(fd, de->d_name, O_NOFOLLOW);
921 if (new_fd == OPEN_ERROR) {
922 if (errno == ELOOP || errno == ENOTDIR) {
923 /* We hit a symlink, either in the last path component (ELOOP)
924 or higher up (ENOTDIR). */
925 if (result == ACL_SUCCESS) {
926 /* Don't overwrite an error result with success/failure. */
927 result = ACL_FAILURE;
928 }
929 continue;
930 }
931 else {
932 perror("apply_default_acl_fds (openat)");
933 result = ACL_ERROR;
934 continue;
935 }
936 }
937 switch (apply_default_acl_fds(fd, new_fd, recursive)) {
938 /* Don't overwrite an error result with success/failure. */
939 case ACL_FAILURE:
940 if (result == ACL_SUCCESS) {
941 result = ACL_FAILURE;
942 }
943 break;
944 case ACL_ERROR:
945 result = ACL_ERROR;
946 default:
947 if (close(new_fd) == CLOSE_ERROR) {
948 perror("apply_default_acl_fds (close)");
949 result = ACL_ERROR;
950 }
951 }
952 }
953 }
954
955 cleanup:
956 acl_free(new_acl);
957 acl_free(new_acl_unmasked);
958 return result;
959 }
960
961
962 /**
963 * @brief Apply parent default ACL to a path and optionally its children.
964 *
965 * This overwrites any existing ACLs on the target, and, if @c
966 * recursive is @c true, its children. When @c recursive is @c true,
967 * the "worst" result encountered is returned as the overall result.
968 *
969 * @param path
970 * The path whose ACL we would like to reset to its default.
971 *
972 * @param recursive
973 * Should we recurse into subdirectories?
974 *
975 * @return
976 * - @c ACL_SUCCESS - The parent default ACLs were inherited successfully.
977 * - @c ACL_FAILURE - If symlinks or hard links are encountered.
978 * - @c ACL_ERROR - Unexpected library error.
979 */
980 int apply_default_acl(const char* path, bool recursive) {
981
982 if (path == NULL) {
983 errno = EINVAL;
984 perror("apply_default_acl (args)");
985 return ACL_ERROR;
986 }
987
988 /* Define these next three variables here because we may have to
989 * jump to the cleanup routine which expects them to exist.
990 */
991
992 /* Our return value. */
993 int result = ACL_SUCCESS;
994
995 /* The file descriptor corresponding to "path" */
996 int fd = 0;
997
998 /* The file descriptor for the directory containing "path" */
999 int parent_fd = 0;
1000
1001 /* dirname() and basename() mangle their arguments, so we need
1002 to make copies of "path" before using them. */
1003 char* dirname_path_copy = NULL;
1004 char* basename_path_copy = NULL;
1005
1006 /* Get the parent directory of "path" with dirname(), which happens
1007 * to murder its argument and necessitates a path_copy. */
1008 dirname_path_copy = strdup(path);
1009 if (dirname_path_copy == NULL) {
1010 perror("apply_default_acl (strdup)");
1011 return ACL_ERROR;
1012 }
1013 char* parent = dirname(dirname_path_copy);
1014
1015 basename_path_copy = strdup(path);
1016 if (basename_path_copy == NULL) {
1017 perror("apply_default_acl (strdup)");
1018 result = ACL_ERROR;
1019 goto cleanup;
1020 }
1021 char* child = basename(basename_path_copy);
1022
1023 /* Just kidding, if the path is "." or "..", then dirname will do
1024 * the wrong thing and give us "." as its parent, too. So, we handle
1025 * those as special cases. We use "child" instead of "path" here to
1026 * catch things like "./" and "../"
1027 */
1028 bool path_is_dots = strcmp(child, ".") == 0 || strcmp(child, "..") == 0;
1029 char dots_parent[6] = "../";
1030 if (path_is_dots) {
1031 /* We know that "child" contains no more than two characters here, and
1032 using strncat to enforce that belief keeps clang-tidy happy. */
1033 parent = strncat(dots_parent, child, 2);
1034 }
1035
1036 parent_fd = safe_open(parent, O_DIRECTORY | O_NOFOLLOW);
1037
1038 if (parent_fd == OPEN_ERROR) {
1039 if (errno == ELOOP || errno == ENOTDIR) {
1040 /* We hit a symlink, either in the last path component (ELOOP)
1041 or higher up (ENOTDIR). */
1042 result = ACL_FAILURE;
1043 goto cleanup;
1044 }
1045 else {
1046 perror("apply_default_acl (open parent fd)");
1047 result = ACL_ERROR;
1048 goto cleanup;
1049 }
1050 }
1051
1052 /* We already obtained the parent fd safely, so if we use the
1053 * basename of path here instead of the full thing, then we can get
1054 * away with using openat() and spare ourselves the slowness of
1055 * another safe_open().
1056 *
1057 * Note that if the basename is "." or "..", then we don't want to
1058 * open it relative to the parent_fd, so we need another special
1059 * case for those paths here.
1060 */
1061 if (path_is_dots) {
1062 fd = open(child, O_NOFOLLOW);
1063 }
1064 else {
1065 fd = openat(parent_fd, child, O_NOFOLLOW);
1066 }
1067 if (fd == OPEN_ERROR) {
1068 if (errno == ELOOP || errno == ENOTDIR) {
1069 /* We hit a symlink, either in the last path component (ELOOP)
1070 or higher up (ENOTDIR). */
1071 result = ACL_FAILURE;
1072 goto cleanup;
1073 }
1074 else {
1075 perror("apply_default_acl (open fd)");
1076 result = ACL_ERROR;
1077 goto cleanup;
1078 }
1079 }
1080
1081 result = apply_default_acl_fds(parent_fd, fd, recursive);
1082
1083 cleanup:
1084 free(dirname_path_copy);
1085 free(basename_path_copy);
1086
1087 if (parent_fd > 0 && close(parent_fd) == CLOSE_ERROR) {
1088 perror("apply_default_acl (close parent_fd)");
1089 result = ACL_ERROR;
1090 }
1091 if (fd > 0 && close(fd) == CLOSE_ERROR) {
1092 perror("apply_default_acl (close fd)");
1093 result = ACL_ERROR;
1094 }
1095 return result;
1096 }