Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp2240936rwb; Sat, 8 Oct 2022 04:52:04 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5MAzj0doVOg5MF9qOWS6uOC5mTmJEP6qAM4mV59qx39uTlcB7q3DxvO3XZOaEfXubcewTz X-Received: by 2002:a17:907:2c62:b0:77a:e3ce:5ef0 with SMTP id ib2-20020a1709072c6200b0077ae3ce5ef0mr7504176ejc.553.1665229924224; Sat, 08 Oct 2022 04:52:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665229924; cv=none; d=google.com; s=arc-20160816; b=dh8b/orR2UfwI4cy2BiH4vrIwMXW78/m3TQevVjGdzm6HnXgyy5MWIhdw/AOlpioJu 5+CXdTBU14PUffpwmSjB2uUN7l7YRWmgGXaGBHqr50F0xQIMsA8CNsQPG4Mz/mI39gvH U8czMnhfZYbDjd3haGU6IG+1Nj+sKw3/GFPZMK3nVY5Ad5HOqfJCwdvIZQE3jOt1X7PP Slieote56f/wadt4O5tSQPHORVtCuV7oIJ0h5+kivQf2v1nI02nlzWnHxekNOqoInk37 4fG+vChQiNZPoaSWgrL/6h2faWilx0HKFDVHodb3X5lQO5IHmxXYdGIKmWq5t3B9LHqq l7tA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=eGhQBNtLRT/iMFU1ypAtNfUplajfF0B7mQZ1FqvKb6I=; b=ZwtvESgnmrCLtk/CO8THQTKKipMa1JHAlJ3axLFQIF685FGRriYwn1kSXWZngOkSnw hh6hOBUYjN8GMJumyQVpRNtEU0WT4CTkcMcmiDOqFvfmW6jfN0NcML1ieuoYiu5ao+DQ Sg3WpzqVaN9bCcPluQZABzXZVZEn/ulx1klWZWra7LIzhXjPHyZ6LBeBBOgha1BPBSKX C4I3aJAx1IXdKvNqf5CHl6+sZZn1XeZBmOdbIafLUQ/et2jMbT30PHRKNRRq2EL3R43k qEV9KXk0/OqLURtv47WlJ4q4/S/gn6MDEWaSelrcx6fdFSpKo71AtLFjQ1UlvOQr3i0N 7Shg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ispras.ru header.s=default header.b=OmsIvw93; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h16-20020aa7c610000000b004597db487cdsi4075596edq.604.2022.10.08.04.51.45; Sat, 08 Oct 2022 04:52:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-wireless-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=@ispras.ru header.s=default header.b=OmsIvw93; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229593AbiJHLt4 (ORCPT + 61 others); Sat, 8 Oct 2022 07:49:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229379AbiJHLty (ORCPT ); Sat, 8 Oct 2022 07:49:54 -0400 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48A87193FD; Sat, 8 Oct 2022 04:49:53 -0700 (PDT) Received: from localhost.localdomain (unknown [46.242.14.200]) by mail.ispras.ru (Postfix) with ESMTPSA id 002464077AE4; Sat, 8 Oct 2022 11:49:49 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru 002464077AE4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ispras.ru; s=default; t=1665229790; bh=eGhQBNtLRT/iMFU1ypAtNfUplajfF0B7mQZ1FqvKb6I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OmsIvw93BGjxV+uEf5ZUFZFc5MwdQyTTCo+AYcHXk2UF/PRppEVBUidOkzG2sqKPy gWT/IJbrkjOSfe4NGHUF/VNU5W93l1zjr+UmfvevwkdKICcfvlEYDVMrHUI8GQSlcA W6AkJ8cEXeqT94el3yHCbNHMaEBgUW5T+HP21fME= From: Fedor Pchelkin To: =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= , Kalle Valo Cc: Fedor Pchelkin , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , linux-wireless@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Alexey Khoroshilov , lvc-project@linuxtesting.org Subject: [PATCH v2] ath9k: hif_usb: Fix use-after-free in ath9k_hif_usb_reg_in_cb() Date: Sat, 8 Oct 2022 14:49:17 +0300 Message-Id: <20221008114917.21404-1-pchelkin@ispras.ru> X-Mailer: git-send-email 2.25.1 In-Reply-To: <87ilkvcys7.fsf@toke.dk> References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS 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-wireless@vger.kernel.org It is possible that skb is freed in ath9k_htc_rx_msg(), then usb_submit_urb() fails and we try to free skb again. It causes use-after-free bug. Moreover, if alloc_skb() fails, urb->context becomes NULL but rx_buf is not freed and there can be a memory leak. The patch removes unnecessary nskb and makes skb processing more clear: it is supposed that ath9k_htc_rx_msg() either frees old skb or passes its managing to another callback function. Found by Linux Verification Center (linuxtesting.org) with Syzkaller. Fixes: 3deff76095c4 ("ath9k_htc: Increase URB count for REG_IN pipe") Signed-off-by: Fedor Pchelkin Signed-off-by: Alexey Khoroshilov --- v1->v2: add comment about freeing an skb and remove double blank line drivers/net/wireless/ath/ath9k/hif_usb.c | 28 +++++++++++++----------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/drivers/net/wireless/ath/ath9k/hif_usb.c b/drivers/net/wireless/ath/ath9k/hif_usb.c index 4d9002a9d082..401b408cb7a4 100644 --- a/drivers/net/wireless/ath/ath9k/hif_usb.c +++ b/drivers/net/wireless/ath/ath9k/hif_usb.c @@ -708,14 +708,13 @@ static void ath9k_hif_usb_reg_in_cb(struct urb *urb) struct rx_buf *rx_buf = (struct rx_buf *)urb->context; struct hif_device_usb *hif_dev = rx_buf->hif_dev; struct sk_buff *skb = rx_buf->skb; - struct sk_buff *nskb; int ret; if (!skb) return; if (!hif_dev) - goto free; + goto free_skb; switch (urb->status) { case 0: @@ -724,7 +723,7 @@ static void ath9k_hif_usb_reg_in_cb(struct urb *urb) case -ECONNRESET: case -ENODEV: case -ESHUTDOWN: - goto free; + goto free_skb; default: skb_reset_tail_pointer(skb); skb_trim(skb, 0); @@ -735,25 +734,27 @@ static void ath9k_hif_usb_reg_in_cb(struct urb *urb) if (likely(urb->actual_length != 0)) { skb_put(skb, urb->actual_length); - /* Process the command first */ + /* + * Process the command first. + * skb is either freed here or passed to be + * managed to another callback function. + */ ath9k_htc_rx_msg(hif_dev->htc_handle, skb, skb->len, USB_REG_IN_PIPE); - - nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC); - if (!nskb) { + skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC); + if (!skb) { dev_err(&hif_dev->udev->dev, "ath9k_htc: REG_IN memory allocation failure\n"); - urb->context = NULL; - return; + goto free_rx_buf; } - rx_buf->skb = nskb; + rx_buf->skb = skb; usb_fill_int_urb(urb, hif_dev->udev, usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE), - nskb->data, MAX_REG_IN_BUF_SIZE, + skb->data, MAX_REG_IN_BUF_SIZE, ath9k_hif_usb_reg_in_cb, rx_buf, 1); } @@ -762,12 +763,13 @@ static void ath9k_hif_usb_reg_in_cb(struct urb *urb) ret = usb_submit_urb(urb, GFP_ATOMIC); if (ret) { usb_unanchor_urb(urb); - goto free; + goto free_skb; } return; -free: +free_skb: kfree_skb(skb); +free_rx_buf: kfree(rx_buf); urb->context = NULL; } -- 2.25.1