Skip to content

fix: fix data loss and path bugs in drivercontrol module - #934

Open
add-uos wants to merge 1 commit into
linuxdeepin:masterfrom
add-uos:agent/bug/14694a4624f5
Open

add-uos wants to merge 1 commit into
linuxdeepin:masterfrom
add-uos:agent/bug/14694a4624f5

Conversation

@add-uos

@add-uos add-uos commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

fix: fix data loss and path bugs in drivercontrol module

  1. Root cause: deleteLineOfFileWithItem never called QFile::open()
    before seek/read/write, but resize(0) still truncated the real
    file, wiping entire config files (data loss)
  2. Fix: add open(QIODevice::ReadWrite | QIODevice::Text) guard before
    file operations; add seek(0) after resize(0) to reset cursor to
    start (resize does not move the file cursor); add stream.flush()
    before close() to ensure data is written to disk
  3. Root cause: setModLoadedOnBoot used QFileInfo(dir).dir() which
    returns the parent directory, producing out-of-bounds paths that
    always fail the path guard, making boot-on-load feature unusable
  4. Fix: use QDir(LOADONBOOT_PROBE_DIR).absoluteFilePath() for correct
    path construction
  5. Root cause: rmFromBlackList libdir loop copy-pasted etcdir, so
    /usr/lib/modprobe.d files were never processed
  6. Fix: change etcdir to libdir in the libdir traversal loop
  7. Root cause: isPathWithinDirectory used canonicalFilePath() which
    returns empty for non-existent files, rejecting legitimate new
    config file paths created by addModBlackList/setModLoadedOnBoot
  8. Fix: fall back to QDir::cleanPath(absoluteFilePath) when canonical
    path is empty, preserving path traversal protection

Log: fix driver control config file data loss and boot-on-load failure

Influence:

  1. Test blacklist removal preserves non-matching lines in config files
  2. Test setModLoadedOnBoot creates config in /etc/modules-load.d correctly
  3. Test rmFromBlackList processes /usr/lib/modprobe.d directory files
  4. Test addModBlackList accepts legitimate new file paths
  5. Verify no path traversal regression in isPathWithinDirectory

fix: 修复驱动管理模块配置文件数据丢失及路径错误

  1. 根因:deleteLineOfFileWithItem 未调用 QFile::open() 即执行
    seek/read/write,但 resize(0) 仍截断实际文件,导致整个配置
    文件被清空(数据丢失)
  2. 方案:在文件操作前增加 open(QIODevice::ReadWrite | QIODevice::Text)
    保护;在 resize(0) 后增加 seek(0) 将游标重置到起始位置(resize
    不移动文件游标);在 close() 前增加 stream.flush() 确保数据落盘
  3. 根因:setModLoadedOnBoot 使用 QFileInfo(dir).dir() 返回父目录,
    拼出越界路径导致边界检查恒失败,开机自启动功能不可用
  4. 方案:改用 QDir(LOADONBOOT_PROBE_DIR).absoluteFilePath() 正确
    构建路径
  5. 根因:rmFromBlackList 的 libdir 循环复制粘贴误用 etcdir,
    /usr/lib/modprobe.d 下文件从未被处理
  6. 方案:将 libdir 循环中的 etcdir 改为 libdir
  7. 根因:isPathWithinDirectory 使用 canonicalFilePath() 对不存在
    文件返回空串,拒绝 addModBlackList/setModLoadedOnBoot 创建的
    合法新文件路径
  8. 方案:canonical 路径为空时回退 QDir::cleanPath(absoluteFilePath),
    保留路径穿越防护

Log: 修复驱动管理配置文件数据丢失及开机自启动失效

Influence:

  1. 测试黑名单移除后配置文件正确保留非匹配行
  2. 测试 setModLoadedOnBoot 正确在 /etc/modules-load.d 创建配置
  3. 测试 rmFromBlackList 正确处理 /usr/lib/modprobe.d 目录文件
  4. 测试 addModBlackList 接受合法新文件路径
  5. 验证 isPathWithinDirectory 无路径穿越回归

Summary by Sourcery

Fix driver control configuration updates and path validation to prevent data loss and restore blacklist and boot-on-load functionality.

Bug Fixes:

  • Prevent driver configuration files from being accidentally emptied while removing blacklist entries.
  • Correct boot-on-load configuration path creation and process files in /usr/lib/modprobe.d during blacklist removal.
  • Allow valid paths for new configuration files while preserving path traversal protection.

@sourcery-ai

sourcery-ai Bot commented Oct 26, 2026

Copy link
Copy Markdown

Sorry @add-uos, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 1 day and 13 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos

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 26, 2026

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

Reviewer's Guide

Fixes driver-control configuration loss and unusable boot-on-load behavior by safely opening and rewriting files, correcting /etc and /usr configuration paths, handling the intended lib directory, and validating non-existent target paths without weakening traversal protection.

Sequence diagram for safe driver configuration file rewriting

sequenceDiagram
    participant ModCore
    participant QFile
    participant QTextStream

    ModCore->>QFile: open(QIODevice::ReadWrite | QIODevice::Text)
    alt file opened
        ModCore->>QFile: seek(0)
        ModCore->>QTextStream: read configuration content
        ModCore->>QFile: resize(0)
        ModCore->>QFile: seek(0)
        ModCore->>QTextStream: write updated content
        ModCore->>QTextStream: flush()
        ModCore->>QFile: close()
    else open failed
        ModCore-->>ModCore: return
    end
Loading

Flow diagram for secure driver configuration path validation

flowchart TD
    A[Build target path] --> B[canonicalFilePath]
    B --> C{Canonical path exists?}
    C -->|Yes| D[Validate path within base directory]
    C -->|No| E[cleanPath absoluteFilePath]
    E --> D
    D --> F{Within base directory?}
    F -->|Yes| G[Allow configuration operation]
    F -->|No| H[Reject path]
Loading

File-Level Changes

Change Details Files
Prevent configuration-file truncation and preserve edited content during line removal.
  • Guard file operations with a successful read/write text open.
  • Reset the cursor after truncation and flush the rewritten stream before closing.
deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/modcore.cpp
Correct driver-control configuration path handling and directory traversal.
  • Construct the modules-load path from the configured probe directory rather than its parent.
  • Process files discovered under /usr/lib/modprobe.d using the lib directory.
  • Allow valid paths for new files while retaining canonical-path traversal protection via an absolute-path fallback.
deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/modcore.cpp
deepin-devicemanager-server/deepin-devicecontrol/src/securityutils.cpp

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

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

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

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交修复 drivercontrol 模块的数据丢失与路径缺陷,涉及三处路径修正与一处文件写入加固。 语法逻辑维度修复方向正确,但 resize(0) 后应使用 stream.seek(0) 而非 tmpfile.seek(0) 以避免 QTextStream 内部缓冲过期,存在轻微逻辑隐患;代码质量维度存在文件打开失败静默返回无日志、 修复后残留未使用变量 fileinfo 两处问题;代码性能与代码安全维度均无问题,路径修复实际增强了 路径边界检查的健壮性。综合评分 93 分,审查通过。

🔍 详细分析

1. 语法逻辑 ✅

评价: 边界处理完善 ✅ 通过

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

建议: ['将 tmpfile.seek(0) 改为 stream.seek(0),确保 QTextStream 内部缓冲与底层设备位置同步']


2. 代码质量 ✅

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

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

建议: ['文件打开失败时增加 qCWarning 日志输出,如 qCWarning(appLog) << "Failed to open file:" << filepath', '删除 setModLoadedOnBoot 中不再使用的 QFileInfo fileinfo(LOADONBOOT_PROBE_DIR) 语句']


3. 代码性能 ✅

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

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

建议: []


4. 代码安全 🔒

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

🔐 发现 0 个安全漏洞

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

建议: []


💡 改进建议代码示例

// modcore.cpp - deleteLineOfFileWithItem 修复建议
void ModCore::deleteLineOfFileWithItem(const QString &filepath, const QString &item)
{
    QFile tmpfile(filepath);

    if (!tmpfile.open(QIODevice::ReadWrite | QIODevice::Text)) {
        qCWarning(appLog) << "Failed to open file for editing:" << filepath;
        return;
    }

    //设置文件游标到起始位置
    tmpfile.seek(0);
    QTextStream stream(&tmpfile);
    // ... 读取与过滤逻辑 ...

    //清除原有内容
    tmpfile.resize(0);
    stream.seek(0);          // 使用 stream.seek(0) 同步 QTextStream 内部状态
    stream << newcontent;
    stream.flush();
    tmpfile.close();
}

// modcore.cpp - setModLoadedOnBoot 修复建议:删除未使用变量
bool ModCore::setModLoadedOnBoot(const QString &modName)
{
    // 已删除: QFileInfo fileinfo(LOADONBOOT_PROBE_DIR);  // 不再使用
    QString strfilename = QString(LOADONBOOT_FILENAME_TEMPLETE).arg(modName);
    QString filepath = QDir(LOADONBOOT_PROBE_DIR).absoluteFilePath(strfilename);

    // 路径边界检查:确保最终路径在预期目录内
    if (!isPathWithinDirectory(filepath, LOADONBOOT_PROBE_DIR)) {
        return false;
    }
    // ...
}

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

1. Root cause: deleteLineOfFileWithItem never called QFile::open()
   before seek/read/write, but resize(0) still truncated the real
   file, wiping entire config files (data loss)
2. Fix: add open(QIODevice::ReadWrite | QIODevice::Text) guard before
   file operations; add seek(0) after resize(0) to reset cursor to
   start (resize does not move the file cursor); add stream.flush()
   before close() to ensure data is written to disk
3. Root cause: setModLoadedOnBoot used QFileInfo(dir).dir() which
   returns the parent directory, producing out-of-bounds paths that
   always fail the path guard, making boot-on-load feature unusable
4. Fix: use QDir(LOADONBOOT_PROBE_DIR).absoluteFilePath() for correct
   path construction
5. Root cause: rmFromBlackList libdir loop copy-pasted etcdir, so
   /usr/lib/modprobe.d files were never processed
6. Fix: change etcdir to libdir in the libdir traversal loop
7. Root cause: isPathWithinDirectory used canonicalFilePath() which
   returns empty for non-existent files, rejecting legitimate new
   config file paths created by addModBlackList/setModLoadedOnBoot
8. Fix: fall back to QDir::cleanPath(absoluteFilePath) when canonical
   path is empty, preserving path traversal protection

Log: fix driver control config file data loss and boot-on-load failure

Influence:
1. Test blacklist removal preserves non-matching lines in config files
2. Test setModLoadedOnBoot creates config in /etc/modules-load.d correctly
3. Test rmFromBlackList processes /usr/lib/modprobe.d directory files
4. Test addModBlackList accepts legitimate new file paths
5. Verify no path traversal regression in isPathWithinDirectory

fix: 修复驱动管理模块配置文件数据丢失及路径错误

1. 根因:deleteLineOfFileWithItem 未调用 QFile::open() 即执行
   seek/read/write,但 resize(0) 仍截断实际文件,导致整个配置
   文件被清空(数据丢失)
2. 方案:在文件操作前增加 open(QIODevice::ReadWrite | QIODevice::Text)
   保护;在 resize(0) 后增加 seek(0) 将游标重置到起始位置(resize
   不移动文件游标);在 close() 前增加 stream.flush() 确保数据落盘
3. 根因:setModLoadedOnBoot 使用 QFileInfo(dir).dir() 返回父目录,
   拼出越界路径导致边界检查恒失败,开机自启动功能不可用
4. 方案:改用 QDir(LOADONBOOT_PROBE_DIR).absoluteFilePath() 正确
   构建路径
5. 根因:rmFromBlackList 的 libdir 循环复制粘贴误用 etcdir,
   /usr/lib/modprobe.d 下文件从未被处理
6. 方案:将 libdir 循环中的 etcdir 改为 libdir
7. 根因:isPathWithinDirectory 使用 canonicalFilePath() 对不存在
   文件返回空串,拒绝 addModBlackList/setModLoadedOnBoot 创建的
   合法新文件路径
8. 方案:canonical 路径为空时回退 QDir::cleanPath(absoluteFilePath),
   保留路径穿越防护

Log: 修复驱动管理配置文件数据丢失及开机自启动失效

Influence:
1. 测试黑名单移除后配置文件正确保留非匹配行
2. 测试 setModLoadedOnBoot 正确在 /etc/modules-load.d 创建配置
3. 测试 rmFromBlackList 正确处理 /usr/lib/modprobe.d 目录文件
4. 测试 addModBlackList 接受合法新文件路径
5. 验证 isPathWithinDirectory 无路径穿越回归
@add-uos
add-uos force-pushed the agent/bug/14694a4624f5 branch from a8e28eb to f1a47b8 Compare October 27, 2026 03:43
@deepin-bot

deepin-bot Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.0.75
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #956

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