All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v2 1/4] Add tst_get_free_gid() helper function to LTP library
@ 2021-08-26 11:22 Martin Doucha
  2021-08-26 11:22 ` [LTP] [PATCH v2 2/4] syscalls/creat08: Convert to new API Martin Doucha
                   ` (4 more replies)
  0 siblings, 5 replies; 11+ messages in thread
From: Martin Doucha @ 2021-08-26 11:22 UTC (permalink / raw)
  To: ltp

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---

Changes since v1: New patch

The man page does not say anything about how setgroups() interacts with
setuid()/setgid() so I've decided to use any unassigned gid for the non-member
setgid subtests.

 include/tst_uid.h | 18 ++++++++++++++++++
 lib/tst_uid.c     | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)
 create mode 100644 include/tst_uid.h
 create mode 100644 lib/tst_uid.c

diff --git a/include/tst_uid.h b/include/tst_uid.h
new file mode 100644
index 000000000..7135a9cad
--- /dev/null
+++ b/include/tst_uid.h
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright (c) 2021 Linux Test Project
+ */
+
+#ifndef TST_UID_H_
+#define TST_UID_H_
+
+#include <sys/types.h>
+
+/*
+ * Find unassigned gid. The skip argument can be used to ignore e.g. the main
+ * group of a specific user in case it's not listed in the group file. If you
+ * do not need to skip any specific gid, simply set it to 0.
+ */
+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))
+
+#endif /* TST_UID_H_ */
diff --git a/lib/tst_uid.c b/lib/tst_uid.c
new file mode 100644
index 000000000..a73cafa46
--- /dev/null
+++ b/lib/tst_uid.c
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 Linux Test Project
+ */
+
+#include <sys/types.h>
+#include <grp.h>
+#include <errno.h>
+
+#define TST_NO_DEFAULT_MAIN
+#include "tst_test.h"
+#include "tst_uid.h"
+
+#define MAX_GID 32767
+
+gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip)
+{
+	gid_t ret;
+
+	errno = 0;
+
+	for (ret = 0; ret < MAX_GID; ret++) {
+		if (ret == skip || getgrgid(ret))
+			continue;
+
+		if (errno == 0 || errno == ENOENT || errno == ESRCH)
+			return ret;
+
+		tst_brk_(file, lineno, TBROK|TERRNO, "Group ID lookup failed");
+		return (gid_t)-1;
+	}
+
+	tst_brk_(file, lineno, TBROK, "No free group ID found");
+	return (gid_t)-1;
+}
-- 
2.32.0


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

end of thread, other threads:[~2021-08-26 15:31 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-26 11:22 [LTP] [PATCH v2 1/4] Add tst_get_free_gid() helper function to LTP library Martin Doucha
2021-08-26 11:22 ` [LTP] [PATCH v2 2/4] syscalls/creat08: Convert to new API Martin Doucha
2021-08-26 11:22 ` [LTP] [PATCH v2 3/4] syscalls/open10: " Martin Doucha
2021-08-26 11:22 ` [LTP] [PATCH v2 4/4] Add test for CVE 2018-13405 Martin Doucha
2021-08-26 13:23   ` Petr Vorel
2021-08-26 13:27   ` Petr Vorel
2021-08-26 14:07     ` Martin Doucha
2021-08-26 14:28       ` Cyril Hrubis
2021-08-26 15:31         ` Petr Vorel
2021-08-26 12:02 ` [LTP] [PATCH v2 1/4] Add tst_get_free_gid() helper function to LTP library Petr Vorel
2021-08-26 14:57 ` Cyril Hrubis

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.