From ea15c5631a1289730dcf9ecca971ecd2087c3343 Mon Sep 17 00:00:00 2001 From: sw1tchbl4d3 Date: Fri, 16 Jul 2021 08:13:59 +0200 Subject: [PATCH] Close directory fd + openat -> open --- sessions.h | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/sessions.h b/sessions.h index 5819417..6150515 100644 --- a/sessions.h +++ b/sessions.h @@ -45,9 +45,9 @@ int getpstartts(int pid, unsigned long long* startts) { return 0; } -int gethandle(int recur) { +int ensuredir(int recur) { if (recur >= 2) - errx(1, "Too many recursions in gethandle()"); + errx(1, "Too many recursions in ensuredir()"); struct stat st; int fd = open("/run/rdo", O_RDONLY, O_DIRECTORY | O_NOFOLLOW); @@ -56,7 +56,8 @@ int gethandle(int recur) { if (errno == ENOENT) { if (mkdir("/run/rdo", 0700) < 0) err(1, "Could not create /run/rdo"); - return gethandle(++recur); + close(fd); + return ensuredir(++recur); } else err(1, "Could not open /run/rdo"); @@ -64,11 +65,12 @@ int gethandle(int recur) { if (fstat(fd, &st) < 0) err(1, "Could not fstat /run/rdo"); + close(fd); if (st.st_uid != 0 || st.st_mode != (0700 | S_IFDIR)) return -1; } - return fd; + return 0; } void setsession(int pid, unsigned int ts_ttl) { @@ -78,22 +80,18 @@ void setsession(int pid, unsigned int ts_ttl) { unsigned long long startts; char path[1024], ts_str[32]; - int dirfd = gethandle(0); - if (dirfd < 0 && errno == 0) - return; - - if (getpstartts(pid, &startts) < 0) + if (ensuredir(0) < 0 || getpstartts(pid, &startts) < 0) return; snprintf(path, sizeof(path), "/run/rdo/%d-%llu", pid, startts); - int fd = openat(dirfd, path, O_CREAT | O_EXCL | O_WRONLY, 0700); + int fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0700); if (fd < 0) { if (errno == EEXIST) return; err(1, "Could not open %s", path); } - + snprintf(ts_str, sizeof(ts_str), "%llu", (unsigned long long)time(NULL)); if (write(fd, ts_str, strlen(ts_str)) < 0) @@ -111,16 +109,12 @@ int getsession(int pid, unsigned int ts_ttl) { unsigned long long startts, current; char path[1024], ts_str[32]; - int dirfd = gethandle(0); - if (dirfd < 0 && errno == 0) - return -1; - - if (getpstartts(pid, &startts) < 0) + if (ensuredir(0) < 0 || getpstartts(pid, &startts) < 0) return -1; snprintf(path, sizeof(path), "/run/rdo/%d-%llu", pid, startts); - int fd = openat(dirfd, path, O_RDONLY); + int fd = open(path, O_RDONLY); if (fd < 0) { if (errno == ENOENT) return -1;