[LTP] [PATCH 3/4] syscalls/setreuid05: Convert to new API

Martin Doucha mdoucha@suse.cz
Wed Sep 15 15:45:04 CEST 2021


The original test looks up specific usernames which may not exist on some
systems. Use any non-root UIDs instead.

Also add verification that saved UID gets updated according to
documentation.

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 .../kernel/syscalls/setreuid/setreuid05.c     | 260 +++++++-----------
 1 file changed, 92 insertions(+), 168 deletions(-)

diff --git a/testcases/kernel/syscalls/setreuid/setreuid05.c b/testcases/kernel/syscalls/setreuid/setreuid05.c
index 39b30f950..4c1c94ee8 100644
--- a/testcases/kernel/syscalls/setreuid/setreuid05.c
+++ b/testcases/kernel/syscalls/setreuid/setreuid05.c
@@ -1,197 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) International Business Machines  Corp., 2001
- *
- * This program is free software;  you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY;  without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program;  if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * Ported by John George
+ *    Ported by John George
+ * Copyright (C) 2021 SUSE LLC <mdoucha@suse.cz>
  */
 
-/*
+/*\
+ * [Description]
+ *
  * Test the setreuid() feature, verifying the role of the saved-set-uid
  * and setreuid's effect on it.
  */
 
-#include <errno.h>
+#include <sys/types.h>
 #include <pwd.h>
 #include <stdlib.h>
-#include <sys/wait.h>
-
-#include "test.h"
-#include "compat_16.h"
 
-TCID_DEFINE(setreuid05);
+#include "tst_test.h"
+#include "tst_uid.h"
+#include "compat_tst_16.h"
 
-static int fail = -1;
-static int pass;
-static uid_t neg_one = -1;
-
-static struct passwd nobody, daemonpw, root, bin;
+static uid_t root_uid, nobody_uid, main_uid, other_uid, neg_one = -1;
 
 struct test_data_t {
 	uid_t *real_uid;
 	uid_t *eff_uid;
-	int *exp_ret;
-	struct passwd *exp_real_usr;
-	struct passwd *exp_eff_usr;
-	char *test_msg;
+	int exp_ret;
+	uid_t *exp_real_uid;
+	uid_t *exp_eff_uid;
+	uid_t *exp_sav_uid;
+	const char *test_msg;
 } test_data[] = {
-	{
-	&nobody.pw_uid, &root.pw_uid, &pass, &nobody, &root, "Initially"}, {
-	&neg_one, &nobody.pw_uid, &pass, &nobody, &nobody,
-		    "After setreuid(-1, nobody),"}, {
-	&neg_one, &root.pw_uid, &pass, &nobody, &root,
-		    "After setreuid(-1, root),"}, {
-	&daemonpw.pw_uid, &neg_one, &pass, &daemonpw, &root,
-		    "After setreuid(daemon, -1),"}, {
-	&neg_one, &bin.pw_uid, &pass, &daemonpw, &bin,
-		    "After setreuid(-1, bin),"}, {
-	&neg_one, &root.pw_uid, &fail, &daemonpw, &bin,
-		    "After setreuid(-1, root),"}, {
-	&neg_one, &nobody.pw_uid, &fail, &daemonpw, &bin,
-		    "After setreuid(-1, nobody),"}, {
-	&neg_one, &daemonpw.pw_uid, &pass, &daemonpw, &daemonpw,
-		    "After setreuid(-1, daemon),"}, {
-	&neg_one, &bin.pw_uid, &pass, &daemonpw, &bin,
-		    "After setreuid(-1, bin),"}, {
-	&bin.pw_uid, &daemonpw.pw_uid, &pass, &bin, &daemonpw,
-		    "After setreuid(bin, daemon),"}, {
-	&neg_one, &bin.pw_uid, &pass, &bin, &bin, "After setreuid(-1, bin),"},
-	{
-	&neg_one, &daemonpw.pw_uid, &pass, &bin, &daemonpw,
-		    "After setreuid(-1, daemon),"}, {
-	&daemonpw.pw_uid, &neg_one, &pass, &daemonpw, &daemonpw,
-		    "After setreuid(daemon, -1),"}, {
-	&neg_one, &bin.pw_uid, &fail, &daemonpw, &daemonpw,
-		    "After setreuid(-1, bin),"},};
-
-int TST_TOTAL = ARRAY_SIZE(test_data);
-
-static void setup(void);
-static void cleanup(void);
-static void uid_verify(struct passwd *, struct passwd *, char *);
-
-int main(int argc, char **argv)
-{
-	int lc;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	pass = 0;
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		int i, pid;
-
-		tst_count = 0;
-
-		if ((pid = FORK_OR_VFORK()) == -1) {
-			tst_brkm(TBROK, cleanup, "fork failed");
-		} else if (pid == 0) {	/* child */
-			for (i = 0; i < TST_TOTAL; i++) {
-				/* Set the real or effective user id */
-				TEST(SETREUID(cleanup, *test_data[i].real_uid,
-					      *test_data[i].eff_uid));
-
-				if (TEST_RETURN == *test_data[i].exp_ret) {
-					if (TEST_RETURN == neg_one) {
-						if (TEST_ERRNO != EPERM) {
-							tst_resm(TFAIL,
-								 "setreuid(%d, %d) "
-								 "did not set errno "
-								 "value as expected.",
-								 *test_data
-								 [i].real_uid,
-								 *test_data
-								 [i].eff_uid);
-							continue;
-						}
-						tst_resm(TPASS,
-							 "setreuid(%d, %d) "
-							 "failed as expected.",
-							 *test_data[i].real_uid,
-							 *test_data[i].eff_uid);
-					} else {
-						tst_resm(TPASS,
-							 "setreuid(%d, %d) "
-							 "succeeded as expected.",
-							 *test_data[i].real_uid,
-							 *test_data[i].eff_uid);
-					}
-				} else {
-					tst_resm(TFAIL, "setreuid(%d, %d) "
-						 "did not return as expected.",
-						 *test_data[i].real_uid,
-						 *test_data[i].eff_uid);
-				}
-
-				if (TEST_RETURN == -1) {
-				}
-				uid_verify(test_data[i].exp_real_usr,
-					   test_data[i].exp_eff_usr,
-					   test_data[i].test_msg);
-			}
-			tst_exit();
-		} else {	/* parent */
-			tst_record_childstatus(cleanup, pid);
-		}
-	}
-	cleanup();
-	tst_exit();
-}
+	{&nobody_uid, &root_uid, 0, &nobody_uid, &root_uid, &root_uid,
+		"setreuid(nobody, root)"},
+	{&neg_one, &nobody_uid, 0, &nobody_uid, &nobody_uid, &root_uid,
+		"setreuid(-1, nobody)"},
+	{&neg_one, &root_uid, 0, &nobody_uid, &root_uid, &root_uid,
+		"setreuid(-1, root)"},
+	{&main_uid, &neg_one, 0, &main_uid, &root_uid, &root_uid,
+		"setreuid(main, -1)"},
+	{&neg_one, &other_uid, 0, &main_uid, &other_uid, &other_uid,
+		"setreuid(-1, other)"},
+	{&neg_one, &root_uid, -1, &main_uid, &other_uid, &other_uid,
+		"setreuid(-1, root)"},
+	{&neg_one, &nobody_uid, -1, &main_uid, &other_uid, &other_uid,
+		"setreuid(-1, nobody)"},
+	{&neg_one, &main_uid, 0, &main_uid, &main_uid, &other_uid,
+		"setreuid(-1, main)"},
+	{&neg_one, &other_uid, 0, &main_uid, &other_uid, &other_uid,
+		"setreuid(-1, other)"},
+	{&other_uid, &main_uid, 0, &other_uid, &main_uid, &main_uid,
+		"setreuid(other, main)"},
+	{&neg_one, &other_uid, 0, &other_uid, &other_uid, &main_uid,
+		"setreuid(-1, other)"},
+	{&neg_one, &main_uid, 0, &other_uid, &main_uid, &main_uid,
+		"setreuid(-1, main)"},
+	{&main_uid, &neg_one, 0, &main_uid, &main_uid, &main_uid,
+		"setreuid(main, -1)"},
+	{&neg_one, &other_uid, -1, &main_uid, &main_uid, &main_uid,
+		"setreuid(-1, other)"},
+};
 
 static void setup(void)
 {
-	tst_require_root();
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	if (getpwnam("nobody") == NULL)
-		tst_brkm(TBROK, NULL, "nobody must be a valid user.");
-
-	if (getpwnam("daemon") == NULL)
-		tst_brkm(TBROK, NULL, "daemon must be a valid user.");
-
-	if (getpwnam("bin") == NULL)
-		tst_brkm(TBROK, NULL, "bin must be a valid user.");
-
-	nobody = *(getpwnam("nobody"));
-	UID16_CHECK(nobody.pw_uid, setreuid, cleanup);
-
-	daemonpw = *(getpwnam("daemon"));
-	UID16_CHECK(daemonpw.pw_uid, setreuid, cleanup);
+	uid_t test_users[3];
+	struct passwd *pw;
+
+	root_uid = getuid();
+	pw = SAFE_GETPWNAM("nobody");
+	nobody_uid = test_users[0] = pw->pw_uid;
+	tst_get_uids(test_users, 1, 3);
+	main_uid = test_users[1];
+	other_uid = test_users[2];
+
+	UID16_CHECK(root_uid, setreuid);
+	UID16_CHECK(nobody_uid, setreuid);
+	UID16_CHECK(main_uid, setreuid);
+	UID16_CHECK(other_uid, setreuid);
+
+	/* Make sure that saved UID is also set to root */
+	SAFE_SETUID(root_uid);
+}
 
-	root = *(getpwnam("root"));
-	UID16_CHECK(root.pw_uid, setreuid, cleanup);
+static void run_child(const struct test_data_t *tc)
+{
+	if (tc->exp_ret) {
+		TST_EXP_FAIL(SETREUID(*tc->real_uid, *tc->eff_uid), EPERM,
+			"%s", tc->test_msg);
+	} else {
+		TST_EXP_PASS(SETREUID(*tc->real_uid, *tc->eff_uid), "%s",
+			tc->test_msg);
+	}
 
-	bin = *(getpwnam("bin"));
-	UID16_CHECK(bin.pw_uid, setreuid, cleanup);
+	if (!TST_PASS)
+		return;
 
-	TEST_PAUSE;
+	tst_check_resuid(tc->test_msg, *tc->exp_real_uid, *tc->exp_eff_uid,
+		*tc->exp_sav_uid);
 }
 
-static void cleanup(void)
+static void run(void)
 {
-}
+	if (!SAFE_FORK()) {
+		unsigned int i;
 
-static void uid_verify(struct passwd *ru, struct passwd *eu, char *when)
-{
-	if ((getuid() != ru->pw_uid) || (geteuid() != eu->pw_uid)) {
-		tst_resm(TFAIL, "ERROR: %s real uid = %d; effective uid = %d",
-			 when, getuid(), geteuid());
-		tst_resm(TINFO, "Expected: real uid = %d; effective uid = %d",
-			 ru->pw_uid, eu->pw_uid);
+		for (i = 0; i < ARRAY_SIZE(test_data); i++)
+			run_child(test_data + i);
+
+		exit(0);
 	}
+
+	tst_reap_children();
 }
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.needs_root = 1,
+	.forks_child = 1,
+};
-- 
2.33.0



More information about the ltp mailing list