Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3348172pxv; Sun, 4 Jul 2021 16:14:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz0U40zTqBapaufMKHY44jTb934Y/EVI6QgOw7Bv+IMfG1bQBHdA/mP5NacYe+qNEfeuXFq X-Received: by 2002:a92:bd0f:: with SMTP id c15mr8526215ile.229.1625440487812; Sun, 04 Jul 2021 16:14:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625440487; cv=none; d=google.com; s=arc-20160816; b=KQLvJ2ciB7teSgCy11skOJRraoQ9VTkUcJ0guSUEerP0NMETCMIc0EDuA2irV69uRU FwZ7+1hEfiKsLPAbT44S2gb4aPmOw2CVew39IHeiAA4WczX+won69xbK9emzALmiaLQZ Ha5AcIHFLj6NYw0yqz1+DDMLi4lBueGCn2t1b4DRfRPFikchDLZGNx146qR3rOkq96B/ wwVSq4DyH4RoJWXxJZNVa19Pgfb5cz8EWS3vZCftr5m+2kJ2mZGedudmKcr7qCDEMO4f 8qcei0zjEbZ+ej65spEXmBp2rAKtgPTi/YhXNI5nOLEAu1SW7LDTqyHgHCcTt2oJ9M+y UeXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=oCuWdLQrMGkp1oL/+2bf1NgSMjPqN2nk/RwFelz1WyA=; b=RMwYs09PqyXMbeqberErFzWZQl4RpEy04LlXSj/WbH1oaqQtc3I3scJtSxCxOKHtJk 2s2CBUeC9KLx8/CJ6jlwB3KLRJjT4FgbK0ZxHegqQFMo0dR8aKl9kc0g43xfb3Iu4reE 53BKj6MdNte+hcpvhTJtCMhQcEurd9TuR3zSjGu1bSe/eZqe4QZWdCrP/unhGKZQ7aAD h7rsY/aoG8FlyDndWYO6qNMIz0foU7TJmm7L0bzSJE8/V9DKHqxLw7e3xK3BvEicw4Jw ZgkyaNC3WYOrIJfOHbUtejfjnVvvaBCgmtLZA1+SPeq//CgTqCYEp5tDFf1zwzfuH3HG ls6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lNlUCAPd; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a1si12610017jat.32.2021.07.04.16.14.36; Sun, 04 Jul 2021 16:14:47 -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=@kernel.org header.s=k20201202 header.b=lNlUCAPd; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234266AbhGDXO6 (ORCPT + 99 others); Sun, 4 Jul 2021 19:14:58 -0400 Received: from mail.kernel.org ([198.145.29.99]:46804 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231864AbhGDXJ2 (ORCPT ); Sun, 4 Jul 2021 19:09:28 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5F26A613FC; Sun, 4 Jul 2021 23:06:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1625440012; bh=nP61IyDwMKPULtKhpyhNvfOHA4ogTEHHQM3vW2uT22k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lNlUCAPd6Tlb2z7HmcEST6sjh0jcETodIGIuRWyG48lTX5KABrl2hYJKHWPwhrg04 MEHPxYiyrFURFScC0vLSUvqp2rJObsGbnQmABivcA7BdV47K5fwvnTQ3QMm+BuDqY/ XT/5GtiCJp4nDN7pl9AwNUX0YYceQQTjnnihmGXVwkjPB69nKfLG43s1pudhDf3pgP j3D//YML79SF8/A21naUywK6rsKg/Iv1XabQ9LxB+xRnOEps68e6HxZkXJ1QxiK0hm BeFV4DWzOb0FdE84gtRtnxThbVoOQ6G9n0qy1s7GSPdvVOGKHdN0sOiiyGx1PahDkl P+liEH07lDjuQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Pavel Skripkin , syzbot+d1e69c888f0d3866ead4@syzkaller.appspotmail.com, Hans Verkuil , Mauro Carvalho Chehab , Sasha Levin , linux-media@vger.kernel.org Subject: [PATCH AUTOSEL 5.12 26/80] media: cpia2: fix memory leak in cpia2_usb_probe Date: Sun, 4 Jul 2021 19:05:22 -0400 Message-Id: <20210704230616.1489200-26-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210704230616.1489200-1-sashal@kernel.org> References: <20210704230616.1489200-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Pavel Skripkin [ Upstream commit be8656e62e9e791837b606a027802b504a945c97 ] syzbot reported leak in cpia2 usb driver. The problem was in invalid error handling. v4l2_device_register() is called in cpia2_init_camera_struct(), but all error cases after cpia2_init_camera_struct() did not call the v4l2_device_unregister() Reported-by: syzbot+d1e69c888f0d3866ead4@syzkaller.appspotmail.com Signed-off-by: Pavel Skripkin Signed-off-by: Hans Verkuil Signed-off-by: Mauro Carvalho Chehab Signed-off-by: Sasha Levin --- drivers/media/usb/cpia2/cpia2.h | 1 + drivers/media/usb/cpia2/cpia2_core.c | 12 ++++++++++++ drivers/media/usb/cpia2/cpia2_usb.c | 13 +++++++------ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/drivers/media/usb/cpia2/cpia2.h b/drivers/media/usb/cpia2/cpia2.h index 50835f5f7512..57b7f1ea68da 100644 --- a/drivers/media/usb/cpia2/cpia2.h +++ b/drivers/media/usb/cpia2/cpia2.h @@ -429,6 +429,7 @@ int cpia2_send_command(struct camera_data *cam, struct cpia2_command *cmd); int cpia2_do_command(struct camera_data *cam, unsigned int command, unsigned char direction, unsigned char param); +void cpia2_deinit_camera_struct(struct camera_data *cam, struct usb_interface *intf); struct camera_data *cpia2_init_camera_struct(struct usb_interface *intf); int cpia2_init_camera(struct camera_data *cam); int cpia2_allocate_buffers(struct camera_data *cam); diff --git a/drivers/media/usb/cpia2/cpia2_core.c b/drivers/media/usb/cpia2/cpia2_core.c index e747548ab286..b5a2d06fb356 100644 --- a/drivers/media/usb/cpia2/cpia2_core.c +++ b/drivers/media/usb/cpia2/cpia2_core.c @@ -2163,6 +2163,18 @@ static void reset_camera_struct(struct camera_data *cam) cam->height = cam->params.roi.height; } +/****************************************************************************** + * + * cpia2_init_camera_struct + * + * Deinitialize camera struct + *****************************************************************************/ +void cpia2_deinit_camera_struct(struct camera_data *cam, struct usb_interface *intf) +{ + v4l2_device_unregister(&cam->v4l2_dev); + kfree(cam); +} + /****************************************************************************** * * cpia2_init_camera_struct diff --git a/drivers/media/usb/cpia2/cpia2_usb.c b/drivers/media/usb/cpia2/cpia2_usb.c index 3ab80a7b4498..76aac06f9fb8 100644 --- a/drivers/media/usb/cpia2/cpia2_usb.c +++ b/drivers/media/usb/cpia2/cpia2_usb.c @@ -844,15 +844,13 @@ static int cpia2_usb_probe(struct usb_interface *intf, ret = set_alternate(cam, USBIF_CMDONLY); if (ret < 0) { ERR("%s: usb_set_interface error (ret = %d)\n", __func__, ret); - kfree(cam); - return ret; + goto alt_err; } if((ret = cpia2_init_camera(cam)) < 0) { ERR("%s: failed to initialize cpia2 camera (ret = %d)\n", __func__, ret); - kfree(cam); - return ret; + goto alt_err; } LOG(" CPiA Version: %d.%02d (%d.%d)\n", cam->params.version.firmware_revision_hi, @@ -872,11 +870,14 @@ static int cpia2_usb_probe(struct usb_interface *intf, ret = cpia2_register_camera(cam); if (ret < 0) { ERR("%s: Failed to register cpia2 camera (ret = %d)\n", __func__, ret); - kfree(cam); - return ret; + goto alt_err; } return 0; + +alt_err: + cpia2_deinit_camera_struct(cam, intf); + return ret; } /****************************************************************************** -- 2.30.2