X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2Flibadacl.c;h=246f30aef1852f1dee3f90b26c4a50cb98a501e9;hb=f9e643a6aaf9d03fd6c36eb0ad953f2563a7c017;hp=21b8141cbbc0158a3c6cfce3e985838bed1e20ce;hpb=aaf323d45b5c2043acd64b7e6b8b1fea1664dd0b;p=apply-default-acl.git diff --git a/src/libadacl.c b/src/libadacl.c index 21b8141..246f30a 100644 --- a/src/libadacl.c +++ b/src/libadacl.c @@ -8,7 +8,7 @@ /* Enables get_current_dir_name() in unistd.h */ #define _GNU_SOURCE -#include /* ELOOP, EINVAL, etc. */ +#include /* EINVAL, ELOOP, ENOTDIR, etc. */ #include /* openat() */ #include /* basename(), dirname() */ #include /* PATH_MAX */ @@ -53,33 +53,39 @@ * and @c OPEN_ERROR if not. */ int safe_open_ex(int at_fd, char* pathname, int flags) { - if (pathname != NULL && strlen(pathname) == 0) { - /* Oops, went one level to deep with nothing to do. */ - return at_fd; + if (pathname == NULL) { + errno = EINVAL; + perror("safe_open_ex (args)"); + return OPEN_ERROR; } char* firstslash = strchr(pathname, '/'); if (firstslash == NULL) { /* No more slashes, this is the base case. */ - int r = openat(at_fd, pathname, flags); - return r; + return openat(at_fd, pathname, flags); + } + else if (firstslash[1] == '\0') { + /* The first slash is the last character; ensure that we open + a directory. */ + firstslash[0] = '\0'; + return openat(at_fd, pathname, flags | O_DIRECTORY); } - /* Temporarily disable the slash, so that the subsequent call to - openat() opens only the next directory (and doesn't recurse). */ + /* The first slash exists and isn't the last character in the path, + so we can split the path wherever that first slash lies and + recurse. */ *firstslash = '\0'; - int fd = safe_open_ex(at_fd, pathname, flags); + int fd = openat(at_fd, pathname, flags | O_DIRECTORY | O_PATH); if (fd == OPEN_ERROR) { - if (errno != ELOOP) { + if (errno != ENOTDIR) { /* Don't output anything if we ignore a symlink */ perror("safe_open_ex (safe_open_ex)"); } return OPEN_ERROR; } - /* The ++ is safe because there needs to be at least a null byte - after the first slash, even if it's the last real character in - the string. */ + /* The +1 is safe because there needs to be at least one character + after the first slash (we checked this above). */ int result = safe_open_ex(fd, firstslash+1, flags); if (close(fd) == CLOSE_ERROR) { perror("safe_open_ex (close)"); @@ -159,7 +165,14 @@ int safe_open(const char* pathname, int flags) { return OPEN_ERROR; } - int fd = open("/", flags); + int fd = 0; + if (strcmp(abspath, "/") == 0) { + fd = open("/", flags | O_DIRECTORY); + } + else { + /* Use O_PATH for some added safety if "/" is not our target */ + fd = open("/", flags | O_DIRECTORY | O_PATH); + } if (fd == OPEN_ERROR) { perror("safe_open (open)"); return OPEN_ERROR; @@ -205,6 +218,11 @@ int safe_open(const char* pathname, int flags) { * */ int acl_set_entry(acl_t* aclp, acl_entry_t entry) { + if (aclp == NULL || entry == NULL) { + errno = EINVAL; + perror("acl_set_entry (args)"); + return ACL_ERROR; + } acl_tag_t entry_tag; if (acl_get_tag_type(entry, &entry_tag) == ACL_ERROR) { @@ -353,6 +371,11 @@ int acl_entry_count(acl_t acl) { * - @c ACL_ERROR - Unexpected library error */ int acl_is_minimal(acl_t acl) { + if (acl == NULL) { + errno = EINVAL; + perror("acl_is_minimal (args)"); + return ACL_ERROR; + } int ec = acl_entry_count(acl); @@ -383,6 +406,11 @@ int acl_is_minimal(acl_t acl) { * - @c ACL_ERROR - Unexpected library error. */ int acl_execute_masked(acl_t acl) { + if (acl == NULL) { + errno = EINVAL; + perror("acl_execute_masked (args)"); + return ACL_ERROR; + } acl_entry_t entry; int ge_result = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry); @@ -446,6 +474,12 @@ int acl_execute_masked(acl_t acl) { * - @c ACL_ERROR - Unexpected library error. */ int any_can_execute(int fd, const struct stat* sp) { + if (sp == NULL) { + errno = EINVAL; + perror("any_can_execute (args)"); + return ACL_ERROR; + } + acl_t acl = acl_get_fd(fd); if (acl == (acl_t)NULL) { @@ -543,8 +577,7 @@ int any_can_execute(int fd, const struct stat* sp) { * - @c ACL_ERROR - Unexpected library error. */ int assign_default_acl(const char* path, acl_t acl) { - - if (path == NULL) { + if (path == NULL || acl == NULL) { errno = EINVAL; perror("assign_default_acl (args)"); return ACL_ERROR; @@ -658,8 +691,10 @@ int apply_default_acl_ex(const char* path, fd = safe_open(path, O_NOFOLLOW); if (fd == OPEN_ERROR) { - if (errno == ELOOP) { - result = ACL_FAILURE; /* hit a symlink */ + if (errno == ELOOP || errno == ENOTDIR) { + /* We hit a symlink, either in the last path component (ELOOP) + or higher up (ENOTDIR). */ + result = ACL_FAILURE; goto cleanup; } else {