From patchwork Wed May 15 12:01:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Amann X-Patchwork-Id: 1099968 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.com Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 453tSr1f7cz9s3q for ; Wed, 15 May 2019 22:01:48 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 5F245294BB0 for ; Wed, 15 May 2019 14:01:43 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-7.smtp.seeweb.it (in-7.smtp.seeweb.it [IPv6:2001:4b78:1:20::7]) by picard.linux.it (Postfix) with ESMTP id 1231C294B23 for ; Wed, 15 May 2019 14:01:36 +0200 (CEST) Received: from mx1.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-7.smtp.seeweb.it (Postfix) with ESMTPS id A2F5620149F for ; Wed, 15 May 2019 14:01:35 +0200 (CEST) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 01498AC4E for ; Wed, 15 May 2019 12:01:35 +0000 (UTC) From: Christian Amann To: ltp@lists.linux.it Date: Wed, 15 May 2019 14:01:16 +0200 Message-Id: <20190515120116.11589-4-camann@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20190515120116.11589-1-camann@suse.com> References: <20190515120116.11589-1-camann@suse.com> X-Virus-Scanned: clamav-milter 0.99.2 at in-7.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=-0.0 required=7.0 tests=SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-7.smtp.seeweb.it Subject: [LTP] [PATCH v1 4/4] syscalls/pidfd_send_signal03 X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Add testcase to check if the syscall will send a signal to a process with the same PID as the targeted process. Signed-off-by: Christian Amann --- runtest/syscalls | 1 + .../kernel/syscalls/pidfd_send_signal/.gitignore | 1 + .../pidfd_send_signal/pidfd_send_signal03.c | 118 +++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal03.c diff --git a/runtest/syscalls b/runtest/syscalls index fd57e2ee7..a534a663b 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -840,6 +840,7 @@ personality02 personality02 pidfd_send_signal01 pidfd_send_signal01 pidfd_send_signal02 pidfd_send_signal02 +pidfd_send_signal03 pidfd_send_signal03 pipe01 pipe01 pipe02 pipe02 diff --git a/testcases/kernel/syscalls/pidfd_send_signal/.gitignore b/testcases/kernel/syscalls/pidfd_send_signal/.gitignore index 6ea6401a8..cba7d50a4 100644 --- a/testcases/kernel/syscalls/pidfd_send_signal/.gitignore +++ b/testcases/kernel/syscalls/pidfd_send_signal/.gitignore @@ -1,2 +1,3 @@ /pidfd_send_signal01 /pidfd_send_signal02 +/pidfd_send_signal03 diff --git a/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal03.c b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal03.c new file mode 100644 index 000000000..ee17e714e --- /dev/null +++ b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal03.c @@ -0,0 +1,118 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2019 SUSE LLC + * Author: Christian Amann + */ +/* + * This test checks if the pidfd_send_signal syscall wrongfully sends + * a signal to a new process which inherited the PID of the actual + * target process. + * In order to do so it is necessary to start a process with a pre- + * determined PID. This is accomplished by writing to the + * /proc/sys/kernel/ns_last_pid file. + * By utilizing this, this test forks two children with the same PID. + * It is then checked, if the syscall will send a signal to the second + * child using the pidfd of the first one. + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include "pidfd_send_signal.h" +#include "tst_safe_pthread.h" + +#define PIDTRIES 3 + +static char *last_pid_file; +static int pidfd, last_pidfd; + +static void verify_pidfd_send_signal(void) +{ + pid_t pid, new_pid; + char pid_filename[32]; + char pid_str[16]; + int i, fail; + + fail = 1; + for (i = 1; i <= PIDTRIES; i++) { + pid = SAFE_FORK(); + if (pid == 0) { + TST_CHECKPOINT_WAIT(0); + return; + } + + sprintf(pid_filename, "/proc/%d", pid); + pidfd = SAFE_OPEN(pid_filename, O_DIRECTORY | O_CLOEXEC); + + TST_CHECKPOINT_WAKE(0); + tst_reap_children(); + + /* Manipulate PID for next process */ + sprintf(pid_str, "%d", pid - 1); + SAFE_LSEEK(last_pidfd, 0, SEEK_SET); + SAFE_WRITE(1, last_pidfd, pid_str, strlen(pid_str)); + + new_pid = SAFE_FORK(); + if (new_pid == 0) { + TST_CHECKPOINT_WAIT(i); + return; + } else if (new_pid == pid) { + fail = 0; + break; + } + + if (i < PIDTRIES) + tst_res(TINFO, + "Failed to set correct PID, trying again..."); + SAFE_CLOSE(pidfd); + TST_CHECKPOINT_WAKE(i); + tst_reap_children(); + } + if (fail) + tst_brk(TBROK, + "Could not set new child to same PID as the old one!"); + + TEST(tst_pidfd_send_signal(pidfd, SIGUSR1, NULL, 0)); + if (TST_RET == -1 && TST_ERR == ESRCH) { + tst_res(TPASS, + "Did not send signal to wrong process with same PID!"); + } else + tst_res(TFAIL | TTERRNO, + "pidf_send_signal() ended unexpectedly - return value: %ld, error", + TST_RET); + + TST_CHECKPOINT_WAKE(i); + tst_reap_children(); + + SAFE_CLOSE(pidfd); +} + +static void setup(void) +{ + last_pid_file = "/proc/sys/kernel/ns_last_pid"; + if (access(last_pid_file, F_OK) == -1) + tst_brk(TCONF, "%s does not exist, cannot set PIDs", + last_pid_file); + last_pidfd = SAFE_OPEN(last_pid_file, O_RDWR); +} + +static void cleanup(void) +{ + tst_reap_children(); + if (pidfd > 0) + SAFE_CLOSE(pidfd); + if (last_pidfd > 0) + SAFE_CLOSE(last_pidfd); +} + +static struct tst_test test = { + .test_all = verify_pidfd_send_signal, + .setup = setup, + .cleanup = cleanup, + .needs_root = 1, + .needs_checkpoints = 1, + .forks_child = 1, + .timeout = 20, +};