Bug 282237 - bhyve: usb_mouse.c segfaults due to incomplete NULL checking
Summary: bhyve: usb_mouse.c segfaults due to incomplete NULL checking
Status: In Progress
Alias: None
Product: Base System
Classification: Unclassified
Component: bhyve (show other bugs)
Version: 15.0-CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: Warner Losh
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-10-21 04:30 UTC by Jack Bendtsen
Modified: 2026-01-10 01:51 UTC (History)
0 users

See Also:
linimon: mfc-stable15?
linimon: mfc-stable14?


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jack Bendtsen 2024-10-21 04:30:15 UTC
Some of the cases inside umouse_request() (usr.sbin/bhyve/usb_mouse.c) use the data component of an event, while only partially checking if it's NULL.

For example:
```
	case UREQ(UR_GET_STATUS, UT_READ_INTERFACE):
	case UREQ(UR_GET_STATUS, UT_READ_ENDPOINT):
		DPRINTF(("umouse: (UR_GET_STATUS, UT_READ_INTERFACE)"));
		if (data != NULL && len > 1) {
			USETW(udata, 0);
			data->blen = len - 2;
			data->bdone += 2;
		}
		eshort = data->blen > 0;
		break;
```
As you can see, 'data' has a NULL check, but then 'data' is immediately deferenced anyway after the check regardless of if it's NULL or not.

There are actually four occurrences of this same bug, each in a different case in this switch block.

Here's a patch that can be applied to CURRENT that fixes the issue:
```
533c533
< 		eshort = data->blen > 0;
---
> 		eshort = data != NULL && data->blen > 0;
544c544
< 		eshort = data->blen > 0;
---
> 		eshort = data != NULL && data->blen > 0;
629c629
< 		eshort = data->blen > 0;
---
> 		eshort = data != NULL && data->blen > 0;
638c638
< 		eshort = data->blen > 0;
---
> 		eshort = data != NULL && data->blen > 0;
```

Cheers,
Jack Bendtsen
Comment 1 commit-hook freebsd_committer freebsd_triage 2026-01-09 20:17:44 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=7631790422464de1aec309018e2c444defe5f629

commit 7631790422464de1aec309018e2c444defe5f629
Author:     Jack Bendtsen <jackdbendtsen@gmail.com>
AuthorDate: 2025-06-19 07:40:31 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2026-01-09 20:17:13 +0000

    Fix NULL deref segfault in bhyve's usb_mouse.c

    Some of the cases inside umouse_request() (usr.sbin/bhyve/usb_mouse.c)
    use the data component of an event, while only partially checking if
    it's NULL. 'data' has a NULL check, but then 'data' is immediately
    deferenced anyway after the check regardless of if it's NULL or not.

    For example:
            case UREQ(UR_GET_STATUS, UT_READ_INTERFACE):
            case UREQ(UR_GET_STATUS, UT_READ_ENDPOINT):
                    DPRINTF(("umouse: (UR_GET_STATUS, UT_READ_INTERFACE)"));
                    if (data != NULL && len > 1) {
                            USETW(udata, 0);
                            data->blen = len - 2;
                            data->bdone += 2;
                    }
                    eshort = data->blen > 0;
                    break;

    There are actually four occurrences of this same bug, each in a
    different case in this switch block.

    Signed-off-by: Jack Bendtsen <jackdbendtsen@gmail.com>
    PR: 282237
    Reviewed by: imp, jhb, vexeduxr
    MFC After: 1 week
    Pull Request: https://github.com/freebsd/freebsd-src/pull/1728

 usr.sbin/bhyve/usb_mouse.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)