Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp6016806pxb; Mon, 14 Feb 2022 13:13:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJxsjGqKw5PXjuQ7i7CWuyS9B8i53GMhgnCXA3bCZBtI4TQhaQsG4D73zccSFEefOA4TfpPG X-Received: by 2002:a63:697:: with SMTP id 145mr746603pgg.339.1644873185765; Mon, 14 Feb 2022 13:13:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644873185; cv=none; d=google.com; s=arc-20160816; b=LF6ZLLj2FQbROWdCIgsrCefZYgCejPvVrsak90XydcwqBsx/XYbRV2hN0IqQPdfH52 c50Mlx6a/aOPlwm549oaus972Hkgec+ydxEn4Xiowlkx3udmOKKbazQtNpM35iBFLbVK Q3c38OhE8kiF9UaplSjq6OwhJ0IgYuxHrUeh5fRclKMzCRP7hVfYMTq6MbwOIMLe8OoE yFaHXiZptt4LAwBoY3x7hJjh1gSiYOR/P3wDB/X05SOE8cJkjc05Y3N6fv8fFM4P3+x8 ButtLIiarQAaA7o4RWqmRO3jDYAr3wJcR+nUQ8+QEH7syJ+5JpesnRwxL5myIPDEPDo9 qYeA== 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=Kqt/ebmNISvZDswB47zMvqd3kHUyAi2Nkyv76wiLTMM=; b=PrifAyvXxl18n2JLB/hC4ra+KEb3i0cdCB1yHsYmmsdICTmKk0QmUhViDPjehsHvhk ADZdj63qAwu/fsPqkvQ7wEym/TIEg2q5ubdL9Gio6C5LBXKNmjIM2IpIfIDrtSBxCEfA 2DBLqiBw3ISTM0PhpoSy8iaZYtPbyeodDVg1x75I/ncwEAkl/qRRsrmu7Cxtu7HPg9qq 0PmIVw/6VgrNpqp5mBvjJJTxrmQRzEd2ulgOBQgXEqner5rAkpSMj6qxn9dddesL9tqS jatXW45ILKqkVIgR8QQpO2a8ULh9m5ZtDNLRsLa1mNJM+HR7MwvJXurROcETQEsOocDz WpaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=nidrAjDq; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id e18si15146482plj.334.2022.02.14.13.13.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Feb 2022 13:13:05 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=nidrAjDq; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D38E0219212; Mon, 14 Feb 2022 12:32:19 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345983AbiBNKRS (ORCPT + 99 others); Mon, 14 Feb 2022 05:17:18 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:42346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346129AbiBNKPJ (ORCPT ); Mon, 14 Feb 2022 05:15:09 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27A556D39A; Mon, 14 Feb 2022 01:52:27 -0800 (PST) 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 ams.source.kernel.org (Postfix) with ESMTPS id C1F17B80D6D; Mon, 14 Feb 2022 09:52:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D0161C340F4; Mon, 14 Feb 2022 09:52:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1644832342; bh=UjNB3wvWO+ihLvlrB/tdA1pAejKA9z210pMttgIl/5o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nidrAjDql8HB/QcKJGM5iMzfN2FUwqVphyVyfMU7suEwEYYu/U7FkzkkAcmewqOqI /C/DftVlPCMi0oHc4MfcCZ1hnuNRLX6J8AH8XJnAXntqXhVXHTyUr9W6q7lPAqbKuE F6fsbG2OaIPhVlaMmTivWu+Fly6OdKdwo0RsnoeA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, stable@kernel.org, Jonathan Cameron , Alexandru Ardelean , Lars-Peter Clausen , Nuno Sa , Dan Carpenter , Mathias Krause , Jonathan Cameron Subject: [PATCH 5.15 156/172] iio: buffer: Fix file related error handling in IIO_BUFFER_GET_FD_IOCTL Date: Mon, 14 Feb 2022 10:26:54 +0100 Message-Id: <20220214092511.787864134@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220214092506.354292783@linuxfoundation.org> References: <20220214092506.354292783@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mathias Krause commit c72ea20503610a4a7ba26c769357d31602769c01 upstream. If we fail to copy the just created file descriptor to userland, we try to clean up by putting back 'fd' and freeing 'ib'. The code uses put_unused_fd() for the former which is wrong, as the file descriptor was already published by fd_install() which gets called internally by anon_inode_getfd(). This makes the error handling code leaving a half cleaned up file descriptor table around and a partially destructed 'file' object, allowing userland to play use-after-free tricks on us, by abusing the still usable fd and making the code operate on a dangling 'file->private_data' pointer. Instead of leaving the kernel in a partially corrupted state, don't attempt to explicitly clean up and leave this to the process exit path that'll release any still valid fds, including the one created by the previous call to anon_inode_getfd(). Simply return -EFAULT to indicate the error. Fixes: f73f7f4da581 ("iio: buffer: add ioctl() to support opening extra buffers for IIO device") Cc: stable@kernel.org Cc: Jonathan Cameron Cc: Alexandru Ardelean Cc: Lars-Peter Clausen Cc: Nuno Sa Reported-by: Dan Carpenter Signed-off-by: Mathias Krause Reviewed-by: Jonathan Cameron Signed-off-by: Greg Kroah-Hartman --- drivers/iio/industrialio-buffer.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) --- a/drivers/iio/industrialio-buffer.c +++ b/drivers/iio/industrialio-buffer.c @@ -1446,9 +1446,17 @@ static long iio_device_buffer_getfd(stru } if (copy_to_user(ival, &fd, sizeof(fd))) { - put_unused_fd(fd); - ret = -EFAULT; - goto error_free_ib; + /* + * "Leak" the fd, as there's not much we can do about this + * anyway. 'fd' might have been closed already, as + * anon_inode_getfd() called fd_install() on it, which made + * it reachable by userland. + * + * Instead of allowing a malicious user to play tricks with + * us, rely on the process exit path to do any necessary + * cleanup, as in releasing the file, if still needed. + */ + return -EFAULT; } return 0;