Skip to contentSkip to conversation

syscalls/setgid: Update to new API

PATCH New zhaogongyi 1 participant 1 message Apr 22, 2021

lore
Share & tools
lore

zhaogongyi

author
#0 lore raw

PATCH 1/1 syscalls/setgid: Update to new API

Hi Cyril,

Thanks for your review!

I have re-submitted the patch according to your review.

Thanks so much!

Best Regards,
Gongyi

Quoted 364 lines

Hi!

Quoted 2 lines
testcases/kernel/syscalls/setgid/setgid01.c +0 -0

...

Quoted 4 lines

-/*
- * AUTHOR : William Roske
- * CO-PILOT : Dave Fenner
*/

Can we please have the documentation comment added here?

Just a single line with something as "Calls setgid() with current gid and expects success." would suffice.

testcases/kernel/syscalls/setgid/setgid01.c

Quoted 10 lines
testcases/kernel/syscalls/setgid/setgid01.c +5 -4
+	TEST(SETGID(gid));
 
-	}
-
-	cleanup();
-	tst_exit();
+	if (TST_RET == -1)
+		tst_res(TFAIL | TTERRNO, "setgid(%d) Failed", gid);
+	else
+		tst_res(TPASS, "setgid(%d) returned %ld", gid, TST_RET);

This should be just TST_EXP_PASS()

Quoted 32 lines

}

static void setup(void)
{

testcases/kernel/syscalls/setgid/setgid01.c +5 -8
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
 	gid = getgid();
-	GID16_CHECK(gid, setgid, cleanup);
+	GID16_CHECK(gid, setgid);
 }
 
-static void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+};
testcases/kernel/syscalls/setgid/setgid02.c +1 -2
index b3f4fd646..92e79eb86 100644
@@ -1,111 +1,57 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) International Business Machines  Corp., 2001
  *  Ported by Wayne Boyer
- *
- * This program is free software;  you can redistribute it and/or
Quoted 8 lines

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

Quoted 6 lines

- * 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

Quoted 15 lines

*/

-/*
- * Testcase to ensure that the setgid() system call sets errno to EPERM
+/*\
+ * [Description]
+ * Testcase to ensure that the setgid() system call sets errno to EPERM. *
- * ALGORITHM
- * Call setgid() to set the gid to that of root. Run this test as
- * ltpuser1, and expect to get EPERM
+ * [Algorithm]
+ * Call setgid() to set the gid to that of root. Run this test as
+ * ltpuser1, and expect to get EPERM.

        ^
	nobody?
Quoted 58 lines

*/

testcases/kernel/syscalls/setgid/setgid02.c +10 -34
-#include <pwd.h>
-#include <errno.h>
 
-#include "test.h"
-#include "compat_16.h"
+#include <pwd.h>
+#include "tst_test.h"
+#include "compat_tst_16.h"
 
-TCID_DEFINE(setgid02);
-int TST_TOTAL = 1;
 
 static char root[] = "root";
 static char nobody_uid[] = "nobody";
-static char nobody_gid[] = "nobody";
 static struct passwd *ltpuser;
 
-static void setup(void);
-static void cleanup(void);
-
-int main(int ac, char **av)
+static void run(void)
 {
-	struct passwd *getpwnam(), *rootpwent;
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
+	struct passwd *rootpwent;
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
+	rootpwent = SAFE_GETPWNAM(root);
 
-		if ((rootpwent = getpwnam(root)) == NULL) {
-			tst_brkm(TBROK, cleanup, "getpwnam failed for user id "
-				 "%s", root);
-		}
+	GID16_CHECK(rootpwent->pw_gid, setgid);
 
-		GID16_CHECK(rootpwent->pw_gid, setgid, cleanup);
+	TEST(SETGID(rootpwent->pw_gid));
 
-		TEST(SETGID(cleanup, rootpwent->pw_gid));
+	if (TST_RET != -1)
+		tst_brk(TBROK, "call succeeded unexpectedly");
 
-		if (TEST_RETURN != -1) {
-			tst_resm(TFAIL, "call succeeded unexpectedly");
-			continue;
-		}
-
-		if (TEST_ERRNO != EPERM) {
-			tst_resm(TFAIL, "setgid set invalid errno, expected: "
-				 "EPERM, got: %d\n", TEST_ERRNO);
-		} else {

testcases/kernel/syscalls/setgid/setgid02.c

Quoted 10 lines
testcases/kernel/syscalls/setgid/setgid02.c +4 -6
-			tst_resm(TPASS, "setgid returned EPERM");
-		}
-	}
-
-	cleanup();
-	tst_exit();
+	if (TST_ERR != EPERM)
+		tst_res(TFAIL | TTERRNO, "setgid set invalid errno");
+	else
+		tst_res(TPASS, "setgid returned EPERM");

And this should be just TST_EXP_FAIL()

Quoted 52 lines

}

static void setup(void)
{

testcases/kernel/syscalls/setgid/setgid02.c +8 -24
-	tst_require_root();
-
 	/* Switch to nobody user for correct error code collection */
-	ltpuser = getpwnam(nobody_uid);
-	if (ltpuser == NULL)
-		tst_brkm(TBROK, cleanup, "getpwnam failed for user id %s",
-			nobody_uid);
-
-	if (setgid(ltpuser->pw_gid) == -1) {
-		tst_resm(TINFO, "setgid failed to "
-			 "to set the effective gid to %d", ltpuser->pw_gid);
-		perror("setgid");
-	}
+	ltpuser = SAFE_GETPWNAM(nobody_uid);
 
-	if (setuid(ltpuser->pw_uid) == -1) {
-		tst_resm(TINFO, "setuid failed to "
-			 "to set the effective uid to %d", ltpuser->pw_uid);
-		perror("setuid");
-	}
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
+	SAFE_SETGID(ltpuser->pw_gid);
+	SAFE_SETUID(ltpuser->pw_uid);
 }
 
-static void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.needs_root = 1,
+	.setup = setup,
+	.test_all = run,
+};
testcases/kernel/syscalls/setgid/setgid03.c +1 -2
index f29347a13..9e4b3b736 100644
@@ -1,100 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) International Business Machines  Corp., 2001
  *  Ported by Wayne Boyer
- *
- * This program is free software;  you can redistribute it and/or
Quoted 8 lines

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

Quoted 6 lines

- * 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

testcases/kernel/syscalls/setgid/setgid03.c

Quoted 8 lines

*/

testcases/kernel/syscalls/setgid/setgid03.c +3 -3
-/*
- * ALGORITHM
- *	As root sets the current group id to ltpuser1, verify the results
+/*\
+ * [Algorithm]
+ * As root sets the current group id to ltpuser1, verify the results.

^ ^
nobody |
This is very generic and useless.

This should rather be something as:

"As a root sets current group id to nobody and expects success."

Quoted 19 lines

*/

testcases/kernel/syscalls/setgid/setgid03.c +3 -9
-#include <pwd.h>
-#include <errno.h>
-
-#include "test.h"
-#include <compat_16.h>
 
-TCID_DEFINE(setgid03);
-int TST_TOTAL = 1;
+#include <pwd.h>
+#include "tst_test.h"
+#include <compat_tst_16.h>
 
 static char ltpuser1[] = "nobody";
 static char root[] = "root";
 static struct passwd *ltpuser1pwent, *rootpwent;  static gid_t mygid;
 
-static void setup(void);
-static void cleanup(void);

testcases/kernel/syscalls/setgid/setgid03.c

Quoted 10 lines
testcases/kernel/syscalls/setgid/setgid03.c +4 -4
-
-int main(int ac, char **av)
+static void run(void)
 {
-	int lc;
+	TEST(SETGID(ltpuser1pwent->pw_gid));
 
-	tst_parse_opts(ac, av, NULL, NULL);
+	if (TST_RET == -1)
+		tst_brk(TBROK | TTERRNO, "setgid failed unexpectedly");

This should be TST_EXP_PASS() as well.

Quoted 38 lines
testcases/kernel/syscalls/setgid/setgid03.c +4 -30
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		TEST(SETGID(cleanup, ltpuser1pwent->pw_gid));
-
-		if (TEST_RETURN == -1) {
-			tst_resm(TFAIL, "call failed unexpectedly");
-			continue;
-		}
-
-		if (getgid() != ltpuser1pwent->pw_gid) {
-			tst_resm(TFAIL, "setgid failed to set gid to "
-				 "ltpuser1's gid");
-		} else {
-			tst_resm(TPASS, "functionality of getgid() is correct");
-		}
-	}
-
-	cleanup();
-	tst_exit();
+	if (getgid() != ltpuser1pwent->pw_gid)
+		tst_res(TFAIL, "setgid failed to set gid to ltpuser1's gid");
+	else
+		tst_res(TPASS, "functionality of getgid() is correct");
 }
 
 static void setup(void)
 {
-	tst_require_root();
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	if ((rootpwent = getpwnam(root)) == NULL) {
-		tst_brkm(TBROK, cleanup, "getpwnam failed for "

testcases/kernel/syscalls/setgid/setgid03.c

Quoted 10 lines
testcases/kernel/syscalls/setgid/setgid03.c +2 -4
-			 "user id %s", root);
-	}
+	rootpwent = SAFE_GETPWNAM(root);
 
 	mygid = getgid();
-
 	if (mygid != rootpwent->pw_gid) {
-		tst_brkm(TBROK, cleanup, "real group id is not root");
+		tst_brk(TBROK, "real group id is not root");
 	}

I do not think that we need this check since we have .needs_root flag set in the tst_test structure.

Quoted 22 lines
testcases/kernel/syscalls/setgid/setgid03.c +7 -9
-	if ((ltpuser1pwent = getpwnam(ltpuser1)) == NULL) {
-		tst_brkm(TBROK, cleanup, "getpwnam failed for user "
-			 "id %s", ltpuser1);
-	}
+	ltpuser1pwent = SAFE_GETPWNAM(ltpuser1);
 
-	GID16_CHECK(ltpuser1pwent->pw_gid, setgid, cleanup);
+	GID16_CHECK(ltpuser1pwent->pw_gid, setgid);
 }
 
-static void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.needs_root = 1,
+	.setup = setup,
+	.test_all = run,
+};
--

2.17.1

Quoted 2 lines

--
Mailing list info: https://lists.linux.it/listinfo/ltp

--
Cyril Hrubis
[email protected]