1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-25 06:03:40 +03:00

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.
This commit is contained in:
Lennart Poettering 2022-07-14 11:31:50 +02:00
parent 2e7dd6682b
commit 12810f3abb

View 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);