1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* unit-test-secmem.c: Test low level secure memory allocation functionality
3 
4    Copyright (C) 2007 Stefan Walter
5 
6    The Gnome Keyring Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10 
11    The Gnome Keyring Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15 
16    You should have received a copy of the GNU Library General Public
17    License along with the Gnome Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.
20 
21    Author: Stef Walter <stef@memberwebs.com>
22 */
23 
24 #include "config.h"
25 
26 #include "egg/egg-secure-memory.h"
27 
28 #include <glib.h>
29 
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 
34 
35 EGG_SECURE_DEFINE_GLIB_GLOBALS ();
36 
37 /* Declared in egg-secure-memory.c */
38 extern int egg_secure_warnings;
39 
40 EGG_SECURE_DECLARE (tests);
41 
42 /*
43  * Each test looks like (on one line):
44  *     void unit_test_xxxxx (CuTest* cu)
45  *
46  * Each setup looks like (on one line):
47  *     void unit_setup_xxxxx (void);
48  *
49  * Each teardown looks like (on one line):
50  *     void unit_teardown_xxxxx (void);
51  *
52  * Tests be run in the order specified here.
53  */
54 
55 static gsize
find_non_zero(gpointer mem,gsize len)56 find_non_zero (gpointer mem, gsize len)
57 {
58 	guchar *b, *e;
59 	gsize sz = 0;
60 	for (b = (guchar*)mem, e = ((guchar*)mem) + len; b != e; ++b, ++sz) {
61 		if (*b != 0x00)
62 			return sz;
63 	}
64 
65 	return G_MAXSIZE;
66 }
67 
68 static void
test_alloc_free(void)69 test_alloc_free (void)
70 {
71 	gpointer p;
72 	gboolean ret;
73 
74 	p = egg_secure_alloc_full ("tests", 512, 0);
75 	g_assert (p != NULL);
76 	g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 512));
77 
78 	memset (p, 0x67, 512);
79 
80 	ret = egg_secure_check (p);
81 	g_assert (ret == TRUE);
82 
83 	egg_secure_free_full (p, 0);
84 }
85 
86 static void
test_realloc_across(void)87 test_realloc_across (void)
88 {
89 	gpointer p, p2;
90 
91 	/* Tiny allocation */
92 	p = egg_secure_realloc_full ("tests", NULL, 1088, 0);
93 	g_assert (p != NULL);
94 	g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 1088));
95 
96 	/* Reallocate to a large one, will have to have changed blocks */
97 	p2 = egg_secure_realloc_full ("tests", p, 16200, 0);
98 	g_assert (p2 != NULL);
99 	g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p2, 16200));
100 
101 	egg_secure_free (p2);
102 }
103 
104 static void
test_alloc_two(void)105 test_alloc_two (void)
106 {
107 	gpointer p, p2;
108 	gboolean ret;
109 
110 	p2 = egg_secure_alloc_full ("tests", 4, 0);
111 	g_assert (p2 != NULL);
112 	g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p2, 4));
113 
114 	memset (p2, 0x67, 4);
115 
116 	p = egg_secure_alloc_full ("tests", 16200, 0);
117 	g_assert (p != NULL);
118 	g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 16200));
119 
120 	memset (p, 0x67, 16200);
121 
122 	ret = egg_secure_check (p);
123 	g_assert (ret == TRUE);
124 
125 	egg_secure_free_full (p2, 0);
126 	egg_secure_free_full (p, 0);
127 }
128 
129 static void
test_realloc(void)130 test_realloc (void)
131 {
132 	gchar *str = "a test string to see if realloc works properly";
133 	gpointer p, p2;
134 	gsize len;
135 
136 	len = strlen (str) + 1;
137 
138 	p = egg_secure_realloc_full ("tests", NULL, len, 0);
139 	g_assert (p != NULL);
140 	g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, len));
141 
142 	strcpy ((gchar*)p, str);
143 
144 	p2 = egg_secure_realloc_full ("tests", p, 512, 0);
145 	g_assert (p2 != NULL);
146 	g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (((gchar*)p2) + len, 512 - len));
147 
148 	g_assert (strcmp (p2, str) == 0);
149 
150 	p = egg_secure_realloc_full ("tests", p2, 0, 0);
151 	g_assert (p == NULL);
152 }
153 
154 static void
test_multialloc(void)155 test_multialloc (void)
156 {
157 	GPtrArray *memory;
158 	gpointer data;
159 	gsize size;
160 	int i, action, index;
161 
162 	/* A predetermined seed to get a predetermined pattern */
163 	g_random_set_seed (15);
164 	memory = g_ptr_array_new ();
165 
166 	/* Don't print "can't allocate" warnings */
167 	egg_secure_warnings = 0;
168 
169 	for (i = 0; TRUE; ++i) {
170 
171 		/* Determine what we want to do */
172 		if (memory->len > 0) {
173 			if (i > 100000) /* Once we've done 100000 alocations start freeing */
174 				action = 2;
175 			else
176 				action = g_random_int_range (0, 3);
177 		} else {
178 			action = 0; /* No allocations, so allocate */
179 		}
180 
181 		switch (action) {
182 		case 0: /* Allocate some memory */
183 			size = g_random_int_range (1, 16384);
184 			data = egg_secure_alloc (size);
185 			g_assert (data != NULL);
186 			memset (data, 0xCAFEBABE, size);
187 			g_ptr_array_add (memory, data);
188 			break;
189 		case 1: /* Reallocate some memory */
190 			index = g_random_int_range (0, memory->len);
191 			data = g_ptr_array_index (memory, index);
192 			g_assert (data != NULL);
193 			size = g_random_int_range (1, 16384);
194 			data = egg_secure_realloc (data, size);
195 			g_assert (data != NULL);
196 			memset (data, 0xCAFEBABE, size);
197 			g_ptr_array_index (memory, index) = data;
198 			break;
199 		case 2: /* Free some memory */
200 			index = g_random_int_range (0, memory->len);
201 			data = g_ptr_array_remove_index_fast (memory, index);
202 			g_assert (data != NULL);
203 			egg_secure_free (data);
204 			break;
205 		default:
206 			g_assert_not_reached ();
207 		}
208 
209 		egg_secure_validate ();
210 
211 		if (i > 100000 && !memory->len)
212 			break;
213 	}
214 
215 	g_assert (memory->len == 0);
216 	for (i = 0; i < memory->len; i++)
217 		egg_secure_free (memory->pdata[i]);
218 	g_ptr_array_free (memory, TRUE);
219 
220 	egg_secure_warnings = 1;
221 }
222 
223 static void
test_clear(void)224 test_clear (void)
225 {
226 	gpointer p;
227 
228 	p = egg_secure_alloc_full ("tests", 188, 0);
229 	g_assert (p != NULL);
230 	memset (p, 0x89, 188);
231 	g_assert (memchr (p, 0x89, 188) == p);
232 
233 	egg_secure_clear (p, 188);
234 	g_assert (memchr (p, 0x89, 188) == NULL);
235 
236 	egg_secure_free_full (p, 0);
237 }
238 
239 static void
test_strclear(void)240 test_strclear (void)
241 {
242 	gchar *str;
243 
244 	str = egg_secure_strdup ("secret");
245 	g_assert (str != NULL);
246 	g_assert_cmpuint (strlen (str), ==, 6);
247 	g_assert (strchr (str, 't') == str + 5);
248 
249 	egg_secure_strclear (str);
250 	g_assert_cmpuint (strlen (str), ==, 6);
251 	g_assert (strchr (str, 't') == NULL);
252 
253 	egg_secure_free_full (str, 0);
254 }
255 
256 int
main(int argc,char ** argv)257 main (int argc, char **argv)
258 {
259 	g_test_init (&argc, &argv, NULL);
260 
261 	g_test_add_func ("/secmem/alloc_free", test_alloc_free);
262 	g_test_add_func ("/secmem/realloc_across", test_realloc_across);
263 	g_test_add_func ("/secmem/alloc_two", test_alloc_two);
264 	g_test_add_func ("/secmem/realloc", test_realloc);
265 	g_test_add_func ("/secmem/multialloc", test_multialloc);
266 	g_test_add_func ("/secmem/clear", test_clear);
267 	g_test_add_func ("/secmem/strclear", test_strclear);
268 
269 	return g_test_run ();
270 }
271