<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Linux on unordered</title>
		<link>https://unorde.red/categories/linux/</link>
		<description>Recent content in Linux on unordered</description>
		<generator>Hugo</generator>
		<language>en-US</language>
		
		
		
		
			<lastBuildDate>Sat, 17 Mar 2018 00:00:00 +0000</lastBuildDate>
		
			<atom:link href="https://unorde.red/categories/linux/feed.xml" rel="self" type="application/rss+xml" />
			<item>
				<title>uid</title>
				<link>https://unorde.red/uid/</link>
				<pubDate>Sat, 17 Mar 2018 00:00:00 +0000</pubDate>
				<guid>https://unorde.red/uid/</guid>
				<description>&lt;p&gt;What happens when an unprivileged user runs the binary&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-txt&#34; data-lang=&#34;txt&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;-r-s---r-x  1 root root 1024 Mar 17 21:57 binary&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;which executes the following lines of code?&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nf&#34;&gt;setreuid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;geteuid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(),&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;geteuid&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;());&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;nf&#34;&gt;system&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;/bin/sh&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Let&amp;rsquo;s find out. First, we need to understand what a uid is.&lt;/p&gt;&#xA;&lt;p&gt;The POSIX standard defines three different user identifiers. These allow&#xA;processes to dynamically take on different roles, providing they have the&#xA;required privileges.&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;The &lt;strong&gt;real uid&lt;/strong&gt; identifies the (real) owner of a process. The real uid can&#xA;be manipulated with &lt;code&gt;getuid()&lt;/code&gt; and &lt;code&gt;setreuid(uid_t ruid, uid_t euid)&lt;/code&gt;.&lt;/li&gt;&#xA;&lt;li&gt;The &lt;strong&gt;effective uid&lt;/strong&gt; of a process is used for access control. It is also&#xA;used as the owner for files created by that process. See &lt;code&gt;geteuid(void)&lt;/code&gt; and&#xA;&lt;code&gt;seteuid(uid_t euid)&lt;/code&gt; (note that confusingly, &lt;code&gt;setuid&lt;/code&gt; also sets the&#xA;effective uid).&lt;/li&gt;&#xA;&lt;li&gt;The &lt;strong&gt;saved uid&lt;/strong&gt; is used to store the effective user id when it is changed&#xA;temporarily. For example, a privileged process may need do some unprivileged&#xA;work. To do this, it would change its euid. But it would not be able to&#xA;change back unless its original (privileged) euid was stored somewhere.&#xA;That&amp;rsquo;s where the saved uid comes in: an unprivileged process may set its euid&#xA;exclusively to one of uid, suid, or euid.&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;Processes are managed by the kernel using a process descriptor. In practice,&#xA;this is a &lt;code&gt;task_struct&lt;/code&gt;&#xA;&lt;a href=&#34;https://github.com/torvalds/linux/blob/8f5fd927c3a7576d57248a2d7a0861c3f2795973/include/linux/sched.h#L524&#34;&gt;struct&lt;/a&gt;,&#xA;containing all the process information that the kernel needs. This includes a&#xA;pointer to a &lt;code&gt;cred&lt;/code&gt;&#xA;&lt;a href=&#34;https://github.com/torvalds/linux/blob/8f5fd927c3a7576d57248a2d7a0861c3f2795973/include/linux/cred.h#L111&#34;&gt;struct&lt;/a&gt;,&#xA;which stores the different uids mentioned above. This is how the kernel&#xA;determines whether or not a process has the privileges to do something.&lt;/p&gt;</description>
			</item>
	</channel>
</rss>
