Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp555333rdb; Thu, 19 Oct 2023 11:54:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGKSlL0eEmUaTnjn896PNdeUPoND7JrEeVf1Tp1gHOjS+queiqjS4JfFbU+ZtjmYoFfjC/O X-Received: by 2002:a05:6a20:4321:b0:154:e793:52b3 with SMTP id h33-20020a056a20432100b00154e79352b3mr3372055pzk.0.1697741640234; Thu, 19 Oct 2023 11:54:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697741640; cv=none; d=google.com; s=arc-20160816; b=PkQKuxtHkpVUjxugo3sPXLGki62wfF+0BsnNp2DV+BKoBt5aqzu85C+iNuvpCBJoUs c6GrbBm7dB+ggnVRZTUDe649kd1Iy0c3w49p2wpnrYvSqJElcLFAFfYsH3pzTPq/9hfr xnBEdGPerImOkfAYSjm5BtfBTpX1MMjFx7P5wGsclcIyBMNNsdiRIGnnSiFW5cQHWLcf 08tde+ko8rDdsL56KZJxM9iMe9XpaR9eRXZBsI7vS7DNcx6MSqX8pt5TE6k+8tPik+aT 9Z36Zkjc1ZigdvpOTMVh2IVO0e5T6BwwOup3TEr14E9ETYPm0gQtEviqfV4++98q5+EX CCbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=CqNwqBp940RbFRM/5o5udERAg9HMJqaqzv8BBVQSwKE=; fh=E5X6/3uWCMdGSKLB7vuNn1z8ZeRDzUIfiV2I8BhU+4Q=; b=Yf0VB5RHMf9q3GUGoTfWjoI9Sp4DEoyTugpy38gTohFFqC8wwZ1XGyylA6koVyhdkd N2V75CL7C1ZWhSOL43btkcfoDdpa5Bvmr0UFK1mIwk2OvxcKVdxNuDilowO/9iwsVP8t LE3+kwi5bujD1yh5jUJMQmLaGjJglmyp3J1gUL+JS4wsTRD4Ukd0G3fiWky8BGe7/GoC r5qebaMTWoQQqT1B5Uw8BEtNQ2hyNTkPW/PyaJvTz1RV65Bav0Qp6T5kUBuOf8eJ9UR8 55sgifHjp1zKu/7dICfDMReq9G3ZThGIE3IX+uJIXDs0eO6Qv9Yg5WdUyCDJSdqMLp6f hELw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ccE6V8xX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id s18-20020a639252000000b00566016fc08csi182044pgn.83.2023.10.19.11.53.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 11:54:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ccE6V8xX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id C69BE81B4AC5; Thu, 19 Oct 2023 11:53:56 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346498AbjJSSxj (ORCPT + 99 others); Thu, 19 Oct 2023 14:53:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346407AbjJSSxd (ORCPT ); Thu, 19 Oct 2023 14:53:33 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3F7912F for ; Thu, 19 Oct 2023 11:53:30 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d9ace796374so43756276.0 for ; Thu, 19 Oct 2023 11:53:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697741610; x=1698346410; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=CqNwqBp940RbFRM/5o5udERAg9HMJqaqzv8BBVQSwKE=; b=ccE6V8xXVe+ht/vRHkKpjfigtgw3Yk8xk1mKElaB6feSDe6HUDQfeEB9VvveWAuyRz 9pycUvzzNwdeIgnCAacHOwcQxZjH9E0T0MEbfulPDMPd5fwV4r+/Yjfa9Ar0pLasuOYM NIrLDYqPGdxPzDmwBVKaTiKUYHrJ5Y3dLdz36+wQxoB0I/dOCcd1OOuinpamcMbXls39 CfiV2iqbltPTATdQkOz78UY8CIwVrLddTBROA4ctf2qj2t/fNOOPBeWtxIHVqSZW279J s+7B4HmauWDWZVatwYZAcgnWycDYIDpkjei8UIi/QlDGrN0UninpP7rG9HnSP4N9TfIJ kOuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697741610; x=1698346410; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CqNwqBp940RbFRM/5o5udERAg9HMJqaqzv8BBVQSwKE=; b=PjE7w8m04mvOdhfewDSrt8QSYeq9U3FKgV5KZ8BuMpnDz/wGGXrsLjiLKdv32dotn+ h8rfZakSVr69iZpCh2H3hpgHVhS3jFcdvcAGPiMbYueeUaddvPiZ3pB5xmKUm0n7fjhH +e/H2L4zcwes/eKWjiCMEk+ttwu9miUHBN+++q6GEnsm7eGTbu+Udh6SlRd7uwXOgNvL b6WI1DRYG1MHNpQalm11cHOLvPaBqRCDqR4UeQoEOtdhs5uSTersUkBZE0LSmh1bYaXk jFpP6gB8XnK6vcBob/pwmvJF4247MpZP03t2+a/evjXtiBE5bz/aO7A4yqIaCbPlvsuo iOPA== X-Gm-Message-State: AOJu0YyTqwyfQ7XnSrqLSzfAx+Wjr/auvRv4e/xQJDI+EmqqG94HvE5w xk+zw4siyXchG2ZfI9HI9THpZ9HcVpoV X-Received: from hi-h2o-specialist.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3cef]) (user=arakesh job=sendgmr) by 2002:a05:6902:526:b0:d9a:3a25:36df with SMTP id y6-20020a056902052600b00d9a3a2536dfmr73398ybs.8.1697741609958; Thu, 19 Oct 2023 11:53:29 -0700 (PDT) Date: Thu, 19 Oct 2023 11:53:17 -0700 In-Reply-To: <20231019185319.2714000-1-arakesh@google.com> Mime-Version: 1.0 References: <20230930184821.310143-1-arakesh@google.com> <20231019185319.2714000-1-arakesh@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Message-ID: <20231019185319.2714000-3-arakesh@google.com> Subject: [PATCH v6 2/4] usb: gadget: uvc: Allocate uvc_requests one at a time From: Avichal Rakesh To: arakesh@google.com, dan.scally@ideasonboard.com, gregkh@linuxfoundation.org, laurent.pinchart@ideasonboard.com, m.grzeschik@pengutronix.de Cc: etalvala@google.com, jchowdhary@google.com, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 19 Oct 2023 11:53:56 -0700 (PDT) Currently, the uvc gadget driver allocates all uvc_requests as one array and deallocates them all when the video stream stops. This includes de-allocating all the usb_requests associated with those uvc_requests. This can lead to use-after-free issues if any of those de-allocated usb_requests were still owned by the usb controller. This patch is 1 of 2 patches addressing the use-after-free issue. Instead of bulk allocating all uvc_requests as an array, this patch allocates uvc_requests one at a time, which should allows for similar granularity when deallocating the uvc_requests. This patch has no functional changes other than allocating each uvc_request separately, and similarly freeing each of them separately. Link: https://lore.kernel.org/7cd81649-2795-45b6-8c10-b7df1055020d@google.com Suggested-by: Michael Grzeschik Reviewed-by: Michael Grzeschik Tested-by: Michael Grzeschik Signed-off-by: Avichal Rakesh --- v1 -> v2: Rebased to ToT v2 -> v3: Fix email threading goof-up v3 -> v4: Address review comments & re-rebase to ToT v4 -> v5: Address more review comments. Add Reviewed-by & Tested-by. v5 -> v6: No change drivers/usb/gadget/function/uvc.h | 3 +- drivers/usb/gadget/function/uvc_video.c | 89 ++++++++++++++----------- 2 files changed, 52 insertions(+), 40 deletions(-) diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h index 989bc6b4e93d..993694da0bbc 100644 --- a/drivers/usb/gadget/function/uvc.h +++ b/drivers/usb/gadget/function/uvc.h @@ -81,6 +81,7 @@ struct uvc_request { struct sg_table sgt; u8 header[UVCG_REQUEST_HEADER_LEN]; struct uvc_buffer *last_buf; + struct list_head list; }; struct uvc_video { @@ -102,7 +103,7 @@ struct uvc_video { /* Requests */ unsigned int req_size; - struct uvc_request *ureq; + struct list_head ureqs; /* all uvc_requests allocated by uvc_video */ struct list_head req_free; spinlock_t req_lock; diff --git a/drivers/usb/gadget/function/uvc_video.c b/drivers/usb/gadget/function/uvc_video.c index c334802ac0a4..c180866c8e34 100644 --- a/drivers/usb/gadget/function/uvc_video.c +++ b/drivers/usb/gadget/function/uvc_video.c @@ -227,6 +227,24 @@ uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video, * Request handling */ +static void +uvc_video_free_request(struct uvc_request *ureq, struct usb_ep *ep) +{ + sg_free_table(&ureq->sgt); + if (ureq->req && ep) { + usb_ep_free_request(ep, ureq->req); + ureq->req = NULL; + } + + kfree(ureq->req_buffer); + ureq->req_buffer = NULL; + + if (!list_empty(&ureq->list)) + list_del_init(&ureq->list); + + kfree(ureq); +} + static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) { int ret; @@ -293,27 +311,12 @@ uvc_video_complete(struct usb_ep *ep, struct usb_request *req) static int uvc_video_free_requests(struct uvc_video *video) { - unsigned int i; - - if (video->ureq) { - for (i = 0; i < video->uvc_num_requests; ++i) { - sg_free_table(&video->ureq[i].sgt); + struct uvc_request *ureq, *temp; - if (video->ureq[i].req) { - usb_ep_free_request(video->ep, video->ureq[i].req); - video->ureq[i].req = NULL; - } - - if (video->ureq[i].req_buffer) { - kfree(video->ureq[i].req_buffer); - video->ureq[i].req_buffer = NULL; - } - } - - kfree(video->ureq); - video->ureq = NULL; - } + list_for_each_entry_safe(ureq, temp, &video->ureqs, list) + uvc_video_free_request(ureq, video->ep); + INIT_LIST_HEAD(&video->ureqs); INIT_LIST_HEAD(&video->req_free); video->req_size = 0; return 0; @@ -322,6 +325,7 @@ uvc_video_free_requests(struct uvc_video *video) static int uvc_video_alloc_requests(struct uvc_video *video) { + struct uvc_request *ureq; unsigned int req_size; unsigned int i; int ret = -ENOMEM; @@ -332,29 +336,34 @@ uvc_video_alloc_requests(struct uvc_video *video) * max_t(unsigned int, video->ep->maxburst, 1) * (video->ep->mult); - video->ureq = kcalloc(video->uvc_num_requests, sizeof(struct uvc_request), GFP_KERNEL); - if (video->ureq == NULL) - return -ENOMEM; + INIT_LIST_HEAD(&video->ureqs); + for (i = 0; i < video->uvc_num_requests; i++) { + ureq = kzalloc(sizeof(struct uvc_request), GFP_KERNEL); + if (ureq == NULL) + goto error; + + INIT_LIST_HEAD(&ureq->list); + + list_add_tail(&ureq->list, &video->ureqs); - for (i = 0; i < video->uvc_num_requests; ++i) { - video->ureq[i].req_buffer = kmalloc(req_size, GFP_KERNEL); - if (video->ureq[i].req_buffer == NULL) + ureq->req_buffer = kmalloc(req_size, GFP_KERNEL); + if (ureq->req_buffer == NULL) goto error; - video->ureq[i].req = usb_ep_alloc_request(video->ep, GFP_KERNEL); - if (video->ureq[i].req == NULL) + ureq->req = usb_ep_alloc_request(video->ep, GFP_KERNEL); + if (ureq->req == NULL) goto error; - video->ureq[i].req->buf = video->ureq[i].req_buffer; - video->ureq[i].req->length = 0; - video->ureq[i].req->complete = uvc_video_complete; - video->ureq[i].req->context = &video->ureq[i]; - video->ureq[i].video = video; - video->ureq[i].last_buf = NULL; + ureq->req->buf = ureq->req_buffer; + ureq->req->length = 0; + ureq->req->complete = uvc_video_complete; + ureq->req->context = ureq; + ureq->video = video; + ureq->last_buf = NULL; - list_add_tail(&video->ureq[i].req->list, &video->req_free); + list_add_tail(&ureq->req->list, &video->req_free); /* req_size/PAGE_SIZE + 1 for overruns and + 1 for header */ - sg_alloc_table(&video->ureq[i].sgt, + sg_alloc_table(&ureq->sgt, DIV_ROUND_UP(req_size - UVCG_REQUEST_HEADER_LEN, PAGE_SIZE) + 2, GFP_KERNEL); } @@ -489,8 +498,8 @@ static void uvcg_video_pump(struct work_struct *work) */ int uvcg_video_enable(struct uvc_video *video, int enable) { - unsigned int i; int ret; + struct uvc_request *ureq; if (video->ep == NULL) { uvcg_info(&video->uvc->func, @@ -502,9 +511,10 @@ int uvcg_video_enable(struct uvc_video *video, int enable) cancel_work_sync(&video->pump); uvcg_queue_cancel(&video->queue, 0); - for (i = 0; i < video->uvc_num_requests; ++i) - if (video->ureq && video->ureq[i].req) - usb_ep_dequeue(video->ep, video->ureq[i].req); + list_for_each_entry(ureq, &video->ureqs, list) { + if (ureq->req) + usb_ep_dequeue(video->ep, ureq->req); + } uvc_video_free_requests(video); uvcg_queue_enable(&video->queue, 0); @@ -536,6 +546,7 @@ int uvcg_video_enable(struct uvc_video *video, int enable) */ int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc) { + INIT_LIST_HEAD(&video->ureqs); INIT_LIST_HEAD(&video->req_free); spin_lock_init(&video->req_lock); INIT_WORK(&video->pump, uvcg_video_pump); -- 2.42.0.758.gaed0368e0e-goog