All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH 0/7] UID/GID lookup fixes
@ 2021-09-03 15:48 Martin Doucha
  2021-09-03 15:48 ` [LTP] [PATCH 1/7] chmod05, fchmod05: Use free GID instead of "bin" group Martin Doucha
                   ` (6 more replies)
  0 siblings, 7 replies; 19+ messages in thread
From: Martin Doucha @ 2021-09-03 15:48 UTC (permalink / raw)
  To: ltp

Some LTP tests assume that certain user/group names exist on all systems.
That is not true and those tests fail as a result.

This patchset replaces lookups of specific named users/groups with either
some unused group ID or any existing user/group IDs, depending on test
scenario. There are more tests with the same issue but they also require
porting to the new LTP API. I'll submit those later one by one.

Martin Doucha (7):
  chmod05, fchmod05: Use free GID instead of "bin" group
  Simplify syscalls/mkdir02
  Add user/group ID lookup helper functions
  syscalls/kill05: Use any two unprivileged users
  syscalls/mkdir04: Simplify test and use any existing users
  syscalls/setregid02-04: Simplify GID handling
  syscalls/setregid02-04: Eliminate named group lookups

 include/tst_safe_macros.h                     |   6 ++
 include/tst_uid.h                             |  17 +++
 lib/tst_uid.c                                 | 100 ++++++++++++++++++
 testcases/kernel/syscalls/chmod/chmod05.c     |   7 +-
 testcases/kernel/syscalls/fchmod/fchmod05.c   |   7 +-
 testcases/kernel/syscalls/kill/kill05.c       |  23 ++--
 testcases/kernel/syscalls/mkdir/mkdir02.c     |  77 +++++---------
 testcases/kernel/syscalls/mkdir/mkdir04.c     |  30 ++----
 .../kernel/syscalls/setregid/setregid02.c     |  75 ++++++-------
 .../kernel/syscalls/setregid/setregid03.c     |  92 +++++++---------
 .../kernel/syscalls/setregid/setregid04.c     |  58 +++++-----
 11 files changed, 279 insertions(+), 213 deletions(-)

-- 
2.33.0


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [LTP] [PATCH 1/7] chmod05, fchmod05: Use free GID instead of "bin" group
  2021-09-03 15:48 [LTP] [PATCH 0/7] UID/GID lookup fixes Martin Doucha
@ 2021-09-03 15:48 ` Martin Doucha
  2021-09-06 12:20     ` Cyril Hrubis
  2021-09-03 15:48 ` [LTP] [PATCH 2/7] Simplify syscalls/mkdir02 Martin Doucha
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 19+ messages in thread
From: Martin Doucha @ 2021-09-03 15:48 UTC (permalink / raw)
  To: ltp

The bin user group does not exist on some systems which will cause test
failure. Set test directory ownership to any unused GID instead.

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 testcases/kernel/syscalls/chmod/chmod05.c   | 7 ++++---
 testcases/kernel/syscalls/fchmod/fchmod05.c | 7 ++++---
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/testcases/kernel/syscalls/chmod/chmod05.c b/testcases/kernel/syscalls/chmod/chmod05.c
index 6a6a0ae21..f5b9d51e7 100644
--- a/testcases/kernel/syscalls/chmod/chmod05.c
+++ b/testcases/kernel/syscalls/chmod/chmod05.c
@@ -37,6 +37,7 @@
 #include <pwd.h>
 
 #include "tst_test.h"
+#include "tst_uid.h"
 
 #define MODE_RWX	(mode_t)(S_IRWXU | S_IRWXG | S_IRWXO)
 #define DIR_MODE	(mode_t)(S_ISVTX | S_ISGID | S_IFDIR)
@@ -69,10 +70,10 @@ static void test_chmod(void)
 static void setup(void)
 {
 	struct passwd *nobody_u;
-	struct group *bin_gr;
+	gid_t free_gid;
 
 	nobody_u = SAFE_GETPWNAM("nobody");
-	bin_gr = SAFE_GETGRNAM("bin");
+	free_gid = tst_get_free_gid(nobody_u->pw_gid);
 
 	/*
 	 * Create a test directory under temporary directory with specified
@@ -83,7 +84,7 @@ static void setup(void)
 	if (setgroups(1, &nobody_u->pw_gid) == -1)
 		tst_brk(TBROK | TERRNO, "setgroups to nobody's gid failed");
 
-	SAFE_CHOWN(TESTDIR, nobody_u->pw_uid, bin_gr->gr_gid);
+	SAFE_CHOWN(TESTDIR, nobody_u->pw_uid, free_gid);
 
 	/* change to nobody:nobody */
 	SAFE_SETEGID(nobody_u->pw_gid);
diff --git a/testcases/kernel/syscalls/fchmod/fchmod05.c b/testcases/kernel/syscalls/fchmod/fchmod05.c
index f75b9bf36..0c731d601 100644
--- a/testcases/kernel/syscalls/fchmod/fchmod05.c
+++ b/testcases/kernel/syscalls/fchmod/fchmod05.c
@@ -20,6 +20,7 @@
 #include <errno.h>
 
 #include "tst_test.h"
+#include "tst_uid.h"
 #include "fchmod.h"
 
 #define PERMS_DIR	043777
@@ -50,10 +51,10 @@ static void verify_fchmod(void)
 static void setup(void)
 {
 	struct passwd *ltpuser;
-	struct group *ltpgroup;
+	gid_t free_gid;
 
 	ltpuser = SAFE_GETPWNAM("nobody");
-	ltpgroup = SAFE_GETGRNAM("bin");
+	free_gid = tst_get_free_gid(ltpuser->pw_gid);
 
 	SAFE_MKDIR(TESTDIR, DIR_MODE);
 
@@ -62,7 +63,7 @@ static void setup(void)
 			tst_strerrno(TST_ERR));
 	}
 
-	SAFE_CHOWN(TESTDIR, ltpuser->pw_uid, ltpgroup->gr_gid);
+	SAFE_CHOWN(TESTDIR, ltpuser->pw_uid, free_gid);
 
 	SAFE_SETEGID(ltpuser->pw_gid);
 	SAFE_SETEUID(ltpuser->pw_uid);
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [LTP] [PATCH 2/7] Simplify syscalls/mkdir02
  2021-09-03 15:48 [LTP] [PATCH 0/7] UID/GID lookup fixes Martin Doucha
  2021-09-03 15:48 ` [LTP] [PATCH 1/7] chmod05, fchmod05: Use free GID instead of "bin" group Martin Doucha
@ 2021-09-03 15:48 ` Martin Doucha
  2021-09-06 12:20     ` Cyril Hrubis
  2021-09-03 15:48 ` [LTP] [PATCH 3/7] Add user/group ID lookup helper functions Martin Doucha
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 19+ messages in thread
From: Martin Doucha @ 2021-09-03 15:48 UTC (permalink / raw)
  To: ltp

mkdir02 does a lot of unnecessary fork()ing left over from the old version.
The same test can be done with just user "nobody", one unused GID and no
fork()ing at all.

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 testcases/kernel/syscalls/mkdir/mkdir02.c | 77 +++++++----------------
 1 file changed, 24 insertions(+), 53 deletions(-)

diff --git a/testcases/kernel/syscalls/mkdir/mkdir02.c b/testcases/kernel/syscalls/mkdir/mkdir02.c
index f09e3c33d..2cb199c56 100644
--- a/testcases/kernel/syscalls/mkdir/mkdir02.c
+++ b/testcases/kernel/syscalls/mkdir/mkdir02.c
@@ -9,85 +9,57 @@
  *  bit is set in the parent directory.
  */
 
-#include <errno.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <pwd.h>
-#include <sys/wait.h>
-#include <unistd.h>
-#include <stdlib.h>
 #include "tst_test.h"
+#include "tst_uid.h"
 
 #define TESTDIR1	"testdir1"
 #define TESTDIR2	"testdir1/testdir2"
 
-static uid_t nobody_uid, bin_uid;
-static gid_t nobody_gid, bin_gid;
+static gid_t free_gid;
 
 static void verify_mkdir(void)
 {
-	struct stat buf1, buf2;
-	pid_t pid;
+	struct stat statbuf;
 	int fail = 0;
 
-	pid = SAFE_FORK();
-	if (pid == 0) {
-		SAFE_SETREGID(bin_gid, bin_gid);
-		SAFE_SETREUID(bin_uid, bin_uid);
-		SAFE_MKDIR(TESTDIR2, 0777);
+	SAFE_MKDIR(TESTDIR2, 0777);
+	SAFE_STAT(TESTDIR2, &statbuf);
 
-		SAFE_STAT(TESTDIR2, &buf2);
-		SAFE_STAT(TESTDIR1, &buf1);
-
-		if (buf2.st_gid != buf1.st_gid) {
-			tst_res(TFAIL,
-				"New dir FAILED to inherit GID have %d expected %d",
-				buf2.st_gid, buf1.st_gid);
-			fail = 1;
-		}
-
-		if (!(buf2.st_mode & S_ISGID)) {
-			tst_res(TFAIL, "New dir FAILED to inherit S_ISGID");
-			fail = 1;
-		}
-
-		if (!fail)
-			tst_res(TPASS, "New dir inherited GID and S_ISGID");
+	if (statbuf.st_gid != free_gid) {
+		tst_res(TFAIL,
+			"New dir FAILED to inherit GID: has %d, expected %d",
+			statbuf.st_gid, free_gid);
+		fail = 1;
+	}
 
-		exit(0);
+	if (!(statbuf.st_mode & S_ISGID)) {
+		tst_res(TFAIL, "New dir FAILED to inherit S_ISGID");
+		fail = 1;
 	}
 
-	tst_reap_children();
+	if (!fail)
+		tst_res(TPASS, "New dir inherited GID and S_ISGID");
+
 	SAFE_RMDIR(TESTDIR2);
 }
 
 
 static void setup(void)
 {
-	struct passwd *pw;
-	struct stat buf;
-	pid_t pid;
+	struct passwd *pw = SAFE_GETPWNAM("nobody");
 
-	pw = SAFE_GETPWNAM("nobody");
-	nobody_uid = pw->pw_uid;
-	nobody_gid = pw->pw_gid;
-	pw = SAFE_GETPWNAM("bin");
-	bin_uid = pw->pw_uid;
-	bin_gid = pw->pw_gid;
+	free_gid = tst_get_free_gid(pw->pw_gid);
 
 	umask(0);
+	SAFE_MKDIR(TESTDIR1, 0777);
+	SAFE_CHMOD(TESTDIR1, 0777 | S_ISGID);
+	SAFE_CHOWN(TESTDIR1, getuid(), free_gid);
 
-	pid = SAFE_FORK();
-	if (pid == 0) {
-		SAFE_SETREGID(nobody_gid, nobody_gid);
-		SAFE_SETREUID(nobody_uid, nobody_uid);
-		SAFE_MKDIR(TESTDIR1, 0777);
-		SAFE_STAT(TESTDIR1, &buf);
-		SAFE_CHMOD(TESTDIR1, buf.st_mode | S_ISGID);
-		exit(0);
-	}
-
-	tst_reap_children();
+	SAFE_SETREGID(pw->pw_gid, pw->pw_gid);
+	SAFE_SETREUID(pw->pw_uid, pw->pw_uid);
 }
 
 static struct tst_test test = {
@@ -95,5 +67,4 @@ static struct tst_test test = {
 	.needs_tmpdir = 1,
 	.needs_root = 1,
 	.test_all = verify_mkdir,
-	.forks_child = 1,
 };
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [LTP] [PATCH 3/7] Add user/group ID lookup helper functions
  2021-09-03 15:48 [LTP] [PATCH 0/7] UID/GID lookup fixes Martin Doucha
  2021-09-03 15:48 ` [LTP] [PATCH 1/7] chmod05, fchmod05: Use free GID instead of "bin" group Martin Doucha
  2021-09-03 15:48 ` [LTP] [PATCH 2/7] Simplify syscalls/mkdir02 Martin Doucha
@ 2021-09-03 15:48 ` Martin Doucha
  2021-09-06 12:21     ` Cyril Hrubis
  2021-09-03 15:48 ` [LTP] [PATCH 4/7] syscalls/kill05: Use any two unprivileged users Martin Doucha
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 19+ messages in thread
From: Martin Doucha @ 2021-09-03 15:48 UTC (permalink / raw)
  To: ltp

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 include/tst_safe_macros.h |   6 +++
 include/tst_uid.h         |  17 +++++++
 lib/tst_uid.c             | 100 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 123 insertions(+)

diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h
index 6fd618597..0238a5de7 100644
--- a/include/tst_safe_macros.h
+++ b/include/tst_safe_macros.h
@@ -625,4 +625,10 @@ int safe_sysinfo(const char *file, const int lineno, struct sysinfo *info);
 #define SAFE_SYSINFO(info) \
 	safe_sysinfo(__FILE__, __LINE__, (info))
 
+struct passwd *safe_getpwent(const char *file, const int lineno);
+#define SAFE_GETPWENT() safe_getpwent(__FILE__, __LINE__)
+
+struct group *safe_getgrent(const char *file, const int lineno);
+#define SAFE_GETGRENT() safe_getgrent(__FILE__, __LINE__)
+
 #endif /* SAFE_MACROS_H__ */
diff --git a/include/tst_uid.h b/include/tst_uid.h
index 7135a9cad..a3bacf64a 100644
--- a/include/tst_uid.h
+++ b/include/tst_uid.h
@@ -15,4 +15,21 @@
 gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip);
 #define tst_get_free_gid(skip) tst_get_free_gid_(__FILE__, __LINE__, (skip))
 
+/*
+ * Get a specific number of unique existing non-root user or group IDs.
+ * Multiple calls will return additional results, if any. Both functions call
+ * SAFE_GETPWENT() and SAFE_GETGRENT() respectively.
+ * Call endpwent()/endgrent() after the last use of these functions.
+ * Call setpwent()/setgrent() to read user/group IDs from the beginning again.
+ */
+int tst_get_uids_(const char *file, const int lineno, unsigned int count,
+	uid_t *buf);
+#define tst_get_uids(count, buf) \
+	tst_get_uids_(__FILE__, __LINE__, (count), (buf))
+
+int tst_get_gids_(const char *file, const int lineno, unsigned int count,
+	gid_t *buf);
+#define tst_get_gids(count, buf) \
+	tst_get_gids_(__FILE__, __LINE__, (count), (buf))
+
 #endif /* TST_UID_H_ */
diff --git a/lib/tst_uid.c b/lib/tst_uid.c
index dd719d312..915a5bc34 100644
--- a/lib/tst_uid.c
+++ b/lib/tst_uid.c
@@ -3,8 +3,10 @@
  * Copyright (c) 2021 Linux Test Project
  */
 
+#define _XOPEN_SOURCE 500
 #include <sys/types.h>
 #include <grp.h>
+#include <pwd.h>
 #include <errno.h>
 
 #define TST_NO_DEFAULT_MAIN
@@ -36,3 +38,101 @@ gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip)
 	tst_brk_(file, lineno, TBROK, "No free group ID found");
 	return (gid_t)-1;
 }
+
+struct passwd *safe_getpwent(const char *file, const int lineno)
+{
+	struct passwd *ret;
+
+	errno = 0;
+	ret = getpwent();
+
+	if (!ret) {
+		if (errno) {
+			tst_brk_(file, lineno, TBROK | TERRNO,
+				"getpwent() failed");
+		} else {
+			tst_brk_(file, lineno, TBROK,
+				"getpwent() failed: end of file");
+		}
+	}
+
+	return ret;
+}
+
+struct group *safe_getgrent(const char *file, const int lineno)
+{
+	struct group *ret;
+
+	errno = 0;
+	ret = getgrent();
+
+	if (!ret) {
+		if (errno) {
+			tst_brk_(file, lineno, TBROK | TERRNO,
+				"getgrent() failed");
+		} else {
+			tst_brk_(file, lineno, TBROK,
+				"getgrent() failed: end of file");
+		}
+	}
+
+	return ret;
+}
+
+int tst_get_uids_(const char *file, const int lineno, unsigned int count,
+	uid_t *buf)
+{
+	struct passwd *pw;
+	unsigned int i, j;
+
+	for (i = 0; i < count;) {
+		pw = safe_getpwent(file, lineno);
+
+		if (!pw)
+			return -1;
+
+		if (!pw->pw_uid)
+			continue;
+
+		for (j = 0; j < i; j++) {
+			if (buf[j] == pw->pw_uid)
+				break;
+		}
+
+		if (j < i)
+			continue;
+
+		buf[i++] = pw->pw_uid;
+	}
+
+	return 0;
+}
+
+int tst_get_gids_(const char *file, const int lineno, unsigned int count,
+	gid_t *buf)
+{
+	struct group *gr;
+	unsigned int i, j;
+
+	for (i = 0; i < count;) {
+		gr = safe_getgrent(file, lineno);
+
+		if (!gr)
+			return -1;
+
+		if (!gr->gr_gid)
+			continue;
+
+		for (j = 0; j < i; j++) {
+			if (buf[j] == gr->gr_gid)
+				break;
+		}
+
+		if (j < i)
+			continue;
+
+		buf[i++] = gr->gr_gid;
+	}
+
+	return 0;
+}
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [LTP] [PATCH 4/7] syscalls/kill05: Use any two unprivileged users
  2021-09-03 15:48 [LTP] [PATCH 0/7] UID/GID lookup fixes Martin Doucha
                   ` (2 preceding siblings ...)
  2021-09-03 15:48 ` [LTP] [PATCH 3/7] Add user/group ID lookup helper functions Martin Doucha
@ 2021-09-03 15:48 ` Martin Doucha
  2021-09-06 12:28     ` Cyril Hrubis
  2021-09-03 15:48 ` [LTP] [PATCH 5/7] syscalls/mkdir04: Simplify test and use any existing users Martin Doucha
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 19+ messages in thread
From: Martin Doucha @ 2021-09-03 15:48 UTC (permalink / raw)
  To: ltp

Usernames such as "bin" may not exist on some systems. Find and use any two
unprivileged user IDs for the test instead of specific named users.

Includes minor code style cleanup in wait_for_flag().

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 testcases/kernel/syscalls/kill/kill05.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/testcases/kernel/syscalls/kill/kill05.c b/testcases/kernel/syscalls/kill/kill05.c
index e694126f6..1dfdda58f 100644
--- a/testcases/kernel/syscalls/kill/kill05.c
+++ b/testcases/kernel/syscalls/kill/kill05.c
@@ -17,15 +17,18 @@
  *	This test must be run as root.
  */
 
+#define _XOPEN_SOURCE 500
 #include <sys/wait.h>
+#include <sys/types.h>
 #include <pwd.h>
 #include <stdlib.h>
 #include "tst_test.h"
 #include "libnewipc.h"
 #include "tst_safe_sysv_ipc.h"
 #include "tst_safe_macros.h"
+#include "tst_uid.h"
 
-static uid_t nobody_uid, bin_uid;
+static uid_t test_users[2];
 static int *flag;
 static int shm_id = -1;
 static key_t shm_key;
@@ -35,8 +38,8 @@ static void wait_for_flag(int value)
 	while (1) {
 		if (*flag == value)
 			break;
-		else
-			usleep(100);
+
+		usleep(100);
 	}
 }
 
@@ -47,14 +50,14 @@ static void do_master_child(void)
 	*flag = 0;
 	pid1 = SAFE_FORK();
 	if (pid1 == 0) {
-		SAFE_SETREUID(nobody_uid, nobody_uid);
+		SAFE_SETREUID(test_users[0], test_users[0]);
 		*flag = 1;
 		wait_for_flag(2);
 
 		exit(0);
 	}
 
-	SAFE_SETREUID(bin_uid, bin_uid);
+	SAFE_SETREUID(test_users[1], test_users[1]);
 	wait_for_flag(1);
 	TEST(kill(pid1, SIGKILL));
 
@@ -85,17 +88,11 @@ static void verify_kill(void)
 
 static void setup(void)
 {
-	struct passwd *pw;
-
 	shm_key = GETIPCKEY();
 	shm_id = SAFE_SHMGET(shm_key, getpagesize(), 0666 | IPC_CREAT);
 	flag = SAFE_SHMAT(shm_id, 0, 0);
-
-	pw = SAFE_GETPWNAM("nobody");
-	nobody_uid = pw->pw_uid;
-
-	pw = SAFE_GETPWNAM("bin");
-	bin_uid = pw->pw_uid;
+	tst_get_uids(2, test_users);
+	endpwent();
 }
 
 static void cleanup(void)
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [LTP] [PATCH 5/7] syscalls/mkdir04: Simplify test and use any existing users
  2021-09-03 15:48 [LTP] [PATCH 0/7] UID/GID lookup fixes Martin Doucha
                   ` (3 preceding siblings ...)
  2021-09-03 15:48 ` [LTP] [PATCH 4/7] syscalls/kill05: Use any two unprivileged users Martin Doucha
@ 2021-09-03 15:48 ` Martin Doucha
  2021-09-03 15:48 ` [LTP] [PATCH 6/7] syscalls/setregid02-04: Simplify GID handling Martin Doucha
  2021-09-03 15:48 ` [LTP] [PATCH 7/7] syscalls/setregid02-04: Eliminate named group lookups Martin Doucha
  6 siblings, 0 replies; 19+ messages in thread
From: Martin Doucha @ 2021-09-03 15:48 UTC (permalink / raw)
  To: ltp

mkdir04 does not need to fork() in setup(). Usernames such as "bin" may not
exist on some systems. Find and use any two unprivileged users instead of
specific named users.

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 testcases/kernel/syscalls/mkdir/mkdir04.c | 30 ++++++-----------------
 1 file changed, 8 insertions(+), 22 deletions(-)

diff --git a/testcases/kernel/syscalls/mkdir/mkdir04.c b/testcases/kernel/syscalls/mkdir/mkdir04.c
index 87512a4be..3880611d7 100644
--- a/testcases/kernel/syscalls/mkdir/mkdir04.c
+++ b/testcases/kernel/syscalls/mkdir/mkdir04.c
@@ -7,20 +7,16 @@
  * user with restrictive permissions and that the errno is set to EACCESS.
  */
 
+#define _XOPEN_SOURCE 500
 #include <errno.h>
-#include <sys/stat.h>
 #include <sys/types.h>
 #include <pwd.h>
-#include <sys/wait.h>
-#include <unistd.h>
-#include <stdlib.h>
 #include "tst_test.h"
+#include "tst_uid.h"
 
 #define TESTDIR	 "testdir"
 #define TESTSUBDIR "testdir/testdir"
 
-static uid_t nobody_uid, bin_uid;
-
 static void verify_mkdir(void)
 {
 	if (mkdir(TESTSUBDIR, 0777) != -1) {
@@ -39,24 +35,15 @@ static void verify_mkdir(void)
 
 static void setup(void)
 {
-	struct passwd *pw;
-	pid_t pid;
-
-	pw = SAFE_GETPWNAM("nobody");
-	nobody_uid = pw->pw_uid;
-	pw = SAFE_GETPWNAM("bin");
-	bin_uid = pw->pw_uid;
+	uid_t test_users[2];
 
-	pid = SAFE_FORK();
-	if (pid == 0) {
-		SAFE_SETREUID(nobody_uid, nobody_uid);
-		SAFE_MKDIR(TESTDIR, 0700);
-		exit(0);
-	}
+	tst_get_uids(2, test_users);
+	endpwent();
 
-	tst_reap_children();
+	SAFE_MKDIR(TESTDIR, 0700);
+	SAFE_CHOWN(TESTDIR, test_users[0], getgid());
 
-	SAFE_SETREUID(bin_uid, bin_uid);
+	SAFE_SETREUID(test_users[1], test_users[1]);
 }
 
 static struct tst_test test = {
@@ -64,5 +51,4 @@ static struct tst_test test = {
 	.needs_tmpdir = 1,
 	.needs_root = 1,
 	.setup = setup,
-	.forks_child = 1,
 };
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [LTP] [PATCH 6/7] syscalls/setregid02-04: Simplify GID handling
  2021-09-03 15:48 [LTP] [PATCH 0/7] UID/GID lookup fixes Martin Doucha
                   ` (4 preceding siblings ...)
  2021-09-03 15:48 ` [LTP] [PATCH 5/7] syscalls/mkdir04: Simplify test and use any existing users Martin Doucha
@ 2021-09-03 15:48 ` Martin Doucha
  2021-09-03 15:48 ` [LTP] [PATCH 7/7] syscalls/setregid02-04: Eliminate named group lookups Martin Doucha
  6 siblings, 0 replies; 19+ messages in thread
From: Martin Doucha @ 2021-09-03 15:48 UTC (permalink / raw)
  To: ltp

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 .../kernel/syscalls/setregid/setregid02.c     | 48 +++++++---------
 .../kernel/syscalls/setregid/setregid03.c     | 57 ++++++++-----------
 .../kernel/syscalls/setregid/setregid04.c     | 42 +++++++-------
 3 files changed, 61 insertions(+), 86 deletions(-)

diff --git a/testcases/kernel/syscalls/setregid/setregid02.c b/testcases/kernel/syscalls/setregid/setregid02.c
index 310eb3a21..2174f81f9 100644
--- a/testcases/kernel/syscalls/setregid/setregid02.c
+++ b/testcases/kernel/syscalls/setregid/setregid02.c
@@ -19,12 +19,10 @@
 #include "tst_test.h"
 #include "compat_tst_16.h"
 
-static gid_t neg_one = -1;
+static gid_t root_gid, nobody_gid, other_gid, neg_one = -1;
 
 static struct passwd *ltpuser;
 
-static struct group ltpgroup, root, bin;
-
 /*
  * The following structure contains all test data.  Each structure in the array
  * is used for a separate test.  The tests are executed in the for loop below.
@@ -34,50 +32,41 @@ static struct tcase {
 	gid_t *real_gid;
 	gid_t *eff_gid;
 	int exp_errno;
-	struct group *exp_real_usr;
-	struct group *exp_eff_usr;
+	gid_t *exp_real_usr;
+	gid_t *exp_eff_usr;
 	char *test_msg;
 } tcases[] = {
 	{
-	&neg_one, &root.gr_gid, EPERM, &ltpgroup, &ltpgroup,
+	&neg_one, &root_gid, EPERM, &nobody_gid, &nobody_gid,
 		    "After setregid(-1, root),"}, {
-	&neg_one, &bin.gr_gid, EPERM, &ltpgroup, &ltpgroup,
+	&neg_one, &other_gid, EPERM, &nobody_gid, &nobody_gid,
 		    "After setregid(-1, bin)"}, {
-	&root.gr_gid, &neg_one, EPERM, &ltpgroup, &ltpgroup,
+	&root_gid, &neg_one, EPERM, &nobody_gid, &nobody_gid,
 		    "After setregid(root,-1),"}, {
-	&bin.gr_gid, &neg_one, EPERM, &ltpgroup, &ltpgroup,
+	&other_gid, &neg_one, EPERM, &nobody_gid, &nobody_gid,
 		    "After setregid(bin, -1),"}, {
-	&root.gr_gid, &bin.gr_gid, EPERM, &ltpgroup, &ltpgroup,
+	&root_gid, &other_gid, EPERM, &nobody_gid, &nobody_gid,
 		    "After setregid(root, bin)"}, {
-	&bin.gr_gid, &root.gr_gid, EPERM, &ltpgroup, &ltpgroup,
+	&other_gid, &root_gid, EPERM, &nobody_gid, &nobody_gid,
 		    "After setregid(bin, root),"}
 };
 
-static struct group get_group_by_name(const char *name)
+static gid_t get_group_by_name(const char *name)
 {
 	struct group *ret = SAFE_GETGRNAM(name);
 
 	GID16_CHECK(ret->gr_gid, setregid);
 
-	return *ret;
-}
-
-static struct group get_group_by_gid(gid_t gid)
-{
-	struct group *ret = SAFE_GETGRGID(gid);
-
-	GID16_CHECK(ret->gr_gid, setregid);
-
-	return *ret;
+	return ret->gr_gid;
 }
 
-void gid_verify(struct group *rg, struct group *eg, char *when)
+void gid_verify(gid_t rg, gid_t eg, char *when)
 {
-	if ((getgid() != rg->gr_gid) || (getegid() != eg->gr_gid)) {
+	if ((getgid() != rg) || (getegid() != eg)) {
 		tst_res(TFAIL, "ERROR: %s real gid = %d; effective gid = %d",
 			 when, getgid(), getegid());
 		tst_res(TINFO, "Expected: real gid = %d; effective gid = %d",
-			 rg->gr_gid, eg->gr_gid);
+			 rg, eg);
 		return;
 	}
 
@@ -107,7 +96,7 @@ static void run(unsigned int n)
 			"setregid(%d, %d) did not fail (ret: %ld) as expected (ret: -1).",
 			*tc->real_gid, *tc->eff_gid, TST_RET);
 	}
-	gid_verify(tc->exp_real_usr, tc->exp_eff_usr, tc->test_msg);
+	gid_verify(*tc->exp_real_usr, *tc->exp_eff_usr, tc->test_msg);
 }
 
 static void setup(void)
@@ -117,9 +106,10 @@ static void setup(void)
 	SAFE_SETGID(ltpuser->pw_gid);
 	SAFE_SETUID(ltpuser->pw_uid);
 
-	root = get_group_by_name("root");
-	ltpgroup = get_group_by_gid(ltpuser->pw_gid);
-	bin = get_group_by_name("bin");
+	nobody_gid = ltpuser->pw_gid;
+	GID16_CHECK(nobody_gid, setregid);
+	root_gid = get_group_by_name("root");
+	other_gid = get_group_by_name("bin");
 }
 
 static struct tst_test test = {
diff --git a/testcases/kernel/syscalls/setregid/setregid03.c b/testcases/kernel/syscalls/setregid/setregid03.c
index e25a7ec72..35aa92b8e 100644
--- a/testcases/kernel/syscalls/setregid/setregid03.c
+++ b/testcases/kernel/syscalls/setregid/setregid03.c
@@ -16,72 +16,61 @@
 
 static int fail = -1;
 static int pass;
-static gid_t neg_one = -1;
+static gid_t primary_gid, secondary_gid, neg_one = -1;
 
-struct group nobody_gr, daemon_gr, root_gr, bin_gr;
 struct passwd nobody;
 
 struct tcase {
 	gid_t *real_gid;
 	gid_t *eff_gid;
 	int *exp_ret;
-	struct group *exp_real_usr;
-	struct group *exp_eff_usr;
+	gid_t *exp_real_usr;
+	gid_t *exp_eff_usr;
 	char *test_msg;
 } tcases[] = {
 	{
-	&daemon_gr.gr_gid, &bin_gr.gr_gid, &pass, &daemon_gr, &bin_gr,
+	&primary_gid, &secondary_gid, &pass, &primary_gid, &secondary_gid,
 		    "After setregid(daemon, bin),"}, {
-	&neg_one, &daemon_gr.gr_gid, &pass, &daemon_gr, &daemon_gr,
+	&neg_one, &primary_gid, &pass, &primary_gid, &primary_gid,
 		    "After setregid(-1, daemon)"}, {
-	&neg_one, &bin_gr.gr_gid, &pass, &daemon_gr, &bin_gr,
+	&neg_one, &secondary_gid, &pass, &primary_gid, &secondary_gid,
 		    "After setregid(-1, bin),"}, {
-	&bin_gr.gr_gid, &neg_one, &pass, &bin_gr, &bin_gr,
+	&secondary_gid, &neg_one, &pass, &secondary_gid, &secondary_gid,
 		    "After setregid(bin, -1),"}, {
-	&neg_one, &neg_one, &pass, &bin_gr, &bin_gr,
+	&neg_one, &neg_one, &pass, &secondary_gid, &secondary_gid,
 		    "After setregid(-1, -1),"}, {
-	&neg_one, &bin_gr.gr_gid, &pass, &bin_gr, &bin_gr,
+	&neg_one, &secondary_gid, &pass, &secondary_gid, &secondary_gid,
 		    "After setregid(-1, bin),"}, {
-	&bin_gr.gr_gid, &neg_one, &pass, &bin_gr, &bin_gr,
+	&secondary_gid, &neg_one, &pass, &secondary_gid, &secondary_gid,
 		    "After setregid(bin, -1),"}, {
-	&bin_gr.gr_gid, &bin_gr.gr_gid, &pass, &bin_gr, &bin_gr,
+	&secondary_gid, &secondary_gid, &pass, &secondary_gid, &secondary_gid,
 		    "After setregid(bin, bin),"}, {
-	&daemon_gr.gr_gid, &neg_one, &fail, &bin_gr, &bin_gr,
+	&primary_gid, &neg_one, &fail, &secondary_gid, &secondary_gid,
 		    "After setregid(daemon, -1)"}, {
-	&neg_one, &daemon_gr.gr_gid, &fail, &bin_gr, &bin_gr,
+	&neg_one, &primary_gid, &fail, &secondary_gid, &secondary_gid,
 		    "After setregid(-1, daemon)"}, {
-	&daemon_gr.gr_gid, &daemon_gr.gr_gid, &fail, &bin_gr, &bin_gr,
+	&primary_gid, &primary_gid, &fail, &secondary_gid, &secondary_gid,
 		    "After setregid(daemon, daemon)"},};
 
 
-static struct group get_group_fallback(const char *gr1, const char *gr2)
-{
-	struct group *junk;
-
-	junk = SAFE_GETGRNAM_FALLBACK(gr1, gr2);
-	GID16_CHECK(junk->gr_gid, setregid);
-	return *junk;
-}
-
-static struct group get_group(const char *group)
+static gid_t get_group(const char *group)
 {
 	struct group *junk;
 
 	junk = SAFE_GETGRNAM(group);
 	GID16_CHECK(junk->gr_gid, setregid);
-	return *junk;
+	return junk->gr_gid;
 }
 
 static void setup(void)
 {
 	nobody = *SAFE_GETPWNAM("nobody");
 
-	nobody_gr = get_group_fallback("nobody", "nogroup");
-	daemon_gr = get_group("daemon");
-	bin_gr = get_group("bin");
+	primary_gid = get_group("daemon");
+	secondary_gid = get_group("bin");
 
 	/* set the appropriate ownership values */
-	SAFE_SETREGID(daemon_gr.gr_gid, bin_gr.gr_gid);
+	SAFE_SETREGID(primary_gid, secondary_gid);
 	SAFE_SETEUID(nobody.pw_uid);
 }
 
@@ -109,13 +98,13 @@ static void test_failure(struct tcase *tc)
 			*tc->real_gid, *tc->eff_gid);
 }
 
-static void gid_verify(struct group *rg, struct group *eg, char *when)
+static void gid_verify(gid_t rg, gid_t eg, char *when)
 {
-	if ((getgid() != rg->gr_gid) || (getegid() != eg->gr_gid)) {
+	if ((getgid() != rg) || (getegid() != eg)) {
 		tst_res(TFAIL, "ERROR: %s real gid = %d; effective gid = %d",
 			 when, getgid(), getegid());
 		tst_res(TINFO, "Expected: real gid = %d; effective gid = %d",
-			 rg->gr_gid, eg->gr_gid);
+			 rg, eg);
 	} else {
 		tst_res(TPASS,
 			"real or effective gid was modified as expected");
@@ -134,7 +123,7 @@ static void run(unsigned int i)
 	else
 		test_failure(tc);
 
-	gid_verify(tc->exp_real_usr, tc->exp_eff_usr, tc->test_msg);
+	gid_verify(*tc->exp_real_usr, *tc->exp_eff_usr, tc->test_msg);
 }
 
 void run_all(void)
diff --git a/testcases/kernel/syscalls/setregid/setregid04.c b/testcases/kernel/syscalls/setregid/setregid04.c
index 9490ae173..6b9e3f524 100644
--- a/testcases/kernel/syscalls/setregid/setregid04.c
+++ b/testcases/kernel/syscalls/setregid/setregid04.c
@@ -12,9 +12,7 @@
 #include "tst_test.h"
 #include "compat_tst_16.h"
 
-static gid_t neg_one = -1;
-
-static struct group nobody_gr, daemon_gr, root_gr, bin_gr;
+static gid_t first_gid, second_gid, root_gid, neg_one = -1;
 
 /*
  * The following structure contains all test data.  Each structure in the array
@@ -24,45 +22,44 @@ static struct group nobody_gr, daemon_gr, root_gr, bin_gr;
 struct test_data_t {
 	gid_t *real_gid;
 	gid_t *eff_gid;
-	struct group *exp_real_usr;
-	struct group *exp_eff_usr;
+	gid_t *exp_real_usr;
+	gid_t *exp_eff_usr;
 	const char *test_msg;
 } test_data[] = {
 	{
-	&root_gr.gr_gid, &root_gr.gr_gid, &root_gr, &root_gr,
+	&root_gid, &root_gid, &root_gid, &root_gid,
 		    "After setregid(root, root),"}, {
-	&nobody_gr.gr_gid, &neg_one, &nobody_gr, &root_gr,
+	&first_gid, &neg_one, &first_gid, &root_gid,
 		    "After setregid(nobody, -1)"}, {
-	&root_gr.gr_gid, &neg_one, &root_gr, &root_gr,
+	&root_gid, &neg_one, &root_gid, &root_gid,
 		    "After setregid(root,-1),"}, {
-	&neg_one, &neg_one, &root_gr, &root_gr,
+	&neg_one, &neg_one, &root_gid, &root_gid,
 		    "After setregid(-1, -1),"}, {
-	&neg_one, &root_gr.gr_gid, &root_gr, &root_gr,
+	&neg_one, &root_gid, &root_gid, &root_gid,
 		    "After setregid(-1, root)"}, {
-	&root_gr.gr_gid, &neg_one, &root_gr, &root_gr,
+	&root_gid, &neg_one, &root_gid, &root_gid,
 		    "After setregid(root, -1),"}, {
-	&daemon_gr.gr_gid, &nobody_gr.gr_gid, &daemon_gr, &nobody_gr,
+	&second_gid, &first_gid, &second_gid, &first_gid,
 		    "After setregid(daemon, nobody)"}, {
-	&neg_one, &neg_one, &daemon_gr, &nobody_gr,
+	&neg_one, &neg_one, &second_gid, &first_gid,
 		    "After setregid(-1, -1)"}, {
-	&neg_one, &nobody_gr.gr_gid, &daemon_gr, &nobody_gr,
+	&neg_one, &first_gid, &second_gid, &first_gid,
 		    "After setregid(-1, nobody)"}
 };
 
-static void gid_verify(struct group *rg, struct group *eg, const char *when)
+static void gid_verify(gid_t rg, gid_t eg, const char *when)
 {
-	if ((getgid() != rg->gr_gid) || (getegid() != eg->gr_gid)) {
+	if ((getgid() != rg) || (getegid() != eg)) {
 		tst_res(TFAIL, "ERROR: %s real gid = %d; effective gid = %d",
 			 when, getgid(), getegid());
 		tst_res(TINFO, "Expected: real gid = %d; effective gid = %d",
-			 rg->gr_gid, eg->gr_gid);
+			 rg, eg);
 	} else {
 		tst_res(TPASS,
 			"real or effective gid was modified as expected");
 	}
 }
 
-
 static void run(unsigned int i)
 {
 	/* Set the real or effective group id */
@@ -74,16 +71,15 @@ static void run(unsigned int i)
 		return;
 	}
 
-	gid_verify(test_data[i].exp_real_usr, test_data[i].exp_eff_usr,
+	gid_verify(*test_data[i].exp_real_usr, *test_data[i].exp_eff_usr,
 		   test_data[i].test_msg);
 }
 
 static void setup(void)
 {
-	root_gr = *SAFE_GETGRNAM("root");
-	nobody_gr = *SAFE_GETGRNAM_FALLBACK("nobody", "nogroup");
-	daemon_gr = *SAFE_GETGRNAM("daemon");
-	bin_gr = *SAFE_GETGRNAM("bin");
+	root_gid = SAFE_GETGRNAM("root")->gr_gid;
+	first_gid = SAFE_GETGRNAM_FALLBACK("nobody", "nogroup")->gr_gid;
+	second_gid = SAFE_GETGRNAM("daemon")->gr_gid;
 }
 
 static struct tst_test test = {
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [LTP] [PATCH 7/7] syscalls/setregid02-04: Eliminate named group lookups
  2021-09-03 15:48 [LTP] [PATCH 0/7] UID/GID lookup fixes Martin Doucha
                   ` (5 preceding siblings ...)
  2021-09-03 15:48 ` [LTP] [PATCH 6/7] syscalls/setregid02-04: Simplify GID handling Martin Doucha
@ 2021-09-03 15:48 ` Martin Doucha
  6 siblings, 0 replies; 19+ messages in thread
From: Martin Doucha @ 2021-09-03 15:48 UTC (permalink / raw)
  To: ltp

Group names like "bin" or "daemon" may not exist on some systems. Find and use
any unprivieged group IDs instead of specific named groups.

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 .../kernel/syscalls/setregid/setregid02.c     | 39 +++++++++--------
 .../kernel/syscalls/setregid/setregid03.c     | 43 +++++++++----------
 .../kernel/syscalls/setregid/setregid04.c     | 22 +++++++---
 3 files changed, 58 insertions(+), 46 deletions(-)

diff --git a/testcases/kernel/syscalls/setregid/setregid02.c b/testcases/kernel/syscalls/setregid/setregid02.c
index 2174f81f9..d7d5eaa24 100644
--- a/testcases/kernel/syscalls/setregid/setregid02.c
+++ b/testcases/kernel/syscalls/setregid/setregid02.c
@@ -17,6 +17,7 @@
 #include <stdlib.h>
 
 #include "tst_test.h"
+#include "tst_uid.h"
 #include "compat_tst_16.h"
 
 static gid_t root_gid, nobody_gid, other_gid, neg_one = -1;
@@ -40,26 +41,17 @@ static struct tcase {
 	&neg_one, &root_gid, EPERM, &nobody_gid, &nobody_gid,
 		    "After setregid(-1, root),"}, {
 	&neg_one, &other_gid, EPERM, &nobody_gid, &nobody_gid,
-		    "After setregid(-1, bin)"}, {
+		    "After setregid(-1, other)"}, {
 	&root_gid, &neg_one, EPERM, &nobody_gid, &nobody_gid,
 		    "After setregid(root,-1),"}, {
 	&other_gid, &neg_one, EPERM, &nobody_gid, &nobody_gid,
-		    "After setregid(bin, -1),"}, {
+		    "After setregid(other, -1),"}, {
 	&root_gid, &other_gid, EPERM, &nobody_gid, &nobody_gid,
-		    "After setregid(root, bin)"}, {
+		    "After setregid(root, other)"}, {
 	&other_gid, &root_gid, EPERM, &nobody_gid, &nobody_gid,
-		    "After setregid(bin, root),"}
+		    "After setregid(other, root),"}
 };
 
-static gid_t get_group_by_name(const char *name)
-{
-	struct group *ret = SAFE_GETGRNAM(name);
-
-	GID16_CHECK(ret->gr_gid, setregid);
-
-	return ret->gr_gid;
-}
-
 void gid_verify(gid_t rg, gid_t eg, char *when)
 {
 	if ((getgid() != rg) || (getegid() != eg)) {
@@ -101,15 +93,26 @@ static void run(unsigned int n)
 
 static void setup(void)
 {
+	struct group *grp;
+
 	ltpuser = SAFE_GETPWNAM("nobody");
+	nobody_gid = ltpuser->pw_gid;
+	root_gid = getgid();
+	setgrent();
 
-	SAFE_SETGID(ltpuser->pw_gid);
-	SAFE_SETUID(ltpuser->pw_uid);
+	do {
+		grp = SAFE_GETGRENT();
+	} while (grp->gr_gid == root_gid || grp->gr_gid == nobody_gid);
 
-	nobody_gid = ltpuser->pw_gid;
+	other_gid = grp->gr_gid;
+	endgrent();
+
+	GID16_CHECK(root_gid, setregid);
 	GID16_CHECK(nobody_gid, setregid);
-	root_gid = get_group_by_name("root");
-	other_gid = get_group_by_name("bin");
+	GID16_CHECK(other_gid, setregid);
+
+	SAFE_SETGID(ltpuser->pw_gid);
+	SAFE_SETUID(ltpuser->pw_uid);
 }
 
 static struct tst_test test = {
diff --git a/testcases/kernel/syscalls/setregid/setregid03.c b/testcases/kernel/syscalls/setregid/setregid03.c
index 35aa92b8e..66667c6b2 100644
--- a/testcases/kernel/syscalls/setregid/setregid03.c
+++ b/testcases/kernel/syscalls/setregid/setregid03.c
@@ -9,9 +9,12 @@
  * Test setregid() when executed by a non-root user.
  */
 
+#include <sys/types.h>
+#include <grp.h>
 #include <pwd.h>
 
 #include "tst_test.h"
+#include "tst_uid.h"
 #include "compat_tst_16.h"
 
 static int fail = -1;
@@ -30,44 +33,40 @@ struct tcase {
 } tcases[] = {
 	{
 	&primary_gid, &secondary_gid, &pass, &primary_gid, &secondary_gid,
-		    "After setregid(daemon, bin),"}, {
+		    "After setregid(primary, secondary),"}, {
 	&neg_one, &primary_gid, &pass, &primary_gid, &primary_gid,
-		    "After setregid(-1, daemon)"}, {
+		    "After setregid(-1, primary)"}, {
 	&neg_one, &secondary_gid, &pass, &primary_gid, &secondary_gid,
-		    "After setregid(-1, bin),"}, {
+		    "After setregid(-1, secondary),"}, {
 	&secondary_gid, &neg_one, &pass, &secondary_gid, &secondary_gid,
-		    "After setregid(bin, -1),"}, {
+		    "After setregid(secondary, -1),"}, {
 	&neg_one, &neg_one, &pass, &secondary_gid, &secondary_gid,
 		    "After setregid(-1, -1),"}, {
 	&neg_one, &secondary_gid, &pass, &secondary_gid, &secondary_gid,
-		    "After setregid(-1, bin),"}, {
+		    "After setregid(-1, secondary),"}, {
 	&secondary_gid, &neg_one, &pass, &secondary_gid, &secondary_gid,
-		    "After setregid(bin, -1),"}, {
+		    "After setregid(secondary, -1),"}, {
 	&secondary_gid, &secondary_gid, &pass, &secondary_gid, &secondary_gid,
-		    "After setregid(bin, bin),"}, {
+		    "After setregid(secondary, secondary),"}, {
 	&primary_gid, &neg_one, &fail, &secondary_gid, &secondary_gid,
-		    "After setregid(daemon, -1)"}, {
+		    "After setregid(primary, -1)"}, {
 	&neg_one, &primary_gid, &fail, &secondary_gid, &secondary_gid,
-		    "After setregid(-1, daemon)"}, {
+		    "After setregid(-1, primary)"}, {
 	&primary_gid, &primary_gid, &fail, &secondary_gid, &secondary_gid,
-		    "After setregid(daemon, daemon)"},};
-
-
-static gid_t get_group(const char *group)
-{
-	struct group *junk;
-
-	junk = SAFE_GETGRNAM(group);
-	GID16_CHECK(junk->gr_gid, setregid);
-	return junk->gr_gid;
-}
+		    "After setregid(primary, primary)"},};
 
 static void setup(void)
 {
+	gid_t test_groups[2];
+
 	nobody = *SAFE_GETPWNAM("nobody");
 
-	primary_gid = get_group("daemon");
-	secondary_gid = get_group("bin");
+	tst_get_gids(2, test_groups);
+	primary_gid = test_groups[0];
+	secondary_gid = test_groups[1];
+	endgrent();
+	GID16_CHECK(primary_gid, setregid);
+	GID16_CHECK(secondary_gid, setregid);
 
 	/* set the appropriate ownership values */
 	SAFE_SETREGID(primary_gid, secondary_gid);
diff --git a/testcases/kernel/syscalls/setregid/setregid04.c b/testcases/kernel/syscalls/setregid/setregid04.c
index 6b9e3f524..911dc0d01 100644
--- a/testcases/kernel/syscalls/setregid/setregid04.c
+++ b/testcases/kernel/syscalls/setregid/setregid04.c
@@ -9,7 +9,11 @@
  * Test setregid() when executed by root.
  */
 
+#include <sys/types.h>
+#include <grp.h>
+
 #include "tst_test.h"
+#include "tst_uid.h"
 #include "compat_tst_16.h"
 
 static gid_t first_gid, second_gid, root_gid, neg_one = -1;
@@ -30,7 +34,7 @@ struct test_data_t {
 	&root_gid, &root_gid, &root_gid, &root_gid,
 		    "After setregid(root, root),"}, {
 	&first_gid, &neg_one, &first_gid, &root_gid,
-		    "After setregid(nobody, -1)"}, {
+		    "After setregid(first, -1)"}, {
 	&root_gid, &neg_one, &root_gid, &root_gid,
 		    "After setregid(root,-1),"}, {
 	&neg_one, &neg_one, &root_gid, &root_gid,
@@ -40,11 +44,11 @@ struct test_data_t {
 	&root_gid, &neg_one, &root_gid, &root_gid,
 		    "After setregid(root, -1),"}, {
 	&second_gid, &first_gid, &second_gid, &first_gid,
-		    "After setregid(daemon, nobody)"}, {
+		    "After setregid(second, first)"}, {
 	&neg_one, &neg_one, &second_gid, &first_gid,
 		    "After setregid(-1, -1)"}, {
 	&neg_one, &first_gid, &second_gid, &first_gid,
-		    "After setregid(-1, nobody)"}
+		    "After setregid(-1, first)"}
 };
 
 static void gid_verify(gid_t rg, gid_t eg, const char *when)
@@ -77,9 +81,15 @@ static void run(unsigned int i)
 
 static void setup(void)
 {
-	root_gid = SAFE_GETGRNAM("root")->gr_gid;
-	first_gid = SAFE_GETGRNAM_FALLBACK("nobody", "nogroup")->gr_gid;
-	second_gid = SAFE_GETGRNAM("daemon")->gr_gid;
+	gid_t groups[3];
+
+	tst_get_gids(3, groups);
+	root_gid = getgid();
+
+	/* At most one of the groups may be equal to root_gid */
+	first_gid = groups[0] != root_gid ? groups[0] : groups[2];
+	second_gid = groups[1] != root_gid ? groups[1] : groups[2];
+	endgrent();
 }
 
 static struct tst_test test = {
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* Re: [LTP] [PATCH 1/7] chmod05, fchmod05: Use free GID instead of "bin" group
@ 2021-09-06 12:20     ` Cyril Hrubis
  0 siblings, 0 replies; 19+ messages in thread
From: Cyril Hrubis @ 2021-09-06 12:20 UTC (permalink / raw)
  To: Martin Doucha; +Cc: ltp

Hi!
Pushed, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

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

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [LTP] [PATCH 2/7] Simplify syscalls/mkdir02
@ 2021-09-06 12:20     ` Cyril Hrubis
  0 siblings, 0 replies; 19+ messages in thread
From: Cyril Hrubis @ 2021-09-06 12:20 UTC (permalink / raw)
  To: Martin Doucha; +Cc: ltp

Hi!
Pushed, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

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

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [LTP] [PATCH 3/7] Add user/group ID lookup helper functions
@ 2021-09-06 12:21     ` Cyril Hrubis
  2021-09-06 12:40         ` Martin Doucha
  0 siblings, 1 reply; 19+ messages in thread
From: Cyril Hrubis @ 2021-09-06 12:21 UTC (permalink / raw)
  To: Martin Doucha; +Cc: ltp

Hi!
> diff --git a/lib/tst_uid.c b/lib/tst_uid.c
> index dd719d312..915a5bc34 100644
> --- a/lib/tst_uid.c
> +++ b/lib/tst_uid.c
> @@ -3,8 +3,10 @@
>   * Copyright (c) 2021 Linux Test Project
>   */
>  
> +#define _XOPEN_SOURCE 500
>  #include <sys/types.h>
>  #include <grp.h>
> +#include <pwd.h>
>  #include <errno.h>
>  
>  #define TST_NO_DEFAULT_MAIN
> @@ -36,3 +38,101 @@ gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip)
>  	tst_brk_(file, lineno, TBROK, "No free group ID found");
>  	return (gid_t)-1;
>  }
> +
> +struct passwd *safe_getpwent(const char *file, const int lineno)
> +{
> +	struct passwd *ret;
> +
> +	errno = 0;
> +	ret = getpwent();
> +
> +	if (!ret) {
> +		if (errno) {
> +			tst_brk_(file, lineno, TBROK | TERRNO,
> +				"getpwent() failed");
> +		} else {
> +			tst_brk_(file, lineno, TBROK,
> +				"getpwent() failed: end of file");
> +		}

I'm not sure if "no more users in the /etc/passwd" should be reported as
an error.

-- 
Cyril Hrubis
chrubis@suse.cz

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

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [LTP] [PATCH 4/7] syscalls/kill05: Use any two unprivileged users
@ 2021-09-06 12:28     ` Cyril Hrubis
  2021-09-06 12:47         ` Martin Doucha
  0 siblings, 1 reply; 19+ messages in thread
From: Cyril Hrubis @ 2021-09-06 12:28 UTC (permalink / raw)
  To: Martin Doucha; +Cc: ltp

Hi!
> Usernames such as "bin" may not exist on some systems. Find and use any two
> unprivileged user IDs for the test instead of specific named users.

I do wonder if we have to lookup the UIDs in this case. As long as I
understand the test correctly we are testing that no signals are send to
a process if the sender real of effective UID do not match either of
real and saved set UIDs of the receiving process. If my understanding is
correct we can just take two arbitrary UIDs that != 0 and the test
should work just fine.

-- 
Cyril Hrubis
chrubis@suse.cz

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

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [LTP] [PATCH 3/7] Add user/group ID lookup helper functions
@ 2021-09-06 12:40         ` Martin Doucha
  0 siblings, 0 replies; 19+ messages in thread
From: Martin Doucha @ 2021-09-06 12:40 UTC (permalink / raw)
  To: Cyril Hrubis; +Cc: ltp

On 06. 09. 21 14:21, Cyril Hrubis wrote:
> Hi!
>> +	if (!ret) {
>> +		if (errno) {
>> +			tst_brk_(file, lineno, TBROK | TERRNO,
>> +				"getpwent() failed");
>> +		} else {
>> +			tst_brk_(file, lineno, TBROK,
>> +				"getpwent() failed: end of file");
>> +		}
> 
> I'm not sure if "no more users in the /etc/passwd" should be reported as
> an error.

The alternative would be that pretty much every test that calls
SAFE_GETPWENT() in setup() will need to check for NULL and explicitly
call tst_brk(). I don't see any use for allowing SAFE_GETPWENT() to
return NULL outside cleanup().

-- 
Martin Doucha   mdoucha@suse.cz
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

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

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [LTP] [PATCH 4/7] syscalls/kill05: Use any two unprivileged users
@ 2021-09-06 12:47         ` Martin Doucha
  2021-09-06 12:50             ` Cyril Hrubis
  0 siblings, 1 reply; 19+ messages in thread
From: Martin Doucha @ 2021-09-06 12:47 UTC (permalink / raw)
  To: Cyril Hrubis; +Cc: ltp

On 06. 09. 21 14:28, Cyril Hrubis wrote:
> Hi!
>> Usernames such as "bin" may not exist on some systems. Find and use any two
>> unprivileged user IDs for the test instead of specific named users.
> 
> I do wonder if we have to lookup the UIDs in this case. As long as I
> understand the test correctly we are testing that no signals are send to
> a process if the sender real of effective UID do not match either of
> real and saved set UIDs of the receiving process. If my understanding is
> correct we can just take two arbitrary UIDs that != 0 and the test
> should work just fine.

We could just use UID 1 and 2 for this test but I'd rather not assume
that set*uid() functions allow setting unused UIDs. Even if it works
now, it could easily break in the future or on some special system auth
backends.

-- 
Martin Doucha   mdoucha@suse.cz
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

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

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [LTP] [PATCH 4/7] syscalls/kill05: Use any two unprivileged users
@ 2021-09-06 12:50             ` Cyril Hrubis
  2021-09-06 13:01                 ` Martin Doucha
  2021-09-06 13:02                 ` Cyril Hrubis
  0 siblings, 2 replies; 19+ messages in thread
From: Cyril Hrubis @ 2021-09-06 12:50 UTC (permalink / raw)
  To: Martin Doucha; +Cc: ltp

Hi!
> > I do wonder if we have to lookup the UIDs in this case. As long as I
> > understand the test correctly we are testing that no signals are send to
> > a process if the sender real of effective UID do not match either of
> > real and saved set UIDs of the receiving process. If my understanding is
> > correct we can just take two arbitrary UIDs that != 0 and the test
> > should work just fine.
> 
> We could just use UID 1 and 2 for this test but I'd rather not assume
> that set*uid() functions allow setting unused UIDs. Even if it works
> now, it could easily break in the future or on some special system auth
> backends.

Don't we already depend on being able to use unused GID? How is this
different?

-- 
Cyril Hrubis
chrubis@suse.cz

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

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [LTP] [PATCH 4/7] syscalls/kill05: Use any two unprivileged users
@ 2021-09-06 13:01                 ` Martin Doucha
  2021-09-06 13:05                     ` Cyril Hrubis
  0 siblings, 1 reply; 19+ messages in thread
From: Martin Doucha @ 2021-09-06 13:01 UTC (permalink / raw)
  To: Cyril Hrubis; +Cc: ltp

On 06. 09. 21 14:50, Cyril Hrubis wrote:
> Hi!
>>> I do wonder if we have to lookup the UIDs in this case. As long as I
>>> understand the test correctly we are testing that no signals are send to
>>> a process if the sender real of effective UID do not match either of
>>> real and saved set UIDs of the receiving process. If my understanding is
>>> correct we can just take two arbitrary UIDs that != 0 and the test
>>> should work just fine.
>>
>> We could just use UID 1 and 2 for this test but I'd rather not assume
>> that set*uid() functions allow setting unused UIDs. Even if it works
>> now, it could easily break in the future or on some special system auth
>> backends.
> 
> Don't we already depend on being able to use unused GID? How is this
> different?

We only set unused user/group IDs on files and directories using
chown(). That's perfectly safe because inode owner is just an arbitrary
number that doesn't need to exist as a real entry in the user/group
database. You can get the same result by creating the files under a
valid owner and then deleting the respective user/group so the system
has to be able to handle that situation.

But setting a non-existent UID/GID on a running process is different
because the kernel might try to look up some additional data in the
user/group database and block the credentials change if the lookup fails.

-- 
Martin Doucha   mdoucha@suse.cz
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

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

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [LTP] [PATCH 4/7] syscalls/kill05: Use any two unprivileged users
@ 2021-09-06 13:02                 ` Cyril Hrubis
  0 siblings, 0 replies; 19+ messages in thread
From: Cyril Hrubis @ 2021-09-06 13:02 UTC (permalink / raw)
  To: Martin Doucha; +Cc: ltp

Hi!
> > We could just use UID 1 and 2 for this test but I'd rather not assume
> > that set*uid() functions allow setting unused UIDs. Even if it works
> > now, it could easily break in the future or on some special system auth
> > backends.
> 
> Don't we already depend on being able to use unused GID? How is this
> different?

Hmm we pass the unused GID mostly to chown(), but still.

The Linux manual page specify that setuid() may fail to change if
the UID is not valid inside a namespace, that means that either there is
no UID map or particular UID is missing from the UID map. So reading
/etc/passwd hypotetically help if we are inside of a thight container
and /etc/passwd matches the UID map, but I'm not sure if this is worth
of the effort.

-- 
Cyril Hrubis
chrubis@suse.cz

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

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [LTP] [PATCH 4/7] syscalls/kill05: Use any two unprivileged users
@ 2021-09-06 13:05                     ` Cyril Hrubis
  2021-09-06 13:27                         ` Martin Doucha
  0 siblings, 1 reply; 19+ messages in thread
From: Cyril Hrubis @ 2021-09-06 13:05 UTC (permalink / raw)
  To: Martin Doucha; +Cc: ltp

Hi!
> But setting a non-existent UID/GID on a running process is different
> because the kernel might try to look up some additional data in the
> user/group database and block the credentials change if the lookup fails.

No it will not, kernel does not care and never cared about system users
and groups, that's purely userspace concept. The login(1) process that
sets up all the process IDS as well as shell variables.

-- 
Cyril Hrubis
chrubis@suse.cz

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

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [LTP] [PATCH 4/7] syscalls/kill05: Use any two unprivileged users
@ 2021-09-06 13:27                         ` Martin Doucha
  0 siblings, 0 replies; 19+ messages in thread
From: Martin Doucha @ 2021-09-06 13:27 UTC (permalink / raw)
  To: Cyril Hrubis; +Cc: ltp

On 06. 09. 21 15:05, Cyril Hrubis wrote:
> Hi!
>> But setting a non-existent UID/GID on a running process is different
>> because the kernel might try to look up some additional data in the
>> user/group database and block the credentials change if the lookup fails.
> 
> No it will not, kernel does not care and never cared about system users
> and groups, that's purely userspace concept. The login(1) process that
> sets up all the process IDS as well as shell variables.

The man pages give no guarantee that it'll stay that way. I'd rather
play it safe with process credentials. From test setup perspective, it's
still less work than the old lookup of specific usernames.

-- 
Martin Doucha   mdoucha@suse.cz
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

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

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2021-09-06 13:27 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-03 15:48 [LTP] [PATCH 0/7] UID/GID lookup fixes Martin Doucha
2021-09-03 15:48 ` [LTP] [PATCH 1/7] chmod05, fchmod05: Use free GID instead of "bin" group Martin Doucha
2021-09-06 12:20   ` Cyril Hrubis
2021-09-06 12:20     ` Cyril Hrubis
2021-09-03 15:48 ` [LTP] [PATCH 2/7] Simplify syscalls/mkdir02 Martin Doucha
2021-09-06 12:20   ` Cyril Hrubis
2021-09-06 12:20     ` Cyril Hrubis
2021-09-03 15:48 ` [LTP] [PATCH 3/7] Add user/group ID lookup helper functions Martin Doucha
2021-09-06 12:21   ` Cyril Hrubis
2021-09-06 12:21     ` Cyril Hrubis
2021-09-06 12:40       ` Martin Doucha
2021-09-06 12:40         ` Martin Doucha
2021-09-03 15:48 ` [LTP] [PATCH 4/7] syscalls/kill05: Use any two unprivileged users Martin Doucha
2021-09-06 12:28   ` Cyril Hrubis
2021-09-06 12:28     ` Cyril Hrubis
2021-09-06 12:47       ` Martin Doucha
2021-09-06 12:47         ` Martin Doucha
2021-09-06 12:50           ` Cyril Hrubis
2021-09-06 12:50             ` Cyril Hrubis
2021-09-06 13:01               ` Martin Doucha
2021-09-06 13:01                 ` Martin Doucha
2021-09-06 13:05                   ` Cyril Hrubis
2021-09-06 13:05                     ` Cyril Hrubis
2021-09-06 13:27                       ` Martin Doucha
2021-09-06 13:27                         ` Martin Doucha
2021-09-06 13:02               ` Cyril Hrubis
2021-09-06 13:02                 ` Cyril Hrubis
2021-09-03 15:48 ` [LTP] [PATCH 5/7] syscalls/mkdir04: Simplify test and use any existing users Martin Doucha
2021-09-03 15:48 ` [LTP] [PATCH 6/7] syscalls/setregid02-04: Simplify GID handling Martin Doucha
2021-09-03 15:48 ` [LTP] [PATCH 7/7] syscalls/setregid02-04: Eliminate named group lookups Martin Doucha

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.