[PATCH v3] nvme: nvme_identify_ns_descs: prevent oob

Keith Busch kbusch at kernel.org
Mon Dec 1 16:27:48 PST 2025


On Mon, Dec 01, 2025 at 10:43:23PM +0300, Eugene Korenevsky wrote:
> -	for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
> -		struct nvme_ns_id_desc *cur = data + pos;
> +	pos = 0;
> +	do {
> +		cur = data + pos;
>  
>  		if (cur->nidl == 0)
>  			break;
> +		/* check ns id desc does not exceed remaining buffer by size */
> +		if (cur->nidl + sizeof(*cur) > NVME_IDENTIFY_DATA_SIZE - pos)
> +			break;
>  
>  		len = nvme_process_ns_desc(ctrl, &info->ids, cur, &csi_seen);
>  		if (len < 0)
>  			break;
>  
> -		len += sizeof(*cur);
> -	}
> +		pos += sizeof(*cur);
> +		pos += len;
> +	} while (pos < NVME_IDENTIFY_DATA_SIZE - sizeof(*cur));

I don't want bikeshed this, but I thought this looked better as a
for-loop. You can just modify the continuing condition instead of
changing the loop type to do-while.



More information about the Linux-nvme mailing list