107-CVE-2017-8816.patch 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. From 7947c50bcd09cf471c95511739bc66d2cb506ee2 Mon Sep 17 00:00:00 2001
  2. From: Daniel Stenberg <daniel@haxx.se>
  3. Date: Mon, 6 Nov 2017 23:51:52 +0100
  4. Subject: [PATCH] ntlm: avoid integer overflow for malloc size
  5. Reported-by: Alex Nichols
  6. Assisted-by: Kamil Dudka and Max Dymond
  7. CVE-2017-8816
  8. Bug: https://curl.haxx.se/docs/adv_2017-11e7.html
  9. ---
  10. lib/curl_ntlm_core.c | 23 +++++++++++++++++++++--
  11. 1 file changed, 21 insertions(+), 2 deletions(-)
  12. --- a/lib/curl_ntlm_core.c
  13. +++ b/lib/curl_ntlm_core.c
  14. @@ -618,6 +618,15 @@ CURLcode Curl_hmac_md5(const unsigned ch
  15. return CURLE_OK;
  16. }
  17. +#ifndef SIZE_T_MAX
  18. +/* some limits.h headers have this defined, some don't */
  19. +#if defined(_LP64) || defined(_I32LPx)
  20. +#define SIZE_T_MAX 18446744073709551615U
  21. +#else
  22. +#define SIZE_T_MAX 4294967295U
  23. +#endif
  24. +#endif
  25. +
  26. /* This creates the NTLMv2 hash by using NTLM hash as the key and Unicode
  27. * (uppercase UserName + Domain) as the data
  28. */
  29. @@ -627,10 +636,20 @@ CURLcode Curl_ntlm_core_mk_ntlmv2_hash(c
  30. unsigned char *ntlmv2hash)
  31. {
  32. /* Unicode representation */
  33. - size_t identity_len = (userlen + domlen) * 2;
  34. - unsigned char *identity = malloc(identity_len);
  35. + size_t identity_len;
  36. + unsigned char *identity;
  37. CURLcode result = CURLE_OK;
  38. + /* we do the length checks below separately to avoid integer overflow risk
  39. + on extreme data lengths */
  40. + if((userlen > SIZE_T_MAX/2) ||
  41. + (domlen > SIZE_T_MAX/2) ||
  42. + ((userlen + domlen) > SIZE_T_MAX/2))
  43. + return CURLE_OUT_OF_MEMORY;
  44. +
  45. + identity_len = (userlen + domlen) * 2;
  46. + identity = malloc(identity_len);
  47. +
  48. if(!identity)
  49. return CURLE_OUT_OF_MEMORY;