Advertisement
moften

Qualys Security Advisory - LibreSSL (CVE-2015-5333 and CVE-2

Oct 15th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. Qualys Security Advisory
  2.  
  3. LibreSSL (CVE-2015-5333 and CVE-2015-5334)
  4.  
  5.  
  6. ========================================================================
  7. Contents
  8. ========================================================================
  9.  
  10. Summary
  11. Memory Leak (CVE-2015-5333)
  12. Buffer Overflow (CVE-2015-5334)
  13. Acknowledgments
  14.  
  15.  
  16. ========================================================================
  17. Summary
  18. ========================================================================
  19.  
  20. In order to achieve remote code execution against the vulnerabilities
  21. that we recently discovered in OpenSMTPD (CVE-2015-7687), a memory leak
  22. is needed. Because we could not find one in OpenSMTPD itself, we started
  23. to review the malloc()s and free()s of its libraries, and eventually
  24. found a memory leak in LibreSSL's OBJ_obj2txt() function; we then
  25. realized that this function also contains a buffer overflow (an
  26. off-by-one, usually stack-based).
  27.  
  28. The vulnerable function OBJ_obj2txt() is reachable through
  29. X509_NAME_oneline() and d2i_X509(), which is called automatically to
  30. decode the X.509 certificates exchanged during an SSL handshake (both
  31. client-side, unless an anonymous mode is used, and server-side, if
  32. client authentication is requested).
  33.  
  34. These vulnerabilities affect all LibreSSL versions, including LibreSSL
  35. 2.0.0 (the first public release) and LibreSSL 2.3.0 (the latest release
  36. at the time of writing). OpenSSL is not affected.
  37.  
  38.  
  39. ========================================================================
  40. Memory Leak (CVE-2015-5333)
  41. ========================================================================
  42.  
  43. OBJ_obj2txt() converts an ASN.1 object identifier (the ASN1_OBJECT a)
  44. into a null-terminated string of numerical subidentifiers separated by
  45. dots (at most buf_len bytes are written to buf).
  46.  
  47. Large subidentifiers are temporarily stored in a BIGNUM (bl) and
  48. converted by BN_bn2dec() into a printable string of decimal characters
  49. (bndec). Many such bndec strings can be malloc()ated and memory-leaked
  50. in a loop, because only the last one will be free()d, after the end of
  51. the loop:
  52.  
  53. 489 int
  54. 490 OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
  55. 491 {
  56. ...
  57. 494 char *bndec = NULL;
  58. ...
  59. 516 len = a->length;
  60. ...
  61. 519 while (len > 0) {
  62. ...
  63. 570 bndec = BN_bn2dec(bl);
  64. 571 if (!bndec)
  65. 572 goto err;
  66. 573 i = snprintf(buf, buf_len, ".%s", bndec);
  67. ...
  68. 598 }
  69. ...
  70. 601 free(bndec);
  71. ...
  72. 609 }
  73.  
  74. This memory leak allows remote attackers to cause a denial of service
  75. (memory exhaustion) or trigger the buffer overflow described below.
  76.  
  77.  
  78. ========================================================================
  79. Buffer Overflow (CVE-2015-5334)
  80. ========================================================================
  81.  
  82. As a result of CVE-2014-3508, OBJ_obj2txt() was modified to "Ensure
  83. that, at every state, |buf| is NUL-terminated." However, in LibreSSL,
  84. the error-handling code at the end of the function may write this
  85. null-terminator out-of-bounds:
  86.  
  87. 489 int
  88. 490 OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
  89. 491 {
  90. ...
  91. 516 len = a->length;
  92. 517 p = a->data;
  93. 518
  94. 519 while (len > 0) {
  95. ...
  96. 522 for (;;) {
  97. 523 unsigned char c = *p++;
  98. 524 len--;
  99. 525 if ((len == 0) && (c & 0x80))
  100. 526 goto err;
  101. ...
  102. 528 if (!BN_add_word(bl, c & 0x7f))
  103. 529 goto err;
  104. ...
  105. 535 if (!bl && !(bl = BN_new()))
  106. 536 goto err;
  107. 537 if (!BN_set_word(bl, l))
  108. 538 goto err;
  109. ...
  110. 542 if (!BN_lshift(bl, bl, 7))
  111. 543 goto err;
  112. ...
  113. 546 }
  114. ...
  115. 553 if (!BN_sub_word(bl, 80))
  116. 554 goto err;
  117. ...
  118. 561 if (buf_len > 1) {
  119. 562 *buf++ = i + '0';
  120. 563 *buf = '\0';
  121. 564 buf_len--;
  122. 565 }
  123. ...
  124. 569 if (use_bn) {
  125. 570 bndec = BN_bn2dec(bl);
  126. 571 if (!bndec)
  127. 572 goto err;
  128. 573 i = snprintf(buf, buf_len, ".%s", bndec);
  129. 574 if (i == -1)
  130. 575 goto err;
  131. 576 if (i >= buf_len) {
  132. 577 buf += buf_len;
  133. 578 buf_len = 0;
  134. 579 } else {
  135. 580 buf += i;
  136. 581 buf_len -= i;
  137. 582 }
  138. ...
  139. 584 } else {
  140. 585 i = snprintf(buf, buf_len, ".%lu", l);
  141. 586 if (i == -1)
  142. 587 goto err;
  143. 588 if (i >= buf_len) {
  144. 589 buf += buf_len;
  145. 590 buf_len = 0;
  146. 591 } else {
  147. 592 buf += i;
  148. 593 buf_len -= i;
  149. 594 }
  150. ...
  151. 597 }
  152. 598 }
  153. 599
  154. 600 out:
  155. ...
  156. 603 return ret;
  157. 604
  158. 605 err:
  159. 606 ret = 0;
  160. 607 buf[0] = '\0';
  161. 608 goto out;
  162. 609 }
  163.  
  164. First, in order to trigger this off-by-one buffer overflow, buf must be
  165. increased until it points to the first out-of-bounds character (i.e.,
  166. until buf_len becomes zero):
  167.  
  168. - on the one hand, this is impossible with the code blocks at lines
  169. 561-564, 579-581, and 591-593;
  170.  
  171. - on the other hand, this is very easy with the code blocks at lines
  172. 576-578 and 588-590 (the destination buffer is usually quite small;
  173. for example, it is only 80 bytes long in X509_NAME_oneline()).
  174.  
  175. Second, the code must branch to the err label:
  176.  
  177. - the "goto err"s at lines 574-575 and 586-587 are unreachable, because
  178. snprintf() cannot possibly return -1 here;
  179.  
  180. - the "goto err" at lines 525-526 is:
  181.  
  182. . very easy to reach in LibreSSL <= 2.0.4;
  183.  
  184. . impossible to reach in LibreSSL >= 2.0.5, because of the "MSB must
  185. be clear in the last octet" sanity check that was added to
  186. c2i_ASN1_OBJECT():
  187.  
  188. 286 /*
  189. 287 * Sanity check OID encoding:
  190. 288 * - need at least one content octet
  191. 289 * - MSB must be clear in the last octet
  192. 290 * - can't have leading 0x80 in subidentifiers, see: X.690 8.19.2
  193. 291 */
  194. 292 if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
  195. 293 p[len - 1] & 0x80) {
  196. 294 ASN1err(ASN1_F_C2I_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
  197. 295 return (NULL);
  198. 296 }
  199.  
  200. - the remaining "goto err"s are triggered by error conditions in various
  201. BIGNUM functions:
  202.  
  203. . either because of a very large BIGNUM (approximately 64 megabytes,
  204. which is impossible in the context of an SSL handshake, where X.509
  205. certificates are limited to 100 kilobytes);
  206.  
  207. . or because of an out-of-memory condition (which can be reached
  208. through the memory leak described above).
  209.  
  210. This off-by-one buffer overflow allows remote attackers to cause a
  211. denial of service (crash) or possibly execute arbitrary code. However,
  212. when triggered through X509_NAME_oneline() (and therefore d2i_X509()),
  213. this buffer overflow is stack-based and probably not exploitable on
  214. OpenBSD x86, where it appears to always smash the stack canary.
  215.  
  216.  
  217. ========================================================================
  218. Acknowledgments
  219. ========================================================================
  220.  
  221. We would like to thank the LibreSSL team for their great work and their
  222. incredibly quick response, and Red Hat Product Security for promptly
  223. assigning CVE-IDs to these issues.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement