[LTP,v2] syscalls/setregid02: Fix retrieving a group of user "nobody"

Submitted by Yuriy Kolerov on Oct. 6, 2015, 1:42 p.m.

Details

Message ID 1444138943-12185-1-git-send-email-yuriy.kolerov@synopsys.com
State New
Headers show

Commit Message

Yuriy Kolerov Oct. 6, 2015, 1:42 p.m.
In setup this test sets UID and GID of the user "nobody". Then
it expects that user "nobody" is in group "nobody" and tries to
retrive a structure for group "nobody" using getgrnam(). However
some Linux distributions use group "nogroup" for user "nobody".
Thus it's necessary to retrieve a group of user "nobody" not by
hardcoded name but by its GID. It's necessary because group
"nobody" may not exist.

I've replaced a variable "nobody" by more generic "ltpgroup".
Then I've created 2 functions get_group_by_name and
get_group_by_gid which are used instead of GET_GID macros.

On my test system I have user "nobody" in group "nogroup". So
I've got this error messages for this test:

    ----------->8----------
    setregid02.c:160: getgrnam("nobody") failed: errno=SUCCESS(0): Success
    setregid02.c:160: Remaining cases broken
    ----------->8----------

Signed-off-by: Yuriy Kolerov <yuriy.kolerov@synopsys.com>
---
 testcases/kernel/syscalls/setregid/setregid02.c | 53 +++++++++++++++----------
 1 file changed, 32 insertions(+), 21 deletions(-)

Comments

Yuriy Kolerov Oct. 6, 2015, 1:45 p.m.
Oh god. Sorry, I've sent this patch to the wrong mailing list. Ignore it please.

Regards,
Yuriy Kolerov


> -----Original Message-----
> From: Yuriy Kolerov [mailto:yuriy.kolerov@synopsys.com]
> Sent: Tuesday, October 06, 2015 4:42 PM
> To: uclibc@uclibc.org
> Cc: chrubis@suse.cz; Vineet Gupta; Alexey Brodkin; Anton Kolesov; Yuriy
> Kolerov
> Subject: [LTP] [PATCH v2] syscalls/setregid02: Fix retrieving a group of user
> "nobody"
> 
> 
> In setup this test sets UID and GID of the user "nobody". Then it expects that
> user "nobody" is in group "nobody" and tries to retrive a structure for group
> "nobody" using getgrnam(). However some Linux distributions use group
> "nogroup" for user "nobody".
> Thus it's necessary to retrieve a group of user "nobody" not by hardcoded
> name but by its GID. It's necessary because group "nobody" may not exist.
> 
> I've replaced a variable "nobody" by more generic "ltpgroup".
> Then I've created 2 functions get_group_by_name and get_group_by_gid
> which are used instead of GET_GID macros.
> 
> On my test system I have user "nobody" in group "nogroup". So I've got this
> error messages for this test:
> 
>     ----------->8----------
>     setregid02.c:160: getgrnam("nobody") failed: errno=SUCCESS(0): Success
>     setregid02.c:160: Remaining cases broken
>     ----------->8----------
> 
> Signed-off-by: Yuriy Kolerov <yuriy.kolerov@synopsys.com>
> ---
>  testcases/kernel/syscalls/setregid/setregid02.c | 53 +++++++++++++++-----
> -----
>  1 file changed, 32 insertions(+), 21 deletions(-)

Patch hide | download patch | download mbox

diff --git a/testcases/kernel/syscalls/setregid/setregid02.c b/testcases/kernel/syscalls/setregid/setregid02.c
index 5d4d044..9b233d5 100644
--- a/testcases/kernel/syscalls/setregid/setregid02.c
+++ b/testcases/kernel/syscalls/setregid/setregid02.c
@@ -40,7 +40,7 @@  static gid_t neg_one = -1;
 
 static struct passwd *ltpuser;
 
-static struct group nobody, root, bin;
+static struct group ltpgroup, root, bin, *junk;
 
 /*
  * The following structure contains all test data.  Each structure in the array
@@ -56,17 +56,17 @@  struct test_data_t {
 	char *test_msg;
 } test_data[] = {
 	{
-	&neg_one, &root.gr_gid, EPERM, &nobody, &nobody,
+	&neg_one, &root.gr_gid, EPERM, &ltpgroup, &ltpgroup,
 		    "After setregid(-1, root),"}, {
-	&neg_one, &bin.gr_gid, EPERM, &nobody, &nobody,
+	&neg_one, &bin.gr_gid, EPERM, &ltpgroup, &ltpgroup,
 		    "After setregid(-1, bin)"}, {
-	&root.gr_gid, &neg_one, EPERM, &nobody, &nobody,
+	&root.gr_gid, &neg_one, EPERM, &ltpgroup, &ltpgroup,
 		    "After setregid(root,-1),"}, {
-	&bin.gr_gid, &neg_one, EPERM, &nobody, &nobody,
+	&bin.gr_gid, &neg_one, EPERM, &ltpgroup, &ltpgroup,
 		    "After setregid(bin, -1),"}, {
-	&root.gr_gid, &bin.gr_gid, EPERM, &nobody, &nobody,
+	&root.gr_gid, &bin.gr_gid, EPERM, &ltpgroup, &ltpgroup,
 		    "After setregid(root, bin)"}, {
-	&bin.gr_gid, &root.gr_gid, EPERM, &nobody, &nobody,
+	&bin.gr_gid, &root.gr_gid, EPERM, &ltpgroup, &ltpgroup,
 		    "After setregid(bin, root),"}
 };
 
@@ -75,6 +75,8 @@  int TST_TOTAL = sizeof(test_data) / sizeof(test_data[0]);
 static void setup(void);
 static void cleanup(void);
 static void gid_verify(struct group *ru, struct group *eu, char *when);
+static struct group get_group_by_name(const char *name);
+static struct group get_group_by_gid(gid_t gid);
 
 int main(int ac, char **av)
 {
@@ -126,8 +128,6 @@  int main(int ac, char **av)
 
 static void setup(void)
 {
-	struct group *junk;
-
 	tst_require_root();
 
 	tst_sig(FORK, DEF_HANDLER, cleanup);
@@ -147,22 +147,33 @@  static void setup(void)
 			 ltpuser->pw_uid);
 	}
 
-#define GET_GID(group)	do {		\
-	junk = getgrnam(#group);	\
-	if (junk == NULL) {		\
-		tst_brkm(TBROK|TERRNO, NULL, "getgrnam(\"%s\") failed", #group); \
-	}				\
-	GID16_CHECK(junk->gr_gid, setregid, NULL); \
-	group = *(junk); \
-} while (0)
-
-	GET_GID(root);
-	GET_GID(nobody);
-	GET_GID(bin);
+	root = get_group_by_name("root");
+	ltpgroup = get_group_by_gid(ltpuser->pw_gid);
+	bin = get_group_by_name("bin");
 
 	TEST_PAUSE;
 }
 
+static struct group get_group_by_name(const char *name)
+{
+	junk = getgrnam(name);
+	if (junk == NULL) {
+		tst_brkm(TBROK|TERRNO, NULL, "getgrnam(\"%s\") failed", name);
+	}
+	GID16_CHECK(junk->gr_gid, setregid, NULL);
+	return *junk;
+}
+
+static struct group get_group_by_gid(gid_t gid)
+{
+	junk = getgrgid(gid);
+	if (junk == NULL) {
+		tst_brkm(TBROK|TERRNO, NULL, "getgrgid(\"%d\") failed", gid);
+	}
+	GID16_CHECK(junk->gr_gid, setregid, NULL);
+	return *junk;
+}
+
 static void cleanup(void)
 {
 }