Skip to content

Commit 8c2ab04

Browse files
committed
selftests/pidfd: test setattr support
Verify that ->setattr() on a pidfd doens't work. Link: https://lore.kernel.org/20250618-work-pidfs-persistent-v2-15-98f3456fd552@kernel.org Reviewed-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 7442d09 commit 8c2ab04

3 files changed

Lines changed: 71 additions & 1 deletion

File tree

tools/testing/selftests/pidfd/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ pidfd_bind_mount
1111
pidfd_info_test
1212
pidfd_exec_helper
1313
pidfd_xattr_test
14+
pidfd_setattr_test

tools/testing/selftests/pidfd/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CFLAGS += -g $(KHDR_INCLUDES) -pthread -Wall
44
TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \
55
pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \
66
pidfd_file_handle_test pidfd_bind_mount pidfd_info_test \
7-
pidfd_xattr_test
7+
pidfd_xattr_test pidfd_setattr_test
88

99
TEST_GEN_PROGS_EXTENDED := pidfd_exec_helper
1010

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#define _GNU_SOURCE
4+
#include <errno.h>
5+
#include <fcntl.h>
6+
#include <limits.h>
7+
#include <linux/types.h>
8+
#include <poll.h>
9+
#include <pthread.h>
10+
#include <sched.h>
11+
#include <signal.h>
12+
#include <stdio.h>
13+
#include <stdlib.h>
14+
#include <string.h>
15+
#include <syscall.h>
16+
#include <sys/prctl.h>
17+
#include <sys/wait.h>
18+
#include <unistd.h>
19+
#include <sys/socket.h>
20+
#include <linux/kcmp.h>
21+
#include <sys/stat.h>
22+
#include <sys/xattr.h>
23+
24+
#include "pidfd.h"
25+
#include "../kselftest_harness.h"
26+
27+
FIXTURE(pidfs_setattr)
28+
{
29+
pid_t child_pid;
30+
int child_pidfd;
31+
};
32+
33+
FIXTURE_SETUP(pidfs_setattr)
34+
{
35+
self->child_pid = create_child(&self->child_pidfd, CLONE_NEWUSER | CLONE_NEWPID);
36+
EXPECT_GE(self->child_pid, 0);
37+
38+
if (self->child_pid == 0)
39+
_exit(EXIT_SUCCESS);
40+
}
41+
42+
FIXTURE_TEARDOWN(pidfs_setattr)
43+
{
44+
sys_waitid(P_PID, self->child_pid, NULL, WEXITED);
45+
EXPECT_EQ(close(self->child_pidfd), 0);
46+
}
47+
48+
TEST_F(pidfs_setattr, no_chown)
49+
{
50+
ASSERT_LT(fchown(self->child_pidfd, 1234, 5678), 0);
51+
ASSERT_EQ(errno, EOPNOTSUPP);
52+
}
53+
54+
TEST_F(pidfs_setattr, no_chmod)
55+
{
56+
ASSERT_LT(fchmod(self->child_pidfd, 0777), 0);
57+
ASSERT_EQ(errno, EOPNOTSUPP);
58+
}
59+
60+
TEST_F(pidfs_setattr, no_exec)
61+
{
62+
char *const argv[] = { NULL };
63+
char *const envp[] = { NULL };
64+
65+
ASSERT_LT(execveat(self->child_pidfd, "", argv, envp, AT_EMPTY_PATH), 0);
66+
ASSERT_EQ(errno, EACCES);
67+
}
68+
69+
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)