Repository navigation
fix(security): allow re-enabling removed sysfs devices - #986
deepin-bot[bot] merged 1 commit into
Conversation
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 正常恢复
Reviewer's GuideImplementationUpdate 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.
TestsAdd regression tests confirming a nonexistent disabled-device path is accepted and a relative path is rejected.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
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>| const QString parent = QFileInfo(existing).dir().absolutePath(); | ||
| if (parent == QLatin1String("/") || parent == existing) { | ||
| existing = QStringLiteral("/"); | ||
| break; | ||
| } | ||
| existing = parent; |
There was a problem hiding this comment.
🟡 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.|
|
||
| QString canonicalPath = QFileInfo(existing).canonicalFilePath(); |
There was a problem hiding this comment.
🟡 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.|
|
||
| QString canonicalPath = QFileInfo(existing).canonicalFilePath(); |
There was a problem hiding this comment.
🟡 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.|
[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. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/forcemerge |
|
This pr force merged! (status: unstable) |
根因分析
isSafeSysfsPath()使用QFileInfo::canonicalFilePath()校验路径,要求完整路径必须存在。但设备被禁用时会通过
/sys/.../remove从内核移除,对应的 sysfs 目录随之消失,而「启用」操作正是针对这些不存在的路径恢复设备 —— 导致启用分支永远被
unsafe sysfs path rejected拒绝,被禁用的设备(如音频)无法从 UI 重新启用。日志证据:
修复方案
同时兼容设备目录暂不存在的启用场景
自测结果
PMS: TASK-398635
Summary by Sourcery
Allow removed sysfs devices to be safely re-enabled without weakening path traversal protection.
Bug Fixes:
Enhancements:
Tests: