From: Jonas Pfefferle Date: Wed, 9 Mar 2022 13:26:42 +0000 (+0100) Subject: librbd: readv/writev fix iovecs length computation overflow X-Git-Tag: v18.0.0~1223^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=e50405ef857f487bc1c104bbf3e8859ea099a0c4;p=ceph.git librbd: readv/writev fix iovecs length computation overflow iovec have unsigned length (size_t) and before this patch the total length was computed by adding iovec's length to a signed length variable (ssize_t). While the code checked if the resulting length was negative on overflow, the case where length is positive after overflow was not checked. This patch fixes the overflow check by changing length to unsigned size_t. Additionally, this patch fixes the case where some iovecs have been added to the bufferlist and the aio completion has been blocked, but adding an additional iovec fails because of overflow. This leads to the UserBufferDeleter trying to unblock the completion on destruction of the bufferlist but asserting because the completion was never armed. We avoid this by first computing the total length and checking for overflows and iovcnt before adding them to the bufferlist. Signed-off-by: Jonas Pfefferle --- diff --git a/src/librbd/librbd.cc b/src/librbd/librbd.cc index 4557b2c7558ed..28bb2449d7520 100644 --- a/src/librbd/librbd.cc +++ b/src/librbd/librbd.cc @@ -107,6 +107,40 @@ static auto create_write_raw(librbd::ImageCtx *ictx, const char *buf, deleter(new UserBufferDeleter(ictx->cct, aio_completion)))); } +static int get_iovec_length(const struct iovec *iov, int iovcnt, size_t &len) +{ + len = 0; + + if (iovcnt <= 0) { + return -EINVAL; + } + + for (int i = 0; i < iovcnt; ++i) { + const struct iovec &io = iov[i]; + // check for overflow + if (len + io.iov_len < len) { + return -EINVAL; + } + len += io.iov_len; + } + + return 0; +} + +static bufferlist iovec_to_bufferlist(librbd::ImageCtx *ictx, + const struct iovec *iov, + int iovcnt, + librbd::io::AioCompletion* aio_completion) +{ + bufferlist bl; + for (int i = 0; i < iovcnt; ++i) { + const struct iovec &io = iov[i]; + bl.push_back(create_write_raw(ictx, static_cast(io.iov_base), + io.iov_len, aio_completion)); + } + return bl; +} + CephContext* get_cct(IoCtx &io_ctx) { return reinterpret_cast(io_ctx.cct()); } @@ -6146,30 +6180,16 @@ extern "C" int rbd_aio_writev(rbd_image_t image, const struct iovec *iov, librbd::ImageCtx *ictx = (librbd::ImageCtx *)image; librbd::RBD::AioCompletion *comp = (librbd::RBD::AioCompletion *)c; - // convert the scatter list into a bufferlist - auto aio_completion = get_aio_completion(comp); - ssize_t len = 0; - bufferlist bl; - for (int i = 0; i < iovcnt; ++i) { - const struct iovec &io = iov[i]; - len += io.iov_len; - if (len < 0) { - break; - } - - bl.push_back(create_write_raw(ictx, static_cast(io.iov_base), - io.iov_len, aio_completion)); - } - - int r = 0; - if (iovcnt <= 0 || len < 0) { - r = -EINVAL; - } + size_t len; + int r = get_iovec_length(iov, iovcnt, len); tracepoint(librbd, aio_write_enter, ictx, ictx->name.c_str(), ictx->snap_name.c_str(), ictx->read_only, off, len, NULL, comp->pc); + if (r == 0) { + auto aio_completion = get_aio_completion(comp); + auto bl = iovec_to_bufferlist(ictx, iov, iovcnt, aio_completion); librbd::api::Io<>::aio_write( *ictx, aio_completion, off, len, std::move(bl), 0, true); } @@ -6210,18 +6230,8 @@ extern "C" int rbd_aio_readv(rbd_image_t image, const struct iovec *iov, librbd::ImageCtx *ictx = (librbd::ImageCtx *)image; librbd::RBD::AioCompletion *comp = (librbd::RBD::AioCompletion *)c; - ssize_t len = 0; - for (int i = 0; i < iovcnt; ++i) { - len += iov[i].iov_len; - if (len < 0) { - break; - } - } - - int r = 0; - if (iovcnt == 0 || len < 0) { - r = -EINVAL; - } + size_t len; + int r = get_iovec_length(iov, iovcnt, len); tracepoint(librbd, aio_read_enter, ictx, ictx->name.c_str(), ictx->snap_name.c_str(), ictx->read_only, off, len, NULL, diff --git a/src/test/librbd/test_librbd.cc b/src/test/librbd/test_librbd.cc index 245e192676295..c46f0dcfd3a05 100644 --- a/src/test/librbd/test_librbd.cc +++ b/src/test/librbd/test_librbd.cc @@ -41,6 +41,7 @@ #include #include #include +#include #include "test/librados/test.h" #include "test/librados/test_cxx.h" @@ -2587,8 +2588,10 @@ TEST_F(TestLibRBD, TestScatterGatherIO) ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL)); std::string write_buffer("This is a test"); + // These iovecs should produce a length overflow struct iovec bad_iovs[] = { - {.iov_base = NULL, .iov_len = static_cast(-1)} + {.iov_base = &write_buffer[0], .iov_len = 5}, + {.iov_base = NULL, .iov_len = std::numeric_limits::max()} }; struct iovec write_iovs[] = { {.iov_base = &write_buffer[0], .iov_len = 5}, @@ -2600,7 +2603,7 @@ TEST_F(TestLibRBD, TestScatterGatherIO) rbd_completion_t comp; rbd_aio_create_completion(NULL, NULL, &comp); ASSERT_EQ(-EINVAL, rbd_aio_writev(image, write_iovs, 0, 0, comp)); - ASSERT_EQ(-EINVAL, rbd_aio_writev(image, bad_iovs, 1, 0, comp)); + ASSERT_EQ(-EINVAL, rbd_aio_writev(image, bad_iovs, 2, 0, comp)); ASSERT_EQ(0, rbd_aio_writev(image, write_iovs, sizeof(write_iovs) / sizeof(struct iovec), 1<