Most efficient replacement for IsBadReadPtr?

前端 未结 10 1262
你的背包
你的背包 2020-12-06 06:45

I have some Visual C++ code that receives a pointer to a buffer with data that needs to be processed by my code and the length of that buffer. Due to a bug outside my contro

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 07:20

    If you have to resort to checking patterns in data, here are a few tips:

    • If you mention using IsBadReadPtr, you are probably developing for Windows x86 or x64.

    • You may be able to range check the pointer. Pointers to objects will be word aligned. In 32-bit windows, user-space pointers are in the range of 0x00401000-0x7FFFFFFF, or for large-address-aware applications, 0x00401000-0xBFFFFFFF instead (edit: 0x00401000-0xFFFF0000 for a 32-bit program on 64-bit windows). The upper 2GB/1GB is reserved for kernel-space pointers.

    • The object itself will live in Read/Write memory which is not executable. It may live in the heap, or it may be a global variable. If it is a global variable, you can validate that it lives in the correct module.

    • If your object has a VTable, and you are not using other classes, compare its VTable pointer with another VTable pointer from a known good object.

    • Range check the variables to see if they are possibly valid. For example, bools can only be 1 or 0, so if you see one with a value of 242, that's obviously wrong. Pointers can also be range checked and checked for alignment as well.

    • If there are objects contained within, check their VTables and data as well.

    • If there are pointers to other objects, you can check that the object lives in memory that is Read/Write and not executable, check the VTable if applicable, and range check the data as well.

    If you do not have a good object with a known VTable address, you can use these rules to check if a VTable is valid:

    • While the object lives in Read/Write memory, and the VTable pointer is part of the object, the VTable itself will live in memory that is Read Only and not executable, and will be aligned to a word boundary. It will also belong to the module.
    • The entries of the VTable are pointers to code, which will be Read Only and Executable, and not writable. There is no alignment restrictions for code addresses. Code will belong to the module.

提交回复
热议问题