Skip to content

ENT-14297: Improve error message when cf-agent cannot write to an immutable file (3.27)#6238

Open
nickanderson wants to merge 2 commits into
cfengine:3.27.xfrom
nickanderson:ENT-14297/3.27.x
Open

ENT-14297: Improve error message when cf-agent cannot write to an immutable file (3.27)#6238
nickanderson wants to merge 2 commits into
cfengine:3.27.xfrom
nickanderson:ENT-14297/3.27.x

Conversation

@nickanderson

Copy link
Copy Markdown
Member

Backport of ENT-14297 to 3.27.x (master PR: #6236).

Improves the error message when cf-agent cannot write to a file whose immutable
bit is set and is not being overridden. The actual system error is always
reported, and (the immutable bit is set) is appended only when confirmed via
FSAttrsGetImmutableFlag() — never guessed from errno (an immutable file
typically fails with EPERM, but that is not exclusive to immutability), so
permission/MAC/read-only/full-filesystem failures are never mislabeled.

Detection lives at the two points where the write actually fails:

  • WriteContentFromString() — the in-place open for the content attribute.
  • OverrideImmutableRename() — the temporary-file rename that is the common
    landing point for edit_line, edit_xml, copy_from, and templating.

It is skipped when overriding the immutable bit (the agent then writes to a
temporary copy renamed into place after clearing the bit).

Cherry-picked cleanly from master (verify_files.c auto-merges; the shared
override_fsattrs.c is identical between 3.27.x and master).

Tests (tests/acceptance/10_files/unsafe/14..20_immutable_*) cover content,
edit_line, copy_from, mustache, cfengine-template, edit_xml, and inline_mustache.
They are unsafe tests (they set the immutable bit), so they run in the chroot
acceptance job (UNSAFE_TESTS=1), not the GitHub Actions
--tests=common,errorlog,errorexit run.

Ticket: ENT-14297

…-14297)

Cover every path where cf-agent can fail to write a file because the
immutable bit is set and is not being overridden, asserting that the
error names the immutable bit:

  - content           (14, fails at the in-place open)
  - edit_line         (15)
  - copy_from         (16)
  - mustache          (17)  fail at the temporary-file rename in
  - cfengine template (18)  OverrideImmutableRename()
  - edit_xml          (19)
  - inline_mustache   (20)

Each test drives a self-contained sub policy in its own agent process:
the sub creates the target, sets the immutable bit via body fsattrs, and
then attempts the modification. The test asserts on the captured output
using native functions only (execresult_as_data + regcmp) and never fails
on the sub's non-zero exit. Teardown clears the immutable bit natively.

(cherry picked from commit 4d490e5)
…4297)

When cf-agent could not write a file whose immutable bit is set, the
error gave no reason. Now the actual system error is always reported, and
the immutable bit is called out explicitly when it is set (and not being
overridden):

    error: Cannot open file '/tmp/x' for writing: Operation not permitted (the immutable bit is set)
    error: Failed to replace original file '/tmp/x' with copy '...': Operation not permitted (the immutable bit is set)

The immutable annotation is confirmed via FSAttrsGetImmutableFlag(), not
guessed from errno (an immutable file typically fails with EPERM, but
that is not exclusive to immutability), so other causes (permissions, MAC
policy, a read-only or full filesystem) are never mislabeled.

The check lives at the two points where the write actually fails: the
in-place open in WriteContentFromString() (content), and the temporary-
file rename in OverrideImmutableRename(), which is the common landing
point for edit_line, edit_xml, copy_from and templating. It is skipped
when overriding the immutable bit, since then the agent writes to a
temporary copy that is renamed into place after clearing the bit.

(cherry picked from commit b9cab70)
@nickanderson nickanderson changed the title Improve error message when cf-agent cannot write to an immutable file (ENT-14297, 3.27.x backport) ENT-14297: Improve error message when cf-agent cannot write to an immutable file (3.27) Jul 17, 2026
@nickanderson

Copy link
Copy Markdown
Member Author

@cf-bottom jenkins please

@cf-bottom

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants