Skip to content

Commit 6952a4f

Browse files
author
Christian Brauner
committed
selftests: add pid namespace ENOMEM regression test
We recently regressed (cf. [1] and its corresponding fix in [2]) returning ENOMEM when trying to create a process in a pid namespace whose init process/child subreaper has already died. This has caused confusion at least once before that (cf. [3]). Let's add a simple regression test to catch this in the future. [1]: 49cb2fc ("fork: extend clone3() to support setting a PID") [2]: b26ebfe ("pid: Fix error return value in some cases") [3]: 35f71bc ("fork: report pid reservation failure properly") Cc: Corey Minyard <[email protected]> Cc: Oleg Nesterov <[email protected]> Cc: Adrian Reber <[email protected]> Cc: Dmitry Safonov <[email protected]> Cc: Andrei Vagin <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 2c523b3 commit 6952a4f

File tree

7 files changed

+60
-0
lines changed

7 files changed

+60
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13158,6 +13158,7 @@ S: Maintained
1315813158
T: git git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git
1315913159
F: samples/pidfd/
1316013160
F: tools/testing/selftests/pidfd/
13161+
F: tools/testing/selftests/pid_namespace/
1316113162
F: tools/testing/selftests/clone3/
1316213163
K: (?i)pidfd
1316313164
K: (?i)clone3

tools/testing/selftests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ TARGETS += netfilter
3838
TARGETS += networking/timestamping
3939
TARGETS += nsfs
4040
TARGETS += pidfd
41+
TARGETS += pid_namespace
4142
TARGETS += powerpc
4243
TARGETS += proc
4344
TARGETS += pstore
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
regression_enomem
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
CFLAGS += -g -I../../../../usr/include/
3+
4+
TEST_GEN_PROGS := regression_enomem
5+
6+
include ../lib.mk
7+
8+
$(OUTPUT)/regression_enomem: regression_enomem.c ../pidfd/pidfd.h
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_PID_NS=y
2+
CONFIG_USER_NS=y
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#define _GNU_SOURCE
2+
#include <assert.h>
3+
#include <errno.h>
4+
#include <fcntl.h>
5+
#include <linux/types.h>
6+
#include <sched.h>
7+
#include <signal.h>
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
#include <syscall.h>
12+
#include <sys/wait.h>
13+
14+
#include "../kselftest.h"
15+
#include "../kselftest_harness.h"
16+
#include "../pidfd/pidfd.h"
17+
18+
/*
19+
* Regression test for:
20+
* 35f71bc0a09a ("fork: report pid reservation failure properly")
21+
* b26ebfe12f34 ("pid: Fix error return value in some cases")
22+
*/
23+
TEST(regression_enomem)
24+
{
25+
pid_t pid;
26+
27+
if (geteuid())
28+
EXPECT_EQ(0, unshare(CLONE_NEWUSER));
29+
30+
EXPECT_EQ(0, unshare(CLONE_NEWPID));
31+
32+
pid = fork();
33+
ASSERT_GE(pid, 0);
34+
35+
if (pid == 0)
36+
exit(EXIT_SUCCESS);
37+
38+
EXPECT_EQ(0, wait_for_pid(pid));
39+
40+
pid = fork();
41+
ASSERT_LT(pid, 0);
42+
ASSERT_EQ(errno, ENOMEM);
43+
}
44+
45+
TEST_HARNESS_MAIN

tools/testing/selftests/pidfd/pidfd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <string.h>
1414
#include <syscall.h>
1515
#include <sys/mount.h>
16+
#include <sys/types.h>
17+
#include <sys/wait.h>
1618

1719
#include "../kselftest.h"
1820

0 commit comments

Comments
 (0)