From: Lennart Poettering Date: Thu, 14 Jul 2022 09:31:50 +0000 (+0200) Subject: growfs: ensure that we operate on a block device before issuing a block ioctl X-Git-Tag: v252-rc1~653^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=12810f3abbc5e3d0d9a54495b79538cb18714180;p=thirdparty%2Fsystemd.git growfs: ensure that we operate on a block device before issuing a block ioctl Similar to the previous commit: let's add extra safety so that we don't issue ioctls on the wrong type of inode. --- diff --git a/src/partition/growfs.c b/src/partition/growfs.c index d7069b8d082..cd859468123 100644 --- a/src/partition/growfs.c +++ b/src/partition/growfs.c @@ -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);