Skip to content

Commit

Permalink
Don't die when is_privileged_executable fails
Browse files Browse the repository at this point in the history
Resolves #3894
  • Loading branch information
rocallahan committed Dec 29, 2024
1 parent db5faf8 commit 6731b72
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,8 @@ set(BASIC_TESTS
fd_cleanup
fd_tracking_across_threads
fds_clean
fexecve
fexecve_memfd
flock
flock_ofd
flock2
Expand Down
13 changes: 8 additions & 5 deletions src/record_syscall.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5566,12 +5566,15 @@ static bool is_privileged_executable(RecordTask* t, const string& path) {
return true;
}
} else {
ASSERT(t, errno == ENODATA || errno == ENOTSUP);
struct stat buf;
stat(path.c_str(), &buf);
if (buf.st_mode & (S_ISUID | S_ISGID)) {
return true;
if (errno == ENOENT) {
return false;
}
ASSERT(t, errno == ENODATA || errno == ENOTSUP);
}
struct stat buf;
stat(path.c_str(), &buf);
if (buf.st_mode & (S_ISUID | S_ISGID)) {
return true;
}
return false;
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/fexecve.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* -*- Mode: C; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil; -*- */

#include "util.h"

int main(int argc, char* argv[]) {
test_assert(argc == 1 || (argc == 2 && !strcmp("self", argv[1])));

if (argc != 2) {
int fd = open("/proc/self/exe", O_RDONLY);
test_assert(fd >= 0);
char* new_args[] = { argv[0], "self", NULL };
int ret = syscall(RR_execveat, fd, "", new_args, environ, AT_EMPTY_PATH);
if (ret < 0 && errno == ENOSYS) {
atomic_puts("execveat not supported, skipping test");
atomic_puts("EXIT-SUCCESS");
return 0;
}
test_assert("Not reached" && 0);
}

atomic_puts("EXIT-SUCCESS");
return 0;
}
35 changes: 35 additions & 0 deletions src/test/fexecve_memfd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* -*- Mode: C; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil; -*- */

#include "util.h"

static void copy_file(int from_fd, int to_fd) {
char buf[4096];
ssize_t ret;

while ((ret = read(from_fd, buf, sizeof(buf))) > 0) {
test_assert(write(to_fd, buf, ret) == ret);
}
test_assert(ret == 0);
}

int main(int argc, char* argv[]) {
test_assert(argc == 1 || (argc == 2 && !strcmp("self", argv[1])));

if (argc != 2) {
int fd = open("/proc/self/exe", O_RDONLY);
test_assert(fd >= 0);
int memfd = syscall(RR_memfd_create, "test", 0);
copy_file(fd, memfd);
char* new_args[] = { argv[0], "self", NULL };
int ret = syscall(RR_execveat, memfd, "", new_args, environ, AT_EMPTY_PATH);
if (ret < 0 && errno == ENOSYS) {
atomic_puts("execveat not supported, skipping test");
atomic_puts("EXIT-SUCCESS");
return 0;
}
test_assert("Not reached" && 0);
}

atomic_puts("EXIT-SUCCESS");
return 0;
}

0 comments on commit 6731b72

Please sign in to comment.