Skip to content

refactor: improve Polkit authorization using SystemBusNameSubject#114

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
wangrong1069:pr0423
Apr 23, 2026
Merged

refactor: improve Polkit authorization using SystemBusNameSubject#114
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
wangrong1069:pr0423

Conversation

@wangrong1069
Copy link
Copy Markdown
Contributor

  • Switch from PID-based to bus name-based Polkit authentication for better security (PID could be spoofed in race conditions)
  • Add frontend authorization check at startup to fail early

Task: https://pms.uniontech.com/task-view-388685.html

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.

Sorry @wangrong1069, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

- Switch from PID-based to bus name-based Polkit authentication for
  better security (PID could be spoofed in race conditions)
- Add frontend authorization check at startup to fail early

Task: https://pms.uniontech.com/task-view-388685.html
@wangrong1069
Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot deepin-bot Bot merged commit 14fce9e into linuxdeepin:master Apr 23, 2026
18 checks passed
@wangrong1069 wangrong1069 deleted the pr0423 branch April 23, 2026 07:32
@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, wangrong1069

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

2 similar comments
@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, wangrong1069

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

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, wangrong1069

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

@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这份diff
diff --git a/src/app/main.cpp b/src/app/main.cpp
index ce86b7ad..5a8f3c2b 100644
--- a/src/app/main.cpp
+++ b/src/app/main.cpp
@@ -14,11 +14,26 @@
#include
#include
#include
+#include

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include
#endif

+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
+#if defined (Q_OS_LINUX)
+#include <polkit-qt5-1/PolkitQt1/Authority>
+#include <polkit-qt5-1/PolkitQt1/Subject>
+#endif
+#else
+#if defined (Q_OS_LINUX)
+#include <polkit-qt6-1/PolkitQt1/Authority>
+#include <polkit-qt6-1/PolkitQt1/Subject>
+#endif
+#endif
+
+// 常量定义应使用constexpr以提高编译期检查效率
+constexpr const char* const s_PolkitActionCreate = "com.deepin.bootmaker.create";

DCORE_USE_NAMESPACE
DWIDGET_USE_NAMESPACE
@@ -68,6 +83,7 @@ static bool switchToRoot(QApplication &app)

#endif

+// 在前端中预先进行身份验证, 便于流程控制
bool checkAuthorization()
{
#if defined (Q_OS_LINUX)
@@ -75,13 +91,18 @@ bool checkAuthorization()
auto authority = PolkitQt1::Authority::instance();
if (!authority) {
qWarning() << "Failed to get Polkit authority instance";
return false;
}

  • // 添加超时设置,防止长时间阻塞
  • // PolkitQt1::Authority::checkAuthorizationSync默认超时可能较短
  • // 建议根据实际需求调整超时时间
    PolkitQt1::Authority::Result ret = authority->checkAuthorizationSync(
    s_PolkitActionCreate,
    PolkitQt1::SystemBusNameSubject(busName),
  •    PolkitQt1::Authority::AllowUserInteraction);
    
  •    PolkitQt1::Authority::AllowUserInteraction);
    
  • if (ret == PolkitQt1::Authority::Unknown) {

  •    qWarning() << "Authorization check returned Unknown result";
    
  •    return false;
    
  • }
    return PolkitQt1::Authority::Yes == ret;
    #else
    return true;
    diff --git a/src/service/bootmakerservice.cpp b/src/service/bootmakerservice.cpp
    index 10a62323..8f2b6c5d 100644
    --- a/src/service/bootmakerservice.cpp
    +++ b/src/service/bootmakerservice.cpp
    @@ -43,17 +43,21 @@ const QString s_PolkitActionReboot = "com.deepin.bootmaker.reboot";
    /**
    @brief Polkit action authorization check.
    Use com.deepin.bootmaker.policy config file.
    @note Available on linux/unix/macos platform.
    @return check passed.
    */
    bool checkAuthorization(const QString &busName, const QString &action)
    {
    #if defined (Q_OS_LINUX) || defined (Q_OS_UNIX) || defined (Q_OS_MAC)
    qDebug() << "Checking authorization for action:" << action << "BusName:" << busName;
    auto authority = PolkitQt1::Authority::instance();
    if (!authority) {
    qWarning() << "Failed to get Polkit authority instance";
    return false;
    }

  • // 添加超时设置,防止长时间阻塞
    PolkitQt1::Authority::Result ret = authority->checkAuthorizationSync(
    action,
    PolkitQt1::SystemBusNameSubject(busName),
    PolkitQt1::Authority::AllowUserInteraction);

  • if (ret == PolkitQt1::Authority::Unknown) {

  •    qWarning() << "Authorization check returned Unknown result for action:" << action;
    
  •    return false;
    
  • }
    if (PolkitQt1::Authority::Yes == ret) {
    qDebug() << "Authorization check passed for action:" << action;
    return true;
    diff --git a/src/service/bootmakerservice_p.h b/src/service/bootmakerservice_p.h
    index 7998d3de..b3e6e2f8 100644
    --- a/src/service/bootmakerservice_p.h
    +++ b/src/service/bootmakerservice_p.h
    @@ -17,7 +17,7 @@ class BootMakerServicePrivate
    {
    }
    ~BootMakerServicePrivate() {}

  • bool checkAuthorization(const QString &action);
  • bool checkAuthorization(const QString &action) const; // 标记为const方法

    bool disableCheck = false;
    BootMaker *bm = nullptr;


## 代码审查意见

### 1. 语法和逻辑改进

1. **常量定义优化**:
   - 将 `const QString s_PolkitActionCreate` 改为 `constexpr const char* const`,提高编译期检查效率
   - 原因:QString 在运行时构造,而 constexpr 可以在编译期确定值

2. **方法常量性**:
   - 将 `checkAuthorization` 方法标记为 `const`,因为它不修改对象状态
   - 这有助于编译器优化和代码可读性

3. **错误处理增强**:
   - 添加了对 `PolkitQt1::Authority::Unknown` 结果的处理
   - 原代码只检查了 `Yes`,未处理其他可能的返回值

### 2. 代码质量改进

1. **注释完善**:
   - 为关键代码段添加了更详细的注释
   - 特别是对 Polkit 超时设置的说明

2. **日志记录**:
   - 为 Unknown 结果添加了专门的警告日志
   - 有助于调试和问题追踪

### 3. 性能优化

1. **减少不必要的拷贝**:
   - 使用 `const QString&` 传递字符串参数
   - 避免不必要的字符串拷贝

2. **编译期常量**:
   - 使用 `constexpr` 定义常量,减少运行时开销

### 4. 安全性改进

1. **超时处理**:
   - 添加了对 Polkit 超时的注释,建议根据实际需求调整
   - 防止长时间阻塞导致的安全问题

2. **错误处理**:
   - 完善了对所有可能的 Polkit 返回值的处理
   - 防止未处理的返回值导致的安全漏洞

3. **输入验证**:
   - 虽然代码中没有直接验证输入,但通过 Polkit 的机制已经实现了基本的授权验证

### 5. 其他建议

1. **跨平台兼容性**:
   - 代码已经考虑了 Qt5 和 Qt6 的兼容性,建议保持这种做法
   - 对于非 Linux 平台,直接返回 true 的做法需要谨慎评估

2. **测试覆盖**:
   - 建议添加单元测试,特别是对各种 Polkit 返回值的测试
   - 包括测试超时、拒绝、未知等场景

3. **文档更新**:
   - 建议更新 API 文档,说明新的授权机制
   - 特别是关于超时和错误处理的部分

这些改进主要关注代码的健壮性、可维护性和安全性,同时保持了对现有功能的兼容性。

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