Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4421199pxy; Tue, 27 Apr 2021 04:51:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw7X3gyN7I9+B4wO+D3N0dfDqbWPipFLAyVvYMj9mWWGtJAD8Z0UkpdyjX2EdzhR2EnkSXt X-Received: by 2002:a63:5d18:: with SMTP id r24mr22127644pgb.94.1619524295994; Tue, 27 Apr 2021 04:51:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619524295; cv=none; d=google.com; s=arc-20160816; b=hgguAmiy6u1LL69vPGWh+rp4207iJVOoH7mvBlYxROLztYZjaT+34XcQt72RH1vHom xr/0EQH1D0AsRtOcvh9y7apl0oDmTW6uWexBE8ONElMZGEs/U7+ZiGhCDtYcBFSPl9kB USpC0c59idPOsU/NEMXBGbo8Y1IjjsMTedzhsM51SQldUZqSKtHDt1RtFVIeAgM67++F OCOOrtkTQZ0hS9KiruWoXRRzXqm6d21Rv7BrnwcF/kTgUyumJXV2k+/EwpHmU0glsgTr R7tLleGHv1fwMH85quFC7n0Yg8xpV/zGCcWio/5s69fQjF13QV7mZZU98gPmQBmSDNlt TicQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=1igDY1snPXXxf81G/U0LWuxQbc8c74E+IZPTx5MK4hA=; b=CwgQXq/i+pBdGif3Dyr5jOBhLJ9X9CQ6ilDIf8+cm3scfUGxiYqbeKGqOsojE6P+JY XEnCYQEbUlDf15nFAzh409QHMRHAPsBuiUX+pKgVD85LFN6yefKDNjFzAZZIbLbAzFR6 6KxM4Llub1HTemHqUcC1WcvKEq2P+m70nf/tRoXs50cisip46n8sFhBPZOkBiM6lNtMZ zDRlMaAO4FeLI3eictgGmZufm5GkJ3/ovt6nwbWWqY+zlLErUezmR3zEn1BCcnw8ttYM W61paPU+Mn3ThlXPNcsqZN8qnAVT84B78BN9WK8pNLNjt5wuv1dygWt3dJYrUtqeLI9n 0Ahg== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m12si2963828pjr.61.2021.04.27.04.51.23; Tue, 27 Apr 2021 04:51:35 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236123AbhD0Lu7 (ORCPT + 99 others); Tue, 27 Apr 2021 07:50:59 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:32785 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230365AbhD0Lu6 (ORCPT ); Tue, 27 Apr 2021 07:50:58 -0400 Received: from 1.general.cking.uk.vpn ([10.172.193.212]) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1lbMEI-0001uB-Sh; Tue, 27 Apr 2021 11:50:14 +0000 Subject: Re: [PATCH][next] gpio: sim: Fix dereference of free'd pointer config To: Bartosz Golaszewski Cc: Linus Walleij , Andy Shevchenko , linux-gpio , kernel-janitors@vger.kernel.org, LKML References: <20210427102427.11066-1-colin.king@canonical.com> From: Colin Ian King Message-ID: Date: Tue, 27 Apr 2021 12:50:14 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 27/04/2021 12:33, Bartosz Golaszewski wrote: > On Tue, Apr 27, 2021 at 12:24 PM Colin King wrote: >> >> From: Colin Ian King >> >> The error return of config->id dereferences the kfree'd object config. >> Fix this by using a temporary variable for the id to avoid this issue. >> >> Addresses-Coverity: ("Read from pointer aftyer free") >> Fixes: a49d14276ac4 ("gpio: sim: allocate IDA numbers earlier") >> Signed-off-by: Colin Ian King >> --- >> drivers/gpio/gpio-sim.c | 4 +++- >> 1 file changed, 3 insertions(+), 1 deletion(-) >> >> diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c >> index 2e2e6399e453..7bba5783a043 100644 >> --- a/drivers/gpio/gpio-sim.c >> +++ b/drivers/gpio/gpio-sim.c >> @@ -751,8 +751,10 @@ gpio_sim_config_make_item(struct config_group *group, const char *name) >> >> config->id = ida_alloc(&gpio_sim_ida, GFP_KERNEL); >> if (config->id < 0) { >> + int id = config->id; >> + >> kfree(config); >> - return ERR_PTR(config->id); >> + return ERR_PTR(id); >> } >> >> config_item_init_type_name(&config->item, name, >> -- >> 2.30.2 >> > > Thanks! Can you do something like this: > > diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c > index 2e2e6399e453..b21541c0b700 100644 > --- a/drivers/gpio/gpio-sim.c > +++ b/drivers/gpio/gpio-sim.c > @@ -744,20 +744,22 @@ static struct config_item * > gpio_sim_config_make_item(struct config_group *group, const char *name) > { > struct gpio_sim_chip_config *config; > + int id; > > config = kzalloc(sizeof(*config), GFP_KERNEL); > if (!config) > return ERR_PTR(-ENOMEM); > > - config->id = ida_alloc(&gpio_sim_ida, GFP_KERNEL); > - if (config->id < 0) { > + id = ida_alloc(&gpio_sim_ida, GFP_KERNEL); > + if (id < 0) { > kfree(config); > - return ERR_PTR(config->id); > + return ERR_PTR(id); > } > > config_item_init_type_name(&config->item, name, > &gpio_sim_chip_config_type); > config->num_lines = 1; > + config->id = id; > mutex_init(&config->lock); > > return &config->item; > > > I think this looks more elegant without the local variable inside the if. OK, good idea, V2 sent. > > Bart >