Skip to content

Commit

Permalink
selftests/memfd: add tests for F_SEAL_EXEC
Browse files Browse the repository at this point in the history
Basic tests to ensure that user/group/other execute bits cannot be changed
after applying F_SEAL_EXEC to a memfd.

Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Daniel Verkamp <[email protected]>
Co-developed-by: Jeff Xu <[email protected]>
Signed-off-by: Jeff Xu <[email protected]>
Reviewed-by: Kees Cook <[email protected]>
Cc: David Herrmann <[email protected]>
Cc: Dmitry Torokhov <[email protected]>
Cc: Hugh Dickins <[email protected]>
Cc: Jann Horn <[email protected]>
Cc: Jorge Lucangeli Obes <[email protected]>
Cc: kernel test robot <[email protected]>
Cc: Shuah Khan <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
danielverkamp authored and akpm00 committed Jan 19, 2023
1 parent 6fd7353 commit 32d118a
Showing 1 changed file with 122 additions and 1 deletion.
123 changes: 122 additions & 1 deletion tools/testing/selftests/memfd/memfd_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,38 @@
#define MFD_DEF_SIZE 8192
#define STACK_SIZE 65536

#define F_SEAL_EXEC 0x0020

/*
* Default is not to test hugetlbfs
*/
static size_t mfd_def_size = MFD_DEF_SIZE;
static const char *memfd_str = MEMFD_STR;

static ssize_t fd2name(int fd, char *buf, size_t bufsize)
{
char buf1[PATH_MAX];
int size;
ssize_t nbytes;

size = snprintf(buf1, PATH_MAX, "/proc/self/fd/%d", fd);
if (size < 0) {
printf("snprintf(%d) failed on %m\n", fd);
abort();
}

/*
* reserver one byte for string termination.
*/
nbytes = readlink(buf1, buf, bufsize-1);
if (nbytes == -1) {
printf("readlink(%s) failed %m\n", buf1);
abort();
}
buf[nbytes] = '\0';
return nbytes;
}

static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
{
int r, fd;
Expand Down Expand Up @@ -98,11 +124,14 @@ static unsigned int mfd_assert_get_seals(int fd)

static void mfd_assert_has_seals(int fd, unsigned int seals)
{
char buf[PATH_MAX];
int nbytes;
unsigned int s;
fd2name(fd, buf, PATH_MAX);

s = mfd_assert_get_seals(fd);
if (s != seals) {
printf("%u != %u = GET_SEALS(%d)\n", seals, s, fd);
printf("%u != %u = GET_SEALS(%s)\n", seals, s, buf);
abort();
}
}
Expand Down Expand Up @@ -594,6 +623,64 @@ static void mfd_fail_grow_write(int fd)
}
}

static void mfd_assert_mode(int fd, int mode)
{
struct stat st;
char buf[PATH_MAX];
int nbytes;

fd2name(fd, buf, PATH_MAX);

if (fstat(fd, &st) < 0) {
printf("fstat(%s) failed: %m\n", buf);
abort();
}

if ((st.st_mode & 07777) != mode) {
printf("fstat(%s) wrong file mode 0%04o, but expected 0%04o\n",
buf, (int)st.st_mode & 07777, mode);
abort();
}
}

static void mfd_assert_chmod(int fd, int mode)
{
char buf[PATH_MAX];
int nbytes;

fd2name(fd, buf, PATH_MAX);

if (fchmod(fd, mode) < 0) {
printf("fchmod(%s, 0%04o) failed: %m\n", buf, mode);
abort();
}

mfd_assert_mode(fd, mode);
}

static void mfd_fail_chmod(int fd, int mode)
{
struct stat st;
char buf[PATH_MAX];
int nbytes;

fd2name(fd, buf, PATH_MAX);

if (fstat(fd, &st) < 0) {
printf("fstat(%s) failed: %m\n", buf);
abort();
}

if (fchmod(fd, mode) == 0) {
printf("fchmod(%s, 0%04o) didn't fail as expected\n",
buf, mode);
abort();
}

/* verify that file mode bits did not change */
mfd_assert_mode(fd, st.st_mode & 07777);
}

static int idle_thread_fn(void *arg)
{
sigset_t set;
Expand Down Expand Up @@ -880,6 +967,39 @@ static void test_seal_resize(void)
close(fd);
}

/*
* Test SEAL_EXEC
* Test that chmod() cannot change x bits after sealing
*/
static void test_seal_exec(void)
{
int fd;

printf("%s SEAL-EXEC\n", memfd_str);

fd = mfd_assert_new("kern_memfd_seal_exec",
mfd_def_size,
MFD_CLOEXEC | MFD_ALLOW_SEALING);

mfd_assert_mode(fd, 0777);

mfd_assert_chmod(fd, 0644);

mfd_assert_has_seals(fd, 0);
mfd_assert_add_seals(fd, F_SEAL_EXEC);
mfd_assert_has_seals(fd, F_SEAL_EXEC);

mfd_assert_chmod(fd, 0600);
mfd_fail_chmod(fd, 0777);
mfd_fail_chmod(fd, 0670);
mfd_fail_chmod(fd, 0605);
mfd_fail_chmod(fd, 0700);
mfd_fail_chmod(fd, 0100);
mfd_assert_chmod(fd, 0666);

close(fd);
}

/*
* Test sharing via dup()
* Test that seals are shared between dupped FDs and they're all equal.
Expand Down Expand Up @@ -1059,6 +1179,7 @@ int main(int argc, char **argv)
test_seal_shrink();
test_seal_grow();
test_seal_resize();
test_seal_exec();

test_share_dup("SHARE-DUP", "");
test_share_mmap("SHARE-MMAP", "");
Expand Down

0 comments on commit 32d118a

Please sign in to comment.