Linux-Fsdevel Archive on lore.kernel.org help / color / mirror / Atom feed
From: Adrian Reber <areber@redhat.com> To: "Christian Brauner" <christian.brauner@ubuntu.com>, "Eric Biederman" <ebiederm@xmission.com>, "Pavel Emelyanov" <ovzxemul@gmail.com>, "Oleg Nesterov" <oleg@redhat.com>, "Dmitry Safonov" <0x7f454c46@gmail.com>, "Andrei Vagin" <avagin@gmail.com>, "Nicolas Viennot" <Nicolas.Viennot@twosigma.com>, "Michał Cłapiński" <mclapinski@google.com>, "Kamil Yurtsever" <kyurtsever@google.com>, "Dirk Petersen" <dipeit@gmail.com>, "Christine Flood" <chf@redhat.com>, "Casey Schaufler" <casey@schaufler-ca.com> Cc: Mike Rapoport <rppt@linux.ibm.com>, Radostin Stoyanov <rstoyanov1@gmail.com>, Adrian Reber <areber@redhat.com>, Cyrill Gorcunov <gorcunov@openvz.org>, Serge Hallyn <serge@hallyn.com>, Stephen Smalley <stephen.smalley.work@gmail.com>, Sargun Dhillon <sargun@sargun.me>, Arnd Bergmann <arnd@arndb.de>, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, selinux@vger.kernel.org, Eric Paris <eparis@parisplace.org>, Jann Horn <jannh@google.com>, linux-fsdevel@vger.kernel.org Subject: [PATCH v6 7/7] selftests: add clone3() CAP_CHECKPOINT_RESTORE test Date: Sun, 19 Jul 2020 12:04:17 +0200 [thread overview] Message-ID: <20200719100418.2112740-8-areber@redhat.com> (raw) In-Reply-To: <20200719100418.2112740-1-areber@redhat.com> This adds a test that changes its UID, uses capabilities to get CAP_CHECKPOINT_RESTORE and uses clone3() with set_tid to create a process with a given PID as non-root. Signed-off-by: Adrian Reber <areber@redhat.com> --- tools/testing/selftests/clone3/.gitignore | 1 + tools/testing/selftests/clone3/Makefile | 4 +- .../clone3/clone3_cap_checkpoint_restore.c | 177 ++++++++++++++++++ 3 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/clone3/clone3_cap_checkpoint_restore.c diff --git a/tools/testing/selftests/clone3/.gitignore b/tools/testing/selftests/clone3/.gitignore index a81085742d40..83c0f6246055 100644 --- a/tools/testing/selftests/clone3/.gitignore +++ b/tools/testing/selftests/clone3/.gitignore @@ -2,3 +2,4 @@ clone3 clone3_clear_sighand clone3_set_tid +clone3_cap_checkpoint_restore diff --git a/tools/testing/selftests/clone3/Makefile b/tools/testing/selftests/clone3/Makefile index cf976c732906..ef7564cb7abe 100644 --- a/tools/testing/selftests/clone3/Makefile +++ b/tools/testing/selftests/clone3/Makefile @@ -1,6 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 CFLAGS += -g -I../../../../usr/include/ +LDLIBS += -lcap -TEST_GEN_PROGS := clone3 clone3_clear_sighand clone3_set_tid +TEST_GEN_PROGS := clone3 clone3_clear_sighand clone3_set_tid \ + clone3_cap_checkpoint_restore include ../lib.mk diff --git a/tools/testing/selftests/clone3/clone3_cap_checkpoint_restore.c b/tools/testing/selftests/clone3/clone3_cap_checkpoint_restore.c new file mode 100644 index 000000000000..c0d83511cd28 --- /dev/null +++ b/tools/testing/selftests/clone3/clone3_cap_checkpoint_restore.c @@ -0,0 +1,177 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Based on Christian Brauner's clone3() example. + * These tests are assuming to be running in the host's + * PID namespace. + */ + +/* capabilities related code based on selftests/bpf/test_verifier.c */ + +#define _GNU_SOURCE +#include <errno.h> +#include <linux/types.h> +#include <linux/sched.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <sys/capability.h> +#include <sys/prctl.h> +#include <sys/syscall.h> +#include <sys/types.h> +#include <sys/un.h> +#include <sys/wait.h> +#include <unistd.h> +#include <sched.h> + +#include "../kselftest_harness.h" +#include "clone3_selftests.h" + +#ifndef MAX_PID_NS_LEVEL +#define MAX_PID_NS_LEVEL 32 +#endif + +static void child_exit(int ret) +{ + fflush(stdout); + fflush(stderr); + _exit(ret); +} + +static int call_clone3_set_tid(pid_t *set_tid, size_t set_tid_size) +{ + int status; + pid_t pid = -1; + + struct clone_args args = { + .exit_signal = SIGCHLD, + .set_tid = ptr_to_u64(set_tid), + .set_tid_size = set_tid_size, + }; + + pid = sys_clone3(&args, sizeof(struct clone_args)); + if (pid < 0) { + ksft_print_msg("%s - Failed to create new process\n", strerror(errno)); + return -errno; + } + + if (pid == 0) { + int ret; + char tmp = 0; + + ksft_print_msg + ("I am the child, my PID is %d (expected %d)\n", getpid(), set_tid[0]); + + if (set_tid[0] != getpid()) + child_exit(EXIT_FAILURE); + child_exit(EXIT_SUCCESS); + } + + ksft_print_msg("I am the parent (%d). My child's pid is %d\n", getpid(), pid); + + if (waitpid(pid, &status, 0) < 0) { + ksft_print_msg("Child returned %s\n", strerror(errno)); + return -errno; + } + + if (!WIFEXITED(status)) + return -1; + + return WEXITSTATUS(status); +} + +static int test_clone3_set_tid(pid_t *set_tid, size_t set_tid_size) +{ + int ret; + + ksft_print_msg("[%d] Trying clone3() with CLONE_SET_TID to %d\n", getpid(), set_tid[0]); + ret = call_clone3_set_tid(set_tid, set_tid_size); + ksft_print_msg("[%d] clone3() with CLONE_SET_TID %d says:%d\n", getpid(), set_tid[0], ret); + return ret; +} + +struct libcap { + struct __user_cap_header_struct hdr; + struct __user_cap_data_struct data[2]; +}; + +static int set_capability(void) +{ + cap_value_t cap_values[] = { CAP_SETUID, CAP_SETGID }; + struct libcap *cap; + int ret = -1; + cap_t caps; + + caps = cap_get_proc(); + if (!caps) { + perror("cap_get_proc"); + return -1; + } + + /* Drop all capabilities */ + if (cap_clear(caps)) { + perror("cap_clear"); + goto out; + } + + cap_set_flag(caps, CAP_EFFECTIVE, 2, cap_values, CAP_SET); + cap_set_flag(caps, CAP_PERMITTED, 2, cap_values, CAP_SET); + + cap = (struct libcap *) caps; + + /* 40 -> CAP_CHECKPOINT_RESTORE */ + cap->data[1].effective |= 1 << (40 - 32); + cap->data[1].permitted |= 1 << (40 - 32); + + if (cap_set_proc(caps)) { + perror("cap_set_proc"); + goto out; + } + ret = 0; +out: + if (cap_free(caps)) + perror("cap_free"); + return ret; +} + +TEST(clone3_cap_checkpoint_restore) +{ + pid_t pid; + int status; + int ret = 0; + pid_t set_tid[1]; + + test_clone3_supported(); + + EXPECT_EQ(getuid(), 0) + SKIP(return, "Skipping all tests as non-root\n"); + + memset(&set_tid, 0, sizeof(set_tid)); + + /* Find the current active PID */ + pid = fork(); + if (pid == 0) { + TH_LOG("Child has PID %d", getpid()); + child_exit(EXIT_SUCCESS); + } + ASSERT_GT(waitpid(pid, &status, 0), 0) + TH_LOG("Waiting for child %d failed", pid); + + /* After the child has finished, its PID should be free. */ + set_tid[0] = pid; + + ASSERT_EQ(set_capability(), 0) + TH_LOG("Could not set CAP_CHECKPOINT_RESTORE"); + prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); + setgid(1000); + setuid(1000); + set_tid[0] = pid; + /* This would fail without CAP_CHECKPOINT_RESTORE */ + ASSERT_EQ(test_clone3_set_tid(set_tid, 1), -EPERM); + ASSERT_EQ(set_capability(), 0) + TH_LOG("Could not set CAP_CHECKPOINT_RESTORE"); + /* This should work as we have CAP_CHECKPOINT_RESTORE as non-root */ + ASSERT_EQ(test_clone3_set_tid(set_tid, 1), 0); +} + +TEST_HARNESS_MAIN -- 2.26.2
next prev parent reply other threads:[~2020-07-19 10:06 UTC|newest] Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-07-19 10:04 [PATCH v6 0/7] capabilities: Introduce CAP_CHECKPOINT_RESTORE Adrian Reber 2020-07-19 10:04 ` [PATCH v6 1/7] " Adrian Reber 2020-07-19 10:04 ` [PATCH v6 2/7] pid: use checkpoint_restore_ns_capable() for set_tid Adrian Reber 2020-07-19 10:04 ` [PATCH v6 3/7] pid_namespace: use checkpoint_restore_ns_capable() for ns_last_pid Adrian Reber 2020-07-19 10:04 ` [PATCH v6 4/7] proc: allow access in init userns for map_files with CAP_CHECKPOINT_RESTORE Adrian Reber 2020-07-19 16:50 ` Serge E. Hallyn 2020-07-19 10:04 ` [PATCH v6 5/7] prctl: Allow local CAP_CHECKPOINT_RESTORE to change /proc/self/exe Adrian Reber 2020-07-19 10:04 ` [PATCH v6 6/7] prctl: exe link permission error changed from -EINVAL to -EPERM Adrian Reber 2020-07-19 17:05 ` Serge E. Hallyn 2020-07-19 10:04 ` Adrian Reber [this message] 2020-07-19 18:17 ` [PATCH v6 0/7] capabilities: Introduce CAP_CHECKPOINT_RESTORE Christian Brauner 2020-07-20 11:54 ` Christian Brauner 2020-07-20 12:46 ` Adrian Reber 2020-07-20 12:58 ` Christian Brauner
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20200719100418.2112740-8-areber@redhat.com \ --to=areber@redhat.com \ --cc=0x7f454c46@gmail.com \ --cc=Nicolas.Viennot@twosigma.com \ --cc=arnd@arndb.de \ --cc=avagin@gmail.com \ --cc=casey@schaufler-ca.com \ --cc=chf@redhat.com \ --cc=christian.brauner@ubuntu.com \ --cc=dipeit@gmail.com \ --cc=ebiederm@xmission.com \ --cc=eparis@parisplace.org \ --cc=gorcunov@openvz.org \ --cc=jannh@google.com \ --cc=kyurtsever@google.com \ --cc=linux-fsdevel@vger.kernel.org \ --cc=linux-kernel@vger.kernel.org \ --cc=linux-security-module@vger.kernel.org \ --cc=mclapinski@google.com \ --cc=oleg@redhat.com \ --cc=ovzxemul@gmail.com \ --cc=rppt@linux.ibm.com \ --cc=rstoyanov1@gmail.com \ --cc=sargun@sargun.me \ --cc=selinux@vger.kernel.org \ --cc=serge@hallyn.com \ --cc=stephen.smalley.work@gmail.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).