Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp633813pxv; Thu, 15 Jul 2021 12:03:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwnzXcvGcOqMhBOc23gr/L31VPsxlNPAYtyxU8jc7qXhY3VwBJ5bFh1VqB9O7uXev5p5b6y X-Received: by 2002:a17:907:76f0:: with SMTP id kg16mr7026546ejc.309.1626375817597; Thu, 15 Jul 2021 12:03:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626375817; cv=none; d=google.com; s=arc-20160816; b=c1ui+HXfT21J1nGdxcTHJWA9qR0LoYL6lfxMU4lP3r55XKdM631A7QHyfAGVF3rhrN j7bjrB7dKxgtgLni7Ea4ach5KWWlEEB/wyMm0RbWopAEj92V9o/z40iFYI5bN94T8TtZ oL0zc3MOPmUKtONTtsArNOpO7ncK4hIYzAluSguOfyw2l/0+pRosf/2XQBQEcKkhbS0G YRk/F0aJ3AOZhB4+3PUP3vYp9rhG81qtXBlTVJ5mn4zPsC+mroH2NKXk7tEGmVcINrdQ R0AANoPAnkxqnxWtPzMgOoUo3MJ72CjE6Keecrx+QEeGiCa3vtLapTvaydo3oC3qXNoi n8qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=NIFKdklOVGzx9kPqIOX0HIKeFAoLwEOxIjL442KqDYU=; b=myNve2TGDojd+aGRjXe7OHP05oS4ESvu1kyPpBoSBbDI34MXO5z4bbQ2/YmoGXZb4M 2447un4yL+V1QZ1PPA0/ZlmPJX7/SbSH/BKxRViGa+Plxi7eM1dEMn6AIaw2hRsvn194 wRjTsju6PZek4PrunfONmlC6nEoO9djV3eWttDJpLkQM5bplsmo/r9OqTEQoMOcECGOk rZ6+BqDHOyYpR/8dlLUy199A2qYTsHEELL7Wp0NVLHGcJ2/3ziyuRh2AxrelTWPXc7RY D8qrm3YeUOp1TT+t/aasiEhN4OJSfVqLralRUxQoq1j5Uip5AUVTTTg6sTopMl31TYWe DSEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ELwAb0V6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dy9si8667673edb.114.2021.07.15.12.03.14; Thu, 15 Jul 2021 12:03:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ELwAb0V6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242411AbhGOTDl (ORCPT + 99 others); Thu, 15 Jul 2021 15:03:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:55914 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240454AbhGOSx7 (ORCPT ); Thu, 15 Jul 2021 14:53:59 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6E763613CF; Thu, 15 Jul 2021 18:51:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626375064; bh=mzIhBdU7ALCjhBTSqRWH2HaTlHDvIODYITh1t62usKQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ELwAb0V6K9z2yTi3y3fn1fZNoJ3xrMZwpe3j3OoXCXILjY44bEOEzp8A7kVX4p9SV JNU7KwiZK9a4gVYwdGsbFJf7ElSRFn+xCjdcKCewFYCUGHVqVa/la52ENwH6lEqHhp zkh8Dw/LWI8ULHDNPfGmQmguf2jmZridBnCadVFA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , yangerkun , "Matthew Wilcox (Oracle)" , Pavel Begunkov , Jens Axboe , Hanjun Guo Subject: [PATCH 5.10 142/215] io_uring: Convert personality_idr to XArray Date: Thu, 15 Jul 2021 20:38:34 +0200 Message-Id: <20210715182624.702615495@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210715182558.381078833@linuxfoundation.org> References: <20210715182558.381078833@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Matthew Wilcox (Oracle)" commit 61cf93700fe6359552848ed5e3becba6cd760efa upstream. You can't call idr_remove() from within a idr_for_each() callback, but you can call xa_erase() from an xa_for_each() loop, so switch the entire personality_idr from the IDR to the XArray. This manifests as a use-after-free as idr_for_each() attempts to walk the rest of the node after removing the last entry from it. Fixes: 071698e13ac6 ("io_uring: allow registering credentials") Cc: stable@vger.kernel.org # 5.6+ Reported-by: yangerkun Signed-off-by: Matthew Wilcox (Oracle) [Pavel: rebased (creds load was moved into io_init_req())] Signed-off-by: Pavel Begunkov Link: https://lore.kernel.org/r/7ccff36e1375f2b0ebf73d957f037b43becc0dde.1615212806.git.asml.silence@gmail.com Signed-off-by: Jens Axboe Signed-off-by: Hanjun Guo Signed-off-by: Greg Kroah-Hartman --- fs/io_uring.c | 59 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 29 deletions(-) --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -346,7 +346,8 @@ struct io_ring_ctx { struct idr io_buffer_idr; - struct idr personality_idr; + struct xarray personalities; + u32 pers_next; struct { unsigned cached_cq_tail; @@ -1212,7 +1213,7 @@ static struct io_ring_ctx *io_ring_ctx_a init_completion(&ctx->ref_comp); init_completion(&ctx->sq_thread_comp); idr_init(&ctx->io_buffer_idr); - idr_init(&ctx->personality_idr); + xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); mutex_init(&ctx->uring_lock); init_waitqueue_head(&ctx->wait); spin_lock_init(&ctx->completion_lock); @@ -6629,7 +6630,7 @@ static int io_init_req(struct io_ring_ct if (id) { struct io_identity *iod; - iod = idr_find(&ctx->personality_idr, id); + iod = xa_load(&ctx->personalities, id); if (unlikely(!iod)) return -EINVAL; refcount_inc(&iod->count); @@ -8445,7 +8446,6 @@ static void io_ring_ctx_free(struct io_r io_sqe_files_unregister(ctx); io_eventfd_unregister(ctx); io_destroy_buffers(ctx); - idr_destroy(&ctx->personality_idr); #if defined(CONFIG_UNIX) if (ctx->ring_sock) { @@ -8509,7 +8509,7 @@ static int io_unregister_personality(str { struct io_identity *iod; - iod = idr_remove(&ctx->personality_idr, id); + iod = xa_erase(&ctx->personalities, id); if (iod) { put_cred(iod->creds); if (refcount_dec_and_test(&iod->count)) @@ -8520,14 +8520,6 @@ static int io_unregister_personality(str return -EINVAL; } -static int io_remove_personalities(int id, void *p, void *data) -{ - struct io_ring_ctx *ctx = data; - - io_unregister_personality(ctx, id); - return 0; -} - static void io_ring_exit_work(struct work_struct *work) { struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, @@ -8554,6 +8546,9 @@ static bool io_cancel_ctx_cb(struct io_w static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) { + unsigned long index; + struct io_identify *iod; + mutex_lock(&ctx->uring_lock); percpu_ref_kill(&ctx->refs); /* if force is set, the ring is going away. always drop after that */ @@ -8574,7 +8569,8 @@ static void io_ring_ctx_wait_and_kill(st /* if we failed setting up the ctx, we might not have any rings */ io_iopoll_try_reap_events(ctx); - idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); + xa_for_each(&ctx->personalities, index, iod) + io_unregister_personality(ctx, index); /* * Do this upfront, so we won't have a grace period where the ring @@ -9137,11 +9133,10 @@ out_fput: } #ifdef CONFIG_PROC_FS -static int io_uring_show_cred(int id, void *p, void *data) +static int io_uring_show_cred(struct seq_file *m, unsigned int id, + const struct io_identity *iod) { - struct io_identity *iod = p; const struct cred *cred = iod->creds; - struct seq_file *m = data; struct user_namespace *uns = seq_user_ns(m); struct group_info *gi; kernel_cap_t cap; @@ -9209,9 +9204,13 @@ static void __io_uring_show_fdinfo(struc seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, (unsigned int) buf->len); } - if (has_lock && !idr_is_empty(&ctx->personality_idr)) { + if (has_lock && !xa_empty(&ctx->personalities)) { + unsigned long index; + const struct io_identity *iod; + seq_printf(m, "Personalities:\n"); - idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); + xa_for_each(&ctx->personalities, index, iod) + io_uring_show_cred(m, index, iod); } seq_printf(m, "PollList:\n"); spin_lock_irq(&ctx->completion_lock); @@ -9597,21 +9596,23 @@ out: static int io_register_personality(struct io_ring_ctx *ctx) { - struct io_identity *id; + struct io_identity *iod; + u32 id; int ret; - id = kmalloc(sizeof(*id), GFP_KERNEL); - if (unlikely(!id)) + iod = kmalloc(sizeof(*iod), GFP_KERNEL); + if (unlikely(!iod)) return -ENOMEM; - io_init_identity(id); - id->creds = get_current_cred(); + io_init_identity(iod); + iod->creds = get_current_cred(); - ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL); - if (ret < 0) { - put_cred(id->creds); - kfree(id); - } + ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)iod, + XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); + if (!ret) + return id; + put_cred(iod->creds); + kfree(iod); return ret; }