richardphibel / rpms / systemd

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