Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4443835pxy; Tue, 27 Apr 2021 05:21:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy0EZKqQ+lfomxcuVGu71+QhGVtlS/3v4U3hP8LBgd4l8iYu2Z3eIvRDiuYeO//xLVjZNxv X-Received: by 2002:a05:6402:5244:: with SMTP id t4mr4059234edd.355.1619526114391; Tue, 27 Apr 2021 05:21:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619526114; cv=none; d=google.com; s=arc-20160816; b=hEykPuAaaEF3hKp2SY3lvI/g9l3w0l95dS97NcSANBpRzycZtSi+F1RLwixZATJzpV E/kJlva/TOdK5QVg8tJulkDQBabBSV23zn/OWbqWPKz8DJYab0XTc7fsf/iLrua5jDhd 3UuRbak39H1OeOaaTu/PRMRF7o3cWRVn6gzdxyUOfuGSdqgF2aTee8+UHOP3+HyrZG9/ Okv84X5rob3cFW3EPhPWjzPwXX/TRdd2nywEqBaBhSuaYZsuIR1CLAGqWmV6Vj7+G+M9 PjA/sPHF5jyvRUfOC/19ynCeUiNphhQnV7Dma4U732ld6nJfSKJ3XZRnRNDgWwbp6aV+ LLBg== 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=PJtYalXjm047Wz0uI3zIc/d9VK7gF5/494NH8V6lk1g=; b=cP1h0LbQrRoS9lSiyjt/Ie+ZUeD4ucUwamd+l40Lhwm7DceGl9525q10Thi6sk+P2I F6OWpkuMRDx68juzxBTe1rY8nhlA4Pwfn9roWQBfq0FnltpDbB1TflQFnPiooEn1vZJR 1qO5Dui5KSwrQTsBvQAtIxNpGsAWvsjI0nbjTDhBJqx1qwfVnf86uvYhsJEaIWkMd5Nm K3qOP8aZNreaSYUR3cFfD9C7cz/Rv68WYgxVqNo8Pm+vZCa8I64/yJK9XSdGauLYW5S6 P8jukpYpeDJANCGlBjw5382pkgiY7WzxRogGeWqcX5Rm+iGRXte4E55jelINSiYy+6a3 4qqA== 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 jz13si15608329ejc.634.2021.04.27.05.21.26; Tue, 27 Apr 2021 05:21:54 -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 S236139AbhD0MVL (ORCPT + 99 others); Tue, 27 Apr 2021 08:21:11 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:33830 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235638AbhD0MVK (ORCPT ); Tue, 27 Apr 2021 08:21:10 -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 1lbMhW-0004Z2-Ll; Tue, 27 Apr 2021 12:20:26 +0000 Subject: Re: [PATCH][next][V2] gpio: sim: Fix dereference of free'd pointer config To: Andy Shevchenko Cc: Linus Walleij , Bartosz Golaszewski , "open list:GPIO SUBSYSTEM" , kernel-janitors , Linux Kernel Mailing List References: <20210427114950.12739-1-colin.king@canonical.com> From: Colin Ian King Message-ID: Date: Tue, 27 Apr 2021 13:20:26 +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 13:11, Andy Shevchenko wrote: > On Tue, Apr 27, 2021 at 2:49 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. > > Thanks! > I'm wondering how I missed this... Nevertheless > > Reviewed-by: Andy Shevchenko > >> Addresses-Coverity: ("Read from pointer aftyer free") > > after > Can that be fixed before applying rather me sending a V3? >> Fixes: a49d14276ac4 ("gpio: sim: allocate IDA numbers earlier") >> Signed-off-by: Colin Ian King >> --- >> V2: Don't make id local to the if statement to improve coding style. >> Thanks to Bartosz Golaszewski for this improvement suggestion. >> --- >> drivers/gpio/gpio-sim.c | 8 +++++--- >> 1 file changed, 5 insertions(+), 3 deletions(-) >> >> 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; >> -- >> 2.30.2 >> > >