Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp2090897ybn; Thu, 26 Sep 2019 06:56:44 -0700 (PDT) X-Google-Smtp-Source: APXvYqzRfzh1i++L/bi8MBrds/6QMBNtRZBQqbcgk1cLshnu9kQHDFYM3SMeLBgfugzmMUyz1WmD X-Received: by 2002:a50:b7ed:: with SMTP id i42mr3703760ede.52.1569506204038; Thu, 26 Sep 2019 06:56:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569506204; cv=none; d=google.com; s=arc-20160816; b=S3+AzLHiU4nibCAuGnNV6Ao4oQCbF0fMa/oVu8OMoGz/qI+qGLYNMEtTv1PvQ8fXXG UPhjzFIhCiHmJgfTf1pps5mDCMVxGYCHke4KRMchqX6C+gVJ408EFOA4kXDh0tGnpUHZ Sy06WiXN98f4R/6QaILkU2uzKB3mnVxbzB5m2i+gSwhwyfK3uVu9tGNXyDGXyC4n9Rc9 jpAJT08+E0Q18jncg0bFfp+tTmPg7W0xFfNHOkjVJb3+Ay8f+gkGcYeJC197iRpvrZdf VHlweMPu1cQr6G+EoaiAME0tyL2ICXYlCqpvL9SBdNlQ8y/5rDXL0ObHq/b4dXMjXTFn Udew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version; bh=/mVhT/fXwK9lB2n80OuetyQRidyOwg4W8y9aVMt/9yY=; b=YnamLkNygg+zKYR2yiH5/Q1jIzx5TpkZj7wmT93wDjZCa+Jcg1aXQKtCthDZQU7oVN Cp5jazDhfPEIwgywlooIsvMD1DU6jPZ0nAG9XdpQDwPT0zqD6nFKDdTmWGgeVg8nh7+B vvIsW/ShtaZQrlvDbmKL1YIogvPdnRoNDEMxBpb0ADCmgHZs9o/E636jbP0vOe9jR6Wk jLYoJqWkt3QaGrNKAMjCXTvjAp7exc0q2UzAdgPEva8x9JroTleFBM2R2Xurw9NDnalT zNhswLKP/1+QoKDwsDRwZ2IwinoP/8b0tiE2hTZK7hdyow3nHwLX7TYy3BfWed8/cXjw dGMQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d22si1413131ede.131.2019.09.26.06.56.20; Thu, 26 Sep 2019 06:56:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727084AbfIZNwL (ORCPT + 99 others); Thu, 26 Sep 2019 09:52:11 -0400 Received: from mail-ot1-f66.google.com ([209.85.210.66]:39470 "EHLO mail-ot1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726666AbfIZNwL (ORCPT ); Thu, 26 Sep 2019 09:52:11 -0400 Received: by mail-ot1-f66.google.com with SMTP id s22so2048668otr.6; Thu, 26 Sep 2019 06:52:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=/mVhT/fXwK9lB2n80OuetyQRidyOwg4W8y9aVMt/9yY=; b=T31Y4Trtl5YsroOIzGjrknOkrsIRcmIng311J120dwWiTDERgxSiwARc07mnPwFzaz gAxuYyNvqRtLfvxR3u51Qwld0XJLDOsED6xuTzYth9JZVZeht18k0+dMjfvhJiKN0LNT 223kCp3EJP0el3Q0BbeyKGKAKWwC7gBd1n89BDWSObh25HLIDD9V9Nia0jaqe8DbuarR q3J6VsOSWQco571Vl+0XywU+0ygNIKbDTVq1MwZoifEuQDFswjaUtNffrFjMF0y5gUrI HG48qa4qgq7A7TV48MSv3me7FWUjojhnyp8QRr/Afd3JOUHszWQcXX1t40PyoI4gBvgk Fa8w== X-Gm-Message-State: APjAAAViZE4WhzNRIcs0538qsZV0/xR1aRKI4ecrIIPTsvfaoNWckKjz vpfroFwg50jvUztPzlX+XkTEVS6kp/GTeQAcWAQ= X-Received: by 2002:a9d:5a06:: with SMTP id v6mr479841oth.250.1569505930027; Thu, 26 Sep 2019 06:52:10 -0700 (PDT) MIME-Version: 1.0 References: <20190926121438.655f1f10@jawa> <20190926144342.327a3c66@jawa> In-Reply-To: <20190926144342.327a3c66@jawa> From: Geert Uytterhoeven Date: Thu, 26 Sep 2019 15:51:58 +0200 Message-ID: Subject: Re: spi: Add call to spi_slave_abort() function when spidev driver is released To: Lukasz Majewski Cc: Colin Ian King , Mark Brown , linux-spi , "linux-kernel@vger.kernel.org" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Lukasz, On Thu, Sep 26, 2019 at 2:49 PM Lukasz Majewski wrote: > > On Thu, Sep 26, 2019 at 12:14 PM Lukasz Majewski > > wrote: > > > > Static analysis with Coverity has detected an potential > > > > dereference of a free'd object with commit: > > > > > > > > commit 9f918a728cf86b2757b6a7025e1f46824bfe3155 > > > > Author: Lukasz Majewski > > > > Date: Wed Sep 25 11:11:42 2019 +0200 > > > > > > > > spi: Add call to spi_slave_abort() function when spidev > > > > driver is released > > > > The call to spi_slave_abort() on spidev is reading an earlier > > > > kfree'd spidev. > > > > > > Thanks for spotting this issue - indeed there is a possibility to > > > use spidev after being kfree'd. > > > > Worse, this makes me realize spidev->spi may be a NULL pointer, which > > will be dereferenced by spi_slave_abort(), so caching it before the > > call to kfree() won't work. > > The patch as it is now can be fixed as follows: > > static int spidev_release(struct inode *inode, struct file *filp) > { > struct spidev_data *spidev; > > mutex_lock(&device_list_lock); > spidev = filp->private_data; > filp->private_data = NULL; > > #ifdef CONFIG_SPI_SLAVE > if (spidev->spi) > spi_slave_abort(spidev->spi); > #endif > > /* last close? */ > spidev->users--; > if (!spidev->users) { > int dofree; > > /* free buffers */ > > 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); > } > > mutex_unlock(&device_list_lock); > > return 0; > } > > The question is if we shall call the spi_slave_abort() when cleaning up > spi after releasing last reference, or each time release callback is > called ? TBH, I don't know. Is it realistic that there are multiple opens? > > > However, Geert (CC'ed) had some questions about placement of this > > > function call, so I will wait with providing fix until he replies. > > > > Seems like this needs more thought... > > Could you be more specific? > > Do you mean to move the spi_slave_abort() call just before dofree > evaluation ? ([*]). That means the abort is called only for the last user. And only if the underlying device still exists. Which means that if it has disappeared (how can that happen? spidev unbind?), the slave was never aborted. Non-spidev slaves can do the abort in their .remove() callbacks (at least my two sample slave drivers do). So probably we need some explicit slave abort in the unbind case too? The more I think about it, the more things I see that can go wrong... Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds