Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp3112599pxm; Mon, 28 Feb 2022 12:16:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJwtVIpCTkJ5bNHvrurmt0nUe3sM4/oIWgJCmRGeZnMgugn6na+2Cnnn8b5NVlKzgbyBHbGD X-Received: by 2002:a17:902:8b85:b0:150:27f1:e86 with SMTP id ay5-20020a1709028b8500b0015027f10e86mr17880874plb.88.1646079376554; Mon, 28 Feb 2022 12:16:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646079376; cv=none; d=google.com; s=arc-20160816; b=wr4ROlJ2N+alxxgHNVDUN3Hpai0tzNyWZcNeU1jZrhHhbCAGd4lIWqNUV8foqN2OpC DXFlfhyqMP+D24cVHem0JruxszZ7kgxX6CbaIwPsFOK1pY2JHWFwL5lk/sRjp+FCh6nO lmtPPD3lbQDwZeMb/6evS5tQAt37S9A9W1CVNE5rFAuH7UbSHbZMr6H2ikRgAL3XelSc 9UaWVQYy355gtGmxE4EASWVhDT6tusgu5HXLAUnFTAEqZ7S9JYpuODUFJWVlOeU7V3HJ Sh+YD1Ucp09CzzfKcNgkKmMNRASXxblkzfR473kA+XfvHGQzWTZ4HDE9zt+aDC8yGi5p XPXw== 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=XIpcOAzPXde5w9rMLOGZY2Jv6EKKv/TEqcziGLUZPJM=; b=lRUXc+37+ixj/5BPU+tek6JRyxhHj6rnJ6xD77p/Oj/ZPPLlOlPH1t1sV7Mgc3mJhI 5sbgfXcETb6e+NzsPIzD25rogqKT2D9ONxIT8pT631OGFOWcy3RAXqggZaVjs3Cpc0Xm DmKi+qTMisYhhGoEzsnIOcT7m77frpn2Pdq7IUfWj+ih4haFx2G5Gegcic46/kSuovk8 mnvqLFYEiG6QeybMLE4PxlJWDOmTbRKMtU3nl0v3sVM0ayJnfeYiFBwvLOqcqDHaPXBs SkGeQXQm+BG7RoY2hTfvxQMeH+8ZRfbDm9BG7yEHEOFKP4778i6yjYIBt1ysznWqPCkX BbFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QD1VEN0i; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id y62-20020a623241000000b004e1ddfa5fc3si9564769pfy.195.2022.02.28.12.16.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Feb 2022 12:16:16 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QD1VEN0i; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7551E60CF1; Mon, 28 Feb 2022 11:38:07 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238646AbiB1RiF (ORCPT + 99 others); Mon, 28 Feb 2022 12:38:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238238AbiB1Rfn (ORCPT ); Mon, 28 Feb 2022 12:35:43 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B949A95A1E; Mon, 28 Feb 2022 09:31:28 -0800 (PST) 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 ams.source.kernel.org (Postfix) with ESMTPS id 37DACB815B8; Mon, 28 Feb 2022 17:31:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8FE02C340F1; Mon, 28 Feb 2022 17:31:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1646069486; bh=p8i4Tq1JhKBA4elh4tr7Rvc8i93blPcxJzdKjp36pJI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QD1VEN0iKUqLavvXScMRd/Zk2Zk1yhbBFEQOvi+95Is/LtJz9eUJLjqI4vjQTjJwJ Z/RPE+mKOb3fDXDySPyXAJPWi+dt1BwMgtpr1SadVAfyIjoF3SwwKBBwEfnc9j/Cnj wVhKfJNd5vIGUXqgtmvZwEBulk4ZCbDTpYMpoId0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, stable , Daehwan Jung Subject: [PATCH 5.4 38/53] usb: gadget: rndis: add spinlock for rndis response list Date: Mon, 28 Feb 2022 18:24:36 +0100 Message-Id: <20220228172251.050260261@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220228172248.232273337@linuxfoundation.org> References: <20220228172248.232273337@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=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: Daehwan Jung commit aaaba1c86d04dac8e49bf508b492f81506257da3 upstream. There's no lock for rndis response list. It could cause list corruption if there're two different list_add at the same time like below. It's better to add in rndis_add_response / rndis_free_response / rndis_get_next_response to prevent any race condition on response list. [ 361.894299] [1: irq/191-dwc3:16979] list_add corruption. next->prev should be prev (ffffff80651764d0), but was ffffff883dc36f80. (next=ffffff80651764d0). [ 361.904380] [1: irq/191-dwc3:16979] Call trace: [ 361.904391] [1: irq/191-dwc3:16979] __list_add_valid+0x74/0x90 [ 361.904401] [1: irq/191-dwc3:16979] rndis_msg_parser+0x168/0x8c0 [ 361.904409] [1: irq/191-dwc3:16979] rndis_command_complete+0x24/0x84 [ 361.904417] [1: irq/191-dwc3:16979] usb_gadget_giveback_request+0x20/0xe4 [ 361.904426] [1: irq/191-dwc3:16979] dwc3_gadget_giveback+0x44/0x60 [ 361.904434] [1: irq/191-dwc3:16979] dwc3_ep0_complete_data+0x1e8/0x3a0 [ 361.904442] [1: irq/191-dwc3:16979] dwc3_ep0_interrupt+0x29c/0x3dc [ 361.904450] [1: irq/191-dwc3:16979] dwc3_process_event_entry+0x78/0x6cc [ 361.904457] [1: irq/191-dwc3:16979] dwc3_process_event_buf+0xa0/0x1ec [ 361.904465] [1: irq/191-dwc3:16979] dwc3_thread_interrupt+0x34/0x5c Fixes: f6281af9d62e ("usb: gadget: rndis: use list_for_each_entry_safe") Cc: stable Signed-off-by: Daehwan Jung Link: https://lore.kernel.org/r/1645507768-77687-1-git-send-email-dh10.jung@samsung.com Signed-off-by: Greg Kroah-Hartman --- drivers/usb/gadget/function/rndis.c | 8 ++++++++ drivers/usb/gadget/function/rndis.h | 1 + 2 files changed, 9 insertions(+) --- a/drivers/usb/gadget/function/rndis.c +++ b/drivers/usb/gadget/function/rndis.c @@ -922,6 +922,7 @@ struct rndis_params *rndis_register(void params->resp_avail = resp_avail; params->v = v; INIT_LIST_HEAD(¶ms->resp_queue); + spin_lock_init(¶ms->resp_lock); pr_debug("%s: configNr = %d\n", __func__, i); return params; @@ -1015,12 +1016,14 @@ void rndis_free_response(struct rndis_pa { rndis_resp_t *r, *n; + spin_lock(¶ms->resp_lock); list_for_each_entry_safe(r, n, ¶ms->resp_queue, list) { if (r->buf == buf) { list_del(&r->list); kfree(r); } } + spin_unlock(¶ms->resp_lock); } EXPORT_SYMBOL_GPL(rndis_free_response); @@ -1030,14 +1033,17 @@ u8 *rndis_get_next_response(struct rndis if (!length) return NULL; + spin_lock(¶ms->resp_lock); list_for_each_entry_safe(r, n, ¶ms->resp_queue, list) { if (!r->send) { r->send = 1; *length = r->length; + spin_unlock(¶ms->resp_lock); return r->buf; } } + spin_unlock(¶ms->resp_lock); return NULL; } EXPORT_SYMBOL_GPL(rndis_get_next_response); @@ -1054,7 +1060,9 @@ static rndis_resp_t *rndis_add_response( r->length = length; r->send = 0; + spin_lock(¶ms->resp_lock); list_add_tail(&r->list, ¶ms->resp_queue); + spin_unlock(¶ms->resp_lock); return r; } --- a/drivers/usb/gadget/function/rndis.h +++ b/drivers/usb/gadget/function/rndis.h @@ -174,6 +174,7 @@ typedef struct rndis_params { void (*resp_avail)(void *v); void *v; struct list_head resp_queue; + spinlock_t resp_lock; } rndis_params; /* RNDIS Message parser and other useless functions */