From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 429C7C43441 for ; Tue, 27 Nov 2018 22:54:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id F278C2086B for ; Tue, 27 Nov 2018 22:54:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=cisco.com header.i=@cisco.com header.b="F2NE1RLd" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F278C2086B Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=cisco.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726731AbeK1JyU (ORCPT ); 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