From: Rahul Sandhu <nvraxn@gmail.com>
To: stephen.smalley.work@gmail.com
Cc: nvraxn@gmail.com, selinux@vger.kernel.org
Subject: [PATCH v2] genhomedircon: cleanup parsing of uid config values
Date: Wed, 22 Oct 2025 01:01:05 +0100 [thread overview]
Message-ID: <20251022000105.54945-1-nvraxn@gmail.com> (raw)
In-Reply-To: <CAEjxPJ7=xQBqEp+TWs8GEr_Oi_-t3MaW9cUh0TE8i1F7qWZ0jg@mail.gmail.com>
Parsing KV files with a separator of similar format is fairly similar,
so we may as well add a helper function to make it easier to read.
Signed-off-by: Rahul Sandhu <nvraxn@gmail.com>
---
libsemanage/src/genhomedircon.c | 82 +++++++++++++--------------------
1 file changed, 33 insertions(+), 49 deletions(-)
v2: rename path to something more sensible (afterall, we are parsing a
UID!) and move the free to later, just before both return paths to
not dereference it when checking whether we actually parsed a valid
number or not.
diff --git a/libsemanage/src/genhomedircon.c b/libsemanage/src/genhomedircon.c
index 34056562..bb840856 100644
--- a/libsemanage/src/genhomedircon.c
+++ b/libsemanage/src/genhomedircon.c
@@ -308,6 +308,30 @@ done:
return retval;
}
+static int parse_uid_config(const char *file, const char *key, const char *sep,
+ uid_t fallback, uid_t *out)
+{
+ char *uid_str = semanage_findval(file, key, sep);
+ if (!uid_str || !*uid_str) {
+ free(uid_str);
+ *out = fallback;
+ return 0;
+ }
+
+ char *endptr;
+ const unsigned long val = strtoul(uid_str, &endptr, 0);
+
+ if (endptr != uid_str && *endptr == '\0') {
+ *out = (uid_t)val;
+ free(uid_str);
+ return 0;
+ }
+
+ free(uid_str);
+ *out = fallback;
+ return -1;
+}
+
static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s)
{
semanage_list_t *homedir_list = NULL;
@@ -315,7 +339,6 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s)
fc_match_handle_t hand;
char *path = NULL;
uid_t temp, minuid = 500, maxuid = 60000;
- int minuid_set = 0;
struct passwd *pwbuf;
struct stat buf;
@@ -362,56 +385,17 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s)
"Conversion failed for key " key ", is its value a number?" \
" Falling back to default value of `%s`.", #val);
- path = semanage_findval(PATH_ETC_LOGIN_DEFS, "UID_MIN", NULL);
- if (path && *path) {
- char *endptr;
- const unsigned long val = strtoul(path, &endptr, 0);
- if (endptr != path && *endptr == '\0') {
- minuid = (uid_t)val;
- minuid_set = 1;
- } else {
- /* we were provided an invalid value, use defaults. */
- genhomedircon_warn_conv_fail("UID_MIN", FALLBACK_MINUID);
- minuid = FALLBACK_MINUID;
- minuid_set = 1;
- }
- }
- free(path);
- path = NULL;
+ if (parse_uid_config(PATH_ETC_LOGIN_DEFS, "UID_MIN", NULL, FALLBACK_MINUID, &minuid) < 0)
+ genhomedircon_warn_conv_fail("UID_MIN", FALLBACK_MINUID);
- path = semanage_findval(PATH_ETC_LOGIN_DEFS, "UID_MAX", NULL);
- if (path && *path) {
- char *endptr;
- const unsigned long val = strtoul(path, &endptr, 0);
- if (endptr != path && *endptr == '\0') {
- maxuid = (uid_t)val;
- } else {
- /* we were provided an invalid value, use defaults. */
- genhomedircon_warn_conv_fail("UID_MAX", FALLBACK_MAXUID);
- maxuid = FALLBACK_MAXUID;
- }
- }
- free(path);
- path = NULL;
+ if (parse_uid_config(PATH_ETC_LOGIN_DEFS, "UID_MAX", NULL, FALLBACK_MAXUID, &maxuid) < 0)
+ genhomedircon_warn_conv_fail("UID_MAX", FALLBACK_MAXUID);
- path = semanage_findval(PATH_ETC_LIBUSER, "LU_UIDNUMBER", "=");
- if (path && *path) {
- char *endptr;
- const unsigned long val = strtoul(path, &endptr, 0);
- if (endptr != path && *endptr == '\0') {
- temp = (uid_t)val;
- } else {
- /* we were provided an invalid value, use defaults. */
- genhomedircon_warn_conv_fail("LU_UIDNUMBER", FALLBACK_LU_UIDNUMBER);
- temp = FALLBACK_LU_UIDNUMBER;
- }
- if (!minuid_set || temp < minuid) {
- minuid = temp;
- minuid_set = 1;
- }
- }
- free(path);
- path = NULL;
+ if (parse_uid_config(PATH_ETC_LIBUSER, "LU_UIDNUMBER", "=", FALLBACK_LU_UIDNUMBER, &temp) < 0)
+ genhomedircon_warn_conv_fail("LU_UIDNUMBER", FALLBACK_LU_UIDNUMBER);
+
+ if (temp < minuid)
+ minuid = temp;
#undef genhomedircon_warn_conv_fail
--
2.51.0
next prev parent reply other threads:[~2025-10-22 0:01 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-21 11:36 [PATCH] genhomedircon: cleanup parsing of uid config values Rahul Sandhu
2025-10-21 13:15 ` Stephen Smalley
2025-10-22 0:01 ` Rahul Sandhu [this message]
2025-10-22 14:51 ` [PATCH v2] " Stephen Smalley
2025-10-24 8:07 ` [PATCH v3] " Rahul Sandhu
2025-10-24 19:29 ` Stephen Smalley
2025-10-26 19:13 ` [PATCH v4] " Rahul Sandhu
2025-10-27 13:10 ` Stephen Smalley
2025-10-27 13:12 ` Stephen Smalley
2025-10-27 16:54 ` Stephen Smalley
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=20251022000105.54945-1-nvraxn@gmail.com \
--to=nvraxn@gmail.com \
--cc=selinux@vger.kernel.org \
--cc=stephen.smalley.work@gmail.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).