Repository navigation
fix(security): allow re-enabling removed sysfs devices #986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,20 +50,39 @@ bool isSafeSysfsPath(const QString &relativePath) | |
| if (relativePath.isEmpty()) | ||
| return false; | ||
|
|
||
| // 必须是以 / 开头的绝对路径(相对 /sys),防止拼出 /sysdevices 之类的路径 | ||
| if (!relativePath.startsWith(QLatin1Char('/'))) | ||
| return false; | ||
|
|
||
| // 第一层:拒绝包含 ".." 的路径,防止路径穿越 | ||
| if (relativePath.contains(QLatin1String(".."))) | ||
| return false; | ||
|
|
||
| // 第二层:构建完整路径并验证其 canonical path 以 /sys 开头 | ||
| QString fullPath = QStringLiteral("/sys") + relativePath; | ||
| QFileInfo fileInfo(fullPath); | ||
| QString canonicalPath = fileInfo.canonicalFilePath(); | ||
| /* | ||
| * 第二层:对路径中「已存在的最深祖先目录」做 canonical path 校验, | ||
| * 确保真实解析位置在 /sys 之下,防止符号链接穿越到 /sys 外部。 | ||
| * | ||
| * 注意:不能要求完整路径本身存在——设备被禁用时会通过 | ||
| * /sys/.../remove 从内核移除,对应的 sysfs 目录随之消失, | ||
| * 而「启用」操作正是针对这些不存在的路径恢复设备。 | ||
| * 若此处要求 canonicalFilePath() 非空,启用操作将永远被拒绝。 | ||
| */ | ||
| 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; | ||
| } | ||
|
|
||
| QString canonicalPath = QFileInfo(existing).canonicalFilePath(); | ||
|
Comment on lines
79
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 agentsIn `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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 agentsIn `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. |
||
| if (canonicalPath.isEmpty()) | ||
| return false; | ||
|
|
||
| // 确保 canonical path 以 /sys/ 开头,防止符号链接穿越到 /sys 外部 | ||
| if (!canonicalPath.startsWith(QLatin1String("/sys/"))) | ||
| // 确保 canonical path 在 /sys 之内(/sys 本身也是合法祖先) | ||
| if (canonicalPath != QLatin1String("/sys") && !canonicalPath.startsWith(QLatin1String("/sys/"))) | ||
| return false; | ||
|
|
||
| return true; | ||
|
|
||
There was a problem hiding this comment.
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
/syspointing outside it, and its target becomes available before the attribute is opened, the ancestor walk setsexistingto the in-/sysparent, soQFileInfo(existing).canonicalFilePath()letsisSafeSysfsPathaccept 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