Re: [PATCH v4 1/2] libceph: zero r_ops slots on request allocation

From: Xiubo Li

Date: Thu Dec 03 2026 - 21:37:57 EST


Hi Daolong

Thanks for the patch.

Reproduced the double free on the base tree (KASAN, init_on_alloc=0): an
early ceph_osdc_new_request() failure released a slot still holding a
stale SPARSE_READ op and freed its sparse_ext again. With this patch the
same test is clean.

This deserves

Fixes: f628d7999727 ("libceph: add sparse read support to OSD client")
Cc: stable@xxxxxxxxxxxxxxx

Reviewed-by: Xiubo Li <xiubo.li@xxxxxxxxx>
Tested-by: Xiubo Li <xiubo.li@xxxxxxxxx>

BRs
Xiubo

Daolong Zhang <dlzhangak@xxxxxxxxxxxxxx> 于2026年12月3日周四 04:19写道:
>
> ceph_osdc_alloc_request() allocates requests from req_mempool,
> ceph_osd_request_cache, or kmalloc_flex() without zeroing the trailing
> r_ops[] flexible array, relying on callers to initialize every op slot
> via osd_req_op_init().
>
> However, ceph_osdc_release_request() iterates over all r_num_ops slots
> and calls osd_req_op_data_release() on each one. If a multi-op
> ceph_osdc_new_request() fails in calc_layout() or
> __ceph_osdc_alloc_messages() (or a caller drops the request before
> initializing all slots), any uninitialized slot can retain stale
> contents from slab reuse or uninitialized heap memory. In particular, a
> slot reused from a prior sparse read can still hold
> CEPH_OSD_OP_SPARSE_READ and its already-freed extent.sparse_ext pointer,
> leading to a double-free.
>
> Zero req->r_ops in ceph_osdc_alloc_request() right after setting
> req->r_num_ops so every op slot starts with opcode 0 and is safe to
> release on any early error path. Also clear op->extent.sparse_ext after
> freeing it in osd_req_op_data_release().
>
> Signed-off-by: Daolong Zhang <dlzhangak@xxxxxxxxxxxxxx>
> ---
> net/ceph/osd_client.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/net/ceph/osd_client.c b/net/ceph/osd_client.c
> index 22a793a4bdc8..b01edcb72c5a 100644
> --- a/net/ceph/osd_client.c
> +++ b/net/ceph/osd_client.c
> @@ -384,6 +384,7 @@ static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
> case CEPH_OSD_OP_WRITE:
> case CEPH_OSD_OP_WRITEFULL:
> kfree(op->extent.sparse_ext);
> + op->extent.sparse_ext = NULL;
> ceph_osd_data_release(&op->extent.osd_data);
> break;
> case CEPH_OSD_OP_CALL:
> @@ -531,7 +532,7 @@ EXPORT_SYMBOL(ceph_osdc_put_request);
>
> static void request_init(struct ceph_osd_request *req)
> {
> - /* req only, each op is zeroed in osd_req_op_init() */
> + /* fixed header only; r_ops is zeroed in ceph_osdc_alloc_request() */
> memset(req, 0, sizeof(*req));
>
> kref_init(&req->r_kref);
> @@ -567,6 +568,7 @@ struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
> req->r_osdc = osdc;
> req->r_mempool = use_mempool;
> req->r_num_ops = num_ops;
> + memset(req->r_ops, 0, flex_array_size(req, r_ops, num_ops));
> req->r_snapid = CEPH_NOSNAP;
> req->r_snapc = ceph_get_snap_context(snapc);
>
> --
> 2.48.1
>
>