Skip to content

fix(cli): use HOME env instead of hardcoded "/home/" path prefix check#1718

Merged
dengbo11 merged 1 commit into
OpenAtom-Linyaps:masterfrom
dengbo11:file-mapping
Jul 2, 2026
Merged

fix(cli): use HOME env instead of hardcoded "/home/" path prefix check#1718
dengbo11 merged 1 commit into
OpenAtom-Linyaps:masterfrom
dengbo11:file-mapping

Conversation

@dengbo11

@dengbo11 dengbo11 commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator
  • Replace hardcoded "/home/" string prefix check with HOME environment variable to support non-standard home directories
  • Add null and empty string checks for HOME env to handle edge cases
  • Append trailing slash to HOME path before prefix matching to avoid false positive on paths like /home/bob-xxx
  • Fix minor indentation on LogE call

@dengbo11 dengbo11 requested a review from reddevillg July 2, 2026 01:47

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request replaces the hardcoded '/home' path check with a dynamic check using the 'HOME' environment variable to avoid mapping files under the user's home directory. The review feedback correctly identifies an edge case where the home directory itself (without a trailing slash) would fail the prefix check and be incorrectly mapped, and offers a code suggestion to fix this issue.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +1797 to 1803
auto homeStr = std::string(homePath);
if (homeStr.back() != '/') {
homeStr.push_back('/');
}
if (auto tmp = target.string(); tmp.rfind(homeStr, 0) == 0) {
return target;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

If the target path is exactly the user's home directory (e.g., /home/bob), appending a trailing slash to homeStr (making it /home/bob/) will cause the prefix check tmp.rfind(homeStr, 0) == 0 to fail. As a result, the home directory itself will be incorrectly mapped to /run/host/rootfs/home/bob.

To fix this, we should strip any trailing slashes from the configured HOME path first, and then check if the target path is either exactly equal to the home directory or starts with the home directory followed by a slash.

    auto homeStr = std::string(homePath);
    while (homeStr.size() > 1 && homeStr.back() == '/') {
        homeStr.pop_back();
    }
    if (auto tmp = target.string(); tmp == homeStr || tmp.rfind(homeStr == "/" ? "/" : homeStr + "/", 0) == 0) {
        return target;
    }

@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
libs/linglong/src/linglong/cli/cli.cpp 0.00% 8 Missing ⚠️
Files with missing lines Coverage Δ
libs/linglong/src/linglong/cli/cli.cpp 1.75% <0.00%> (-0.01%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Replace hardcoded "/home/" string prefix check with HOME environment variable to support non-standard home directories
- Add null and empty string checks for HOME env to handle edge cases
- Append trailing slash to HOME path before prefix matching to avoid false positive on paths like /home/bob-xxx
- Fix minor indentation on LogE call
@deepin-ci-robot

Copy link
Copy Markdown
Collaborator

deepin pr auto review

★ 总体评分:60分

■ 【总体评价】

代码修复了硬编码路径导致的逻辑缺陷,但引入了依赖不可信环境变量的安全漏洞
逻辑正确但因环境变量伪造导致安全检查绕过扣40分

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

修复后的代码正确获取环境变量,并严格判空,通过追加斜杠保证了前缀匹配的准确性,不存在空指针解引用或未定义行为

  • 2.代码质量(良好)✓

移除了硬编码字符串,提升了代码的可移植性和可维护性,注释更新准确反映了当前逻辑意图

  • 3.代码性能(高效)✓

getenv调用和字符串操作开销极低,对整体性能无负面影响

  • 4.代码安全(存在 1 个安全漏洞)✕

漏洞对比统计:新增漏洞 1 个,减少漏洞 0 个,持平 0 个
在沙箱路径校验场景下依赖不可信的环境变量,攻击者可通过伪造环境变量绕过安全限制

  • 安全漏洞1(中危):环境变量伪造导致安全检查绕过 在 Cli::content 函数中,代码使用 getenv 获取用户主目录以决定是否将符号链接目标映射进容器。由于环境变量可被用户或调用方轻易篡改,攻击者可通过设置伪造的 HOME 环境变量来欺骗路径前缀校验逻辑,导致原本应被拦截的敏感目录下的文件被错误地映射进容器,引发潜在的信息泄露或容器逃逸风险 ——非常重要

  • 建议:摒弃使用 getenv 获取主目录的做法,改用 getpwuid 系统调用获取真实的用户主目录路径,并对该路径进行规范化处理后再进行前缀匹配

■ 【改进建议代码示例】

#include <pwd.h>
#include <unistd.h>

    // 获取真实的用户主目录,不依赖可伪造的环境变量
    struct passwd *pw = getpwuid(getuid());
    if (pw == nullptr || pw->pw_dir == nullptr || pw->pw_dir[0] == '\0') {
        LogE("failed to get real user home directory");
        return target;
    }

    // Don't map files under the user's home directory
    auto homeStr = std::string(pw->pw_dir);
    if (homeStr.back() != '/') {
        homeStr.push_back('/');
    }
    if (auto tmp = target.string(); tmp.rfind(homeStr, 0) == 0) {
        return target;
    }

@deepin-ci-robot

Copy link
Copy Markdown
Collaborator

@dengbo11: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
github-pr-review-ci 645d2e9 link true /test github-pr-review-ci

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@deepin-ci-robot

Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengbo11, reddevillg

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

@dengbo11 dengbo11 merged commit ff1097f into OpenAtom-Linyaps:master Jul 2, 2026
15 of 18 checks passed
@dengbo11 dengbo11 deleted the file-mapping branch July 2, 2026 02:26
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