VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/memsafer-generic.cpp@ 51930

Last change on this file since 51930 was 51916, checked in by vboxsync, 11 years ago

Drop electric fences before looking for leaks.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/* $Id: memsafer-generic.cpp 51916 2014-07-08 01:36:38Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocate for Sensitive Data, generic heap-based implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2014 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/memsafer.h>
33
34#include <iprt/assert.h>
35#include <iprt/string.h>
36
37
38/*******************************************************************************
39* Defined Constants And Macros *
40*******************************************************************************/
41/** Allocation size alignment. */
42#define RTMEMSAFER_ALIGN 16
43/** Padding after the block to avoid small overruns. */
44#define RTMEMSAFER_PAD_BEFORE 96
45/** Padding after the block to avoid small underruns. */
46#define RTMEMSAFER_PAD_AFTER 32
47
48
49/*******************************************************************************
50* Global Variables *
51*******************************************************************************/
52/** XOR scrabler value.
53 * @todo determine this at runtime */
54#if ARCH_BITS == 32
55static uintptr_t g_uScramblerXor = UINT32_C(0x867af88d);
56#elif ARCH_BITS == 64
57static uintptr_t g_uScramblerXor = UINT64_C(0xed95ecc99416d312);
58#else
59# error "Bad ARCH_BITS value"
60#endif
61
62
63
64RTDECL(int) RTMemSaferScramble(void *pv, size_t cb)
65{
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));
69
70 /* Note! This isn't supposed to be safe, just less obvious. */
71 uintptr_t *pu = (uintptr_t *)pv;
72 cb = RT_ALIGN_Z(cb, RTMEMSAFER_ALIGN);
73 while (cb > 0)
74 {
75 *pu ^= g_uScramblerXor;
76 pu++;
77 cb -= sizeof(*pu);
78 }
79
80 return VINF_SUCCESS;
81}
82RT_EXPORT_SYMBOL(RTMemSaferScramble);
83
84
85RTDECL(int) RTMemSaferUnscramble(void *pv, size_t cb)
86{
87 AssertMsg(*(size_t *)((char *)pv - RTMEMSAFER_PAD_BEFORE) == cb,
88 ("*pvStart=%#zx cb=%#zx\n", *(size_t *)((char *)pv - RTMEMSAFER_PAD_BEFORE), cb));
89
90 /* Note! This isn't supposed to be safe, just less obvious. */
91 uintptr_t *pu = (uintptr_t *)pv;
92 cb = RT_ALIGN_Z(cb, RTMEMSAFER_ALIGN);
93 while (cb > 0)
94 {
95 *pu ^= g_uScramblerXor;
96 pu++;
97 cb -= sizeof(*pu);
98 }
99
100 return VINF_SUCCESS;
101}
102RT_EXPORT_SYMBOL(RTMemSaferUnscramble);
103
104
105RTDECL(int) RTMemSaferAllocZExTag(void **ppvNew, size_t cb, const char *pszTag) RT_NO_THROW
106{
107 AssertReturn(cb, VERR_INVALID_PARAMETER);
108 AssertPtrReturn(ppvNew, VERR_INVALID_PARAMETER);
109 *ppvNew = NULL;
110
111 /*
112 * Don't request zeroed memory. We want random heap garbage in the
113 * padding zones, notthing that makes our allocations easier to find.
114 */
115 size_t cbUser = RT_ALIGN_Z(cb, RTMEMSAFER_ALIGN);
116 void *pvNew = RTMemAlloc(cbUser + RTMEMSAFER_PAD_BEFORE + RTMEMSAFER_PAD_AFTER);
117 if (pvNew)
118 {
119#ifdef RT_STRICT /* For checking input in string builds. */
120 memset(pvNew, 0xad, RTMEMSAFER_PAD_BEFORE);
121 memset((char *)pvNew + RTMEMSAFER_PAD_BEFORE + cb, 0xda, RTMEMSAFER_PAD_AFTER + (cbUser - cb));
122 *(size_t *)pvNew = cb;
123#endif
124
125 void *pvUser = (char *)pvNew + RTMEMSAFER_PAD_BEFORE;
126 *ppvNew = pvUser;
127
128 /* You don't use this API for performance, so we always clean memory. */
129 RT_BZERO(pvUser, cb);
130
131 return VINF_SUCCESS;
132 }
133 return VERR_NO_MEMORY;
134}
135RT_EXPORT_SYMBOL(RTMemSaferAllocZExTag);
136
137
138RTDECL(void) RTMemSaferFree(void *pv, size_t cb) RT_NO_THROW
139{
140 if (pv)
141 {
142 Assert(cb);
143 void *pvStart = (char *)pv - RTMEMSAFER_PAD_BEFORE;
144 AssertMsg(*(size_t *)pvStart == cb, ("*pvStart=%#zx cb=%#zx\n", *(size_t *)pvStart, cb));
145 RTMemWipeThoroughly(pv, RT_ALIGN_Z(cb, RTMEMSAFER_ALIGN), 3);
146 RTMemFree(pvStart);
147 }
148 else
149 Assert(cb == 0);
150}
151RT_EXPORT_SYMBOL(RTMemSaferFree);
152
153
154RTDECL(int) RTMemSaferReallocZExTag(size_t cbOld, void *pvOld, size_t cbNew, void **ppvNew, const char *pszTag) RT_NO_THROW
155{
156 /*
157 * We cannot let the heap move us around because we will be failing in our
158 * duty to clean things up. So, allocate a new block, copy over the old
159 * content, and free the old one.
160 */
161 int rc;
162 /* Real realloc. */
163 if (cbNew && cbOld)
164 {
165 AssertPtr(pvOld);
166 AssertMsg(*(size_t *)((char *)pvOld - RTMEMSAFER_PAD_BEFORE) == cbOld,
167 ("*pvStart=%#zx cbOld=%#zx\n", *(size_t *)((char *)pvOld - RTMEMSAFER_PAD_BEFORE), cbOld));
168
169 void *pvNew;
170 rc = RTMemSaferAllocZExTag(&pvNew, cbNew, pszTag);
171 if (RT_SUCCESS(rc))
172 {
173 memcpy(pvNew, pvOld, RT_MIN(cbNew, cbOld));
174 RTMemSaferFree(pvOld, cbOld);
175 *ppvNew = pvNew;
176 }
177 }
178 /* First allocation. */
179 else if (!cbOld)
180 {
181 Assert(pvOld == NULL);
182 rc = RTMemSaferAllocZExTag(ppvNew, cbNew, pszTag);
183 }
184 /* Free operation*/
185 else
186 {
187 RTMemSaferFree(pvOld, cbOld);
188 rc = VINF_SUCCESS;
189 }
190 return rc;
191}
192RT_EXPORT_SYMBOL(RTMemSaferReallocZExTag);
193
194
195RTDECL(void *) RTMemSaferAllocZTag(size_t cb, const char *pszTag) RT_NO_THROW
196{
197 void *pvNew = NULL;
198 int rc = RTMemSaferAllocZExTag(&pvNew, cb, pszTag);
199 if (RT_SUCCESS(rc))
200 return pvNew;
201 return NULL;
202}
203RT_EXPORT_SYMBOL(RTMemSaferAllocZTag);
204
205
206RTDECL(void *) RTMemSaferReallocZTag(size_t cbOld, void *pvOld, size_t cbNew, const char *pszTag) RT_NO_THROW
207{
208 void *pvNew = NULL;
209 int rc = RTMemSaferReallocZExTag(cbOld, pvOld, cbNew, &pvNew, pszTag);
210 if (RT_SUCCESS(rc))
211 return pvNew;
212 return NULL;
213}
214RT_EXPORT_SYMBOL(RTMemSaferReallocZTag);
215
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette