Skip to content

fix(security): allow re-enabling removed sysfs devices - #986

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix-task-398635-re-enable-removed-sysfs-device
Nov 24, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:fix-task-398635-re-enable-removed-sysfs-device

Conversation

@add-uos

@add-uos add-uos commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

根因分析

isSafeSysfsPath() 使用 QFileInfo::canonicalFilePath() 校验路径,要求完整路径必须存在。
但设备被禁用时会通过 /sys/.../remove 从内核移除,对应的 sysfs 目录随之消失,
而「启用」操作正是针对这些不存在的路径恢复设备 —— 导致启用分支永远被
unsafe sysfs path rejected 拒绝,被禁用的设备(如音频)无法从 UI 重新启用。

日志证据:

removeEnable: unsafe sysfs path rejected: "/devices/pci0000:00/0000:00:1f.3"

修复方案

  • 对路径中「已存在的最深祖先目录」做 canonical path 校验,保持符号链接逃逸防护,
    同时兼容设备目录暂不存在的启用场景
  • 新增绝对路径前缀校验,防止相对路径拼接逃逸出 /sys 语义
  • 新增 2 个回归测试用例(禁用设备路径必须校验通过 / 相对路径拒绝)

自测结果

  • SecurityUtilsTest 全部 18 个用例通过(含新增回归用例)
  • 相关 EnableUtilsTest 44 个用例通过,共 62 个无回归
  • 实机验证:被禁用的音频设备(00:1f.3 / 01:00.1)恢复启用成功,声卡正常出声

PMS: TASK-398635

Summary by Sourcery

Allow removed sysfs devices to be safely re-enabled without weakening path traversal protection.

Bug Fixes:

  • Allow re-enabling sysfs devices whose directories were removed when disabled while preserving protection against unsafe paths.

Enhancements:

  • Strengthen sysfs path validation with absolute-path requirements and canonical validation of the deepest existing ancestor.

Tests:

  • Add regression coverage for removed-device paths and rejection of relative paths.

isSafeSysfsPath() required the full sysfs path to exist via
canonicalFilePath(), but a disabled device is removed from the
kernel and its sysfs directory disappears. The enable path was
therefore always rejected with "unsafe sysfs path rejected",
making disabled devices impossible to re-enable.

Validate the canonical path of the deepest existing ancestor
instead, keeping symlink-escape protection while allowing
not-yet-existing device paths; also reject relative paths that
would escape the /sys prefix.

Log: 修复被禁用设备无法重新启用的问题(安全校验误杀不存在的设备路径)
PMS: TASK-398635
Influence: deepin-devicecontrol 设备启用/禁用功能;被禁用的音频等设备可通过 UI 正常恢复
@sourcery-ai

sourcery-ai Bot commented Nov 23, 2026

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Update sysfs path validation to require an absolute path and validate the canonical path of its deepest existing ancestor, allowing removed device paths while preserving symlink-escape protection.

  • deepin-devicemanager-server/deepin-devicecontrol/src/securityutils.cpp L53-56, L61-85

Tests

Add regression tests confirming a nonexistent disabled-device path is accepted and a relative path is rejected.

  • test/deepin-devicemanager-server/src/SecurityUtils/ut_securityutils.cpp L17, L230-262

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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="deepin-devicemanager-server/deepin-devicecontrol/src/securityutils.cpp" line_range="72-77" />
<code_context>
+     */
+    QString existing = QStringLiteral("/sys") + relativePath;
+    while (!QFileInfo::exists(existing)) {
+        const QString parent = QFileInfo(existing).dir().absolutePath();
+        if (parent == QLatin1String("/") || parent == existing) {
+            existing = QStringLiteral("/");
+            break;
+        }
+        existing = parent;
+    }

</code_context>
<issue_to_address>
**Privileged writes escape /sys**

When a requested path traverses a dangling symlink under `/sys` pointing outside it, and its target becomes available before the attribute is opened, the ancestor walk sets `existing` to the in-`/sys` parent, so `QFileInfo(existing).canonicalFilePath()` lets `isSafeSysfsPath` accept the path; the later open follows the link and sends the privileged write outside `/sys`.

Reject dangling symlinks during validation and open the attribute in a way that prevents a symlink target from changing the validated path.
</issue_to_address>

### Comment 2
<location path="deepin-devicemanager-server/deepin-devicecontrol/src/securityutils.cpp" line_range="79-80" />
<code_context>
+        }
+        existing = parent;
+    }

+    QString canonicalPath = QFileInfo(existing).canonicalFilePath();
     if (canonicalPath.isEmpty())
         return false;
</code_context>
<issue_to_address>
**Concurrent toggles leave stale device state**

When disable and enable requests for the same device overlap in that order, a disable request can write `/remove` and pause before inserting its database row; an enable request can then rescan, delete the old row, and read the removal list before the new row is inserted. The disable request finally records the now-enabled device as disabled, so a later startup removes it again.

Serialize enable/disable operations per device and coordinate the sysfs operation with the database update so a stale row cannot be committed after enable completes.
</issue_to_address>

### Comment 3
<location path="deepin-devicemanager-server/deepin-devicecontrol/src/securityutils.cpp" line_range="79-80" />
<code_context>
+        }
+        existing = parent;
+    }

+    QString canonicalPath = QFileInfo(existing).canonicalFilePath();
     if (canonicalPath.isEmpty())
         return false;
</code_context>
<issue_to_address>
**Failed rescan loses disabled-device record**

When the rescan attribute opens successfully but rejects or short-writes the rescan request, the newly accepted missing-device path reaches the enable branch, where `removeEnable` ignores the rescan write result and deletes the device's removal-table row anyway. If the kernel rejects that write after the file opens, the device remains removed but its persisted record is lost, so the UI no longer has the stored disabled-device entry to retry.

Check the rescan write result and only remove the persisted row after confirming the operation succeeded; retain or restore the row on failure.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment on lines +72 to +77
const QString parent = QFileInfo(existing).dir().absolutePath();
if (parent == QLatin1String("/") || parent == existing) {
existing = QStringLiteral("/");
break;
}
existing = parent;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Privileged writes escape /sys

When a requested path traverses a dangling symlink under /sys pointing outside it, and its target becomes available before the attribute is opened, the ancestor walk sets existing to the in-/sys parent, so QFileInfo(existing).canonicalFilePath() lets isSafeSysfsPath accept the path; the later open follows the link and sends the privileged write outside /sys.

Reject dangling symlinks during validation and open the attribute in a way that prevents a symlink target from changing the validated path.

Prompt for AI agents
In `deepin-devicemanager-server/deepin-devicecontrol/src/securityutils.cpp` at lines 72-77:

**Privileged writes escape /sys**

When a requested path traverses a dangling symlink under `/sys` pointing outside it, and its target becomes available before the attribute is opened, the ancestor walk sets `existing` to the in-`/sys` parent, so `QFileInfo(existing).canonicalFilePath()` lets `isSafeSysfsPath` accept the path; the later open follows the link and sends the privileged write outside `/sys`.

Reject dangling symlinks during validation and open the attribute in a way that prevents a symlink target from changing the validated path.

Comment on lines 79 to +80

QString canonicalPath = QFileInfo(existing).canonicalFilePath();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Concurrent toggles leave stale device state

When disable and enable requests for the same device overlap in that order, a disable request can write /remove and pause before inserting its database row; an enable request can then rescan, delete the old row, and read the removal list before the new row is inserted. The disable request finally records the now-enabled device as disabled, so a later startup removes it again.

Serialize enable/disable operations per device and coordinate the sysfs operation with the database update so a stale row cannot be committed after enable completes.

Prompt for AI agents
In `deepin-devicemanager-server/deepin-devicecontrol/src/securityutils.cpp` at lines 79-80:

**Concurrent toggles leave stale device state**

When disable and enable requests for the same device overlap in that order, a disable request can write `/remove` and pause before inserting its database row; an enable request can then rescan, delete the old row, and read the removal list before the new row is inserted. The disable request finally records the now-enabled device as disabled, so a later startup removes it again.

Serialize enable/disable operations per device and coordinate the sysfs operation with the database update so a stale row cannot be committed after enable completes.

Comment on lines 79 to +80

QString canonicalPath = QFileInfo(existing).canonicalFilePath();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Failed rescan loses disabled-device record

When the rescan attribute opens successfully but rejects or short-writes the rescan request, the newly accepted missing-device path reaches the enable branch, where removeEnable ignores the rescan write result and deletes the device's removal-table row anyway. If the kernel rejects that write after the file opens, the device remains removed but its persisted record is lost, so the UI no longer has the stored disabled-device entry to retry.

Check the rescan write result and only remove the persisted row after confirming the operation succeeded; retain or restore the row on failure.

Prompt for AI agents
In `deepin-devicemanager-server/deepin-devicecontrol/src/securityutils.cpp` at lines 79-80:

**Failed rescan loses disabled-device record**

When the rescan attribute opens successfully but rejects or short-writes the rescan request, the newly accepted missing-device path reaches the enable branch, where `removeEnable` ignores the rescan write result and deletes the device's removal-table row anyway. If the kernel rejects that write after the file opens, the device remains removed but its persisted record is lost, so the UI no longer has the stored disabled-device entry to retry.

Check the rescan write result and only remove the persisted row after confirming the operation succeeded; retain or restore the row on failure.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@add-uos

add-uos commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot
deepin-bot Bot merged commit 6df0f3b into linuxdeepin:master Nov 24, 2026
18 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants