1
0
Fork 0
forked from soccera/rdo

Close directory fd + openat -> open

This commit is contained in:
sw1tchbl4d3 2021-07-16 08:13:59 +02:00
parent 5ba40256a0
commit ea15c5631a

View file

@ -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,16 +80,12 @@ 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;
@ -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;