Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp875004rwi; Wed, 19 Oct 2022 04:11:02 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4kC54n/9zCKIv+X2mUZxXN20s6YbKYVaLnZNNBAx6Fnu9MbEWEmv8aWkJQK08V/Hc1d6QZ X-Received: by 2002:a17:906:cc5a:b0:78d:c3cf:6ff7 with SMTP id mm26-20020a170906cc5a00b0078dc3cf6ff7mr6234591ejb.383.1666177861946; Wed, 19 Oct 2022 04:11:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666177861; cv=none; d=google.com; s=arc-20160816; b=F8KO5A6wlFVUQNCtB3q1t0iRZMD0BiDHoYbnvcPOb6Nw4IKHu2oPU+du3XEe7RuMKR YpJtOo+5eM4jxT/haLZ0p6gcCDst8CEK6RW0zk4uhUkBu3iAzgh60z1HW8V2yR+q+3aL T9Ypdhi1JYWnLz+wp85xq6So6tPtfwyDu1pYrZFqWhl1YrFx/mFEsOJ16ngxxfDuBua9 aXyKghiXqTu2d7MTbuR3QtMC4/MszZMF6pdFQEpOG2AMn+LPiNMvPHaGgKz9CJ5lB0zK 8HVgQkJGjiFD/3BxiQVjXWpRm/EQmEsl4CoDg2TZACG3tsZcREBiap7M1sJeqwHiKYwh Ajig== 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=gnoTKFganq8WlELK/iSYCLvh52KKTxuFVMqPOk/R5Tk=; b=0atu2rAMHvGi9zzymmq2mvUu9+eipMu7avYcRc6fJ4kPU4t1fB6HmLDDKSu0R4jlP4 GKpod83GVtwUfea9aP4f1C1XjCHXVx8EZWwQynNaE34LgStp8cNeSlhe7WPdy53hEwgG Q5iOKiUaAQ4cV4429SXJASSUi6DFdAA+8tnCvBU90Cm4f7luBt3kRGXH5yiE6uVSVvz6 CxWPyBwYZJIXNWfzFD8CcBNOMChodE8ufAC3doOJjd60B5Qq1+LuXQUcVrJst62rDPnd V1zOd/jSEu/vQV3IsfcmNIV1ntNMFWD/sRI86YXkOFMPSvuAyO1MXaW3oFY5pIJblxtr qXdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ejAue5PO; 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 s17-20020a056402521100b004523826ed6dsi14643268edd.554.2022.10.19.04.10.36; Wed, 19 Oct 2022 04:11:01 -0700 (PDT) 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=ejAue5PO; 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 S234197AbiJSJmz (ORCPT + 99 others); Wed, 19 Oct 2022 05:42:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234363AbiJSJkN (ORCPT ); Wed, 19 Oct 2022 05:40:13 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B6BFCF87C; Wed, 19 Oct 2022 02:16:36 -0700 (PDT) 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 BD3E761800; Wed, 19 Oct 2022 09:15:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CCC92C433D6; Wed, 19 Oct 2022 09:15:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666170911; bh=GmRUeDbFgkpqIs8eExCjyoabv5cVnd0mR8YC0z3X1xw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ejAue5POvMRwis+kv4YijXWlaSBWyeCytmGtNZUueeClKpnn+/D9reqxG8kh/UClT 5+ykOoLymFkmBPw37ZdQL3i39I06Hw7sui122Z3COZ7PaFR7qfXgXLfpV+QIcVhzem 1U5NaqsoElirOqtmEvl1pnzU3W1TX1zu+WdwOSEA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eddie James , Guenter Roeck , Joel Stanley , Sasha Levin Subject: [PATCH 6.0 834/862] fsi: occ: Prevent use after free Date: Wed, 19 Oct 2022 10:35:21 +0200 Message-Id: <20221019083326.738514785@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham 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: Eddie James [ Upstream commit d3e1e24604031b0d83b6c2d38f54eeea265cfcc0 ] Use get_device and put_device in the open and close functions to make sure the device doesn't get freed while a file descriptor is open. Also, lock around the freeing of the device buffer and check the buffer before using it in the submit function. Signed-off-by: Eddie James Reviewed-by: Guenter Roeck Link: https://lore.kernel.org/r/20220513194424.53468-1-eajames@linux.ibm.com Signed-off-by: Joel Stanley Signed-off-by: Sasha Levin --- drivers/fsi/fsi-occ.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/drivers/fsi/fsi-occ.c b/drivers/fsi/fsi-occ.c index c9cc75fbdfb9..28c176d038a2 100644 --- a/drivers/fsi/fsi-occ.c +++ b/drivers/fsi/fsi-occ.c @@ -94,6 +94,7 @@ static int occ_open(struct inode *inode, struct file *file) client->occ = occ; mutex_init(&client->lock); file->private_data = client; + get_device(occ->dev); /* We allocate a 1-page buffer, make sure it all fits */ BUILD_BUG_ON((OCC_CMD_DATA_BYTES + 3) > PAGE_SIZE); @@ -197,6 +198,7 @@ static int occ_release(struct inode *inode, struct file *file) { struct occ_client *client = file->private_data; + put_device(client->occ->dev); free_page((unsigned long)client->buffer); kfree(client); @@ -493,12 +495,19 @@ int fsi_occ_submit(struct device *dev, const void *request, size_t req_len, for (i = 1; i < req_len - 2; ++i) checksum += byte_request[i]; - mutex_lock(&occ->occ_lock); + rc = mutex_lock_interruptible(&occ->occ_lock); + if (rc) + return rc; occ->client_buffer = response; occ->client_buffer_size = user_resp_len; occ->client_response_size = 0; + if (!occ->buffer) { + rc = -ENOENT; + goto done; + } + /* * Get a sequence number and update the counter. Avoid a sequence * number of 0 which would pass the response check below even if the @@ -671,10 +680,13 @@ static int occ_remove(struct platform_device *pdev) { struct occ *occ = platform_get_drvdata(pdev); - kvfree(occ->buffer); - misc_deregister(&occ->mdev); + mutex_lock(&occ->occ_lock); + kvfree(occ->buffer); + occ->buffer = NULL; + mutex_unlock(&occ->occ_lock); + device_for_each_child(&pdev->dev, NULL, occ_unregister_child); ida_simple_remove(&occ_ida, occ->idx); -- 2.35.1