Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp835778pxb; Tue, 12 Apr 2022 14:49:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwMV6HczccKCuJ9ZhgymWpYEeohf+iOUmDaw96B4G+VRm7Pu4mc0hPvK6WHJ+lxNxhdTqVN X-Received: by 2002:a17:90b:3882:b0:1cd:41c1:712a with SMTP id mu2-20020a17090b388200b001cd41c1712amr2717505pjb.72.1649800191578; Tue, 12 Apr 2022 14:49:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649800191; cv=none; d=google.com; s=arc-20160816; b=vKnPxDwEhoE6tIqH2T19bHRpLxHNQAtwPxKZU0tVXcIHgZRG0CzjekSoEAYLfSitCE Ylwufs3EDDFV4aDU6iXWzU1UKqXfcjhkIRZ0AxbTkpl4qtxgcGgQY6jicrynQoi9OVdT EkG5smAYm3O8UYoYtjeaF/hgf3YY0/oUlMJNLtaaUE4P3BqIgjXJbLMG9EkFaaY3LLQr felFR5Ecfz5Ko1t1ImtMU2zphp35BuMPNa15DPjn/XeuFQavn0KyukzvoUMQOaaxmFvi Ivz6gT0mcfgPlnxbS9OqIWa9JE3kAg7g+VyaEGKxSbuxArwVmeViWDXW0wPOFxqHJOBD PIEQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=FdJOIniNbDkBiyZdJNOXTSNay6nVXxJitPKiNRVgjn8=; b=gxHRWTepY9Zd+FSZi40wMDSz9iOt5D4vkyM49HwgbLZ3xqHq7dm6hXaIw+C6JKTc0Z XFs9nw1N37/YobxHiZsiQc56yWs854dAiRGEsOTKqG9bYOS+9dNBp0ZwxxLypCjPBLQ1 skBJIVuO/Cat7Abn07+KDLMQe8B4pZ5tbvgkCajAp4NMAGhK2IeJGEtCJOrlpn7Xqxe0 LOJV2sLcY3WX3QlCy+PYhdLNwlHAU+1Stsw/Z8/enNN/aQOqhfYN543HNzxj+xGOo28f JG1nLblUlEJz/up2HwCfTNHZnUld7QRD+s7EDwzXZjoKSIYr4WxU+rhXtkU2K/3M9t9a 9neA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="RzTuy/a3"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id w4-20020a056a0014c400b004fa849f7ed6si13127974pfu.243.2022.04.12.14.49.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 14:49:51 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="RzTuy/a3"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 30EAD11DD23; Tue, 12 Apr 2022 13:47:56 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382951AbiDLIex (ORCPT + 99 others); Tue, 12 Apr 2022 04:34:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353862AbiDLHZ4 (ORCPT ); Tue, 12 Apr 2022 03:25:56 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E6D2E09D; Tue, 12 Apr 2022 00:05:01 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 369F260B65; Tue, 12 Apr 2022 07:05:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3F800C385A6; Tue, 12 Apr 2022 07:05:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649747100; bh=nixB3c9ysBwrhtGSDitgq3s8EgiWl03CMJTTQJF6RHg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RzTuy/a36OJjqBruwFtNHqNs9zfQF1y9T3aW04XE6G/UQMAYRmixDpzwnYV/Rn6dI ET3NmKr0BTY2oZanMYuGAlwxpdVw3RxMhGWfI0seaC7zay8z9loaKbwfzDi2JpZAZq /Bedhnpl816FDr0a9u1fd2mZZdkkx0wpV7iK0AJk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Shreeya Patel , Andy Shevchenko , Linus Walleij , Bartosz Golaszewski Subject: [PATCH 5.16 241/285] gpio: Restrict usage of GPIO chip irq members before initialization Date: Tue, 12 Apr 2022 08:31:38 +0200 Message-Id: <20220412062950.617893187@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220412062943.670770901@linuxfoundation.org> References: <20220412062943.670770901@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 From: Shreeya Patel commit 5467801f1fcbdc46bc7298a84dbf3ca1ff2a7320 upstream. GPIO chip 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. Following are the logs for reference :- kernel: Call Trace: kernel: gpiod_to_irq+0x53/0x70 kernel: acpi_dev_gpio_irq_get_by+0x113/0x1f0 kernel: i2c_acpi_get_irq+0xc0/0xd0 kernel: i2c_device_probe+0x28a/0x2a0 kernel: really_probe+0xf2/0x460 kernel: RIP: 0010:gpiochip_to_irq+0x47/0xc0 To avoid such scenarios, restrict usage of GPIO chip irq members before they are completely initialized. Signed-off-by: Shreeya Patel Cc: stable@vger.kernel.org Reviewed-by: Andy Shevchenko Reviewed-by: Linus Walleij Signed-off-by: Bartosz Golaszewski Signed-off-by: Greg Kroah-Hartman --- drivers/gpio/gpiolib.c | 19 +++++++++++++++++++ include/linux/gpio/driver.h | 9 +++++++++ 2 files changed, 28 insertions(+) --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -1368,6 +1368,16 @@ static int gpiochip_to_irq(struct gpio_c { struct irq_domain *domain = gc->irq.domain; +#ifdef CONFIG_GPIOLIB_IRQCHIP + /* + * Avoid race condition with other code, which tries to lookup + * an IRQ before the irqchip has been properly registered, + * i.e. while gpiochip is still being brought up. + */ + if (!gc->irq.initialized) + return -EPROBE_DEFER; +#endif + if (!gpiochip_irqchip_irq_valid(gc, offset)) return -ENXIO; @@ -1557,6 +1567,15 @@ static int gpiochip_add_irqchip(struct g acpi_gpiochip_request_interrupts(gc); + /* + * Using barrier() here to prevent compiler from reordering + * gc->irq.initialized before initialization of above + * GPIO chip irq members. + */ + barrier(); + + gc->irq.initialized = true; + return 0; } --- a/include/linux/gpio/driver.h +++ b/include/linux/gpio/driver.h @@ -219,6 +219,15 @@ struct gpio_irq_chip { bool per_parent_data; /** + * @initialized: + * + * Flag to track GPIO chip irq member's initialization. + * This flag will make sure GPIO chip irq members are not used + * before they are initialized. + */ + bool initialized; + + /** * @init_hw: optional routine to initialize hardware before * an IRQ chip will be added. This is quite useful when * a particular driver wants to clear IRQ related registers