From patchwork Thu Oct 10 13:35:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 34908 Received: (qmail 32870 invoked by alias); 10 Oct 2019 15:24:27 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 32661 invoked by uid 89); 10 Oct 2019 15:24:19 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-16.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_SBL_CSS, RCVD_IN_SORBS_WEB autolearn=unavailable version=3.3.1 spammy=HReceived-SPF:pass, HX-Google-DKIM-Signature:sk:x-origi, HX-Original-Authentication-Results:spf X-HELO: youngberry.canonical.com Resent-From: Christian Brauner Resent-Date: Thu, 10 Oct 2019 17:24:04 +0200 Resent-Message-ID: <20191010152404.tyt6y72du2vo5j4y@wittgenstein> Resent-To: libc-alpha@sourceware.org ARC-Seal: i=2; a=rsa-sha256; t=1570714604; cv=pass; d=google.com; s=arc-20160816; b=OpulqP6H67CRD7w1JoV7jSPNM4uVUdGvmnANbUs02dbR6cac9BN7BJYiUGNsYjSR4W IRSmR/7o43Sft6Vy4GVoHlZdbBq5bUyuNxz1ZNAdVd84eoSSvzuH9dQYMi/g7n/HLMOQ GByhyvARNjL/W1GOZmZcS+vQGnw7hv16NhVo+l9F16+kJVRF4fPLr3nJFUX5Ua2eMDZ3 uledNjQhC9urXybxoypDowa2umHpLi2Q/2nYqfeyyshq+K+G2epT99YAfJZrMTqbEfj/ rZK4YnU/vetWQ1M74hfynqADwcobmoTkImf5IaTNnc7CgNxtklYvEPw12NOdyPsmjC+o efPQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:delivered-to; bh=1p+RW2enGj83fyb+JDnGw1yxQAW4Fx7T40dgCxcFWlE=; b=s9tIkdej5wOK9L4D9gcjxkWHGWi9Nj74o2dYleVDEdoW0XZGDD2jDrvsggbmBmcM+v DGX/DZUDgKYjeY/ykpaCo2cttA0xWEi2GeS3yRE13kaHwNBtwosUGI7s3MP//jrbx3On q843k18ExtDTcA1a7koMqgXf/XzeAGWnfyzEqh2dl9WwQgqtLKDMB8BOYOcAsQk7xayz 78w6SVpF8FgJzfKZbPfsTjaiKIpAK7WYMKI8RWuyvLy6ehyrSJ2jW+6mQ/jOFkFPCj+y 7wGtG/lusFNHQlk0bwzM2Lbxy+7GADEWx0qaNrUHOvbQRH2LhQowoda10T3VxnxZsQQt g0Og== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: best guess record for domain of christian.brauner+caf_=christian=brauner.io@canonical.com designates 91.189.89.112 as permitted sender) smtp.mailfrom="christian.brauner+caf_=christian=brauner.io@canonical.com" Received-SPF: pass (google.com: best guess record for domain of christian.brauner+caf_=christian=brauner.io@canonical.com designates 91.189.89.112 as permitted sender) client-ip=91.189.89.112; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: best guess record for domain of christian.brauner+caf_=christian=brauner.io@canonical.com designates 91.189.89.112 as permitted sender) smtp.mailfrom="christian.brauner+caf_=christian=brauner.io@canonical.com" X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 91.189.89.112 is neither permitted nor denied by best guess record for domain of christian.brauner@ubuntu.com) smtp.mailfrom=christian.brauner@ubuntu.com X-Forwarded-To: christian@brauner.io X-Forwarded-For: christian.brauner@canonical.com christian@brauner.io ARC-Seal: i=1; a=rsa-sha256; t=1570714600; cv=none; d=google.com; s=arc-20160816; b=vTmfh4hRwSoqODPb/rghXZf1dXMAMI/sknCDrQa0z1C1d7NR02cpHILzI17LTbmycy 9K7a/XEE25euHUErjpmtydp3p0tqABvvVvbk9uO5HzlsX1KO2rmkF24no9NJrGWi+2/d aqqh/pHuxDES6nEoBeG1Fqb5uHUpWem/ypW4Gm91fxsPvKYttUtDfagXQSJw/EzyXWLg gR1Iq5VY1sgDJ0E88T4NlWi/8622gu5Q8PzxlAOf7j6nqRQTQzXR2UrygPcmiC7Juw5d aAEOohUn1Mz1uSBz1sV8GzVSJFJVs5DQ/p5XZTBUJjGibMR1Io6xTbI8w3+qvyPCMST5 HPUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=1p+RW2enGj83fyb+JDnGw1yxQAW4Fx7T40dgCxcFWlE=; b=cJaxcaRv474dU8VYC+Bz8QTN7raZPvOJyMleU3s+d7mjdOO//Vi7H3OLg9lBFdmbzT 7XF4B4HuTzGv84Qh4x81yQs43VUvxtMBBhcCLiLg6S2F8oY0JqUbKRxKVAlkCzQh3Pwh IJy78aikLFThk2+xQOoNoWcXNjOnYFH5QOZAAzpXe1TyH6ijloMSe9el0sEIdGzliidf NgswZhmaUIpl9xt8oSeQbB7ipFE8wMVBdzvA9UU+dqINsS+VVgpyVHVZl0vNfdadg8j1 tvBqE3YTaIi5h24qTDoEbRMnmoE9hxAOh3UvSKRvslAWbhthkcZwQ2nMuRvyV/VSEZD+ 95Dg== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 91.189.89.112 is neither permitted nor denied by best guess record for domain of christian.brauner@ubuntu.com) smtp.mailfrom=christian.brauner@ubuntu.com Received-SPF: neutral (google.com: 91.189.89.112 is neither permitted nor denied by best guess record for domain of christian.brauner@ubuntu.com) client-ip=91.189.89.112; From: Christian Brauner To: linux-kernel@vger.kernel.org, Oleg Nesterov , Florian Weimer , libc-alpha@sourceware.org Cc: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Shuah Khan , Andrew Morton , Michal Hocko , Elena Reshetova , Thomas Gleixner , Roman Gushchin , Andrea Arcangeli , Al Viro , Aleksa Sarai , "Dmitry V. Levin" , linux-kselftest@vger.kernel.org, Christian Brauner Subject: [PATCH 2/2] tests: test CLONE3_CLEAR_SIGHAND Date: Thu, 10 Oct 2019 15:35:18 +0200 Message-Id: <20191010133518.5420-2-christian.brauner@ubuntu.com> In-Reply-To: <20191010133518.5420-1-christian.brauner@ubuntu.com> References: <20191010133518.5420-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 Test that CLONE3_CLEAR_SIGHAND resets signal handlers to SIG_DFL for the child process and that CLONE3_CLEAR_SIGHAND and CLONE_SIGHAND are mutually exclusive. Cc: Florian Weimer Cc: libc-alpha@sourceware.org Signed-off-by: Christian Brauner --- MAINTAINERS | 1 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/clone3/.gitignore | 1 + tools/testing/selftests/clone3/Makefile | 7 + .../selftests/clone3/clone3_clear_sighand.c | 171 ++++++++++++++++++ 5 files changed, 181 insertions(+) create mode 100644 tools/testing/selftests/clone3/.gitignore create mode 100644 tools/testing/selftests/clone3/Makefile create mode 100644 tools/testing/selftests/clone3/clone3_clear_sighand.c diff --git a/MAINTAINERS b/MAINTAINERS index 55199ef7fa74..582275d85607 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -12828,6 +12828,7 @@ S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git F: samples/pidfd/ F: tools/testing/selftests/pidfd/ +F: tools/testing/selftests/clone3/ K: (?i)pidfd K: (?i)clone3 K: \b(clone_args|kernel_clone_args)\b diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index c3feccb99ff5..6bf7aeb47650 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -4,6 +4,7 @@ TARGETS += bpf TARGETS += breakpoints TARGETS += capabilities TARGETS += cgroup +TARGETS += clone3 TARGETS += cpufreq TARGETS += cpu-hotplug TARGETS += drivers/dma-buf diff --git a/tools/testing/selftests/clone3/.gitignore b/tools/testing/selftests/clone3/.gitignore new file mode 100644 index 000000000000..6c9f98097774 --- /dev/null +++ b/tools/testing/selftests/clone3/.gitignore @@ -0,0 +1 @@ +clone3_clear_sighand diff --git a/tools/testing/selftests/clone3/Makefile b/tools/testing/selftests/clone3/Makefile new file mode 100644 index 000000000000..3ecd56ebc99d --- /dev/null +++ b/tools/testing/selftests/clone3/Makefile @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0-only +CFLAGS += -g -I../../../../usr/include/ + +TEST_GEN_PROGS := clone3_clear_sighand + +include ../lib.mk + diff --git a/tools/testing/selftests/clone3/clone3_clear_sighand.c b/tools/testing/selftests/clone3/clone3_clear_sighand.c new file mode 100644 index 000000000000..0e38c06dd6eb --- /dev/null +++ b/tools/testing/selftests/clone3/clone3_clear_sighand.c @@ -0,0 +1,171 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +#ifndef CLONE3_CLEAR_SIGHAND +#define CLONE3_CLEAR_SIGHAND 0x100000000ULL +#endif + +#ifndef __NR_clone3 +#define __NR_clone3 -1 +struct clone_args { + __aligned_u64 flags; + __aligned_u64 pidfd; + __aligned_u64 child_tid; + __aligned_u64 parent_tid; + __aligned_u64 exit_signal; + __aligned_u64 stack; + __aligned_u64 stack_size; + __aligned_u64 tls; +}; +#endif + +static pid_t sys_clone3(struct clone_args *args, size_t size) +{ + return syscall(__NR_clone3, args, size); +} + +static void test_clone3_supported(void) +{ + pid_t pid; + struct clone_args args = {}; + + if (__NR_clone3 < 0) + ksft_exit_skip("clone3() syscall is not supported\n"); + + /* Set to something that will always cause EINVAL. */ + args.exit_signal = -1; + pid = sys_clone3(&args, sizeof(args)); + if (!pid) + exit(EXIT_SUCCESS); + + if (pid > 0) { + wait(NULL); + ksft_exit_fail_msg( + "Managed to create child process with invalid exit_signal\n"); + } + + if (errno == ENOSYS) + ksft_exit_skip("clone3() syscall is not supported\n"); + + ksft_print_msg("clone3() syscall supported\n"); +} + +static void nop_handler(int signo) +{ +} + +static int wait_for_pid(pid_t pid) +{ + int status, ret; + +again: + ret = waitpid(pid, &status, 0); + if (ret == -1) { + if (errno == EINTR) + goto again; + + return -1; + } + + if (!WIFEXITED(status)) + return -1; + + return WEXITSTATUS(status); +} + +static void test_clone3_clear_sighand(void) +{ + int ret; + pid_t pid; + struct clone_args args = {}; + struct sigaction new_action, old_action; + + new_action.sa_handler = nop_handler; + ret = sigemptyset(&new_action.sa_mask); + if (ret < 0) + ksft_exit_fail_msg("%s - sigemptyset() failed\n", + strerror(errno)); + + new_action.sa_flags = 0; + + ret = sigaction(SIGUSR1, &new_action, NULL); + if (ret < 0) + ksft_exit_fail_msg( + "%s - sigaction(SIGUSR1, &new_action, NULL) failed\n", + strerror(errno)); + + ret = sigaction(SIGUSR2, &new_action, NULL); + if (ret < 0) + ksft_exit_fail_msg( + "%s - sigaction(SIGUSR2, &new_action, NULL) failed\n", + strerror(errno)); + + /* + * Check that CLONE3_CLEAR_SIGHAND and CLONE_SIGHAND are mutually + * exclusive. + */ + args.flags |= CLONE3_CLEAR_SIGHAND | CLONE_SIGHAND; + args.exit_signal = SIGCHLD; + pid = sys_clone3(&args, sizeof(args)); + if (pid > 0) + ksft_exit_fail_msg( + "clone3(CLONE3_CLEAR_SIGHAND | CLONE_SIGHAND) succeeded\n"); + + /* Check that CLONE3_CLEAR_SIGHAND works. */ + args.flags = CLONE3_CLEAR_SIGHAND; + pid = sys_clone3(&args, sizeof(args)); + if (pid < 0) + ksft_exit_fail_msg("%s - clone3(CLONE3_CLEAR_SIGHAND) failed\n", strerror(errno)); + + if (pid == 0) { + struct sigaction query_action; + + ret = sigaction(SIGUSR1, NULL, &query_action); + if (ret < 0) + exit(EXIT_FAILURE); + + if (query_action.sa_handler != SIG_DFL) + exit(EXIT_FAILURE); + + ret = sigaction(SIGUSR2, NULL, &query_action); + if (ret < 0) + exit(EXIT_FAILURE); + + if (query_action.sa_handler != SIG_DFL) + exit(EXIT_FAILURE); + + exit(EXIT_SUCCESS); + } + + ret = wait_for_pid(pid); + if (ret) + ksft_exit_fail_msg( + "Failed to clear signal handler for child process\n"); + + ksft_test_result_pass("Cleared signal handlers for child process\n"); +} + +int main(int argc, char **argv) +{ + ksft_print_header(); + ksft_set_plan(1); + + test_clone3_supported(); + test_clone3_clear_sighand(); + + return ksft_exit_pass(); +}