Bug 219951 - Missing null check in ast_vhub_init_dev
Summary: Missing null check in ast_vhub_init_dev
Status: NEW
Alias: None
Product: Drivers
Classification: Unclassified
Component: USB (show other bugs)
Hardware: All Linux
: P3 normal
Assignee: Default virtual assignee for Drivers/USB
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-03-28 11:15 UTC by henry
Modified: 2025-03-29 00:32 UTC (History)
0 users

See Also:
Kernel Version:
Subsystem:
Regression: No
Bisected commit-id:


Attachments
attachment-31827-0.html (2.08 KB, text/html)
2025-03-28 12:41 UTC, henry
Details
0001-usb-gadget-aspeed-Null-Check-devm_kasprintf-return-i.patch (1.16 KB, application/octet-stream)
2025-03-28 13:14 UTC, henry
Details
attachment-24698-0.html (2.52 KB, text/html)
2025-03-29 00:32 UTC, henry
Details

Description henry 2025-03-28 11:15:22 UTC
usb: gadget: udc: aspeed-vhub: dev.c
Add NULL check in the ast_vhub_init_dev

When devm_kasprintf() fails, it returns a NULL pointer. However, this return value is not properly checked in the function ast_vhub_init_dev. 

A NULL check should be added after the devm_kasprintf call to prevent potential NULL pointer dereference error.

CODE:
	struct ast_vhub_dev *d = &vhub->ports[idx].dev;
	struct device *parent = &vhub->pdev->dev;
	int rc;

	d->vhub = vhub;
	d->index = idx;
	d->name = devm_kasprintf(parent, GFP_KERNEL, "port%d", idx+1);
	d->regs = vhub->regs + 0x100 + 0x10 * idx;

	ast_vhub_init_ep0(vhub, &d->ep0, d);
Comment 1 Greg Kroah-Hartman 2025-03-28 11:27:26 UTC
On Fri, Mar 28, 2025 at 11:15:22AM +0000, bugzilla-daemon@kernel.org wrote:
> When devm_kasprintf() fails, it returns a NULL pointer. However, this return
> value is not properly checked in the function ast_vhub_init_dev. 
> 
> A NULL check should be added after the devm_kasprintf call to prevent
> potential
> NULL pointer dereference error.

Please submit a patch for this if you feel it needs to be fixed up.

thanks,

greg k-h
Comment 2 henry 2025-03-28 12:41:16 UTC
Created attachment 307902 [details]
attachment-31827-0.html

We should first check devm_kasprintf() before setting d->vhub and d->index,
so that if allocation fails, the d struct remains in a clean state.

Patch code:
int ast_vhub_init_dev(struct ast_vhub *vhub, unsigned int idx)
{
struct ast_vhub_dev *d = &vhub->ports[idx].dev;
struct device *parent = &vhub->pdev->dev;
int rc;

/* First allocate the name (before modifying d->vhub/index) */
d->name = devm_kasprintf(parent, GFP_KERNEL, "port%d", idx + 1);
if (!d->name)
return -ENOMEM;

/* Now safe to set vhub and index */
d->vhub = vhub;
d->index = idx;
d->regs = vhub->regs + 0x100 + 0x10 * idx;

ast_vhub_init_ep0(vhub, &d->ep0, d);

<bugzilla-daemon@kernel.org> 于2025年3月28日周五 19:27写道:

> https://bugzilla.kernel.org/show_bug.cgi?id=219951
>
> --- Comment #1 from Greg Kroah-Hartman (greg@kroah.com) ---
> On Fri, Mar 28, 2025 at 11:15:22AM +0000, bugzilla-daemon@kernel.org
> wrote:
> > When devm_kasprintf() fails, it returns a NULL pointer. However, this
> return
> > value is not properly checked in the function ast_vhub_init_dev.
> >
> > A NULL check should be added after the devm_kasprintf call to prevent
> > potential
> > NULL pointer dereference error.
>
> Please submit a patch for this if you feel it needs to be fixed up.
>
> thanks,
>
> greg k-h
>
> --
> You may reply to this email to add a comment.
>
> You are receiving this mail because:
> You reported the bug.
Comment 3 henry 2025-03-28 13:14:23 UTC
Created attachment 307903 [details]
0001-usb-gadget-aspeed-Null-Check-devm_kasprintf-return-i.patch

This is patch file.

henry martin <bsdhenrymartin@gmail.com> 于2025年3月28日周五 20:41写道:

> We should first check devm_kasprintf() before setting d->vhub and
> d->index, so that if allocation fails, the d struct remains in a clean
> state.
>
> Patch code:
> int ast_vhub_init_dev(struct ast_vhub *vhub, unsigned int idx)
> {
> struct ast_vhub_dev *d = &vhub->ports[idx].dev;
> struct device *parent = &vhub->pdev->dev;
> int rc;
>
> /* First allocate the name (before modifying d->vhub/index) */
> d->name = devm_kasprintf(parent, GFP_KERNEL, "port%d", idx + 1);
> if (!d->name)
> return -ENOMEM;
>
> /* Now safe to set vhub and index */
> d->vhub = vhub;
> d->index = idx;
> d->regs = vhub->regs + 0x100 + 0x10 * idx;
>
> ast_vhub_init_ep0(vhub, &d->ep0, d);
>
> <bugzilla-daemon@kernel.org> 于2025年3月28日周五 19:27写道:
>
>> https://bugzilla.kernel.org/show_bug.cgi?id=219951
>>
>> --- Comment #1 from Greg Kroah-Hartman (greg@kroah.com) ---
>> On Fri, Mar 28, 2025 at 11:15:22AM +0000, bugzilla-daemon@kernel.org
>> wrote:
>> > When devm_kasprintf() fails, it returns a NULL pointer. However, this
>> return
>> > value is not properly checked in the function ast_vhub_init_dev.
>> >
>> > A NULL check should be added after the devm_kasprintf call to prevent
>> > potential
>> > NULL pointer dereference error.
>>
>> Please submit a patch for this if you feel it needs to be fixed up.
>>
>> thanks,
>>
>> greg k-h
>>
>> --
>> You may reply to this email to add a comment.
>>
>> You are receiving this mail because:
>> You reported the bug.
>
>
Comment 4 Greg Kroah-Hartman 2025-03-28 21:08:17 UTC
On Fri, Mar 28, 2025 at 01:14:23PM +0000, bugzilla-daemon@kernel.org wrote:
> https://bugzilla.kernel.org/show_bug.cgi?id=219951
> 
> --- Comment #3 from henry (bsdhenrymartin@gmail.com) ---
> This is patch file.

Please read the kernel documentation for how to submit a patch in a way
we can accept it.  We can't take it through bugzilla, sorry.
Comment 5 henry 2025-03-29 00:32:44 UTC
Created attachment 307904 [details]
attachment-24698-0.html

Thank you for your feedback. I've now properly submitted this patch via
email to the relevant mailing lists with you in CC, as required by the
kernel submission process.
Please let me know if there are any issues with the submission format or
content that I should address. I'm happy to make any necessary adjustments.

<bugzilla-daemon@kernel.org> 于2025年3月29日周六 05:08写道:

> https://bugzilla.kernel.org/show_bug.cgi?id=219951
>
> --- Comment #4 from Greg Kroah-Hartman (greg@kroah.com) ---
> On Fri, Mar 28, 2025 at 01:14:23PM +0000, bugzilla-daemon@kernel.org
> wrote:
> > https://bugzilla.kernel.org/show_bug.cgi?id=219951
> >
> > --- Comment #3 from henry (bsdhenrymartin@gmail.com) ---
> > This is patch file.
>
> Please read the kernel documentation for how to submit a patch in a way
> we can accept it.  We can't take it through bugzilla, sorry.
>
> --
> You may reply to this email to add a comment.
>
> You are receiving this mail because:
> You reported the bug.

Note You need to log in before you can comment on or make changes to this bug.