Re: [PATCH] rsi: Fix NULL pointer dereference in kmalloc

From: Joe Perches
Date: Sat Mar 02 2019 - 22:02:12 EST


On Sat, 2019-03-02 at 14:31 -0600, Aditya Pakki wrote:
> kmalloc can fail in rsi_register_rates_channels but memcpy still attempts
> to write to channels. The patch checks and avoids such a situation.
[]
> diff --git a/drivers/net/wireless/rsi/rsi_91x_mac80211.c b/drivers/net/wireless/rsi/rsi_91x_mac80211.c
[]
> @@ -197,6 +197,11 @@ static void rsi_register_rates_channels(struct rsi_hw *adapter, int band)

It'd be better to make this return -ENOMEM on failure
and test in the caller and push the failure up-stack.

>
> if (band == NL80211_BAND_2GHZ) {
> channels = kmalloc(sizeof(rsi_2ghz_channels), GFP_KERNEL);
> + if (!channels) {
> + rsi_dbg(ERR_ZONE, "Failed to allocate memory\n");

Allocation error messages aren't really useful
as there's a generic OOM message.

> + return;
> + }
> +
> memcpy(channels,
> rsi_2ghz_channels,
> sizeof(rsi_2ghz_channels));
> @@ -206,6 +211,11 @@ static void rsi_register_rates_channels(struct rsi_hw *adapter, int band)
> sbands->n_bitrates = ARRAY_SIZE(rsi_rates);
> } else {
> channels = kmalloc(sizeof(rsi_5ghz_channels), GFP_KERNEL);
> + if (!channels) {
> + rsi_dbg(ERR_ZONE, "Failed to allocate memory\n");
> + return;
> + }
> +
> memcpy(channels,
> rsi_5ghz_channels,
> sizeof(rsi_5ghz_channels));