Skip to content

Commit

Permalink
Merge tag 'block-pull-request' of https://gitlab.com/stefanha/qemu in…
Browse files Browse the repository at this point in the history
…to staging

Pull request

Fix for an fd leak in the blkio block driver.

# -----BEGIN PGP SIGNATURE-----
#
# iQEzBAABCAAdFiEEhpWov9P5fNqsNXdanKSrs4Grc8gFAmTLzf0ACgkQnKSrs4Gr
# c8hoGQf+KjsuChyk8/aoDP4MMkNB1/X3nsazCd3GY3uE+DRK8ieiRJeT6chMIey/
# sK3v/drkDmdjj30qbXGxjLVa5SNsP9N6pVoo8fnFJN7LmGBE/JLEYUYVNpHAKEzb
# N7mgDBcTHZWKGwZsh109X5l3Cr6HR484m3qKI/49qlVuWJmp8/lDUbFJbp96I6g9
# ki9W0itwOrdtebYyUDml8eE/yLOxOTWx5Q7Q+qwSiEUNCwyd7yOS1QHQbnCgKw3m
# c0Qzch2Z3dT61YbMrF6j0H7M1dXXcbNFdYVeMHYYJRkeN+bz4fWcUC4HkrL6YWf5
# GLIj5irTSnae4TevlYVZT+72v99QQQ==
# =pQ96
# -----END PGP SIGNATURE-----
# gpg: Signature made Thu 03 Aug 2023 08:55:41 AM PDT
# gpg:                using RSA key 8695A8BFD3F97CDAAC35775A9CA4ABB381AB73C8
# gpg: Good signature from "Stefan Hajnoczi <stefanha@redhat.com>" [full]
# gpg:                 aka "Stefan Hajnoczi <stefanha@gmail.com>" [full]

* tag 'block-pull-request' of https://gitlab.com/stefanha/qemu:
  block/blkio: add more comments on the fd passing handling
  block/blkio: close the fd when blkio_connect() fails

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
  • Loading branch information
rth7680 committed Aug 3, 2023
2 parents c167c80 + 9b06d0d commit 2e6a56f
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions block/blkio.c
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ static int blkio_virtio_blk_connect(BlockDriverState *bs, QDict *options,
const char *path = qdict_get_try_str(options, "path");
BDRVBlkioState *s = bs->opaque;
bool fd_supported = false;
int fd, ret;
int fd = -1, ret;

if (!path) {
error_setg(errp, "missing 'path' option");
Expand Down Expand Up @@ -713,12 +713,19 @@ static int blkio_virtio_blk_connect(BlockDriverState *bs, QDict *options,
*/
fd = qemu_open(path, O_RDWR, NULL);
if (fd < 0) {
/*
* qemu_open() can fail if the user specifies a path that is not
* a file or device, for example in the case of Unix Domain Socket
* for the virtio-blk-vhost-user driver. In such cases let's have
* libblkio open the path directly.
*/
fd_supported = false;
} else {
ret = blkio_set_int(s->blkio, "fd", fd);
if (ret < 0) {
fd_supported = false;
qemu_close(fd);
fd = -1;
}
}
}
Expand All @@ -733,14 +740,21 @@ static int blkio_virtio_blk_connect(BlockDriverState *bs, QDict *options,
}

ret = blkio_connect(s->blkio);
if (ret < 0 && fd >= 0) {
/* Failed to give the FD to libblkio, close it */
qemu_close(fd);
fd = -1;
}

/*
* If the libblkio driver doesn't support the `fd` property, blkio_connect()
* will fail with -EINVAL. So let's try calling blkio_connect() again by
* directly setting `path`.
* Before https://gitlab.com/libblkio/libblkio/-/merge_requests/208
* (libblkio <= v1.3.0), setting the `fd` property is not enough to check
* whether the driver supports the `fd` property or not. In that case,
* blkio_connect() will fail with -EINVAL.
* So let's try calling blkio_connect() again by directly setting `path`
* to cover this scenario.
*/
if (fd_supported && ret == -EINVAL) {
qemu_close(fd);

/*
* We need to clear the `fd` property we set previously by setting
* it to -1.
Expand Down

0 comments on commit 2e6a56f

Please sign in to comment.