]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
growfs: ensure that we operate on a block device before issuing a block ioctl
authorLennart Poettering <lennart@poettering.net>
Thu, 14 Jul 2022 09:31:50 +0000 (11:31 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 14 Jul 2022 09:31:50 +0000 (11:31 +0200)
Similar to the previous commit: let's add extra safety so that we don't
issue ioctls on the wrong type of inode.

src/partition/growfs.c

index d7069b8d0824f04b8eefd3f2da963943280bb725..cd859468123377f736db26dce14c2219583d7d52 100644 (file)
@@ -198,6 +198,7 @@ static int run(int argc, char *argv[]) {
         _cleanup_close_ int mountfd = -1, devfd = -1;
         _cleanup_free_ char *devpath = NULL;
         uint64_t size, newsize;
+        struct stat st;
         dev_t devno;
         int r;
 
@@ -233,10 +234,15 @@ static int run(int argc, char *argv[]) {
         if (r < 0)
                 return log_error_errno(r, "Failed to format device major/minor path: %m");
 
-        devfd = open(devpath, O_RDONLY|O_CLOEXEC);
+        devfd = open(devpath, O_RDONLY|O_CLOEXEC|O_NOCTTY);
         if (devfd < 0)
                 return log_error_errno(errno, "Failed to open \"%s\": %m", devpath);
 
+        if (fstat(devfd, &st) < 0)
+                return log_error_errno(r, "Failed to stat() device %s: %m", devpath);
+        if (!S_ISBLK(st.st_mode))
+                return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Backing device of file system is not a block device, refusing.");
+
         if (ioctl(devfd, BLKGETSIZE64, &size) != 0)
                 return log_error_errno(errno, "Failed to query size of \"%s\": %m", devpath);