Skip to content

fix(security): update authority check to use system bus name subject#613

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:develop/eaglefrom
pppanghu77:develop/eagle
Mar 4, 2026
Merged

fix(security): update authority check to use system bus name subject#613
deepin-bot[bot] merged 1 commit intolinuxdeepin:develop/eaglefrom
pppanghu77:develop/eagle

Conversation

@pppanghu77
Copy link

Replace UnixProcessSubject with SystemBusNameSubject for authorization checks in driver installation and main application startup to improve security and proper authentication handling.
Log: fix(security): update authority check to use system bus name subject

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

Replace UnixProcessSubject with SystemBusNameSubject for authorization
checks in driver installation and main application startup to improve
security and proper authentication handling.
Log: fix(security): update authority check to use system bus name subject

Task: https://pms.uniontech.com/task-view-386841.html
@deepin-ci-robot
Copy link

deepin pr auto review

这段代码的修改主要是将 Polkit(PolicyKit)权限验证的主体(Subject)从 UnixProcessSubject(getpid()) 修改为 SystemBusNameSubject(QDBusConnection::sessionBus().baseService())

以下是对这段代码修改的详细审查意见,包括语法逻辑、代码质量、代码性能和代码安全方面的分析:

1. 语法逻辑与代码质量

  • 修改分析

    • 原逻辑:使用 UnixProcessSubject(getpid())。这意味着程序向 Polkit 请求权限时,证明自己的身份是“当前进程 ID”。Polkit 会根据进程 ID 查找启动该进程的用户,并检查该用户是否具有执行操作的权限(通过 pkexec 或类似的机制)。
    • 新逻辑:使用 SystemBusNameSubject(...)。这意味着程序向 Polkit 请求权限时,证明自己的身份是“D-Bus 会话总线上的一个服务名称”。
  • 潜在问题与建议

    • 架构一致性:这种修改暗示了应用程序的权限验证机制发生了变化。通常,SystemBusNameSubject 用于 D-Bus 服务端(被调用方)验证调用者的身份,或者服务端验证自身。而在 GUI 客户端(如 deepin-devicemanager)中,通常是由客户端发起请求。
    • 上下文检查:请确认 deepin-devicemanager 是否注册了一个 D-Bus 服务在会话总线上。如果该应用只是一个普通的 GUI 应用且没有注册特定的 Bus Name,QDBusConnection::sessionBus().baseService() 可能返回一个类似 :1.xxx 的唯一名称,这在 Polkit 策略文件中很难预先定义规则(因为每次启动名称可能不同)。如果策略文件中没有匹配这个 Bus Name 的规则,验证将始终失败
    • 建议:如果 deepin-devicemanager 是通过 D-Bus 激活的服务,或者有固定的 Bus Name(如 com.deepin.devicemanager),建议使用固定的名称字符串,而不是 baseService()(动态名称)。例如:SystemBusNameSubject("com.deepin.devicemanager")

2. 代码安全

  • 安全风险分析
    • UnixProcessSubject:通过 PID 验证。存在理论上“TOCTOU”(Time-of-check to time-of-use)竞态条件的风险,但在 Polkit 的辅助守护进程处理下通常是安全的。
    • SystemBusNameSubject:通过 D-Bus Bus Name 验证。D-Bus 守护进程负责验证连接是否拥有该名称。这通常被认为是更安全的方式,因为它依赖于 D-Bus 的安全传输层和命名所有权机制。
    • 关键点:安全性完全取决于 Polkit 的 策略文件
      • 如果策略文件配置为允许 unix-process,原代码是安全的。
      • 如果策略文件配置为允许 system-bus-name,新代码是安全的。
      • 风险:如果代码改成了 SystemBusNameSubject,但 Polkit 策略文件(通常在 /usr/share/polkit-1/actions/ 下)仍然只定义了 unix-process 的 allow 规则,或者定义的 system-bus-name 规则不匹配当前应用返回的名称,那么所有权限请求都会被拒绝,导致功能不可用。

3. 代码性能

  • 影响getpid() 是极轻量级的系统调用。QDBusConnection::sessionBus().baseService() 涉及 D-Bus 连接的查询,但通常也是缓存的。两者在性能上的差异对用户体验(点击安装按钮后的等待时间)可以忽略不计。

4. 改进建议

  1. 确认 D-Bus 注册情况
    必须确认 deepin-devicemanager 在 D-Bus 上注册了什么名字。如果它没有注册一个众所周知的服务名,baseService() 返回的唯一名(如 :1.123)在 Polkit 策略中很难静态匹配。如果必须使用 Bus Name,请确保代码中注册了固定的名字:

    // 假设在 main.cpp 或初始化代码中
    if (!QDBusConnection::sessionBus().registerService("com.deepin.DeviceManager")) {
        // 处理错误
    }

    然后在验证时使用:

    SystemBusNameSubject("com.deepin.DeviceManager")
  2. 同步检查 Polkit 策略文件
    必须同步修改对应的 .policy 文件。例如 com.deepin.deepin-devicemanager.policy

    • 旧配置<allow_any>auth_admin_keep</allow_any> ... <annotate key="org.freedesktop.policykit.exec.path">...</annotate> (针对 unix-process)
    • 新配置:需要确保 allow_active 等字段针对 system-bus-name 有效。
    • 如果策略文件未更新,这个代码修改会导致功能失效。
  3. 代码健壮性
    main.cpp 中,如果 D-Bus 会话总线未连接或未初始化,baseService() 可能返回空字符串或无效值。建议增加检查:

    QString busName = QDBusConnection::sessionBus().baseService();
    if (busName.isEmpty()) {
        qWarning() << "Cannot get D-Bus session bus name, auth check failed.";
        return 0;
    }
    Authority::Result result = Authority::instance()->checkAuthorizationSync(
        "com.deepin.deepin-devicemanager.checkAuthentication",
        SystemBusNameSubject(busName),
        Authority::AllowUserInteraction
    );

总结

这个修改将权限验证方式从“基于进程 ID”转变为“基于 D-Bus 名称”。这在逻辑上是一个较大的变更。

  • 主要风险:如果对应的 Polkit 策略文件没有同步更新以允许该 D-Bus 名称,或者应用没有注册固定的 D-Bus 名称,权限验证将失败,导致无法安装驱动
  • 建议:请务必检查配套的 Polkit .policy 文件,确保其中包含针对 system-bus-name 的授权规则,并且该规则匹配代码中使用的 Bus Name。如果应用没有固定的 Bus Name,建议回滚到 UnixProcessSubject 或先注册固定的 Bus Name。

Copy link

@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 @pppanghu77, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: max-lvs, pppanghu77

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

@pppanghu77
Copy link
Author

/forcemerge

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Mar 4, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot bot merged commit 4b36a34 into linuxdeepin:develop/eagle Mar 4, 2026
17 of 19 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