Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1089574ybt; Wed, 1 Jul 2020 18:38:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwAYMFuf2Pv25pUaHsAyMVy/4S8avrvyE0LkXNX8wetXzlWO4VC66BIZNVtK2UmPwatcCDm X-Received: by 2002:a05:6402:234b:: with SMTP id r11mr4322669eda.5.1593653900799; Wed, 01 Jul 2020 18:38:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593653900; cv=none; d=google.com; s=arc-20160816; b=vJFEC7QLM+E+ZS5zGyUprnXjmc/KBHDGU6xEAFMjtzN1Ll+xSlclZekrXxmSpYr3dM ZhVVfR1UA41ooZ5F5ayqol7YGkLaW10byMRmcyX3PPW5T7ZYsCtgXPtHKC/z4o+YXAqp RuK7as6+qCPbNUSOHpjvo4RBzdIUf9u5GTYAaFe63e7HmdozoChj6F8Aw2RWV4yPUgPm ke94BSvDfKl1Zz32boKpCIzynAU70H5swOimSFPUfPWiVyRzV4Md5oZ5LAFFOCLBxcBQ 3Q7fBsdFWuSWEuDHzo9L1BIHf2gkCBskt3lsFwPTUigxHYBzXzpNV7dmMt0/1cxl6JKv 0pRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=5SOgtQ+dctVSXMynpNASSSsH+nX0gxpgQftWkE+PcPw=; b=JGTUn+XGQZupIHlFuUkosu1fovytgQSohIezsQzBgn80/I/R8Px0AzUr/7s2e1n7Ob Z3Bb4YvROZBstOCoBatVN9tWDq8MNznZ3PI2dc8vCsEag024PIeYEQNjjG0dBV14PVxW zKCT0rut/yEDtrCjGX8wTneTqRpT5zdRAJ5hpoDWCBKiWBdvWIEZhkS2Eg5LZVfaIZ3m Ae6fbFeO1+L/dBPigPZ3ZOHCuULby9xGvqNrrkMEjADIegcuI8TLRs/CuY4PFciGW2AT gtLas40MoY+9XFdIj9PXtmsHF/XI54IqxZotJVZEygnevYgw35WXkE4oaj5N2pDsdejT hLFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gRaF9A5J; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rv26si4999644ejb.2.2020.07.01.18.37.58; Wed, 01 Jul 2020 18:38:20 -0700 (PDT) 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=@kernel.org header.s=default header.b=gRaF9A5J; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728545AbgGBBf5 (ORCPT + 99 others); Wed, 1 Jul 2020 21:35:57 -0400 Received: from mail.kernel.org ([198.145.29.99]:53916 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728014AbgGBBXV (ORCPT ); Wed, 1 Jul 2020 21:23:21 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6946620748; Thu, 2 Jul 2020 01:23:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1593653001; bh=C/9MLaKqlwsK7BQ6AAFWVM54QNYaZWI0wyN/0N4q3Vo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gRaF9A5JdpiNQ5m+13cX0KTxxqsHr9QwsEKRKVpcqudFx9070in7F+UWkINSkQVB3 9DniZq3MWp/CNVokjhwZ12BrP9ErE67OviSZblqZYE/0CvSSwQSVrS/vIgDOEZh0Ww 8RIkR+9GjYaP+Tsfm18FLVfJFrAUEvDuAQ72g3FA= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Zhenzhong Duan , Mark Brown , Sasha Levin , linux-spi@vger.kernel.org Subject: [PATCH AUTOSEL 5.7 21/53] spi: spidev: fix a potential use-after-free in spidev_release() Date: Wed, 1 Jul 2020 21:21:30 -0400 Message-Id: <20200702012202.2700645-21-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200702012202.2700645-1-sashal@kernel.org> References: <20200702012202.2700645-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Zhenzhong Duan [ Upstream commit 06096cc6c5a84ced929634b0d79376b94c65a4bd ] If an spi device is unbounded from the driver before the release process, there will be an NULL pointer reference when it's referenced in spi_slave_abort(). Fix it by checking it's already freed before reference. Signed-off-by: Zhenzhong Duan Link: https://lore.kernel.org/r/20200618032125.4650-2-zhenzhong.duan@gmail.com Signed-off-by: Mark Brown Signed-off-by: Sasha Levin --- drivers/spi/spidev.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/drivers/spi/spidev.c b/drivers/spi/spidev.c index 82e6481fdf950..012a89123067c 100644 --- a/drivers/spi/spidev.c +++ b/drivers/spi/spidev.c @@ -608,15 +608,20 @@ static int spidev_open(struct inode *inode, struct file *filp) static int spidev_release(struct inode *inode, struct file *filp) { struct spidev_data *spidev; + int dofree; mutex_lock(&device_list_lock); spidev = filp->private_data; filp->private_data = NULL; + spin_lock_irq(&spidev->spi_lock); + /* ... after we unbound from the underlying device? */ + dofree = (spidev->spi == NULL); + spin_unlock_irq(&spidev->spi_lock); + /* last close? */ spidev->users--; if (!spidev->users) { - int dofree; kfree(spidev->tx_buffer); spidev->tx_buffer = NULL; @@ -624,19 +629,14 @@ static int spidev_release(struct inode *inode, struct file *filp) kfree(spidev->rx_buffer); spidev->rx_buffer = NULL; - spin_lock_irq(&spidev->spi_lock); - if (spidev->spi) - spidev->speed_hz = spidev->spi->max_speed_hz; - - /* ... after we unbound from the underlying device? */ - dofree = (spidev->spi == NULL); - spin_unlock_irq(&spidev->spi_lock); - if (dofree) kfree(spidev); + else + spidev->speed_hz = spidev->spi->max_speed_hz; } #ifdef CONFIG_SPI_SLAVE - spi_slave_abort(spidev->spi); + if (!dofree) + spi_slave_abort(spidev->spi); #endif mutex_unlock(&device_list_lock); -- 2.25.1