Skip to contentSkip to conversation

cleanup capset testcase

PATCH Superseded Yang Xu 2 participants 22 messages Dec 17, 2019 – Jan 15, 2020

lore
Share & tools
lore

Yang Xu

author
#0 lore raw

cover cleanup capset testcase

I cleanup capset testcase as below:

capset01: test on three versions
capset02: add various EPERM error cases
capset03: add EPERM error test without CAP_SETPCAP
capset04: add new EPERM error test with vfs cap support

I have tested them with root/nobody user and debug EPERM error in kernel code (security/commoncap.c cap_capset function ). They are ok.

Yang Xu (4):
syscalls/capset01: Cleanup & convert to new library syscalls/capset02: Cleanup & convert to new library syscalls/capset03: add new EPERM error test without CAP_SETPCAP syscalls/capset04: add new EPERM error test with vfs cap support

 runtest/syscalls                            |   2 +
 testcases/kernel/syscalls/capset/.gitignore |   2 +
 testcases/kernel/syscalls/capset/capset01.c | 150 ++------
 testcases/kernel/syscalls/capset/capset02.c | 364 ++++++++------------
 testcases/kernel/syscalls/capset/capset03.c |  65 ++++
 testcases/kernel/syscalls/capset/capset04.c |  86 +++++
 6 files changed, 319 insertions(+), 350 deletions(-)
 create mode 100644 testcases/kernel/syscalls/capset/capset03.c
 create mode 100644 testcases/kernel/syscalls/capset/capset04.c
Signature · 1 line
2.18.0
author
#1 lore raw

PATCH 1/4 syscalls/capset01: Cleanup & convert to new library

Test capset() syscall on three versions.

---
 testcases/kernel/syscalls/capset/capset01.c | 150 ++++----------------
 1 file changed, 29 insertions(+), 121 deletions(-)
testcases/kernel/syscalls/capset/capset01.c +29 -121
index 9c61773bb..75cdd83b9 100644
@@ -1,142 +1,50 @@
 /*
  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-/**********************************************************
- *
- *    TEST IDENTIFIER	: capset01
- *
- *    EXECUTED BY	: anyone
- *
- *    TEST TITLE	: Basic test for capset(2)
- *
- *    TEST CASE TOTAL	: 1
- *
  *    AUTHOR		: Saji Kumar.V.R <[email protected]>
  *
- *    SIGNALS
- * 	Uses SIGUSR1 to pause before test if option set.
- * 	(See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- *	This is a Phase I test for the capset(2) system call.
- *	It is intended to provide a limited exposure of the system call.
- *
- * 	Setup:
- * 	  Setup signal handling.
- *	  Pause for SIGUSR1 if option specified.
- *	  call capget() to save the current capability data
- *
- * 	Test:
- *	 Loop if the proper options are given.
- * 	  call capset() with the saved data
- *	  if return value == 0
- *		Test passed
- *	  Otherwise
- *		Test failed
- *
- * 	Cleanup:
- * 	  Print errno log and/or timing stats if options given
- *
- * USAGE:  <for command-line>
- * capset01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *			where,  -c n : Run n copies concurrently.
- *				-e   : Turn on errno logging.
- *				-h   : Show help screen
- *				-f   : Turn off functional testing
- *				-i n : Execute test n times.
- *				-I x : Execute test for x seconds.
- *				-p   : Pause for SIGUSR1 before starting
- *				-P x : Pause for x seconds between iterations.
- *				-t   : Turn on syscall timing.
- *
  * CHANGES:
  *  2005/01/01: add an hint to a possible solution when test fails
  *              - Ricky Ng-Adam <[email protected]>
  ****************************************************************/
+#include <sys/types.h>
 #include <unistd.h>
-#include <errno.h>
-#include "test.h"
+#include "tst_test.h"
 #include "lapi/syscalls.h"
-
-/**************************************************************************/
-/*                                                                        */
-/*   Some archs do not have the manpage documented sys/capability.h file, */
-/*   and require the use of the line below                                */
-
 #include <linux/capability.h>
 
-/*   If you are having issues with including this file and have the sys/  */
-/*   version, then you may want to try switching to it. -Robbie W.        */
-/**************************************************************************/
+static struct tcase {
+	int version;
+	char *message;
+} tcases[] = {
+	{0x19980330, "Test on LINUX_CAPABILITY_VERSION_1"},
+	{0x20071026, "Test on LINUX_CAPABILITY_VERSION_2"},
+	{0x20080522, "Test on LINUX_CAPABILITY_VERSION_3"},
+};
 
-static void setup();
-static void cleanup();
 
-char *TCID = "capset01";
-int TST_TOTAL = 1;
-
-static struct __user_cap_header_struct header;	/* cap_user_header_t is a pointer
-						   to __user_cap_header_struct */
-
-static struct __user_cap_data_struct data;	/* cap_user_data_t is a pointer to
-						   __user_cap_data_struct */
-
-int main(int ac, char **av)
+static void verify_capset(unsigned int n)
 {
+	struct tcase *tc = &tcases[n];
+	struct __user_cap_header_struct header;
+	struct __user_cap_data_struct data[2];
 
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
+	tst_res(TINFO, "%s", tc->message);
+	header.version = tc->version;
+	header.pid = getpid();
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		TEST(ltp_syscall(__NR_capset, &header, &data));
-
-		if (TEST_RETURN == 0) {
-			tst_resm(TPASS, "capset() returned %ld", TEST_RETURN);
-		} else {
-			tst_resm(TFAIL | TTERRNO,
-				 "Test Failed, capset() returned %ld"
-				 " Maybe you need to do `modprobe capability`?",
-				 TEST_RETURN);
-		}
+	if (tst_syscall(__NR_capget, &header, data) == -1) {
+		tst_res(TFAIL | TTERRNO, "capget() failed");
+		return;
 	}
 
-	cleanup();
-
-	tst_exit();
+	TEST(tst_syscall(__NR_capset, &header, data));
+	if (TST_RET == 0)
+		tst_res(TPASS, "capset() returned %ld", TST_RET);
+	else
+		tst_res(TFAIL | TTERRNO, "Test Failed, capset() returned %ld", TST_RET);
 }
 
-void setup(void)
-{
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	header.version = _LINUX_CAPABILITY_VERSION;
-	header.pid = 0;
-	if (ltp_syscall(__NR_capget, &header, &data) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "capget() failed");
-}
-
-void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_capset,
+};
Signature · 1 line
2.18.0
#2 lore raw

PATCH 1/4 syscalls/capset01: Cleanup & convert to new library

Hi!
This test was missing the SPDX licence header, I've added it and pushed, thanks.

Also we do not cover the pid == 0 case here either, that should be added later on.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#3 lore raw

PATCH 1/4 syscalls/capset01: Cleanup & convert to new library

Hi

Quoted 7 lines

Hi!
This test was missing the SPDX licence header, I've added it and pushed, thanks.

Also we do not cover the pid == 0 case here either, that should be added later on.

Like capget01 discussion, I will add it.

author
#4 lore raw

PATCH 2/4 syscalls/capset02: Cleanup & convert to new library

In reply to Yang Xu

It adds a preferred linux capabilities version check like capget02. Also, it adds various cases about EPERM error.

---
 testcases/kernel/syscalls/capset/capset02.c | 364 ++++++++------------
 1 file changed, 135 insertions(+), 229 deletions(-)
testcases/kernel/syscalls/capset/capset02.c +135 -229
index aece29511..4df555ce4 100644
@@ -1,261 +1,167 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
+ * Author: Saji Kumar.V.R <[email protected]>
+ *
+ * Tests basic error handling of the capset syscall.
+ * 1) capset() fails with errno set to EFAULT if an invalid address
+ * is given for header.
+ * 2) capset() fails with errno set to EFAULT if an invalid address
+ * is given for data.
+ * 3) capset() fails with errno set ot EINVAL if an unused pid is
+ * given for header->pid.
+ * 4) capset() fails with errno set to EINVAL if an invalid value
+ * is given for header->version.
+ * 5) capset() fails with errno set to EPERM if the new_Effective is
+ * not a subset of the new_Permitted.
+ * 6) capset() fails with errno set to EPERM if the new_Permitted is
+ * not a subset of the old_Permitted.
+ * 7) capset() fails with errno set ot EPERM if the new_Inheritable is
+ * not a subset of  the old_Inheritable and bounding set.
  */
-/**********************************************************
- *
- *    TEST IDENTIFIER	: capset02
- *
- *    EXECUTED BY	: anyone
- *
- *    TEST TITLE	: Tests for error conditions.
- *
- *    TEST CASE TOTAL	: 4
- *
- *    AUTHOR		: Saji Kumar.V.R <[email protected]>
- *
- *    SIGNALS
- * 	Uses SIGUSR1 to pause before test if option set.
- * 	(See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- *	Verify that
- *	1) capset() fails with errno set to EFAULT if an invalid address
- *	   is given for header
- *	2) capset() fails with errno set to EFAULT if an invalid address
- *	   is given for data
- *	3) capset() fails with errno set to EINVAL if an invalid value
- *	   is given for header->version
- *	4) capset() fails with errno set to EPERM the process does not
- *	   have enough privilege to set capabilities
- *
- *
- * 	Setup:
- * 	  Setup signal handling.
- *	  Pause for SIGUSR1 if option specified.
- *	  Call capget() to save current capability data
- *
- * 	Test:
- *	 Loop if the proper options are given.
- *	  do test specific setup.
- * 	  call capset with proper arguments
- *	  if capset() fails with expected errno
- *		Test passed
- *	  Otherwise
- *		Test failed
- *	  do test specific cleanup
- *
- * 	Cleanup:
- * 	  Print errno log and/or timing stats if options given
- *
- * USAGE:  <for command-line>
- * capset02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *			where,  -c n : Run n copies concurrently.
- *				-e   : Turn on errno logging.
- *				-h   : Show help screen
- *				-f   : Turn off functional testing
- *				-i n : Execute test n times.
- *				-I x : Execute test for x seconds.
- *				-p   : Pause for SIGUSR1 before starting
- *				-P x : Pause for x seconds between iterations.
- *				-t   : Turn on syscall timing.
- *
- ****************************************************************/
+#include <stdlib.h>
 #include <sys/types.h>
-#include <sys/wait.h>
-#include <errno.h>
-#include <pwd.h>
-#include <signal.h>
-#include <string.h>
 #include <unistd.h>
-#include "test.h"
-#include "safe_macros.h"
+#include <sys/prctl.h>
+#include "tst_test.h"
 #include "lapi/syscalls.h"
-
-/**************************************************************************/
-/*                                                                        */
-/*   Some archs do not have the manpage documented sys/capability.h file, */
-/*   and require the use of the line below                                */
-
 #include <linux/capability.h>
 
-/*   If you are having issues with including this file and have the sys/  */
-/*   version, then you may want to try switching to it. -Robbie W.        */
-/**************************************************************************/
+#define CAP1 (1 << CAP_NET_RAW | 1 << CAP_CHOWN  | 1 << CAP_SETPCAP)
+#define CAP2 (CAP1 | 1 << CAP_KILL)
 
-#define INVALID_VERSION 0
-
-static void setup(void);
-static void cleanup(void);
-static void test_setup(int, char *);
-static void child_func(void);
+static unsigned int check_root_flag, drop_flag;
+static struct __user_cap_header_struct header, bad_version_header, unused_pid_header;
+static struct __user_cap_data_struct data[2];
+static struct __user_cap_data_struct good_data[2] = {
+	{
+		.effective = CAP1,
+		.permitted = CAP1,
+		.inheritable = CAP1,
+	},
+};
 
-static pid_t child_pid = -1;
+static struct __user_cap_data_struct bad_data_pe[2] = {
+	{
+		.effective = CAP2,
+		.permitted = CAP1,
+		.inheritable = CAP1,
+	},
+};
 
-char *TCID = "capset02";
+static struct __user_cap_data_struct bad_data_pp[2] = {
+	{
+		.effective = CAP1,
+		.permitted = CAP2,
+		.inheritable = CAP1,
+	},
+};
 
-static struct __user_cap_header_struct header;
-static struct __user_cap_data_struct data;
+static struct __user_cap_data_struct bad_data_pi[2] = {
+	{
+		.effective = CAP1,
+		.permitted = CAP1,
+		.inheritable = CAP2,
+	},
+};
 
-struct test_case_t {
+static struct tcase {
 	cap_user_header_t headerp;
 	cap_user_data_t datap;
-	int exp_errno;
-	char *errdesc;
-} test_cases[] = {
-#ifndef UCLINUX
-	/* Skip since uClinux does not implement memory protection */
-	{
-	(cap_user_header_t) - 1, &data, EFAULT, "EFAULT"}, {
-	&header, (cap_user_data_t) - 1, EFAULT, "EFAULT"},
-#endif
-	{
-	&header, &data, EINVAL, "EINVAL"}, {
-&header, &data, EPERM, "EPERM"},};
-
-int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
-
-int main(int ac, char **av)
+	int exp_err;
+	/*
+	 * 1 needs reset header version
+	 * 2 needs root privilege
+	 * 3 needs drop cap in bouding set
+	 */
+	int flag;
+	char *message;
+} tcases[] = {
+	{NULL, data, EFAULT, 0, "Test bad address header"},
+	{&header, NULL, EFAULT, 0, "Test bad address data"},
+	{&unused_pid_header, data, EINVAL, 1, "Test bad pid"},
+	{&bad_version_header, data, EINVAL, 1, "Test bad version"},
+	{&header, bad_data_pe, EPERM, 0, "Test bad value data(when pE is not in pP)"},
+	{&header, bad_data_pp, EPERM, 2, "Test bad value data(when pP is not in old pP)"},
+	{&header, bad_data_pi, EPERM, 3, "Test bad value data(when pI is not in bounding set or old pI)"},
+};
+
+static void verify_capset(unsigned int n)
 {
+	struct tcase *tc = &tcases[n];
 
-	int lc, i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-#ifdef UCLINUX
-	maybe_run_child(&child_func, "");
-#endif
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-#ifdef UCLINUX
-		i = 2;
-#else
-		i = 0;
-#endif
-
-		for (; i < TST_TOTAL; i++) {
-
-			test_setup(i, av[0]);
-			TEST(ltp_syscall(__NR_capset, test_cases[i].headerp,
-				     test_cases[i].datap));
-
-			if (TEST_RETURN == -1 &&
-			    TEST_ERRNO == test_cases[i].exp_errno) {
-				tst_resm(TPASS, "capset() returned -1,"
-					 " errno: %s", test_cases[i].errdesc);
-			} else {
-				tst_resm(TFAIL | TTERRNO,
-					 "Test Failed, capset() returned %ld",
-					 TEST_RETURN);
-			}
-		}
+	tst_res(TINFO, "%s", tc->message);
+	if (tc->flag == 2 && !check_root_flag) {
+		tst_res(TCONF, "This test needs root privilege, skip it");
+		return;
+	}
+	if (tc->flag == 3 && !drop_flag) {
+		tst_res(TCONF, "This test needs to drop CAP_KILL in bounding set, skip it");
+		return;
 	}
 
-	cleanup();
-
-	tst_exit();
-
-}
-
-void setup(void)
-{
-	tst_require_root();
-
-	TEST_PAUSE;
-
+	TEST(tst_syscall(__NR_capset, tc->headerp, tc->datap));
+	if (TST_RET == 0) {
+		tst_res(TFAIL, "capset() succeed unexpectedly");
+		return;
+	}
+	if (TST_ERR == tc->exp_err)
+		tst_res(TPASS | TTERRNO, "capset() failed as expected");
+	else
+		tst_res(TFAIL | TTERRNO, "capset() expected %s got ",
+			tst_strerrno(tc->exp_err));
 	/*
-	 * Save current capability data.
-	 * header.version must be _LINUX_CAPABILITY_VERSION
+	 * When an unsupported version value is specified, it will
+	 * return the kernel preferred value of _LINUX_CAPABILITY_VERSION_?.
+	 * Since linux 2.6.26, version 3 is default. We use it.
 	 */
-	header.version = _LINUX_CAPABILITY_VERSION;
-	if (ltp_syscall(__NR_capget, &header, &data) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "capget failed");
-}
-
-void cleanup(void)
-{
-	if (0 < child_pid) {
-		kill(child_pid, SIGTERM);
-		wait(NULL);
+	if (tc->flag == 1) {
+		if (tc->headerp->version == 0x20080522)
+			tc->headerp->version = 0;
+		else
+			tst_res(TFAIL, "kernel doesn't return preferred linux"
+				" capability version when using bad version");
 	}
 }
 
-void child_func(void)
+static void setup(void)
 {
-	for (;;) {
-		sleep(10);
-	}
-}
-
-void test_setup(int i, char *argv0)
-{
-	char nobody_uid[] = "nobody";
-	struct passwd *ltpuser;
+	unsigned int i;
+	pid_t pid;
 
-	switch (i) {
-	case 0:
-		break;
+	pid = getpid();
 
-	case 1:
-		header.version = _LINUX_CAPABILITY_VERSION;
-		header.pid = 0;
-		break;
+	header.version = 0x20080522;
+	header.pid = pid;
+	bad_version_header.version = 0;
+	bad_version_header.pid = pid;
+	unused_pid_header.pid = 0x20080522;
+	unused_pid_header.pid = tst_get_unused_pid();
 
-	case 2:
-		header.version = INVALID_VERSION;
-		header.pid = 0;
-		break;
 
-	case 3:
-		header.version = _LINUX_CAPABILITY_VERSION;
-		/*
-		 * when a non-zero pid is specified, process should have
-		 * CAP_SETPCAP capability to change capabilities.
-		 * by default, CAP_SETPCAP is not enabled. So giving
-		 * a non-zero pid results in capset() failing with
-		 * errno EPERM
-		 *
-		 * Note: this seems to have changed with recent kernels
-		 * => create a child and try to set its capabilities
-		 */
-		child_pid = FORK_OR_VFORK();
-		if (child_pid == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
-		else if (child_pid == 0) {
-#ifdef UCLINUX
-			if (self_exec(argv0, "") < 0) {
-				perror("self_exec failed");
-				exit(1);
-			}
-#else
-			child_func();
-#endif
-		} else {
-			header.pid = child_pid;
-			ltpuser = getpwnam(nobody_uid);
-			if (ltpuser == NULL)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "getpwnam failed");
-			SAFE_SETEUID(cleanup, ltpuser->pw_uid);
-
-		}
-		break;
+	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
+		if (!tcases[i].headerp)
+			tcases[i].headerp = tst_get_bad_addr(NULL);
+		if (!tcases[i].datap)
+			tcases[i].datap = tst_get_bad_addr(NULL);
+	}
 
+	if (geteuid() == 0) {
+		TEST(tst_syscall(__NR_capset, &header, good_data));
+		if (TST_RET == -1)
+			tst_res(TFAIL | TTERRNO, "capset good_data failed");
+		else
+			check_root_flag = 1;
+		TEST(prctl(PR_CAPBSET_DROP, CAP_KILL));
+		if (TST_RET == -1)
+			tst_res(TFAIL | TTERRNO, "drop CAP_KILL failed");
+		else
+			drop_flag = 1;
 	}
 }
+
+static struct tst_test test = {
+	.setup = setup,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_capset,
+};
Signature · 1 line
2.18.0
author
#5 lore raw

PATCH 3/4 syscalls/capset03: add new EPERM error test without CAP_SETPCAP

In reply to Yang Xu
---
 runtest/syscalls                            |  1 +
 testcases/kernel/syscalls/capset/.gitignore |  1 +
 testcases/kernel/syscalls/capset/capset03.c | 65 +++++++++++++++++++++
 3 files changed, 67 insertions(+)
 create mode 100644 testcases/kernel/syscalls/capset/capset03.c
runtest/syscalls +1 -0
index fa87ef63f..4f481be6d 100644
@@ -44,6 +44,7 @@ capget02 capget02
 
 capset01 capset01
 capset02 capset02
+capset03 capset03
 
 cacheflush01 cacheflush01
 
testcases/kernel/syscalls/capset/.gitignore +1 -0
index 004ce7b3e..3f9a4d5e8 100644
@@ -1,2 +1,3 @@
 /capset01
 /capset02
+/capset03
testcases/kernel/syscalls/capset/capset03.c new file +65 -0

65 lines not on this page · raw message

Signature · 1 line
2.18.0
#6 lore raw

PATCH 3/4 syscalls/capset03: add new EPERM error test without CAP_SETPCAP

Hi!

testcases/kernel/syscalls/capset/capset03.c line 55

Quoted 11 lines
testcases/kernel/syscalls/capset/capset03.c new file +11 -0
+static void setup(void)
+{
+	pid_t pid;
+
+	pid = getpid();
+	header.pid = pid;
+	if (geteuid() == 0) {
+		TEST(tst_syscall(__NR_capset, &header, data));
+		if (TST_RET == -1)
+			tst_brk(TBROK | TTERRNO, "capset data failed");
+	}

Please don't do that. If tests needs root (even for a subset of the test) just set the .needs_root flag.

Quoted 18 lines
testcases/kernel/syscalls/capset/capset03.c new file +10 -0
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = verify_capset,
+	.caps = (struct tst_cap []) {
+		TST_CAP(TST_CAP_DROP, CAP_SETPCAP),
+		{}
+	},
+};
Signature · 7 lines
2.18.0




-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#7 lore raw

PATCH 3/4 syscalls/capset03: add new EPERM error test without CAP_SETPCAP

Hi

Quoted 16 lines

Hi!

testcases/kernel/syscalls/capset/capset03.c line 55

Quoted 11 lines
testcases/kernel/syscalls/capset/capset03.c new file +11 -0
+static void setup(void)
+{
+	pid_t pid;
+
+	pid = getpid();
+	header.pid = pid;
+	if (geteuid() == 0) {
+		TEST(tst_syscall(__NR_capset, &header, data));
+		if (TST_RET == -1)
+			tst_brk(TBROK | TTERRNO, "capset data failed");
+	}

Please don't do that. If tests needs root (even for a subset of the test) just set the .needs_root flag.

This test doesn't need root. These code is designed to create a envrionment for root user to generate this type EPERM error?new_Inheritable is not a subset of old_Inheritable and old_Permitted without CAP_SETPCAP?.
root user:
old pI: CAP_KILL
old pP: CAP_KILL
new pI: CAP_KILL + CAP_NET_RAW

other user:
old pI: 0
old pP: 0
new pI: CAP_KILL + CAP_NET_RAW

other user also met condition and can generate this EPERM error.

ps: In capset03, getpid() is useless, we can use pid = 0 to replace. Also, if we can use pid =0 in error test, maybe we don't need to test pid =0 in capget01/capset01.c . What do you think about it?

Quoted 19 lines
Quoted 18 lines
testcases/kernel/syscalls/capset/capset03.c new file +10 -0
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = verify_capset,
+	.caps = (struct tst_cap []) {
+		TST_CAP(TST_CAP_DROP, CAP_SETPCAP),
+		{}
+	},
+};
Signature · 7 lines
2.18.0




-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

PATCH 3/4 syscalls/capset03: add new EPERM error test without CAP_SETPCAP

Hi!

Quoted 24 lines
Quoted 6 lines
Quoted 6 lines
testcases/kernel/syscalls/capset/capset03.c new file +6 -0
+static void setup(void)
+{
+	pid_t pid;
+
+	pid = getpid();
+	header.pid = pid;
Quoted 9 lines

testcases/kernel/syscalls/capset/capset03.c line 55

Quoted 5 lines
testcases/kernel/syscalls/capset/capset03.c new file +5 -0
+	if (geteuid() == 0) {
+		TEST(tst_syscall(__NR_capset, &header, data));
+		if (TST_RET == -1)
+			tst_brk(TBROK | TTERRNO, "capset data failed");
+	}

Please don't do that. If tests needs root (even for a subset of the test) just set the .needs_root flag.

This test doesn't need root. These code is designed to create a envrionment for root user to generate this type EPERM error??new_Inheritable is not a subset of old_Inheritable and old_Permitted without CAP_SETPCAP??.
root user:
old pI: CAP_KILL
old pP: CAP_KILL
new pI: CAP_KILL + CAP_NET_RAW

Quoted 6 lines

other user:
old pI: 0
old pP: 0
new pI: CAP_KILL + CAP_NET_RAW

other user also met condition and can generate this EPERM error.

Hmm, we are testing different things under root and non-root then. When the test is executed under a regular user we assert that the system default is sane + the capset assertion, while under the root we test only capset.

It would make sense to run the test only under root to make sure that we are consistent.

Also the CAP_DROP in the tst_test structure seems to be useless to me.

Looking at man 7 capabilities, there are also transitions defined for what is supposed to happen when we change user id. It would make sense to write tests that capabilities are correctly dropped when UID changes from 0 to nonzero. Which is what this test is testing when executed as non-root, since the transition from 0 to nonzero must have happened somewhere when user has logged in.

Quoted 3 lines

ps: In capset03, getpid() is useless, we can use pid = 0 to replace. Also, if we can use pid =0 in error test, maybe we don't need to test pid =0 in capget01/capset01.c . What do you think about it?

Sure. We can use 0 in all tests if we have a least one for each syscall that tests it with pid != 0.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#9 lore raw

PATCH 3/4 syscalls/capset03: add new EPERM error test without CAP_SETPCAP

Hi!

Quoted 32 lines

Hi!

Quoted 24 lines
Quoted 6 lines
Quoted 6 lines
testcases/kernel/syscalls/capset/capset03.c new file +6 -0
+static void setup(void)
+{
+	pid_t pid;
+
+	pid = getpid();
+	header.pid = pid;
Quoted 9 lines

testcases/kernel/syscalls/capset/capset03.c line 55

Quoted 5 lines
testcases/kernel/syscalls/capset/capset03.c new file +5 -0
+	if (geteuid() == 0) {
+		TEST(tst_syscall(__NR_capset, &header, data));
+		if (TST_RET == -1)
+			tst_brk(TBROK | TTERRNO, "capset data failed");
+	}

Please don't do that. If tests needs root (even for a subset of the test) just set the .needs_root flag.

This test doesn't need root. These code is designed to create a envrionment for root user to generate this type EPERM error??new_Inheritable is not a subset of old_Inheritable and old_Permitted without CAP_SETPCAP??.
root user:
old pI: CAP_KILL
old pP: CAP_KILL
new pI: CAP_KILL + CAP_NET_RAW

Quoted 6 lines

other user:
old pI: 0
old pP: 0
new pI: CAP_KILL + CAP_NET_RAW

other user also met condition and can generate this EPERM error.

Quoted 8 lines

Hmm, we are testing different things under root and non-root then. When the test is executed under a regular user we assert that the system default is sane + the capset assertion, while under the root we test only capset.

It would make sense to run the test only under root to make sure that we are consistent.

Ok. I will make this case consistent and add .need_root flag.

Quoted 9 lines

Also the CAP_DROP in the tst_test structure seems to be useless to me.

Looking at man 7 capabilities, there are also transitions defined for what is supposed to happen when we change user id. It would make sense to write tests that capabilities are correctly dropped when UID changes from 0 to nonzero. Which is what this test is testing when executed as non-root, since the transition from 0 to nonzero must have happened somewhere when user has logged in.

In man 7 capabilities " Effect of user ID changes on capabilities", I see transitions between 0 and nonzero user IDs. But it is about capabilities?not about capset syscall. I think we should add these cases(user ID changes on capabilities) into kernel/security (such as cap_bound or filecaps). In capset, we can only test capset various EPERM error as kernel sercurity/commoncap.c cap_capset function.
---------------------------------

      if (cap_inh_is_capped() &&
             !cap_issubset(*inheritable,
                           cap_combine(old->cap_inheritable,
                                       old->cap_permitted)))
                 /* incapable of using this inheritable set */
                 return -EPERM;

         if (!cap_issubset(*inheritable,
                           cap_combine(old->cap_inheritable,
                                       old->cap_bset)))
                 /* no new pI capabilities outside bounding set */
                 return -EPERM;

         /* verify restrictions on target's new Permitted set */
         if (!cap_issubset(*permitted, old->cap_permitted))
                 return -EPERM;

         /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
         if (!cap_issubset(*effective, *permitted))
                 return -EPERM;

---------------------------------
Also, if we only run under root, CAP_DROP(CAP_SETPCAP) is needed to reproduce this EPERM error.

Quoted 7 lines
Quoted 4 lines

ps: In capset03, getpid() is useless, we can use pid = 0 to replace. Also, if we can use pid =0 in error test, maybe we don't need to test pid =0 in capget01/capset01.c . What do you think about it?

Sure. We can use 0 in all tests if we have a least one for each syscall that tests it with pid != 0.

Ok. I will do it.

Quoted 1 line
#10 lore raw

PATCH 3/4 syscalls/capset03: add new EPERM error test without CAP_SETPCAP

Hi!

Quoted 36 lines
Quoted 9 lines

Also the CAP_DROP in the tst_test structure seems to be useless to me.

Looking at man 7 capabilities, there are also transitions defined for what is supposed to happen when we change user id. It would make sense to write tests that capabilities are correctly dropped when UID changes from 0 to nonzero. Which is what this test is testing when executed as non-root, since the transition from 0 to nonzero must have happened somewhere when user has logged in.

In man 7 capabilities " Effect of user ID changes on capabilities", I see transitions between 0 and nonzero user IDs. But it is about capabilities??not about capset syscall. I think we should add these cases(user ID changes on capabilities) into kernel/security (such as cap_bound or filecaps). In capset, we can only test capset various EPERM error as kernel sercurity/commoncap.c cap_capset function.
---------------------------------

      if (cap_inh_is_capped() &&
             !cap_issubset(*inheritable,
                           cap_combine(old->cap_inheritable,
                                       old->cap_permitted)))
                 /* incapable of using this inheritable set */
                 return -EPERM;

         if (!cap_issubset(*inheritable,
                           cap_combine(old->cap_inheritable,
                                       old->cap_bset)))
                 /* no new pI capabilities outside bounding set */
                 return -EPERM;

         /* verify restrictions on target's new Permitted set */
         if (!cap_issubset(*permitted, old->cap_permitted))
                 return -EPERM;

         /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
         if (!cap_issubset(*effective, *permitted))
                 return -EPERM;
Quoted 1 line

---------------------------------

Indeed these does not belog under setcap(). Maybe we could add these checks under setuid tests, since we are testing side efect of setuid. But having these under security/ would work as well.

Quoted 2 lines

Also, if we only run under root, CAP_DROP(CAP_SETPCAP) is needed to reproduce this EPERM error.

Isn't the first thing that the test does to remove all capabilities but CAP_KILL? Why do we need to drop anything beforehand?

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#11 lore raw

PATCH 3/4 syscalls/capset03: add new EPERM error test without CAP_SETPCAP

Hi!

Quoted 34 lines

Hi!

Quoted 33 lines
Quoted 9 lines

Also the CAP_DROP in the tst_test structure seems to be useless to me.

Looking at man 7 capabilities, there are also transitions defined for what is supposed to happen when we change user id. It would make sense to write tests that capabilities are correctly dropped when UID changes from 0 to nonzero. Which is what this test is testing when executed as non-root, since the transition from 0 to nonzero must have happened somewhere when user has logged in.

In man 7 capabilities " Effect of user ID changes on capabilities", I see transitions between 0 and nonzero user IDs. But it is about capabilities??not about capset syscall. I think we should add these cases(user ID changes on capabilities) into kernel/security (such as cap_bound or filecaps). In capset, we can only test capset various EPERM error as kernel sercurity/commoncap.c cap_capset function.
---------------------------------

       if (cap_inh_is_capped() &&
              !cap_issubset(*inheritable,
                            cap_combine(old->cap_inheritable,
                                        old->cap_permitted)))
                  /* incapable of using this inheritable set */
                  return -EPERM;

          if (!cap_issubset(*inheritable,
                            cap_combine(old->cap_inheritable,
                                        old->cap_bset)))
                  /* no new pI capabilities outside bounding set */
                  return -EPERM;

          /* verify restrictions on target's new Permitted set */
          if (!cap_issubset(*permitted, old->cap_permitted))
                  return -EPERM;
Quoted 8 lines
Quoted 4 lines
          /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
          if (!cap_issubset(*effective, *permitted))
                  return -EPERM;

---------------------------------

Indeed these does not belog under setcap(). Maybe we could add these
checks under setuid tests, since we are testing side efect of setuid.
But having these under security/ would work as well.Maybe put them in setuid is better because I don't know a good

directory name for them in security(such as user_change_cap). Anyway, I will list them in my todo.

Quoted 6 lines
Quoted 3 lines

Also, if we only run under root, CAP_DROP(CAP_SETPCAP) is needed to reproduce this EPERM error.

Isn't the first thing that the test does to remove all capabilities but CAP_KILL? Why do we need to drop anything beforehand?

Yes, you are right. I forgot it. I will remove this drop and also used guarded buffer for the other capset cases .

Quoted 1 line
author
#12 lore raw

PATCH v2 1/3 syscalls/capset02: Cleanup & convert to new library

In reply to Cyril Hrubis

It adds a preferred linux capabilities version check like capget02. Also, it adds various cases about EPERM error.

--------
v1-v2:
1. use guarded buffer
2. add needs_root flag
3. remove wrong EINVAL error test about unused pid
--------

---
 testcases/kernel/syscalls/capset/capset02.c | 332 ++++++--------------
 1 file changed, 93 insertions(+), 239 deletions(-)
testcases/kernel/syscalls/capset/capset02.c +93 -239
index aece29511..15589d462 100644
@@ -1,261 +1,115 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
+ * Author: Saji Kumar.V.R <[email protected]>
+ *
+ * Tests basic error handling of the capset syscall.
+ * 1) capset() fails with errno set to EFAULT if an invalid address
+ * is given for header.
+ * 2) capset() fails with errno set to EFAULT if an invalid address
+ * is given for data.
+ * 3) capset() fails with errno set to EINVAL if an invalid value
+ * is given for header->version.
+ * 4) capset() fails with errno set to EPERM if the new_Effective is
+ * not a subset of the new_Permitted.
+ * 5) capset() fails with errno set to EPERM if the new_Permitted is
+ * not a subset of the old_Permitted.
+ * 6) capset() fails with errno set ot EPERM if the new_Inheritable is
+ * not a subset of  the old_Inheritable and bounding set.
  */
-/**********************************************************
- *
- *    TEST IDENTIFIER	: capset02
- *
- *    EXECUTED BY	: anyone
- *
- *    TEST TITLE	: Tests for error conditions.
- *
- *    TEST CASE TOTAL	: 4
- *
- *    AUTHOR		: Saji Kumar.V.R <[email protected]>
- *
- *    SIGNALS
- * 	Uses SIGUSR1 to pause before test if option set.
- * 	(See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- *	Verify that
- *	1) capset() fails with errno set to EFAULT if an invalid address
- *	   is given for header
- *	2) capset() fails with errno set to EFAULT if an invalid address
- *	   is given for data
- *	3) capset() fails with errno set to EINVAL if an invalid value
- *	   is given for header->version
- *	4) capset() fails with errno set to EPERM the process does not
- *	   have enough privilege to set capabilities
- *
- *
- * 	Setup:
- * 	  Setup signal handling.
- *	  Pause for SIGUSR1 if option specified.
- *	  Call capget() to save current capability data
- *
- * 	Test:
- *	 Loop if the proper options are given.
- *	  do test specific setup.
- * 	  call capset with proper arguments
- *	  if capset() fails with expected errno
- *		Test passed
- *	  Otherwise
- *		Test failed
- *	  do test specific cleanup
- *
- * 	Cleanup:
- * 	  Print errno log and/or timing stats if options given
- *
- * USAGE:  <for command-line>
- * capset02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *			where,  -c n : Run n copies concurrently.
- *				-e   : Turn on errno logging.
- *				-h   : Show help screen
- *				-f   : Turn off functional testing
- *				-i n : Execute test n times.
- *				-I x : Execute test for x seconds.
- *				-p   : Pause for SIGUSR1 before starting
- *				-P x : Pause for x seconds between iterations.
- *				-t   : Turn on syscall timing.
- *
- ****************************************************************/
+#include <stdlib.h>
 #include <sys/types.h>
-#include <sys/wait.h>
-#include <errno.h>
-#include <pwd.h>
-#include <signal.h>
-#include <string.h>
 #include <unistd.h>
-#include "test.h"
-#include "safe_macros.h"
+#include <sys/prctl.h>
+#include "tst_test.h"
 #include "lapi/syscalls.h"
-
-/**************************************************************************/
-/*                                                                        */
-/*   Some archs do not have the manpage documented sys/capability.h file, */
-/*   and require the use of the line below                                */
-
 #include <linux/capability.h>
 
-/*   If you are having issues with including this file and have the sys/  */
-/*   version, then you may want to try switching to it. -Robbie W.        */
-/**************************************************************************/
-
-#define INVALID_VERSION 0
-
-static void setup(void);
-static void cleanup(void);
-static void test_setup(int, char *);
-static void child_func(void);
-
-static pid_t child_pid = -1;
-
-char *TCID = "capset02";
-
-static struct __user_cap_header_struct header;
-static struct __user_cap_data_struct data;
-
-struct test_case_t {
-	cap_user_header_t headerp;
-	cap_user_data_t datap;
-	int exp_errno;
-	char *errdesc;
-} test_cases[] = {
-#ifndef UCLINUX
-	/* Skip since uClinux does not implement memory protection */
-	{
-	(cap_user_header_t) - 1, &data, EFAULT, "EFAULT"}, {
-	&header, (cap_user_data_t) - 1, EFAULT, "EFAULT"},
-#endif
-	{
-	&header, &data, EINVAL, "EINVAL"}, {
-&header, &data, EPERM, "EPERM"},};
-
-int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
-
-int main(int ac, char **av)
+#define CAP1 (1 << CAP_NET_RAW | 1 << CAP_CHOWN  | 1 << CAP_SETPCAP)
+#define CAP2 (CAP1 | 1 << CAP_KILL)
+
+static int drop_flag;
+static struct __user_cap_header_struct *header;
+static struct __user_cap_data_struct *data;
+
+static struct tcase {
+	int version;
+	int pid;
+	int effective;
+	int permitted;
+	int inheritable;
+	int exp_err;
+	int flag;
+	char *message;
+} tcases[] = {
+	{0x20080522, 0, CAP1, CAP1, CAP1, EFAULT, 1, "Test bad address header"},
+	{0x20080522, 0, CAP1, CAP1, CAP1, EFAULT, 2, "Test bad address data"},
+	{0, 0, CAP1, CAP1, CAP1, EINVAL, 0, "Test bad version"},
+	{0x20080522, 0, CAP2, CAP1, CAP1, EPERM, 0, "Test bad value data(when pE is not in pP)"},
+	{0x20080522, 0, CAP1, CAP2, CAP1, EPERM, 0, "Test bad value data(when pP is not in old pP)"},
+	{0x20080522, 0, CAP1, CAP1, CAP2, EPERM, 0, "Test bad value data(when pI is not in bounding set or old pI)"},
+};
+
+static void verify_capset(unsigned int n)
 {
+	struct tcase *tc = &tcases[n];
 
-	int lc, i;
+	header->version = tc->version;
+	header->pid = tc->pid;
 
-	tst_parse_opts(ac, av, NULL, NULL);
-#ifdef UCLINUX
-	maybe_run_child(&child_func, "");
-#endif
+	data->effective = tc->effective;
+	data->permitted = tc->permitted;
+	data->inheritable = tc->inheritable;
 
-	setup();
+	tst_res(TINFO, "%s", tc->message);
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-#ifdef UCLINUX
-		i = 2;
-#else
-		i = 0;
-#endif
-
-		for (; i < TST_TOTAL; i++) {
-
-			test_setup(i, av[0]);
-			TEST(ltp_syscall(__NR_capset, test_cases[i].headerp,
-				     test_cases[i].datap));
-
-			if (TEST_RETURN == -1 &&
-			    TEST_ERRNO == test_cases[i].exp_errno) {
-				tst_resm(TPASS, "capset() returned -1,"
-					 " errno: %s", test_cases[i].errdesc);
-			} else {
-				tst_resm(TFAIL | TTERRNO,
-					 "Test Failed, capset() returned %ld",
-					 TEST_RETURN);
-			}
-		}
+	TEST(tst_syscall(__NR_capset, tc->flag - 1 ? header : NULL,
+				tc->flag - 2 ? data : NULL));
+	if (TST_RET == 0) {
+		tst_res(TFAIL, "capset() succeed unexpectedly");
+		return;
 	}
-
-	cleanup();
-
-	tst_exit();
-
-}
-
-void setup(void)
-{
-	tst_require_root();
-
-	TEST_PAUSE;
-
+	if (TST_ERR == tc->exp_err)
+		tst_res(TPASS | TTERRNO, "capset() failed as expected");
+	else
+		tst_res(TFAIL | TTERRNO, "capset() expected %s got ",
+			tst_strerrno(tc->exp_err));
 	/*
-	 * Save current capability data.
-	 * header.version must be _LINUX_CAPABILITY_VERSION
+	 * When an unsupported version value is specified, it will
+	 * return the kernel preferred value of _LINUX_CAPABILITY_VERSION_?.
+	 * Since linux 2.6.26, version 3 is default. We use it.
 	 */
-	header.version = _LINUX_CAPABILITY_VERSION;
-	if (ltp_syscall(__NR_capget, &header, &data) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "capget failed");
+	if (header->version != 0x20080522)
+		tst_res(TFAIL, "kernel doesn't return preferred linux"
+			" capability version when using bad version");
 }
 
-void cleanup(void)
+static void setup(void)
 {
-	if (0 < child_pid) {
-		kill(child_pid, SIGTERM);
-		wait(NULL);
-	}
+	header->version = 0x20080522;
+	data->effective = CAP1;
+	data->permitted = CAP1;
+	data->inheritable = CAP1;
+
+	TEST(tst_syscall(__NR_capset, header, data));
+	if (TST_RET == -1)
+		tst_brk(TBROK | TTERRNO, "capset data failed");
+
+	TEST(prctl(PR_CAPBSET_DROP, CAP_KILL));
+	if (TST_RET == -1)
+		tst_res(TFAIL | TTERRNO, "drop CAP_KILL failed");
+	else
+		drop_flag = 1;
 }
 
-void child_func(void)
-{
-	for (;;) {
-		sleep(10);
+static struct tst_test test = {
+	.setup = setup,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_capset,
+	.needs_root = 1,
+	.bufs = (struct tst_buffers []) {
+		{&header, .size = sizeof(*header)},
+		{&data, .size = 2 * sizeof(*data)},
+		{},
 	}
-}
-
-void test_setup(int i, char *argv0)
-{
-	char nobody_uid[] = "nobody";
-	struct passwd *ltpuser;
-
-	switch (i) {
-	case 0:
-		break;
-
-	case 1:
-		header.version = _LINUX_CAPABILITY_VERSION;
-		header.pid = 0;
-		break;
-
-	case 2:
-		header.version = INVALID_VERSION;
-		header.pid = 0;
-		break;
-
-	case 3:
-		header.version = _LINUX_CAPABILITY_VERSION;
-		/*
-		 * when a non-zero pid is specified, process should have
-		 * CAP_SETPCAP capability to change capabilities.
-		 * by default, CAP_SETPCAP is not enabled. So giving
-		 * a non-zero pid results in capset() failing with
-		 * errno EPERM
-		 *
-		 * Note: this seems to have changed with recent kernels
-		 * => create a child and try to set its capabilities
-		 */
-		child_pid = FORK_OR_VFORK();
-		if (child_pid == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
-		else if (child_pid == 0) {
-#ifdef UCLINUX
-			if (self_exec(argv0, "") < 0) {
-				perror("self_exec failed");
-				exit(1);
-			}
-#else
-			child_func();
-#endif
-		} else {
-			header.pid = child_pid;
-			ltpuser = getpwnam(nobody_uid);
-			if (ltpuser == NULL)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "getpwnam failed");
-			SAFE_SETEUID(cleanup, ltpuser->pw_uid);
-
-		}
-		break;
-
-	}
-}
+};
Signature · 1 line
2.18.0
author
#13 lore raw

PATCH v2 2/3 syscalls/capset03: add new EPERM error test without CAP_SETPCAP

-------------------
v1->v2:
1.remove useless drop
2. use guarded buffer allocation
-------------------

---
 runtest/syscalls                            |  1 +
 testcases/kernel/syscalls/capset/.gitignore |  1 +
 testcases/kernel/syscalls/capset/capset03.c | 59 +++++++++++++++++++++
 3 files changed, 61 insertions(+)
 create mode 100644 testcases/kernel/syscalls/capset/capset03.c
runtest/syscalls +1 -0
index fa87ef63f..4f481be6d 100644
@@ -44,6 +44,7 @@ capget02 capget02
 
 capset01 capset01
 capset02 capset02
+capset03 capset03
 
 cacheflush01 cacheflush01
 
testcases/kernel/syscalls/capset/.gitignore +1 -0
index 004ce7b3e..3f9a4d5e8 100644
@@ -1,2 +1,3 @@
 /capset01
 /capset02
+/capset03
testcases/kernel/syscalls/capset/capset03.c new file +59 -0

59 lines not on this page · raw message

Signature · 1 line
2.18.0
author
#15 lore raw

PATCH v2 3/3 syscalls/capset04: add new EPERM error test with vfs cap support

In reply to Yang Xu

Now, most linux distributions has VFS capabilities supports, so modifying other process cap is never permitted. This case from old capset02.c.

---
 runtest/syscalls                            |  1 +
 testcases/kernel/syscalls/capset/.gitignore |  1 +
 testcases/kernel/syscalls/capset/capset04.c | 81 +++++++++++++++++++++
 3 files changed, 83 insertions(+)
 create mode 100644 testcases/kernel/syscalls/capset/capset04.c
runtest/syscalls +1 -0
index 4f481be6d..f58fefe17 100644
@@ -45,6 +45,7 @@ capget02 capget02
 capset01 capset01
 capset02 capset02
 capset03 capset03
+capset04 capset04
 
 cacheflush01 cacheflush01
 
testcases/kernel/syscalls/capset/.gitignore +1 -0
index 3f9a4d5e8..d0a7a13a4 100644
@@ -1,3 +1,4 @@
 /capset01
 /capset02
 /capset03
+/capset04
testcases/kernel/syscalls/capset/capset04.c new file +81 -0

81 lines not on this page · raw message

Signature · 1 line
2.18.0
#16 lore raw

PATCH v2 3/3 syscalls/capset04: add new EPERM error test with vfs cap support

Hi!
I've simplified the code a bit and pushed, thanks.

Signature · 2 lines
Cyril Hrubis
[email protected]
#17 lore raw

PATCH v2 1/3 syscalls/capset02: Cleanup & convert to new library

In reply to Yang Xu

Hi!

Quoted 279 lines
---
 testcases/kernel/syscalls/capset/capset02.c | 332 ++++++--------------
 1 file changed, 93 insertions(+), 239 deletions(-)
testcases/kernel/syscalls/capset/capset02.c +73 -177
index aece29511..15589d462 100644
@@ -1,261 +1,115 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
+ * Author: Saji Kumar.V.R <[email protected]>
+ *
+ * Tests basic error handling of the capset syscall.
+ * 1) capset() fails with errno set to EFAULT if an invalid address
+ * is given for header.
+ * 2) capset() fails with errno set to EFAULT if an invalid address
+ * is given for data.
+ * 3) capset() fails with errno set to EINVAL if an invalid value
+ * is given for header->version.
+ * 4) capset() fails with errno set to EPERM if the new_Effective is
+ * not a subset of the new_Permitted.
+ * 5) capset() fails with errno set to EPERM if the new_Permitted is
+ * not a subset of the old_Permitted.
+ * 6) capset() fails with errno set ot EPERM if the new_Inheritable is
+ * not a subset of  the old_Inheritable and bounding set.
  */
-/**********************************************************
- *
- *    TEST IDENTIFIER	: capset02
- *
- *    EXECUTED BY	: anyone
- *
- *    TEST TITLE	: Tests for error conditions.
- *
- *    TEST CASE TOTAL	: 4
- *
- *    AUTHOR		: Saji Kumar.V.R <[email protected]>
- *
- *    SIGNALS
- * 	Uses SIGUSR1 to pause before test if option set.
- * 	(See the parse_opts(3) man page).
- *
- *    DESCRIPTION
- *	Verify that
- *	1) capset() fails with errno set to EFAULT if an invalid address
- *	   is given for header
- *	2) capset() fails with errno set to EFAULT if an invalid address
- *	   is given for data
- *	3) capset() fails with errno set to EINVAL if an invalid value
- *	   is given for header->version
- *	4) capset() fails with errno set to EPERM the process does not
- *	   have enough privilege to set capabilities
- *
- *
- * 	Setup:
- * 	  Setup signal handling.
- *	  Pause for SIGUSR1 if option specified.
- *	  Call capget() to save current capability data
- *
- * 	Test:
- *	 Loop if the proper options are given.
- *	  do test specific setup.
- * 	  call capset with proper arguments
- *	  if capset() fails with expected errno
- *		Test passed
- *	  Otherwise
- *		Test failed
- *	  do test specific cleanup
- *
- * 	Cleanup:
- * 	  Print errno log and/or timing stats if options given
- *
- * USAGE:  <for command-line>
- * capset02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
- *			where,  -c n : Run n copies concurrently.
- *				-e   : Turn on errno logging.
- *				-h   : Show help screen
- *				-f   : Turn off functional testing
- *				-i n : Execute test n times.
- *				-I x : Execute test for x seconds.
- *				-p   : Pause for SIGUSR1 before starting
- *				-P x : Pause for x seconds between iterations.
- *				-t   : Turn on syscall timing.
- *
- ****************************************************************/
+#include <stdlib.h>
 #include <sys/types.h>
-#include <sys/wait.h>
-#include <errno.h>
-#include <pwd.h>
-#include <signal.h>
-#include <string.h>
 #include <unistd.h>
-#include "test.h"
-#include "safe_macros.h"
+#include <sys/prctl.h>
+#include "tst_test.h"
 #include "lapi/syscalls.h"
-
-/**************************************************************************/
-/*                                                                        */
-/*   Some archs do not have the manpage documented sys/capability.h file, */
-/*   and require the use of the line below                                */
-
 #include <linux/capability.h>
 
-/*   If you are having issues with including this file and have the sys/  */
-/*   version, then you may want to try switching to it. -Robbie W.        */
-/**************************************************************************/
-
-#define INVALID_VERSION 0
-
-static void setup(void);
-static void cleanup(void);
-static void test_setup(int, char *);
-static void child_func(void);
-
-static pid_t child_pid = -1;
-
-char *TCID = "capset02";
-
-static struct __user_cap_header_struct header;
-static struct __user_cap_data_struct data;
-
-struct test_case_t {
-	cap_user_header_t headerp;
-	cap_user_data_t datap;
-	int exp_errno;
-	char *errdesc;
-} test_cases[] = {
-#ifndef UCLINUX
-	/* Skip since uClinux does not implement memory protection */
-	{
-	(cap_user_header_t) - 1, &data, EFAULT, "EFAULT"}, {
-	&header, (cap_user_data_t) - 1, EFAULT, "EFAULT"},
-#endif
-	{
-	&header, &data, EINVAL, "EINVAL"}, {
-&header, &data, EPERM, "EPERM"},};
-
-int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
-
-int main(int ac, char **av)
+#define CAP1 (1 << CAP_NET_RAW | 1 << CAP_CHOWN  | 1 << CAP_SETPCAP)
+#define CAP2 (CAP1 | 1 << CAP_KILL)
+
+static int drop_flag;
+static struct __user_cap_header_struct *header;
+static struct __user_cap_data_struct *data;
+
+static struct tcase {
+	int version;
+	int pid;
+	int effective;
+	int permitted;
+	int inheritable;
+	int exp_err;
+	int flag;
+	char *message;
+} tcases[] = {
+	{0x20080522, 0, CAP1, CAP1, CAP1, EFAULT, 1, "Test bad address header"},
+	{0x20080522, 0, CAP1, CAP1, CAP1, EFAULT, 2, "Test bad address data"},
+	{0, 0, CAP1, CAP1, CAP1, EINVAL, 0, "Test bad version"},
+	{0x20080522, 0, CAP2, CAP1, CAP1, EPERM, 0, "Test bad value data(when pE is not in pP)"},
+	{0x20080522, 0, CAP1, CAP2, CAP1, EPERM, 0, "Test bad value data(when pP is not in old pP)"},
+	{0x20080522, 0, CAP1, CAP1, CAP2, EPERM, 0, "Test bad value data(when pI is not in bounding set or old pI)"},
+};
+
+static void verify_capset(unsigned int n)
 {
+	struct tcase *tc = &tcases[n];
 
-	int lc, i;
+	header->version = tc->version;
+	header->pid = tc->pid;
 
-	tst_parse_opts(ac, av, NULL, NULL);
-#ifdef UCLINUX
-	maybe_run_child(&child_func, "");
-#endif
+	data->effective = tc->effective;
+	data->permitted = tc->permitted;
+	data->inheritable = tc->inheritable;
 
-	setup();
+	tst_res(TINFO, "%s", tc->message);
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-#ifdef UCLINUX
-		i = 2;
-#else
-		i = 0;
-#endif
-
-		for (; i < TST_TOTAL; i++) {
-
-			test_setup(i, av[0]);
-			TEST(ltp_syscall(__NR_capset, test_cases[i].headerp,
-				     test_cases[i].datap));
-
-			if (TEST_RETURN == -1 &&
-			    TEST_ERRNO == test_cases[i].exp_errno) {
-				tst_resm(TPASS, "capset() returned -1,"
-					 " errno: %s", test_cases[i].errdesc);
-			} else {
-				tst_resm(TFAIL | TTERRNO,
-					 "Test Failed, capset() returned %ld",
-					 TEST_RETURN);
-			}
-		}
+	TEST(tst_syscall(__NR_capset, tc->flag - 1 ? header : NULL,
+				tc->flag - 2 ? data : NULL));
+	if (TST_RET == 0) {
+		tst_res(TFAIL, "capset() succeed unexpectedly");
+		return;
 	}
-
-	cleanup();
-
-	tst_exit();
-
-}
-
-void setup(void)
-{
-	tst_require_root();
-
-	TEST_PAUSE;
-
+	if (TST_ERR == tc->exp_err)
+		tst_res(TPASS | TTERRNO, "capset() failed as expected");
+	else
+		tst_res(TFAIL | TTERRNO, "capset() expected %s got ",
+			tst_strerrno(tc->exp_err));
 	/*
-	 * Save current capability data.
-	 * header.version must be _LINUX_CAPABILITY_VERSION
+	 * When an unsupported version value is specified, it will
+	 * return the kernel preferred value of _LINUX_CAPABILITY_VERSION_?.
+	 * Since linux 2.6.26, version 3 is default. We use it.
 	 */
-	header.version = _LINUX_CAPABILITY_VERSION;
-	if (ltp_syscall(__NR_capget, &header, &data) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "capget failed");
+	if (header->version != 0x20080522)
+		tst_res(TFAIL, "kernel doesn't return preferred linux"
+			" capability version when using bad version");
 }
 
-void cleanup(void)
+static void setup(void)
 {
-	if (0 < child_pid) {
-		kill(child_pid, SIGTERM);
-		wait(NULL);
-	}
+	header->version = 0x20080522;
+	data->effective = CAP1;
+	data->permitted = CAP1;
+	data->inheritable = CAP1;

testcases/kernel/syscalls/capset/capset02.c line 102

Quoted 10 lines
testcases/kernel/syscalls/capset/capset02.c +10 -0
+
+	TEST(tst_syscall(__NR_capset, header, data));
+	if (TST_RET == -1)
+		tst_brk(TBROK | TTERRNO, "capset data failed");
+
+	TEST(prctl(PR_CAPBSET_DROP, CAP_KILL));
+	if (TST_RET == -1)
+		tst_res(TFAIL | TTERRNO, "drop CAP_KILL failed");
+	else
+		drop_flag = 1;

This flag does not seem to be used anywhere. Can't we simply tst_brk() here if the call fails?

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#18 lore raw

PATCH v2 1/3 syscalls/capset02: Cleanup & convert to new library

Hi Cyril

Quoted 7 lines

testcases/kernel/syscalls/capset/capset02.c line 102

Quoted 5 lines
testcases/kernel/syscalls/capset/capset02.c +5 -0
+	TEST(prctl(PR_CAPBSET_DROP, CAP_KILL));
+	if (TST_RET == -1)
+		tst_res(TFAIL | TTERRNO, "drop CAP_KILL failed");
+	else
+		drop_flag = 1;

This flag does not seem to be used anywhere. Can't we simply tst_brk() here if the call fails?

Since it has used needs_root flag, I think removing this flag is ok(I removed it in verify functions, but forgot to remove it in setup).

Best Regards
Yang Xu

#19 lore raw

PATCH v2 1/3 syscalls/capset02: Cleanup & convert to new library

In reply to Yang Xu

Hi!
Pushed with minor changes, thanks.

The diff is:

testcases/kernel/syscalls/capset/capset02.c +7 -6
index 15589d462..a6c4f29a0 100644
@@ -28,10 +28,11 @@
 #define CAP1 (1 << CAP_NET_RAW | 1 << CAP_CHOWN  | 1 << CAP_SETPCAP)
 #define CAP2 (CAP1 | 1 << CAP_KILL)
 
-static int drop_flag;
 static struct __user_cap_header_struct *header;
 static struct __user_cap_data_struct *data;
 
+static void *bad_addr;
+
 static struct tcase {
        int version;
        int pid;
@@ -63,8 +64,8 @@ static void verify_capset(unsigned int n)
 
        tst_res(TINFO, "%s", tc->message);
 
-       TEST(tst_syscall(__NR_capset, tc->flag - 1 ? header : NULL,
-                               tc->flag - 2 ? data : NULL));
+       TEST(tst_syscall(__NR_capset, tc->flag - 1 ? header : bad_addr,
+                               tc->flag - 2 ? data : bad_addr));
        if (TST_RET == 0) {
                tst_res(TFAIL, "capset() succeed unexpectedly");
                return;
@@ -97,9 +98,9 @@ static void setup(void)
 
        TEST(prctl(PR_CAPBSET_DROP, CAP_KILL));
        if (TST_RET == -1)
-               tst_res(TFAIL | TTERRNO, "drop CAP_KILL failed");
-       else
-               drop_flag = 1;
+               tst_brk(TBROK | TTERRNO, "drop CAP_KILL failed");
+
+       bad_addr = tst_get_bad_addr(NULL);
 }
 
 
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#20 lore raw

PATCH v3 4/5 syscalls/quotactl05: add project quota test for xfs filesystem

In reply to Yang Xu

This is a variant about quotactl02. It is used to test project quota. I split it into a new case instead of adding it in quotaclt02 because kernel doesn't permit mount both group and project quota before commit d892d5864f02 ("xfs: Start using pquotaino from the superblock.").

---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/quotactl/.gitignore |  2 +
 .../kernel/syscalls/quotactl/quotactl05.c     | 99 +++++++++++++++++++
 3 files changed, 102 insertions(+)
 create mode 100644 testcases/kernel/syscalls/quotactl/quotactl05.c
runtest/syscalls +1 -0
index c30e9e620..f2c7b0daf 100644
@@ -954,6 +954,7 @@ quotactl01 quotactl01
 quotactl02 quotactl02
 quotactl03 quotactl03
 quotactl04 quotactl04
+quotactl05 quotactl05
 
 read01 read01
 read02 read02
testcases/kernel/syscalls/quotactl/.gitignore +2 -0
index 1db7c5d98..9621e7717 100644
@@ -2,3 +2,5 @@
 /quotactl02
 /quotactl03
 /quotactl04
+/quotactl05
+
testcases/kernel/syscalls/quotactl/quotactl05.c new file +99 -0

99 lines not on this page · raw message

Signature · 1 line
2.18.0
author
#21 lore raw

PATCH v3 4/5 syscalls/quotactl05: add project quota test for xfs filesystem

Hi All
Sorry, please ingore this email. It has been merged.

Kind Regards
Yang Xu

Quoted 12 lines · Yang Xu

This is a variant about quotactl02. It is used to test project quota. I split it into a new case instead of adding it in quotaclt02 because kernel doesn't permit mount both group and project quota before commit d892d5864f02 ("xfs: Start using pquotaino from the superblock.").

---
  runtest/syscalls                              |  1 +
  testcases/kernel/syscalls/quotactl/.gitignore |  2 +
  .../kernel/syscalls/quotactl/quotactl05.c     | 99 +++++++++++++++++++
  3 files changed, 102 insertions(+)

create mode 100644 testcases/kernel/syscalls/quotactl/quotactl05.c