Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp445545iog; Mon, 13 Jun 2022 06:08:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzzZDUNO9uYTpZUn67Z/KS+sI8QrKTq1BqhTtuy5kcX8DUQBPS4wdbdP6XCvbxFOKz26O8A X-Received: by 2002:a17:907:c71b:b0:718:d0a2:a447 with SMTP id ty27-20020a170907c71b00b00718d0a2a447mr1252257ejc.157.1655125697959; Mon, 13 Jun 2022 06:08:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655125697; cv=none; d=google.com; s=arc-20160816; b=F+IBO0HtfHkSm2z+jyS8jnliOuyCeuwKuWd7+IMsTA63J6aTi+UiOz6Ueyq83S9I1F 6MaiAKlVTa122t859mntjSOYAV0UKi7N6FzSlAlDZmPpOmeuKrTLnb9Mr9sIACNh/Ph5 EXemxh2u9hfzIFnalN8NOP0AvdC21w9XKcvUa7bEmQynloovgaz9bFvTtPWRNmwT5NyG i6V3PWx70PglGWvKJ0SF5UfNc2uu0kUeYwb4nIEEO9rtyi9ejAMxS/xu86sdN4svf1om RAhybUIn1lVnDyuAhJF2QS4LybOUjqTR2aUHhX7AWRBlAxY83Izj5EN04kZWhK99x15Z UIqQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Jju1URgE41BsAd4/IfjBCctdzAfbILgO4EFC1ozCChk=; b=EX4qIHBk8FVoLfnI/3BWE+yzj76K5QoAx+O155sblvrWXL6qJ68zLsETfZA+e3vyuS T3sxWrPnoQpWoHGrH+blUhKJUVrMh9oFA2nk/pj4qbnhO6hKq6f13w0QF2lriG/5abis IEIEs5Krqw7kwcQVwx1mJLiw7y/8UHEHmmRLAGINjVfuVKzEsIrlCzf38IcB9P1Vaxjx swXnYw2vmrac8whb93+NCcF+Er6B+F5mEfs/WRZc4wTEirfA9Hn9HBts/HqcjNb1pMNL HtiZcSwI0z3iM1VsbUs9AgsP2XOuR4y1bg6IhDV440LJwrQaykqcalCfEc2xwuX3oUnY gqFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GMJ4Z6qq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ht9-20020a170907608900b006f445fe1697si8569892ejc.35.2022.06.13.06.07.51; Mon, 13 Jun 2022 06:08:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GMJ4Z6qq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242721AbiFMKU1 (ORCPT + 99 others); Mon, 13 Jun 2022 06:20:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242729AbiFMKSd (ORCPT ); Mon, 13 Jun 2022 06:18:33 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7249220BD5; Mon, 13 Jun 2022 03:16:20 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sin.source.kernel.org (Postfix) with ESMTPS id BC0ACCE1109; Mon, 13 Jun 2022 10:16:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B08CAC34114; Mon, 13 Jun 2022 10:16:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655115377; bh=lXOwunPvjLmnJDHlPHNJ8y9/+BMNkm1eEegXiSOoUHk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GMJ4Z6qqM6lKq0OKqrXN2heb9CVZgiboWdf+cq/gbn1ZuZ72eWfPVu4Gp41TWeZH+ QmJdwaaTpE7DG/WlTUxXy0iu73PvJMXdh1bEYz8kw0/EkRSxcZhcrAhUPEuJQ34Ihf Gv7ExYneSj5OA5/6c/s9dnmj3J1u2v87dcO6j1pc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xiaomeng Tong , Laurent Pinchart , Mauro Carvalho Chehab , Sasha Levin Subject: [PATCH 4.9 052/167] media: uvcvideo: Fix missing check to determine if element is found in list Date: Mon, 13 Jun 2022 12:08:46 +0200 Message-Id: <20220613094853.179297413@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094840.720778945@linuxfoundation.org> References: <20220613094840.720778945@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Xiaomeng Tong [ Upstream commit 261f33388c29f6f3c12a724e6d89172b7f6d5996 ] The list iterator will point to a bogus position containing HEAD if the list is empty or the element is not found in list. This case should be checked before any use of the iterator, otherwise it will lead to a invalid memory access. The missing check here is before "pin = iterm->id;", just add check here to fix the security bug. In addition, the list iterator value will *always* be set and non-NULL by list_for_each_entry(), so it is incorrect to assume that the iterator value will be NULL if the element is not found in list, considering the (mis)use here: "if (iterm == NULL". Use a new value 'it' as the list iterator, while use the old value 'iterm' as a dedicated pointer to point to the found element, which 1. can fix this bug, due to 'iterm' is NULL only if it's not found. 2. do not need to change all the uses of 'iterm' after the loop. 3. can also limit the scope of the list iterator 'it' *only inside* the traversal loop by simply declaring 'it' inside the loop in the future, as usage of the iterator outside of the list_for_each_entry is considered harmful. https://lkml.org/lkml/2022/2/17/1032 Fixes: d5e90b7a6cd1c ("[media] uvcvideo: Move to video_ioctl2") Signed-off-by: Xiaomeng Tong Signed-off-by: Laurent Pinchart Signed-off-by: Mauro Carvalho Chehab Signed-off-by: Sasha Levin --- drivers/media/usb/uvc/uvc_v4l2.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c index 2b1e06e825f0..53d81ef9a4be 100644 --- a/drivers/media/usb/uvc/uvc_v4l2.c +++ b/drivers/media/usb/uvc/uvc_v4l2.c @@ -846,29 +846,31 @@ static int uvc_ioctl_enum_input(struct file *file, void *fh, struct uvc_video_chain *chain = handle->chain; const struct uvc_entity *selector = chain->selector; struct uvc_entity *iterm = NULL; + struct uvc_entity *it; u32 index = input->index; - int pin = 0; if (selector == NULL || (chain->dev->quirks & UVC_QUIRK_IGNORE_SELECTOR_UNIT)) { if (index != 0) return -EINVAL; - list_for_each_entry(iterm, &chain->entities, chain) { - if (UVC_ENTITY_IS_ITERM(iterm)) + list_for_each_entry(it, &chain->entities, chain) { + if (UVC_ENTITY_IS_ITERM(it)) { + iterm = it; break; + } } - pin = iterm->id; } else if (index < selector->bNrInPins) { - pin = selector->baSourceID[index]; - list_for_each_entry(iterm, &chain->entities, chain) { - if (!UVC_ENTITY_IS_ITERM(iterm)) + list_for_each_entry(it, &chain->entities, chain) { + if (!UVC_ENTITY_IS_ITERM(it)) continue; - if (iterm->id == pin) + if (it->id == selector->baSourceID[index]) { + iterm = it; break; + } } } - if (iterm == NULL || iterm->id != pin) + if (iterm == NULL) return -EINVAL; memset(input, 0, sizeof(*input)); -- 2.35.1