All of lore.kernel.org
 help / color / mirror / Atom feed
From: Martin Doucha <mdoucha@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2 1/4] Add tst_get_free_gid() helper function to LTP library
Date: Thu, 26 Aug 2021 13:22:09 +0200	[thread overview]
Message-ID: <20210826112212.26394-1-mdoucha@suse.cz> (raw)

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


             reply	other threads:[~2021-08-26 11:22 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-26 11:22 Martin Doucha [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210826112212.26394-1-mdoucha@suse.cz \
    --to=mdoucha@suse.cz \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.