3 * $Id: lmem.c,v 1.4 2002/01/24 22:26:11 mdw Exp $
5 * Locked memory allocation (Unix-specific)
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 2002/01/24 22:26:11 mdw
34 * Fix build failure when @mlock@ not available.
36 * Revision 1.3 2000/07/29 21:58:15 mdw
37 * (l_destroy): New function for destroying locked memory blocks.
39 * Revision 1.2 2000/06/17 11:29:20 mdw
42 * Revision 1.1 1999/12/22 16:02:52 mdw
43 * Interface to allocating `locked' memory (which isn't paged out).
47 /*----- Header files ------------------------------------------------------*/
57 #include <sys/types.h>
61 # include <sys/mman.h>
64 #include <mLib/arena.h>
65 #include <mLib/dstr.h>
70 /*----- Arena operations --------------------------------------------------*/
72 static void *aalloc(arena
*a
, size_t sz
) { return l_alloc((lmem
*)a
, sz
); }
73 static void afree(arena
*a
, void *p
) { l_free((lmem
*)a
, p
); }
74 static void apurge(arena
*a
) { l_purge((lmem
*)a
); }
76 static arena_ops l_ops
= { aalloc
, arena_fakerealloc
, afree
, apurge
};
78 /*----- Main code ---------------------------------------------------------*/
82 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
83 * @size_t sz@ = size of locked memory area requested
85 * Returns: Zero if everything is fine, @+1@ if some insecure memory was
86 * allocated, and @-1@ if everything went horribly wrong.
88 * Use: Initializes the locked memory manager. This function is safe
89 * to call in a privileged program; privileges should usually be
90 * dropped after allocating the locked memory block.
92 * You must call @sub_init@ before allocating locked memory
96 int l_init(lmem
*lm
, size_t sz
)
102 /* --- Preliminaries --- */
108 /* --- Try making a secure locked passphrase buffer --- *
110 * Drop privileges before emitting diagnostic messages.
115 /* --- Memory-map a page from somewhere --- */
118 p
= mmap(0, sz
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANON
, -1, 0);
122 if ((fd
= open("/dev/zero", O_RDWR
)) >= 0) {
123 p
= mmap(0, sz
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
129 /* --- Lock the page in memory --- *
131 * Why does @mmap@ return such a stupid result if it fails?
134 if (p
== 0 || p
== MAP_FAILED
) {
135 lm
->emsg
= "couldn't map locked memory area: %s";
138 } else if (mlock(p
, sz
)) {
139 lm
->emsg
= "error locking memory area: %s";
148 /* --- Make a standard passphrase buffer --- */
154 lm
->emsg
= "locked memory not available on this system";
157 if ((p
= malloc(sz
)) == 0) {
158 lm
->emsg
= "not enough standard memory!";
165 /* --- Initialize the buffer --- */
167 lm
->sz
= lm
->free
= sz
;
170 /* --- Initialize the free list --- */
184 /* --- @l_alloc@ --- *
186 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
187 * @size_t sz@ = size requested
189 * Returns: Pointer to allocated memory.
191 * Use: Allocates @sz@ bytes of locked memory.
194 void *l_alloc(lmem
*lm
, size_t sz
)
198 sz
= (sz
+ 3u) & ~3u;
199 for (l
= lm
->l
; l
; l
= l
->next
) {
206 l_node
*n
= CREATE(l_node
);
214 assert(((void)"Locked buffer space has vanished", lm
->free
>= sz
));
221 /* --- @l_free@ --- *
223 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
224 * @void *p@ = pointer to block
228 * Use: Releases a block of locked memory.
231 void l_free(lmem
*lm
, void *p
)
236 for (l
= lm
->l
; l
; l
= l
->next
) {
239 /* --- If this isn't the block, skip it --- */
245 assert(((void)"Block is already free", l
->f
& LF_ALLOC
));
247 /* --- Coalesce with adjacent free blocks --- */
253 if (ll
&& !(ll
->f
& LF_ALLOC
)) {
254 assert(((void)"Previous block doesn't fit", ll
->p
+ ll
->sz
== p
));
262 if (ll
&& !(ll
->f
& LF_ALLOC
)) {
263 assert(((void)"Next block doesn't fit", ll
->p
== l
->p
+ l
->sz
));
270 assert(((void)"Free lunch", lm
->free
<= lm
->sz
));
273 assert(((void)"Not a locked block", 0));
276 /* --- @l_purge@ --- *
278 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
282 * Use: Purges all the free blocks in the buffer, and clears all of
283 * the locked memory. Memory is not freed back to the system.
286 void l_purge(lmem
*lm
)
292 l_node
*ll
= l
->next
;
296 memset(lm
->p
, 0, lm
->sz
);
306 /* --- @l_destroy@ --- *
308 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
312 * Use: Disposes of a locked memory arena permanently.
315 void l_destroy(lmem
*lm
)
321 l_node
*ll
= l
->next
;
325 memset(lm
->p
, 0, lm
->sz
);
327 if (lm
->f
& LF_LOCKED
)
328 munmap(lm
->p
, lm
->sz
);
333 /* --- @l_report@ --- *
335 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
336 * @dstr *d@ = string to write the error message on
338 * Returns: Zero if the buffer is fine, @+1@ if there was a problem
339 * getting locked memory but insecure stuff could be allocated,
340 * and @-1@ if not even insecure memory could be found.
342 * Use: Returns a user-digestable explanation for the state of a
343 * locked memory buffer. If the return code is zero, no message
344 * is emitted to the string @d@.
347 int l_report(lmem
*lm
, dstr
*d
)
351 dstr_putf(d
, lm
->emsg
, strerror(lm
->err
));
361 /*----- That's all, folks -------------------------------------------------*/