ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
923a60
From b53ec8d7dca8eba189c45ae29e4d5ff03e5e5556 Mon Sep 17 00:00:00 2001
923a60
From: Evgeny Vereshchagin <evvers@ya.ru>
923a60
Date: Fri, 27 Nov 2015 08:54:42 +0000
923a60
Subject: [PATCH] core: fix rlimit parsing
923a60
923a60
* refuse limits if soft > hard
923a60
* print an actual value instead of (null)
923a60
923a60
see https://github.com/systemd/systemd/pull/1994#issuecomment-159999123
923a60
923a60
Cherry-picked from: 0316f2aeebde7569d24a93ab788ac4bc1657b11b
923a60
Related: #1351415
923a60
---
923a60
 src/core/load-fragment.c  |  5 ++++-
923a60
 src/test/test-unit-file.c | 21 +++++++++++++++++++++
923a60
 2 files changed, 25 insertions(+), 1 deletion(-)
923a60
923a60
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
923a60
index d307f1c743..2f6209e053 100644
923a60
--- a/src/core/load-fragment.c
923a60
+++ b/src/core/load-fragment.c
923a60
@@ -1160,6 +1160,7 @@ static int parse_rlimit_range(
923a60
                 struct rlimit **rl,
923a60
                 int (*rlim_parser)(const char *, rlim_t *)) {
923a60
 
923a60
+        const char *whole_value = value;
923a60
         rlim_t soft, hard;
923a60
         _cleanup_free_ char *sword = NULL, *hword = NULL;
923a60
         int nwords, r;
923a60
@@ -1175,9 +1176,11 @@ static int parse_rlimit_range(
923a60
         if (r == 0 && nwords == 2)
923a60
                 r = rlim_parser(hword, &hard);
923a60
         if (r < 0) {
923a60
-                log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", value);
923a60
+                log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse resource value, ignoring: %s", whole_value);
923a60
                 return 0;
923a60
         }
923a60
+        if (nwords == 2 && soft > hard)
923a60
+                return log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid resource value ("RLIM_FMT" > "RLIM_FMT"), ignoring: %s", soft, hard, whole_value);
923a60
 
923a60
         if (!*rl) {
923a60
                 *rl = new(struct rlimit, 1);
923a60
diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c
923a60
index 931dfeda88..8acf071ff3 100644
923a60
--- a/src/test/test-unit-file.c
923a60
+++ b/src/test/test-unit-file.c
923a60
@@ -570,6 +570,27 @@ static void test_config_parse_rlimit(void) {
923a60
         assert_se(rl[RLIMIT_NOFILE]->rlim_cur == RLIM_INFINITY);
923a60
         assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max);
923a60
 
923a60
+        assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "10:20:30", rl, NULL) >= 0);
923a60
+        assert_se(rl[RLIMIT_NOFILE]);
923a60
+        assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10);
923a60
+        assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20);
923a60
+
923a60
+        /* Invalid values don't change rl */
923a60
+        assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "wat:wat", rl, NULL) >= 0);
923a60
+        assert_se(rl[RLIMIT_NOFILE]);
923a60
+        assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10);
923a60
+        assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20);
923a60
+
923a60
+        assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "66:wat", rl, NULL) >= 0);
923a60
+        assert_se(rl[RLIMIT_NOFILE]);
923a60
+        assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10);
923a60
+        assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20);
923a60
+
923a60
+        assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "200:100", rl, NULL) >= 0);
923a60
+        assert_se(rl[RLIMIT_NOFILE]);
923a60
+        assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10);
923a60
+        assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20);
923a60
+
923a60
         free(rl[RLIMIT_NOFILE]);
923a60
         assert_se(config_parse_sec_limit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "56", rl, NULL) >= 0);
923a60
         assert_se(rl[RLIMIT_CPU]);