Somebody has got root on a server. It happens, through a forgotten service or a password that was never quite as private as everyone assumed, and the first thing an intruder with root wants is to stay. The obvious place is a system binary or a startup script. The administrator had seen this coming and marked those files immutable.
On Linux, that buys about a second:
chattr -i /usr/sbin/sshd
The i attribute can be cleared by "the superuser or a process possessing the CAP_LINUX_IMMUTABLE capability", in the words of the manual page, and the intruder is the superuser. On a FreeBSD machine running at securelevel 1, the same move looks like this:
chflags noschg /usr/sbin/sshd
The kernel answers with EPERM, which chflags reports as "Operation not permitted", and it gives root exactly the answer it would give anybody else.
A ratchet in the kernel
security(7) describes five levels and settles the important part in one sentence: "Any super-user process can raise the level, but no process can lower it." At -1, the default, nothing is enforced. At 1, the secure mode, "the system immutable and system append-only flags may not be turned off", mounted disks and kernel memory may no longer be opened for writing, and "kernel modules (see kld(4)) may not be loaded or unloaded". Level 2 also closes unmounted disks to writing, and level 3 freezes the packet filter rules of ipfw and pf.
Switching it on takes two lines in /etc/rc.conf:
kern_securelevel_enable="YES"
kern_securelevel="1"
The idea comes from Berkeley. The kern_sysctl.c of 4.4BSD, dated 16 June 1993, holds the whole rule in a single condition, if (level < securelevel && p->p_pid != 1): anybody may raise the level, and only init may lower it. FreeBSD has since closed even that door. init(8) today says of the level that "it will be at least 1 for subsequent operation, even on return to single-user", so only a reboot starts the count again. The check that every subsystem calls is a single line. securelevel_gt() in kern_prot.c returns EPERM when cr->cr_prison->pr_securelevel is above the level asked about. UFS calls it before a privileged process may touch a file that carries a system flag, and ZFS does the same.
That last detail is worth a second look, because the level is read from the prison of the caller.
One level per jail
Every jail carries a securelevel of its own. jail(8) puts the rule plainly: "A jail never has a lower securelevel than its parent system, but by setting this parameter it may have a higher one." The sysctl handler in kern_mib.c enforces the other half, raising every child jail along with its parent and lowering none of them.
So a host can run at 1, where its own updates are still possible after a planned reboot, whilst the jail that faces the internet runs at 3 and keeps its system files frozen until the jail is started again. With the host locked as well there are two levels to get past, and no way to lower either from inside. One integer in the jail's configuration sets it up, and nothing inside the jail can change it.
Linux had one
None of this is foreign to Linux, which makes the present state more interesting still. From version 2.2.11 the kernel kept a system-wide capability bounding set in /proc/sys/kernel/cap-bound. According to capabilities(7), "Only the init process may set capabilities in the capability bounding set"; everyone else could only take capabilities away. Clear CAP_LINUX_IMMUTABLE there at boot, and no process on the machine could clear an immutable flag again. It was a ratchet, near enough.
With 2.6.25 the system-wide set was gone. "From Linux 2.6.25, the capability bounding set is a per-thread attribute. (The system-wide capability bounding set described below no longer exists.)" In its place, a service manager can give each daemon a set of its own, which is finer than any single number, and systemd does exactly that through CapabilityBoundingSet= in each unit.
In September 2013 Matthew Garrett proposed bringing the idea back for the kernel itself, and said where it came from: "There's strong parallels between the functionality that I'm interested in and the BSD securelevel interface, so here's a trivial implementation." In February 2014, after a discussion at the Linux Plumbers Conference, he wrote that the patches were "basically fine but that Linus hated the name "securelevel" more than I hate pickled herring". The work was renamed "Trusted Kernel" and later lockdown, the name under which version 5.4 took it in.
Lockdown does a real job. It closes /dev/mem, /dev/kmem, kprobes and a long list of hardware interfaces through which root could rewrite the running kernel. It protects the kernel image, which is what it was designed for, and it still loads modules, provided they are signed. It has no opinion on files. The immutable attribute on /usr/sbin/sshd is as removable under lockdown as without it.
The strongest answer Linux has lies elsewhere, in mandatory access control. SELinux, in the words of its own notebook, "stops all access unless allowed by policy", and a policy written for the purpose can refuse root the very operations that securelevel refuses. It is a policy, though, which somebody has to write for every program on the machine and keep current afterwards, and the same notebook warns that without that work "there could be 'a false sense of security'". Securelevel asks for one integer.
The limit
It is off by default. FreeBSD ships at -1, and the administrator has to set the level and mark the files. A machine on which nobody did either is as open to its root as any other.
It makes maintenance slower. security(7) puts it bluntly: "upgrading the system is much more difficult when you operate at a higher security level". Replacing a file marked schg means rebooting at a lower level first, and the same manual page warns that marking everything with it closes "a detection window".
The console still decides. The level starts again with every boot, so anyone at the console can restart the machine into single-user mode before the level is raised. That is the design, and it moves the problem from the network into the room.
A note on the door
The Linux answer of today is fine-grained and spread out: a bounding set per unit, a lockdown mode for the kernel image, an attribute on the file that the superuser may clear. Each piece works where it is put. What the system no longer has is one setting that binds root everywhere at once, and the immutable flag on a stock Linux server is, against its own root, a note on the door asking politely not to come in.
FreeBSD kept the lock of 1993 and gave every jail a key of its own to turn it further, which leaves root on a FreeBSD server in the unaccustomed position of having to ask.