Skip to content

Commit 172bb24

Browse files
committed
tests: add pidfd_open() tests
This adds testing for the new pidfd_open() syscalls. Specifically, we test: - that no invalid flags can be passed to pidfd_open() - that no invalid pid can be passed to pidfd_open() - that a pidfd can be retrieved with pidfd_open() - that the retrieved pidfd references the correct pid Signed-off-by: Christian Brauner <christian@brauner.io> Cc: Arnd Bergmann <arnd@arndb.de> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: Kees Cook <keescook@chromium.org> Cc: Joel Fernandes (Google) <joel@joelfernandes.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Jann Horn <jannh@google.com> Cc: David Howells <dhowells@redhat.com> Cc: "Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com> Cc: Andy Lutomirsky <luto@kernel.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Aleksa Sarai <cyphar@cyphar.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: linux-api@vger.kernel.org
1 parent 7615d9e commit 172bb24

5 files changed

Lines changed: 234 additions & 44 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
pidfd_open_test
12
pidfd_test
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
12
CFLAGS += -g -I../../../../usr/include/ -lpthread
23

3-
TEST_GEN_PROGS := pidfd_test
4+
TEST_GEN_PROGS := pidfd_test pidfd_open_test
45

56
include ../lib.mk
67

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#ifndef __PIDFD_H
4+
#define __PIDFD_H
5+
6+
#define _GNU_SOURCE
7+
#include <errno.h>
8+
#include <fcntl.h>
9+
#include <sched.h>
10+
#include <signal.h>
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <string.h>
14+
#include <syscall.h>
15+
#include <sys/mount.h>
16+
17+
#include "../kselftest.h"
18+
19+
/*
20+
* The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c
21+
* That means, when it wraps around any pid < 300 will be skipped.
22+
* So we need to use a pid > 300 in order to test recycling.
23+
*/
24+
#define PID_RECYCLE 1000
25+
26+
/*
27+
* Define a few custom error codes for the child process to clearly indicate
28+
* what is happening. This way we can tell the difference between a system
29+
* error, a test error, etc.
30+
*/
31+
#define PIDFD_PASS 0
32+
#define PIDFD_FAIL 1
33+
#define PIDFD_ERROR 2
34+
#define PIDFD_SKIP 3
35+
#define PIDFD_XFAIL 4
36+
37+
int wait_for_pid(pid_t pid)
38+
{
39+
int status, ret;
40+
41+
again:
42+
ret = waitpid(pid, &status, 0);
43+
if (ret == -1) {
44+
if (errno == EINTR)
45+
goto again;
46+
47+
return -1;
48+
}
49+
50+
if (!WIFEXITED(status))
51+
return -1;
52+
53+
return WEXITSTATUS(status);
54+
}
55+
56+
57+
#endif /* __PIDFD_H */
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#define _GNU_SOURCE
4+
#include <errno.h>
5+
#include <fcntl.h>
6+
#include <inttypes.h>
7+
#include <limits.h>
8+
#include <linux/types.h>
9+
#include <linux/wait.h>
10+
#include <sched.h>
11+
#include <signal.h>
12+
#include <stdbool.h>
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
#include <string.h>
16+
#include <syscall.h>
17+
#include <sys/mount.h>
18+
#include <sys/prctl.h>
19+
#include <sys/wait.h>
20+
#include <unistd.h>
21+
22+
#include "pidfd.h"
23+
#include "../kselftest.h"
24+
25+
static inline int sys_pidfd_open(pid_t pid, unsigned int flags)
26+
{
27+
return syscall(__NR_pidfd_open, pid, flags);
28+
}
29+
30+
static int safe_int(const char *numstr, int *converted)
31+
{
32+
char *err = NULL;
33+
long sli;
34+
35+
errno = 0;
36+
sli = strtol(numstr, &err, 0);
37+
if (errno == ERANGE && (sli == LONG_MAX || sli == LONG_MIN))
38+
return -ERANGE;
39+
40+
if (errno != 0 && sli == 0)
41+
return -EINVAL;
42+
43+
if (err == numstr || *err != '\0')
44+
return -EINVAL;
45+
46+
if (sli > INT_MAX || sli < INT_MIN)
47+
return -ERANGE;
48+
49+
*converted = (int)sli;
50+
return 0;
51+
}
52+
53+
static int char_left_gc(const char *buffer, size_t len)
54+
{
55+
size_t i;
56+
57+
for (i = 0; i < len; i++) {
58+
if (buffer[i] == ' ' ||
59+
buffer[i] == '\t')
60+
continue;
61+
62+
return i;
63+
}
64+
65+
return 0;
66+
}
67+
68+
static int char_right_gc(const char *buffer, size_t len)
69+
{
70+
int i;
71+
72+
for (i = len - 1; i >= 0; i--) {
73+
if (buffer[i] == ' ' ||
74+
buffer[i] == '\t' ||
75+
buffer[i] == '\n' ||
76+
buffer[i] == '\0')
77+
continue;
78+
79+
return i + 1;
80+
}
81+
82+
return 0;
83+
}
84+
85+
static char *trim_whitespace_in_place(char *buffer)
86+
{
87+
buffer += char_left_gc(buffer, strlen(buffer));
88+
buffer[char_right_gc(buffer, strlen(buffer))] = '\0';
89+
return buffer;
90+
}
91+
92+
static pid_t get_pid_from_fdinfo_file(int pidfd, const char *key, size_t keylen)
93+
{
94+
int ret;
95+
char path[512];
96+
FILE *f;
97+
size_t n = 0;
98+
pid_t result = -1;
99+
char *line = NULL;
100+
101+
snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", pidfd);
102+
103+
f = fopen(path, "re");
104+
if (!f)
105+
return -1;
106+
107+
while (getline(&line, &n, f) != -1) {
108+
char *numstr;
109+
110+
if (strncmp(line, key, keylen))
111+
continue;
112+
113+
numstr = trim_whitespace_in_place(line + 4);
114+
ret = safe_int(numstr, &result);
115+
if (ret < 0)
116+
goto out;
117+
118+
break;
119+
}
120+
121+
out:
122+
free(line);
123+
fclose(f);
124+
return result;
125+
}
126+
127+
int main(int argc, char **argv)
128+
{
129+
int pidfd = -1, ret = 1;
130+
pid_t pid;
131+
132+
ksft_set_plan(3);
133+
134+
pidfd = sys_pidfd_open(-1, 0);
135+
if (pidfd >= 0) {
136+
ksft_print_msg(
137+
"%s - succeeded to open pidfd for invalid pid -1\n",
138+
strerror(errno));
139+
goto on_error;
140+
}
141+
ksft_test_result_pass("do not allow invalid pid test: passed\n");
142+
143+
pidfd = sys_pidfd_open(getpid(), 1);
144+
if (pidfd >= 0) {
145+
ksft_print_msg(
146+
"%s - succeeded to open pidfd with invalid flag value specified\n",
147+
strerror(errno));
148+
goto on_error;
149+
}
150+
ksft_test_result_pass("do not allow invalid flag test: passed\n");
151+
152+
pidfd = sys_pidfd_open(getpid(), 0);
153+
if (pidfd < 0) {
154+
ksft_print_msg("%s - failed to open pidfd\n", strerror(errno));
155+
goto on_error;
156+
}
157+
ksft_test_result_pass("open a new pidfd test: passed\n");
158+
159+
pid = get_pid_from_fdinfo_file(pidfd, "Pid:", sizeof("Pid:") - 1);
160+
ksft_print_msg("pidfd %d refers to process with pid %d\n", pidfd, pid);
161+
162+
ret = 0;
163+
164+
on_error:
165+
if (pidfd >= 0)
166+
close(pidfd);
167+
168+
return !ret ? ksft_exit_pass() : ksft_exit_fail();
169+
}

‎tools/testing/selftests/pidfd/pidfd_test.c‎

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@
1818
#include <time.h>
1919
#include <unistd.h>
2020

21+
#include "pidfd.h"
2122
#include "../kselftest.h"
2223

24+
#ifndef __NR_pidfd_send_signal
25+
#define __NR_pidfd_send_signal -1
26+
#endif
27+
2328
#define str(s) _str(s)
2429
#define _str(s) #s
2530
#define CHILD_THREAD_MIN_WAIT 3 /* seconds */
2631

2732
#define MAX_EVENTS 5
28-
#ifndef __NR_pidfd_send_signal
29-
#define __NR_pidfd_send_signal 424
30-
#endif
3133

3234
#ifndef CLONE_PIDFD
3335
#define CLONE_PIDFD 0x00001000
@@ -91,28 +93,6 @@ static int test_pidfd_send_signal_simple_success(void)
9193
return 0;
9294
}
9395

94-
static int wait_for_pid(pid_t pid)
95-
{
96-
int status, ret;
97-
98-
again:
99-
ret = waitpid(pid, &status, 0);
100-
if (ret == -1) {
101-
if (errno == EINTR)
102-
goto again;
103-
104-
return -1;
105-
}
106-
107-
if (ret != pid)
108-
goto again;
109-
110-
if (!WIFEXITED(status))
111-
return -1;
112-
113-
return WEXITSTATUS(status);
114-
}
115-
11696
static int test_pidfd_send_signal_exited_fail(void)
11797
{
11898
int pidfd, ret, saved_errno;
@@ -157,13 +137,6 @@ static int test_pidfd_send_signal_exited_fail(void)
157137
return 0;
158138
}
159139

160-
/*
161-
* The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c
162-
* That means, when it wraps around any pid < 300 will be skipped.
163-
* So we need to use a pid > 300 in order to test recycling.
164-
*/
165-
#define PID_RECYCLE 1000
166-
167140
/*
168141
* Maximum number of cycles we allow. This is equivalent to PID_MAX_DEFAULT.
169142
* If users set a higher limit or we have cycled PIDFD_MAX_DEFAULT number of
@@ -172,17 +145,6 @@ static int test_pidfd_send_signal_exited_fail(void)
172145
*/
173146
#define PIDFD_MAX_DEFAULT 0x8000
174147

175-
/*
176-
* Define a few custom error codes for the child process to clearly indicate
177-
* what is happening. This way we can tell the difference between a system
178-
* error, a test error, etc.
179-
*/
180-
#define PIDFD_PASS 0
181-
#define PIDFD_FAIL 1
182-
#define PIDFD_ERROR 2
183-
#define PIDFD_SKIP 3
184-
#define PIDFD_XFAIL 4
185-
186148
static int test_pidfd_send_signal_recycled_pid_fail(void)
187149
{
188150
int i, ret;

0 commit comments

Comments
 (0)