-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathegg-secure-memory.c
1386 lines (1122 loc) · 30.5 KB
/
egg-secure-memory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* egg-secure-memory.h - library for allocating memory that is non-pageable
Copyright (C) 2007 Stefan Walter
The Gnome Keyring Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Keyring Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Stef Walter <[email protected]>
*/
/*
* IMPORTANT: This is pure vanila standard C, no glib. We need this
* because certain consumers of this protocol need to be built
* without linking in any special libraries. ie: the PKCS#11 module.
*/
#include "config.h"
#include "egg-secure-memory.h"
#include <sys/types.h>
#include <sys/mman.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#ifdef WITH_VALGRIND
#include <valgrind/valgrind.h>
#include <valgrind/memcheck.h>
#endif
#define DEBUG_SECURE_MEMORY 0
#if DEBUG_SECURE_MEMORY
#define DEBUG_ALLOC(msg, n) fprintf(stderr, "%s %lu bytes\n", msg, n);
#else
#define DEBUG_ALLOC(msg, n)
#endif
#define DEFAULT_BLOCK_SIZE 16384
/* Use our own assert to guarantee no glib allocations */
#ifndef ASSERT
#ifdef G_DISABLE_ASSERT
#define ASSERT(x)
#else
#define ASSERT(x) assert(x)
#endif
#endif
#define DO_LOCK() \
EGG_SECURE_GLOBALS.lock ();
#define DO_UNLOCK() \
EGG_SECURE_GLOBALS.unlock ();
static int show_warning = 1;
int egg_secure_warnings = 1;
/*
* We allocate all memory in units of sizeof(void*). This
* is our definition of 'word'.
*/
typedef void* word_t;
/* The amount of extra words we can allocate */
#define WASTE 4
/*
* Track allocated memory or a free block. This structure is not stored
* in the secure memory area. It is allocated from a pool of other
* memory. See meta_pool_xxx ().
*/
typedef struct _Cell {
word_t *words; /* Pointer to secure memory */
size_t n_words; /* Amount of secure memory in words */
size_t requested; /* Amount actually requested by app, in bytes, 0 if unused */
const char *tag; /* Tag which describes the allocation */
struct _Cell *next; /* Next in memory ring */
struct _Cell *prev; /* Previous in memory ring */
} Cell;
/*
* A block of secure memory. This structure is the header in that block.
*/
typedef struct _Block {
word_t *words; /* Actual memory hangs off here */
size_t n_words; /* Number of words in block */
size_t n_used; /* Number of used allocations */
struct _Cell* used_cells; /* Ring of used allocations */
struct _Cell* unused_cells; /* Ring of unused allocations */
struct _Block *next; /* Next block in list */
} Block;
/* -----------------------------------------------------------------------------
* UNUSED STACK
*/
static inline void
unused_push (void **stack, void *ptr)
{
ASSERT (ptr);
ASSERT (stack);
*((void**)ptr) = *stack;
*stack = ptr;
}
static inline void*
unused_pop (void **stack)
{
void *ptr;
ASSERT (stack);
ptr = *stack;
*stack = *(void**)ptr;
return ptr;
}
static inline void*
unused_peek (void **stack)
{
ASSERT (stack);
return *stack;
}
/* -----------------------------------------------------------------------------
* POOL META DATA ALLOCATION
*
* A pool for memory meta data. We allocate fixed size blocks. There are actually
* two different structures stored in this pool: Cell and Block. Cell is allocated
* way more often, and is bigger so we just allocate that size for both.
*/
/* Pool allocates this data type */
typedef union _Item {
Cell cell;
Block block;
} Item;
typedef struct _Pool {
struct _Pool *next; /* Next pool in list */
size_t length; /* Length in bytes of the pool */
size_t used; /* Number of cells used in pool */
void *unused; /* Unused stack of unused stuff */
size_t n_items; /* Total number of items in pool */
Item items[1]; /* Actual items hang off here */
} Pool;
static void *
pool_alloc (void)
{
Pool *pool;
void *pages, *item;
size_t len, i;
if (!EGG_SECURE_GLOBALS.pool_version ||
strcmp (EGG_SECURE_GLOBALS.pool_version, EGG_SECURE_POOL_VER_STR) != 0) {
if (show_warning && egg_secure_warnings)
fprintf (stderr, "the secure memory pool version does not match the code '%s' != '%s'\n",
EGG_SECURE_GLOBALS.pool_version ? EGG_SECURE_GLOBALS.pool_version : "(null)",
EGG_SECURE_POOL_VER_STR);
show_warning = 0;
return NULL;
}
/* A pool with an available item */
for (pool = EGG_SECURE_GLOBALS.pool_data; pool; pool = pool->next) {
if (unused_peek (&pool->unused))
break;
}
/* Create a new pool */
if (pool == NULL) {
len = getpagesize () * 2;
pages = mmap (0, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (pages == MAP_FAILED)
return NULL;
/* Fill in the block header, and inlude in block list */
pool = pages;
pool->next = EGG_SECURE_GLOBALS.pool_data;
EGG_SECURE_GLOBALS.pool_data = pool;
pool->length = len;
pool->used = 0;
pool->unused = NULL;
/* Fill block with unused items */
pool->n_items = (len - sizeof (Pool)) / sizeof (Item);
for (i = 0; i < pool->n_items; ++i)
unused_push (&pool->unused, pool->items + i);
#ifdef WITH_VALGRIND
VALGRIND_CREATE_MEMPOOL(pool, 0, 0);
#endif
}
++pool->used;
ASSERT (unused_peek (&pool->unused));
item = unused_pop (&pool->unused);
#ifdef WITH_VALGRIND
VALGRIND_MEMPOOL_ALLOC (pool, item, sizeof (Item));
#endif
return memset (item, 0, sizeof (Item));
}
static void
pool_free (void* item)
{
Pool *pool, **at;
char *ptr, *beg, *end;
ptr = item;
/* Find which block this one belongs to */
for (at = (Pool **)&EGG_SECURE_GLOBALS.pool_data, pool = *at;
pool != NULL; at = &pool->next, pool = *at) {
beg = (char*)pool->items;
end = (char*)pool + pool->length - sizeof (Item);
if (ptr >= beg && ptr <= end) {
ASSERT ((ptr - beg) % sizeof (Item) == 0);
break;
}
}
/* Otherwise invalid meta */
ASSERT (at);
ASSERT (pool);
ASSERT (pool->used > 0);
/* No more meta cells used in this block, remove from list, destroy */
if (pool->used == 1) {
*at = pool->next;
#ifdef WITH_VALGRIND
VALGRIND_DESTROY_MEMPOOL (pool);
#endif
munmap (pool, pool->length);
return;
}
#ifdef WITH_VALGRIND
VALGRIND_MEMPOOL_FREE (pool, item);
VALGRIND_MAKE_MEM_UNDEFINED (item, sizeof (Item));
#endif
--pool->used;
memset (item, 0xCD, sizeof (Item));
unused_push (&pool->unused, item);
}
#ifndef G_DISABLE_ASSERT
static int
pool_valid (void* item)
{
Pool *pool;
char *ptr, *beg, *end;
ptr = item;
/* Find which block this one belongs to */
for (pool = EGG_SECURE_GLOBALS.pool_data; pool; pool = pool->next) {
beg = (char*)pool->items;
end = (char*)pool + pool->length - sizeof (Item);
if (ptr >= beg && ptr <= end)
return (pool->used && (ptr - beg) % sizeof (Item) == 0);
}
return 0;
}
#endif /* G_DISABLE_ASSERT */
/* -----------------------------------------------------------------------------
* SEC ALLOCATION
*
* Each memory cell begins and ends with a pointer to its metadata. These are also
* used as guards or red zones. Since they're treated as redzones by valgrind we
* have to jump through a few hoops before reading and/or writing them.
*/
static inline size_t
sec_size_to_words (size_t length)
{
return (length % sizeof (void*) ? 1 : 0) + (length / sizeof (void*));
}
static inline void
sec_write_guards (Cell *cell)
{
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_UNDEFINED (cell->words, sizeof (word_t));
VALGRIND_MAKE_MEM_UNDEFINED (cell->words + cell->n_words - 1, sizeof (word_t));
#endif
((void**)cell->words)[0] = (void*)cell;
((void**)cell->words)[cell->n_words - 1] = (void*)cell;
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_NOACCESS (cell->words, sizeof (word_t));
VALGRIND_MAKE_MEM_NOACCESS (cell->words + cell->n_words - 1, sizeof (word_t));
#endif
}
static inline void
sec_check_guards (Cell *cell)
{
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_DEFINED (cell->words, sizeof (word_t));
VALGRIND_MAKE_MEM_DEFINED (cell->words + cell->n_words - 1, sizeof (word_t));
#endif
ASSERT(((void**)cell->words)[0] == (void*)cell);
ASSERT(((void**)cell->words)[cell->n_words - 1] == (void*)cell);
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_NOACCESS (cell->words, sizeof (word_t));
VALGRIND_MAKE_MEM_NOACCESS (cell->words + cell->n_words - 1, sizeof (word_t));
#endif
}
static void
sec_insert_cell_ring (Cell **ring, Cell *cell)
{
ASSERT (ring);
ASSERT (cell);
ASSERT (cell != *ring);
ASSERT (cell->next == NULL);
ASSERT (cell->prev == NULL);
/* Insert back into the mix of available memory */
if (*ring) {
cell->next = (*ring)->next;
cell->prev = *ring;
cell->next->prev = cell;
cell->prev->next = cell;
} else {
cell->next = cell;
cell->prev = cell;
}
*ring = cell;
ASSERT (cell->next->prev == cell);
ASSERT (cell->prev->next == cell);
}
static void
sec_remove_cell_ring (Cell **ring, Cell *cell)
{
ASSERT (ring);
ASSERT (*ring);
ASSERT (cell->next);
ASSERT (cell->prev);
ASSERT (cell->next->prev == cell);
ASSERT (cell->prev->next == cell);
if (cell == *ring) {
/* The last meta? */
if (cell->next == cell) {
ASSERT (cell->prev == cell);
*ring = NULL;
/* Just pointing to this meta */
} else {
ASSERT (cell->prev != cell);
*ring = cell->next;
}
}
cell->next->prev = cell->prev;
cell->prev->next = cell->next;
cell->next = cell->prev = NULL;
ASSERT (*ring != cell);
}
static inline void*
sec_cell_to_memory (Cell *cell)
{
return cell->words + 1;
}
static inline int
sec_is_valid_word (Block *block, word_t *word)
{
return (word >= block->words && word < block->words + block->n_words);
}
static inline void
sec_clear_undefined (void *memory,
size_t from,
size_t to)
{
char *ptr = memory;
ASSERT (from <= to);
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_UNDEFINED (ptr + from, to - from);
#endif
memset (ptr + from, 0, to - from);
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_UNDEFINED (ptr + from, to - from);
#endif
}
static inline void
sec_clear_noaccess (void *memory, size_t from, size_t to)
{
char *ptr = memory;
ASSERT (from <= to);
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_UNDEFINED (ptr + from, to - from);
#endif
memset (ptr + from, 0, to - from);
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_NOACCESS (ptr + from, to - from);
#endif
}
static Cell*
sec_neighbor_before (Block *block, Cell *cell)
{
word_t *word;
ASSERT (cell);
ASSERT (block);
word = cell->words - 1;
if (!sec_is_valid_word (block, word))
return NULL;
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_DEFINED (word, sizeof (word_t));
#endif
cell = *word;
sec_check_guards (cell);
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_NOACCESS (word, sizeof (word_t));
#endif
return cell;
}
static Cell*
sec_neighbor_after (Block *block, Cell *cell)
{
word_t *word;
ASSERT (cell);
ASSERT (block);
word = cell->words + cell->n_words;
if (!sec_is_valid_word (block, word))
return NULL;
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_DEFINED (word, sizeof (word_t));
#endif
cell = *word;
sec_check_guards (cell);
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_NOACCESS (word, sizeof (word_t));
#endif
return cell;
}
static void*
sec_alloc (Block *block,
const char *tag,
size_t length)
{
Cell *cell, *other;
size_t n_words;
void *memory;
ASSERT (block);
ASSERT (length);
ASSERT (tag);
if (!block->unused_cells)
return NULL;
/*
* Each memory allocation is aligned to a pointer size, and
* then, sandwidched between two pointers to its meta data.
* These pointers also act as guards.
*
* We allocate memory in units of sizeof (void*)
*/
n_words = sec_size_to_words (length) + 2;
/* Look for a cell of at least our required size */
cell = block->unused_cells;
while (cell->n_words < n_words) {
cell = cell->next;
if (cell == block->unused_cells) {
cell = NULL;
break;
}
}
if (!cell)
return NULL;
ASSERT (cell->tag == NULL);
ASSERT (cell->requested == 0);
ASSERT (cell->prev);
ASSERT (cell->words);
sec_check_guards (cell);
/* Steal from the cell if it's too long */
if (cell->n_words > n_words + WASTE) {
other = pool_alloc ();
if (!other)
return NULL;
other->n_words = n_words;
other->words = cell->words;
cell->n_words -= n_words;
cell->words += n_words;
sec_write_guards (other);
sec_write_guards (cell);
cell = other;
}
if (cell->next)
sec_remove_cell_ring (&block->unused_cells, cell);
++block->n_used;
cell->tag = tag;
cell->requested = length;
sec_insert_cell_ring (&block->used_cells, cell);
memory = sec_cell_to_memory (cell);
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_UNDEFINED (memory, length);
#endif
return memset (memory, 0, length);
}
static void*
sec_free (Block *block, void *memory)
{
Cell *cell, *other;
word_t *word;
ASSERT (block);
ASSERT (memory);
word = memory;
--word;
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_DEFINED (word, sizeof (word_t));
#endif
/* Lookup the meta for this memory block (using guard pointer) */
ASSERT (sec_is_valid_word (block, word));
ASSERT (pool_valid (*word));
cell = *word;
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_DEFINED (cell->words, cell->n_words * sizeof (word_t));
#endif
sec_check_guards (cell);
sec_clear_noaccess (memory, 0, cell->requested);
sec_check_guards (cell);
ASSERT (cell->requested > 0);
ASSERT (cell->tag != NULL);
/* Remove from the used cell ring */
sec_remove_cell_ring (&block->used_cells, cell);
/* Find previous unallocated neighbor, and merge if possible */
other = sec_neighbor_before (block, cell);
if (other && other->requested == 0) {
ASSERT (other->tag == NULL);
ASSERT (other->next && other->prev);
other->n_words += cell->n_words;
sec_write_guards (other);
pool_free (cell);
cell = other;
}
/* Find next unallocated neighbor, and merge if possible */
other = sec_neighbor_after (block, cell);
if (other && other->requested == 0) {
ASSERT (other->tag == NULL);
ASSERT (other->next && other->prev);
other->n_words += cell->n_words;
other->words = cell->words;
if (cell->next)
sec_remove_cell_ring (&block->unused_cells, cell);
sec_write_guards (other);
pool_free (cell);
cell = other;
}
/* Add to the unused list if not already there */
if (!cell->next)
sec_insert_cell_ring (&block->unused_cells, cell);
cell->tag = NULL;
cell->requested = 0;
--block->n_used;
return NULL;
}
static void
memcpy_with_vbits (void *dest,
void *src,
size_t length)
{
#ifdef WITH_VALGRIND
int vbits_setup = 0;
void *vbits = NULL;
if (RUNNING_ON_VALGRIND) {
vbits = malloc (length);
if (vbits != NULL)
vbits_setup = VALGRIND_GET_VBITS (src, vbits, length);
VALGRIND_MAKE_MEM_DEFINED (src, length);
}
#endif
memcpy (dest, src, length);
#ifdef WITH_VALGRIND
if (vbits_setup == 1) {
VALGRIND_SET_VBITS (dest, vbits, length);
VALGRIND_SET_VBITS (src, vbits, length);
}
free (vbits);
#endif
}
static void*
sec_realloc (Block *block,
const char *tag,
void *memory,
size_t length)
{
Cell *cell, *other;
word_t *word;
size_t n_words;
size_t valid;
void *alloc;
/* Standard realloc behavior, should have been handled elsewhere */
ASSERT (memory != NULL);
ASSERT (length > 0);
ASSERT (tag != NULL);
/* Dig out where the meta should be */
word = memory;
--word;
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_DEFINED (word, sizeof (word_t));
#endif
ASSERT (sec_is_valid_word (block, word));
ASSERT (pool_valid (*word));
cell = *word;
/* Validate that it's actually for real */
sec_check_guards (cell);
ASSERT (cell->requested > 0);
ASSERT (cell->tag != NULL);
/* The amount of valid data */
valid = cell->requested;
/* How many words we actually want */
n_words = sec_size_to_words (length) + 2;
/* Less memory is required than is in the cell */
if (n_words <= cell->n_words) {
/* TODO: No shrinking behavior yet */
cell->requested = length;
alloc = sec_cell_to_memory (cell);
/*
* Even though we may be reusing the same cell, that doesn't
* mean that the allocation is shrinking. It could have shrunk
* and is now expanding back some.
*/
if (length < valid)
sec_clear_undefined (alloc, length, valid);
return alloc;
}
/* Need braaaaaiiiiiinsss... */
while (cell->n_words < n_words) {
/* See if we have a neighbor who can give us some memory */
other = sec_neighbor_after (block, cell);
if (!other || other->requested != 0)
break;
/* Eat the whole neighbor if not too big */
if (n_words - cell->n_words + WASTE >= other->n_words) {
cell->n_words += other->n_words;
sec_write_guards (cell);
sec_remove_cell_ring (&block->unused_cells, other);
pool_free (other);
/* Steal from the neighbor */
} else {
other->words += n_words - cell->n_words;
other->n_words -= n_words - cell->n_words;
sec_write_guards (other);
cell->n_words = n_words;
sec_write_guards (cell);
}
}
if (cell->n_words >= n_words) {
cell->requested = length;
cell->tag = tag;
alloc = sec_cell_to_memory (cell);
sec_clear_undefined (alloc, valid, length);
return alloc;
}
/* That didn't work, try alloc/free */
alloc = sec_alloc (block, tag, length);
if (alloc) {
memcpy_with_vbits (alloc, memory, valid);
sec_free (block, memory);
}
return alloc;
}
static size_t
sec_allocated (Block *block, void *memory)
{
Cell *cell;
word_t *word;
ASSERT (block);
ASSERT (memory);
word = memory;
--word;
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_DEFINED (word, sizeof (word_t));
#endif
/* Lookup the meta for this memory block (using guard pointer) */
ASSERT (sec_is_valid_word (block, word));
ASSERT (pool_valid (*word));
cell = *word;
sec_check_guards (cell);
ASSERT (cell->requested > 0);
ASSERT (cell->tag != NULL);
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_NOACCESS (word, sizeof (word_t));
#endif
return cell->requested;
}
static void
sec_validate (Block *block)
{
Cell *cell;
word_t *word, *last;
#ifdef WITH_VALGRIND
if (RUNNING_ON_VALGRIND)
return;
#endif
word = block->words;
last = word + block->n_words;
for (;;) {
ASSERT (word < last);
ASSERT (sec_is_valid_word (block, word));
ASSERT (pool_valid (*word));
cell = *word;
/* Validate that it's actually for real */
sec_check_guards (cell);
/* Is it an allocated block? */
if (cell->requested > 0) {
ASSERT (cell->tag != NULL);
ASSERT (cell->next != NULL);
ASSERT (cell->prev != NULL);
ASSERT (cell->next->prev == cell);
ASSERT (cell->prev->next == cell);
ASSERT (cell->requested <= (cell->n_words - 2) * sizeof (word_t));
/* An unused block */
} else {
ASSERT (cell->tag == NULL);
ASSERT (cell->next != NULL);
ASSERT (cell->prev != NULL);
ASSERT (cell->next->prev == cell);
ASSERT (cell->prev->next == cell);
}
word += cell->n_words;
if (word == last)
break;
}
}
/* -----------------------------------------------------------------------------
* LOCKED MEMORY
*/
static void*
sec_acquire_pages (size_t *sz,
const char *during_tag)
{
void *pages;
unsigned long pgsize;
ASSERT (sz);
ASSERT (*sz);
ASSERT (during_tag);
/* Make sure sz is a multiple of the page size */
pgsize = getpagesize ();
*sz = (*sz + pgsize -1) & ~(pgsize - 1);
#if defined(HAVE_MLOCK)
pages = mmap (0, *sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (pages == MAP_FAILED) {
if (show_warning && egg_secure_warnings)
fprintf (stderr, "couldn't map %lu bytes of memory (%s): %s\n",
(unsigned long)*sz, during_tag, strerror (errno));
show_warning = 0;
return NULL;
}
if (mlock (pages, *sz) < 0) {
if (show_warning && egg_secure_warnings && errno != EPERM) {
fprintf (stderr, "couldn't lock %lu bytes of memory (%s): %s\n",
(unsigned long)*sz, during_tag, strerror (errno));
show_warning = 0;
}
munmap (pages, *sz);
return NULL;
}
DEBUG_ALLOC ("gkr-secure-memory: new block ", *sz);
show_warning = 1;
return pages;
#else
if (show_warning && egg_secure_warnings)
fprintf (stderr, "your system does not support private memory");
show_warning = 0;
return NULL;
#endif
}
static void
sec_release_pages (void *pages, size_t sz)
{
ASSERT (pages);
ASSERT (sz % getpagesize () == 0);
#if defined(HAVE_MLOCK)
if (munlock (pages, sz) < 0 && egg_secure_warnings)
fprintf (stderr, "couldn't unlock private memory: %s\n", strerror (errno));
if (munmap (pages, sz) < 0 && egg_secure_warnings)
fprintf (stderr, "couldn't unmap private anonymous memory: %s\n", strerror (errno));
DEBUG_ALLOC ("gkr-secure-memory: freed block ", sz);
#else
ASSERT (FALSE);
#endif
}
/* -----------------------------------------------------------------------------
* MANAGE DIFFERENT BLOCKS
*/
static Block *all_blocks = NULL;
static Block*
sec_block_create (size_t size,
const char *during_tag)
{
Block *block;
Cell *cell;
ASSERT (during_tag);
/* We can force all all memory to be malloced */
if (getenv ("SECMEM_FORCE_FALLBACK"))
return NULL;
block = pool_alloc ();
if (!block)
return NULL;
cell = pool_alloc ();
if (!cell) {
pool_free (block);
return NULL;
}
/* The size above is a minimum, we're free to go bigger */
if (size < DEFAULT_BLOCK_SIZE)
size = DEFAULT_BLOCK_SIZE;
block->words = sec_acquire_pages (&size, during_tag);
block->n_words = size / sizeof (word_t);
if (!block->words) {
pool_free (block);
pool_free (cell);
return NULL;
}
#ifdef WITH_VALGRIND
VALGRIND_MAKE_MEM_DEFINED (block->words, size);
#endif
/* The first cell to allocate from */
cell->words = block->words;
cell->n_words = block->n_words;
cell->requested = 0;
sec_write_guards (cell);
sec_insert_cell_ring (&block->unused_cells, cell);
block->next = all_blocks;
all_blocks = block;
return block;
}
static void
sec_block_destroy (Block *block)
{
Block *bl, **at;
Cell *cell;
ASSERT (block);
ASSERT (block->words);
ASSERT (block->n_used == 0);
/* Remove from the list */
for (at = &all_blocks, bl = *at; bl; at = &bl->next, bl = *at) {
if (bl == block) {
*at = block->next;
break;
}
}
/* Must have been found */
ASSERT (bl == block);
ASSERT (block->used_cells == NULL);