803fb7
From 38d00b8a0453d38aecb725342ddd89a7c3dcb134 Mon Sep 17 00:00:00 2001
803fb7
From: Michal Sekletar <msekleta@redhat.com>
803fb7
Date: Fri, 18 Nov 2016 14:00:57 +0100
803fb7
Subject: [PATCH] load-fragment: fix parsing values in bytes and prevent
803fb7
 returning -ERANGE incorrectly
803fb7
803fb7
We didn't port our code base to use uint64_t instead of off_t as
803fb7
upstream did. RLIMIT_INIFINITY is -1ULL and if we cast to off_t (64 bit
803fb7
signed int on arches we support) then we get -1 and that is always
803fb7
smaller than correct value returned by parse_size().
803fb7
803fb7
To make code changes as minimal as possible (i.e. not port everything
803fb7
to uint64_t) let's cast off_t to uint64_t and not the other way
803fb7
around.
803fb7
803fb7
RHEL-only
803fb7
803fb7
Resolves: #1396277
803fb7
---
803fb7
 src/core/load-fragment.c  | 2 +-
803fb7
 src/test/test-unit-file.c | 4 ++++
803fb7
 2 files changed, 5 insertions(+), 1 deletion(-)
803fb7
803fb7
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
803fb7
index 2f6209e05..83b6e7efc 100644
803fb7
--- a/src/core/load-fragment.c
803fb7
+++ b/src/core/load-fragment.c
803fb7
@@ -1105,7 +1105,7 @@ static int rlim_parse_size(const char *val, rlim_t *res) {
803fb7
                 off_t u;
803fb7
 
803fb7
                 r = parse_size(val, 1024, &u);
803fb7
-                if (r >= 0 && u >= (off_t) RLIM_INFINITY)
803fb7
+                if (r >= 0 && (uint64_t) u >= RLIM_INFINITY)
803fb7
                         r = -ERANGE;
803fb7
                 if (r == 0)
803fb7
                         *res = (rlim_t) u;
803fb7
diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c
803fb7
index 8acf071ff..038430505 100644
803fb7
--- a/src/test/test-unit-file.c
803fb7
+++ b/src/test/test-unit-file.c
803fb7
@@ -554,6 +554,10 @@ static void test_config_parse_rlimit(void) {
803fb7
         assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 55);
803fb7
         assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max);
803fb7
 
803fb7
+        assert_se(config_parse_bytes_limit(NULL, "fake", 1, "section", 1, "LimitSTACK", RLIMIT_STACK, "55", rl, NULL) >= 0);
803fb7
+        assert_se(rl[RLIMIT_STACK]);
803fb7
+        assert_se(rl[RLIMIT_STACK]->rlim_cur == 55);
803fb7
+        assert_se(rl[RLIMIT_STACK]->rlim_cur == rl[RLIMIT_STACK]->rlim_max);
803fb7
 
803fb7
         assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "55:66", rl, NULL) >= 0);
803fb7
         assert_se(rl[RLIMIT_NOFILE]);