]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sysctl-util: modernize sysctl_read() a bit
authorLennart Poettering <lennart@poettering.net>
Wed, 15 Sep 2021 07:20:49 +0000 (09:20 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 15 Sep 2021 14:32:40 +0000 (16:32 +0200)
Let's add similar path validation to sysctl_read() as we already have in
sysctl_write().

Let's also drop the trailing newline from the returned string, like
sysctl_read_ip_property() already does it.

(I checked all users of this, they don't care)

src/basic/sysctl-util.c

index a19f3e2649148536eec1f86fe460b33848c78307..9c81001e69b825b660444c07136ac3d91767e706 100644 (file)
@@ -98,12 +98,23 @@ int sysctl_write_ip_property(int af, const char *ifname, const char *property, c
 
 int sysctl_read(const char *property, char **ret) {
         char *p;
+        int r;
 
         assert(property);
-        assert(ret);
 
         p = strjoina("/proc/sys/", property);
-        return read_full_virtual_file(p, ret, NULL);
+
+        path_simplify(p);
+        if (!path_is_normalized(p)) /* Filter out attempts to write to /proc/sys/../../…, just in case */
+                return -EINVAL;
+
+        r = read_full_virtual_file(p, ret, NULL);
+        if (r < 0)
+                return r;
+        if (ret)
+                delete_trailing_chars(*ret, NEWLINE);
+
+        return r;
 }
 
 int sysctl_read_ip_property(int af, const char *ifname, const char *property, char **ret) {