Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

wifi: brcmfmac: pcie: handle randbuf allocation failure

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 <arnd@arndb.de>
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
Signed-off-by: Kalle Valo <kvalo@kernel.org>
Link: https://msgid.link/20240306140437.18177-1-duoming@zju.edu.cn

authored by

Duoming Zhou and committed by
Kalle Valo
316f790e 789c1718

+10 -5
+10 -5
drivers/net/wireless/broadcom/brcm80211/brcmfmac/pcie.c
··· 1675 1675 #define BRCMF_RANDOM_SEED_MAGIC 0xfeedc0de 1676 1676 #define BRCMF_RANDOM_SEED_LENGTH 0x100 1677 1677 1678 + static noinline_for_stack void 1679 + brcmf_pcie_provide_random_bytes(struct brcmf_pciedev_info *devinfo, u32 address) 1680 + { 1681 + u8 randbuf[BRCMF_RANDOM_SEED_LENGTH]; 1682 + 1683 + get_random_bytes(randbuf, BRCMF_RANDOM_SEED_LENGTH); 1684 + memcpy_toio(devinfo->tcm + address, randbuf, BRCMF_RANDOM_SEED_LENGTH); 1685 + } 1686 + 1678 1687 static int brcmf_pcie_download_fw_nvram(struct brcmf_pciedev_info *devinfo, 1679 1688 const struct firmware *fw, void *nvram, 1680 1689 u32 nvram_len) ··· 1726 1717 .length = cpu_to_le32(rand_len), 1727 1718 .magic = cpu_to_le32(BRCMF_RANDOM_SEED_MAGIC), 1728 1719 }; 1729 - void *randbuf; 1730 1720 1731 1721 /* Some Apple chips/firmwares expect a buffer of random 1732 1722 * data to be present before NVRAM ··· 1737 1729 sizeof(footer)); 1738 1730 1739 1731 address -= rand_len; 1740 - randbuf = kzalloc(rand_len, GFP_KERNEL); 1741 - get_random_bytes(randbuf, rand_len); 1742 - memcpy_toio(devinfo->tcm + address, randbuf, rand_len); 1743 - kfree(randbuf); 1732 + brcmf_pcie_provide_random_bytes(devinfo, address); 1744 1733 } 1745 1734 } else { 1746 1735 brcmf_dbg(PCIE, "No matching NVRAM file found %s\n",