Skip to content

fix: disable immutable remount for read-only dpkg/apt queries - #469

Merged
qiuzhiqian merged 1 commit into
masterfrom
fix-apt-remount
Aug 11, 2026
Merged

qiuzhiqian merged 1 commit into
masterfrom
fix-apt-remount

Conversation

@qiuzhiqian

@qiuzhiqian qiuzhiqian commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

For read-only or verification commands such as 'dpkg --print-architecture', 'dpkg --print-foreign-architectures' and 'apt list --upgradable', no data is written to the system, so remounting the immutable /usr to rw is unnecessary. Set IMMUTABLE_DISABLE_REMOUNT=true on these subprocesses to skip the redundant remount flow.

Note: most other dpkg/apt query call sites inherit this variable via the process-level os.Setenv in src/internal/system/system_apt.go init(). config/cache is the exception because it does not import the system package, so an explicit Env assignment there is required.

Summary by Sourcery

Disable immutable /usr remounts for read-only dpkg and apt query commands by marking these subprocesses as remount-safe.

Enhancements:

  • Ensure dpkg architecture and foreign-architecture queries run with IMMUTABLE_DISABLE_REMOUNT set to avoid unnecessary remounts.
  • Propagate IMMUTABLE_DISABLE_REMOUNT to apt source download size queries and lastore tools that invoke dpkg for architecture detection.

@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.

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

Please try again later or upgrade to continue using Sourcery

@github-actions

Copy link
Copy Markdown

CLA Assistant Lite bot:
提交邮箱中包含我们的合作伙伴,但您似乎并非合作伙伴的成员或对接人,请联系相关对接人将您添加至组织之中,或由其重新发起 Pull Request。
The commit email domain belongs to one of our partners, but it seems you are not yet a member of the current organization, please contact the contact person to add you to the organization or let them submit the Pull Request.

xml seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You can retrigger this bot by commenting recheck in this Pull Request

@sourcery-ai

sourcery-ai Bot commented Aug 11, 2026

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

Reviewer's Guide

This PR ensures read-only dpkg/apt queries do not trigger the immutable /usr remount logic by explicitly setting IMMUTABLE_DISABLE_REMOUNT=true on those subprocesses that don’t inherit the global env initialization.

Sequence diagram for dpkg architecture query with IMMUTABLE_DISABLE_REMOUNT

sequenceDiagram
    participant GetArchInfo
    participant dpkg

    GetArchInfo->>dpkg: exec.Command("dpkg","--print-architecture")
    GetArchInfo->>dpkg: cmd.Env append IMMUTABLE_DISABLE_REMOUNT=true
    dpkg-->>GetArchInfo: Output()
Loading

Flow diagram for IMMUTABLE_DISABLE_REMOUNT propagation to dpkg/apt queries

flowchart TD
    A[process_start] --> B[system_apt_init]
    B --> C[os.Setenv IMMUTABLE_DISABLE_REMOUNT=true]
    C --> D[dpkg_apt_queries_in_system_package]

    A --> E[config_cache_init]
    E --> F[exec.Command dpkg --print-architecture]
    F --> G[cmd.Env append IMMUTABLE_DISABLE_REMOUNT=true]

    A --> H[check_init]
    H --> I[exec.Command dpkg --print-architecture]
    I --> J[cmd.Env append IMMUTABLE_DISABLE_REMOUNT=true]
Loading

File-Level Changes

Change Details Files
Disable immutable remount for apt source download size query subprocess.
  • Extend apt-get command environment to include IMMUTABLE_DISABLE_REMOUNT=true alongside LC_ALL=C for source download size computation
  • Ensure read-only apt query for download size does not perform unnecessary remount of immutable /usr
src/internal/system/system_apt.go
Ensure dpkg architecture/foreign-architecture queries run with IMMUTABLE_DISABLE_REMOUNT=true.
  • Wrap dpkg --print-foreign-architectures in a Command with explicit Env including IMMUTABLE_DISABLE_REMOUNT=true
  • Wrap dpkg --print-architecture calls in Commands with explicit Env including IMMUTABLE_DISABLE_REMOUNT=true in system and tooling packages
  • Avoid write-oriented remount path for architecture discovery helpers used across the system
src/internal/system/system_apt.go
src/internal/updateplatform/systeminfo_utils.go
src/lastore-tools/upgradable.go
src/lastore-update-tools/config/cache/package.go
src/lastore-update-tools/controller/check/check.go

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

@qiuzhiqian
qiuzhiqian force-pushed the fix-apt-remount branch 2 times, most recently from 3ea3d56 to 5180d9c Compare August 11, 2026 12:22
For read-only or verification commands such as 'dpkg
--print-architecture',
'dpkg --print-foreign-architectures' and 'apt list --upgradable', no
data
is written to the system, so remounting the immutable /usr to rw is
unnecessary. Set IMMUTABLE_DISABLE_REMOUNT=true on these subprocesses to
skip the redundant remount flow.

Note: most other dpkg/apt query call sites inherit this variable via the
process-level os.Setenv in src/internal/system/system_apt.go init().
config/cache is the exception because it does not import the system
package, so an explicit Env assignment there is required.

PMS: TASK-393303
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码精准收窄了环境变量作用域并清理了冗余初始化逻辑,重构质量高
逻辑完全正确且显著提升了代码可维护性,仅因忽略错误处理扣5分

■ 【详细分析】

  • 1.语法逻辑基本正确✓

system_apt.golastore-apt-clean/main.golastore-daemon/main.golastore-tools/main.go 中将 IMMUTABLE_DISABLE_REMOUNT 环境变量的设置从全局 init() 迁移至各可执行入口的 main() 函数,逻辑严谨,符合最小作用域原则。在 upgradable.go 中移除了对子进程显式追加该环境变量的操作,因为父进程 main() 已设置,继承机制保证了逻辑正确性。在 package.gocheck.go 中清理了完全相同的未使用变量 sysRealArch 及其 init() 函数,消除了死代码。
潜在问题:所有调用 os.Setenv 的地方均使用 _ = 忽略了返回的错误,虽然在当前场景下设置环境变量极少失败,但这属于错误处理缺失。
建议:在 main 函数入口处增加对 os.Setenv 返回值的日志记录,以便在极端情况下(如系统资源耗尽)排查问题。

  • 2.代码质量优秀✓

变更极大改善了代码结构。原实现在被多个二进制文件间接依赖的 system_apt.goinit() 中设置环境变量,会导致任何导入该包的程序都受此副作用影响,违反了最小惊讶原则。重构后,各二进制按需自举,职责清晰。删除 package.gocheck.go 中重复的 init() 块,消除了代码坏味道,提升了可读性。新增的注释“默认禁用不可变系统的 remount,仅对需要写 /usr 的子进程显式置为 false”准确解释了设计意图。
建议:无需额外修改,当前代码结构已非常清晰。

  • 3.代码性能高效✓

移除了 package.gocheck.go 中在 init() 阶段通过 exec.Command 启动 dpkg 子进程获取架构的操作。这不仅减少了两个冗余的进程创建开销,还加快了相关模块的初始化速度,属于正向的性能优化。
建议:保持当前实现。

  • 4.代码安全存在0个安全漏洞✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次变更未引入新的安全风险。原代码中 upgradable.goqueryDpkgUpgradeInfoByAptList 函数调用了 exec.Command("apt", ...) 并带有 #nosec G204 标记,虽然移除了 cmd.Env 的赋值,但由于环境变量已从父进程继承,且参数拼接部分未发生改变,安全边界保持不变。移除死代码减少了潜在的攻击面。
建议:继续保持对 exec.Command 调用的参数校验。

■ 【改进建议代码示例】

// src/lastore-tools/main.go
func main() {
	// 默认禁用不可变系统的 remount,仅对需要写 /usr 的子进程显式置为 false
	if err := os.Setenv("IMMUTABLE_DISABLE_REMOUNT", "true"); err != nil {
		logger.Warningf("Failed to set IMMUTABLE_DISABLE_REMOUNT environment variable: %v", err)
	}

	// 清除语言相关环境变量
	_ = utils.UnsetEnv("LC_ALL")
	// ...
}

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: qiuzhiqian, zhaohuiw42

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

@qiuzhiqian
qiuzhiqian merged commit 7a8d22c into master Aug 11, 2026
23 of 28 checks passed
@qiuzhiqian
qiuzhiqian deleted the fix-apt-remount branch August 11, 2026 13:01
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