Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp434352imu; Tue, 27 Nov 2018 14:55:56 -0800 (PST) X-Google-Smtp-Source: AFSGD/UNZF8cRqT6KHILvy2g/gmSknzOryAK1clwxhjZlEmyBFMsad9S2WoQqam/z5BqBd97Dkhs X-Received: by 2002:a17:902:8687:: with SMTP id g7mr7986511plo.96.1543359356660; Tue, 27 Nov 2018 14:55:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543359356; cv=none; d=google.com; s=arc-20160816; b=nNAwSs65RwVWx1oY2mwj6c4CD799Ns3oLovSHQIdq3xJZvZdl733HH+UA9DEMNvTyt mj/ljhjZBYY4+3Nu+aM13Uhkwglmu/CSbq5b+xTzezODqHnm2AKP03aJnjcmta8ZkUQJ TuaqBPHJlYPKP5clF6q12JHpEw5cPYzDyyKRlqgOkFkgUtjjXSevBF9UkoCOH2JeXdpf Bt9FEF30N47QX8oGK6p46BPap70i+jvFk9cSPQ8/KfdT9ektmm13kaewPu69T0Zl0228 k3Kg7qLweLGBmJiYQ08YZy9bQjK/98Lg00SWFSP0MtU/2sja96JjNMcN5HSLRgHvBGPK vOvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=vuXzpURCQOgH9bDuScBGGDzf0i14ZIhMwOGDp73g50o=; b=h2PVRq0Rqjwb+a1R4Ce8iZBB1mdl68HrErYc4b4CkrKnvCOwn0NB3xJF74bTE3HcO0 bBAh4TcwUwTImCvpBRVElVokQhLVqN2rqnJDuJOTQ8sK/tqc0opkUnib9E1Bj+9niiX5 m5H4n5CfZeuSM57+AhyDMwhqYAHSHrNNhvghAMDUAu8uHIpEAoMK9GhxAbB1aDGiPQnA Pye7UZcavo8AHDMJddDWPBnyu1Jh/y9kgIQgiSRQol0aiF6jhl6e7WhL+uYEVIHvwgiB RSXE8RdKV8n5rXN1cSw79eu5kYwS3p1/SUV8TBcZqRBeWfLE2GHiiB+azmfCgx10+2qe FjnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cisco.com header.s=iport header.b=F2NE1RLd; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=cisco.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y10si4781150plt.406.2018.11.27.14.55.41; Tue, 27 Nov 2018 14:55:56 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@cisco.com header.s=iport header.b=F2NE1RLd; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=cisco.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726705AbeK1JyU (ORCPT + 99 others); Wed, 28 Nov 2018 04:54:20 -0500 Received: from rcdn-iport-5.cisco.com ([173.37.86.76]:34090 "EHLO rcdn-iport-5.cisco.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726636AbeK1JyT (ORCPT ); Wed, 28 Nov 2018 04:54:19 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=cisco.com; i=@cisco.com; l=4984; q=dns/txt; s=iport; t=1543359293; x=1544568893; h=subject:to:cc:references:from:message-id:date: mime-version:in-reply-to:content-transfer-encoding; bh=wQyeyHXswUoX+PDb2qH7y5udUscH6lVO+syNe/Qngbc=; b=F2NE1RLdO7ajpXuG6ogp+PeVOvp5uLA4bX8OWDDntB7rddD6ZAfhSbXQ sXoWJOg5g/mYZI7/CxVS08hlZVw9ayBKh3rsew4oHCKgkg4mMxiYGUxbY zFaPj+RlHIAljeqe3fJXKpeDED2FkUhJPgEizuKozJodeFcUpC9e434mw k=; X-IronPort-AV: E=Sophos;i="5.56,288,1539648000"; d="scan'208";a="270695280" Received: from rcdn-core-3.cisco.com ([173.37.93.154]) by rcdn-iport-5.cisco.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 27 Nov 2018 22:54:53 +0000 Received: from [10.154.208.155] ([10.154.208.155]) by rcdn-core-3.cisco.com (8.15.2/8.15.2) with ESMTP id wARMsnVH020095; Tue, 27 Nov 2018 22:54:49 GMT Subject: [PATCH v5 2/2] selftests/prctl: selftest for pre-coredump signal notification To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Peter Zijlstra , Arnd Bergmann , "Eric W. Biederman" , Khalid Aziz , Kate Stewart , Helge Deller , Greg Kroah-Hartman , Al Viro , Andrew Morton , Christian Brauner , Catalin Marinas , Will Deacon , Dave Martin , Mauro Carvalho Chehab , Michal Hocko , Rik van Riel , "Kirill A. Shutemov" , Marcos Paulo de Souza , Oleg Nesterov , Dominik Brodowski , Cyrill Gorcunov , Yang Shi , Jann Horn , Kees Cook Cc: Roman Gushchin , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, "Victor Kamensky (kamensky)" , xe-linux-external@cisco.com, Stefan Strogin , Enke Chen References: <458c04d8-d189-4a26-729a-bb1d1d751534@cisco.com> <7741efa7-a3f8-62a1-ba52-613883164643@cisco.com> <24d8585c-e1d8-b363-35e5-5343142c3355@cisco.com> From: Enke Chen Message-ID: <2814952f-9409-dbed-eefc-7156ede4cf99@cisco.com> Date: Tue, 27 Nov 2018 14:54:51 -0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <24d8585c-e1d8-b363-35e5-5343142c3355@cisco.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Auto-Response-Suppress: DR, OOF, AutoReply X-Outbound-SMTP-Client: 10.154.208.155, [10.154.208.155] X-Outbound-Node: rcdn-core-3.cisco.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [Repost as a series, as suggested by Andrew Morton] Selftest for the pre-coredump signal notification Signed-off-by: Enke Chen --- tools/testing/selftests/prctl/Makefile | 2 +- tools/testing/selftests/prctl/predump-sig-test.c | 160 +++++++++++++++++++++++ 2 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/prctl/predump-sig-test.c diff --git a/tools/testing/selftests/prctl/Makefile b/tools/testing/selftests/prctl/Makefile index c7923b2..f8d60d5 100644 --- a/tools/testing/selftests/prctl/Makefile +++ b/tools/testing/selftests/prctl/Makefile @@ -5,7 +5,7 @@ ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/) ifeq ($(ARCH),x86) TEST_PROGS := disable-tsc-ctxt-sw-stress-test disable-tsc-on-off-stress-test \ - disable-tsc-test + disable-tsc-test predump-sig-test all: $(TEST_PROGS) include ../lib.mk diff --git a/tools/testing/selftests/prctl/predump-sig-test.c b/tools/testing/selftests/prctl/predump-sig-test.c new file mode 100644 index 0000000..15d62691 --- /dev/null +++ b/tools/testing/selftests/prctl/predump-sig-test.c @@ -0,0 +1,160 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2018, Enke Chen, Cisco Systems, Inc. + * + * Tests for prctl(PR_SET_PREDUMP_SIG, ...) / prctl(PR_GET_PREDUMP_SIG, ...) + * + * When set with prctl(), the specified signal is sent to the parent process + * prior to the coredump of a child process. + * + * Usage: ./predump-sig-test {SIGUSR1 | SIGRT2} + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef PR_SET_PREDUMP_SIG +#define PR_SET_PREDUMP_SIG 54 +#define PR_GET_PREDUMP_SIG 55 +#endif + +#define SIGRT2 (SIGRTMIN + 1) + +#define handle_error(msg) \ + do { perror(msg); exit(EXIT_FAILURE); } while (0) + +static sig_idx; +static siginfo_t siginfo_rcv[2]; + +static void sigaction_func(int sig, siginfo_t *siginfo, void *arg) +{ + memcpy(&siginfo_rcv[sig_idx], siginfo, sizeof(siginfo_t)); + sig_idx++; +} + +static int set_sigaction(int sig) +{ + struct sigaction new_action; + int rc; + + memset(&new_action, 0, sizeof(struct sigaction)); + new_action.sa_sigaction = sigaction_func; + new_action.sa_flags = SA_SIGINFO; + sigemptyset(&new_action.sa_mask); + + return sigaction(sig, &new_action, NULL); +} + +static int test_prctl(int sig) +{ + int sig2, rc; + + rc = prctl(PR_SET_PREDUMP_SIG, sig, 0, 0, 0); + if (rc < 0) + handle_error("prctl: setting"); + + rc = prctl(PR_GET_PREDUMP_SIG, &sig2, 0, 0, 0); + if (rc < 0) + handle_error("prctl: getting"); + + if (sig2 != sig) { + printf("prctl: sig %d, post %d\n", sig, sig2); + return -1; + } + return 0; +} + +static void child_fn(void) +{ + int rc, sig; + + printf("\nChild pid: %ld\n", (long)getpid()); + + /* Test: Child should not inherit the predump_signal */ + rc = prctl(PR_GET_PREDUMP_SIG, &sig, 0, 0, 0); + if (rc < 0) + handle_error("prctl: child"); + + printf("child: predump_signal %d\n", sig); + + /* Force coredump here */ + printf("child: calling abort()\n"); + fflush(stdout); + abort(); +} + +static int parent_fn(pid_t child_pid) +{ + int i, status, count; + siginfo_t *si; + pid_t w; + + for (count = 0; count < 2; count++) { + w = waitpid(child_pid, &status, 0); + printf("\nwaitpid: %d\n", w); + if (w < 0) + perror("waitpid"); + + si = &siginfo_rcv[count]; + printf("signal: si_signo %d, si_pid %ld, si_uid %d\n", + si->si_signo, si->si_pid, si->si_uid); + printf("siginfo: si_errno %d, si_code %d, si_status %d\n", + si->si_errno, si->si_code, si->si_status); + } + fflush(stdout); +} + +int main(int argc, char *argv[]) +{ + pid_t child_pid; + int rc, signo; + + if (argc != 2) { + printf("invalid number of arguments\n"); + exit(EXIT_FAILURE); + } + + if (strcmp(argv[1], "SIGUSR1") == 0) + signo = SIGUSR1; + else if (strcmp(argv[1], "SIGRT2") == 0) + signo = SIGRT2; + else { + printf("invalid argument for signal\n"); + fflush(stdout); + exit(EXIT_FAILURE); + } + + rc = set_sigaction(SIGCHLD); + if (rc < 0) + handle_error("set_sigaction: SIGCHLD"); + + if (signo != SIGCHLD) { + rc = set_sigaction(signo); + if (rc < 0) + handle_error("set_sigaction: SIGCHLD"); + } + + /* Test: prctl() setting */ + rc = test_prctl(0); + printf("prctl: sig %d %s\n", 0, (rc == 0) ? "PASS" : "FAIL"); + + rc = test_prctl(signo); + printf("prctl: sig %d %s\n", signo, (rc == 0) ? "PASS" : "FAIL"); + + child_pid = fork(); + if (child_pid == -1) + handle_error("fork"); + + if (child_pid == 0) { /* Code executed by child */ + child_fn(); + } else { /* Code executed by parent */ + parent_fn(child_pid); + exit(EXIT_SUCCESS); + } +} -- 1.8.3.1