Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp1173738ybn; Wed, 2 Oct 2019 11:57:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqyZSOq3tOMbUSYWPLOz72wbD6eoKPP+NzpBNMk0SOGIjG0v5bIbA13FS5KnrvKf8SdhkVYV X-Received: by 2002:a05:6402:1a4f:: with SMTP id bf15mr5590688edb.292.1570042637507; Wed, 02 Oct 2019 11:57:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570042637; cv=none; d=google.com; s=arc-20160816; b=FG6Anv1rDi5pDjej27VkLIoFgAGIYI0iWm6IDKATPCINYZpFngEYKoRgIoKG2hnLTa vV3sdwYSdPe6iiTk0Uk7+jWvlERS9DDtdhemDpWsHddpOhjzbf5AVS0LuulW06sjOiH5 sEvKml9Gssl2tAEaYeduyyBFY6Ce1mzj+U1muqGcKZqVty5SSJE8jdOIhHzbu9EqtT36 elfTEfnZB14pPfd+OpnqK+HJTg3GjtgDhkIn8duYbiY+AJ/EWskg8XlchPHDx0ukTCjH ptKPIqO0YM/nEW/4yp8RKZeICZU6bBFRtm+up83ZXAGempPNtNR/Qb5Xw+cqpm+gYxx8 dMMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=sBL9UjqvssR06SgDr2K6Tk7vRCF5bOqPgtMjb7sRyPo=; b=PWTdUHBTI9+b5Y2CfKwJR+qiVg4fRJmh2DIgmzjAbRPHYLdeAnsU541hNMn1p1KaaN TtKm3OX6uNz5WGKtqF7MjtuFX2oSvTiu9BmthlDLJEJ1wKExpipsNpz6OTAv/E+sZKbo iiPDtaBUxNtUERnh3iW0r2ueNEpPSDXvgPVvNH/V2t0i1XDmZiGHCVUE6iHsSDufELhy pJuGiIa9g6UqjCT6s38refxvMmSfoECAPAy64gYCnX5SwDZD6MnvvZ7NVGpkhcSeDQ9R TgkJGJFyf72cTBa/d0Cyr4glR+svMeXW/GjI3k2DaK1vd6K7KZsAsKG21MrLz79zZ3tQ KUWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b=gJqpqcIy; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p18si11970058ejn.370.2019.10.02.11.56.52; Wed, 02 Oct 2019 11:57:17 -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 (test mode) header.i=@ideasonboard.com header.s=mail header.b=gJqpqcIy; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728808AbfJBS4V (ORCPT + 99 others); Wed, 2 Oct 2019 14:56:21 -0400 Received: from perceval.ideasonboard.com ([213.167.242.64]:38490 "EHLO perceval.ideasonboard.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726669AbfJBS4U (ORCPT ); Wed, 2 Oct 2019 14:56:20 -0400 Received: from pendragon.ideasonboard.com (unknown [132.205.229.212]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 5EFB72BB; Wed, 2 Oct 2019 20:56:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1570042577; bh=PbWtfJfT3AWYLkOwJItUMiaSDU3tjjAskp4aOSEpGOQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=gJqpqcIy/Ynox93mO7HvGxTuyrOys4VaUqnoHi1rZFK1hsgiQV/ACZkRapmTILkXi wiMpzGtpraFthrkAfi22sMtR6n/fNUlbwwo83aPbWTfJqdQOUiABhm+n4YP4WHnO8t KustgNeyUlNXd85nXeh7sChLFAPD+n9zji2X3nog= Date: Wed, 2 Oct 2019 21:56:04 +0300 From: Laurent Pinchart To: Will Deacon Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, andreyknvl@google.com, Mauro Carvalho Chehab , Dmitry Vyukov , Kostya Serebryany , stable@vger.kernel.org Subject: Re: [PATCH] media: uvc: Avoid cyclic entity chains due to malformed USB descriptors Message-ID: <20191002185604.GF5262@pendragon.ideasonboard.com> References: <20191002112753.21630-1-will@kernel.org> <20191002130913.GA5262@pendragon.ideasonboard.com> <20191002131928.yp5r4tyvtvwvuoba@willie-the-truck> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20191002131928.yp5r4tyvtvwvuoba@willie-the-truck> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Will, On Wed, Oct 02, 2019 at 02:19:29PM +0100, Will Deacon wrote: > On Wed, Oct 02, 2019 at 04:09:13PM +0300, Laurent Pinchart wrote: > > Thank you for the patch. > > And thank you for the quick response. > > > On Wed, Oct 02, 2019 at 12:27:53PM +0100, Will Deacon wrote: > > > I don't have a way to reproduce the original issue, so this change is > > > based purely on inspection. Considering I'm not familiar with USB nor > > > UVC, I may well have missed something! > > > > I may also be missing something, I haven't touched this code for a long > > time :-) > > Actually, that is pretty helpful because it will make backporting easier > if we get to that :) > > > uvc_scan_chain_entity(), at the end of the function, adds the entity to > > the list of entities in the chain with > > > > list_add_tail(&entity->chain, &chain->entities); > > Yes. > > > uvc_scan_chain_forward() is then called (from uvc_scan_chain()), and > > iterates over all entities connected to the entity being scanned. > > > > while (1) { > > forward = uvc_entity_by_reference(chain->dev, entity->id, > > forward); > > Yes. > > > At this point forward may be equal to entity, if entity references > > itself. > > Correct -- that's indicative of a malformed entity which we want to reject, > right? Right. We can reject the whole chain in that case. There's one case where we still want to succeed though, which is handled by uvc_scan_fallback(). Looking at the code, uvc_scan_device() does if (uvc_scan_chain(chain, term) < 0) { kfree(chain); continue; } It seems that's missing removal of all entities that would have been successfully added to the chain. This prevents, I think, uvc_scan_fallback() from working properly in some cases. > > if (forward == NULL) > > break; > > if (forward == prev) > > continue; > > if (forward->chain.next || forward->chain.prev) { > > uvc_trace(UVC_TRACE_DESCR, "Found reference to " > > "entity %d already in chain.\n", forward->id); > > return -EINVAL; > > } > > > > But then this check should trigger, as forward == entity and entity is > > in the chain's list of entities. > > Right, but this code is added by my patch, no? In mainline, the code only > has the first two checks, which both end up comparing against NULL the first > time around: > > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/media/usb/uvc/uvc_driver.c#n1489 > > Or are you referring to somewhere else? Oops. This is embarassing... :-) You're of course right. The second hunk seems fine too, even if I would have preferred centralising the check in a single place. That should be possible, but it would involve refactoring that isn't worth it at the moment. -- Regards, Laurent Pinchart