]> gitweb.michael.orlitzky.com - apply-default-acl.git/blob - src/libadacl.c
src/libadacl.c: kill a pointless "else if" after an "if" that returns.
[apply-default-acl.git] / src / libadacl.c
1 /**
2 * @file libadacl.c
3 *
4 * @brief The adacl (apply default acl) shared library.
5 *
6 */
7
8 /* Enables get_current_dir_name() in unistd.h and the O_PATH flag. */
9 #define _GNU_SOURCE
10
11 #include <errno.h> /* EINVAL, ELOOP, ENOTDIR, etc. */
12 #include <fcntl.h> /* openat() */
13 #include <libgen.h> /* basename(), dirname() */
14 #include <limits.h> /* PATH_MAX */
15 #include <stdbool.h> /* the "bool" type */
16 #include <stdio.h> /* perror(), snprintf() */
17 #include <stdlib.h> /* free() */
18 #include <string.h> /* strdup() */
19 #include <sys/stat.h> /* fstat() */
20 #include <sys/xattr.h> /* fgetxattr(), fsetxattr() */
21 #include <unistd.h> /* get_current_dir_name() */
22
23 /* ACLs */
24 #include <acl/libacl.h> /* acl_get_perm, not portable */
25 #include <sys/acl.h> /* all other acl_foo functions */
26
27 /* XATTR_NAME_POSIX_ACL_ACCESS and XATTR_NAME_POSIX_ACL_DEFAULT */
28 #include <linux/xattr.h>
29
30 #include "libadacl.h"
31
32
33 /* Even though most other library functions reliably return -1 for
34 * error, it feels a little wrong to re-use the ACL_ERROR constant.
35 */
36 #define CLOSE_ERROR -1
37 #define OPEN_ERROR -1
38 #define SNPRINTF_ERROR -1
39 #define STAT_ERROR -1
40 #define XATTR_ERROR -1
41
42
43 /**
44 * @brief The recursive portion of the @c safe_open function, used to
45 * open a file descriptor in a symlink-safe way when combined with
46 * the @c O_NOFOLLOW flag.
47 *
48 * @param at_fd
49 * A file descriptor relative to which @c pathname will be opened.
50 *
51 * @param pathname
52 * The path to the file/directory/whatever whose descriptor you want.
53 *
54 * @param flags
55 * File status flags to be passed to @c openat.
56 *
57 * @return a file descriptor for @c pathname if everything goes well,
58 * and @c OPEN_ERROR if not.
59 */
60 int safe_open_ex(int at_fd, char* pathname, int flags) {
61 if (pathname == NULL) {
62 errno = EINVAL;
63 perror("safe_open_ex (args)");
64 return OPEN_ERROR;
65 }
66
67 char* firstslash = strchr(pathname, '/');
68 if (firstslash == NULL) {
69 /* No more slashes, this is the base case. */
70 return openat(at_fd, pathname, flags);
71 }
72 if (firstslash[1] == '\0') {
73 /* The first slash is the last character; ensure that we open
74 a directory. */
75 firstslash[0] = '\0';
76 return openat(at_fd, pathname, flags | O_DIRECTORY);
77 }
78
79 /* The first slash exists and isn't the last character in the path,
80 so we can split the path wherever that first slash lies and
81 recurse. */
82 *firstslash = '\0';
83 int fd = openat(at_fd, pathname, flags | O_DIRECTORY | O_PATH);
84 if (fd == OPEN_ERROR) {
85 if (errno != ENOTDIR) {
86 /* Don't output anything if we ignore a symlink */
87 perror("safe_open_ex (safe_open_ex)");
88 }
89 return OPEN_ERROR;
90 }
91
92 /* The +1 is safe because there needs to be at least one character
93 after the first slash (we checked this above). */
94 int result = safe_open_ex(fd, firstslash+1, flags);
95 if (close(fd) == CLOSE_ERROR) {
96 perror("safe_open_ex (close)");
97 return OPEN_ERROR;
98 }
99 return result;
100 }
101
102
103 /**
104 * @brief A version of @c open that is completely symlink-safe when
105 * used with the @c O_NOFOLLOW flag.
106 *
107 * The @c openat function exists to ensure that you can anchor one
108 * path to a particular directory while opening it; however, if you
109 * open "b/c/d" relative to "/a", then even the @c openat function will
110 * still follow symlinks in the "b" component. This can be exploited
111 * by an attacker to make you open the wrong path.
112 *
113 * To avoid that problem, this function uses a recursive
114 * implementation that opens every path from the root, one level at a
115 * time. So "a" is opened relative to "/", and then "b" is opened
116 * relative to "/a", and then "c" is opened relative to "/a/b",
117 * etc. When the @c O_NOFOLLOW flag is used, this approach ensures
118 * that no symlinks in any component are followed.
119 *
120 * @param pathname
121 * The path to the file/directory/whatever whose descriptor you want.
122 *
123 * @param flags
124 * File status flags to be passed to @c openat.
125 *
126 * @return a file descriptor for @c pathname if everything goes well,
127 * and @c OPEN_ERROR if not.
128 */
129 int safe_open(const char* pathname, int flags) {
130 if (pathname == NULL || 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 Determine if a file descriptor has a default ACL.
686 *
687 * @param fd
688 * The file descriptor whose default ACL is in question.
689 *
690 * @return
691 * - @c ACL_SUCCESS - If @c fd has a default ACL.
692 * - @c ACL_FAILURE - If @c fd does not have a default ACL.
693 * - @c ACL_ERROR - Unexpected library error.
694 */
695 int has_default_acl_fd(int fd) {
696 if (fgetxattr(fd, XATTR_NAME_POSIX_ACL_DEFAULT, NULL, 0) == XATTR_ERROR) {
697 if (errno == ENODATA) {
698 return ACL_FAILURE;
699 }
700 perror("has_default_acl_fd (fgetxattr)");
701 return ACL_ERROR;
702 }
703
704 return ACL_SUCCESS;
705 }
706
707
708 /**
709 * @brief Apply parent default ACL to a path.
710 *
711 * This overwrites any existing ACLs on @c path.
712 *
713 * @param path
714 * The path whose ACL we would like to reset to its default.
715 *
716 * @param sp
717 * A pointer to a stat structure for @c path, or @c NULL if you don't
718 * have one handy.
719 *
720 * @param no_exec_mask
721 * The value (either true or false) of the --no-exec-mask flag.
722 *
723 * @return
724 * - @c ACL_SUCCESS - The parent default ACL was inherited successfully.
725 * - @c ACL_FAILURE - If symlinks or hard links are encountered.
726 * - @c ACL_ERROR - Unexpected library error.
727 */
728 int apply_default_acl_ex(const char* path,
729 const struct stat* sp,
730 bool no_exec_mask) {
731
732 if (path == NULL) {
733 errno = EINVAL;
734 perror("apply_default_acl_ex (args)");
735 return ACL_ERROR;
736 }
737
738 /* Define these next three variables here because we may have to
739 * jump to the cleanup routine which expects them to exist.
740 */
741
742 /* Our return value. */
743 int result = ACL_SUCCESS;
744
745 /* The new ACL for this path */
746 acl_t new_acl = (acl_t)NULL;
747
748 /* A copy of new_acl, to be made before we begin mangling new_acl in
749 order to mask the execute bit. */
750 acl_t new_acl_unmasked = (acl_t)NULL;
751
752 /* The file descriptor corresponding to "path" */
753 int fd = 0;
754
755 /* The file descriptor for the directory containing "path" */
756 int parent_fd = 0;
757
758 /* Get the parent directory of "path" with dirname(), which happens
759 * to murder its argument and necessitates a path_copy. */
760 char* path_copy = strdup(path);
761 if (path_copy == NULL) {
762 perror("apply_default_acl_ex (strdup)");
763 return ACL_ERROR;
764 }
765 char* parent = dirname(path_copy);
766 parent_fd = safe_open(parent, O_DIRECTORY | O_NOFOLLOW);
767 if (parent_fd == OPEN_ERROR) {
768 if (errno == ELOOP || errno == ENOTDIR) {
769 /* We hit a symlink, either in the last path component (ELOOP)
770 or higher up (ENOTDIR). */
771 result = ACL_FAILURE;
772 goto cleanup;
773 }
774 else {
775 perror("apply_default_acl_ex (open parent fd)");
776 result = ACL_ERROR;
777 goto cleanup;
778 }
779 }
780
781 /* Check to make sure the parent descriptor actually has a default
782 ACL. If it doesn't, then we can "succeed" immediately. */
783 if (has_default_acl_fd(parent_fd) == ACL_FAILURE) {
784 result = ACL_SUCCESS;
785 goto cleanup;
786 }
787
788 fd = safe_open(path, O_NOFOLLOW);
789 if (fd == OPEN_ERROR) {
790 if (errno == ELOOP || errno == ENOTDIR) {
791 /* We hit a symlink, either in the last path component (ELOOP)
792 or higher up (ENOTDIR). */
793 result = ACL_FAILURE;
794 goto cleanup;
795 }
796 else {
797 perror("apply_default_acl_ex (open fd)");
798 result = ACL_ERROR;
799 goto cleanup;
800 }
801 }
802
803 /* Refuse to operate on hard links, which can be abused by an
804 * attacker to trick us into changing the ACL on a file we didn't
805 * intend to; namely the "target" of the hard link. There is TOCTOU
806 * race condition here, but the window is as small as possible
807 * between when we open the file descriptor (look above) and when we
808 * fstat it.
809 *
810 * Note: we only need to call fstat ourselves if we weren't passed a
811 * valid pointer to a stat structure (nftw does that).
812 */
813 if (sp == NULL) {
814 struct stat s;
815 if (fstat(fd, &s) == STAT_ERROR) {
816 perror("apply_default_acl_ex (fstat)");
817 goto cleanup;
818 }
819
820 sp = &s;
821 }
822
823 if (!S_ISDIR(sp->st_mode)) {
824 /* If it's not a directory, make sure it's a regular,
825 non-hard-linked file. */
826 if (!S_ISREG(sp->st_mode) || sp->st_nlink != 1) {
827 result = ACL_FAILURE;
828 goto cleanup;
829 }
830 }
831
832
833 /* Default to not masking the exec bit; i.e. applying the default
834 ACL literally. If --no-exec-mask was not specified, then we try
835 to "guess" whether or not to mask the exec bit. This behavior
836 is modeled after the capital 'X' perms of setfacl. */
837 bool allow_exec = true;
838
839 if (!no_exec_mask) {
840 /* Never mask the execute bit on directories. */
841 int ace_result = any_can_execute(fd,sp) || S_ISDIR(sp->st_mode);
842
843 if (ace_result == ACL_ERROR) {
844 perror("apply_default_acl_ex (any_can_execute)");
845 result = ACL_ERROR;
846 goto cleanup;
847 }
848
849 allow_exec = (bool)ace_result;
850 }
851
852 if (wipe_acls(fd) == ACL_ERROR) {
853 perror("apply_default_acl_ex (wipe_acls)");
854 result = ACL_ERROR;
855 goto cleanup;
856 }
857
858 /* If it's a directory, inherit the parent's default. */
859 if (S_ISDIR(sp->st_mode)) {
860 if (acl_copy_xattr(parent_fd,
861 ACL_TYPE_DEFAULT,
862 fd,
863 ACL_TYPE_DEFAULT) == ACL_ERROR) {
864 perror("apply_default_acl_ex (acl_copy_xattr default)");
865 result = ACL_ERROR;
866 goto cleanup;
867 }
868 }
869
870 /* If it's anything, _apply_ the parent's default. */
871 if (acl_copy_xattr(parent_fd,
872 ACL_TYPE_DEFAULT,
873 fd,
874 ACL_TYPE_ACCESS) == ACL_ERROR) {
875 perror("apply_default_acl_ex (acl_copy_xattr access)");
876 result = ACL_ERROR;
877 goto cleanup;
878 }
879
880 /* There's a good reason why we saved the ACL above, even though
881 * we're about tto read it back into memory and mess with it on the
882 * next line. The acl_copy_xattr() function is already a hack to let
883 * us copy default ACLs without resorting to path names; we simply
884 * have no way to read the parent's default ACL into memory using
885 * parent_fd. We can, however, copy the parent's ACL to a file (with
886 * acl_copy_xattr), and then read the ACL from a file using
887 * "fd". It's quite the circus, but it works and should be safe from
888 * sym/hardlink attacks.
889 */
890
891 /* Now we potentially need to mask the execute permissions in the
892 ACL on fd; or maybe now. */
893 if (allow_exec) {
894 goto cleanup;
895 }
896
897 /* OK, we need to mask some execute permissions. First obtain the
898 current ACL... */
899 new_acl = acl_get_fd(fd);
900 if (new_acl == (acl_t)NULL) {
901 perror("apply_default_acl_ex (acl_get_fd)");
902 result = ACL_ERROR;
903 goto cleanup;
904 }
905
906 /* ...and now make a copy of it, because otherwise when we loop
907 below, some shit gets stuck (modifying the structure while
908 looping over it no worky). */
909 new_acl_unmasked = acl_dup(new_acl);
910 if (new_acl_unmasked == (acl_t)NULL) {
911 perror("apply_default_acl_ex (acl_dup)");
912 result = ACL_ERROR;
913 goto cleanup;
914 }
915
916 acl_entry_t entry;
917 int ge_result = acl_get_entry(new_acl_unmasked, ACL_FIRST_ENTRY, &entry);
918
919 while (ge_result == ACL_SUCCESS) {
920 acl_tag_t tag = ACL_UNDEFINED_TAG;
921
922 if (acl_get_tag_type(entry, &tag) == ACL_ERROR) {
923 perror("apply_default_acl_ex (acl_get_tag_type)");
924 result = ACL_ERROR;
925 goto cleanup;
926 }
927
928
929 /* We've got an entry/tag from the default ACL. Get its permset. */
930 acl_permset_t permset;
931 if (acl_get_permset(entry, &permset) == ACL_ERROR) {
932 perror("apply_default_acl_ex (acl_get_permset)");
933 result = ACL_ERROR;
934 goto cleanup;
935 }
936
937 if (tag == ACL_MASK ||
938 tag == ACL_USER_OBJ ||
939 tag == ACL_GROUP_OBJ ||
940 tag == ACL_OTHER) {
941
942 /* The mask doesn't affect acl_user_obj, acl_group_obj (in
943 minimal ACLs) or acl_other entries, so if execute should be
944 masked, we have to do it manually. */
945 if (acl_delete_perm(permset, ACL_EXECUTE) == ACL_ERROR) {
946 perror("apply_default_acl_ex (acl_delete_perm)");
947 result = ACL_ERROR;
948 goto cleanup;
949 }
950
951 if (acl_set_permset(entry, permset) == ACL_ERROR) {
952 perror("apply_default_acl_ex (acl_set_permset)");
953 result = ACL_ERROR;
954 goto cleanup;
955 }
956 }
957
958 /* Finally, add the permset to the access ACL. It's actually
959 * important that we pass in the address of "new_acl" here, and not
960 * "new_acl" itself. Why? The call to acl_create_entry() within
961 * acl_set_entry() can allocate new memory for the entry.
962 * Sometimes that can be done in-place, in which case everything
963 * is cool and the new memory gets released when we call
964 * acl_free(acl).
965 *
966 * But occasionally, the whole ACL structure will have to be moved
967 * in order to allocate the extra space. When that happens,
968 * acl_create_entry() modifies the pointer it was passed (in this
969 * case, &acl) to point to the new location. We want to call
970 * acl_free() on the new location, and since acl_free() gets
971 * called right here, we need acl_create_entry() to update the
972 * value of "new_acl". To do that, it needs the address of "new_acl".
973 */
974
975 if (acl_set_entry(&new_acl, entry) == ACL_ERROR) {
976 perror("apply_default_acl_ex (acl_set_entry)");
977 result = ACL_ERROR;
978 goto cleanup;
979 }
980
981 ge_result = acl_get_entry(new_acl_unmasked, ACL_NEXT_ENTRY, &entry);
982 }
983
984 /* Catches the first acl_get_entry as well as the ones at the end of
985 the loop. */
986 if (ge_result == ACL_ERROR) {
987 perror("apply_default_acl_ex (acl_get_entry)");
988 result = ACL_ERROR;
989 goto cleanup;
990 }
991
992 if (acl_set_fd(fd, new_acl) == ACL_ERROR) {
993 perror("apply_default_acl_ex (acl_set_fd)");
994 result = ACL_ERROR;
995 goto cleanup;
996 }
997
998 cleanup:
999 free(path_copy);
1000 if (new_acl != (acl_t)NULL) {
1001 acl_free(new_acl);
1002 }
1003 if (new_acl_unmasked != (acl_t)NULL) {
1004 acl_free(new_acl_unmasked);
1005 }
1006 if (fd > 0 && close(fd) == CLOSE_ERROR) {
1007 perror("apply_default_acl_ex (close fd)");
1008 result = ACL_ERROR;
1009 }
1010 if (parent_fd > 0 && close(parent_fd) == CLOSE_ERROR) {
1011 perror("apply_default_acl_ex (close parent_fd)");
1012 result = ACL_ERROR;
1013 }
1014 return result;
1015 }
1016
1017
1018
1019 /**
1020 * @brief The friendly interface to @c apply_default_acl_ex.
1021 *
1022 * The @c apply_default_acl_ex function holds the real implementation
1023 * of this function, but it takes a weird second argument that most
1024 * people won't care about (a stat structure). But, we use that
1025 * argument for the recursive mode of the CLI, so it's there.
1026 *
1027 * If you don't have a stat structure for your @c path, use this instead.
1028 *
1029 * @param path
1030 * The path whose ACL we would like to reset to its default.
1031 *
1032 * @param no_exec_mask
1033 * The value (either true or false) of the --no-exec-mask flag.
1034 *
1035 * @return
1036 * - @c ACL_SUCCESS - The parent default ACL was inherited successfully.
1037 * - @c ACL_FAILURE - If symlinks or hard links are encountered.
1038 * or the parent of @c path is not a directory.
1039 * - @c ACL_ERROR - Unexpected library error.
1040 */
1041 int apply_default_acl(const char* path, bool no_exec_mask) {
1042 return apply_default_acl_ex(path, NULL, no_exec_mask);
1043 }