- Timestamp:
- Jul 14, 2014 7:44:01 PM (11 years ago)
- Location:
- trunk/src/VBox/Runtime
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/math/bignum.cpp
r51915 r52018 118 118 Assert(cbNew > cbOld); 119 119 120 void *pvNew ;120 void *pvNew = NULL; 121 121 if (pBigNum->fSensitive) 122 pvNew = RTMemSaferReallocZ(cbOld, pBigNum->pauElements, cbNew); 122 { 123 int rc = RTMemSaferReallocZEx(cbOld, pBigNum->pauElements, cbNew, &pvNew, RTMEMSAFER_ALLOC_EX_ALLOW_PAGEABLE_BACKING); 124 Assert(VALID_PTR(pvNew) || RT_FAILURE(rc)); 125 } 123 126 else 124 127 pvNew = RTMemRealloc(pBigNum->pauElements, cbNew); … … 323 326 pBigNum->cAllocated = RT_ALIGN_32(pBigNum->cUsed, 4); 324 327 if (pBigNum->fSensitive) 325 pBigNum->pauElements = (RTBIGNUMELEMENT *)RTMemSaferAllocZ(pBigNum->cAllocated * RTBIGNUM_ELEMENT_SIZE); 328 { 329 int rc = RTMemSaferAllocZEx((void **)&pBigNum->pauElements, pBigNum->cAllocated * RTBIGNUM_ELEMENT_SIZE, 330 RTMEMSAFER_ALLOC_EX_ALLOW_PAGEABLE_BACKING); 331 Assert(VALID_PTR(pBigNum->pauElements) || RT_FAILURE(rc)); 332 } 326 333 else 327 334 pBigNum->pauElements = (RTBIGNUMELEMENT *)RTMemAlloc(pBigNum->cAllocated * RTBIGNUM_ELEMENT_SIZE); … … 457 464 pBigNum->cAllocated = RT_ALIGN_32(pBigNum->cUsed, 4); 458 465 if (pBigNum->fSensitive) 459 pBigNum->pauElements = (RTBIGNUMELEMENT *)RTMemSaferAllocZ(pBigNum->cAllocated * RTBIGNUM_ELEMENT_SIZE); 466 { 467 rc = RTMemSaferAllocZEx((void **)&pBigNum->pauElements, pBigNum->cAllocated * RTBIGNUM_ELEMENT_SIZE, 468 RTMEMSAFER_ALLOC_EX_ALLOW_PAGEABLE_BACKING); 469 Assert(VALID_PTR(pBigNum->pauElements) || RT_FAILURE(rc)); 470 } 460 471 else 461 472 pBigNum->pauElements = (RTBIGNUMELEMENT *)RTMemAlloc(pBigNum->cAllocated * RTBIGNUM_ELEMENT_SIZE); -
trunk/src/VBox/Runtime/generic/memsafer-generic.cpp
r51916 r52018 34 34 #include <iprt/assert.h> 35 35 #include <iprt/string.h> 36 #if defined(IN_SUP_R3) && defined(VBOX) && !defined(RT_NO_GIP) 37 # include <iprt/param.h> 38 # include <VBox/sup.h> 39 #endif /* IN_SUP_R3 */ 36 40 37 41 … … 46 50 #define RTMEMSAFER_PAD_AFTER 32 47 51 52 /******************************************************************************* 53 * Structures and Typedefs * 54 *******************************************************************************/ 55 56 /** 57 * Supported allocation methods. 58 */ 59 typedef enum RTMEMSAFERALLOCMETHOD 60 { 61 /** Invalid method. */ 62 RTMEMSAFERALLOCMETHOD_INVALID = 0, 63 /** RTMem{Alloc|Free} methods, least secure!. */ 64 RTMEMSAFERALLOCMETHOD_RTMEM, 65 /** Support library. */ 66 RTMEMSAFERALLOCMETHOD_SUPR3, 67 /** 32bit hack. */ 68 RTMEMSAFERALLOCMETHOD_32BIT_HACK = 0x7fffffff 69 } RTMEMSAFERALLOCMETHOD; 70 /** Pointer to a allocation method enum. */ 71 typedef RTMEMSAFERALLOCMETHOD *PRTMEMSAFERALLOCMETHOD; 72 73 /** 74 * Memory header for safer memory allocations. 75 * 76 * @note: There is no magic value used deliberately to make identifying this structure 77 * as hard as possible. 78 */ 79 typedef struct RTMEMSAFERHDR 80 { 81 /** Flags passed to this allocation - used for freeing and reallocation. */ 82 uint32_t fFlags; 83 /** Allocation method used. */ 84 RTMEMSAFERALLOCMETHOD enmAllocMethod; 85 /** Amount of bytes allocated. */ 86 size_t cb; 87 } RTMEMSAFERHDR; 88 /** Pointer to a safer memory header. */ 89 typedef RTMEMSAFERHDR *PRTMEMSAFERHDR; 90 /** Make sure we are staying in the padding area. */ 91 AssertCompile(sizeof(RTMEMSAFERHDR) < RTMEMSAFER_PAD_BEFORE); 48 92 49 93 /******************************************************************************* 50 94 * Global Variables * 51 95 *******************************************************************************/ 52 /** XOR scra bler value.96 /** XOR scrambler value. 53 97 * @todo determine this at runtime */ 54 98 #if ARCH_BITS == 32 … … 62 106 63 107 108 /** 109 * Support (SUPR3) based allocator. 110 * 111 * @returns VBox status code. 112 * @retval VERR_NOT_SUPPORTED if this allocation method is not supported in this 113 * version of the library. 114 * @param ppvNew Where to store the pointer to the new buffer on success. 115 * @param cb Amount of bytes to allocate. 116 * 117 * @note: The allocation will have an extra page allocated before and after the 118 * user area with all access rights removed to prevent heartbleed like 119 * attacks. 120 */ 121 static int rtMemSaferSupR3Alloc(void **ppvNew, size_t cb) 122 { 123 #if defined(IN_SUP_R3) && defined(VBOX) && !defined(RT_NO_GIP) 124 /* 125 * Allocate locked memory from the support library. 126 * 127 */ 128 size_t cbUser = RT_ALIGN_Z(cb, PAGE_SIZE); 129 size_t cPages = cbUser / PAGE_SIZE + 2; /* For the extra guarding pages. */ 130 void *pvNew = NULL; 131 int rc = SUPR3PageAllocEx(cPages, 0 /* fFlags */, &pvNew, NULL /* pR0Ptr */, NULL /* paPages */); 132 if (RT_SUCCESS(rc)) 133 { 134 /* Change the memory protection of the pages guarding the allocation. */ 135 rc = SUPR3PageProtect(pvNew, NIL_RTR0PTR, 0, PAGE_SIZE, RTMEM_PROT_NONE); 136 if (RT_SUCCESS(rc)) 137 { 138 rc = SUPR3PageProtect(pvNew, NIL_RTR0PTR, PAGE_SIZE + cbUser, PAGE_SIZE, RTMEM_PROT_NONE); 139 if (RT_SUCCESS(rc)) 140 { 141 *ppvNew = (uint8_t *)pvNew + PAGE_SIZE; 142 return VINF_SUCCESS; 143 } 144 } 145 146 rc = SUPR3PageFreeEx(pvNew, cPages); 147 AssertRC(rc); 148 } 149 150 return rc; 151 #else 152 return VERR_NOT_SUPPORTED; 153 #endif 154 } 155 156 157 /** 158 * Free method for memory allocated using the Support (SUPR3) based allocator. 159 * 160 * @returns nothing. 161 * @param pv Pointer to the memory to free. 162 * @param cb Amount of bytes allocated. 163 */ 164 static void rtMemSafeSupR3Free(void *pv, size_t cb) 165 { 166 #if defined(IN_SUP_R3) && defined(VBOX) && !defined(RT_NO_GIP) 167 size_t cbUser = RT_ALIGN_Z(cb, PAGE_SIZE); 168 size_t cPages = cbUser / PAGE_SIZE + 2; /* For the extra pages. */ 169 void *pvStart = (uint8_t *)pv - PAGE_SIZE; 170 171 int rc = SUPR3PageFreeEx(pvStart, cPages); 172 AssertRC(rc); 173 #else 174 AssertMsgFailed(("SUPR3 allocated memory but freeing is not supported, messed up\n")); 175 #endif 176 } 177 178 64 179 RTDECL(int) RTMemSaferScramble(void *pv, size_t cb) 65 180 { 66 67 AssertMsg(*(size_t *)((char *)pv - RTMEMSAFER_PAD_BEFORE) == cb, 68 ("*pvStart=%#zx cb=%#zx\n", *(size_t *)((char *)pv- RTMEMSAFER_PAD_BEFORE), cb)); 181 PRTMEMSAFERHDR pHdr = (PRTMEMSAFERHDR)((char *)pv - RTMEMSAFER_PAD_BEFORE); 182 AssertMsg(pHdr->cb == cb, ("pHdr->cb=%#zx cb=%#zx\n", pHdr->cb, cb)); 69 183 70 184 /* Note! This isn't supposed to be safe, just less obvious. */ … … 85 199 RTDECL(int) RTMemSaferUnscramble(void *pv, size_t cb) 86 200 { 87 AssertMsg(*(size_t *)((char *)pv - RTMEMSAFER_PAD_BEFORE) == cb,88 ("*pvStart=%#zx cb=%#zx\n", *(size_t *)((char *)pv - RTMEMSAFER_PAD_BEFORE), cb));201 PRTMEMSAFERHDR pHdr = (PRTMEMSAFERHDR)((char *)pv - RTMEMSAFER_PAD_BEFORE); 202 AssertMsg(pHdr->cb == cb, ("pHdr->cb=%#zx cb=%#zx\n", pHdr->cb, cb)); 89 203 90 204 /* Note! This isn't supposed to be safe, just less obvious. */ … … 103 217 104 218 105 RTDECL(int) RTMemSaferAllocZExTag(void **ppvNew, size_t cb, const char *pszTag) RT_NO_THROW219 RTDECL(int) RTMemSaferAllocZExTag(void **ppvNew, size_t cb, uint32_t fFlags, const char *pszTag) RT_NO_THROW 106 220 { 107 221 AssertReturn(cb, VERR_INVALID_PARAMETER); … … 111 225 /* 112 226 * Don't request zeroed memory. We want random heap garbage in the 113 * padding zones, not thing that makes our allocations easier to find.227 * padding zones, nothing that makes our allocations easier to find. 114 228 */ 229 RTMEMSAFERALLOCMETHOD enmAllocMethod = RTMEMSAFERALLOCMETHOD_SUPR3; 115 230 size_t cbUser = RT_ALIGN_Z(cb, RTMEMSAFER_ALIGN); 116 void *pvNew = RTMemAlloc(cbUser + RTMEMSAFER_PAD_BEFORE + RTMEMSAFER_PAD_AFTER); 231 void *pvNew = NULL; 232 int rc = rtMemSaferSupR3Alloc(&pvNew, cbUser + RTMEMSAFER_PAD_BEFORE + RTMEMSAFER_PAD_AFTER); 233 if ( RT_FAILURE(rc) 234 && fFlags & RTMEMSAFER_ALLOC_EX_ALLOW_PAGEABLE_BACKING) 235 { 236 /* Pageable memory allowed. */ 237 enmAllocMethod = RTMEMSAFERALLOCMETHOD_RTMEM; 238 pvNew = RTMemAlloc(cbUser + RTMEMSAFER_PAD_BEFORE + RTMEMSAFER_PAD_AFTER); 239 } 240 117 241 if (pvNew) 118 242 { 119 #ifdef RT_STRICT /* For checking input in string builds. */ 120 memset(pvNew, 0xad, RTMEMSAFER_PAD_BEFORE); 243 PRTMEMSAFERHDR pHdr = (PRTMEMSAFERHDR)pvNew; 244 pHdr->fFlags = fFlags; 245 pHdr->cb = cb; 246 pHdr->enmAllocMethod = enmAllocMethod; 247 #ifdef RT_STRICT /* For checking input in strict builds. */ 248 memset((char *)pvNew + sizeof(RTMEMSAFERHDR), 0xad, RTMEMSAFER_PAD_BEFORE - sizeof(RTMEMSAFERHDR)); 121 249 memset((char *)pvNew + RTMEMSAFER_PAD_BEFORE + cb, 0xda, RTMEMSAFER_PAD_AFTER + (cbUser - cb)); 122 *(size_t *)pvNew = cb;123 250 #endif 124 251 … … 131 258 return VINF_SUCCESS; 132 259 } 133 return VERR_NO_MEMORY;260 return rc; 134 261 } 135 262 RT_EXPORT_SYMBOL(RTMemSaferAllocZExTag); … … 141 268 { 142 269 Assert(cb); 270 271 size_t cbUser = RT_ALIGN_Z(cb, RTMEMSAFER_ALIGN); 143 272 void *pvStart = (char *)pv - RTMEMSAFER_PAD_BEFORE; 144 AssertMsg(*(size_t *)pvStart == cb, ("*pvStart=%#zx cb=%#zx\n", *(size_t *)pvStart, cb)); 273 PRTMEMSAFERHDR pHdr = (PRTMEMSAFERHDR)pvStart; 274 AssertMsg(pHdr->cb == cb, ("pHdr->cb=%#zx cb=%#zx\n", pHdr->cb, cb)); 275 145 276 RTMemWipeThoroughly(pv, RT_ALIGN_Z(cb, RTMEMSAFER_ALIGN), 3); 146 RTMemFree(pvStart); 277 278 switch (pHdr->enmAllocMethod) 279 { 280 case RTMEMSAFERALLOCMETHOD_SUPR3: 281 rtMemSafeSupR3Free(pvStart, cbUser + RTMEMSAFER_PAD_BEFORE + RTMEMSAFER_PAD_AFTER); 282 break; 283 case RTMEMSAFERALLOCMETHOD_RTMEM: 284 RTMemFree(pvStart); 285 break; 286 default: 287 AssertMsgFailed(("Invalid allocation method, corrupted header\n")); 288 } 147 289 } 148 290 else … … 152 294 153 295 154 RTDECL(int) RTMemSaferReallocZExTag(size_t cbOld, void *pvOld, size_t cbNew, void **ppvNew, const char *pszTag) RT_NO_THROW296 RTDECL(int) RTMemSaferReallocZExTag(size_t cbOld, void *pvOld, size_t cbNew, void **ppvNew, uint32_t fFlags, const char *pszTag) RT_NO_THROW 155 297 { 156 298 /* … … 163 305 if (cbNew && cbOld) 164 306 { 307 PRTMEMSAFERHDR pHdr = (PRTMEMSAFERHDR)((char *)pvOld - RTMEMSAFER_PAD_BEFORE); 165 308 AssertPtr(pvOld); 166 309 AssertMsg(*(size_t *)((char *)pvOld - RTMEMSAFER_PAD_BEFORE) == cbOld, … … 168 311 169 312 void *pvNew; 170 rc = RTMemSaferAllocZExTag(&pvNew, cbNew, p szTag);313 rc = RTMemSaferAllocZExTag(&pvNew, cbNew, pHdr->fFlags, pszTag); 171 314 if (RT_SUCCESS(rc)) 172 315 { … … 180 323 { 181 324 Assert(pvOld == NULL); 182 rc = RTMemSaferAllocZExTag(ppvNew, cbNew, pszTag);325 rc = RTMemSaferAllocZExTag(ppvNew, cbNew, fFlags, pszTag); 183 326 } 184 327 /* Free operation*/ … … 196 339 { 197 340 void *pvNew = NULL; 198 int rc = RTMemSaferAllocZExTag(&pvNew, cb, pszTag);341 int rc = RTMemSaferAllocZExTag(&pvNew, cb, RTMEMSAFER_ALLOC_EX_FLAGS_DEFAULT, pszTag); 199 342 if (RT_SUCCESS(rc)) 200 343 return pvNew; … … 207 350 { 208 351 void *pvNew = NULL; 209 int rc = RTMemSaferReallocZExTag(cbOld, pvOld, cbNew, &pvNew, pszTag);352 int rc = RTMemSaferReallocZExTag(cbOld, pvOld, cbNew, &pvNew, RTMEMSAFER_ALLOC_EX_FLAGS_DEFAULT, pszTag); 210 353 if (RT_SUCCESS(rc)) 211 354 return pvNew; -
trunk/src/VBox/Runtime/testcase/Makefile.kmk
r51906 r52018 90 90 tstRTMemPool \ 91 91 tstRTMemWipe \ 92 tstRTMemSafer \ 92 93 tstMove \ 93 94 tstRTMp-1 \ … … 456 457 tstRTMemWipe_SOURCES = tstRTMemWipe.cpp 457 458 459 tstRTMemSafer_TEMPLATE = VBOXR3TSTEXE 460 tstRTMemSafer_SOURCES = tstRTMemSafer.cpp 461 458 462 tstMove_TEMPLATE = VBOXR3TSTEXE 459 463 tstMove_SOURCES = tstMove.cpp
Note:
See TracChangeset
for help on using the changeset viewer.