cherry-pick.FILE5_30-34-g22067c96.simplify-the-property-info-copy-function-and-check-for-bounds.patch 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. Subject: Simplify the property info copy function and check for bounds
  2. Origin: FILE5_30-34-g22067c96
  3. Upstream-Author: Christos Zoulas <christos@zoulas.com>
  4. Date: Wed Mar 29 19:45:22 2017 +0000
  5. Found by oss-fuzz.
  6. --- a/src/cdf.c
  7. +++ b/src/cdf.c
  8. @@ -878,6 +878,34 @@
  9. return NULL;
  10. }
  11. +static int
  12. +cdf_copy_info(cdf_property_info_t *inp, const void *p, const void *e,
  13. + size_t len)
  14. +{
  15. + if (inp->pi_type & CDF_VECTOR)
  16. + return 0;
  17. +
  18. + if ((size_t)(CAST(const char *, e) - CAST(const char *, p)) < len)
  19. + return 0;
  20. +
  21. + (void)memcpy(&inp->pi_val, p, len);
  22. +
  23. + switch (len) {
  24. + case 2:
  25. + inp->pi_u16 = CDF_TOLE2(inp->pi_u16);
  26. + break;
  27. + case 4:
  28. + inp->pi_u32 = CDF_TOLE4(inp->pi_u32);
  29. + break;
  30. + case 8:
  31. + inp->pi_u64 = CDF_TOLE8(inp->pi_u64);
  32. + break;
  33. + default:
  34. + abort();
  35. + }
  36. + return 1;
  37. +}
  38. +
  39. int
  40. cdf_read_property_info(const cdf_stream_t *sst, const cdf_header_t *h,
  41. uint32_t offs, cdf_property_info_t **info, size_t *count, size_t *maxcount)
  42. @@ -885,12 +913,6 @@
  43. const cdf_section_header_t *shp;
  44. cdf_section_header_t sh;
  45. const uint8_t *p, *q, *e;
  46. - int16_t s16;
  47. - int32_t s32;
  48. - uint32_t u32;
  49. - int64_t s64;
  50. - uint64_t u64;
  51. - cdf_timestamp_t tp;
  52. size_t i, o4, nelements, j, slen, left;
  53. cdf_property_info_t *inp;
  54. @@ -953,49 +975,22 @@
  55. case CDF_EMPTY:
  56. break;
  57. case CDF_SIGNED16:
  58. - if (inp[i].pi_type & CDF_VECTOR)
  59. + if (!cdf_copy_info(&inp[i], &q[o4], e, sizeof(int16_t)))
  60. goto unknown;
  61. - (void)memcpy(&s16, &q[o4], sizeof(s16));
  62. - inp[i].pi_s16 = CDF_TOLE2(s16);
  63. break;
  64. case CDF_SIGNED32:
  65. - if (inp[i].pi_type & CDF_VECTOR)
  66. - goto unknown;
  67. - (void)memcpy(&s32, &q[o4], sizeof(s32));
  68. - inp[i].pi_s32 = CDF_TOLE4((uint32_t)s32);
  69. - break;
  70. case CDF_BOOL:
  71. case CDF_UNSIGNED32:
  72. - if (inp[i].pi_type & CDF_VECTOR)
  73. + case CDF_FLOAT:
  74. + if (!cdf_copy_info(&inp[i], &q[o4], e, sizeof(int32_t)))
  75. goto unknown;
  76. - (void)memcpy(&u32, &q[o4], sizeof(u32));
  77. - inp[i].pi_u32 = CDF_TOLE4(u32);
  78. break;
  79. case CDF_SIGNED64:
  80. - if (inp[i].pi_type & CDF_VECTOR)
  81. - goto unknown;
  82. - (void)memcpy(&s64, &q[o4], sizeof(s64));
  83. - inp[i].pi_s64 = CDF_TOLE8((uint64_t)s64);
  84. - break;
  85. case CDF_UNSIGNED64:
  86. - if (inp[i].pi_type & CDF_VECTOR)
  87. - goto unknown;
  88. - (void)memcpy(&u64, &q[o4], sizeof(u64));
  89. - inp[i].pi_u64 = CDF_TOLE8((uint64_t)u64);
  90. - break;
  91. - case CDF_FLOAT:
  92. - if (inp[i].pi_type & CDF_VECTOR)
  93. - goto unknown;
  94. - (void)memcpy(&u32, &q[o4], sizeof(u32));
  95. - u32 = CDF_TOLE4(u32);
  96. - memcpy(&inp[i].pi_f, &u32, sizeof(inp[i].pi_f));
  97. - break;
  98. case CDF_DOUBLE:
  99. - if (inp[i].pi_type & CDF_VECTOR)
  100. + case CDF_FILETIME:
  101. + if (!cdf_copy_info(&inp[i], &q[o4], e, sizeof(int64_t)))
  102. goto unknown;
  103. - (void)memcpy(&u64, &q[o4], sizeof(u64));
  104. - u64 = CDF_TOLE8((uint64_t)u64);
  105. - memcpy(&inp[i].pi_d, &u64, sizeof(inp[i].pi_d));
  106. break;
  107. case CDF_LENGTH32_STRING:
  108. case CDF_LENGTH32_WSTRING:
  109. @@ -1038,12 +1033,6 @@
  110. }
  111. i--;
  112. break;
  113. - case CDF_FILETIME:
  114. - if (inp[i].pi_type & CDF_VECTOR)
  115. - goto unknown;
  116. - (void)memcpy(&tp, &q[o4], sizeof(tp));
  117. - inp[i].pi_tp = CDF_TOLE8((uint64_t)tp);
  118. - break;
  119. case CDF_CLIPBOARD:
  120. if (inp[i].pi_type & CDF_VECTOR)
  121. goto unknown;