Skip to content

evdev: create sysctl entries before cdev to close userspace race#386

Open
ericbsd wants to merge 1 commit into
stable/15from
evdev-patch
Open

evdev: create sysctl entries before cdev to close userspace race#386
ericbsd wants to merge 1 commit into
stable/15from
evdev-patch

Conversation

@ericbsd
Copy link
Copy Markdown
Contributor

@ericbsd ericbsd commented May 19, 2026

evdev_register_common() was creating the character device before registering the kern.evdev.input.N.* sysctl entries. The moment the cdev appears, devd fires a CREATE event for /dev/input/eventN, and userspace libraries (e.g. libudev-devd) immediately call sysctlbyname("kern.evdev.input.N.name") to enumerate device capabilities. With the old ordering, that call could arrive before the sysctl tree was populated, causing it to fail. The result was that the device was not recognised by the input stack, leaving keyboards and other HID devices non-functional after plug-in or resume from suspend.

Fix the race by calling evdev_sysctl_create() before evdev_cdev_create(). On cdev failure, free the already-registered sysctl context with sysctl_ctx_free() to avoid leaking it.

Summary by Sourcery

Fix evdev device registration ordering to avoid userspace races when probing new input devices.

Bug Fixes:

  • Resolve a race where userspace could query evdev sysctl entries before they were registered when a new input device cdev appeared, causing device enumeration to fail.

Enhancements:

  • Ensure sysctl context is freed if character device creation fails after sysctl registration to prevent resource leaks.

evdev_register_common() was creating the character device before
registering the kern.evdev.input.N.* sysctl entries. The moment
the cdev appears, devd fires a CREATE event for /dev/input/eventN,
and userspace libraries (e.g. libudev-devd) immediately call
sysctlbyname("kern.evdev.input.N.name") to enumerate device
capabilities. With the old ordering, that call could arrive before
the sysctl tree was populated, causing it to fail. The result was
that the device was not recognised by the input stack, leaving
keyboards and other HID devices non-functional after plug-in or
resume from suspend.

Fix the race by calling evdev_sysctl_create() before
evdev_cdev_create(). On cdev failure, free the already-registered
sysctl context with sysctl_ctx_free() to avoid leaking it.
@ericbsd ericbsd requested review from a team as code owners May 19, 2026 11:08
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 19, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts evdev device registration order so sysctl entries are created before the character device node, closing a userspace race, and ensures sysctl context is freed on cdev creation failure.

Sequence diagram for updated evdev device registration and userspace sysctl access

sequenceDiagram
    participant KernelEvdev
    participant Sysctl
    participant Cdev
    participant UserspaceLib

    KernelEvdev->>KernelEvdev: evdev_register_common
    KernelEvdev->>Sysctl: evdev_sysctl_create
    KernelEvdev->>Cdev: evdev_cdev_create
    UserspaceLib->>KernelEvdev: sysctlbyname
    KernelEvdev-->>UserspaceLib: sysctl values from kern.evdev.input.N.*
Loading

Flow diagram for evdev_register_common error handling with sysctl_ctx_free

flowchart TD
    A[evdev_register_common] --> B[evdev_sysctl_create]
    B --> C[evdev_cdev_create]
    C --> D{ret == 0?}
    D -- Yes --> E[Success]
    D -- No --> F["sysctl_ctx_free(ev_sysctl_ctx)"]
    F --> G[bail_out / return error]
Loading

File-Level Changes

Change Details Files
Reorder evdev registration so sysctl entries are created before the character device node and clean up sysctl context on failure.
  • Call evdev_sysctl_create(evdev) before evdev_cdev_create(evdev) to ensure sysctl nodes exist when devd emits the device CREATE event
  • On evdev_cdev_create() failure, free the previously created sysctl context with sysctl_ctx_free(&evdev->ev_sysctl_ctx) before bailing out
  • Add an explanatory comment documenting the userspace race between cdev creation and sysctl availability
sys/dev/evdev/evdev.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions
Copy link
Copy Markdown

Thank you for taking the time to contribute to FreeBSD!
There is an issue that needs to be fixed:

  • Missing Signed-off-by lines9756a73

Please review CONTRIBUTING.md, then update and push your branch again.

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Now that sysctls are created before the cdev, consider ensuring sysctl_ctx_free(&evdev->ev_sysctl_ctx) is called on all subsequent failure paths in evdev_register_common() (not just the evdev_cdev_create() error case) to avoid leaking the sysctl context when later steps fail.
  • If feasible, it may be safer for evdev_sysctl_create() to return an error code on failure instead of void, allowing evdev_register_common() to bail out early and cleanly roll back the partially initialized sysctl context.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Now that sysctls are created before the cdev, consider ensuring `sysctl_ctx_free(&evdev->ev_sysctl_ctx)` is called on all subsequent failure paths in `evdev_register_common()` (not just the `evdev_cdev_create()` error case) to avoid leaking the sysctl context when later steps fail.
- If feasible, it may be safer for `evdev_sysctl_create()` to return an error code on failure instead of `void`, allowing `evdev_register_common()` to bail out early and cleanly roll back the partially initialized sysctl context.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@ericbsd
Copy link
Copy Markdown
Contributor Author

ericbsd commented May 19, 2026

@emaste I would like your opinion on this patch when you have time to look at it.

@ericbsd
Copy link
Copy Markdown
Contributor Author

ericbsd commented May 19, 2026

I need to think more about it. I just found an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

2 participants