Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp1127283lqo; Thu, 9 May 2024 05:44:41 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX6HAby3+m0iqzbGcEFlVQ4tYFBTCfgQMudoyBGWdMPzlTqSvnsyGtbbeVy4t9tPja9P4I95CB+uQuSK4X4cFqV1LOaJjaldctYGFjzdg== X-Google-Smtp-Source: AGHT+IHPdnbWUpGuCJnfgGx96WgBIpJVmxhLts6fAFzA5wCPsQAnyhXP3xKWDBpB4CCyiaasZ2s5 X-Received: by 2002:a05:6a20:9744:b0:1af:aeb7:7a14 with SMTP id adf61e73a8af0-1afd1417d07mr3590367637.2.1715258681455; Thu, 09 May 2024 05:44:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715258681; cv=pass; d=google.com; s=arc-20160816; b=PtUcyGg2pMie3odqCVN46yGnCAhVaF1KgIEfug+74WpUpLKtUFZJISeqNhQYogzGgd OU+rycUgFtEotpKQOVJ7WLuHa+ZwGq+xNTAg85KXWKlBD1uewjw0HAts5taZtgf0+YRw ZyncmLAW322nDi0u3irpGhQ94aobV6uMAcLuksbbrNfNHoDtE2f4oV0rS1vYWqR+OIhF bFJI9+w6zfx5i2xX8vQKZcRhlgEE3YOVChDnNckfGv0CwimEn1kCaWsmxrPwoD/12lDn LN0xgfGEGEX7zb9qRZDpVy/Du/5sU6MTuPZCxpFzlYoLh7CFnIL7bZsdi46CrWEp+0dR UoWw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=JuLFJmmbhoykJrllYE1cfrCLZIGazO2bppCFaELUbdU=; fh=f12NYqKkXJ39h0uuGEsoANEbqclncI5bTATw4KYpRzc=; b=IuntapCDl488okwO6byFdTF0YVjdGzWF2uqCygii78PsGMAEVonwJP0mHXxUrx4D00 Umv/U30mlLnk3o1dZNup5ntWhjJqh+eEWMTSc+jt8NV1Cd2EJnYSiQfqTj3/YM4P3/uc zkT6wO9ZP5fSL/77bYyJ08JwWOzjj5w5pm1DnpN5f9+dQsC7pLgyh8huvFaw5lojIYT8 44UqJ4ikEOwBkyNwTLq+3LkVBDcAqKa17Io0BUHSuXTKChIKZ6tXSrWIF+tTW0q9r6jo 0tFrcUUdqEXab1Co/5n2zk1pesmRExWo4BaS9tA+/u+7bgmeS6wagyKT+P27DaaBfdDZ nvSw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-174512-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174512-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-6341180e35bsi1366862a12.549.2024.05.09.05.44.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 05:44:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-174512-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-174512-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174512-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 3EF512838BC for ; Thu, 9 May 2024 12:44:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3098B147C99; Thu, 9 May 2024 12:44:26 +0000 (UTC) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F3FDF13B7BD; Thu, 9 May 2024 12:44:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715258665; cv=none; b=bYswjcLLqmi3ZcAp8srVjde/sB49/pRAGsbGKoVJ1OTDX0xKmie3kYpRwHgWgaDy4AKMQQ/aZZX/W2VTpJ0idM5+MVlyfDI3hvyvbTzcYxAvW49srIuUsYAuTaR+S7PUOfAoKkbEsEa+Km0ETwrlrANTKlIL047O1w+HG2Bal6M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715258665; c=relaxed/simple; bh=R73MtKD/rDP3asltM0O7fwSxQBJPsPSHFdmUHkI6Pbo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=R7z9ZZDGcuSwLh3yPveMKuKowzjiPq7KbDVCx+8KLVZviXzIFCJk60TRVs5mA+x9eXhgQsHF9zTOgoXvIfEa6oMcUoms/5jeQ94jWu9f4WD5dD7SdOl9pBc0mcdDqaoRN38lMizFWrXx/wAUhxGvcumxwGbg2mdOqrAGaUEvrKM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4VZs7X3gqnzvXf1; Thu, 9 May 2024 20:40:56 +0800 (CST) Received: from dggpeml500012.china.huawei.com (unknown [7.185.36.15]) by mail.maildlp.com (Postfix) with ESMTPS id E0D4E18007B; Thu, 9 May 2024 20:44:17 +0800 (CST) Received: from localhost.localdomain (10.67.175.61) by dggpeml500012.china.huawei.com (7.185.36.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Thu, 9 May 2024 20:44:17 +0800 From: Zheng Yejian To: CC: , , , Subject: [PATCH v3] media: dvb-usb: Fix unexpected infinite loop in dvb_usb_read_remote_control() Date: Thu, 9 May 2024 20:44:14 +0800 Message-ID: <20240509124414.1392304-1-zhengyejian1@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500012.china.huawei.com (7.185.36.15) Infinite log printing occurs during fuzz test: rc rc1: DViCO FusionHDTV DVB-T USB (LGZ201) as ... ... dvb-usb: schedule remote query interval to 100 msecs. dvb-usb: DViCO FusionHDTV DVB-T USB (LGZ201) successfully initialized ... dvb-usb: bulk message failed: -22 (1/0) dvb-usb: bulk message failed: -22 (1/0) dvb-usb: bulk message failed: -22 (1/0) ... dvb-usb: bulk message failed: -22 (1/0) Looking into the codes, there is a loop in dvb_usb_read_remote_control(), that is in rc_core_dvb_usb_remote_init() create a work that will call dvb_usb_read_remote_control(), and this work will reschedule itself at 'rc_interval' intervals to recursively call dvb_usb_read_remote_control(), see following code snippet: rc_core_dvb_usb_remote_init() { ... INIT_DELAYED_WORK(&d->rc_query_work, dvb_usb_read_remote_control); schedule_delayed_work(&d->rc_query_work, msecs_to_jiffies(rc_interval)); ... } dvb_usb_read_remote_control() { ... err = d->props.rc.core.rc_query(d); if (err) err(...) // Did not return even if query failed schedule_delayed_work(&d->rc_query_work, msecs_to_jiffies(rc_interval)); } When the infinite log printing occurs, the query callback 'd->props.rc.core.rc_query' is cxusb_rc_query(). And the log is due to the failure of finding a valid 'generic_bulk_ctrl_endpoint' in usb_bulk_msg(), see following code snippet: cxusb_rc_query() { cxusb_ctrl_msg() { dvb_usb_generic_rw() { ret = usb_bulk_msg(d->udev, usb_sndbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint),...); if (ret) err("bulk message failed: %d (%d/%d)",ret,wlen,actlen); ... } ... } By analyzing the corresponding USB descriptor, it shows that the bNumEndpoints is 0 in its interface descriptor, but the 'generic_bulk_ctrl_endpoint' is 1, that means user don't configure a valid endpoint for 'generic_bulk_ctrl_endpoint', therefore this 'invalid' USB device should be rejected before it calls into dvb_usb_read_remote_control(). To fix it, we need to add endpoint check for 'generic_bulk_ctrl_endpoint'. And as Sean suggested, the same check and clear halts should be done for 'generic_bulk_ctrl_endpoint_response'. So introduce dvb_usb_check_bulk_endpoint() to do it for both of them. Fixes: 4d43e13f723e ("V4L/DVB (4643): Multi-input patch for DVB-USB device") Signed-off-by: Zheng Yejian --- drivers/media/usb/dvb-usb/dvb-usb-init.c | 35 +++++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) v3: - Fix following warning: drivers/media/usb/dvb-usb/dvb-usb-init.c:133:9: warning: 'adap' may be used uninitialized [-Wmaybe-uninitialized] Link: https://lore.kernel.org/all/ZjTPqR3_EhbNU-fm@gofer.mess.org/ v2: - As Sean suggested, check endpoint and clear halt for both 'generic_bulk_ctrl_endpoint' and 'generic_bulk_ctrl_endpoint_response' with the new introduced dvb_usb_check_bulk_endpoint(); Link: https://lore.kernel.org/all/ZjCl97Ww6NrzJQCB@gofer.mess.org/ Link: https://lore.kernel.org/all/ZjC7rXU7ViaH60_S@gofer.mess.org/ - Add Fixes tag. - Link: https://lore.kernel.org/all/20240430104137.1014471-1-zhengyejian1@huawei.com/ v1: - Link: https://lore.kernel.org/all/20240412135256.1546051-1-zhengyejian1@huawei.com/ diff --git a/drivers/media/usb/dvb-usb/dvb-usb-init.c b/drivers/media/usb/dvb-usb/dvb-usb-init.c index fbf58012becd..22d83ac18eb7 100644 --- a/drivers/media/usb/dvb-usb/dvb-usb-init.c +++ b/drivers/media/usb/dvb-usb/dvb-usb-init.c @@ -23,11 +23,40 @@ static int dvb_usb_force_pid_filter_usage; module_param_named(force_pid_filter_usage, dvb_usb_force_pid_filter_usage, int, 0444); MODULE_PARM_DESC(force_pid_filter_usage, "force all dvb-usb-devices to use a PID filter, if any (default: 0)."); +static int dvb_usb_check_bulk_endpoint(struct dvb_usb_device *d, u8 endpoint) +{ + if (endpoint) { + int ret; + + ret = usb_pipe_type_check(d->udev, usb_sndbulkpipe(d->udev, endpoint)); + if (ret) + return ret; + ret = usb_pipe_type_check(d->udev, usb_rcvbulkpipe(d->udev, endpoint)); + if (ret) + return ret; + } + return 0; +} + +static void dvb_usb_clear_halt(struct dvb_usb_device *d, u8 endpoint) +{ + if (endpoint) { + usb_clear_halt(d->udev, usb_sndbulkpipe(d->udev, endpoint)); + usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, endpoint)); + } +} + static int dvb_usb_adapter_init(struct dvb_usb_device *d, short *adapter_nrs) { struct dvb_usb_adapter *adap; int ret, n, o; + ret = dvb_usb_check_bulk_endpoint(d, d->props.generic_bulk_ctrl_endpoint); + if (ret) + return ret; + ret = dvb_usb_check_bulk_endpoint(d, d->props.generic_bulk_ctrl_endpoint_response); + if (ret) + return ret; for (n = 0; n < d->props.num_adapters; n++) { adap = &d->adapter[n]; adap->dev = d; @@ -103,10 +132,8 @@ static int dvb_usb_adapter_init(struct dvb_usb_device *d, short *adapter_nrs) * when reloading the driver w/o replugging the device * sometimes a timeout occurs, this helps */ - if (d->props.generic_bulk_ctrl_endpoint != 0) { - usb_clear_halt(d->udev, usb_sndbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint)); - usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, d->props.generic_bulk_ctrl_endpoint)); - } + dvb_usb_clear_halt(d, d->props.generic_bulk_ctrl_endpoint); + dvb_usb_clear_halt(d, d->props.generic_bulk_ctrl_endpoint_response); return 0; -- 2.25.1