1
0

checkbmi.h 3.6 KB

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