Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3343899pxv; Sun, 4 Jul 2021 16:06:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwjmHKy2l/Qeu33Uwgs8/apgDfvQ8E70PHpUfwR5A5NliN3B5Wbq2O5lk3SVBK+g/NPLWh/ X-Received: by 2002:a5d:8747:: with SMTP id k7mr9818110iol.83.1625439980119; Sun, 04 Jul 2021 16:06:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625439980; cv=none; d=google.com; s=arc-20160816; b=w+YTkL4mDNgAtbD8cbGTcS4sJsmsukKcwkWuN5Fxmdxwup45rkGE5bt7vhwLmb3kXz dz7PH3M+avupW10+G+cWbhtJhrxVD6MQLS68PfFyr7vHzV2Nfhs6P/BSe8iDgG1K+/dE 8By6vNqJF2ZuGsCAa0sfNA19z5L+SHpbXWHiGI2cflOHh873nxCILyvjdRr+8ft7S37C kHvypqIISBlHeUo68aSODc78aW5buaNmyWiYKlFAYhBRLXwKArEgxD/oUBLPMbZqDu7K LZMhjb92m086ijlQHs1M+lSoqblEL2ARqileUqM+P+nAZVI3B/K/kErOwmWXqeU3iUGX 2iGg== 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=hV44biSpnhQ+aZC3gdop2/xQBbaYc9iuI5Of+5JEXQ5xUwBCivW+xg7x8LumSezMqN JTdlsYbpNlSXmGw3oezeGyIxggy+mlrT5nNdM053EJiFKCEqSB153y+uSgm6//Yitanv TNxTjwlJD9qh6oS+jcshS8UpYPktIFdk46gqvAfM1CXFh8Q18d5ULN0928ygRi5UikbE drCEYPTSrvRBsNGUOLcbc7+yFsbLO/7iTnmug3QmBJNAcdjWzI4lEg/26IhP3Nvl5GA/ 4BvaQUsxu0LznqgsqJkMc5RfOU7wU36euQABKDpKBde1LwSER7sWMjOfh8eBRccjmkbG srHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=u+sH+odq; 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 b6si1344928ilv.22.2021.07.04.16.06.08; Sun, 04 Jul 2021 16:06:20 -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=u+sH+odq; 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 S230203AbhGDXHy (ORCPT + 99 others); Sun, 4 Jul 2021 19:07:54 -0400 Received: from mail.kernel.org ([198.145.29.99]:45894 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230208AbhGDXHd (ORCPT ); Sun, 4 Jul 2021 19:07:33 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 762DE611ED; Sun, 4 Jul 2021 23:04:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1625439897; bh=nP61IyDwMKPULtKhpyhNvfOHA4ogTEHHQM3vW2uT22k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=u+sH+odq6G8Z6slee5jaiOhU0YIuLOK6Dtr6qta3diooCQmMqhOBAyPVJshGtU1io Y8Z0ZFOA5rAN45O09C6ZUNgta21Eys1skx0SdYt5cc4x24hfAAn+BcXpZevDY5QgE/ yhRUyq6AmVwTvxTZZP5JpKxYtz/K/k9Tu2k50bQYPfdDcC/F/et3uBuWfgyAxHI4JP lN+GqpT4lgDVR+BuZzvMQjmGS2+KXnp+vrazuQ28uJZDlKbR66PkKZgPjPoFhaa84R g1cBdYaJJTOavQ4jOO/ovhe/95lt7cj1vsIFPsEIt8r0TR0W21K25oFSIFiytZWap5 aiY2rXc7yREsg== 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.13 26/85] media: cpia2: fix memory leak in cpia2_usb_probe Date: Sun, 4 Jul 2021 19:03:21 -0400 Message-Id: <20210704230420.1488358-26-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210704230420.1488358-1-sashal@kernel.org> References: <20210704230420.1488358-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