summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-09-14 23:19:38 +0200
committerLennart Poettering <lennart@poettering.net>2021-09-15 16:19:45 +0200
commit13239c86e01b4d96ca08b321ffc01a30905d46ce (patch)
treef8fecf4dbbe1b574c0a9171f2bed733d90a2e8ba /src/basic
parentbe991d7678c35aa037ef79672c0c70781eebed9c (diff)
downloadsystemd-13239c86e01b4d96ca08b321ffc01a30905d46ce.tar.gz
sysctl-util: rework sysctl_write() to wrap write_string_file()
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.
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/sysctl-util.c16
1 files changed, 5 insertions, 11 deletions
diff --git a/src/basic/sysctl-util.c b/src/basic/sysctl-util.c
index 8913e6ff85..60eec3dfec 100644
--- a/src/basic/sysctl-util.c
+++ b/src/basic/sysctl-util.c
@@ -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, ...) {