Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp170930rwi; Wed, 12 Oct 2022 18:03:16 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7IqVCKKWp6b+1YtmZAg2ekmAQ0vNzCIhilWuPYRYkzydsSUiCLAiTtIS1C93SjMQ8ZEfKn X-Received: by 2002:a17:906:770d:b0:73c:a08f:593c with SMTP id q13-20020a170906770d00b0073ca08f593cmr25593484ejm.182.1665622996024; Wed, 12 Oct 2022 18:03:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665622996; cv=none; d=google.com; s=arc-20160816; b=ncrv9PHH45fNSU0u6sV91SkQomLaOXobkMJ2qQeAyNJ69GYvQkDwEFkA+VrWXpVkZK TEv79eTEEoN8dkRtkk4fl7QNSFcJ7HB70vSDOu36qlyL0TD8TNftnDkRpiPolT16pU4c 76/60pMUueaXLTxx1cRVXY4d+U17x6FEp2aOwMEOQPyndtM0tj9jSMItWb0FtAkEvzZJ YCSh/edm/Ut5dBHa3xgukt0cL5wjjJzF5iN4OwvnRMrXDLfUNYb5A1eaIgpp8UyP0nBA QBtDJ7UIqYcqJIiosX5yxZ2/7LETFNpAv5ObmesetyvhhYYRyyCEXQsfy6yLFKWBsvjy 1yyg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=gnoTKFganq8WlELK/iSYCLvh52KKTxuFVMqPOk/R5Tk=; b=u/fjojzLVh+fMJI8J2k8Yr/Ou0BP0kq8CWPSUU8Y2exFgp5NnXiVV9v/VRODgVRCwW h4Am1+gA3t0utzUqpH4Hb1R8NE3IwoV2cilcQ4rHto5gpO5uG38IvAYSF0hgR6ws3TRl uo65PBTFT4H5yfD4RFGcYgiG7azJWkeSd/u+yju5ySOTyKXnXB2qv6qzSKdSYp/maZop rjcxVvE/jczI1lKj03KKloaJFbxyE5HJFuF5xRCligClnfNUg5LImURQe4H5Y0aiwYKJ QsuGYRilJufz35soS1vDYMZ8c4fxbOU0Y5bSVf53DMjX3CE0qWEFPg7XSuusc1Ic0Tet wxLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PhWe5kIO; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nb20-20020a1709071c9400b0078dad5e8d65si10681217ejc.68.2022.10.12.18.02.50; Wed, 12 Oct 2022 18:03:16 -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=@kernel.org header.s=k20201202 header.b=PhWe5kIO; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230449AbiJMAYd (ORCPT + 99 others); Wed, 12 Oct 2022 20:24:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230407AbiJMAXd (ORCPT ); Wed, 12 Oct 2022 20:23:33 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 172E51055E; Wed, 12 Oct 2022 17:20:51 -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 ams.source.kernel.org (Postfix) with ESMTPS id B9799B81CCD; Thu, 13 Oct 2022 00:18:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8B6FEC43470; Thu, 13 Oct 2022 00:18:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665620300; bh=GmRUeDbFgkpqIs8eExCjyoabv5cVnd0mR8YC0z3X1xw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PhWe5kIOEJWqZLR9X9/yB18bHLWdiWByUY2SNyEOPqyEsiCF7zy04abPYxXInxL9f U3EIjRI2MqT91LeTIEbuLVxLfPdjHGdj087jMqPsmzvWq1bl9hEzbSS3lE3iKGRCzI hW05TF7M8JqSQ4K/ls/N/KucUKpQfv8NAy/d5E4e0dLX5aXhXhQxYAb3Kg4b3pMw01 BiqeUGoM21ECqyfY04QznCJ8RA47h7w8FUfcKRLagXXvee37B91gCBaxTad/L2crNy R9VR/haG4cvDCLGO6ENgj5aVGeEUEzYFFROjb0sJ7gDyzKwG81wXW7fOkBpccyW/7I 6D+5ENAmKMGqQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Eddie James , Guenter Roeck , Joel Stanley , Sasha Levin , jk@ozlabs.org, linux-fsi@lists.ozlabs.org Subject: [PATCH AUTOSEL 6.0 61/67] fsi: occ: Prevent use after free Date: Wed, 12 Oct 2022 20:15:42 -0400 Message-Id: <20221013001554.1892206-61-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221013001554.1892206-1-sashal@kernel.org> References: <20221013001554.1892206-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 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