From 66e2cb0c42a40bfd3ab72d19770668f45704dd48 Mon Sep 17 00:00:00 2001 From: remoof Date: Fri, 16 Jul 2021 23:55:59 +0200 Subject: [PATCH 1/3] Restructured ensuredir and other small fixes --- sessions.h | 51 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/sessions.h b/sessions.h index 6150515..de18583 100644 --- a/sessions.h +++ b/sessions.h @@ -1,4 +1,4 @@ -#pragma once +#pragma once #include #include @@ -14,12 +14,16 @@ int getpstartts(int pid, unsigned long long* startts) { snprintf(path, sizeof(path), "/proc/%d/stat", pid); int fd = open(path, O_RDONLY); - if (fd < 0) + if (fd < 0) { + close(fd); err(1, "Could not open %s", path); + } int bytes_read = read(fd, fc, sizeof(fc)); - if (bytes_read < 0) + if (bytes_read < 0) { + close(fd); err(1, "Could not read %s", path); + } fc[bytes_read] = '\0'; close(fd); @@ -31,7 +35,7 @@ int getpstartts(int pid, unsigned long long* startts) { char* token = strtok(ptr, " "); - for (short i = 0; i<20 && token; i++) + for (int i = 0; i<20 && token; i++) token = strtok(NULL, " "); if (!token) @@ -54,22 +58,28 @@ int ensuredir(int recur) { if (fd < 0) { if (errno == ENOENT) { - if (mkdir("/run/rdo", 0700) < 0) + if (mkdir("/run/rdo", 0700) < 0) { + close(fd); err(1, "Could not create /run/rdo"); + } close(fd); return ensuredir(++recur); - } - else + } else { + close(fd); err(1, "Could not open /run/rdo"); - } else { - if (fstat(fd, &st) < 0) - err(1, "Could not fstat /run/rdo"); + } + } + if (fstat(fd, &st) < 0) { close(fd); - if (st.st_uid != 0 || st.st_mode != (0700 | S_IFDIR)) - return -1; + err(1, "Could not fstat /run/rdo"); } + close(fd); + + if (st.st_uid != 0 || st.st_mode != (0700 | S_IFDIR)) + return -1; + return 0; } @@ -87,15 +97,20 @@ void setsession(int pid, unsigned int ts_ttl) { int fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0700); if (fd < 0) { - if (errno == EEXIST) + if (errno == EEXIST) { + close(fd); return; + } + close(fd); 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) + if (write(fd, ts_str, strlen(ts_str)) < 0) { + close(fd); err(1, "Could not write to %s", path); + } close(fd); @@ -116,12 +131,18 @@ int getsession(int pid, unsigned int ts_ttl) { int fd = open(path, O_RDONLY); if (fd < 0) { - if (errno == ENOENT) + if (errno == ENOENT) { + close(fd); return -1; + } + close(fd); err(1, "Could not open %s", path); } int bytes_read = read(fd, ts_str, sizeof(ts_str)); + + close(fd); + if (bytes_read < 0) err(1, "Could not read %s", path); ts_str[bytes_read] = '\0'; From d44301d8fa5f3c6748ff22bc001231632387a7b5 Mon Sep 17 00:00:00 2001 From: remoof Date: Sat, 17 Jul 2021 15:31:18 +0200 Subject: [PATCH 2/3] Close file descriptors earlier --- sessions.h | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/sessions.h b/sessions.h index de18583..13c4fc3 100644 --- a/sessions.h +++ b/sessions.h @@ -20,14 +20,14 @@ int getpstartts(int pid, unsigned long long* startts) { } int bytes_read = read(fd, fc, sizeof(fc)); - if (bytes_read < 0) { - close(fd); - err(1, "Could not read %s", path); - } - fc[bytes_read] = '\0'; close(fd); + if (bytes_read < 0) + err(1, "Could not read %s", path); + + fc[bytes_read] = '\0'; + if (memchr(ptr, '\0', bytes_read) != NULL) return -1; @@ -57,17 +57,14 @@ int ensuredir(int recur) { int fd = open("/run/rdo", O_RDONLY, O_DIRECTORY | O_NOFOLLOW); if (fd < 0) { + close(fd); if (errno == ENOENT) { - if (mkdir("/run/rdo", 0700) < 0) { - close(fd); + if (mkdir("/run/rdo", 0700) < 0) err(1, "Could not create /run/rdo"); - } - close(fd); return ensuredir(++recur); - } else { - close(fd); - err(1, "Could not open /run/rdo"); } + else + err(1, "Could not open /run/rdo"); } if (fstat(fd, &st) < 0) { @@ -97,11 +94,9 @@ void setsession(int pid, unsigned int ts_ttl) { int fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0700); if (fd < 0) { - if (errno == EEXIST) { - close(fd); - return; - } close(fd); + if (errno == EEXIST) + return; err(1, "Could not open %s", path); } @@ -131,11 +126,9 @@ int getsession(int pid, unsigned int ts_ttl) { int fd = open(path, O_RDONLY); if (fd < 0) { - if (errno == ENOENT) { - close(fd); - return -1; - } close(fd); + if (errno == ENOENT) + return -1; err(1, "Could not open %s", path); } From df1cb0d70cfd46cef3156ac38d0b5d802ec16418 Mon Sep 17 00:00:00 2001 From: remoof Date: Sat, 17 Jul 2021 15:54:39 +0200 Subject: [PATCH 3/3] Avoid closing on bad file descriptors --- sessions.h | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/sessions.h b/sessions.h index 13c4fc3..19cf679 100644 --- a/sessions.h +++ b/sessions.h @@ -14,10 +14,8 @@ int getpstartts(int pid, unsigned long long* startts) { snprintf(path, sizeof(path), "/proc/%d/stat", pid); int fd = open(path, O_RDONLY); - if (fd < 0) { - close(fd); + if (fd < 0) err(1, "Could not open %s", path); - } int bytes_read = read(fd, fc, sizeof(fc)); @@ -57,7 +55,6 @@ int ensuredir(int recur) { int fd = open("/run/rdo", O_RDONLY, O_DIRECTORY | O_NOFOLLOW); if (fd < 0) { - close(fd); if (errno == ENOENT) { if (mkdir("/run/rdo", 0700) < 0) err(1, "Could not create /run/rdo"); @@ -94,7 +91,6 @@ void setsession(int pid, unsigned int ts_ttl) { int fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0700); if (fd < 0) { - close(fd); if (errno == EEXIST) return; err(1, "Could not open %s", path); @@ -126,7 +122,6 @@ int getsession(int pid, unsigned int ts_ttl) { int fd = open(path, O_RDONLY); if (fd < 0) { - close(fd); if (errno == ENOENT) return -1; err(1, "Could not open %s", path);