Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp828172pxb; Tue, 8 Feb 2022 03:30:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJyA2nVpUuY85jEK4qIfnw0AdlTYoME2eQTgpWPDUSlJOmow/5b08nPEn5uDaU9SgiShUcaQ X-Received: by 2002:a17:907:16a2:: with SMTP id hc34mr3457916ejc.330.1644319830166; Tue, 08 Feb 2022 03:30:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644319830; cv=none; d=google.com; s=arc-20160816; b=sIXUtxvrcuHI/OtTUwX9ntSQHCLrljewuMYTqkW7qPMkip2Fhos8qD9oQBQI24JanB P1YikVQbrd0/n0WdTtuoADh1+8vo8UMuxrgxJgHHB8JU9rCjbCJLGekLkmg4ZK3VK/IW KrkQmam9YKy8op40Kl8uhveOOy2gvHbtN6R8a2Rg/p+ye6ZhVt2qywPLr5LnaTljVSyp CYNFXS8XUTsqcJ1iZX4PSVeG8+KXKJLtB5jfzas+z86ywgUT15a+iUn/SDKTFngttfY5 Rf2KHv+jAP17YA3Iyzm2aXIOofbUOX/MQBSTgF1z+tlpJ260JAiVHZBJj6oF9w93Pnlc D7ww== 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=M4UOyQvOejNQ/oZCRk4BYba7yM6MksYKxlpM1Nhesic=; b=a8fJOpxbbDfmMCmsBKa3UyQIj1oXJdtpvroLjp7Ofehb2eYp9yfb1AkiOnUYA1CAEq I40XcfpgqzOjmbjaD1UvRjrl8gLAyyLrbaukt00DUke8M0pP+2ttvjj1NfB1epg2sjNS YU91p5VE7Fo8J2AXonaa82GaF6BY6uMmGpTIDnXx5nGWttZUgABBSLAyd+VSkXhk2SK7 qPojVM0sfoGhNUCaHi1/Ote7haJPx7I+GcidwhJAWP2N24IaCy9PohlFCuoPPBCbzc4t OwOcQUKgy8y9C0G+ckoGxF8FGg/MmMYf7RlOjEny7J5UXnU5W822SEiysVdV5UNbpQRX u+kg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LmIs9LcN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b26si8644160edv.315.2022.02.08.03.30.05; Tue, 08 Feb 2022 03:30:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LmIs9LcN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S229617AbiBDJZu (ORCPT + 99 others); Fri, 4 Feb 2022 04:25:50 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:43630 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357554AbiBDJYc (ORCPT ); Fri, 4 Feb 2022 04:24:32 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 18809616DB; Fri, 4 Feb 2022 09:24:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D06A3C340EF; Fri, 4 Feb 2022 09:24:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643966671; bh=ZzMj6G9e6/gWX7cJeYR8I7KvzFa61O0cT2/d4KYtnsM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LmIs9LcNmIDEgis9fGmzfKwy0n7j5DYhqFCFDz33xnobrg0dUqKiqNpKDbau/A1c5 SgbxKLocC9PPpGzH7IG9I2EYMsJRMypcr/L5lPDonE23P8ArBtv84OVf0D57S0WFwQ eXgxUAIKFGwIOVkrO/5SE5LIx2FX2yWl/EJnY84s= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dan Carpenter , Mathias Krause , Jan Kara Subject: [PATCH 5.15 26/32] fanotify: Fix stale file descriptor in copy_event_to_user() Date: Fri, 4 Feb 2022 10:22:36 +0100 Message-Id: <20220204091916.121063674@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220204091915.247906930@linuxfoundation.org> References: <20220204091915.247906930@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: Dan Carpenter commit ee12595147ac1fbfb5bcb23837e26dd58d94b15d upstream. This code calls fd_install() which gives the userspace access to the fd. Then if copy_info_records_to_user() fails it calls put_unused_fd(fd) but that will not release it and leads to a stale entry in the file descriptor table. Generally you can't trust the fd after a call to fd_install(). The fix is to delay the fd_install() until everything else has succeeded. Fortunately it requires CAP_SYS_ADMIN to reach this code so the security impact is less. Fixes: f644bc449b37 ("fanotify: fix copy_event_to_user() fid error clean up") Link: https://lore.kernel.org/r/20220128195656.GA26981@kili Signed-off-by: Dan Carpenter Reviewed-by: Mathias Krause Signed-off-by: Jan Kara Signed-off-by: Greg Kroah-Hartman --- fs/notify/fanotify/fanotify_user.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -611,9 +611,6 @@ static ssize_t copy_event_to_user(struct if (fanotify_is_perm_event(event->mask)) FANOTIFY_PERM(event)->fd = fd; - if (f) - fd_install(fd, f); - if (info_mode) { ret = copy_info_records_to_user(event, info, info_mode, pidfd, buf, count); @@ -621,6 +618,9 @@ static ssize_t copy_event_to_user(struct goto out_close_fd; } + if (f) + fd_install(fd, f); + return metadata.event_len; out_close_fd: