Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp4177712pxp; Tue, 15 Mar 2022 14:31:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwOcupIsNdCPyJetYwLd3fiES0yAAejCR5uVeyz1KD1Fjyb6KNXlDVNv4drQYhulbmecx+l X-Received: by 2002:a63:cf0c:0:b0:380:fb66:fa2a with SMTP id j12-20020a63cf0c000000b00380fb66fa2amr22497745pgg.273.1647379871559; Tue, 15 Mar 2022 14:31:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647379871; cv=none; d=google.com; s=arc-20160816; b=S+MC2gTIzMhE0EDl3e49XDNWeL0ECAz/xKcaPYzSDDHYCANgaFPyYij8BYNifLegRa Fkx2V05cCtAbqG7Fnltzhrofw5fPylClomaboPi0Ulc/1N9ahkHXNNac2EJHbMW7Z/QT t3If7FIPWlB029SKHffwJ2KepgD2bGThXAi/rozK3sEpK7QSHONYJbvipFxxXMrC4QK1 thBPjvsInW+jmeW455hZqM7AQILRYSAcQE6/o7Rj+Hc1v/5o94LnOuglcsRfSND+3Yfy f7mRbR21JhbODwDCwn3UnoV5TLK9GZDtEtg2JDNoc8o/gn4Yf4AzSEVG5yKsRlLf21S7 54/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=hTLs9f3nNHYj6AGLbF8Rys9KIyAVonoboV6lM0c+s90=; b=KZ3IWk2lSfcI8FGR11dI7jbFW/OUIU3ZWq7z8yhsmotMAo22kR9otjde0MRcsKTwTu DcMqwRrZecSdvK9hpg8FI5vt+SdTCk8GOWHIhFReqxVtCvF96o4gsseiLIgPaQnHkrcm O0tySyypkLaJ2QJK0y+vyKCnHeepj9xVw2mWhNn68q16fGQ3/VQKpexMFBrkdeW8TREm xeZIrqJxGArM4GzleMl5ik36FQSAwSGUfB3IzUNHtc/y0w8wqUBjV/n2k3OJnV0dcilw ijhl6x/Gp2O+86udTSkyiJSGPuW9CuytzYsu+OB2eLB9zuJKkY45pgrTPvb+EeC3WypX b+hA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=YFtQr2t9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f16-20020a056a001ad000b004f7ba8b4428si297085pfv.16.2022.03.15.14.30.57; Tue, 15 Mar 2022 14:31:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=YFtQr2t9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347152AbiCOKkJ (ORCPT + 99 others); Tue, 15 Mar 2022 06:40:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233588AbiCOKkJ (ORCPT ); Tue, 15 Mar 2022 06:40:09 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B899040E65; Tue, 15 Mar 2022 03:38:53 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: shreeya) with ESMTPSA id C63431F41BCA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1647340732; bh=0Ayx2ck3KklerDtSR4xP/Yhxts6NZBaufUlBtW0oRvU=; h=From:To:Cc:Subject:Date:From; b=YFtQr2t9RxvdrDk6j5Dk4gPKK0fkc9d1H4DaHAl7U931gjBv+F/sZRPwpfJA6PTnP vYb80IkieuQo7s+DsNr4a3A9V44Xf2S2Ksigkthe4geF4A2y8ncxcRuA8xvCz7CzkR VKXwwAqD9tnxgE4b5pYrUlGv4adqaNQFwziH9smQ3ZEOxlrytSBEaK6y/kXk4Sww8O eT1zps2TlL+lfvc3aelsG2M3464T5EWu/wkqXVueEljxq+us34muafVNVQjkN2wrLu erxAAIk8URm3cTW8HVGOzP1tSpvBIlrUYuKrwiklyqlNJRtpnKHLKtF2Z5e/xYHxjt IWtCkTb6lCVdg== From: Shreeya Patel To: linus.walleij@linaro.org, brgl@bgdev.pl, krisman@collabora.com, andy.shevchenko@gmail.com Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, kernel@collabora.com, Shreeya Patel Subject: [PATCH v2] gpio: Restrict usage of gc irq members before initialization Date: Tue, 15 Mar 2022 16:08:13 +0530 Message-Id: <20220315103813.84407-1-shreeya.patel@collabora.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org gc irq members are exposed before they could be completely initialized and this leads to race conditions. One such issue was observed for the gc->irq.domain variable which was accessed through the I2C interface in gpiochip_to_irq() before it could be initialized by gpiochip_add_irqchip(). This resulted in Kernel NULL pointer dereference. To avoid such scenarios, restrict usage of gc irq members before they are completely initialized. Signed-off-by: Shreeya Patel --- Changes in v2 :- - Make gc_irq_initialized flag a member of gpio_irq_chip structure. - Make use of barrier() to avoid reordering of flag initialization before other gc irq members are initialized. drivers/gpio/gpiolib.c | 11 ++++++++++- include/linux/gpio/driver.h | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index defb7c464b87..3973146736a1 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -1593,6 +1593,15 @@ static int gpiochip_add_irqchip(struct gpio_chip *gc, acpi_gpiochip_request_interrupts(gc); + /* + * Using barrier() here to prevent compiler from reordering + * gc->irq.gc_irq_initialized before initialization of above + * gc irq members. + */ + barrier(); + + gc->irq.gc_irq_initialized = true; + return 0; } @@ -3138,7 +3147,7 @@ int gpiod_to_irq(const struct gpio_desc *desc) gc = desc->gdev->chip; offset = gpio_chip_hwgpio(desc); - if (gc->to_irq) { + if (gc->to_irq && gc->irq.gc_irq_initialized) { int retirq = gc->to_irq(gc, offset); /* Zero means NO_IRQ */ diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h index b0728c8ad90c..e93de63feece 100644 --- a/include/linux/gpio/driver.h +++ b/include/linux/gpio/driver.h @@ -203,6 +203,15 @@ struct gpio_irq_chip { */ unsigned int *map; + /** + * @gc_irq_initialized: + * + * Flag to track gc irq member's initialization. + * This flag will make sure gc irq members are not used before + * they are initialized. + */ + bool gc_irq_initialized; + /** * @threaded: * -- 2.30.2