checkbmi.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 1992 - 1997 Microsoft Corporation. All Rights Reserved.
  2. #ifndef _CHECKBMI_H_
  3. #define _CHECKBMI_H_
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. // Helper
  8. __inline BOOL MultiplyCheckOverflow(DWORD a, DWORD b, __deref_out_range(==, a * b) DWORD *pab)
  9. {
  10. *pab = a * b;
  11. if ((a == 0) || (((*pab) / a) == b)) {
  12. return TRUE;
  13. }
  14. return FALSE;
  15. }
  16. // Checks if the fields in a BITMAPINFOHEADER won't generate
  17. // overlows and buffer overruns
  18. // This is not a complete check and does not guarantee code using this structure will be secure
  19. // from attack
  20. // Bugs this is guarding against:
  21. // 1. Total structure size calculation overflowing
  22. // 2. biClrUsed > 256 for 8-bit palettized content
  23. // 3. Total bitmap size in bytes overflowing
  24. // 4. biSize < size of the base structure leading to accessessing random memory
  25. // 5. Total structure size exceeding know size of data
  26. //
  27. __success(return != 0) __inline BOOL ValidateBitmapInfoHeader(
  28. const BITMAPINFOHEADER *pbmi, // pointer to structure to check
  29. __out_range(>=, sizeof(BITMAPINFOHEADER)) DWORD cbSize // size of memory block containing structure
  30. )
  31. {
  32. DWORD dwWidthInBytes;
  33. DWORD dwBpp;
  34. DWORD dwWidthInBits;
  35. DWORD dwHeight;
  36. DWORD dwSizeImage;
  37. DWORD dwClrUsed;
  38. // Reject bad parameters - do the size check first to avoid reading bad memory
  39. if (cbSize < sizeof(BITMAPINFOHEADER) ||
  40. pbmi->biSize < sizeof(BITMAPINFOHEADER) ||
  41. pbmi->biSize > 4096) {
  42. return FALSE;
  43. }
  44. // Reject 0 size
  45. if (pbmi->biWidth == 0 || pbmi->biHeight == 0) {
  46. return FALSE;
  47. }
  48. // Use bpp of 200 for validating against further overflows if not set for compressed format
  49. dwBpp = 200;
  50. if (pbmi->biBitCount > dwBpp) {
  51. return FALSE;
  52. }
  53. // Strictly speaking abs can overflow so cast explicitly to DWORD
  54. dwHeight = (DWORD)abs(pbmi->biHeight);
  55. if (!MultiplyCheckOverflow(dwBpp, (DWORD)pbmi->biWidth, &dwWidthInBits)) {
  56. return FALSE;
  57. }
  58. // Compute correct width in bytes - rounding up to 4 bytes
  59. dwWidthInBytes = (dwWidthInBits / 8 + 3) & ~3;
  60. if (!MultiplyCheckOverflow(dwWidthInBytes, dwHeight, &dwSizeImage)) {
  61. return FALSE;
  62. }
  63. // Fail if total size is 0 - this catches indivual quantities being 0
  64. // Also don't allow huge values > 1GB which might cause arithmetic
  65. // errors for users
  66. if (dwSizeImage > 0x40000000 ||
  67. pbmi->biSizeImage > 0x40000000) {
  68. return FALSE;
  69. }
  70. // Fail if biClrUsed looks bad
  71. if (pbmi->biClrUsed > 256) {
  72. return FALSE;
  73. }
  74. if (pbmi->biClrUsed == 0 && pbmi->biBitCount <= 8 && pbmi->biBitCount > 0) {
  75. dwClrUsed = (1 << pbmi->biBitCount);
  76. }
  77. else {
  78. dwClrUsed = pbmi->biClrUsed;
  79. }
  80. // Check total size
  81. if (cbSize < pbmi->biSize + dwClrUsed * sizeof(RGBQUAD) +
  82. (pbmi->biCompression == BI_BITFIELDS ? 3 * sizeof(DWORD) : 0)) {
  83. return FALSE;
  84. }
  85. // If it is RGB validate biSizeImage - lots of code assumes the size is correct
  86. if (pbmi->biCompression == BI_RGB || pbmi->biCompression == BI_BITFIELDS) {
  87. if (pbmi->biSizeImage != 0) {
  88. DWORD dwBits = (DWORD)pbmi->biWidth * (DWORD)pbmi->biBitCount;
  89. DWORD dwWidthInBytes = ((DWORD)((dwBits+31) & (~31)) / 8);
  90. DWORD dwTotalSize = (DWORD)abs(pbmi->biHeight) * dwWidthInBytes;
  91. if (dwTotalSize > pbmi->biSizeImage) {
  92. return FALSE;
  93. }
  94. }
  95. }
  96. return TRUE;
  97. }
  98. #ifdef __cplusplus
  99. }
  100. #endif
  101. #endif // _CHECKBMI_H_