Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2
  3/*
  4 * Based on Christian Brauner's clone3() example.
  5 * These tests are assuming to be running in the host's
  6 * PID namespace.
  7 */
  8
  9/* capabilities related code based on selftests/bpf/test_verifier.c */
 10
 11#define _GNU_SOURCE
 12#include <errno.h>
 13#include <linux/types.h>
 14#include <linux/sched.h>
 15#include <stdio.h>
 16#include <stdlib.h>
 17#include <stdbool.h>
 18#include <sys/capability.h>
 19#include <sys/prctl.h>
 20#include <sys/syscall.h>
 21#include <sys/types.h>
 22#include <sys/un.h>
 23#include <sys/wait.h>
 24#include <unistd.h>
 25#include <sched.h>
 26
 27#include "../kselftest_harness.h"
 28#include "clone3_selftests.h"
 29
 
 
 30static void child_exit(int ret)
 31{
 32	fflush(stdout);
 33	fflush(stderr);
 34	_exit(ret);
 35}
 36
 37static int call_clone3_set_tid(struct __test_metadata *_metadata,
 38			       pid_t *set_tid, size_t set_tid_size)
 39{
 40	int status;
 41	pid_t pid = -1;
 42
 43	struct __clone_args args = {
 44		.exit_signal = SIGCHLD,
 45		.set_tid = ptr_to_u64(set_tid),
 46		.set_tid_size = set_tid_size,
 47	};
 48
 49	pid = sys_clone3(&args, sizeof(args));
 50	if (pid < 0) {
 51		TH_LOG("%s - Failed to create new process", strerror(errno));
 52		return -errno;
 53	}
 54
 55	if (pid == 0) {
 56		int ret;
 57		char tmp = 0;
 58
 59		TH_LOG("I am the child, my PID is %d (expected %d)", getpid(), set_tid[0]);
 60
 61		if (set_tid[0] != getpid())
 62			child_exit(EXIT_FAILURE);
 63		child_exit(EXIT_SUCCESS);
 64	}
 65
 66	TH_LOG("I am the parent (%d). My child's pid is %d", getpid(), pid);
 67
 68	if (waitpid(pid, &status, 0) < 0) {
 69		TH_LOG("Child returned %s", strerror(errno));
 70		return -errno;
 71	}
 72
 73	if (!WIFEXITED(status))
 74		return -1;
 75
 76	return WEXITSTATUS(status);
 77}
 78
 79static int test_clone3_set_tid(struct __test_metadata *_metadata,
 80			       pid_t *set_tid, size_t set_tid_size)
 81{
 82	int ret;
 83
 84	TH_LOG("[%d] Trying clone3() with CLONE_SET_TID to %d", getpid(), set_tid[0]);
 85	ret = call_clone3_set_tid(_metadata, set_tid, set_tid_size);
 86	TH_LOG("[%d] clone3() with CLONE_SET_TID %d says:%d", getpid(), set_tid[0], ret);
 87	return ret;
 88}
 89
 90struct libcap {
 91	struct __user_cap_header_struct hdr;
 92	struct __user_cap_data_struct data[2];
 93};
 94
 95static int set_capability(void)
 96{
 97	cap_value_t cap_values[] = { CAP_SETUID, CAP_SETGID };
 98	struct libcap *cap;
 99	int ret = -1;
100	cap_t caps;
101
102	caps = cap_get_proc();
103	if (!caps) {
104		perror("cap_get_proc");
105		return -1;
106	}
107
108	/* Drop all capabilities */
109	if (cap_clear(caps)) {
110		perror("cap_clear");
111		goto out;
112	}
113
114	cap_set_flag(caps, CAP_EFFECTIVE, 2, cap_values, CAP_SET);
115	cap_set_flag(caps, CAP_PERMITTED, 2, cap_values, CAP_SET);
116
117	cap = (struct libcap *) caps;
118
119	/* 40 -> CAP_CHECKPOINT_RESTORE */
120	cap->data[1].effective |= 1 << (40 - 32);
121	cap->data[1].permitted |= 1 << (40 - 32);
122
123	if (cap_set_proc(caps)) {
124		perror("cap_set_proc");
125		goto out;
126	}
127	ret = 0;
128out:
129	if (cap_free(caps))
130		perror("cap_free");
131	return ret;
132}
133
134TEST(clone3_cap_checkpoint_restore)
135{
136	pid_t pid;
137	int status;
138	int ret = 0;
139	pid_t set_tid[1];
140
141	test_clone3_supported();
142
143	EXPECT_EQ(getuid(), 0)
144		SKIP(return, "Skipping all tests as non-root");
145
146	memset(&set_tid, 0, sizeof(set_tid));
147
148	/* Find the current active PID */
149	pid = fork();
150	if (pid == 0) {
151		TH_LOG("Child has PID %d", getpid());
152		child_exit(EXIT_SUCCESS);
153	}
154	ASSERT_GT(waitpid(pid, &status, 0), 0)
155		TH_LOG("Waiting for child %d failed", pid);
156
157	/* After the child has finished, its PID should be free. */
158	set_tid[0] = pid;
159
160	ASSERT_EQ(set_capability(), 0)
161		TH_LOG("Could not set CAP_CHECKPOINT_RESTORE");
162
163	ASSERT_EQ(prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0), 0);
164
165	EXPECT_EQ(setgid(65534), 0)
166		TH_LOG("Failed to setgid(65534)");
167	ASSERT_EQ(setuid(65534), 0);
168
169	set_tid[0] = pid;
170	/* This would fail without CAP_CHECKPOINT_RESTORE */
171	ASSERT_EQ(test_clone3_set_tid(_metadata, set_tid, 1), -EPERM);
172	ASSERT_EQ(set_capability(), 0)
173		TH_LOG("Could not set CAP_CHECKPOINT_RESTORE");
174	/* This should work as we have CAP_CHECKPOINT_RESTORE as non-root */
175	ASSERT_EQ(test_clone3_set_tid(_metadata, set_tid, 1), 0);
176}
177
178TEST_HARNESS_MAIN
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2
  3/*
  4 * Based on Christian Brauner's clone3() example.
  5 * These tests are assuming to be running in the host's
  6 * PID namespace.
  7 */
  8
  9/* capabilities related code based on selftests/bpf/test_verifier.c */
 10
 11#define _GNU_SOURCE
 12#include <errno.h>
 13#include <linux/types.h>
 14#include <linux/sched.h>
 15#include <stdio.h>
 16#include <stdlib.h>
 17#include <stdbool.h>
 18#include <sys/capability.h>
 19#include <sys/prctl.h>
 20#include <sys/syscall.h>
 21#include <sys/types.h>
 22#include <sys/un.h>
 23#include <sys/wait.h>
 24#include <unistd.h>
 25#include <sched.h>
 26
 27#include "../kselftest_harness.h"
 28#include "clone3_selftests.h"
 29
 30#define MAX_PID_NS_LEVEL 32
 31
 32static void child_exit(int ret)
 33{
 34	fflush(stdout);
 35	fflush(stderr);
 36	_exit(ret);
 37}
 38
 39static int call_clone3_set_tid(struct __test_metadata *_metadata,
 40			       pid_t *set_tid, size_t set_tid_size)
 41{
 42	int status;
 43	pid_t pid = -1;
 44
 45	struct __clone_args args = {
 46		.exit_signal = SIGCHLD,
 47		.set_tid = ptr_to_u64(set_tid),
 48		.set_tid_size = set_tid_size,
 49	};
 50
 51	pid = sys_clone3(&args, sizeof(args));
 52	if (pid < 0) {
 53		TH_LOG("%s - Failed to create new process", strerror(errno));
 54		return -errno;
 55	}
 56
 57	if (pid == 0) {
 58		int ret;
 59		char tmp = 0;
 60
 61		TH_LOG("I am the child, my PID is %d (expected %d)", getpid(), set_tid[0]);
 62
 63		if (set_tid[0] != getpid())
 64			child_exit(EXIT_FAILURE);
 65		child_exit(EXIT_SUCCESS);
 66	}
 67
 68	TH_LOG("I am the parent (%d). My child's pid is %d", getpid(), pid);
 69
 70	if (waitpid(pid, &status, 0) < 0) {
 71		TH_LOG("Child returned %s", strerror(errno));
 72		return -errno;
 73	}
 74
 75	if (!WIFEXITED(status))
 76		return -1;
 77
 78	return WEXITSTATUS(status);
 79}
 80
 81static int test_clone3_set_tid(struct __test_metadata *_metadata,
 82			       pid_t *set_tid, size_t set_tid_size)
 83{
 84	int ret;
 85
 86	TH_LOG("[%d] Trying clone3() with CLONE_SET_TID to %d", getpid(), set_tid[0]);
 87	ret = call_clone3_set_tid(_metadata, set_tid, set_tid_size);
 88	TH_LOG("[%d] clone3() with CLONE_SET_TID %d says:%d", getpid(), set_tid[0], ret);
 89	return ret;
 90}
 91
 92struct libcap {
 93	struct __user_cap_header_struct hdr;
 94	struct __user_cap_data_struct data[2];
 95};
 96
 97static int set_capability(void)
 98{
 99	cap_value_t cap_values[] = { CAP_SETUID, CAP_SETGID };
100	struct libcap *cap;
101	int ret = -1;
102	cap_t caps;
103
104	caps = cap_get_proc();
105	if (!caps) {
106		perror("cap_get_proc");
107		return -1;
108	}
109
110	/* Drop all capabilities */
111	if (cap_clear(caps)) {
112		perror("cap_clear");
113		goto out;
114	}
115
116	cap_set_flag(caps, CAP_EFFECTIVE, 2, cap_values, CAP_SET);
117	cap_set_flag(caps, CAP_PERMITTED, 2, cap_values, CAP_SET);
118
119	cap = (struct libcap *) caps;
120
121	/* 40 -> CAP_CHECKPOINT_RESTORE */
122	cap->data[1].effective |= 1 << (40 - 32);
123	cap->data[1].permitted |= 1 << (40 - 32);
124
125	if (cap_set_proc(caps)) {
126		perror("cap_set_proc");
127		goto out;
128	}
129	ret = 0;
130out:
131	if (cap_free(caps))
132		perror("cap_free");
133	return ret;
134}
135
136TEST(clone3_cap_checkpoint_restore)
137{
138	pid_t pid;
139	int status;
140	int ret = 0;
141	pid_t set_tid[1];
142
143	test_clone3_supported();
144
145	EXPECT_EQ(getuid(), 0)
146		SKIP(return, "Skipping all tests as non-root");
147
148	memset(&set_tid, 0, sizeof(set_tid));
149
150	/* Find the current active PID */
151	pid = fork();
152	if (pid == 0) {
153		TH_LOG("Child has PID %d", getpid());
154		child_exit(EXIT_SUCCESS);
155	}
156	ASSERT_GT(waitpid(pid, &status, 0), 0)
157		TH_LOG("Waiting for child %d failed", pid);
158
159	/* After the child has finished, its PID should be free. */
160	set_tid[0] = pid;
161
162	ASSERT_EQ(set_capability(), 0)
163		TH_LOG("Could not set CAP_CHECKPOINT_RESTORE");
164
165	ASSERT_EQ(prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0), 0);
166
167	EXPECT_EQ(setgid(65534), 0)
168		TH_LOG("Failed to setgid(65534)");
169	ASSERT_EQ(setuid(65534), 0);
170
171	set_tid[0] = pid;
172	/* This would fail without CAP_CHECKPOINT_RESTORE */
173	ASSERT_EQ(test_clone3_set_tid(_metadata, set_tid, 1), -EPERM);
174	ASSERT_EQ(set_capability(), 0)
175		TH_LOG("Could not set CAP_CHECKPOINT_RESTORE");
176	/* This should work as we have CAP_CHECKPOINT_RESTORE as non-root */
177	ASSERT_EQ(test_clone3_set_tid(_metadata, set_tid, 1), 0);
178}
179
180TEST_HARNESS_MAIN