]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sysctl-util: rework sysctl_write() to wrap write_string_file()
authorLennart Poettering <lennart@poettering.net>
Tue, 14 Sep 2021 21:19:38 +0000 (23:19 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 15 Sep 2021 14:19:45 +0000 (16:19 +0200)
The sysctl_write_ip_property() call already uses write_string_file(), so
let's do so here, too, to make the codepaths more uniform.

While we are at it, let's also validate the passed path a bit, since we
shouldn't allow sysctls with /../ or such in the name. Hence simplify
the path first, and then check if it is normalized, and refuse if not.

src/basic/sysctl-util.c

index 8913e6ff85bf962afe2e1627ea6b31505b8ae994..60eec3dfec195e22c74b99a7b730ac0fd2dda8fa 100644 (file)
@@ -44,25 +44,19 @@ char *sysctl_normalize(char *s) {
 
 int sysctl_write(const char *property, const char *value) {
         char *p;
-        _cleanup_close_ int fd = -1;
 
         assert(property);
         assert(value);
 
-        log_debug("Setting '%s' to '%.*s'.", property, (int) strcspn(value, NEWLINE), value);
-
         p = strjoina("/proc/sys/", property);
-        fd = open(p, O_WRONLY|O_CLOEXEC);
-        if (fd < 0)
-                return -errno;
 
-        if (!endswith(value, "\n"))
-                value = strjoina(value, "\n");
+        path_simplify(p);
+        if (!path_is_normalized(p))
+                return -EINVAL;
 
-        if (write(fd, value, strlen(value)) < 0)
-                return -errno;
+        log_debug("Setting '%s' to '%s'", p, value);
 
-        return 0;
+        return write_string_file(p, value, WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER);
 }
 
 int sysctl_writef(const char *property, const char *format, ...) {