the_open_door
coffee
ESEN
← Back to index

Append-only file attributes (chattr +a)

ATT&CKT1222.002

The ext family of filesystems has a layer of permissions that are neither the UNIX permissions nor POSIX ACLs: the extended attributes set with chattr and read with lsattr. Two of them matter here:

Removing either one requires the CAP_LINUX_IMMUTABLE capability, which in practice means root. A normal user cannot turn them off. The point about a is that you do not need to.

The signal: an error that is not about permissions#

The symptom is an "Operation not permitted" when saving with an editor over a file that is ours. It is very misleading, because the UNIX permissions say it is writable. The check is lsattr, and it is worth running it before fighting the editor:


lsattr /usr/local/sbin/ssh-alert.sh
-----a--------e--- /usr/local/sbin/ssh-alert.sh

That a in the fifth position explains everything: nano, vi and any > redirection fail. But >> works, because that is exactly what the attribute allows.

The abuse: a script root reads top to bottom#

If the append-only file is a script that root executes, the attribute protects nothing. A shell reads the script from top to bottom and runs whatever it finds, so appending a line has the same effect as editing the file:


echo 'chmod u+s /bin/bash' >> /usr/local/sbin/ssh-alert.sh

What is missing is the trigger, which is what you have to locate first. On late the script is launched by PAM on every SSH login, so opening a second session is enough; in other cases the trigger is a cron job or a systemd unit. Once it fires, /bin/bash is left with the SUID bit and the shell that preserves the euid closes the escalation:


bash -p

On that same machine, pspy showed where the attribute came from: a root cron ran chattr -a before touching the file and chattr +a afterwards. That back-and-forth confirms the attribute is deliberate and not a leftover.

Requirements#

A file with the a attribute that we have UNIX write permission on (owning it is enough), that is interpreted by a privileged process (a shell script, a config file read line by line, an authorized_keys), and a trigger that runs it again. If the file is a binary or a format that tolerates no trailing junk, appending gets you nowhere.

How to detect it. The at-rest indicator is the attribute where it does not belong: lsattr -R /etc /usr/local 2>/dev/null | grep -v -- '^--------------' lists the flagged files, and an a on an executable script is suspicious by itself, because append-only makes sense on a log, not on code. At runtime, auditd on the O_APPEND open of a file under /usr/local/sbin followed by root executing it is the whole chain, and the later chmod +s /bin/bash or sh -p is the payoff. Comparing the file against its version in configuration control gives the appended line away: the content grows at the end and nothing else changes.

Defence. A script root executes must be owned by root and not writable by anyone else; chattr +a is no substitute for ownership, it only constrains the shape of the write. If the intent is "nobody touches this", the right attribute is i, not a. And the root problem in these cases is not the attribute but the layout: move the file out of the user's reach, or stop running as root something that lives in someone else's directory.

Machines where it's used