teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone

Blame SOURCES/0461-parse-util-allow-tweaking-how-to-parse-integers.patch

bd1529
From 1d11e79fefea34b4395043e8e951414c5b7817ba Mon Sep 17 00:00:00 2001
bd1529
From: Lennart Poettering <lennart@poettering.net>
bd1529
Date: Mon, 1 Jun 2020 17:06:19 +0200
bd1529
Subject: [PATCH] parse-util: allow tweaking how to parse integers
bd1529
bd1529
This allows disabling a few alternative ways to decode integers
bd1529
formatted as strings, for safety reasons.
bd1529
bd1529
See: #15991
bd1529
(cherry picked from commit 707e93aff8f358f8a62117e54b857530d6594e4b)
bd1529
bd1529
Related: #1848373
bd1529
---
bd1529
 src/basic/parse-util.c | 65 +++++++++++++++++++++++++++++++++---------
bd1529
 src/basic/parse-util.h |  6 ++++
bd1529
 2 files changed, 58 insertions(+), 13 deletions(-)
bd1529
bd1529
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
bd1529
index 67056c0434..6cc4fc3e57 100644
bd1529
--- a/src/basic/parse-util.c
bd1529
+++ b/src/basic/parse-util.c
bd1529
@@ -383,20 +383,35 @@ int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) {
bd1529
         unsigned long l;
bd1529
 
bd1529
         assert(s);
bd1529
-        assert(base <= 16);
bd1529
+        assert(SAFE_ATO_MASK_FLAGS(base) <= 16);
bd1529
 
bd1529
-        /* strtoul() is happy to parse negative values, and silently
bd1529
-         * converts them to unsigned values without generating an
bd1529
-         * error. We want a clean error, hence let's look for the "-"
bd1529
-         * prefix on our own, and generate an error. But let's do so
bd1529
-         * only after strtoul() validated that the string is clean
bd1529
-         * otherwise, so that we return EINVAL preferably over
bd1529
-         * ERANGE. */
bd1529
+        /* strtoul() is happy to parse negative values, and silently converts them to unsigned values without
bd1529
+         * generating an error. We want a clean error, hence let's look for the "-" prefix on our own, and
bd1529
+         * generate an error. But let's do so only after strtoul() validated that the string is clean
bd1529
+         * otherwise, so that we return EINVAL preferably over ERANGE. */
bd1529
+
bd1529
+        if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_WHITESPACE) &&
bd1529
+            strchr(WHITESPACE, s[0]))
bd1529
+                return -EINVAL;
bd1529
 
bd1529
         s += strspn(s, WHITESPACE);
bd1529
 
bd1529
+        if (FLAGS_SET(base, SAFE_ATO_REFUSE_PLUS_MINUS) &&
bd1529
+            IN_SET(s[0], '+', '-'))
bd1529
+                return -EINVAL; /* Note that we check the "-" prefix again a second time below, but return a
bd1529
+                                 * different error. I.e. if the SAFE_ATO_REFUSE_PLUS_MINUS flag is set we
bd1529
+                                 * blanket refuse +/- prefixed integers, while if it is missing we'll just
bd1529
+                                 * return ERANGE, because the string actually parses correctly, but doesn't
bd1529
+                                 * fit in the return type. */
bd1529
+
bd1529
+        if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_ZERO) &&
bd1529
+            s[0] == '0' && !streq(s, "0"))
bd1529
+                return -EINVAL; /* This is particularly useful to avoid ambiguities between C's octal
bd1529
+                                 * notation and assumed-to-be-decimal integers with a leading zero. */
bd1529
+
bd1529
         errno = 0;
bd1529
-        l = strtoul(s, &x, base);
bd1529
+        l = strtoul(s, &x, SAFE_ATO_MASK_FLAGS(base) /* Let's mask off the flags bits so that only the actual
bd1529
+                                                      * base is left */);
bd1529
         if (errno > 0)
bd1529
                 return -errno;
bd1529
         if (!x || x == s || *x != 0)
bd1529
@@ -438,11 +453,24 @@ int safe_atollu_full(const char *s, unsigned base, long long unsigned *ret_llu)
bd1529
         unsigned long long l;
bd1529
 
bd1529
         assert(s);
bd1529
+        assert(SAFE_ATO_MASK_FLAGS(base) <= 16);
bd1529
+
bd1529
+        if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_WHITESPACE) &&
bd1529
+            strchr(WHITESPACE, s[0]))
bd1529
+                return -EINVAL;
bd1529
 
bd1529
         s += strspn(s, WHITESPACE);
bd1529
 
bd1529
+        if (FLAGS_SET(base, SAFE_ATO_REFUSE_PLUS_MINUS) &&
bd1529
+            IN_SET(s[0], '+', '-'))
bd1529
+                return -EINVAL;
bd1529
+
bd1529
+        if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_ZERO) &&
bd1529
+            s[0] == '0' && s[1] != 0)
bd1529
+                return -EINVAL;
bd1529
+
bd1529
         errno = 0;
bd1529
-        l = strtoull(s, &x, base);
bd1529
+        l = strtoull(s, &x, SAFE_ATO_MASK_FLAGS(base));
bd1529
         if (errno > 0)
bd1529
                 return -errno;
bd1529
         if (!x || x == s || *x != 0)
bd1529
@@ -504,13 +532,24 @@ int safe_atou16_full(const char *s, unsigned base, uint16_t *ret) {
bd1529
         unsigned long l;
bd1529
 
bd1529
         assert(s);
bd1529
-        assert(ret);
bd1529
-        assert(base <= 16);
bd1529
+        assert(SAFE_ATO_MASK_FLAGS(base) <= 16);
bd1529
+
bd1529
+        if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_WHITESPACE) &&
bd1529
+            strchr(WHITESPACE, s[0]))
bd1529
+                return -EINVAL;
bd1529
 
bd1529
         s += strspn(s, WHITESPACE);
bd1529
 
bd1529
+        if (FLAGS_SET(base, SAFE_ATO_REFUSE_PLUS_MINUS) &&
bd1529
+            IN_SET(s[0], '+', '-'))
bd1529
+                return -EINVAL;
bd1529
+
bd1529
+        if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_ZERO) &&
bd1529
+            s[0] == '0' && s[1] != 0)
bd1529
+                return -EINVAL;
bd1529
+
bd1529
         errno = 0;
bd1529
-        l = strtoul(s, &x, base);
bd1529
+        l = strtoul(s, &x, SAFE_ATO_MASK_FLAGS(base));
bd1529
         if (errno > 0)
bd1529
                 return -errno;
bd1529
         if (!x || x == s || *x != 0)
bd1529
diff --git a/src/basic/parse-util.h b/src/basic/parse-util.h
bd1529
index 8a49257050..c6bbc98dff 100644
bd1529
--- a/src/basic/parse-util.h
bd1529
+++ b/src/basic/parse-util.h
bd1529
@@ -26,6 +26,12 @@ int parse_syscall_and_errno(const char *in, char **name, int *error);
bd1529
 #define FORMAT_BYTES_MAX 8
bd1529
 char *format_bytes(char *buf, size_t l, uint64_t t);
bd1529
 
bd1529
+#define SAFE_ATO_REFUSE_PLUS_MINUS (1U << 30)
bd1529
+#define SAFE_ATO_REFUSE_LEADING_ZERO (1U << 29)
bd1529
+#define SAFE_ATO_REFUSE_LEADING_WHITESPACE (1U << 28)
bd1529
+#define SAFE_ATO_ALL_FLAGS (SAFE_ATO_REFUSE_PLUS_MINUS|SAFE_ATO_REFUSE_LEADING_ZERO|SAFE_ATO_REFUSE_LEADING_WHITESPACE)
bd1529
+#define SAFE_ATO_MASK_FLAGS(base) ((base) & ~SAFE_ATO_ALL_FLAGS)
bd1529
+
bd1529
 int safe_atou_full(const char *s, unsigned base, unsigned *ret_u);
bd1529
 
bd1529
 static inline int safe_atou(const char *s, unsigned *ret_u) {