user-util: return malloc'ed strings in get_user_creds

get_user_creds would use getpwnam() and then returns strings pointing
into the static buffer. This seems very iffy. Let's duplicate the
strings properly
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-05-21 18:02:05 +02:00
parent 3ecd6b69cf
commit c83ff0e58d
21 changed files with 176 additions and 203 deletions
+1 -2
View File
@@ -41,8 +41,7 @@ TEST_RET(add_acls_for_user) {
ASSERT_OK_ZERO_ERRNO(system(cmd));
if (getuid() == 0 && !userns_has_single_user()) {
const char *nobody = NOBODY_USER_NAME;
r = get_user_creds(&nobody, &uid, NULL, NULL, NULL, 0);
r = get_user_creds(NOBODY_USER_NAME, /* flags= */ 0, NULL, &uid, NULL, NULL, NULL);
if (r < 0)
uid = 0;
} else
+1 -1
View File
@@ -13,7 +13,7 @@ static int run(int argc, char *argv[]) {
test_setup_logging(LOG_INFO);
r = get_user_creds(&name, &uid, NULL, NULL, NULL, 0);
r = get_user_creds(name, /* flags= */ 0, NULL, &uid, NULL, NULL, NULL);
if (r == -ESRCH)
return log_tests_skipped("Failed to resolve user");
if (r < 0)
+4 -5
View File
@@ -287,8 +287,7 @@ TEST(valid_shell) {
}
static void test_get_user_creds_one(const char *id, const char *name, uid_t uid, gid_t gid, const char *home, const char *shell) {
const char *rhome = NULL;
const char *rshell = NULL;
_cleanup_free_ char *rname = NULL, *rhome = NULL, *rshell = NULL;
uid_t ruid = UID_INVALID;
gid_t rgid = GID_INVALID;
int r;
@@ -296,15 +295,15 @@ static void test_get_user_creds_one(const char *id, const char *name, uid_t uid,
log_info("/* %s(\"%s\", \"%s\", "UID_FMT", "GID_FMT", \"%s\", \"%s\") */",
__func__, id, name, uid, gid, home, shell);
r = get_user_creds(&id, &ruid, &rgid, &rhome, &rshell, 0);
r = get_user_creds(id, /* flags= */ 0, &rname, &ruid, &rgid, &rhome, &rshell);
log_info_errno(r, "got \"%s\", "UID_FMT", "GID_FMT", \"%s\", \"%s\": %m",
id, ruid, rgid, strnull(rhome), strnull(rshell));
strnull(rname), ruid, rgid, strnull(rhome), strnull(rshell));
if (!synthesize_nobody() && streq(name, NOBODY_USER_NAME)) {
log_info("(skipping detailed tests because nobody is not synthesized)");
return;
}
ASSERT_OK(r);
ASSERT_STREQ(id, name);
ASSERT_STREQ(rname, name);
ASSERT_EQ(ruid, uid);
ASSERT_EQ(rgid, gid);
ASSERT_TRUE(path_equal(rhome, home));