0004-Improve-deep-recursion-detection-in-exif_data_load_d.patch 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. From 6aa11df549114ebda520dde4cdaea2f9357b2c89 Mon Sep 17 00:00:00 2001
  2. From: Dan Fandrich <dan@coneharvesters.com>
  3. Date: Fri, 12 Oct 2018 16:01:45 +0200
  4. Subject: [PATCH] Improve deep recursion detection in
  5. exif_data_load_data_content.
  6. The existing detection was still vulnerable to pathological cases
  7. causing DoS by wasting CPU. The new algorithm takes the number of tags
  8. into account to make it harder to abuse by cases using shallow recursion
  9. but with a very large number of tags. This improves on commit 5d28011c
  10. which wasn't sufficient to counter this kind of case.
  11. The limitation in the previous fix was discovered by Laurent Delosieres,
  12. Secunia Research at Flexera (Secunia Advisory SA84652) and is assigned
  13. the identifier CVE-2018-20030.
  14. [Peter: drop NEWS change]
  15. Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
  16. ---
  17. libexif/exif-data.c | 45 +++++++++++++++++++++++++++++++++++++--------
  18. 1 file changed, 37 insertions(+), 8 deletions(-)
  19. diff --git a/libexif/exif-data.c b/libexif/exif-data.c
  20. index e35403d..a6f9c94 100644
  21. --- a/libexif/exif-data.c
  22. +++ b/libexif/exif-data.c
  23. @@ -35,6 +35,7 @@
  24. #include <libexif/olympus/exif-mnote-data-olympus.h>
  25. #include <libexif/pentax/exif-mnote-data-pentax.h>
  26. +#include <math.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. @@ -350,6 +351,20 @@ if (data->ifd[(i)]->count) { \
  31. break; \
  32. }
  33. +/*! Calculate the recursion cost added by one level of IFD loading.
  34. + *
  35. + * The work performed is related to the cost in the exponential relation
  36. + * work=1.1**cost
  37. + */
  38. +static unsigned int
  39. +level_cost(unsigned int n)
  40. +{
  41. + static const double log_1_1 = 0.09531017980432493;
  42. +
  43. + /* Adding 0.1 protects against the case where n==1 */
  44. + return ceil(log(n + 0.1)/log_1_1);
  45. +}
  46. +
  47. /*! Load data for an IFD.
  48. *
  49. * \param[in,out] data #ExifData
  50. @@ -357,13 +372,13 @@ if (data->ifd[(i)]->count) { \
  51. * \param[in] d pointer to buffer containing raw IFD data
  52. * \param[in] ds size of raw data in buffer at \c d
  53. * \param[in] offset offset into buffer at \c d at which IFD starts
  54. - * \param[in] recursion_depth number of times this function has been
  55. - * recursively called without returning
  56. + * \param[in] recursion_cost factor indicating how expensive this recursive
  57. + * call could be
  58. */
  59. static void
  60. exif_data_load_data_content (ExifData *data, ExifIfd ifd,
  61. const unsigned char *d,
  62. - unsigned int ds, unsigned int offset, unsigned int recursion_depth)
  63. + unsigned int ds, unsigned int offset, unsigned int recursion_cost)
  64. {
  65. ExifLong o, thumbnail_offset = 0, thumbnail_length = 0;
  66. ExifShort n;
  67. @@ -378,9 +393,20 @@ exif_data_load_data_content (ExifData *data, ExifIfd ifd,
  68. if ((((int)ifd) < 0) || ( ((int)ifd) >= EXIF_IFD_COUNT))
  69. return;
  70. - if (recursion_depth > 12) {
  71. + if (recursion_cost > 170) {
  72. + /*
  73. + * recursion_cost is a logarithmic-scale indicator of how expensive this
  74. + * recursive call might end up being. It is an indicator of the depth of
  75. + * recursion as well as the potential for worst-case future recursive
  76. + * calls. Since it's difficult to tell ahead of time how often recursion
  77. + * will occur, this assumes the worst by assuming every tag could end up
  78. + * causing recursion.
  79. + * The value of 170 was chosen to limit typical EXIF structures to a
  80. + * recursive depth of about 6, but pathological ones (those with very
  81. + * many tags) to only 2.
  82. + */
  83. exif_log (data->priv->log, EXIF_LOG_CODE_CORRUPT_DATA, "ExifData",
  84. - "Deep recursion detected!");
  85. + "Deep/expensive recursion detected!");
  86. return;
  87. }
  88. @@ -422,15 +448,18 @@ exif_data_load_data_content (ExifData *data, ExifIfd ifd,
  89. switch (tag) {
  90. case EXIF_TAG_EXIF_IFD_POINTER:
  91. CHECK_REC (EXIF_IFD_EXIF);
  92. - exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o, recursion_depth + 1);
  93. + exif_data_load_data_content (data, EXIF_IFD_EXIF, d, ds, o,
  94. + recursion_cost + level_cost(n));
  95. break;
  96. case EXIF_TAG_GPS_INFO_IFD_POINTER:
  97. CHECK_REC (EXIF_IFD_GPS);
  98. - exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o, recursion_depth + 1);
  99. + exif_data_load_data_content (data, EXIF_IFD_GPS, d, ds, o,
  100. + recursion_cost + level_cost(n));
  101. break;
  102. case EXIF_TAG_INTEROPERABILITY_IFD_POINTER:
  103. CHECK_REC (EXIF_IFD_INTEROPERABILITY);
  104. - exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o, recursion_depth + 1);
  105. + exif_data_load_data_content (data, EXIF_IFD_INTEROPERABILITY, d, ds, o,
  106. + recursion_cost + level_cost(n));
  107. break;
  108. case EXIF_TAG_JPEG_INTERCHANGE_FORMAT:
  109. thumbnail_offset = o;
  110. --
  111. 2.20.1