Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp82237ima; Thu, 25 Oct 2018 15:57:46 -0700 (PDT) X-Google-Smtp-Source: AJdET5fV9QwonPs22p5l4clhH/s0ho2Q+rBULcMH4mMAV9uD0VhH3lLqr8ewfZ5H+Yy6dHTcI3Ew X-Received: by 2002:a63:87c3:: with SMTP id i186mr1015690pge.74.1540508266317; Thu, 25 Oct 2018 15:57:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540508266; cv=none; d=google.com; s=arc-20160816; b=pJu3f2NvxhclG8A7xdPdr6cVZf7HzGgs16S1hO9j0JfIaseHWF7PNIh2oXitaJ4CwE CJBx00EIZCknxAVGwI6udaB3imCeIW4MNZKYvRB0okPLScYOh34dNlZ0+uToIX+bC+Xf 3oZ+9bSaCACm7GrzGkifo6Y68TKZxGIEG8vjUAC/IDpXm8tVhXPhySFJyeg7iTePrOFk m1mBWZdTu+KO9GXFjVBo3kJLEecWyVz357FsVHISYglel/kpB5B7h9rovdms0nWDoyba ZDY3TMLJkJp2he/nhuesLNkHM9oGjWBwhZWEaKxiZYuq4r7jYySOE0eW3TVgHDsH2hr5 9K0g== 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=TnSeToGbAFR5ldLYcmm8wk/02NF/SVTHAN6KGNwOfOM=; b=oGlz7Oci7Q/rxwYrfVs4ZqWkZmnFUNCOyqhLMmtvUj6Mo2w90IRQO2UFGIc+Mi0i+w +R6DbgxLdPNzOxYq3z4Ooq9nZNeWY6C9bshAeF6epa3U364vl4NGzlJ9bePuTzl6Ht7r DeZxLUxOa1Qu9Baup8Qyse1ZJRN3xReO6G6Ebidv2tPiF2IUEOz0RJOgCEPU4f9qWW3s gP1q/vlmuRr4JiCm8TsZrKoJgqKsH1dhgUNbGkqu+dX744Id4BDU2K67PQmbYCGZL7jh B/u6i/r7cCUEERC1WNmZ2igU6uFWVrQDaLE3UshAei77Mw/1os/2//i1RAzQ67WvVtSb U/Sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cisco.com header.s=iport header.b=AfNzroLh; 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 p25-v6si9801365pli.239.2018.10.25.15.57.29; Thu, 25 Oct 2018 15:57:46 -0700 (PDT) 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=AfNzroLh; 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 S1727694AbeJZHbB (ORCPT + 99 others); Fri, 26 Oct 2018 03:31:01 -0400 Received: from alln-iport-6.cisco.com ([173.37.142.93]:36225 "EHLO alln-iport-6.cisco.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725790AbeJZHbB (ORCPT ); Fri, 26 Oct 2018 03:31:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=cisco.com; i=@cisco.com; l=4955; q=dns/txt; s=iport; t=1540508187; x=1541717787; h=subject:to:cc:references:from:message-id:date: mime-version:in-reply-to:content-transfer-encoding; bh=q+Q+0Txx2qwWQPSAtle8JtgRB4QL6sQXIhU7O5ADtfg=; b=AfNzroLhSkxseQvpZH/pBgQXIlU4Sew373rDq4ylGzn9X9Sa5R7nUqDZ FtMacg9X5kBl3nWtyL3GLsgExWejMkepTVaXaeU3hx1mj+MTkQlx/MUel os1c6C3adnDBXh6Hq8F9PNZHc9uo92ZvvMro0guKFn1k2JsB/LPh4eXYS E=; X-IronPort-AV: E=Sophos;i="5.54,425,1534809600"; d="scan'208";a="191105236" Received: from alln-core-8.cisco.com ([173.36.13.141]) by alln-iport-6.cisco.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Oct 2018 22:56:27 +0000 Received: from [10.154.208.133] ([10.154.208.133]) by alln-core-8.cisco.com (8.15.2/8.15.2) with ESMTP id w9PMuOC8009082; Thu, 25 Oct 2018 22:56:24 GMT Subject: [PATCH] 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" , Roman Gushchin , Marcos Paulo de Souza , Oleg Nesterov , Dominik Brodowski , Cyrill Gorcunov , Yang Shi , Jann Horn , Kees Cook Cc: x86@kernel.org, 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> From: Enke Chen Message-ID: <24d8585c-e1d8-b363-35e5-5343142c3355@cisco.com> Date: Thu, 25 Oct 2018 15:56:25 -0700 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: <7741efa7-a3f8-62a1-ba52-613883164643@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.133, [10.154.208.133] X-Outbound-Node: alln-core-8.cisco.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Dependency: [PATCH] kernel/signal: Signal-based pre-coredump 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