From 160af9319799a4200c595a7472c18c094b777440 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Sun, 18 Sep 2016 18:03:06 +0000 Subject: [PATCH] Better error checking if getcwd fails: just ignore it and do not try to adding to the list of possible path where to find the files. if fdopen fails, warn and return NULL the rest of the code knows how to deal with it Reported by: oshogbo --- usr.bin/soelim/soelim.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/usr.bin/soelim/soelim.c b/usr.bin/soelim/soelim.c index 9fe2418cad4c..c9ac69c5329e 100644 --- a/usr.bin/soelim/soelim.c +++ b/usr.bin/soelim/soelim.c @@ -68,6 +68,7 @@ relpath(const char *path) static FILE * soelim_fopen(int rootfd, const char *name) { + FILE *f = NULL; char path[PATH_MAX]; size_t i; int fd; @@ -75,8 +76,10 @@ soelim_fopen(int rootfd, const char *name) if (strcmp(name, "-") == 0) return (stdin); - if ((fd = openat(rootfd, relpath(name), O_RDONLY)) != -1) - return (fdopen(fd, "r")); + if ((fd = openat(rootfd, relpath(name), O_RDONLY)) != -1) { + f = fdopen(fd, "r"); + goto out; + } if (*name == '/') { warn("can't open '%s'", name); @@ -86,13 +89,17 @@ soelim_fopen(int rootfd, const char *name) for (i = 0; i < includes->sl_cur; i++) { snprintf(path, sizeof(path), "%s/%s", includes->sl_str[i], name); - if ((fd = openat(rootfd, relpath(path), O_RDONLY)) != -1) - return (fdopen(fd, "r")); + if ((fd = openat(rootfd, relpath(path), O_RDONLY)) != -1) { + f = fdopen(fd, "r"); + break; + } } - warn("can't open '%s'", name); +out: + if (f == NULL) + warn("can't open '%s'", name); - return (NULL); + return (f); } static int @@ -157,7 +164,9 @@ main(int argc, char **argv) cap_rights_t rights; includes = sl_init(); - sl_add(includes, getcwd(cwd, sizeof(cwd))); + if (getcwd(cwd, sizeof(cwd)) != NULL) + sl_add(includes, cwd); + if (includes == NULL) err(EXIT_FAILURE, "sl_init()"); @@ -196,6 +205,8 @@ main(int argc, char **argv) if (cap_rights_limit(STDERR_FILENO, &rights) < 0 && errno != ENOSYS) err(EXIT_FAILURE, "unable to limit rights for stderr"); rootfd = open("/", O_DIRECTORY | O_RDONLY); + if (rootfd == -1) + err(EXIT_FAILURE, "unable to open '/'"); cap_rights_init(&rights, CAP_READ, CAP_LOOKUP, CAP_FSTAT, CAP_FCNTL); if (cap_rights_limit(rootfd, &rights) < 0 && errno != ENOSYS) err(EXIT_FAILURE, "unable to limit rights");