Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp2589924lqz; Wed, 3 Apr 2024 02:42:56 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX/U1XRbPunYJ+dhWftYdLnhm3M4hsOkJ6Zh41cdDgJM0RGsFTFgA+3/mp35GpgVvpvc0I7c8P6ITAJJ7+o1zthyMHW5usn8GwNG5srAw== X-Google-Smtp-Source: AGHT+IHXbakcSsGZjRCVjuTkdh41aiIWriYVIvhcv39s8kMby2aj2kin0d6znwjAqiURQ9LQpZWa X-Received: by 2002:a17:906:3652:b0:a4e:96fa:e1b2 with SMTP id r18-20020a170906365200b00a4e96fae1b2mr1937053ejb.53.1712137376637; Wed, 03 Apr 2024 02:42:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712137376; cv=pass; d=google.com; s=arc-20160816; b=hQ8Gg8+DFns76LMm8jwmv0jFyXSO6I2sJiyGisC1G9k0qXHiHV+eTZnLp4epaKZ/7M bh2pmQPia6uvZwP7i9Y7aU294BuOcPdQublZvU10DEoTKv1qgWN6E/GlsJlti+WdgyjF Q8CEppxLO8ndK+hK0p00ViUpVLfxbkD1zvaDOtWOM5It/5JUa3MfRE+XwhTXE5e7YYY8 CSP2BriQvmytwVhoKaafo6HFQH8Ud7xuw1llXBH9B2FfavDF3B0dknLQBpWZQhuDBhES PsieiB7JFrKRG30cJFiH6Q0DPcJ46J5HPE2LWSNgCOZYfH9NADbuQk8THQQP6JCuprs6 mzNQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=7bU382ztyspHGb0aCvHLVuPZy5Tv25ydMKByJLrVwNE=; fh=e6yY4SSRKOsOeDV6OSy7zCm7w/vgCcby03oiRudCJus=; b=xKIU1AgzUtTSijjo97FFP1HRNWBjimu+nJiRNy068P35teeTg3JB7ubmKRM93R/4d9 XD63Py+yz1fQ8q3Ybi/vbQ1GUfYmFvPQ7Zm+PwNeiwrTxPi4bOZT3fPsUeS8+tTa3Fv0 tPIDzPkSAYOKtxY+Fiz05X4uw4fqz2WD4mcOuqkDpIjcev19T5WiBZYB3p5krQhEYWMn Fj/A/u6qnkyV9I7rUDgnnJ4bnR7mrBu30vbp1r50tCKEz+MDGjDoCa/11StMSFnp985J h6N/LLtx9aKvfnLaMtepElyh0K7OxOEeXStCGi1W6ga0fSqOtag++d3X2gmUPrj53VIY KmIA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="HnCkfYt/"; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-129443-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-129443-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id lv21-20020a170906bc9500b00a4def184a37si6499050ejb.732.2024.04.03.02.42.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 02:42:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-129443-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="HnCkfYt/"; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-129443-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-129443-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 2F4D71F2324C for ; Wed, 3 Apr 2024 09:42:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CA0B6134CC8; Wed, 3 Apr 2024 09:42:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HnCkfYt/" Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 841696EB73; Wed, 3 Apr 2024 09:42:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712137333; cv=none; b=DDLSJBviuJxlvRyOMF2H3EALepJKnigTFVZAjW5/lqbiMTw3+Tbjsnbeo/JbkABQO4ubfoU12FXcjSsNuDlfjFri5XxdfCzGOF9UVnGQAXLhLI3RJox83vTx4JseT+zcL3twGgNMrqiun3AEuz0zL3LuYTd09kWFj085Isteoz8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712137333; c=relaxed/simple; bh=+ej/TWizrI+era82uq7nCvFBl8kRFoOo2CvsTpNzVDI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=VA4p8+lwHhBOSCLPDkCMUf8AFi0KQTYxBp5t9HGNKDiYSVWiraU6Cfli4NyLHH2iTGmj21zYW4p9kGI+JNfnQzdUD3hAFmPdeXdbJ0rgSpKWhMFiHb9HpOjWmafHSFSMs8v8kdEVnUm2obPi01+Blx+KZH2yJyB7gaE2j7+Xs+A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=HnCkfYt/; arc=none smtp.client-ip=209.85.215.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-5d4d15ec7c5so4698703a12.1; Wed, 03 Apr 2024 02:42:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712137332; x=1712742132; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=7bU382ztyspHGb0aCvHLVuPZy5Tv25ydMKByJLrVwNE=; b=HnCkfYt/yWdxFNjR31alY6fCzVMEjEyGcKqbUZ2a2GloxUtelcZ1HihxocK6M35hX+ p95yiFhkIwcloXbY78u9xqDoUWDKCnBqrbptUPKgR1mYKrdZjTa/t7IkvEjIMOZf+WjQ g1a0i2hGiXrVNAbmITF/wtfVP9LlXFq72LKwcH9o9wAMSl9M/cgujxrZv7/qELiwtb7E OBmDNcHuHnJ18kP1nzp9WUx4SZErdg9XmdW+cJKCBuuK5dkbG8rBEdXFLZP4aJxLs1hF CJD0F+AxnXWBbxH7AQ2mBmeHgBtJ0eR4PgF5iWloCLi+VB/0/DuCQLkvI4Kkr1d9fM7O Nt+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712137332; x=1712742132; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=7bU382ztyspHGb0aCvHLVuPZy5Tv25ydMKByJLrVwNE=; b=ZqeYrhNdHdPcOmxbt8tu/sdUIoeLxL7IG3aV2h+x/JoiQIaTQZk3Yp5jd3dhQPnKuP MyoqXbiMsiS4lHsuX9N2XSW7ReXp1DsPhJpHGnIoqybhBjyYAazL1NcTZdCjXVnAdnjU T5MGbfxxs5Ax05kJJJEY7u/zU+oV3znMA2UHKhXCr1fw/HhKMjsi/lMnj6kW71JnGIIf oi0cz3BD8cFlBuPWequq8FMX6rg1ZWQAc6jaJ2yXzf3rBmSZhhFR3RG5qp4SWdQkSkC/ TLoMrV70w4pm2g5OH+DmeCfi3mqy0p/ZM5JID1GeZbQcn7PoSB8mtc7ydpRW+PzndMVJ MSTw== X-Forwarded-Encrypted: i=1; AJvYcCUVvpxjEU4lobwDh4MG4wI+OQRrivRIvDaIe4qr8KYi6IimUHgIEg+SNA6rzxmuRv5SdghG78ZfNBDKOLYwHK5c6rCsg8V46Vs3jOoi97D/2sJYtwM8Ooi3sYP8yHXtrWPZn2AnfzcleN/W5/p54+/wLnX3CIeWPLSX/Pidxf99EQ== X-Gm-Message-State: AOJu0YyGMX4R1UpimGFm3IB8wjdO8vcjzV7cEipCKvzvZVabxvsCSvvH iK27l0MyzbJQQZmAY7e1X+FGsZa8AtTNY366TWFSpiwet+LyHJJr X-Received: by 2002:a05:6a20:d498:b0:1a7:1695:2c19 with SMTP id im24-20020a056a20d49800b001a716952c19mr8355503pzb.33.1712137331716; Wed, 03 Apr 2024 02:42:11 -0700 (PDT) Received: from rigel (194-223-186-215.tpgi.com.au. [194.223.186.215]) by smtp.gmail.com with ESMTPSA id v65-20020a626144000000b006e6854d45afsm12072926pfb.97.2024.04.03.02.42.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 02:42:11 -0700 (PDT) Date: Wed, 3 Apr 2024 17:42:05 +0800 From: Kent Gibson To: Bartosz Golaszewski Cc: Linus Walleij , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Alexey Dobriyan , stable@vger.kernel.org, Stefan Wahren , Naresh Kamboju , Bartosz Golaszewski , Linux Kernel Functional Testing Subject: Re: [PATCH] gpio: cdev: check for NULL labels when sanitizing them for irqs Message-ID: <20240403094205.GA158151@rigel> References: <20240402114159.32920-1-brgl@bgdev.pl> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240402114159.32920-1-brgl@bgdev.pl> On Tue, Apr 02, 2024 at 01:41:59PM +0200, Bartosz Golaszewski wrote: > From: Bartosz Golaszewski > > We need to take into account that a line's consumer label may be NULL > and not try to kstrdup() it in that case but rather pass the NULL > pointer up the stack to the interrupt request function. > > To that end: let make_irq_label() return NULL as a valid return value > and use ERR_PTR() instead to signal an allocation failure to callers. > > Cc: stable@vger.kernel.org > Fixes: b34490879baa ("gpio: cdev: sanitize the label before requesting the interrupt") > Reported-by: Linux Kernel Functional Testing > Closes: https://lore.kernel.org/lkml/20240402093534.212283-1-naresh.kamboju@linaro.org/ > Signed-off-by: Bartosz Golaszewski > --- > drivers/gpio/gpiolib-cdev.c | 19 ++++++++++++++----- > 1 file changed, 14 insertions(+), 5 deletions(-) > > diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c > index fa9635610251..1426cc1c4a28 100644 > --- a/drivers/gpio/gpiolib-cdev.c > +++ b/drivers/gpio/gpiolib-cdev.c > @@ -1085,7 +1085,16 @@ static u32 gpio_v2_line_config_debounce_period(struct gpio_v2_line_config *lc, > > static inline char *make_irq_label(const char *orig) > { > - return kstrdup_and_replace(orig, '/', ':', GFP_KERNEL); > + char *new; > + > + if (!orig) > + return NULL; > + > + new = kstrdup_and_replace(orig, '/', ':', GFP_KERNEL); > + if (!new) > + return ERR_PTR(-ENOMEM); > + > + return new; > } > > static inline void free_irq_label(const char *label) > @@ -1158,8 +1167,8 @@ static int edge_detector_setup(struct line *line, > irqflags |= IRQF_ONESHOT; > > label = make_irq_label(line->req->label); > - if (!label) > - return -ENOMEM; > + if (IS_ERR(label)) > + return PTR_ERR(label); > > /* Request a thread to read the events */ > ret = request_threaded_irq(irq, edge_irq_handler, edge_irq_thread, > @@ -2217,8 +2226,8 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip) > goto out_free_le; > > label = make_irq_label(le->label); > - if (!label) { > - ret = -ENOMEM; > + if (IS_ERR(label)) { > + ret = PTR_ERR(label); > goto out_free_le; > } > It occurred to me that none of my tests cover this case, as they always request edges with the consumer set, so I added some and can confirm both the problem and the fix. In the process I found another bug - we overlooked setting up the irq label in debounce_setup() - the alternate path in edge_detector_setup() that performs sw debounce. That results in a double free of the req->label and memory corruption hilarity follows. I've got a patch for that - the unfortunate part being that debounce_setup() is earlier in the file than make_irq_label() and free_irq_label(). Those will need to be pushed earlier, so it is sure to conflict with this patch. How would you prefer to proceed? Cheers, Kent.