]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sysctl-util: make sysctl_read_ip_property() a wrapper around sysctl_read()
authorLennart Poettering <lennart@poettering.net>
Wed, 15 Sep 2021 07:22:06 +0000 (09:22 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 15 Sep 2021 14:32:40 +0000 (16:32 +0200)
let's do what we did for sysctl_write()/sysctl_write_ip_property() also
for the read paths: i.e. make one a wrapper of the other, and add more
careful input validation.

src/basic/sysctl-util.c

index 9c81001e69b825b660444c07136ac3d91767e706..4e168dd48a16f165813cdbdf70e1b3fc8dc272c1 100644 (file)
@@ -118,24 +118,20 @@ int sysctl_read(const char *property, char **ret) {
 }
 
 int sysctl_read_ip_property(int af, const char *ifname, const char *property, char **ret) {
-        _cleanup_free_ char *value = NULL;
         const char *p;
-        int r;
 
-        assert(IN_SET(af, AF_INET, AF_INET6));
         assert(property);
 
-        p = strjoina("/proc/sys/net/ipv", af == AF_INET ? "4" : "6",
-                     ifname ? "/conf/" : "", strempty(ifname),
-                     property[0] == '/' ? "" : "/", property);
+        if (!IN_SET(af, AF_INET, AF_INET6))
+                return -EAFNOSUPPORT;
 
-        r = read_full_virtual_file(p, &value, NULL);
-        if (r < 0)
-                return r;
+        if (ifname) {
+                if (!ifname_valid_full(ifname, IFNAME_VALID_SPECIAL))
+                        return -EINVAL;
 
-        truncate_nl(value);
-        if (ret)
-                *ret = TAKE_PTR(value);
+                p = strjoina("net/", af_to_ipv4_ipv6(af), "/conf/", ifname, "/", property);
+        } else
+                p = strjoina("net/", af_to_ipv4_ipv6(af), "/", property);
 
-        return r;
+        return sysctl_read(p, ret);
 }