From patchwork Tue Apr 10 22:44:30 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Brezillon X-Patchwork-Id: 896963 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bootlin.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="r6R2hR1a"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40LMgz3gH7z9s0n for ; Wed, 11 Apr 2018 08:45:15 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=YknH5h+MyIxQgRo85rKzBkNj7hZ0lWa/Fyg1IwXyYe4=; b=r6R2hR1apT9XZduy0UPR+Rw/wJ 6Hf5InrwtyaX9Ai/2VZaD1v8ixVAMpYJuVkgPky24BYZjuXbs3Al1BjyO29MZM04xgSCEsnrzcLC8 wX9Z/bqm+w5H2Ya3i3rkTH1Hj9c2SD7l8UmHIcwPHQTboWLmQ980uTh/2B6Epg+hS5nAVgJ1H0oLh US39HKu2epSowQXdMijU8WhzeC6A9RzRo32/Xn8xcLJeIti07Zh6V3lCKOd1Z6flXSLyaXM5RTL+2 oQ+1+9keueEKKcyBrwMGMMjQOQXbrBfXqlxXf1PBgBQMMNc+eQTpjNHQgNL5xSit2woWaqkY0JPbS XEHIja3w==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1f620a-0006HN-Mm; Tue, 10 Apr 2018 22:45:00 +0000 Received: from mail.bootlin.com ([62.4.15.54]) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1f620X-00060B-84 for linux-mtd@lists.infradead.org; Tue, 10 Apr 2018 22:44:59 +0000 Received: by mail.bootlin.com (Postfix, from userid 110) id AF1AB2072F; Wed, 11 Apr 2018 00:44:44 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on mail.bootlin.com X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,SHORTCIRCUIT shortcircuit=ham autolearn=disabled version=3.4.0 Received: from localhost.localdomain (unknown [91.160.177.164]) by mail.bootlin.com (Postfix) with ESMTPSA id 5375C2071B; Wed, 11 Apr 2018 00:44:44 +0200 (CEST) From: Boris Brezillon To: David Woodhouse , Brian Norris , Boris Brezillon , Marek Vasut , Richard Weinberger , Cyrille Pitchen , linux-mtd@lists.infradead.org, Miquel Raynal , Mark Brown , linux-spi@vger.kernel.org Subject: [PATCH v2 01/10] spi: Check presence the of ->transfer[_xxx]() before registering a controller Date: Wed, 11 Apr 2018 00:44:30 +0200 Message-Id: <20180410224439.9260-2-boris.brezillon@bootlin.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180410224439.9260-1-boris.brezillon@bootlin.com> References: <20180410224439.9260-1-boris.brezillon@bootlin.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180410_154457_441915_EF49B2BC X-CRM114-Status: GOOD ( 15.00 ) X-Spam-Score: -0.0 (/) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (-0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [62.4.15.54 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Yogesh Gaur , Vignesh R , Kamal Dasu , Maxime Chevallier , Peter Pan , Frieder Schrempf , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Sourav Poddar MIME-Version: 1.0 Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Right now, no checks are done on the presence of a ->transfer[_xxx]() method, which can lead to a NULL pointer dereference when someone starts sending something on the bus. Do the check at registration time and refuse to add the controller if all ->transfer[_xxx]() pointers are NULL. Signed-off-by: Boris Brezillon --- drivers/spi/spi.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 7b213faa0a2b..24369e437c6b 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -2063,6 +2063,19 @@ static int of_spi_register_master(struct spi_controller *ctlr) } #endif +static int spi_controller_check_ops(struct spi_controller *ctlr) +{ + /* + * The controller must at least implement one of the ->transfer() + * hooks. + */ + if (!ctlr->transfer && !ctlr->transfer_one && + !ctlr->transfer_one_message) + return -EINVAL; + + return 0; +} + /** * spi_register_controller - register SPI master or slave controller * @ctlr: initialized master, originally from spi_alloc_master() or @@ -2096,6 +2109,14 @@ int spi_register_controller(struct spi_controller *ctlr) if (!dev) return -ENODEV; + /* + * Make sure all necessary hooks are implemented before registering + * the SPI controller. + */ + status = spi_controller_check_ops(ctlr); + if (status) + return status; + if (!spi_controller_is_slave(ctlr)) { status = of_spi_register_master(ctlr); if (status)