{"id":751,"date":"2026-11-27T06:21:00","date_gmt":"2026-11-27T14:21:00","guid":{"rendered":"https:\/\/blog.canutethegreat.com\/?p=751"},"modified":"2026-11-23T01:31:46","modified_gmt":"2026-11-23T09:31:46","slug":"linux-privilege-escalation-hygiene","status":"publish","type":"post","link":"https:\/\/blog.canutethegreat.com\/index.php\/2026\/11\/27\/linux-privilege-escalation-hygiene\/","title":{"rendered":"Linux privilege escalation hygiene: the SUID bits, capabilities and sudo rules that quietly hand out root"},"content":{"rendered":"<p>Most local privilege escalation I run into on real systems isn&#8217;t a kernel exploit. It&#8217;s a configuration someone added three years ago to make a deploy script work, and nobody has looked at since. A binary with a stray setuid bit, a capability granted to an interpreter, a sudoers line with a wildcard in it. Each one is a perfectly legitimate way for an unprivileged user to become root, and the system is doing exactly what it was told.<\/p>\n<p>This post is about auditing those three places and tightening them. It&#8217;s defensive: run these on boxes you own.<\/p>\n<h2>SUID and SGID binaries<\/h2>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/blog.canutethegreat.com\/wp-content\/uploads\/2026\/11\/diagram-1.png\" alt=\"Before and after diagram. Before: an unprivileged user reaches root through a stray SUID binary, a cap_setuid interpreter and a sudo tee wildcard rule. After: nosuid mounts with baseline diff, AmbientCapabilities in the unit file and a validated wrapper script close those paths, leaving one narrow logged route.\" \/><\/figure>\n<p>The diagram compares the three paths above before and after the controls described in this post.<\/p>\n<p>A file with the setuid bit runs with the owner&#8217;s UID instead of the caller&#8217;s. That is how <code>passwd<\/code> can write to <code>\/etc\/shadow<\/code>. It also means every bug in that binary is a root bug, and the bug can be reached by any user who can execute it, through arguments, environment variables, and files it opens.<\/p>\n<p>The history is bad. CVE-2021-4034 (PwnKit) was an out-of-bounds read and write in <code>pkexec<\/code>, a SUID-root binary shipped on essentially every mainstream distribution, and it sat there for about twelve years. CVE-2023-4911 (Looney Tunables) was a buffer overflow in glibc&#8217;s dynamic loader, triggered through the <code>GLIBC_TUNABLES<\/code> environment variable, which made every SUID binary on affected systems a way in. Neither needed a misconfiguration. The attack surface was just there.<\/p>\n<p>So the first job is an inventory, and a diff against it over time:<\/p>\n<pre tabindex=\"0\" class=\"wp-block-code\"><code>find \/ -xdev -perm -4000 -type f 2&gt;\/dev\/null | sort &gt; suid.now\nfind \/ -xdev -perm -2000 -type f 2&gt;\/dev\/null | sort &gt; sgid.now\ndiff suid.baseline suid.now<\/code><\/pre>\n<p>On a stock Ubuntu 24.04 container image the SUID list is short: <code>chfn<\/code>, <code>chsh<\/code>, <code>gpasswd<\/code>, <code>mount<\/code>, <code>newgrp<\/code>, <code>passwd<\/code>, <code>su<\/code>, <code>umount<\/code>. A full server install has more. Anything that isn&#8217;t in your baseline needs an owner and a reason.<\/p>\n<p>Three habits remove most of the risk. Mount <code>\/tmp<\/code>, <code>\/var\/tmp<\/code>, <code>\/home<\/code> and <code>\/dev\/shm<\/code> with <code>nosuid,nodev<\/code>, so a SUID file dropped by an attacker or a user is inert there. Remove the bit from binaries you don&#8217;t use (<code>chfn<\/code> and <code>chsh<\/code> are good candidates on a server with no local logins). And alert on new SUID files at creation time. With auditd, a rule on the chmod family catches the bit being set (04000 is 0x800):<\/p>\n<pre tabindex=\"0\" class=\"wp-block-code\"><code>-a always,exit -F arch=b64 -S chmod,fchmod -F a1&amp;0x800 -F auid&gt;=1000 -F auid!=unset -k suid_set\n-a always,exit -F arch=b64 -S fchmodat -F a2&amp;0x800 -F auid&gt;=1000 -F auid!=unset -k suid_set<\/code><\/pre>\n<p>That doesn&#8217;t catch a SUID file arriving inside a tarball extracted as root, so the periodic find-and-diff stays.<\/p>\n<h2>File capabilities<\/h2>\n<p>Capabilities split root into pieces, which sounds like a safe improvement. In practice, some of those pieces are root under another name. <code>CAP_SETUID<\/code> lets a process become any UID, including 0. <code>CAP_DAC_READ_SEARCH<\/code> bypasses file read permission checks, so it reads <code>\/etc\/shadow<\/code> and SSH host keys. <code>CAP_SYS_ADMIN<\/code> is so broad that the capabilities man page itself describes it as overloaded. <code>CAP_SYS_PTRACE<\/code> lets you inject into other processes.<\/p>\n<p>The classic mistake is giving a capability to something that can run arbitrary code. I reproduced it in a throwaway container for this post: copy the Python interpreter, give it <code>cap_setuid<\/code>, and an ordinary user can become root.<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/blog.canutethegreat.com\/wp-content\/uploads\/2026\/11\/shot.png\" alt=\"Terminal output from a disposable Ubuntu 24.04 container: the SUID file list, getcap showing cap_setuid=ep on a copied python3 binary, and user alice (uid 1001) running it to get uid=0(root).\" \/><\/figure>\n<p>The screenshot is a real run in a disposable Ubuntu 24.04 container, with the output rendered to an image. The user <code>alice<\/code> has UID 1001 until she calls <code>os.setuid(0)<\/code> through the capable interpreter. Nothing in that sequence is a vulnerability. The capability does what it says.<\/p>\n<p>To find these on a host:<\/p>\n<pre tabindex=\"0\" class=\"wp-block-code\"><code>getcap -r \/ 2&gt;\/dev\/null<\/code><\/pre>\n<p>Expect a few legitimate entries, for example <code>cap_net_bind_service<\/code> on a web server binary or <code>cap_net_raw<\/code> on <code>ping<\/code> on distributions that use capabilities instead of SUID for it. Any capability on a shell, an interpreter, <code>find<\/code>, <code>tar<\/code>, <code>cp<\/code>, <code>vim<\/code> or an editor is a finding. Check these against GTFOBins, which lists which common binaries turn each capability into a shell or a file write.<\/p>\n<p>For services, don&#8217;t set file capabilities at all. Use <code>AmbientCapabilities=CAP_NET_BIND_SERVICE<\/code> and <code>CapabilityBoundingSet=<\/code> in the unit file, which scopes the grant to that service and leaves no tagged binary on disk for someone else to execute. I covered the systemd side in the sandboxing post earlier in this blog.<\/p>\n<h2>Sudo rules<\/h2>\n<p>Sudo is where I find the most variety. The pattern is always the same: a rule meant to allow one narrow action that allows a general one instead.<\/p>\n<p>Anything that can spawn a shell or write arbitrary files is root. <code>NOPASSWD: \/usr\/bin\/vim<\/code>, <code>less<\/code>, <code>find<\/code>, <code>awk<\/code>, <code>tar<\/code>, <code>git<\/code>, <code>man<\/code>, any interpreter. A user runs the allowed program, then escapes to a shell from inside it, and the shell keeps root. The <code>NOEXEC<\/code> tag blocks some of these by intercepting <code>execve<\/code> through LD_PRELOAD, but the sudoers manual is clear that it&#8217;s not a defence against a determined user, so I don&#8217;t count on it.<\/p>\n<p>Wildcards are the other classic. In sudoers, <code>*<\/code> matches any characters including slashes and spaces. This rule looks like it restricts writes to a log directory:<\/p>\n<pre tabindex=\"0\" class=\"wp-block-code\"><code>deploy ALL=(root) NOPASSWD: \/usr\/bin\/tee \/var\/log\/app\/*<\/code><\/pre>\n<p>It allows <code>sudo tee \/var\/log\/app\/..\/..\/..\/etc\/sudoers.d\/x<\/code>, and it allows extra arguments after the first path, so a second file can be named too. The fix is a wrapper script that validates its input, or better, a design that doesn&#8217;t need root for it, like a group-writable log directory.<\/p>\n<p>Sudo itself has had its moments. CVE-2021-3156 (Baron Samedit) was a heap overflow in sudo&#8217;s argument handling, exploitable by any local user without being in sudoers, affecting 1.8.2 through 1.8.31p2 and 1.9.0 through 1.9.5p1. CVE-2023-22809 let a user with <code>sudoedit<\/code> access edit extra files by smuggling them into the <code>EDITOR<\/code> variable after a <code>--<\/code>. Keeping sudo patched matters, and so does keeping the number of people with sudo rights small, because those bugs are only reachable by whoever can run it.<\/p>\n<p>Audit with these:<\/p>\n<pre tabindex=\"0\" class=\"wp-block-code\"><code>sudo -l -U deploy\ngrep -rEn 'NOPASSWD|\\*|!|env_keep|SETENV' \/etc\/sudoers \/etc\/sudoers.d\/\nvisudo -c<\/code><\/pre>\n<p>Things I&#8217;d rather see in a sudoers file: full paths only, <code>sudoedit<\/code> instead of a bare editor, no <code>SETENV<\/code> tag, <code>Defaults use_pty<\/code> and <code>Defaults logfile=<\/code> or shipped logs, and a <code>Cmnd_Alias<\/code> per task so a review can read intent. If a rule needs a wildcard, treat it as code and review it like code.<\/p>\n<h2>Making it stick<\/h2>\n<p>A one-time cleanup decays in a quarter. What has worked for me is boring: put the SUID and capability baselines into configuration management so the expected state is declared, run the find and getcap diffs on a schedule, and ship sudo&#8217;s logs and the auditd <code>suid_set<\/code> key to wherever you already alert. Lynis and OpenSCAP both check parts of this if you&#8217;d rather start from a tool than from scripts. Tools like linPEAS are worth running against your own hosts once, because they show you the same list an attacker&#8217;s first script sees.<\/p>\n<p>The rule I apply to every entry on these three lists is the same one: if you can&#8217;t say who needs it and why, it goes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most local privilege escalation I run into on real systems isn&#8217;t a kernel exploit. It&#8217;s a configuration someone added three years ago to make a deploy script work, and nobody has looked at since. A binary with a stray setuid bit, a capability granted to an interpreter, a sudoers line with a wildcard in it. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":754,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,21],"tags":[46,95,393,395,394,93],"class_list":["post-751","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-security","tag-auditd","tag-least-privilege","tag-linux","tag-privilege-escalation","tag-sudo","tag-systemd"],"_links":{"self":[{"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/posts\/751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/comments?post=751"}],"version-history":[{"count":2,"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/posts\/751\/revisions"}],"predecessor-version":[{"id":935,"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/posts\/751\/revisions\/935"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/media\/754"}],"wp:attachment":[{"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/media?parent=751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/categories?post=751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/tags?post=751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}