An fd opened O_PATH cannot be read, and an O_WRONLY fd cannot serve as
a backing file for a virtual disk image. Reject both at the bind-volume
entry point with -EBADF instead of letting the request proceed to QMP
where QEMU's file backend would fail to read from the fd. The
ReplaceStorage entry point grew the same checks in parallel.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
if (r < 0)
return r;
+ int oflags = fcntl(owned_fd, F_GETFL);
+ if (oflags < 0)
+ return -errno;
+ if (FLAGS_SET(oflags, O_PATH))
+ return -EBADF;
+ if ((oflags & O_ACCMODE_STRICT) == O_WRONLY)
+ return -EBADF;
+
_cleanup_(drive_info_unrefp) DriveInfo *d = drive_info_new();
if (!d)
return -ENOMEM;
if (!d->id || !d->disk_driver || !d->format || !d->path)
return -ENOMEM;
- int oflags = fcntl(owned_fd, F_GETFL);
- if (oflags < 0)
- return -errno;
-
d->disk_type = dt;
d->fd = TAKE_FD(owned_fd);
if (S_ISBLK(st.st_mode))