Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9108121pxu; Mon, 28 Dec 2020 06:51:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJyB+vdXpLCscPEkQm1fY/RbimPIE4xDE5yzjCVfigv6YJ1q0mWp6nE7H5GtVA68Ouoe8RcM X-Received: by 2002:a17:906:6448:: with SMTP id l8mr15940618ejn.357.1609167069991; Mon, 28 Dec 2020 06:51:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609167069; cv=none; d=google.com; s=arc-20160816; b=tEMl69C0GIU7aBxsH4y4XyiEfYcbLvr/BkiubBYfq76yyQ70pvutV5n/JILy9oVc/h 7DjzsA+AuiCrueZBa+Obxwj0mwbX/jg86GxKZwe9igAkezJEYpcvlNXP9BHJu3F2tbFZ WCDf+160DtSV22FJY85OSwPMtZkmbfBNAMNOd/mN5AWPwuM9plvN1d7bZPI7cyN4phqH 8qBIXFQgsN4jyTvyxD15cVsFqdVmIjKhZWiftmZB6W3yBVKHdAbmNL9w+ZtKAO4MPqff Kd5vq5xp3leqx/07+Hr43KAWFqtD3HsiKZ7RNOlZlSTxkNbbCNfsLVaN7ur6vhmz/6xj ItPQ== 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=hJI4uJv1FGXNXks3EX5Iiw7DJ1FsoxPdPMe8fsV6yn8=; b=RyUFepOVXJGvN7VPuyptebSPKO4MNHLPvkVZeG3MvGFuBIz38C9Q+uZ3gTg/p1XWvk ZnXCS0lEqoI0w2GCgZUTYKJOGQO8RcrTPFlk9hNFf/e0yMl/RM/d7dvgqAziJACEi2RH OE6EDBpVf9MG6V3Ixb5YfKXrHNOTdmOussETAdtqFsqj0XL4S9qlXyQzd42IChYakg+y lLiT5LiOuL1Fz8cq4drMn+kq4ZdLy++MDkk/O2pjqwq2qVgvOa73Qj6hBYWyC8AC4xZG 5+A+fkIcklgoy4wjT+w+p62iJDlD2D2pF7OEmk5Q8oiUEpiRY/CZcuSUUav6XCKFDXc4 lcAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YIdvop3o; 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 a18si18248917ejb.180.2020.12.28.06.50.46; Mon, 28 Dec 2020 06:51:09 -0800 (PST) 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=YIdvop3o; 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 S2503002AbgL1OYH (ORCPT + 99 others); Mon, 28 Dec 2020 09:24:07 -0500 Received: from mail.kernel.org ([198.145.29.99]:60182 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2502984AbgL1OYF (ORCPT ); Mon, 28 Dec 2020 09:24:05 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 744282245C; Mon, 28 Dec 2020 14:23:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609165404; bh=F0FnHfMERRw5xu6UhLVLDLTxoiecxzuy0pehTAoY8tI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YIdvop3oy8YITbyZGjlKjS7paaDHHJg7S+83nJjLDI1K7tGcLuJS/uOQpkNVaHKfs ZAlW76QzIVun19UOYsTANgxM7XtexL/IFnxKvYGq3sBpz13mZrDk+ToMyDl6VfAjzv ZSbg1sCQHAJusZO8T/uKWev+D5jSvQ/M0lIUwz4k= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+c9937dfb2303a5f18640@syzkaller.appspotmail.com, Pavel Begunkov , Jens Axboe Subject: [PATCH 5.10 520/717] io_uring: fix double io_uring free Date: Mon, 28 Dec 2020 13:48:38 +0100 Message-Id: <20201228125045.874173758@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228125020.963311703@linuxfoundation.org> References: <20201228125020.963311703@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: Pavel Begunkov commit 9faadcc8abe4b83d0263216dc3a6321d5bbd616b upstream. Once we created a file for current context during setup, we should not call io_ring_ctx_wait_and_kill() directly as it'll be done by fput(file) Cc: stable@vger.kernel.org # 5.10 Reported-by: syzbot+c9937dfb2303a5f18640@syzkaller.appspotmail.com Signed-off-by: Pavel Begunkov [axboe: fix unused 'ret' for !CONFIG_UNIX] Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- fs/io_uring.c | 71 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 32 deletions(-) --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -9195,55 +9195,52 @@ static int io_allocate_scq_urings(struct return 0; } +static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) +{ + int ret, fd; + + fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); + if (fd < 0) + return fd; + + ret = io_uring_add_task_file(ctx, file); + if (ret) { + put_unused_fd(fd); + return ret; + } + fd_install(fd, file); + return fd; +} + /* * Allocate an anonymous fd, this is what constitutes the application * visible backing of an io_uring instance. The application mmaps this * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, * we have to tie this fd to a socket for file garbage collection purposes. */ -static int io_uring_get_fd(struct io_ring_ctx *ctx) +static struct file *io_uring_get_file(struct io_ring_ctx *ctx) { struct file *file; +#if defined(CONFIG_UNIX) int ret; - int fd; -#if defined(CONFIG_UNIX) ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, &ctx->ring_sock); if (ret) - return ret; + return ERR_PTR(ret); #endif - ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); - if (ret < 0) - goto err; - fd = ret; - file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, O_RDWR | O_CLOEXEC); - if (IS_ERR(file)) { - put_unused_fd(fd); - ret = PTR_ERR(file); - goto err; - } - #if defined(CONFIG_UNIX) - ctx->ring_sock->file = file; -#endif - ret = io_uring_add_task_file(ctx, file); - if (ret) { - fput(file); - put_unused_fd(fd); - goto err; + if (IS_ERR(file)) { + sock_release(ctx->ring_sock); + ctx->ring_sock = NULL; + } else { + ctx->ring_sock->file = file; } - fd_install(fd, file); - return fd; -err: -#if defined(CONFIG_UNIX) - sock_release(ctx->ring_sock); - ctx->ring_sock = NULL; #endif - return ret; + return file; } static int io_uring_create(unsigned entries, struct io_uring_params *p, @@ -9251,6 +9248,7 @@ static int io_uring_create(unsigned entr { struct user_struct *user = NULL; struct io_ring_ctx *ctx; + struct file *file; bool limit_mem; int ret; @@ -9397,13 +9395,22 @@ static int io_uring_create(unsigned entr goto err; } + file = io_uring_get_file(ctx); + if (IS_ERR(file)) { + ret = PTR_ERR(file); + goto err; + } + /* * Install ring fd as the very last thing, so we don't risk someone * having closed it before we finish setup */ - ret = io_uring_get_fd(ctx); - if (ret < 0) - goto err; + ret = io_uring_install_fd(ctx, file); + if (ret < 0) { + /* fput will clean it up */ + fput(file); + return ret; + } trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); return ret;