Re: memory leakage in ncm_wrap_ntb() in USB ncm mode with kernel 5.15

From: Oliver Neukum
Date: Tue May 21 2024 - 09:19:02 EST




On 20.05.24 16:32, nanfengwq@xxxxxxxx wrote:
hello:
In embedded devices with relatively small memory, if the transfer speed of ncm is fast and other programs occupy CPU memory, it is likely that the return value ncm ->skd_tx_data of alloc_skb() is NULL, and the code is likely to enter err. If skb2 is not processed in err, it will cause memory leakage.

Hi,

apart from the submission process, could you please explain how this
condition may happem?

if (skb) {

[..]
if (ncm->skb_tx_data

We know ncm->skb_tx_data != NULL && skb != NULL

&& (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE
|| (ncm->skb_tx_data->len +
div + rem + skb->len +
ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len))
> max_size)) {
skb2 = package_for_tx(ncm);


if (!ncm->skb_tx_data) {

We know ncm->skb_tx_data == NULL && skb != NULL

/* Create a new skb for the NTH and datagrams. */
ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC);
if (!ncm->skb_tx_data)
goto err;

It seems to me that either

skb2 = package_for_tx(ncm);

or

ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC);

can be executed. The code paths seem to be mutually exclusive.

Regards
Oliver