blob: 0bd4238049e2702f762d25e021541af8fb859706 [file] [log] [blame]
NingSun0c89b3c2018-02-08 08:34:03 -08001/*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27/*****************************************************************************
28 salloc.cpp
29
30 Contains an implementation of malloc that allocates memory securely
31 *****************************************************************************/
32
33#include "config.h"
34#include "log.h"
35#include "salloc.h"
36#include <limits>
37#if defined(SENSITIVE_NON_PAGED) && !defined(_WIN32)
38#include <sys/mman.h>
39#endif // SENSITIVE_NON_PAGED
40#include <string.h>
41#include "SecureMemoryRegistry.h"
42
43// Allocate memory
44void* salloc(size_t len)
45{
46#ifdef SENSITIVE_NON_PAGED
47 // Allocate memory on a page boundary
48#ifndef _WIN32
49 void* ptr = (void*) valloc(len);
50#else
51 pointer r = (pointer) VirtualAlloc(NULL, n * sizeof(T), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
52#endif
53
54 if (ptr == NULL)
55 {
56 ERROR_MSG("Out of memory");
57
58 return NULL;
59 }
60
61 // Lock the memory so it doesn't get swapped out
62#ifndef _WIN32
63 if (mlock((const void*) ptr, len) != 0)
64#else
65 if (VirtualLock((const void*) r, n * sizeof(T)) == 0)
66#endif
67 {
68 ERROR_MSG("Could not allocate non-paged memory for secure storage");
69
70 // Hmmm... best to not return any allocated space in this case
71#ifndef _WIN32
72 free(ptr);
73#else
74 VirtualFree((const void*) pre, MEM_RELEASE);
75#endif
76
77 return NULL;
78 }
79
80 // Register the memory in the secure memory registry
81 SecureMemoryRegistry::i()->add(ptr, len);
82
83 return ptr;
84#else
85 void* ptr = (void*) malloc(len);
86
87 if (ptr == NULL)
88 {
89 ERROR_MSG("Out of memory");
90
91 return NULL;
92 }
93
94 // Register the memory in the secure memory registry
95 SecureMemoryRegistry::i()->add(ptr, len);
96
97 return ptr;
98#endif // SENSITIVE_NON_PAGED
99}
100
101// Free memory
102void sfree(void* ptr)
103{
104 // Unregister the memory from the secure memory registry
105 size_t len = SecureMemoryRegistry::i()->remove(ptr);
106
107#ifdef PARANOID
108 // First toggle all bits on
109 memset(ptr, 0xFF, len);
110#endif // PARANOID
111
112 // Toggle all bits off
113 memset(ptr, 0x00, len);
114
115#ifdef SENSITIVE_NON_PAGED
116#ifndef _WIN32
117 munlock((const void*) ptr, len);
118#else
119 VirtualFree((const void*) pre, MEM_RELEASE);
120#endif
121
122#endif // SENSITIVE_NON_PAGED
123
124 free(ptr);
125}
126