home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / linux / security / ylonen-ssh / ssh-1.2.27-race-condition.patch < prev    next >
Text File  |  2000-06-09  |  14KB  |  338 lines

  1. http://marc.theaimsgroup.com/?l=secure-shell&m=95451481631163&w=2
  2.  
  3. List:     secure-shell
  4. Subject:  Serious bug in ssh 1.2.27
  5. From:     Wout van Albada <wout@365.co.uk>
  6. Date:     2000-03-31 10:17:08
  7.  
  8. --liOOAslEiF7prFVr
  9. Content-Type: text/plain; charset=us-ascii
  10. Content-Disposition: inline
  11.  
  12. Hi,
  13.  
  14. I think I encountered a serious bug in ssh 1.2.27. There seems to be
  15. a race condition where the ssh daemon (sshd) drops data when it has
  16. to send it over a slow line. I sent this bug report to ssh-bugs@cs.hut.fi
  17. and ipsec-support@ssh.fi on 27/03 but have heard nothing from either so
  18. far.
  19.  
  20. I'll try to clarify what happens:
  21.  
  22. There are two machines, server and client. Both machines run Solaris.
  23. The client makes an ssh connection to the server to download a file:
  24.  
  25. server% ls -l /tmp/DATA
  26. -rw-r--r--   1 wout  staff     200000 Mar 23 11:20 DATA
  27.  
  28. client% ssh server cat /tmp/DATA > /tmp/DATA
  29. client% ls -l /tmp/DATA
  30. -rw-r--r--   1 wout  staff     194560 Mar 24 17:10 /tmp/DATA
  31.  
  32.  
  33. This would copy a file '/tmp/DATA' from server to /tmp/DATA on client.
  34. In this particular case file DATA was 200000 bytes. The size has
  35. to be larger then the buffers used inside sshd.
  36.  
  37. When the command is run, most data is sent over the line as it should
  38. be. However, when the 'cat' process dies, sshd receives a SIGCHLD and
  39. then fails to read the data left in the pipe to the 'cat' program.
  40.  
  41. To be more precise, sshd only reads the data left in the pipe to 'cat'
  42. if it has space for it in the outgoing buffer (the buffer that is used
  43. to store data going back to the client).
  44.  
  45. So the following happens (all in serverloop.c):
  46.  
  47.  1. For a while sshd reads data from the 'cat' command. This data is
  48.     transmitted to the client, where it is put in /tmp/DATA.
  49.  2. cat writes the final data to the pipe to sshd and exits.
  50.  3. sshd receives a SIGCHLD and sets child_terminated and
  51.     child_just_terminated to 1.
  52.  4. sshd falls out of the select() (line 413) it was in
  53.     (it usually receives the signal during the select() call).
  54.     select() returns -1 because it was interrupted by the signal.
  55.  5. sshd empties readset and writeset (lines 415-422 serverloop.c).
  56.  6. The if statements on lines 426 and 446 fail.
  57.  7. sshd does its usual stuff and then calls
  58.     wait_until_can_do_something().
  59.  8. The call to packet_not_very_much_data_to_write() on line 353
  60.     returns false (because the outgoing buffer contains more than
  61.     16384 bytes). This causes fdout and fderr not to be set in the
  62.     readset file descriptor set (lines 355-358).
  63.  9. select() on line 413 returns 0 again (due to slow network
  64.     connection to client). This time the if statement on line
  65.     426 succeeds (child_just_terminated has been set to 0 earlier).
  66. 10. Descriptor fdout, fderr and fdin are closed (lines 432-442)
  67.     causing the data available to fdout never being read.
  68.  
  69. The change I made to fix this is in a patch (diff on original
  70. serverloop.c and modified serverloop.c) you will find attached
  71. to this mail. It changes lines 432-439. Instead of blindly closing
  72. the fdout and fderr descriptors when select() returns 0, it only
  73. closes them if the fdout_eof and fderr_eof flags have been set,
  74. respectively. The bug was that the code in lines 426-443 assumed
  75. that select() always provides information on fdout and fderr, which
  76. is not the case as they had not been set in the readset.
  77.  
  78. For completeness, I also attach the 'sshd -d' output for a faulty
  79. session (original sshd 1.2.27, data is lost) and output for a session
  80. after having applied my patch.
  81.  
  82. Please let me know what you make of this.
  83.  
  84. Wout van Albada
  85. Software Engineer
  86.  
  87. wout@365.co.uk
  88.  
  89. --liOOAslEiF7prFVr
  90. Content-Type: text/plain; charset=us-ascii
  91. Content-Disposition: attachment; filename=ssh-bug-patch
  92.  
  93. --- serverloop.c.ORIG    Sun Mar 26 13:20:14 2000
  94. +++ serverloop.c    Sun Mar 26 13:25:15 2000
  95. @@ -429,14 +429,14 @@
  96.        if (cleanup_context)
  97.          pty_cleanup_proc(cleanup_context);
  98.        
  99. -      if (fdout != -1)
  100. +      if (fdout != -1 && fdout_eof) {
  101.          close(fdout);
  102. -      fdout = -1;
  103. -      fdout_eof = 1;
  104. -      if (fderr != -1)
  105. +    fdout = -1;
  106. +      }
  107. +      if (fderr != -1 && fderr_eof) {
  108.          close(fderr);
  109. -      fderr = -1;
  110. -      fderr_eof = 1;
  111. +        fderr = -1;
  112. +      }
  113.        if (fdin != -1)
  114.          close(fdin);
  115.        fdin = -1;
  116.  
  117. --liOOAslEiF7prFVr
  118. Content-Type: text/plain; charset=us-ascii
  119. Content-Disposition: attachment; filename=ssh-bug-orig-session
  120.  
  121. Original sshd 1.2.27 session (server):
  122.  
  123. 13:39 [wout@dnp2] /usr/local/sbin 18#./sshd1.orig-2000.03.27 -p 5544 -d
  124. debug: sshd version 1.2.27 [i386-unknown-solaris2.6]
  125. debug: Initializing random number generator; seed file /etc/ssh_random_seed
  126. log: Server listening on port 5544.
  127. log: Generating 768 bit RSA key.
  128. Generating p:  ............++ (distance 180)
  129. Generating q:  ....++ (distance 62)
  130. Computing the keys...
  131. Testing the keys...
  132. Key generation complete.
  133. log: RSA key generation complete.
  134. debug: Server will not fork when running in debugging mode.
  135. log: Connection from 194.42.244.44 port 1022
  136. debug: Client protocol version 1.5; client software version 1.2.27
  137. debug: Sent 768 bit public key and 1024 bit host key.
  138. debug: Encryption type: idea
  139. debug: Received session key; encryption turned on.
  140. debug: Installing crc compensation attack detector.
  141. debug: Attempting authentication for wout.
  142. debug: Trying rhosts with RSA host authentication for wout
  143. debug: RhostsRSA authentication failed for 'wout', remote 'wout', host 'lisa.365.co.uk'.
  144. log: RSA authentication for wout accepted.
  145. debug: Received request for X11 forwarding with auth spoofing.
  146. debug: bind port 6010: Address already in use
  147. debug: Allocated channel 0 of type 1.
  148. debug: Received authentication agent forwarding request.
  149. debug: Allocated channel 1 of type 10.
  150. debug: Executing command 'cat /tmp/DATA'
  151. debug: Entering interactive session.
  152. debug: Received SIGCHLD.
  153. debug: End of interactive session; stdin 0, stdout (read 194560, sent 194560), stderr 567 bytes.
  154. debug: Command exited with status 0.
  155. debug: Received exit confirmation.
  156. log: Closing connection to 194.42.244.44
  157.  
  158.  
  159. Corresponding ssh output (client):
  160.  
  161. 13:39 [wout@lisa] /tmp 32>ssh -v -p 5544 dnp2.football365.co.uk cat /tmp/DATA > /tmp/DATA-orig-sshd
  162. SSH Version 1.2.27 [sparc-sun-solaris2.7], protocol version 1.5.
  163. Standard version.  Does not use RSAREF.
  164. lisa: Reading configuration data /etc/ssh_config
  165. lisa: ssh_connect: getuid 1000 geteuid 0 anon 0
  166. lisa: Connecting to dnp2.football365.co.uk [212.2.1.2] port 5544.
  167. lisa: Allocated local port 1022.
  168. lisa: Connection established.
  169. lisa: Remote protocol version 1.5, remote software version 1.2.27
  170. lisa: Waiting for server public key.
  171. lisa: Received server public key (768 bits) and host key (1024 bits).
  172. lisa: Host 'dnp2.football365.co.uk' is known and matches the host key.
  173. lisa: Initializing random; seed file /home/wout/.ssh/random_seed
  174. lisa: Encryption type: idea
  175. lisa: Sent encrypted session key.
  176. lisa: Installing crc compensation attack detector.
  177. lisa: Received encrypted confirmation.
  178. lisa: Trying rhosts or /etc/hosts.equiv with RSA host authentication.
  179. lisa: Remote: Rhosts/hosts.equiv authentication refused: client user 'wout', server user 'wout', client host 'lisa.365.co.uk'.
  180. lisa: Server refused our rhosts authentication or host key.
  181. lisa: Connection to authentication agent opened.
  182. lisa: Trying RSA authentication via agent with 'wout@borg.365.co.uk'
  183. lisa: Received RSA challenge from server.
  184. lisa: Sending response to RSA challenge.
  185. lisa: Remote: RSA authentication accepted.
  186. lisa: RSA authentication accepted by server.
  187. lisa: Failed to get local xauth data.
  188. lisa: Requesting X11 forwarding with authentication spoofing.
  189. lisa: Requesting authentication agent forwarding.
  190. lisa: Sending command: cat /tmp/DATA
  191. lisa: Entering interactive session.
  192. log: executing remote command as user wout
  193. Environment:
  194.   HOME=/homes/home/wout
  195.   USER=wout
  196.   LOGNAME=wout
  197.   PATH=/bin:/usr/bin:/usr/ucb:/usr/bin/X11:/usr/local/bin:/usr/local/bin
  198.   MAIL=/var/mail/wout
  199.   SHELL=/bin/tcsh
  200.   TZ=GB
  201.   SSH_CLIENT=194.42.244.44 1022 5544
  202.   DISPLAY=dnp2.football365.co.uk:11.0
  203.   SSH_AUTH_SOCK=/tmp/ssh-wout/ssh-17388-agent
  204.  
  205. Running /usr/openwin/bin/xauth add dnp2.football365.co.uk:11.0 MIT-MAGIC-COOKIE-1 213d7f173bd7c5698930191fa6ad6f21
  206. Running /usr/openwin/bin/xauth add 212.2.1.2:11.0 MIT-MAGIC-COOKIE-1 213d7f173bd7c5698930191fa6ad6f21
  207. lisa: Transferred: stdin 0, stdout 195127, stderr 0 bytes in 15.4 seconds
  208. lisa: Bytes per second: stdin 0.0, stdout 12677.5, stderr 0.0
  209. lisa: Exit status 0
  210.  
  211. 13:40 [wout@lisa] /tmp 33>ls -l /tmp/DATA-orig-sshd
  212. -rw-r--r--   1 wout     staff     194560 Mar 27 13:40 /tmp/DATA-orig-sshd
  213.  
  214. 13:41 [wout@lisa] /tmp 34>sum /tmp/DATA-orig-sshd                               11522 380 /tmp/DATA-orig-sshd
  215.  
  216.  
  217. And on server:
  218.  
  219. 13:43 [wout@dnp2] /usr/local/sbin 20#ls -l /tmp/DATA 
  220. -rw-r--r--   1 root     other     200000 Mar 23 11:20 /tmp/DATA
  221.  
  222. 13:43 [wout@dnp2] /usr/local/sbin 21#sum /tmp/DATA 
  223. 57919 391 /tmp/DATA
  224.  
  225.  
  226.  
  227. --liOOAslEiF7prFVr
  228. Content-Type: text/plain; charset=us-ascii
  229. Content-Disposition: attachment; filename=ssh-bug-patched-session
  230.  
  231. Patched sshd 1.2.27 session (server):
  232.  
  233. 13:41 [wout@dnp2] /usr/local/sbin 19#./sshd -p 5544 -d
  234. debug: sshd version 1.2.27 [i386-unknown-solaris2.6]
  235. debug: Initializing random number generator; seed file /etc/ssh_random_seed
  236. log: Server listening on port 5544.
  237. log: Generating 768 bit RSA key.
  238. Generating p:  .............++ (distance 218)
  239. Generating q:  ...........++ (distance 196)
  240. Computing the keys...
  241. Testing the keys...
  242. Key generation complete.
  243. log: RSA key generation complete.
  244. debug: Server will not fork when running in debugging mode.
  245. log: Connection from 194.42.244.44 port 1021
  246. debug: Client protocol version 1.5; client software version 1.2.27
  247. debug: Sent 768 bit public key and 1024 bit host key.
  248. debug: Encryption type: idea
  249. debug: Received session key; encryption turned on.
  250. debug: Installing crc compensation attack detector.
  251. debug: Attempting authentication for wout.
  252. debug: Trying rhosts with RSA host authentication for wout
  253. debug: RhostsRSA authentication failed for 'wout', remote 'wout', host 'lisa.365.co.uk'.
  254. log: RSA authentication for wout accepted.
  255. debug: Received request for X11 forwarding with auth spoofing.
  256. debug: bind port 6010: Address already in use
  257. debug: Allocated channel 0 of type 1.
  258. debug: Received authentication agent forwarding request.
  259. debug: Allocated channel 1 of type 10.
  260. debug: Executing command 'cat /tmp/DATA'
  261. debug: Entering interactive session.
  262. debug: Received SIGCHLD.
  263. debug: End of interactive session; stdin 0, stdout (read 200000, sent 200000), stderr 567 bytes.
  264. debug: Command exited with status 0.
  265. debug: Received exit confirmation.
  266. log: Closing connection to 194.42.244.44
  267.  
  268.  
  269. Corresponding ssh output (client):
  270.  
  271. 13:41 [wout@lisa] /tmp 35>ssh -v -p 5544 dnp2.football365.co.uk cat /tmp/DATA > /tmp/DATA-patched-sshd
  272. SSH Version 1.2.27 [sparc-sun-solaris2.7], protocol version 1.5.
  273. Standard version.  Does not use RSAREF.
  274. lisa: Reading configuration data /etc/ssh_config
  275. lisa: ssh_connect: getuid 1000 geteuid 0 anon 0
  276. lisa: Connecting to dnp2.football365.co.uk [212.2.1.2] port 5544.
  277. lisa: Allocated local port 1021.
  278. lisa: Connection established.
  279. lisa: Remote protocol version 1.5, remote software version 1.2.27
  280. lisa: Waiting for server public key.
  281. lisa: Received server public key (768 bits) and host key (1024 bits).
  282. lisa: Host 'dnp2.football365.co.uk' is known and matches the host key.
  283. lisa: Initializing random; seed file /home/wout/.ssh/random_seed
  284. lisa: Encryption type: idea
  285. lisa: Sent encrypted session key.
  286. lisa: Installing crc compensation attack detector.
  287. lisa: Received encrypted confirmation.
  288. lisa: Trying rhosts or /etc/hosts.equiv with RSA host authentication.
  289. lisa: Remote: Rhosts/hosts.equiv authentication refused: client user 'wout', server user 'wout', client host 'lisa.365.co.uk'.
  290. lisa: Server refused our rhosts authentication or host key.
  291. lisa: Connection to authentication agent opened.
  292. lisa: Trying RSA authentication via agent with 'wout@borg.365.co.uk'
  293. lisa: Received RSA challenge from server.
  294. lisa: Sending response to RSA challenge.
  295. lisa: Remote: RSA authentication accepted.
  296. lisa: RSA authentication accepted by server.
  297. lisa: Failed to get local xauth data.
  298. lisa: Requesting X11 forwarding with authentication spoofing.
  299. lisa: Requesting authentication agent forwarding.
  300. lisa: Sending command: cat /tmp/DATA
  301. lisa: Entering interactive session.
  302. log: executing remote command as user wout
  303. Environment:
  304.   HOME=/homes/home/wout
  305.   USER=wout
  306.   LOGNAME=wout
  307.   PATH=/bin:/usr/bin:/usr/ucb:/usr/bin/X11:/usr/local/bin:/usr/local/bin
  308.   MAIL=/var/mail/wout
  309.   SHELL=/bin/tcsh
  310.   TZ=GB
  311.   SSH_CLIENT=194.42.244.44 1021 5544
  312.   DISPLAY=dnp2.football365.co.uk:11.0
  313.   SSH_AUTH_SOCK=/tmp/ssh-wout/ssh-17462-agent
  314.  
  315. Running /usr/openwin/bin/xauth add dnp2.football365.co.uk:11.0 MIT-MAGIC-COOKIE-1 89baa674c215da2f6c290a8e9442426d
  316. Running /usr/openwin/bin/xauth add 212.2.1.2:11.0 MIT-MAGIC-COOKIE-1 89baa674c215da2f6c290a8e9442426d
  317. lisa: Transferred: stdin 0, stdout 200567, stderr 0 bytes in 14.8 seconds
  318. lisa: Bytes per second: stdin 0.0, stdout 13547.2, stderr 0.0
  319. lisa: Exit status 0
  320.  
  321. 13:42 [wout@lisa] /tmp 36>ls -l /tmp/DATA-patched-sshd 
  322. -rw-r--r--   1 wout     staff     200000 Mar 27 13:42 /tmp/DATA-patched-sshd
  323.  
  324. 13:43 [wout@lisa] /tmp 37>sum /tmp/DATA-patched-sshd 
  325. 57919 391 /tmp/DATA-patched-sshd
  326.  
  327.  
  328. And on server:
  329.  
  330. 13:43 [wout@dnp2] /usr/local/sbin 20#ls -l /tmp/DATA 
  331. -rw-r--r--   1 root     other     200000 Mar 23 11:20 /tmp/DATA
  332.  
  333. 13:43 [wout@dnp2] /usr/local/sbin 21#sum /tmp/DATA 
  334. 57919 391 /tmp/DATA
  335.  
  336.  
  337. --liOOAslEiF7prFVr--
  338.