]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
[import] fix stdin/stdout pipe behavior in import/export tar/raw
authorAnita Zhang <the.anitazha@gmail.com>
Tue, 17 Dec 2019 09:08:04 +0000 (01:08 -0800)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 17 Dec 2019 14:14:53 +0000 (23:14 +0900)
The code existed in machinectl to use stdin/stdout if the path for
import/export tar/raw was empty or dash (-) but a check to
`fd_verify_regular` in importd prevented it from working.

Update the check instead to explicitly check for regular file or
pipe/fifo.

Fixes #14346

src/import/importd.c
test/TEST-25-IMPORT/testsuite.sh

index 93d89a3a1f3fa3592dde462aee14cf8da6e4d19c..a75ab6bc070b9dcb2f450f054d5f210ca7819b9b 100644 (file)
@@ -694,6 +694,7 @@ static int method_import_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_
         const char *local, *object;
         Manager *m = userdata;
         TransferType type;
+        struct stat st;
         uint32_t id;
 
         assert(msg);
@@ -717,9 +718,11 @@ static int method_import_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_
         if (r < 0)
                 return r;
 
-        r = fd_verify_regular(fd);
-        if (r < 0)
-                return r;
+        if (fstat(fd, &st) < 0)
+                return -errno;
+
+        if (!S_ISREG(st.st_mode) && !S_ISFIFO(st.st_mode))
+                return -EINVAL;
 
         if (!machine_name_is_valid(local))
                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Local name %s is invalid", local);
@@ -829,6 +832,7 @@ static int method_export_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_
         const char *local, *object, *format;
         Manager *m = userdata;
         TransferType type;
+        struct stat st;
         uint32_t id;
 
         assert(msg);
@@ -855,9 +859,11 @@ static int method_export_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_
         if (!machine_name_is_valid(local))
                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Local name %s is invalid", local);
 
-        r = fd_verify_regular(fd);
-        if (r < 0)
-                return r;
+        if (fstat(fd, &st) < 0)
+                return -errno;
+
+        if (!S_ISREG(st.st_mode) && !S_ISFIFO(st.st_mode))
+                return -EINVAL;
 
         type = streq_ptr(sd_bus_message_get_member(msg), "ExportTar") ? TRANSFER_EXPORT_TAR : TRANSFER_EXPORT_RAW;
 
index 380ba3d82defb4504f469b99a9c9e9e0fe1410e5..d4efd71e0651fe09fec16d6617419d25750bd272 100755 (executable)
@@ -119,6 +119,18 @@ machinectl remove scratch4
 ! test -f /var/lib/machines/scratch4
 ! machinectl image-status scratch4
 
+# Test import-tar hypen/stdin pipe behavior
+cat /var/tmp/scratch.tar.gz | machinectl import-tar - scratch5
+test -d /var/lib/machines/scratch5
+machinectl image-status scratch5
+diff -r /var/tmp/scratch/ /var/lib/machines/scratch5
+
+# Test export-tar hypen/stdout pipe behavior
+mkdir -p /var/tmp/extract
+machinectl export-tar scratch5 - | tar xvf - -C /var/tmp/extract/
+diff -r /var/tmp/scratch/ /var/tmp/extract/
+rm -rf /var/tmp/extract
+
 rm -rf /var/tmp/scratch
 
 echo OK > /testok