Skip to content

file.managed omits mode from changes when creating a file (test=True and real runs), making created-file permissions unauditable #69969

Open

Description

@non7top

Description

When file.managed creates a file, the mode it applies is never included in changes — in neither the test=True plan nor the real run's output. When the file already exists and only its mode is wrong, the mode is reported. Same module, same property, observability depends on whether the file happens to already exist.

The mode itself is applied correctly, so this is not a vulnerability — it is a reporting/auditability defect. But it has a consequence worth taking seriously:

A state declaring mode: '0600' and an otherwise identical state declaring no mode at all produce byte-identical output, in both the plan and the real run, while producing different results on disk (0600 vs a umask-derived mode, 0644 under a typical root umask). Neither Salt's pre-application plan nor its post-application output distinguishes a root-only file from a world-readable one.

Three things make this more than cosmetic:

  1. Absence is misleading, not merely missing. Because the existing-file path does print a mode: line, an operator can reasonably read the absence of that line as "no mode is being managed here" — which is exactly inverted.

  2. The run output is the audit record. Job returns are what get retained (job cache, external returners, log shipping). That retained record permanently omits the mode of every file Salt creates, so there is no in-band retrospective verification either. state.show_sls exposes the declared mode, but that is intent rather than applied effect, it is not the retained record, and under conditional Jinja it need not match what a given run actually did.

  3. The gap is irrecoverable, because creation is a one-shot event. Over the entire lifetime of a correctly-declared mode: '0600' file, the mode appears in no output at any point:

    stage reported changes
    plan, before creation {'newfile': '/tmp/lifetime.txt'}
    real apply (creation) {'diff': 'New file'}
    plan, after creation {} — "is in the correct state"
    real apply, again {} — "is in the correct state"

    Re-running cannot back-fill the record: subsequent runs assert conformance without disclosing the property being conformed to. The only moment the mode could be captured is the moment it is omitted, and that moment never recurs.

This matters most in a CI / change-control workflow, where test=True is run on a proposed change, the plan is reviewed and stored as the authorising artifact, and idempotency is asserted from that baseline forward. For every file the run creates, the permissions are absent from that baseline and unrecoverable downstream — so an auditor reviewing the plan for a state that writes secrets must assume the worst case, with no way to discharge that assumption from the artifact then or later.

Setup

Masterless local run (salt-call --local). Nothing environment-specific — reproduced with inline contents:, no fileserver or template involved.

Steps to Reproduce

Two states differing only in the presence of mode:

# with_mode.sls
/tmp/secretfile.txt:
  file.managed:
    - contents: 'mysql_root_password=hunter2'
    - mode: '0600'
# without_mode.sls
/tmp/secretfile.txt:
  file.managed:
    - contents: 'mysql_root_password=hunter2'
rm -f /tmp/secretfile.txt
salt-call --local --out=json state.apply with_mode    test=True
rm -f /tmp/secretfile.txt
salt-call --local --out=json state.apply without_mode test=True

changes for both plans, identical:

{ "newfile": "/tmp/secretfile.txt" }

Applying each for real:

with mode 0600  -> actual mode: 600    reported changes: {'diff': 'New file'}
with NO mode    -> actual mode: 644    reported changes: {'diff': 'New file'}

For contrast, with the file already existing, content already correct, and only the mode wrong (0644 -> 0600):

Changes:
          mode:
              0600

Notes / scope — the defect is specific to the creation path; reporting on existing files is complete:

  • Existing file, wrong mode and wrong content: reports both, {'diff': ..., 'mode': '0600'} — a content change does not mask the mode.
  • replace: False + wrong mode: reports {'mode': '0600'} in both test=True and the real run.
  • So drift detection and idempotency on existing files are unaffected. Only the initial creation omits the mode.
  • show_changes: False is not a factor; behaviour is identical with and without it.
  • The unspecified-mode case is umask-derived, not a fixed default (umask 022 -> 644, 000 -> 666, 077 -> 600), so what an unspecified-mode state produces is environment-dependent.
  • A declared mode is applied reliably regardless of umask (umask 000 + mode: '0600' -> 600).

Expected Behavior

When file.managed creates a file, changes should include the mode (and ideally user/group) being applied, e.g.:

Changes:
          newfile:
              /tmp/secretfile.txt
          mode:
              0600

— so the plan is sufficient to review the permissions of a to-be-created file, and the run output is sufficient to verify them after the fact, consistent with the existing-file path.

Actual Behavior

changes contains only newfile (test=True) or diff: New file (real run). The mode is omitted, making a correct secrets-file state and an accidentally world-readable one indistinguishable in both the plan and the retained result.

Versions Report

salt-call 3008.2 (Argon)

Minion OS: Ubuntu 24.04 (osfinger: Ubuntu-24.04), saltversion: 3008.2.

Additional context

Found while reviewing the test=True plan of a state that writes an installer answer-file containing a plaintext database root password. The plan reported only newfile:, which gave no way to confirm from the plan that the file would land 0600 rather than umask-default — the single most important property to verify about that state before letting it run.

Activity

  1. non7top commented on Aug 7, 2026

    @non7top
    Author

    Some additional detail on scope, existing mitigations, and why the fix should be cheap.

    The two paths are in the same function, and the values are already in scope

    Both branches live in salt/modules/file.py, in check_file_meta (def at line 6059 in 3008.2):

    # create path (~6217) — early return, skips the perms reporting block entirely
    if not lstats and not new_file_diff:
        changes["newfile"] = name
        if any([ignore_ordering, ignore_whitespace, ignore_comment_characters]):
            return True, changes
        return changes
    
    # existing-file path (~6318-6330) — same function, ~110 lines later
    if user is not None and user != lstats["user"] and user != lstats["uid"]:
        changes["user"] = user
    if group is not None and group != lstats["group"] and group != lstats["gid"]:
        changes["group"] = group
    smode = salt.utils.files.normalize_mode(lstats["mode"])
    mode = salt.utils.files.normalize_mode(mode)
    if mode is not None and mode != smode:
        changes["mode"] = mode

    mode, user and group are already parameters of check_file_meta, so they are in scope at the early return. Reporting them on the create path needs no new plumbing, no new data, and no change to what the state actually does — it only adds keys to a dict that is already being returned. On the create path there is no prior mode to compare against, so the natural semantics are to report the mode being established whenever mode is not None (for a new file, establishing the mode is part of the change).

    There is no existing mitigation

    new_file_diff: True (added in 3008.0, which extends reporting on this same branch) does not surface the mode:

    new_file_diff:True, plan -> {'diff': '--- \n+++ \n@@ -0,0 +1 @@\n+mysql_root_password=hunter2\n'}
    new_file_diff:True, real -> {'diff': '--- \n+++ \n@@ -0,0 +1 @@\n+mysql_root_password=hunter2\n'}
    actual mode: 600
    

    It reports the content and still omits the mode — and for the secrets-file case that motivated this report it is unusable anyway, since it prints the secret into the plan and hence into the retained audit record. So the only flag that extends new-file reporting is precisely the one you cannot enable on a file whose permissions you care about.

    That flag is also useful precedent: this same early-return branch was deliberately extended in 3008.0 to improve new-file observability, so extending it again to cover mode/user/group is consistent with recent direction rather than a new concept.

    Summary of the ask

    • Low effort: populate mode (and ideally user/group) in the create branch before returning.
    • Low risk: reporting-only; the mode is already applied correctly today, verified as reliable even under umask 000.
    • Already-established idiom: the exact reporting pattern exists ~110 lines below in the same function.
    • Narrow scope: existing-file reporting is already complete (mode reported alone, and alongside a content diff without being masked; replace: False also reports correctly), so idempotency and drift detection on existing files are unaffected.
  2. added a commit that references this issue on Aug 19, 2026
    b860413
  3. added a commit that references this issue on Sep 3, 2026
    ac912ac
  4. added
    severity-medium3rd level, incorrect or bad functionality, confusing and lacks a work around
    bugbroken, incorrect, or confusing behavior
    on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugbroken, incorrect, or confusing behaviorseverity-medium3rd level, incorrect or bad functionality, confusing and lacks a work around

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions