Skip to content

Commit

Permalink
tests: test for setns() EINVAL regression
Browse files Browse the repository at this point in the history
Verify that setns() reports EINVAL when an fd is passed that refers to an
open file but the file is not a file descriptor useable to interact with
namespaces.

Cc: Jan Stancek <jstancek@redhat.com>
Cc: Cyril Hrubis <chrubis@suse.cz>
Link: https://lore.kernel.org/lkml/20200615085836.GR12456@shao2-debian
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Jun 16, 2020
1 parent e571d4e commit 86f5639
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
5 changes: 5 additions & 0 deletions tools/testing/selftests/pidfd/pidfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,9 @@ static inline int sys_pidfd_getfd(int pidfd, int fd, int flags)
return syscall(__NR_pidfd_getfd, pidfd, fd, flags);
}

static inline int sys_memfd_create(const char *name, unsigned int flags)
{
return syscall(__NR_memfd_create, name, flags);
}

#endif /* __PIDFD_H */
5 changes: 0 additions & 5 deletions tools/testing/selftests/pidfd/pidfd_getfd_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ static int sys_kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1,
return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
}

static int sys_memfd_create(const char *name, unsigned int flags)
{
return syscall(__NR_memfd_create, name, flags);
}

static int __child(int sk, int memfd)
{
int ret;
Expand Down
12 changes: 12 additions & 0 deletions tools/testing/selftests/pidfd/pidfd_setns_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,16 @@ TEST_F(current_nsset, no_foul_play)
}
}

TEST(setns_einval)
{
int fd;

fd = sys_memfd_create("rostock", 0);
EXPECT_GT(fd, 0);

ASSERT_NE(setns(fd, 0), 0);
EXPECT_EQ(errno, EINVAL);
close(fd);
}

TEST_HARNESS_MAIN

0 comments on commit 86f5639

Please sign in to comment.