]> gitweb.michael.orlitzky.com - apply-default-acl.git/blob - src/libadacl.c
956063aa75ad0f4a5584fb5e3f22d0ea91f04b5f
[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 updated_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 updated_entry
236 * An updated copy of an existing entry in @c aclp.
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 updated_entry) {
244 if (aclp == NULL || updated_entry == NULL) {
245 errno = EINVAL;
246 perror("acl_update_entry (args)");
247 return ACL_ERROR;
248 }
249
250 acl_tag_t updated_tag;
251 if (acl_get_tag_type(updated_entry, &updated_tag) == ACL_ERROR) {
252 perror("acl_update_entry (acl_get_tag_type)");
253 return ACL_ERROR;
254 }
255
256 acl_permset_t updated_permset;
257 if (acl_get_permset(updated_entry, &updated_permset) == ACL_ERROR) {
258 perror("acl_update_entry (acl_get_permset)");
259 return ACL_ERROR;
260 }
261
262 /* This can allocate memory, so from here on out we have to jump to
263 the "cleanup" label to exit. */
264 void* updated_qualifier = acl_get_qualifier(updated_entry);
265 if (updated_qualifier == NULL &&
266 (updated_tag == ACL_USER || updated_tag == ACL_GROUP)) {
267 /* acl_get_qualifier() can return NULL, but it shouldn't for
268 ACL_USER or ACL_GROUP entries. */
269 perror("acl_update_entry (acl_get_qualifier)");
270 return ACL_ERROR;
271 }
272
273 /* Our return value. Default to failure, and change to success if we
274 actually update something. */
275 int result = ACL_FAILURE;
276
277 acl_entry_t existing_entry;
278 /* Loop through the given ACL looking for matching entries. */
279 int get_entry_result = acl_get_entry(aclp, ACL_FIRST_ENTRY, &existing_entry);
280
281 while (get_entry_result == ACL_SUCCESS) {
282 acl_tag_t existing_tag = ACL_UNDEFINED_TAG;
283
284 if (acl_get_tag_type(existing_entry, &existing_tag) == ACL_ERROR) {
285 perror("set_acl_tag_permset (acl_get_tag_type)");
286 result = ACL_ERROR;
287 goto cleanup;
288 }
289
290 if (existing_tag == updated_tag) {
291 /* Our tag types match, but if we have a named user or group
292 entry, then we need to check that the user/group (that is,
293 the qualifier) matches too. */
294 bool qualifiers_match = false;
295
296 /* There are three ways the qualifiers can match... */
297 void* existing_qualifier = acl_get_qualifier(existing_entry);
298 if (existing_qualifier == NULL) {
299 if (existing_tag == ACL_USER || existing_tag == ACL_GROUP) {
300 perror("acl_update_entry (acl_get_qualifier)");
301 result = ACL_ERROR;
302 goto cleanup;
303 }
304 else {
305 /* First, we could be dealing with an entry that isn't a
306 named user or group, in which case they "match
307 vacuously." */
308 qualifiers_match = true;
309 }
310 }
311
312 /* Otherwise, we have to have matching UIDs or GIDs. */
313 if (updated_tag == ACL_USER) {
314 qualifiers_match = ( *((uid_t*)existing_qualifier)
315 ==
316 *((uid_t*)updated_qualifier) );
317 }
318 else if (updated_tag == ACL_GROUP) {
319 qualifiers_match = ( *((gid_t*)existing_qualifier)
320 ==
321 *((gid_t*)updated_qualifier) );
322 }
323
324 /* Be sure to free this inside the loop, where memory is allocated. */
325 acl_free(existing_qualifier);
326
327 if (qualifiers_match) {
328 /* If we update something, we're done and return ACL_SUCCESS */
329 if (acl_set_permset(existing_entry, updated_permset) == ACL_ERROR) {
330 perror("acl_update_entry (acl_set_permset)");
331 result = ACL_ERROR;
332 goto cleanup;
333 }
334
335 result = ACL_SUCCESS;
336 goto cleanup;
337 }
338 }
339
340 get_entry_result = acl_get_entry(aclp, ACL_NEXT_ENTRY, &existing_entry);
341 }
342
343 /* This catches both the initial acl_get_entry and the ones at the
344 end of the loop. */
345 if (get_entry_result == ACL_ERROR) {
346 perror("acl_update_entry (acl_get_entry)");
347 result = ACL_ERROR;
348 }
349
350 cleanup:
351 acl_free(updated_qualifier);
352 return result;
353 }
354
355
356
357 /**
358 * @brief Determine the number of entries in the given ACL.
359 *
360 * @param acl
361 * The ACL to inspect.
362 *
363 * @return Either the non-negative number of entries in @c acl, or
364 * @c ACL_ERROR on error.
365 */
366 int acl_entry_count(acl_t acl) {
367
368 acl_entry_t entry;
369 int entry_count = 0;
370 int result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
371
372 while (result == ACL_SUCCESS) {
373 entry_count++;
374 result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
375 }
376
377 if (result == ACL_ERROR) {
378 perror("acl_entry_count (acl_get_entry)");
379 return ACL_ERROR;
380 }
381
382 return entry_count;
383 }
384
385
386
387 /**
388 * @brief Determine whether or not the given ACL is minimal.
389 *
390 * An ACL is minimal if it has fewer than four entries.
391 *
392 * @param acl
393 * The ACL whose minimality is in question.
394 *
395 * @return
396 * - @c ACL_SUCCESS - @c acl is minimal
397 * - @c ACL_FAILURE - @c acl is not minimal
398 * - @c ACL_ERROR - Unexpected library error
399 */
400 int acl_is_minimal(acl_t acl) {
401 if (acl == NULL) {
402 errno = EINVAL;
403 perror("acl_is_minimal (args)");
404 return ACL_ERROR;
405 }
406
407 int ec = acl_entry_count(acl);
408
409 if (ec == ACL_ERROR) {
410 perror("acl_is_minimal (acl_entry_count)");
411 return ACL_ERROR;
412 }
413
414 if (ec < 4) {
415 return ACL_SUCCESS;
416 }
417 else {
418 return ACL_FAILURE;
419 }
420 }
421
422
423
424 /**
425 * @brief Determine whether the given ACL's mask denies execute.
426 *
427 * @param acl
428 * The ACL whose mask we want to check.
429 *
430 * @return
431 * - @c ACL_SUCCESS - The @c acl has a mask which denies execute.
432 * - @c ACL_FAILURE - The @c acl has a mask which does not deny execute.
433 * - @c ACL_ERROR - Unexpected library error.
434 */
435 int acl_execute_masked(acl_t acl) {
436 if (acl == NULL) {
437 errno = EINVAL;
438 perror("acl_execute_masked (args)");
439 return ACL_ERROR;
440 }
441
442 acl_entry_t entry;
443 int ge_result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
444
445 while (ge_result == ACL_SUCCESS) {
446 acl_tag_t tag = ACL_UNDEFINED_TAG;
447
448 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
449 perror("acl_execute_masked (acl_get_tag_type)");
450 return ACL_ERROR;
451 }
452
453 if (tag == ACL_MASK) {
454 /* This is the mask entry, get its permissions, and see if
455 execute is specified. */
456 acl_permset_t permset;
457
458 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
459 perror("acl_execute_masked (acl_get_permset)");
460 return ACL_ERROR;
461 }
462
463 int gp_result = acl_get_perm(permset, ACL_EXECUTE);
464 if (gp_result == ACL_ERROR) {
465 perror("acl_execute_masked (acl_get_perm)");
466 return ACL_ERROR;
467 }
468
469 if (gp_result == ACL_FAILURE) {
470 /* No execute bit set in the mask; execute not allowed. */
471 return ACL_SUCCESS;
472 }
473 }
474
475 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
476 }
477
478 return ACL_FAILURE;
479 }
480
481
482
483 /**
484 * @brief Determine whether @c fd is executable by anyone.
485 *
486 *
487 * This is used as part of the heuristic to determine whether or not
488 * we should mask the execute bit when inheriting an ACL. If @c fd
489 * describes a file, we check the @a effective permissions, contrary
490 * to what setfacl does.
491 *
492 * @param fd
493 * The file descriptor to check.
494 *
495 * @param sp
496 * A pointer to a stat structure for @c fd.
497 *
498 * @return
499 * - @c ACL_SUCCESS - Someone has effective execute permissions on @c fd.
500 * - @c ACL_FAILURE - Nobody can execute @c fd.
501 * - @c ACL_ERROR - Unexpected library error.
502 */
503 int any_can_execute(int fd, const struct stat* sp) {
504 if (sp == NULL) {
505 errno = EINVAL;
506 perror("any_can_execute (args)");
507 return ACL_ERROR;
508 }
509
510 acl_t acl = acl_get_fd(fd);
511
512 if (acl == (acl_t)NULL) {
513 perror("any_can_execute (acl_get_fd)");
514 return ACL_ERROR;
515 }
516
517 /* Our return value. */
518 int result = ACL_FAILURE;
519
520 if (acl_is_minimal(acl)) {
521 if (sp->st_mode & (S_IXUSR | S_IXOTH | S_IXGRP)) {
522 result = ACL_SUCCESS;
523 goto cleanup;
524 }
525 else {
526 result = ACL_FAILURE;
527 goto cleanup;
528 }
529 }
530
531 acl_entry_t entry;
532 int ge_result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
533
534 while (ge_result == ACL_SUCCESS) {
535 /* The first thing we do is check to see if this is a mask
536 entry. If it is, we skip it entirely. */
537 acl_tag_t tag = ACL_UNDEFINED_TAG;
538
539 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
540 perror("any_can_execute_or (acl_get_tag_type)");
541 result = ACL_ERROR;
542 goto cleanup;
543 }
544
545 if (tag == ACL_MASK) {
546 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
547 continue;
548 }
549
550 /* Ok, so it's not a mask entry. Check the execute perms. */
551 acl_permset_t permset;
552
553 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
554 perror("any_can_execute_or (acl_get_permset)");
555 result = ACL_ERROR;
556 goto cleanup;
557 }
558
559 int gp_result = acl_get_perm(permset, ACL_EXECUTE);
560 if (gp_result == ACL_ERROR) {
561 perror("any_can_execute (acl_get_perm)");
562 result = ACL_ERROR;
563 goto cleanup;
564 }
565
566 if (gp_result == ACL_SUCCESS) {
567 /* Only return ACL_SUCCESS if this execute bit is not masked. */
568 if (acl_execute_masked(acl) != ACL_SUCCESS) {
569 result = ACL_SUCCESS;
570 goto cleanup;
571 }
572 }
573
574 ge_result = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
575 }
576
577 if (ge_result == ACL_ERROR) {
578 perror("any_can_execute (acl_get_entry)");
579 result = ACL_ERROR;
580 goto cleanup;
581 }
582
583 cleanup:
584 acl_free(acl);
585 return result;
586 }
587
588
589
590 /**
591 * @brief Copy ACLs between file descriptors as xattrs, verbatim.
592 *
593 * There is a small deficiency in libacl, namely that there is no way
594 * to get or set default ACLs through file descriptors. The @c
595 * acl_get_file and @c acl_set_file functions can do it, but they use
596 * paths, and are vulnerable to symlink attacks.
597 *
598 * Fortunately, when inheriting an ACL, we don't really need to look
599 * at what it contains. That means that we can copy the on-disk xattrs
600 * from the source directory to the destination file/directory without
601 * passing through libacl, and this can be done with file descriptors
602 * through @c fgetxattr and @c fsetxattr. That's what this function
603 * does.
604 *
605 * @param src_fd
606 * The file descriptor from which the ACL will be copied.
607 *
608 * @param src_type
609 * The type of ACL (either @c ACL_TYPE_ACCESS or @c ACL_TYPE_DEFAULT)
610 * to copy from @c src_fd.
611 *
612 * @param dst_fd
613 * The file descriptor whose ACL will be overwritten with the one
614 * from @c src_fd.
615 *
616 * @param dst_type
617 * The type of ACL (either @c ACL_TYPE_ACCESS or @c ACL_TYPE_DEFAULT)
618 * to replace on @c dst_fd.
619 *
620 * @return
621 * - @c ACL_SUCCESS - The ACL was copied successfully.
622 * - @c ACL_FAILURE - There was no ACL on @c src_fd.
623 * - @c ACL_ERROR - Unexpected library error.
624 */
625 int acl_copy_xattr(int src_fd,
626 acl_type_t src_type,
627 int dst_fd,
628 acl_type_t dst_type) {
629
630 const char* src_name;
631 if (src_type == ACL_TYPE_ACCESS) {
632 src_name = XATTR_NAME_POSIX_ACL_ACCESS;
633 }
634 else if (src_type == ACL_TYPE_DEFAULT) {
635 src_name = XATTR_NAME_POSIX_ACL_DEFAULT;
636 }
637 else {
638 errno = EINVAL;
639 perror("acl_copy_xattr (src type)");
640 return ACL_ERROR;
641 }
642
643 const char* dst_name;
644 if (dst_type == ACL_TYPE_ACCESS) {
645 dst_name = XATTR_NAME_POSIX_ACL_ACCESS;
646 }
647 else if (dst_type == ACL_TYPE_DEFAULT) {
648 dst_name = XATTR_NAME_POSIX_ACL_DEFAULT;
649 }
650 else {
651 errno = EINVAL;
652 perror("acl_copy_xattr (dst type)");
653 return ACL_ERROR;
654 }
655
656 ssize_t src_size_guess = fgetxattr(src_fd, src_name, NULL, 0);
657 if (src_size_guess == XATTR_ERROR) {
658 if (errno == ENODATA) {
659 /* A missing ACL isn't really an error. ENOATTR and ENODATA are
660 synonyms, but using ENODATA here lets us avoid another
661 "include" directive. */
662 return ACL_FAILURE;
663 }
664 perror("acl_copy_xattr (fgetxattr size guess)");
665 return ACL_ERROR;
666 }
667 char* src_acl_p = alloca(src_size_guess);
668 /* The actual size may be smaller than our guess? I don't know. The
669 return value from fgetxattr() will either be nonnegative, or
670 XATTR_ERROR (which we've already ruled out), so it's safe to cast
671 it to an unsigned size_t here to avoid a compiler warning. */
672 ssize_t src_size = fgetxattr(src_fd,
673 src_name,
674 src_acl_p,
675 (size_t)src_size_guess);
676 if (src_size == XATTR_ERROR) {
677 if (errno == ENODATA) {
678 /* A missing ACL isn't an error. */
679 return ACL_FAILURE;
680 }
681 perror("acl_copy_xattr (fgetxattr)");
682 return ACL_ERROR;
683 }
684
685 /* See above: src_size must be nonnegative at this point,so we cast
686 it to size_t to avoid a compiler warning. */
687 if (fsetxattr(dst_fd,
688 dst_name,
689 src_acl_p,
690 (size_t)src_size,
691 0)
692 == XATTR_ERROR) {
693 perror("acl_copy_xattr (fsetxattr)");
694 return ACL_ERROR;
695 }
696
697 return ACL_SUCCESS;
698 }
699
700
701 /**
702 * @brief Determine if a file descriptor has a default ACL.
703 *
704 * @param fd
705 * The file descriptor whose default ACL is in question.
706 *
707 * @return
708 * - @c ACL_SUCCESS - If @c fd has a default ACL.
709 * - @c ACL_FAILURE - If @c fd does not have a default ACL.
710 * - @c ACL_ERROR - Unexpected library error.
711 */
712 int has_default_acl_fd(int fd) {
713 if (fgetxattr(fd, XATTR_NAME_POSIX_ACL_DEFAULT, NULL, 0) == XATTR_ERROR) {
714 if (errno == ENODATA) {
715 return ACL_FAILURE;
716 }
717 perror("has_default_acl_fd (fgetxattr)");
718 return ACL_ERROR;
719 }
720
721 return ACL_SUCCESS;
722 }
723
724
725
726 /**
727 * @brief The recursive portion of @c apply_default_acl.
728 *
729 * The @c apply_default_acl function takes a path, but then opens file
730 * descriptors for the path and its parent. Afterwards, everything is
731 * done using file descriptors, including the recursive application on
732 * the path's children. This function encapsulates the portion of @c
733 * apply_default_acl that uses only file descriptors; for the
734 * recursion, this function ultimately calls itself.
735 *
736 * This overwrites any existing ACLs on @c fd and, if @c recursive is
737 * @c true, its children. When @c recursive is @c true, the "worst"
738 * result encountered is returned as the overall result.
739 *
740 * @param parent_fd
741 * A file descriptor for the parent directory of @c fd.
742 *
743 * @param fd
744 * The file descriptor that should inherit its parent's default ACL.
745 *
746 * @param recursive
747 * Should we recurse into subdirectories?
748 *
749 * @return
750 * - @c ACL_SUCCESS - The parent default ACLs were inherited successfully.
751 * - @c ACL_FAILURE - If symlinks or hard links are encountered.
752 * - @c ACL_ERROR - Unexpected library error.
753 */
754 int apply_default_acl_fds(int parent_fd, int fd, bool recursive) {
755 int result = ACL_SUCCESS;
756
757 /* The new ACL for this path */
758 acl_t new_acl = (acl_t)NULL;
759
760 /* A copy of new_acl, to be made before we begin mangling new_acl in
761 order to mask the execute bit. */
762 acl_t new_acl_unmasked = (acl_t)NULL;
763
764 /* Refuse to operate on hard links, which can be abused by an
765 * attacker to trick us into changing the ACL on a file we didn't
766 * intend to; namely the "target" of the hard link. There is TOCTOU
767 * race condition here, but the window is as small as possible
768 * between when we open the file descriptor (look above) and when we
769 * fstat it.
770 */
771 struct stat s;
772 if (fstat(fd, &s) == STAT_ERROR) {
773 perror("apply_default_acl_fds (fstat)");
774 /* We can't recurse without the stat struct for fd */
775 goto cleanup;
776 }
777
778
779 /* Check to make sure the parent descriptor actually has a default
780 ACL. If it doesn't, then we can "succeed" immediately, saving a
781 little work, particularly in any_can_execute(). Note that we
782 can't skip the fstat() above, because we need it in case we
783 recurse. */
784 if (has_default_acl_fd(parent_fd) == ACL_FAILURE) {
785 result = ACL_SUCCESS;
786 /* Just because this target can't inherit anything doesn't mean
787 that one of it's children can't. For example, if there's a
788 default on "c" in "a/b/c/d", then we don't want to skip all
789 children of "a"! */
790 goto recurse;
791 }
792
793
794 if (!S_ISDIR(s.st_mode)) {
795 /* If it's not a directory, make sure it's a regular,
796 non-hard-linked file. */
797 if (!S_ISREG(s.st_mode) || s.st_nlink != 1) {
798 result = ACL_FAILURE;
799 goto cleanup; /* It's not a directory, so we can skip the recursion. */
800 }
801 }
802
803
804 /* Next We try to guess whether or not to strip the execute bits.
805 * This behavior is modeled after the capital 'X' perms of setfacl.
806 */
807 int ace_result = any_can_execute(fd, &s);
808
809 if (ace_result == ACL_ERROR) {
810 perror("apply_default_acl_fds (any_can_execute)");
811 result = ACL_ERROR;
812 goto cleanup;
813 }
814
815 /* Never mask the execute bit on directories. */
816 bool allow_exec = (bool)ace_result || S_ISDIR(s.st_mode);
817
818
819 /* If it's a directory, inherit the parent's default. */
820 if (S_ISDIR(s.st_mode)) {
821 if (acl_copy_xattr(parent_fd,
822 ACL_TYPE_DEFAULT,
823 fd,
824 ACL_TYPE_DEFAULT) == ACL_ERROR) {
825 perror("apply_default_acl_fds (acl_copy_xattr default)");
826 result = ACL_ERROR;
827 goto cleanup;
828 }
829 }
830
831 /* If it's anything, _apply_ the parent's default. */
832 if (acl_copy_xattr(parent_fd,
833 ACL_TYPE_DEFAULT,
834 fd,
835 ACL_TYPE_ACCESS) == ACL_ERROR) {
836 perror("apply_default_acl_fds (acl_copy_xattr access)");
837 result = ACL_ERROR;
838 goto cleanup;
839 }
840
841 /* There's a good reason why we saved the ACL above, even though
842 * we're about to read it back into memory and mess with it on the
843 * next line. The acl_copy_xattr() function is already a hack to let
844 * us copy default ACLs without resorting to path names; we simply
845 * have no way to read the parent's default ACL into memory using
846 * parent_fd. We can, however, copy the parent's ACL to a file (with
847 * acl_copy_xattr), and then read the ACL from a file using
848 * "fd". It's quite the circus, but it works and should be safe from
849 * sym/hardlink attacks.
850 */
851
852 /* Now we potentially need to mask the execute permissions in the
853 ACL on fd; or maybe not. */
854 if (allow_exec) {
855 /* Skip the mask code for this target, but don't skip its children! */
856 goto recurse;
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_fds (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_fds (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_fds (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_fds (acl_get_permset)");
895 result = ACL_ERROR;
896 goto cleanup;
897 }
898
899 /* To mimic what the kernel does, I think we could drop
900 ACL_GROUP_OBJ from the list below? */
901 if (tag == ACL_MASK ||
902 tag == ACL_USER_OBJ ||
903 tag == ACL_GROUP_OBJ ||
904 tag == ACL_OTHER) {
905
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_fds (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_fds (acl_set_permset)");
917 result = ACL_ERROR;
918 goto cleanup;
919 }
920 }
921
922 if (acl_update_entry(new_acl, entry) == ACL_ERROR) {
923 perror("apply_default_acl_fds (acl_update_entry)");
924 result = ACL_ERROR;
925 goto cleanup;
926 }
927
928 ge_result = acl_get_entry(new_acl_unmasked, ACL_NEXT_ENTRY, &entry);
929 }
930
931 /* Catches the first acl_get_entry as well as the ones at the end of
932 the loop. */
933 if (ge_result == ACL_ERROR) {
934 perror("apply_default_acl_fds (acl_get_entry)");
935 result = ACL_ERROR;
936 goto cleanup;
937 }
938
939 if (acl_set_fd(fd, new_acl) == ACL_ERROR) {
940 perror("apply_default_acl_fds (acl_set_fd)");
941 result = ACL_ERROR;
942 goto cleanup;
943 }
944
945 recurse:
946 if (recursive && S_ISDIR(s.st_mode)) {
947 /* Recurse into subdirectories. Don't call closedir() on d! It
948 closes the open file descriptor as well, and subsequent calls
949 to close() then throw errors. */
950 DIR* d = fdopendir(fd);
951 if (d == NULL) {
952 perror("apply_default_acl_fds (fdopendir)");
953 result = ACL_ERROR;
954 goto cleanup;
955 }
956
957 struct dirent* de;
958 int new_fd = 0;
959 while ((de = readdir(d)) != NULL) {
960 if (de->d_type != DT_DIR && de->d_type != DT_REG) {
961 /* Hit a symlink or whatever. */
962 result = ACL_FAILURE;
963 continue;
964 }
965 if (strcmp(de->d_name, ".") == 0) { continue; }
966 if (strcmp(de->d_name, "..") == 0) { continue; }
967
968 /* Be careful not to "return" out of this loop and leave the
969 new_fd open! */
970 new_fd = openat(fd, de->d_name, O_NOFOLLOW);
971 if (new_fd == OPEN_ERROR) {
972 if (errno == ELOOP || errno == ENOTDIR) {
973 /* We hit a symlink, either in the last path component (ELOOP)
974 or higher up (ENOTDIR). */
975 if (result == ACL_SUCCESS) {
976 /* Don't overwrite an error result with success/failure. */
977 result = ACL_FAILURE;
978 }
979 continue;
980 }
981 else {
982 perror("apply_default_acl_fds (openat)");
983 result = ACL_ERROR;
984 continue;
985 }
986 }
987 switch (apply_default_acl_fds(fd, new_fd, recursive)) {
988 /* Don't overwrite an error result with success/failure. */
989 case ACL_FAILURE:
990 if (result == ACL_SUCCESS) {
991 result = ACL_FAILURE;
992 }
993 break;
994 case ACL_ERROR:
995 result = ACL_ERROR;
996 default:
997 if (close(new_fd) == CLOSE_ERROR) {
998 perror("apply_default_acl_fds (close)");
999 result = ACL_ERROR;
1000 }
1001 }
1002 }
1003 }
1004
1005 cleanup:
1006 acl_free(new_acl);
1007 acl_free(new_acl_unmasked);
1008 return result;
1009 }
1010
1011
1012 /**
1013 * @brief Apply parent default ACL to a path and optionally its children.
1014 *
1015 * This overwrites any existing ACLs on the target, and, if @c
1016 * recursive is @c true, its children. When @c recursive is @c true,
1017 * the "worst" result encountered is returned as the overall result.
1018 *
1019 * @param path
1020 * The path whose ACL we would like to reset to its default.
1021 *
1022 * @param recursive
1023 * Should we recurse into subdirectories?
1024 *
1025 * @return
1026 * - @c ACL_SUCCESS - The parent default ACLs were inherited successfully.
1027 * - @c ACL_FAILURE - If symlinks or hard links are encountered.
1028 * - @c ACL_ERROR - Unexpected library error.
1029 */
1030 int apply_default_acl(const char* path, bool recursive) {
1031
1032 if (path == NULL) {
1033 errno = EINVAL;
1034 perror("apply_default_acl (args)");
1035 return ACL_ERROR;
1036 }
1037
1038 /* Define these next three variables here because we may have to
1039 * jump to the cleanup routine which expects them to exist.
1040 */
1041
1042 /* Our return value. */
1043 int result = ACL_SUCCESS;
1044
1045 /* The file descriptor corresponding to "path" */
1046 int fd = 0;
1047
1048 /* The file descriptor for the directory containing "path" */
1049 int parent_fd = 0;
1050
1051 /* dirname() and basename() mangle their arguments, so we need
1052 to make copies of "path" before using them. */
1053 char* dirname_path_copy = NULL;
1054 char* basename_path_copy = NULL;
1055
1056 /* Get the parent directory of "path" with dirname(), which happens
1057 * to murder its argument and necessitates a path_copy. */
1058 dirname_path_copy = strdup(path);
1059 if (dirname_path_copy == NULL) {
1060 perror("apply_default_acl (strdup)");
1061 return ACL_ERROR;
1062 }
1063 char* parent = dirname(dirname_path_copy);
1064
1065 basename_path_copy = strdup(path);
1066 if (basename_path_copy == NULL) {
1067 perror("apply_default_acl (strdup)");
1068 result = ACL_ERROR;
1069 goto cleanup;
1070 }
1071 char* child = basename(basename_path_copy);
1072
1073 /* Just kidding, if the path is "." or "..", then dirname will do
1074 * the wrong thing and give us "." as its parent, too. So, we handle
1075 * those as special cases. We use "child" instead of "path" here to
1076 * catch things like "./" and "../"
1077 */
1078 bool path_is_dots = strcmp(child, ".") == 0 || strcmp(child, "..") == 0;
1079 char dots_parent[6] = "../";
1080 if (path_is_dots) {
1081 /* We know that "child" contains no more than two characters here, and
1082 using strncat to enforce that belief keeps clang-tidy happy. */
1083 parent = strncat(dots_parent, child, 2);
1084 }
1085
1086 parent_fd = safe_open(parent, O_DIRECTORY | O_NOFOLLOW);
1087
1088 if (parent_fd == OPEN_ERROR) {
1089 if (errno == ELOOP || errno == ENOTDIR) {
1090 /* We hit a symlink, either in the last path component (ELOOP)
1091 or higher up (ENOTDIR). */
1092 result = ACL_FAILURE;
1093 goto cleanup;
1094 }
1095 else {
1096 perror("apply_default_acl (open parent fd)");
1097 result = ACL_ERROR;
1098 goto cleanup;
1099 }
1100 }
1101
1102 /* We already obtained the parent fd safely, so if we use the
1103 * basename of path here instead of the full thing, then we can get
1104 * away with using openat() and spare ourselves the slowness of
1105 * another safe_open().
1106 *
1107 * Note that if the basename is "." or "..", then we don't want to
1108 * open it relative to the parent_fd, so we need another special
1109 * case for those paths here.
1110 */
1111 if (path_is_dots) {
1112 fd = open(child, O_NOFOLLOW);
1113 }
1114 else {
1115 fd = openat(parent_fd, child, O_NOFOLLOW);
1116 }
1117 if (fd == OPEN_ERROR) {
1118 if (errno == ELOOP || errno == ENOTDIR) {
1119 /* We hit a symlink, either in the last path component (ELOOP)
1120 or higher up (ENOTDIR). */
1121 result = ACL_FAILURE;
1122 goto cleanup;
1123 }
1124 else {
1125 perror("apply_default_acl (open fd)");
1126 result = ACL_ERROR;
1127 goto cleanup;
1128 }
1129 }
1130
1131 result = apply_default_acl_fds(parent_fd, fd, recursive);
1132
1133 cleanup:
1134 free(dirname_path_copy);
1135 free(basename_path_copy);
1136
1137 if (parent_fd > 0 && close(parent_fd) == CLOSE_ERROR) {
1138 perror("apply_default_acl (close parent_fd)");
1139 result = ACL_ERROR;
1140 }
1141 if (fd > 0 && close(fd) == CLOSE_ERROR) {
1142 perror("apply_default_acl (close fd)");
1143 result = ACL_ERROR;
1144 }
1145 return result;
1146 }