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