Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp777302pxy; Thu, 22 Apr 2021 13:12:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy+6t5rVYPxUzvBb3li/KlUr+taZg+peECuT3uBkZrCZTvljuOLuQsZEAtIwumjkbCwFp3Q X-Received: by 2002:a17:902:7201:b029:ec:b5c2:571c with SMTP id ba1-20020a1709027201b02900ecb5c2571cmr532784plb.55.1619122322712; Thu, 22 Apr 2021 13:12:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619122322; cv=none; d=google.com; s=arc-20160816; b=a+YNRx7HbIvLqIQFRuDJaOtyAzDPMwRG70A6JNqE0bPaR4HEzuaDXMkG/Hb8drAErD TbqblijLsTZvHHtbc0FBLJBbHBuAdtfZWeZZE0tGZKfKONbiJpMqYRgrKh/sEvOKlR4h ND7nTEbye6UMt1wi7ZrTY3QASxeyC4LxPG5O2IA+Tj+vSIX7ku325T21RK/76euCMYmH MtzEo8wuLYpaKZj1aRmJzz+9GdeXk5C9U8zal5GsG+TblJqxtQqxbQPDHE3JSR6wutKV kX3TuyCjTLswmmLH/Qt9rnvAzUNrrjy9qdDmd9aejdck/3GtEHYZzugix21zMnckC4+l Dk2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:subject:from :references:to:dkim-signature; bh=wgmYy/LMkr3JJRVe6OWv2xNNfb7j4siB1GggwwkeZVY=; b=Y4gYnI+uBKryAXCeykTwaNGH9dXnEtDgPmAzs9OgkOjVdMZp6yFy2FCAk9PD7miHFH wwrpyw+Mdf59S73bzBLkYnii2HKB5MvxUhxpS52wkmuOgT3FBuToUAPqvKqj5RaBtrMP hEex4QYNlPey51jVJ7rzn+Nxyg+V4nIJG0y+0t9rR5dz5Od1UCkUU7blXyzxFjHJJZLp uI8/KTvdzA+meqrX7lfiKzY9Mwnqik9vtzl63qTXFandtXuyVhIhbmVHsIDQSdNl/mji aiwTxfRfQyQyXD3zp/J9R2tYo4ewrBrQNWT9vFz1vAPQxlE0o4oBOXhRdDa7x6VYzg41 gtDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@devtank-co-uk.20150623.gappssmtp.com header.s=20150623 header.b=Hw87f0vK; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z3si4208798pgb.216.2021.04.22.13.11.48; Thu, 22 Apr 2021 13:12:02 -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=@devtank-co-uk.20150623.gappssmtp.com header.s=20150623 header.b=Hw87f0vK; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236993AbhDVULB (ORCPT + 99 others); Thu, 22 Apr 2021 16:11:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236896AbhDVULB (ORCPT ); Thu, 22 Apr 2021 16:11:01 -0400 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B138DC061756 for ; Thu, 22 Apr 2021 13:10:25 -0700 (PDT) Received: by mail-ej1-x62a.google.com with SMTP id n2so70621706ejy.7 for ; Thu, 22 Apr 2021 13:10:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=devtank-co-uk.20150623.gappssmtp.com; s=20150623; h=to:references:from:subject:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=wgmYy/LMkr3JJRVe6OWv2xNNfb7j4siB1GggwwkeZVY=; b=Hw87f0vKJX6ikFIsxEDefV4hQCKQ/vDut2bwg0vXNIh02gg65hFiBF8sTw4f7o7GjG Z6FyVx2oJzlAM46oSFZaHsQ1l5+O7fhOnITZ2wIAflJbhrXnu6PY3n0fpltOy2TsIHfw 4Dl7frgNDQer60q80SPZ9Y2Wqek58dYaAsRLL7HEq9z6fwLEB/aHF2TkaGMSvLpYcf9u Xlw01V5fLlPeiDKRF08xi4RVvTG5UtNFskloYRI5kplwujmNACwwz7MdJ/tcgqVa7XAp 7QBli3aWPsi9+nLsuaIAAGEg1lkHXLlXysOq+3OKixqzv28oqv8zVIFPrlZ7CfvO85MN sejQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:references:from:subject:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=wgmYy/LMkr3JJRVe6OWv2xNNfb7j4siB1GggwwkeZVY=; b=hObgwHbritnae6MJoZEu/V+Bv4wTgaxnrNIie8XQgY2Owolbt+19WdwQrffZC56EhC aT6LnAt5I1LHCox3fUon8/KyLZVCA6l4ChqIFCvMIgvT9UxjP4uVkW6I1lDd0pgeHqKu bR8PPj2a3F5avxIbIooG7pxO5Bh/db+3T20SG6olm88oGdEinB0725M3hsoXPManESb1 aOGGAYm4+H8/KlwPLEkVOsZEazLjygxbO7Q6EMszecSDucDAdfqNHr1YoM21+yb9KoGo Vsg5qGh7vBdNMTiHqaVFiNrZKIuWvj527R5jN8sBI6MXzk61UFUKrbeU2sueeXkE/az1 brzw== X-Gm-Message-State: AOAM531xeP233OYBnd0LIOYj6K+U9HWKECiCJUYoyKH1RSCqTbHox6XL 9LSVki1jwHVozq5x8pbBII15zBv04HnFBHKb X-Received: by 2002:a17:906:6896:: with SMTP id n22mr461659ejr.316.1619122224095; Thu, 22 Apr 2021 13:10:24 -0700 (PDT) Received: from ?IPv6:2a02:8010:673b:0:65cb:937a:e25b:18a9? ([2a02:8010:673b:0:65cb:937a:e25b:18a9]) by smtp.gmail.com with ESMTPSA id s9sm2833911edd.16.2021.04.22.13.10.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 22 Apr 2021 13:10:23 -0700 (PDT) To: Florian Fainelli , Mark Brown , Ray Jui , Scott Branden , bcm-kernel-feedback-list@broadcom.com, Nicolas Saenz Julienne , linux-spi@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org References: <20210420083402.6950-1-joe.burmeister@devtank.co.uk> From: Joe Burmeister Subject: Re: [PATCH] spi: bcm2835: Fix buffer overflow with CS able to go beyond limit. Message-ID: <7c9f9376-1a80-b624-7b9e-0f6d04437c02@devtank.co.uk> Date: Thu, 22 Apr 2021 21:10:21 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.9.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Language: en-GB Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On 4/20/2021 1:34 AM, Joe Burmeister wrote: >> It was previoulsy possible to have a device tree with more chips than >> the driver supports and go off the end of CS arrays. > Do you mind walking me through the code how that could have happened? W= e > have spi_register_controller() call of_spi_get_gpio_numbers() which has= > the following: > > ctlr->num_chipselect =3D max_t(int, nb, ctlr->num_chipselect); > > such that what the controller has is the maximum between the number of > 'cs-gpios' properties parsed and what was already populated in > ctrl->num_chipselect during bcm2835_spi_probe(), which for this driver > is BCM2835_SPI_NUM_CS (3). If you make a initial device tree (or add overlay in the rpi's=20 config.txt) with more on the bus than BCM2835_SPI_NUM_CS (in my case 8 devices), you get into this trampling memory state. As the devices are added, once the chip_select is equal to or greater than BCM2835_SPI_NUM_CS, it's writing off the end of the arrays. There is no protection from this happening. By the looks of it, this isn't the only driver this could happen with, but it is the one I have hardware for to test. There are also drivers that look like they don't have a problem going well beyond the limit they gave. There is protection in spi_add_device, which will catch extra added later, but not ones in the device tree when the spi controller was registered. >> This patches inforces CS limit but sets that limit to the max of the >> default limit and what is in the device tree when driver is loaded. >> >> Signed-off-by: Joe Burmeister > You have changed many more things that just enforcing a limit on > BCM2835_SPI_NUM_CS you have now made all chip-select related data > structuresd dynamically allocated and you have changed a number of > prints to use the shorthand "dev" instead of &pdev->dev. The change to dynamic allocated arrays is just to support what is given in the device=C2=A0 tree rather than increase and enforce the CS limit ju= st for my case. The shorthand is of course not required. I'll drop it on resubmitting. Regards, Joe