21255d
From 87c22d3bb794118d25bc138108fd5bdd607365ef Mon Sep 17 00:00:00 2001
21255d
From: Lennart Poettering <lennart@poettering.net>
21255d
Date: Mon, 1 Jun 2020 17:16:46 +0200
21255d
Subject: [PATCH] user-util: be stricter in parse_uid()
21255d
21255d
Let's refuse "+" and "-" prefixed UIDs. Let's refuse whitespace-prefixed
21255d
UIDS, Let's refuse zero-prefixed UIDs. Let's be safe than sorry.
21255d
21255d
(cherry picked from commit f5979b63cc305ba217dfd174b1bf0583bcf75a73)
21255d
21255d
Related: #1848373
21255d
---
21255d
 src/basic/user-util.c     | 10 +++++++++-
21255d
 src/test/test-user-util.c | 26 +++++++++++++++++++++++---
21255d
 2 files changed, 32 insertions(+), 4 deletions(-)
21255d
21255d
diff --git a/src/basic/user-util.c b/src/basic/user-util.c
21255d
index 10eeb256cd..40f4e45db6 100644
21255d
--- a/src/basic/user-util.c
21255d
+++ b/src/basic/user-util.c
21255d
@@ -49,7 +49,15 @@ int parse_uid(const char *s, uid_t *ret) {
21255d
         assert(s);
21255d
 
21255d
         assert_cc(sizeof(uid_t) == sizeof(uint32_t));
21255d
-        r = safe_atou32_full(s, 10, &uid);
21255d
+
21255d
+        /* We are very strict when parsing UIDs, and prohibit +/- as prefix, leading zero as prefix, and
21255d
+         * whitespace. We do this, since this call is often used in a context where we parse things as UID
21255d
+         * first, and if that doesn't work we fall back to NSS. Thus we really want to make sure that UIDs
21255d
+         * are parsed as UIDs only if they really really look like UIDs. */
21255d
+        r = safe_atou32_full(s, 10
21255d
+                             | SAFE_ATO_REFUSE_PLUS_MINUS
21255d
+                             | SAFE_ATO_REFUSE_LEADING_ZERO
21255d
+                             | SAFE_ATO_REFUSE_LEADING_WHITESPACE, &uid);
21255d
         if (r < 0)
21255d
                 return r;
21255d
 
21255d
diff --git a/src/test/test-user-util.c b/src/test/test-user-util.c
21255d
index 8bf3dcd567..99203f7e48 100644
21255d
--- a/src/test/test-user-util.c
21255d
+++ b/src/test/test-user-util.c
21255d
@@ -52,13 +52,33 @@ static void test_parse_uid(void) {
21255d
         assert_se(r == -EINVAL);
21255d
         assert_se(uid == 100);
21255d
 
21255d
+        r = parse_uid("+1234", &uid);
21255d
+        assert_se(r == -EINVAL);
21255d
+        assert_se(uid == 100);
21255d
+
21255d
+        r = parse_uid("-1234", &uid);
21255d
+        assert_se(r == -EINVAL);
21255d
+        assert_se(uid == 100);
21255d
+
21255d
+        r = parse_uid(" 1234", &uid);
21255d
+        assert_se(r == -EINVAL);
21255d
+        assert_se(uid == 100);
21255d
+
21255d
         r = parse_uid("01234", &uid);
21255d
-        assert_se(r == 0);
21255d
-        assert_se(uid == 1234);
21255d
+        assert_se(r == -EINVAL);
21255d
+        assert_se(uid == 100);
21255d
+
21255d
+        r = parse_uid("-0", &uid);
21255d
+        assert_se(r == -EINVAL);
21255d
+        assert_se(uid == 100);
21255d
+
21255d
+        r = parse_uid("+0", &uid);
21255d
+        assert_se(r == -EINVAL);
21255d
+        assert_se(uid == 100);
21255d
 
21255d
         r = parse_uid("asdsdas", &uid);
21255d
         assert_se(r == -EINVAL);
21255d
-        assert_se(uid == 1234);
21255d
+        assert_se(uid == 100);
21255d
 }
21255d
 
21255d
 static void test_uid_ptr(void) {