Skip to content

Commit

Permalink
Allow invalid memfd_create *name arguments
Browse files Browse the repository at this point in the history
Some applications might call it with e.g. NULL to check for memfd_create support, in these cases EFAULT should be returned
  • Loading branch information
usatiuk authored and rocallahan committed Apr 26, 2024
1 parent 5b7c1f4 commit 0fd5efb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,7 @@ set(BASIC_TESTS
map_shared_syscall
membarrier
memfd_create
memfd_create_efault
memfd_create_shared
memfd_create_shared_huge
mincore
Expand Down
7 changes: 5 additions & 2 deletions src/record_syscall.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4383,8 +4383,11 @@ static Switchable rec_prepare_syscall_arch(RecordTask* t,
}

case Arch::memfd_create: {
string name = t->read_c_str(remote_ptr<char>(regs.arg1()));
if (is_blacklisted_memfd(name.c_str())) {
bool ok = true;
string name = t->read_c_str(remote_ptr<char>(regs.arg1()), &ok);
if (!ok) {
syscall_state.expect_errno = EFAULT;
} else if (is_blacklisted_memfd(name.c_str())) {
LOG(warn) << "Cowardly refusing to memfd_create " << name;
Registers r = regs;
r.set_arg1(0);
Expand Down
19 changes: 19 additions & 0 deletions src/test/memfd_create_efault.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* -*- Mode: C; tab-width: 8; c-basic-offset: 2; indent-tabs-mode: nil; -*- */

#include "util.h"

int main(void) {
int fd;

/* There's no libc helper for this syscall. */
fd = syscall(RR_memfd_create, NULL, 0);
if (ENOSYS == errno) {
atomic_puts("SYS_memfd_create not supported on this kernel");
} else {
test_assert(fd == -1);
test_assert(errno == EFAULT);
}

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

0 comments on commit 0fd5efb

Please sign in to comment.