cherry-pick.FILE5_30-49-gbf90083a.fix-memory-handling.patch 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. Subject: [ Fix memory handling ]
  2. Origin: FILE5_30-49-gbf90083a
  3. Upstream-Author: Christos Zoulas <christos@zoulas.com>
  4. Date: Mon Apr 24 18:57:35 2017 +0000
  5. - centralize allocation so we can easily find out where things are done
  6. - limit property list memory limit further for oss-fuzz.
  7. --- a/src/cdf.c
  8. +++ b/src/cdf.c
  9. @@ -80,6 +80,28 @@
  10. CDF_TOLE8(CAST(uint64_t, x))))
  11. #define CDF_GETUINT32(x, y) cdf_getuint32(x, y)
  12. +#define CDF_MALLOC(n) cdf_malloc(__FILE__, __LINE__, (n))
  13. +#define CDF_REALLOC(p, n) cdf_realloc(__FILE__, __LINE__, (p), (n))
  14. +#define CDF_CALLOC(n, u) cdf_calloc(__FILE__, __LINE__, (n), (u))
  15. +
  16. +
  17. +static void *
  18. +cdf_malloc(const char *file, size_t line, size_t n) {
  19. + DPRINTF(("%s,%zu: %s %zu\n", file, line, __func__, n));
  20. + return malloc(n);
  21. +}
  22. +
  23. +static void *
  24. +cdf_realloc(const char *file, size_t line, void *p, size_t n) {
  25. + DPRINTF(("%s,%zu: %s %zu\n", file, line, __func__, n));
  26. + return realloc(p, n);
  27. +}
  28. +
  29. +static void *
  30. +cdf_calloc(const char *file, size_t line, size_t n, size_t u) {
  31. + DPRINTF(("%s,%zu: %s %zu %zu\n", file, line, __func__, n, u));
  32. + return calloc(n, u);
  33. +}
  34. /*
  35. * swap a short
  36. @@ -421,7 +443,7 @@
  37. sat->sat_len = h->h_num_sectors_in_master_sat * nsatpersec + i;
  38. DPRINTF(("sat_len = %" SIZE_T_FORMAT "u ss = %" SIZE_T_FORMAT "u\n",
  39. sat->sat_len, ss));
  40. - if ((sat->sat_tab = CAST(cdf_secid_t *, calloc(sat->sat_len, ss)))
  41. + if ((sat->sat_tab = CAST(cdf_secid_t *, CDF_CALLOC(sat->sat_len, ss)))
  42. == NULL)
  43. return -1;
  44. @@ -435,7 +457,7 @@
  45. }
  46. }
  47. - if ((msa = CAST(cdf_secid_t *, calloc(1, ss))) == NULL)
  48. + if ((msa = CAST(cdf_secid_t *, CDF_CALLOC(1, ss))) == NULL)
  49. goto out1;
  50. mid = h->h_secid_first_sector_in_master_sat;
  51. @@ -536,7 +558,7 @@
  52. if (scn->sst_len == (size_t)-1)
  53. goto out;
  54. - scn->sst_tab = calloc(scn->sst_len, ss);
  55. + scn->sst_tab = CDF_CALLOC(scn->sst_len, ss);
  56. if (scn->sst_tab == NULL)
  57. return cdf_zero_stream(scn);
  58. @@ -582,7 +604,7 @@
  59. if (scn->sst_len == (size_t)-1)
  60. goto out;
  61. - scn->sst_tab = calloc(scn->sst_len, ss);
  62. + scn->sst_tab = CDF_CALLOC(scn->sst_len, ss);
  63. if (scn->sst_tab == NULL)
  64. return cdf_zero_stream(scn);
  65. @@ -640,11 +662,11 @@
  66. dir->dir_len = ns * nd;
  67. dir->dir_tab = CAST(cdf_directory_t *,
  68. - calloc(dir->dir_len, sizeof(dir->dir_tab[0])));
  69. + CDF_CALLOC(dir->dir_len, sizeof(dir->dir_tab[0])));
  70. if (dir->dir_tab == NULL)
  71. return -1;
  72. - if ((buf = CAST(char *, malloc(ss))) == NULL) {
  73. + if ((buf = CAST(char *, CDF_MALLOC(ss))) == NULL) {
  74. free(dir->dir_tab);
  75. return -1;
  76. }
  77. @@ -690,7 +712,7 @@
  78. if (ssat->sat_len == (size_t)-1)
  79. goto out;
  80. - ssat->sat_tab = CAST(cdf_secid_t *, calloc(ssat->sat_len, ss));
  81. + ssat->sat_tab = CAST(cdf_secid_t *, CDF_CALLOC(ssat->sat_len, ss));
  82. if (ssat->sat_tab == NULL)
  83. goto out1;
  84. @@ -819,7 +841,7 @@
  85. }
  86. #define CDF_SHLEN_LIMIT (UINT32_MAX / 8)
  87. -#define CDF_PROP_LIMIT (UINT32_MAX / (4 * sizeof(cdf_property_info_t)))
  88. +#define CDF_PROP_LIMIT (UINT32_MAX / (8 * sizeof(cdf_property_info_t)))
  89. static const void *
  90. cdf_offset(const void *p, size_t l)
  91. @@ -864,11 +886,13 @@
  92. cdf_property_info_t *inp;
  93. size_t newcount = *maxcount + incr;
  94. - if (newcount > CDF_PROP_LIMIT)
  95. + if (newcount > CDF_PROP_LIMIT) {
  96. + DPRINTF(("exceeded property limit %zu > %zu\n",
  97. + newcount, CDF_PROP_LIMIT));
  98. goto out;
  99. -
  100. + }
  101. inp = CAST(cdf_property_info_t *,
  102. - realloc(*info, newcount * sizeof(*inp)));
  103. + CDF_REALLOC(*info, newcount * sizeof(*inp)));
  104. if (inp == NULL)
  105. goto out;
  106. @@ -938,10 +962,10 @@
  107. goto out;
  108. sh.sh_properties = CDF_TOLE4(shp->sh_properties);
  109. - if (sh.sh_properties > CDF_PROP_LIMIT)
  110. - goto out;
  111. DPRINTF(("section len: %u properties %u\n", sh.sh_len,
  112. sh.sh_properties));
  113. + if (sh.sh_properties > CDF_PROP_LIMIT)
  114. + goto out;
  115. inp = cdf_grow_info(info, maxcount, sh.sh_properties);
  116. if (inp == NULL)
  117. goto out;
  118. @@ -1126,7 +1150,7 @@
  119. return -1;
  120. nr--;
  121. *cat = CAST(cdf_catalog_t *,
  122. - malloc(sizeof(cdf_catalog_t) + nr * sizeof(*ce)));
  123. + CDF_MALLOC(sizeof(cdf_catalog_t) + nr * sizeof(*ce)));
  124. if (*cat == NULL)
  125. return -1;
  126. ce = (*cat)->cat_e;