0021-Check-for-integer-overflow-in-xsltAddTextString.patch 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. From 08ab2774b870de1c7b5a48693df75e8154addae5 Mon Sep 17 00:00:00 2001
  2. From: Nick Wellnhofer <wellnhofer@aevum.de>
  3. Date: Thu, 12 Jan 2017 15:39:52 +0100
  4. Subject: [PATCH] Check for integer overflow in xsltAddTextString
  5. Limit buffer size in xsltAddTextString to INT_MAX. The issue can be
  6. exploited to trigger an out of bounds write on 64-bit systems.
  7. Originally reported to Chromium:
  8. https://crbug.com/676623
  9. ---
  10. libxslt/transform.c | 25 ++++++++++++++++++++++---
  11. libxslt/xsltInternals.h | 4 ++--
  12. 2 files changed, 24 insertions(+), 5 deletions(-)
  13. --- a/libxslt/transform.c
  14. +++ b/libxslt/transform.c
  15. @@ -718,13 +718,32 @@ xsltAddTextString(xsltTransformContextPt
  16. return(target);
  17. if (ctxt->lasttext == target->content) {
  18. + int minSize;
  19. - if (ctxt->lasttuse + len >= ctxt->lasttsize) {
  20. + /* Check for integer overflow accounting for NUL terminator. */
  21. + if (len >= INT_MAX - ctxt->lasttuse) {
  22. + xsltTransformError(ctxt, NULL, target,
  23. + "xsltCopyText: text allocation failed\n");
  24. + return(NULL);
  25. + }
  26. + minSize = ctxt->lasttuse + len + 1;
  27. +
  28. + if (ctxt->lasttsize < minSize) {
  29. xmlChar *newbuf;
  30. int size;
  31. + int extra;
  32. +
  33. + /* Double buffer size but increase by at least 100 bytes. */
  34. + extra = minSize < 100 ? 100 : minSize;
  35. +
  36. + /* Check for integer overflow. */
  37. + if (extra > INT_MAX - ctxt->lasttsize) {
  38. + size = INT_MAX;
  39. + }
  40. + else {
  41. + size = ctxt->lasttsize + extra;
  42. + }
  43. - size = ctxt->lasttsize + len + 100;
  44. - size *= 2;
  45. newbuf = (xmlChar *) xmlRealloc(target->content,size);
  46. if (newbuf == NULL) {
  47. xsltTransformError(ctxt, NULL, target,
  48. --- a/libxslt/xsltInternals.h
  49. +++ b/libxslt/xsltInternals.h
  50. @@ -1752,8 +1752,8 @@ struct _xsltTransformContext {
  51. * Speed optimization when coalescing text nodes
  52. */
  53. const xmlChar *lasttext; /* last text node content */
  54. - unsigned int lasttsize; /* last text node size */
  55. - unsigned int lasttuse; /* last text node use */
  56. + int lasttsize; /* last text node size */
  57. + int lasttuse; /* last text node use */
  58. /*
  59. * Per Context Debugging
  60. */