Skip to content

[bug] Tilde for known-but-unlisted system users expands to '/' instead of staying unexpanded #1657

Closed

Description

@caomengxuan666

Summary

When a username is not resolvable via the user database, GNU leaves the tilde word unexpanded; rubash expands a subset of well-known system names (root, daemon, bin, …) to /.

Environment: Termux (no /etc/passwd entries for these users), niu 1.4.4 / rubash d15a02d vs GNU bash 5.3.20.

Regression seed

echo ~daemon
# GNU: ~daemon (unexpanded — no such user)
# niu:  / 

Same for ~root, ~bin, and suffixed ~root/x (niu //x, GNU ~root/x).

Measured (timeout 5 <shell> --noprofile --norc -c, 3/3 each side)

word niu GNU
~nosuchuser123 unexpanded (agree) unexpanded
~root / ~daemon / ~bin / unexpanded
~root/x //x ~root/x
~, ~+, /data/data/com.termux/files/home agree agree

So lookup failure is handled correctly for arbitrary names, but hardcoded system names bypass the lookup-failure path and map to /.

Dup-check

Searched all 73 open issues for tilde/home/expand/passwd — no match. Grep of labels harvest files for tilde — no hit. Known-multibyte/expansion issues (#1058, #1061) are unrelated. Note: on systems where these users do resolve, both shells presumably agree, so this bites on minimal/container user databases.

Found by differential fuzz round 4 (seed 12377, 400 snippets).

Activity

  1. added
    bug
    gnu-baseline
    P3-low
    area:expansion
    on Oct 25, 2026
  2. caomengxuan666 commented on Oct 25, 2026

    @caomengxuan666
    MemberAuthor

    Note for triage: this reproduces the exact table/words of open #1233 (~root/~daemon/~bin → /, ~root/x → //x, arbitrary names pass through) on the same Termux empty-passwd environment — likely the same hardcoded-fallback root cause. Consider closing as duplicate of #1233; leaving open for owner decision.

  3. caomengxuan666 commented on Oct 25, 2026

    @caomengxuan666
    MemberAuthor

    Adjudication (fix7/mopup-c cluster, full evidence in ~/workspace/rubash-fix7/clusters/mopup-c/REPORT.md §#1657):

    • All tilde expansion in the repo funnels into passwd_home_for_user, which on unix is a faithful getpwnam_r lookup (not found → tilde left as-is). There is no hardcoded system-user table anywhere in src/ (grep-verified).
    • On this machine (glibc), rubash and GNU bash 5.3.20 agree item-for-item: ~daemon → /usr/sbin, ~root → /root, ~nosuchuser123 unexpanded — both sides read the same passwd database, and GNU's tilde code pastes pw_dir verbatim just like we do.
    • The reported Termux divergence can therefore only enter through the platform libc: if bionic returns pw_dir="/" for its synthesized user entries, getpwnam_r cannot distinguish a synthesized entry from a real one, and any code-side "correction" would deviate from GNU's algorithm itself.

    Disposition: not an in-repo bug on current evidence. The in-repo invariant (faithful getpwnam lookup) is pinned by tests/issue1657_tilde_system_user_regressions.rs, landing with the mopup-c slice as Refs, not Fixes.

    Reclassification path: on a real Termux device, run a C probe of getpwnam("daemon") and record pw_dir. If bionic indeed returns /, this is a platform-database face and this issue should close as such; if it returns a real home, reopen with that evidence and it becomes an engine bug again.

  4. self-assigned this
    on Oct 25, 2026
  5. arookieofc commented on Oct 25, 2026

    @arookieofc
    Member

    Investigation (not a fix yet — needs a Termux-side probe before any code change):

    What I verified on a glibc host (rubash master vs GNU bash 5.2, both directions agree):

    • echo ~daemon ~root ~bin ~nosuchuser123 ~root/x ~ →
      rubash: /usr/sbin /root /bin ~nosuchuser123 /root/x /home/hatch
      GNU: identical, byte for byte.
    • So on a system where the passwd database is faithful, rubash already matches GNU, including unknown-user-kept-verbatim.

    What I ruled out in rubash's own code (master and the issue's d15a02d):

    • There is no hardcoded system-user table anywhere in the repo (grep for daemon across all sources finds nothing but comments, at both revisions). The issue title's "hardcoded system names" is an inference, not something present in code.
    • Every ~user path funnels into one function, passwd_home_for_user (src/expand/tilde/tilde.rs), which on unix is a plain getpwnam_r call: status != 0 or NULL result → None → the caller keeps the word verbatim. No / fallback exists in the tilde module or its callers (expand_word.rs, shell_options.rs, subscript_expansion.rs all keep the word on None).
    • GNU's own rule (vendored third_party/bash/lib/tilde/tilde.c:379-403, tilde_expand_word) is exactly the same: getpwnam NULL → savestring(filename), no extra check. So on one shared database, getpwnam and getpwnam_r must agree — and on glibc they do.

    Where the divergence must therefore live: Bionic's getpwnam_r vs getpwnam on Termux. GNU bash calls getpwnam; rubash calls getpwnam_r. If Bionic's _r variant returns a stub entry (pw_dir /) for these names while getpwnam returns NULL, rubash faithfully reports what libc told it, and no rubash-side table is involved. This VM has no Android/Bionic environment to confirm that, so I am not shipping a guessed fix — a hardcoded denylist of root/daemon/bin would be whack-a-mole (it would break systems where daemon legitimately resolves, e.g. glibc /usr/sbin above) and violates the repo's no-blacklist rule.

    What would settle it (one command on Termux):

    /* probe: getpwnam vs getpwnam_r for root/daemon/bin/nosuchuser123 */

    If the probe shows getpwnam("daemon") = NULL but getpwnam_r("daemon") returning pw_dir /, the fix belongs in the lookup call (e.g. prefer getpwnam on Bionic, or validate the _r result), and I'll implement it with a regression test. If both agree on Termux too, the niu 1.4.4 binary's environment needs a second look (e.g. a passwd file the release resolves differently).

    Leaving this open and unassigned-to-a-fix until that probe result lands; the glibc matrix above is the regression baseline either way.

  6. arookieofc commented on Oct 25, 2026

    @arookieofc
    Member

    Follow-up: the actual probe program referenced above (the previous comment's code block was a placeholder). Save as probe.c, run on Termux with cc probe.c -o probe && ./probe:

    #include <pwd.h>
    #include <stdio.h>
    
    int main(void) {
        const char *names[] = {"root", "daemon", "bin", "nosuchuser123", 0};
        for (int i = 0; names[i]; i++) {
            struct passwd *p = getpwnam(names[i]);
            struct passwd pwd;
            struct passwd *res = 0;
            char buf[4096];
            int st = getpwnam_r(names[i], &pwd, buf, sizeof buf, &res);
            printf("%s: getpwnam=%s getpwnam_r status=%d result=%s dir=%s\n",
                   names[i], p ? p->pw_dir : "(null)", st,
                   res ? "non-null" : "null", res ? res->pw_dir : "-");
        }
        return 0;
    }

    Interpretation:

    • getpwnam(daemon) = (null) but getpwnam_r returning dir=/ → the divergence is in Bionic's _r variant; fix belongs in rubash's lookup call.
    • Both NULL → rubash's Termux behavior needs re-measurement against a fresh build (the 1.4.4 binary may differ from master).
    • Both returning / → the Termux passwd database itself carries those entries, and GNU bash on Termux would need a second look too.
  7. added
    platform:windows
    on Oct 27, 2026
  8. added a commit that references this issue on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3-lowarea:expansionbuggnu-baselineplatform:windows

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions