From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752972AbbIKMLw (ORCPT ); Fri, 11 Sep 2015 08:11:52 -0400 Received: from mga09.intel.com ([134.134.136.24]:5422 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751875AbbIKMLu (ORCPT ); Fri, 11 Sep 2015 08:11:50 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.17,511,1437462000"; d="scan'208";a="767003056" Message-ID: <55F2C4F4.3090704@linux.intel.com> Date: Fri, 11 Sep 2015 15:11:32 +0300 From: Jarkko Nikula User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.7.0 MIME-Version: 1.0 To: Sudip Mukherjee , Mark Brown CC: linux-kernel@vger.kernel.org, linux-spi@vger.kernel.org Subject: Re: [PATCH] spi: spidev: fix possible NULL dereference References: <1441883893-20596-1-git-send-email-sudipm.mukherjee@gmail.com> In-Reply-To: <1441883893-20596-1-git-send-email-sudipm.mukherjee@gmail.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi On 09/10/2015 02:18 PM, Sudip Mukherjee wrote: > During the last close we are freeing spidev if spidev->spi is NULL, but > just before checking if spidev->spi is NULL we are dereferencing it. > Lets add a check there to avoid the NULL dereference. > > Signed-off-by: Sudip Mukherjee > --- > drivers/spi/spidev.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/drivers/spi/spidev.c b/drivers/spi/spidev.c > index fba92a5..ef008e5 100644 > --- a/drivers/spi/spidev.c > +++ b/drivers/spi/spidev.c > @@ -651,7 +651,8 @@ static int spidev_release(struct inode *inode, struct file *filp) > kfree(spidev->rx_buffer); > spidev->rx_buffer = NULL; > > - spidev->speed_hz = spidev->spi->max_speed_hz; > + if (spidev->spi) > + spidev->speed_hz = spidev->spi->max_speed_hz; > > /* ... after we unbound from the underlying device? */ > spin_lock_irq(&spidev->spi_lock); Actually this fixes a *real* NULL dereference case but it is not related to normal open/close life cycle of spidev. It requires that spidev is kept open when SPI master is removed. spidev->spi is set to NULL in spidev_remove() and it is called before spidev_release() in case master is removed while spidev is in use It would be good to update this to commit log, add a tag below and Cc Fixes: 9169051617df ("spi: spidev: Don't mangle max_speed_hz in underlying spi device") You can add these too: Reviewed-by: Jarkko Nikula Tested-by: Jarkko Nikula