Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp3016594pxy; Mon, 3 May 2021 13:07:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzD9yM5hIxIemd2JUniyv2wqnXRfGUWJFjkaJe5skfMFIw2b0nWTmLgZJBcs5XInWGkzaVO X-Received: by 2002:a65:4c89:: with SMTP id m9mr19559224pgt.330.1620072457166; Mon, 03 May 2021 13:07:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620072457; cv=none; d=google.com; s=arc-20160816; b=SfYul3MJ4LJfgpzWOdznxXWMFjPgCAH2wJI3Fo/3S8I2FyAbcD7J+csrf2n2/6MjuH 8CCClsPW3PwZVmuhF1vzK2TIBzOUuS+nVJKVaZ7gXrMYQIkfQO7FXBO4y54fLgrTmyXa BwGXAPhZOF8rnVkSDFc1hJv0kANm4PMu8MaUCitm/OxU9I0TDbMJUJrSi41/Fof9rQSc zEjPc7ZJiNGGY3e2mhU3tFS2NZBmR096RdZVLt08uIbEpms20MFDnAXkMDsoIAZS6pzY I9ae4M55Up0WUY6xd8S9jTvWTZYFAXtQ6OpWUX18x+BCA25qUsyhxfv/DY/12RWZ4Hdm 4CSQ== 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:dkim-signature; bh=F2io10OAL67cb7EHMlJRCzofAP/Qs75/sPOGNK5/d6o=; b=TT863LpgtbC6gLsiLTJ1VzgiEbTlkOBCxSeOmemTibxBdseH7O3X2A4+GXVipLZWP4 OTNdYbhlcTgnFKtjnUladQASKeeNF+C14GrwRIXpeAfjYXOq01lGpHIpQfNvdT3Yt4C5 tN3AgBnjgkk5xRnD0JiWoafNtnzn0cuX8MBMsLpxrImThNUd80xYMDT4mPQ3bhIWoceV MBZ9RM1nDFHbmi3oRdgF9nkKB8bsX2dEFK2qWYBOm6NcFInC5FIpX2qRfyRAC2lJC8Gb UMyF2fcCsj5lzr6J8Y+0W2hngzwAAE/sBQyymNAlqv+BpXovYGE2Fj3zDNImkvmeyIu4 t2xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=google header.b=fJkMqqW4; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p8si14013179pjm.141.2021.05.03.13.07.22; Mon, 03 May 2021 13:07:37 -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; dkim=pass header.i=@linuxfoundation.org header.s=google header.b=fJkMqqW4; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229576AbhECUHG (ORCPT + 99 others); Mon, 3 May 2021 16:07:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229497AbhECUHG (ORCPT ); Mon, 3 May 2021 16:07:06 -0400 Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65A1CC061761 for ; Mon, 3 May 2021 13:06:12 -0700 (PDT) Received: by mail-qk1-x734.google.com with SMTP id a2so6378287qkh.11 for ; Mon, 03 May 2021 13:06:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=F2io10OAL67cb7EHMlJRCzofAP/Qs75/sPOGNK5/d6o=; b=fJkMqqW4BsgYDoQdC+rpvXd6PP2Kwhd5l1y08IonEtTtWULi3c3M0PEaE7Rb0GZlqu xBpMeYNhIBZHSp1kAeDUx0ErAB+UL6ryjm6iWnCkZhNsaquj2XYfvIM4XVDQL3EVWNW6 tu1iGpsGZo8xVPCmEF6ISJSawjhUXdcJm7hU8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=F2io10OAL67cb7EHMlJRCzofAP/Qs75/sPOGNK5/d6o=; b=XQYP8AeO6sD6s4TKiBGrwVacYyzrYi5MNnjXuSd0DaSgypfs5otmQljVrbA0B94OjZ /3z0W+q2NruquRC5IUCYYtRPRsZWSd34H9RPd5d6AwDxE1p21le4I3LNmSNiaUYjBRoW QKnOvnGdox1YDuDgojF7hiUa+wcSZ7whWCyDC5pz1bYUQsKqZiyABUyceb8N1xu4eaUt zGPIRCLjCMBVRczC0uCsizILIneIgMoCrsODQvHF4SrCi5d4kSRtleCSzNEWrbM9B6Hz 282pAUoYSGJNlxS9sAkc92WUSpnp0yymu+ZR3mgSR/9D7FDKEik1q6zNMTwx3oMne5Nr Y0Jg== X-Gm-Message-State: AOAM531w9ABpDjYTwFriOjtX2CfwMW/8PLi78lTcK0jRess53eQxmRmV BCw9DDQiyLRIGHRvGY+IGHugviynLZgq6g== X-Received: by 2002:a37:5945:: with SMTP id n66mr21057766qkb.138.1620072371604; Mon, 03 May 2021 13:06:11 -0700 (PDT) Received: from [192.168.151.33] (50-232-25-43-static.hfc.comcastbusiness.net. [50.232.25.43]) by smtp.gmail.com with ESMTPSA id l16sm720725qtr.65.2021.05.03.13.06.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 03 May 2021 13:06:11 -0700 (PDT) Subject: Re: [PATCH] media: em28xx: Fix possible memory leak of em28xx struct To: Igor Matheus Andrade Torrente , mchehab@kernel.org Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, hverkuil-cisco@xs4all.nl, Shuah Khan References: <20210503173716.21652-1-igormtorrente@gmail.com> From: Shuah Khan Message-ID: Date: Mon, 3 May 2021 16:06:10 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 In-Reply-To: <20210503173716.21652-1-igormtorrente@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Igor, On 5/3/21 11:37 AM, Igor Matheus Andrade Torrente wrote: > The em28xx struct kref isn't being decreased after an error in the > em28xx_ir_init, leading to a possible memory leak. > > A kref_put is added to the error handler code. > > Signed-off-by: Igor Matheus Andrade Torrente > --- > drivers/media/usb/em28xx/em28xx-input.c | 7 +++++-- > 1 file changed, 5 insertions(+), 2 deletions(-) > > diff --git a/drivers/media/usb/em28xx/em28xx-input.c b/drivers/media/usb/em28xx/em28xx-input.c > index 5aa15a7a49de..b89527014cad 100644 > --- a/drivers/media/usb/em28xx/em28xx-input.c > +++ b/drivers/media/usb/em28xx/em28xx-input.c > @@ -720,7 +720,8 @@ static int em28xx_ir_init(struct em28xx *dev) > dev->board.has_ir_i2c = 0; > dev_warn(&dev->intf->dev, > "No i2c IR remote control device found.\n"); > - return -ENODEV; > + err = -ENODEV; > + goto ref_put; This doesn't look right. em28xx_init_buttons() is already happened and em28xx_shutdown_buttons() needs to be done from fini. fini needs to run with this ref. If ref is released here, device might be released before em28xx_shutdown_buttons() can run leading to potential use-after-free > } > } > > @@ -735,7 +736,7 @@ static int em28xx_ir_init(struct em28xx *dev) > > ir = kzalloc(sizeof(*ir), GFP_KERNEL); > if (!ir) > - return -ENOMEM; > + goto ref_put; This doesn't look right. Same comment as above. fini accounts for null ir. em28xx_shutdown_buttons(dev); /* skip detach on non attached boards */ if (!ir) goto ref_put; > rc = rc_allocate_device(RC_DRIVER_SCANCODE); > if (!rc) > goto error; > @@ -839,6 +840,8 @@ static int em28xx_ir_init(struct em28xx *dev) > dev->ir = NULL; > rc_free_device(rc); > kfree(ir); > +ref_put: > + kref_put(&dev->ref, em28xx_free_device); > return err; > } > > thanks, -- Shuah