Skip to content

fix: use --print-reply for udev dbus-send to fix USB auto-refresh - #235

Open
wangrong1069 wants to merge 1 commit into
linuxdeepin:masterfrom
wangrong1069:agent/agent/fa5067fbe5e6
Open

wangrong1069 wants to merge 1 commit into
linuxdeepin:masterfrom
wangrong1069:agent/agent/fa5067fbe5e6

Conversation

@wangrong1069

@wangrong1069 wangrong1069 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Root Cause Analysis

USB hot-plug auto-refresh stopped working after a security hardening commit (f6ada21) that changed DiskManagerService::checkAuthorization() to properly validate the GetConnectionUnixUser reply. The udev scripts (USBadd.sh, USBremove.sh) used fire-and-forget dbus-send --type=method_call, so the caller process exited before checkAuthorization() could resolve its bus name — GetConnectionUnixUser returned NameHasNoOwner, authorization was denied, and the UI refresh signal was never emitted. A minimal DBus demo confirmed this: fire-and-forget mode fails authorization, while --print-reply (blocking) mode succeeds.

Fix

Added the --print-reply flag to dbus-send in both service/udev/USBadd.sh and service/udev/USBremove.sh, switching from fire-and-forget to blocking mode. The caller process now stays alive until DiskManagerService finishes processing. Since udev scripts run as root (UID=0), checkAuthorization() resolves the caller identity and passes the root check. No C++ authorization model changes.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • Target dbus-send lines are not the product of a previous bug fix — historical commits modified trigger conditions (ProcNumber check) and process detection, not the dbus-send call mode. This change does not revert any historical fix.
  • No code references these scripts (references = 0); no callers are affected.
  • udev timeout risk: PartedCore::updateUsb() contains sleep(5), so USBadd.sh blocks ~5s under --print-reply. The default udev event timeout is 30s — 5s is well within bounds. PartedCore::updateUsbRemove() has no sleep and returns immediately. Different USB devices' udev events are handled by independent worker processes, so one device blocking 5s does not affect others. No udev timeout risk.

Business Impact Scope

USB hot-plug auto-refresh in the disk manager. Users inserting or removing external drives will see the interface auto-refresh again.

Verification Suggestion

Test inserting/removing external USB drives and verify the disk manager UI auto-refreshes. Test rapid sequential plug/unplug of multiple devices for udev timeout or event loss.


根因分析

安全加固提交(f6ada21)将 DiskManagerService::checkAuthorization() 改为正确校验 GetConnectionUnixUser 回复后,USB 热插拔自动刷新失效。udev 脚本(USBadd.sh、USBremove.sh)使用 fire-and-forget 模式的 dbus-send --type=method_call,调用者进程在 checkAuthorization() 解析 bus name 前即退出——GetConnectionUnixUser 返回 NameHasNoOwner,授权被拒,界面刷新信号未发出。最小 DBus demo 已验证:fire-and-forget 模式授权失败,--print-reply(阻塞)模式授权成功。

修复方案

在 service/udev/USBadd.sh 和 service/udev/USBremove.sh 的 dbus-send 中新增 --print-reply 标志,从 fire-and-forget 改为阻塞模式。调用者进程存活至 DiskManagerService 处理完毕,udev 以 root 运行(UID=0),checkAuthorization() 解析到 root 身份并通过检查。不改变 C++ 授权安全模型。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • 目标 dbus-send 行非历史 bug 修复产物——历史提交修改的是触发条件(ProcNumber 检查)和进程检测方法,未改 dbus-send 调用模式。本次改动不撤销任何历史修复。
  • 无代码引用这两个脚本(references = 0),无调用者受影响。
  • udev 超时风险:PartedCore::updateUsb() 含 sleep(5),--print-reply 模式下 USBadd.sh 阻塞约 5 秒。udev 默认事件超时 30 秒,5 秒远低于此限。PartedCore::updateUsbRemove() 无 sleep,立即返回。不同 USB 设备的 udev 事件由独立 worker 进程并行处理,单个设备阻塞 5 秒不影响其他设备。无 udev 超时风险。

业务影响范围

磁盘管理器 USB 热插拔自动刷新。用户插入或拔出外接硬盘时界面将恢复自动刷新。

验证建议

测试插入/拔出外接 USB 硬盘,验证磁盘管理器界面自动刷新。测试快速连续插拔多个设备,验证无 udev 超时或事件丢失。

Summary by Sourcery

Bug Fixes:

  • Restore USB hot-plug auto-refresh by keeping udev-triggered D-Bus calls active until the disk manager processes them.

1. Root cause: USBadd.sh and USBremove.sh used fire-and-forget
   dbus-send (--type=method_call), so the caller process exited
   before DiskManagerService::checkAuthorization() could resolve
   its bus name via GetConnectionUnixUser, returning NameHasNoOwner
   and denying authorization - the UI refresh signal was never sent
2. Fix: add --print-reply flag to both scripts so the caller stays
   alive until the service finishes processing; udev runs as root
   (UID=0) so checkAuthorization passes the root identity check
3. Impact: USB hot-plug auto-refresh works again; udev scripts block
   ~5s (updateUsb sleep) well within the 30s default udev timeout;
   no C++ authorization model change

Log: Fix USB hot-plug auto-refresh not working in disk manager

Influence:
1. Test USB device insertion triggers auto-refresh in disk manager
2. Test USB device removal triggers auto-refresh in disk manager
3. Test rapid sequential plug/unplug of multiple USB devices for
   udev timeout or event loss

fix: 修复udev脚本dbus-send为--print-reply以修复USB自动刷新

1. 根因:USBadd.sh 和 USBremove.sh 使用 fire-and-forget 模式的
   dbus-send(--type=method_call),调用者进程在
   DiskManagerService::checkAuthorization() 通过
   GetConnectionUnixUser 解析 bus name 前即退出,返回
   NameHasNoOwner 导致授权失败,界面刷新信号未发出
2. 方案:两个脚本各新增 --print-reply 标志,使调用者进程存活至
   服务端处理完毕;udev 以 root 运行(UID=0),通过 root 身份
   授权检查
3. 影响:USB 热插拔自动刷新恢复;udev 脚本阻塞约 5 秒
   (updateUsb sleep)远低于 30 秒默认 udev 超时;不改变 C++
   授权安全模型

Log: 修复磁盘管理器USB热插拔不自动刷新的问题

Influence:
1. 测试插入USB设备后磁盘管理器界面自动刷新
2. 测试拔出USB设备后磁盘管理器界面自动刷新
3. 测试快速连续插拔多个USB设备,验证无udev超时或事件丢失

PMS: BUG-362437
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 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

@sourcery-ai

sourcery-ai Bot commented Oct 28, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

The udev USB add/remove scripts now use blocking dbus-send --print-reply calls instead of fire-and-forget calls, keeping the root caller alive until DiskManagerService completes authorization and emits the refresh signal; the main review consideration is validating USB refresh behavior and the approximately 5-second add-event blocking time against udev timing.

Sequence diagram for blocking USB add refresh authorization

sequenceDiagram
    participant Udev as USBadd.sh
    participant DBus as D-Bus
    participant Service as DiskManagerService
    participant Auth as GetConnectionUnixUser
    participant UI as DiskManagerUI

    Udev->>DBus: dbus-send --print-reply updateUsb
    DBus->>Service: updateUsb()
    Service->>Auth: GetConnectionUnixUser
    Auth-->>Service: UID=0
    Service->>Service: checkAuthorization()
    Service-->>DBus: method reply
    DBus-->>Udev: print reply
    Service-->>UI: refresh signal
Loading

Sequence diagram for blocking USB remove refresh authorization

sequenceDiagram
    participant Udev as USBremove.sh
    participant DBus as D-Bus
    participant Service as DiskManagerService
    participant Auth as GetConnectionUnixUser
    participant UI as DiskManagerUI

    Udev->>DBus: dbus-send --print-reply updateUsbRemove
    DBus->>Service: updateUsbRemove()
    Service->>Auth: GetConnectionUnixUser
    Auth-->>Service: UID=0
    Service->>Service: checkAuthorization()
    Service-->>DBus: method reply
    DBus-->>Udev: print reply
    Service-->>UI: refresh signal
Loading

File-Level Changes

Change Details Files
Make udev-to-D-Bus USB refresh calls synchronous so authorization can resolve the caller before the script exits.
  • Add --print-reply to the USB insertion refresh request.
  • Add --print-reply to the USB removal refresh request.
  • Preserve the existing process-detection guards, destinations, methods, and authorization implementation.
service/udev/USBadd.sh
service/udev/USBremove.sh

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 100 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次 PR 在 USBadd.sh 和 USBremove.sh 两个 udev 脚本中为 dbus-send 命令添加 --print-reply 参数,修复 USB 自动刷新问题。变更最小化且聚焦,语法正确,逻辑清晰,无安全漏洞,无性能问题,OCR 审查亦未发现问题。

🔍 详细分析

1. 语法逻辑 ✅

评价: 语法正确,逻辑清晰 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: []


💡 改进建议代码示例


本报告由 AI 代码审查工具自动生成

@deepin-bot

deepin-bot Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.0.28
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #251

@deepin-bot

deepin-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.0.29
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #259

@deepin-bot

deepin-bot Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.0.30
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #261

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.

2 participants