Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp2417879ybl; Thu, 29 Aug 2019 07:56:30 -0700 (PDT) X-Google-Smtp-Source: APXvYqzxVBKIEBjLuliGLH5pmjRuX5YxbcgbipIzHUcNTLihw5QYow5Lz5DKSJw5MPfyP9h/PU61 X-Received: by 2002:a17:90a:e654:: with SMTP id ep20mr10434264pjb.38.1567090590581; Thu, 29 Aug 2019 07:56:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567090590; cv=none; d=google.com; s=arc-20160816; b=sA4uF7LlbJGjxZwEAm/69+08bpm/wqbIJeksbIUpNkTHNAl++4p6+Sh/clp0BIfcGi xXIXwHITNuQkTMrgNZY02VqCE+rwsv/Rc1oPzMAP+RHgmpbJ4RYe4oTRcZKMSJzJntph fpFpkh6fax+bjbB+BbFSdVf3QEITM4lL5qwAmE2VFQR2so0oIP1tyXm/1jr9KyOYv2jY X/4xa9y5RxlAP74vHMatwReFBP8r41/9roPu5lOdwCH9s3z128CTSvyIAW9j6jMHcF8S oBkZ4ObEIyaa45muY4VFqWvH22bmiepJz5Phs9dO42zipvaTvm1/H669NpHq5nQK9Yzd UE1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:in-reply-to :subject:cc:to:from:user-agent:references:dkim-signature; bh=rDl3XjmzWq1Bt/Mn9v6gfnxlgojJ+gdaOYg/Ia4k7aQ=; b=phcO1G8NQ5zp6Z7mvlNh+9zcWOXyFXMptBTr4TfDiAteYxuirOcUXGjgWNbHUOvZ61 5Wm5pfUJHtS/joI/CeuBShXscZWc+FScUo+OyAW7+LgargI2IWwF//KV2eDq+O8CK0ou fe0z9WrTEi3xXf9cP7mzzFhxGgUkOXb48tEpjtowuJxqBtMcg7u47p4gM4UBY0c8+neM KUg98BoPlvJ2QE8OTIEsFs0h4zU/4x/gPX37ZWgTJBf+iDcr0C86cpzNYBK2P3Qyk5/s lgdovvt0nywyVB6z0XCKYYLx9DbQ+tM5oKYM7XzM93D345FNsYJX6pEFoybsp81XrRgn bhDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RIGowJQe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t16si3028847pfq.88.2019.08.29.07.56.14; Thu, 29 Aug 2019 07:56:30 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RIGowJQe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727908AbfH2Oxa (ORCPT + 99 others); Thu, 29 Aug 2019 10:53:30 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:55558 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728886AbfH2Ox2 (ORCPT ); Thu, 29 Aug 2019 10:53:28 -0400 Received: by mail-wm1-f66.google.com with SMTP id g207so78360wmg.5; Thu, 29 Aug 2019 07:53:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:cc:subject:in-reply-to:date :message-id:mime-version; bh=rDl3XjmzWq1Bt/Mn9v6gfnxlgojJ+gdaOYg/Ia4k7aQ=; b=RIGowJQeoEjivCh/DN3Lv2KX6/nyX/A+Y8irhNkd+a2lHx7EikwzfiOIQpc48ZWGsV g5wMl+ixmo6o9IXMaEVS8OIRpDgQyTbGVVFHM+Cd0GCQ3du3BghkH1cjo2CVYzQvCdVS wKXJnGOIqGWwwopYDuFRNj5O6753hPd5yg88sX5gZ0QyO2C+1IaPBHxSNPhiGMiJoQe1 GQkrxVcVszqQaL7eAp84YmPWgfXQpip3fXWrLPn93tW5H+mrGVAqy0mFv7+4JV8+lkSp 1CaVU15mFTHAYctRFC35og30Sl/TyHwMTlELBMPRhkA6TmbDXsQnMcUeK791D8BYUQ/T ki+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:cc:subject :in-reply-to:date:message-id:mime-version; bh=rDl3XjmzWq1Bt/Mn9v6gfnxlgojJ+gdaOYg/Ia4k7aQ=; b=tN8iqYUySMhxfeBixaARbi8ym0klnDRln3q8ih+79VL0j3l0wgA/XOyZpapfMNnlnW pmJrvRWLZ7gMXX/+fGzBH5SQUo/rqMaK5p11KyXIJEgty7nGofGfH7rAaiYkV4OQWCCi C6CN+0zcvi1bSRrQch3iLKyphBMlTnPytK1F1nVy2tfGPVHyK+qylFVa6KQYKScg3aVB BMT6lTx+rb3tI4U1tibva1jlVVJY6qn+lsqIU0lS3z/ETz7lcMLJa9H+4AHv77LCpgJH SRp+08/Br00WUj4oinT4ilTODAmbWkhk/MDuoGnyG4+J0XzlFpjjr79m2Ut45zxgOEjk xOTQ== X-Gm-Message-State: APjAAAX/Yh25E69ymGEuoeXLQoDQ9C3lxrBSoUu1h2uhPcxST6l50QSu yF9Zr95/WvNhFGjGB1VAk6fRaPlw4WI= X-Received: by 2002:a1c:4e19:: with SMTP id g25mr6193427wmh.30.1567090405896; Thu, 29 Aug 2019 07:53:25 -0700 (PDT) Received: from arch-late ([87.196.81.67]) by smtp.gmail.com with ESMTPSA id e14sm1958252wma.37.2019.08.29.07.53.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Aug 2019 07:53:24 -0700 (PDT) References: <20190829122839.GA20116@mwanda> User-agent: mu4e 1.2.0; emacs 27.0.50 From: Rui Miguel Silva To: Dan Carpenter Cc: Johan Hovold , Alex Elder , Greg Kroah-Hartman , greybus-dev@lists.linaro.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org Subject: Re: [PATCH] staging: greybus: light: fix a couple double frees In-reply-to: <20190829122839.GA20116@mwanda> Date: Thu, 29 Aug 2019 15:53:20 +0100 Message-ID: MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Dan, On Thu 29 Aug 2019 at 13:28, Dan Carpenter wrote: > The problem is in gb_lights_request_handler(). If we get a request to > change the config then we release the light with gb_lights_light_release() > and re-allocated it. However, if the allocation fails part way through > then we call gb_lights_light_release() again. This can lead to a couple > different double frees where we haven't cleared out the original values: > > gb_lights_light_v4l2_unregister(light); > ... > kfree(light->channels); > kfree(light->name); > > I also made a small change to how we set "light->channels_count = 0;". > The original code handled this part fine and did not cause a use after > free but it was sort of complicated to read. > > Fixes: 2870b52bae4c ("greybus: lights: add lights implementation") > Signed-off-by: Dan Carpenter > Thanks so much for this, I was looking for some time at this and was half way to a much less elegant fix then yours. Acked-by: Rui Miguel Silva Cheers, Rui > --- > drivers/staging/greybus/light.c | 12 ++++++------ > 1 file changed, 6 insertions(+), 6 deletions(-) > > diff --git a/drivers/staging/greybus/light.c b/drivers/staging/greybus/light.c > index 010ae1e9c7fb..40680eaf3974 100644 > --- a/drivers/staging/greybus/light.c > +++ b/drivers/staging/greybus/light.c > @@ -1098,21 +1098,21 @@ static void gb_lights_channel_release(struct gb_channel *channel) > static void gb_lights_light_release(struct gb_light *light) > { > int i; > - int count; > > light->ready = false; > > - count = light->channels_count; > - > if (light->has_flash) > gb_lights_light_v4l2_unregister(light); > + light->has_flash = false; > > - for (i = 0; i < count; i++) { > + for (i = 0; i < light->channels_count; i++) > gb_lights_channel_release(&light->channels[i]); > - light->channels_count--; > - } > + light->channels_count = 0; > + > kfree(light->channels); > + light->channels = NULL; > kfree(light->name); > + light->name = NULL; > } > > static void gb_lights_release(struct gb_lights *glights)