From d88c233404f2d01cee18502d263289fc40bea109 Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Fri, 26 Apr 2024 21:59:51 +0200 Subject: [PATCH] Allow invalid memfd_create *name arguments Some applications might call it with e.g. NULL to check for memfd_create support, in these cases EFAULT should be returned --- CMakeLists.txt | 1 + src/record_syscall.cc | 7 +++++-- src/test/memfd_create_efault.c | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/test/memfd_create_efault.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 31e291ae182..ed31fdd58b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1096,6 +1096,7 @@ set(BASIC_TESTS map_shared_syscall membarrier memfd_create + memfd_create_efault memfd_create_shared memfd_create_shared_huge mincore diff --git a/src/record_syscall.cc b/src/record_syscall.cc index 0e653c8ddd8..49e386324dc 100644 --- a/src/record_syscall.cc +++ b/src/record_syscall.cc @@ -4383,8 +4383,11 @@ static Switchable rec_prepare_syscall_arch(RecordTask* t, } case Arch::memfd_create: { - string name = t->read_c_str(remote_ptr(regs.arg1())); - if (is_blacklisted_memfd(name.c_str())) { + bool ok = true; + string name = t->read_c_str(remote_ptr(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); diff --git a/src/test/memfd_create_efault.c b/src/test/memfd_create_efault.c new file mode 100644 index 00000000000..c777dbb02dd --- /dev/null +++ b/src/test/memfd_create_efault.c @@ -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; +}