Bug 264472 - ib_sysfs: use the correct unregister function
Summary: ib_sysfs: use the correct unregister function
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: Unspecified
Hardware: Any Any
: --- Affects Many People
Assignee: Hans Petter Selasky
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-06-05 12:32 UTC by ruc_gongyuanjun
Modified: 2022-06-27 08:15 UTC (History)
2 users (show)

See Also:


Attachments
a possible patch (1.04 KB, application/mbox)
2022-06-05 12:32 UTC, ruc_gongyuanjun
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description ruc_gongyuanjun 2022-06-05 12:32:08 UTC
Created attachment 234461 [details]
a possible patch

In ib_device_register_sysfs, on the error path, device_unregister
is called. device_register deletes the device, which will cause use
after free. device_del should be called which undose device_add but
not delete the device.
Comment 1 Hans Petter Selasky freebsd_committer freebsd_triage 2022-06-17 17:46:40 UTC
I need to check with the LinuxKPI if this is correct or not.
Comment 2 commit-hook freebsd_committer freebsd_triage 2022-06-21 09:34:20 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=55d183367104e3efcdaa936f65e4f82303871271

commit 55d183367104e3efcdaa936f65e4f82303871271
Author:     Hans Petter Selasky <hselasky@FreeBSD.org>
AuthorDate: 2022-06-21 09:07:06 +0000
Commit:     Hans Petter Selasky <hselasky@FreeBSD.org>
CommitDate: 2022-06-21 09:33:27 +0000

    ibcore: Fix sysfs registration error flow

    The kernel commit cited below restructured ib device management
    so that the device kobject is initialized in ib_alloc_device.

    As part of the restructuring, the kobject is now initialized in
    procedure ib_alloc_device, and is later added to the device hierarchy
    in the ib_register_device call stack, in procedure
    ib_device_register_sysfs (which calls device_add).

    However, in the ib_device_register_sysfs error flow, if an error
    occurs following the call to device_add, the cleanup procedure
    device_unregister is called. This call results in the device object
    being deleted -- which results in various use-after-free crashes.

    The correct cleanup call is device_del -- which undoes device_add
    without deleting the device object.

    The device object will then (correctly) be deleted in the
    ib_register_device caller's error cleanup flow, when the caller invokes
    ib_dealloc_device.

    Linux commit:
    b312be3d87e4c80872cbea869e569175c5eb0f9a

    PR:             264472
    MFC after:      3 days
    Sponsored by:   NVIDIA Networking

 sys/ofed/drivers/infiniband/core/ib_sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Comment 3 Hans Petter Selasky freebsd_committer freebsd_triage 2022-06-21 09:35:31 UTC
Thank you!
Comment 4 commit-hook freebsd_committer freebsd_triage 2022-06-27 08:15:27 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=1ecd211915ff0dd1989db9e8052794f1ea872772

commit 1ecd211915ff0dd1989db9e8052794f1ea872772
Author:     Hans Petter Selasky <hselasky@FreeBSD.org>
AuthorDate: 2022-06-21 09:07:06 +0000
Commit:     Hans Petter Selasky <hselasky@FreeBSD.org>
CommitDate: 2022-06-27 08:14:49 +0000

    ibcore: Fix sysfs registration error flow

    The kernel commit cited below restructured ib device management
    so that the device kobject is initialized in ib_alloc_device.

    As part of the restructuring, the kobject is now initialized in
    procedure ib_alloc_device, and is later added to the device hierarchy
    in the ib_register_device call stack, in procedure
    ib_device_register_sysfs (which calls device_add).

    However, in the ib_device_register_sysfs error flow, if an error
    occurs following the call to device_add, the cleanup procedure
    device_unregister is called. This call results in the device object
    being deleted -- which results in various use-after-free crashes.

    The correct cleanup call is device_del -- which undoes device_add
    without deleting the device object.

    The device object will then (correctly) be deleted in the
    ib_register_device caller's error cleanup flow, when the caller invokes
    ib_dealloc_device.

    Linux commit:
    b312be3d87e4c80872cbea869e569175c5eb0f9a

    PR:             264472
    Sponsored by:   NVIDIA Networking

    (cherry picked from commit 55d183367104e3efcdaa936f65e4f82303871271)

 sys/ofed/drivers/infiniband/core/ib_sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)