Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp1086737lqs; Wed, 6 Mar 2024 06:06:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX9/Hfy729jk6GyokhqpTWbMNxRqufa2gH8Kg4aAnYHRSIlFepaO0noiLmUMfH0viUHuyFQvVJ88nO0yQnly70ldlWCbboCQvLsjhCAcw== X-Google-Smtp-Source: AGHT+IGRjrVvdQ/nGRKlkhNgbra2YIexQzKBItSkf1tIQO+yEtEsLUSMsHBZiKmWtcGr0qhprEbn X-Received: by 2002:a6b:c9cb:0:b0:7c8:800f:dce5 with SMTP id z194-20020a6bc9cb000000b007c8800fdce5mr225214iof.16.1709733976615; Wed, 06 Mar 2024 06:06:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709733976; cv=pass; d=google.com; s=arc-20160816; b=CMN8EasSyGnDLOqMvLFC2NV1+8Rvx7rYYPz6NoHrKMMTtVvzYFPKsGeRrb5fOgWir7 Mz0aUPvWyp8ciMhxRNALDGvDIMTG3ogk16gaWuPB10Sv2dbJFwjyAIGlFrvYgy3jlDdW W+2AoA+H5cHVkhFVIVLovyfRdItK/O/hRt2VMmRdfyhLE6Dgd+CxXyyx7AJQ7OiIgv0M kbp7qacETV1FH46RLw81bMalm9PzmqKlnjs2+3iSZuYmqrT0cbeNyGJAb1PsZ4/Be8X4 hBN25Yc4DQuA3Bt9SA2M7uXfGWxIdyNDcRFtQx3JRkgtIUKbuY/wPnq0sSOVpyvgrsWh eDlw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-id:precedence:message-id:date :subject:cc:to:from; bh=eYuM/LcQ7V4dE4SUyLnNjMvVxLCqn5yT4MctUy4Aq9g=; fh=65px+FN6SsZ5uUzFNDubVXMlr6PvYezE4Di2aImKo6g=; b=tJsNL6CyLuqY50OpkSghiGd2qp1dNQgPYb522Gqx2yQTjYP9V1/NKcWvH2Q0DQRDdc ti8Rzv1h65IeTfQlipcxEFPpC+2irXzQWG6BeRZRKwuu/rn4HBPYu47ubIQ9Q0gYHiNB C2rPeGUsjcprVIkD6orw2xkpKYcSBZ0503MjTlyeALXTTVMofdk47iEfDK0ltOLpnFye TG8rKDFZwjlRfTtPCPq+KjEVJJg4cgqerFEqqn8/CbmA+wzbm+zjwTZ0hDl+Cve2dm6U x+USJWHfR61Q+O2HOcXdinVP6UOPuNtPoSJM9wkjnN9SUFBSBGeVcBiq5bt1sJSVItwJ r4bw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=zju.edu.cn); spf=pass (google.com: domain of linux-wireless+bounces-4435-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-wireless+bounces-4435-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id z20-20020a056638241400b00474e0c312a7si3269737jat.196.2024.03.06.06.06.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 06:06:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-wireless+bounces-4435-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=zju.edu.cn); spf=pass (google.com: domain of linux-wireless+bounces-4435-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-wireless+bounces-4435-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 3CE25284AAD for ; Wed, 6 Mar 2024 14:06:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AE5E5131E32; Wed, 6 Mar 2024 14:05:16 +0000 (UTC) X-Original-To: linux-wireless@vger.kernel.org Received: from zg8tmtu5ljg5lje1ms4xmtka.icoremail.net (zg8tmtu5ljg5lje1ms4xmtka.icoremail.net [159.89.151.119]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E279F131744; Wed, 6 Mar 2024 14:05:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.89.151.119 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709733916; cv=none; b=Dqa3q6u3DRulFTh76UN/uX4R/lidW3hoPkQlmfP+J+cyWfL+1VqwUTa/XJNDU+46oMiL3X2WVAq1SYDcydPm4TK5u7dg1YHT6w4qneBod4rjNNFawnrmw9/y/q1A9hvN+7O88wW7s9c2hhaXWkRlVTvX4KWtsNgnwzgQLdgz8PQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709733916; c=relaxed/simple; bh=c+XZMGGuRxFnqFTMxWLbMgz0UPxrybXa/Oa8LOWX8MA=; h=From:To:Cc:Subject:Date:Message-Id; b=gtYnVzMy1xrIF07tzlWlBa5DEqxFVDDbS5FEKAmGvjL7NgajjS2Y0ONmE9gGwZtRoiM97G4F/tHkD5bdQpUNrgzRMouoU3+Mguwm6VdJflKt1tiHtLRwDfiqEVVMWrBBTeVSWILy565ZJt6yXT9oxkQ8atotSxisDTxpDf0Z8y4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=zju.edu.cn; spf=pass smtp.mailfrom=zju.edu.cn; arc=none smtp.client-ip=159.89.151.119 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=zju.edu.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zju.edu.cn Received: from ubuntu.localdomain (unknown [106.117.76.127]) by mail-app3 (Coremail) with SMTP id cC_KCgAnLjn6d+hlumYFAg--.2983S2; Wed, 06 Mar 2024 22:04:52 +0800 (CST) From: Duoming Zhou To: linux-kernel@vger.kernel.org Cc: brcm80211-dev-list.pdl@broadcom.com, brcm80211@lists.linux.dev, linux-wireless@vger.kernel.org, minipli@grsecurity.net, hdegoede@redhat.com, kvalo@kernel.org, arend.vanspriel@broadcom.com, arnd@arndb.de, Duoming Zhou Subject: [PATCH v2] wifi: brcmfmac: pcie: handle randbuf allocation failure Date: Wed, 6 Mar 2024 22:04:37 +0800 Message-Id: <20240306140437.18177-1-duoming@zju.edu.cn> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID:cC_KCgAnLjn6d+hlumYFAg--.2983S2 X-Coremail-Antispam: 1UD129KBjvJXoWxGw4UJFW7Zr17Xr1rGryrXrb_yoW5Gr1Dpa y5JFW29r1UGa1Sg39YkFZ7Cr1Yy3ZYk3y0gw4xG397uF15Ary8Zry09FyF9r1UurWIya47 tayUtF9rWFsxt37anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU9Y14x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26w1j6s0DM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4U JVWxJr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Gc CE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E 2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJV W8JwACjcxG0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1lFIxGxcIEc7CjxVA2 Y2ka0xkIwI1lc7CjxVAaw2AFwI0_Jw0_GFyl42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x 0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2 zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF 4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWU CwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCT nIWIevJa73UjIFyTuYvjfUonmRUUUUU X-CM-SenderInfo: qssqjiasttq6lmxovvfxof0/1tbiAwQMAWXnadMZWQAusc Precedence: bulk X-Mailing-List: linux-wireless@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: The kzalloc() in brcmf_pcie_download_fw_nvram() will return null if the physical memory has run out. As a result, if we use get_random_bytes() to generate random bytes in the randbuf, the null pointer dereference bug will happen. In order to prevent allocation failure, this patch adds a separate function using buffer on kernel stack to generate random bytes in the randbuf, which could prevent the kernel stack from overflow. Fixes: 91918ce88d9f ("wifi: brcmfmac: pcie: Provide a buffer of random bytes to the device") Suggested-by: Arnd Bergmann Signed-off-by: Duoming Zhou --- Changes in v2: - Add a separate function using buffer on stack. .../wireless/broadcom/brcm80211/brcmfmac/pcie.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c index d7fb88bb6ae..06698a714b5 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c @@ -1675,6 +1675,15 @@ struct brcmf_random_seed_footer { #define BRCMF_RANDOM_SEED_MAGIC 0xfeedc0de #define BRCMF_RANDOM_SEED_LENGTH 0x100 +static noinline_for_stack void +brcmf_pcie_provide_random_bytes(struct brcmf_pciedev_info *devinfo, u32 address) +{ + u8 randbuf[BRCMF_RANDOM_SEED_LENGTH]; + + get_random_bytes(randbuf, BRCMF_RANDOM_SEED_LENGTH); + memcpy_toio(devinfo->tcm + address, randbuf, BRCMF_RANDOM_SEED_LENGTH); +} + static int brcmf_pcie_download_fw_nvram(struct brcmf_pciedev_info *devinfo, const struct firmware *fw, void *nvram, u32 nvram_len) @@ -1717,7 +1726,6 @@ static int brcmf_pcie_download_fw_nvram(struct brcmf_pciedev_info *devinfo, .length = cpu_to_le32(rand_len), .magic = cpu_to_le32(BRCMF_RANDOM_SEED_MAGIC), }; - void *randbuf; /* Some Apple chips/firmwares expect a buffer of random * data to be present before NVRAM @@ -1729,10 +1737,7 @@ static int brcmf_pcie_download_fw_nvram(struct brcmf_pciedev_info *devinfo, sizeof(footer)); address -= rand_len; - randbuf = kzalloc(rand_len, GFP_KERNEL); - get_random_bytes(randbuf, rand_len); - memcpy_toio(devinfo->tcm + address, randbuf, rand_len); - kfree(randbuf); + brcmf_pcie_provide_random_bytes(devinfo, address); } } else { brcmf_dbg(PCIE, "No matching NVRAM file found %s\n", -- 2.17.1