public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Christian Amann <camann@suse.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2 3/4] syscalls/pidfd_send_signal02
Date: Tue,  4 Jun 2019 13:47:01 +0200	[thread overview]
Message-ID: <20190604114702.28255-2-camann@suse.com> (raw)
In-Reply-To: <20190604114702.28255-1-camann@suse.com>

Add test to check basic error handling of the syscall.

Signed-off-by: Christian Amann <camann@suse.com>
---
 runtest/syscalls                                   |   1 +
 .../kernel/syscalls/pidfd_send_signal/.gitignore   |   1 +
 .../pidfd_send_signal/pidfd_send_signal02.c        | 106 +++++++++++++++++++++
 3 files changed, 108 insertions(+)
 create mode 100644 testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal02.c

diff --git a/runtest/syscalls b/runtest/syscalls
index bd71a12f8..90a1cf8e6 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -837,6 +837,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..83e0790bf
--- /dev/null
+++ b/testcases/kernel/syscalls/pidfd_send_signal/pidfd_send_signal02.c
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 SUSE LLC
+ * Author: Christian Amann <camann@suse.com>
+ */
+/*
+ * 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 <signal.h>
+#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(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 | TTERRNO,
+		"pidfd_send_signal() failed as expected");
+}
+
+static void setup(void)
+{
+	struct passwd *pw;
+
+	check_syscall_support();
+
+	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,
+};
-- 
2.16.4


  reply	other threads:[~2019-06-04 11:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-04 11:47 [LTP] [PATCH v2 2/4] syscalls/pidfd_send_signal01 Christian Amann
2019-06-04 11:47 ` Christian Amann [this message]
2019-06-12 13:54   ` [LTP] [PATCH v2 3/4] syscalls/pidfd_send_signal02 Petr Vorel
2019-06-04 11:47 ` [LTP] [PATCH v2 4/4] syscalls/pidfd_send_signal03 Christian Amann
2019-06-12 14:13   ` Petr Vorel
2019-06-06 13:10 ` [LTP] [PATCH v2 2/4] syscalls/pidfd_send_signal01 Cyril Hrubis
2019-06-11 11:12   ` Christian Amann
2019-06-12 13:41 ` Petr Vorel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190604114702.28255-2-camann@suse.com \
    --to=camann@suse.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox