daandemeyer / rpms / systemd

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