]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-fileio: fix bogus error when /proc/cmdline contains newlines
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 13 Dec 2019 11:21:11 +0000 (12:21 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 28 Mar 2020 10:46:47 +0000 (11:46 +0100)
The kernel does not sanitize /proc/cmdline. E.g. when running under qemu, it is
easy to pass a string with newline by mistake. We use read_one_line_file(), so
we would read only the first list of the file, and
write_string_file(WRITE_STRING_FILE_VERIFY_ON_FAILURE) would fail because the
target file is obviously different. Change to a kernel-generated file to avoid
the issue.

v2:
- use /proc/version instead of /proc/uptime for attempted writes, so the test
  test passes even if test_write_string_file_verify() takes more than 10 ms ;]

src/test/test-fileio.c

index 23c7d370d4eb1a86fae3e8e500482631ec412d47..32a00349acb22a976a39d82c22cf2ff06017f77a 100644 (file)
@@ -444,20 +444,20 @@ static void test_write_string_file_verify(void) {
         _cleanup_free_ char *buf = NULL, *buf2 = NULL;
         int r;
 
-        assert_se(read_one_line_file("/proc/cmdline", &buf) >= 0);
+        assert_se(read_one_line_file("/proc/version", &buf) >= 0);
         assert_se(buf2 = strjoin(buf, "\n"));
 
-        r = write_string_file("/proc/cmdline", buf, 0);
+        r = write_string_file("/proc/version", buf, 0);
         assert_se(IN_SET(r, -EACCES, -EIO));
-        r = write_string_file("/proc/cmdline", buf2, 0);
+        r = write_string_file("/proc/version", buf2, 0);
         assert_se(IN_SET(r, -EACCES, -EIO));
 
-        assert_se(write_string_file("/proc/cmdline", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0);
-        assert_se(write_string_file("/proc/cmdline", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0);
+        assert_se(write_string_file("/proc/version", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0);
+        assert_se(write_string_file("/proc/version", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE) == 0);
 
-        r = write_string_file("/proc/cmdline", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE);
+        r = write_string_file("/proc/version", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE);
         assert_se(IN_SET(r, -EACCES, -EIO));
-        assert_se(write_string_file("/proc/cmdline", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE) == 0);
+        assert_se(write_string_file("/proc/version", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE) == 0);
 }
 
 static void test_load_env_file_pairs(void) {
@@ -757,7 +757,7 @@ static void test_read_line3(void) {
         _cleanup_free_ char *line = NULL;
         int r;
 
-        f = fopen("/proc/cmdline", "re");
+        f = fopen("/proc/uptime", "re");
         if (!f && IN_SET(errno, ENOENT, EPERM))
                 return;
         assert_se(f);