Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines +72 to +77

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.

}

QString canonicalPath = QFileInfo(existing).canonicalFilePath();
Comment on lines 79 to +80

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

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.

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#undef private
#undef protected

#include <QDir>

Check warning on line 15 in test/deepin-devicemanager-server/src/SecurityUtils/ut_securityutils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDir> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFile>

Check warning on line 16 in test/deepin-devicemanager-server/src/SecurityUtils/ut_securityutils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFileInfo>

Check warning on line 17 in test/deepin-devicemanager-server/src/SecurityUtils/ut_securityutils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFileInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QTemporaryDir>

Check warning on line 18 in test/deepin-devicemanager-server/src/SecurityUtils/ut_securityutils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QTemporaryDir> not found. Please note: Cppcheck does not need standard library headers to get proper results.

class SecurityUtilsTest : public ::testing::Test
{
Expand Down Expand Up @@ -226,6 +227,39 @@
EXPECT_EQ(path.isEmpty(), true);
}

/*
* 禁用设备会被内核从 sysfs 移除,目录不存在;
* 启用操作正是针对这类不存在的路径恢复设备,必须校验通过。
* 回归:BUG - canonicalFilePath() 对不存在的路径返回空,
* 导致 removeEnable 启用分支永远被 "unsafe sysfs path rejected" 拒绝。
*/
TEST_F(SecurityUtilsTest, isSafeSysfsPath_disabledDevicePathNotExists_returnsTrue)
{
// Arrange:一个格式合法但当前不存在的 PCI 设备路径
QString path = "/devices/pci0000:00/0000:00:1f.99";

// Precondition:确保该路径确实不存在,测试才有意义
ASSERT_FALSE(QFileInfo::exists(QStringLiteral("/sys") + path));

// Act
bool result = isSafeSysfsPath(path);

// Assert
EXPECT_TRUE(result);
}

TEST_F(SecurityUtilsTest, isSafeSysfsPath_relativePath_returnsFalse)
{
// Arrange:不以 / 开头的相对路径,拼接后会逃逸出 /sys 语义
QString path = "devices/pci0000:00/0000:00:1f.3";

// Act
bool result = isSafeSysfsPath(path);

// Assert
EXPECT_FALSE(result);
}

// validateModNameForInstall

TEST_F(SecurityUtilsTest, validateModNameForInstall_validName_returnsTrueNoError)
Expand Down
Loading