From patchwork Wed May 15 12:01:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Amann X-Patchwork-Id: 1099967 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=213.254.12.146; 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 [213.254.12.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 453tSk5R3Xz9sBV for ; Wed, 15 May 2019 22:01:42 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 2DCB7294B5F for ; Wed, 15 May 2019 14:01:40 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-6.smtp.seeweb.it (in-6.smtp.seeweb.it [217.194.8.6]) by picard.linux.it (Postfix) with ESMTP id D8AB3294B04 for ; Wed, 15 May 2019 14:01:32 +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-6.smtp.seeweb.it (Postfix) with ESMTPS id 7C1F0141CFA5 for ; Wed, 15 May 2019 14:01:32 +0200 (CEST) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id C9E8AAD03 for ; Wed, 15 May 2019 12:01:31 +0000 (UTC) From: Christian Amann To: ltp@lists.linux.it Date: Wed, 15 May 2019 14:01:15 +0200 Message-Id: <20190515120116.11589-3-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-6.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-6.smtp.seeweb.it Subject: [LTP] [PATCH v1 3/4] syscalls/pidfd_send_signal02 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 test to check basic error handling of the syscall. Signed-off-by: Christian Amann --- runtest/syscalls | 1 + .../kernel/syscalls/pidfd_send_signal/.gitignore | 1 + .../pidfd_send_signal/pidfd_send_signal02.c | 104 +++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal02.c diff --git a/runtest/syscalls b/runtest/syscalls index c719b77b2..fd57e2ee7 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -839,6 +839,7 @@ personality01 personality01 personality02 personality02 pidfd_send_signal01 pidfd_send_signal01 +pidfd_send_signal02 pidfd_send_signal02 pipe01 pipe01 pipe02 pipe02 diff --git a/testcases/kernel/syscalls/pidfd_send_signal/.gitignore b/testcases/kernel/syscalls/pidfd_send_signal/.gitignore index 7ccbf2d80..6ea6401a8 100644 --- a/testcases/kernel/syscalls/pidfd_send_signal/.gitignore +++ b/testcases/kernel/syscalls/pidfd_send_signal/.gitignore @@ -1 +1,2 @@ /pidfd_send_signal01 +/pidfd_send_signal02 diff --git a/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal02.c b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal02.c new file mode 100644 index 000000000..25e66a214 --- /dev/null +++ b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal02.c @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2019 SUSE LLC + * Author: Christian Amann + */ +/* + * Tests basic error handling of the pidfd_send_signal + * system call. + * + * 1) Pass invalid flag value to syscall (value chosen + * to be unlikely to collide with future extensions) + * -> EINVAL + * 2) Pass a file descriptor that is corresponding to a + * regular file instead of a pid directory + * -> EBADF + * 3) Pass a signal that is different from the one used + * to initialize the siginfo_t struct + * -> EINVAL + * 4) Try to send signal to other process (init) with + * missing privileges + * -> EPERM + */ + +#define _GNU_SOURCE + +#include +#include "pwd.h" +#include "tst_safe_pthread.h" +#include "pidfd_send_signal.h" + +#define CORRECT_SIGNAL SIGUSR1 +#define DIFFERENT_SIGNAL SIGUSR2 + +static siginfo_t info; +static int pidfd; +static int init_pidfd; +static int dummyfd; + +static struct tcase { + int *fd; + siginfo_t *siginf; + int signal; + int flags; + int exp_err; +} tcases[] = { + {&pidfd, &info, CORRECT_SIGNAL, 99999, EINVAL}, + {&dummyfd, &info, CORRECT_SIGNAL, 0, EBADF}, + {&pidfd, &info, DIFFERENT_SIGNAL, 0, EINVAL}, + {&init_pidfd, &info, CORRECT_SIGNAL, 0, EPERM}, +}; + +static void verify_pidfd_send_signal(unsigned int n) +{ + struct tcase *tc = &tcases[n]; + + TEST(tst_pidfd_send_signal(*tc->fd, tc->signal, tc->siginf, tc->flags)); + if (tc->exp_err != TST_ERR) { + tst_res(TFAIL | TTERRNO, + "pidfd_send_signal() did not fail with %s but", + tst_strerrno(tc->exp_err)); + return; + } + + tst_res(TPASS, + "pidfd_send_signal() failed as expected!"); +} + +static void setup(void) +{ + struct passwd *pw; + + pidfd = SAFE_OPEN("/proc/self", O_DIRECTORY | O_CLOEXEC); + init_pidfd = SAFE_OPEN("/proc/1", O_DIRECTORY | O_CLOEXEC); + dummyfd = SAFE_OPEN("dummy_file", O_RDWR | O_CREAT, 0664); + + if (getuid() == 0) { + pw = SAFE_GETPWNAM("nobody"); + SAFE_SETUID(pw->pw_uid); + } + + memset(&info, 0, sizeof(info)); + info.si_signo = CORRECT_SIGNAL; + info.si_code = SI_QUEUE; + info.si_pid = getpid(); + info.si_uid = getuid(); +} + +static void cleanup(void) +{ + if (dummyfd > 0) + SAFE_CLOSE(dummyfd); + if (init_pidfd > 0) + SAFE_CLOSE(init_pidfd); + if (pidfd > 0) + SAFE_CLOSE(pidfd); +} + +static struct tst_test test = { + .test = verify_pidfd_send_signal, + .tcnt = ARRAY_SIZE(tcases), + .setup = setup, + .cleanup = cleanup, + .needs_tmpdir = 1, +};