Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp4509337ybp; Mon, 7 Oct 2019 09:28:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqyDChhB+DzKy73U3ffYfZ2bHxqSxAZMWoQRlouzl5YzA9ZcBJBRuB7u9zPs6qwg0Fgb8w8P X-Received: by 2002:a17:906:168f:: with SMTP id s15mr24857580ejd.109.1570465698038; Mon, 07 Oct 2019 09:28:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570465698; cv=none; d=google.com; s=arc-20160816; b=qP9jGlOhGN5S1cdoDCq4yess2Gay7sY+wJ/bkK6fV2Y/aWMnI5wWJ0lG/592+wim7P QSeQQf1ldV+nvFzrABCwYaRphUa143DD7Hw76oIXD5OOln1W/iL8PBt6ip15o45GyE6z 962Gi+OBZpVbHpL2lyLCy+YvTdeIhjc2Tnq+SrBI+fzrBT0NzANfDyvRnjs6DbdbU0Q/ j1YpvwtLLcKVGTtGxXfjICELFyxo8Ecxh0/vowO4XIkoeCpKU2NEC99HhWB9HSdtWeaY Zm3cieY+YFuetl9djq8lNiUbTohZ6w5umVwn8c25NXmuJ5PpW5hkywe3CtqulcEthT9+ blDA== 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=D8L/Rx5BoGCOmBEvgsVDmgyEnBbb9drRqDdfHUhVmmI=; b=ndlk2QLFxqSPZ5mT3ARzMqDv/8DRFlQ3qW6xtQ/MKoLFlJaEJ21lTIdra3/6B23KnA WBRDIk9YEAExp+zkauKK5nwI+8oLn+tEAR3lN8yaVtlF8TbKBCFqnc6ekcyGqGnY5gS+ KhEchoZ4ElB3fME8XMw5JlYg3P+TCJ/Vtj2Zdg09ub7n7hpKvE3Kmnf+nd26QZBVqBKz lKojPXlx6YKugGUBlSOWz3H0QL+g+Ed0tyFSxDbKzeOvgiIUgbqkaF3N/Y53Eu/5h5d9 47le8Mhp3Ro7uBmD34DBEnT//erCokvFgNsnzSQi1biNZDc2aPIpgvjXkn2ujjQyw4LV VFvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1fPp3LLq; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g17si7324115ejd.123.2019.10.07.09.27.54; Mon, 07 Oct 2019 09:28:18 -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 header.i=@kernel.org header.s=default header.b=1fPp3LLq; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728626AbfJGQ1Q (ORCPT + 99 others); Mon, 7 Oct 2019 12:27:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:34468 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727801AbfJGQ1Q (ORCPT ); Mon, 7 Oct 2019 12:27:16 -0400 Received: from willie-the-truck (236.31.169.217.in-addr.arpa [217.169.31.236]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8F319206C0; Mon, 7 Oct 2019 16:27:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1570465635; bh=qkPwaifvDP84GW7cD+F3JRfGMTnA8T3MOxCOa4QrBpU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=1fPp3LLq8WpQ3cVRHDVr1luJR17NmuqeTva4l2taEfkyZ1QAwT8/xIu2UEiWgRi5b EpAJTPPWL72mKGJqgZlHdhKbmvyW3gcWiN5QWC/teWy1c6/Ppe7o0FShfQIgo+UunD vg9v5LjwZ5tKqn9FIIdIO6E4esYz5aQm2I1qvNww= Date: Mon, 7 Oct 2019 17:27:10 +0100 From: Will Deacon To: Laurent Pinchart 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: <20191007162709.3vrtbcpoymmnqikl@willie-the-truck> References: <20191002112753.21630-1-will@kernel.org> <20191002130913.GA5262@pendragon.ideasonboard.com> <20191002131928.yp5r4tyvtvwvuoba@willie-the-truck> <20191002185604.GF5262@pendragon.ideasonboard.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191002185604.GF5262@pendragon.ideasonboard.com> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Laurent, Sorry for the delay, I got tied up with other patches. On Wed, Oct 02, 2019 at 09:56:04PM +0300, Laurent Pinchart wrote: > On Wed, Oct 02, 2019 at 02:19:29PM +0100, Will Deacon wrote: > > > 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. I started trying to hack something up here, but I'm actually not sure there's anything to do! I agree that 'uvc_scan_chain()' can fail after adding entities to the chain, however, 'uvc_scan_fallback()' allocates a new chain and calls only 'uvc_scan_chain_entity()' to add entities to it. This doesn't fail on pre-existing 'list_head' structures, so the dangling pointers shouldn't pose a problem there. My patch only adds the checks to 'uvc_scan_chain_forward()' and 'uvc_scan_chain_backward()', neither of which are invoked on the fallback path. The fallback also seems like a best-effort thing, since it isn't even invoked if we managed to initialise *any* chains successfully. Does that make sense, or did you have another failure case in mind? > > > 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. Agreed, thanks. Will