3 * $Id: lmem.c,v 1.1 1999/12/22 16:02:52 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.1 1999/12/22 16:02:52 mdw
34 * Interface to allocating `locked' memory (which isn't paged out).
38 /*----- Header files ------------------------------------------------------*/
48 #include <sys/types.h>
52 # include <sys/mman.h>
55 #include <mLib/dstr.h>
60 /*----- Main code ---------------------------------------------------------*/
64 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
65 * @size_t sz@ = size of locked memory area requested
67 * Returns: Zero if everything is fine, @+1@ if some insecure memory was
68 * allocated, and @-1@ if everything went horribly wrong.
70 * Use: Initializes the locked memory manager. This function is safe
71 * to call in a privileged program; privileges should usually be
72 * dropped after allocating the locked memory block.
74 * You must call @sub_init@ before allocating locked memory
78 int l_init(lmem
*lm
, size_t sz
)
84 /* --- Preliminaries --- */
88 /* --- Try making a secure locked passphrase buffer --- *
90 * Drop privileges before emitting diagnostic messages.
95 /* --- Memory-map a page from somewhere --- */
98 p
= mmap(0, sz
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANON
, -1, 0);
102 if ((fd
= open("/dev/zero", O_RDWR
)) >= 0) {
103 p
= mmap(0, sz
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
109 /* --- Lock the page in memory --- *
111 * Why does @mmap@ return such a stupid result if it fails?
114 if (p
== 0 || p
== MAP_FAILED
) {
115 lm
->emsg
= "couldn't map locked memory area: %s";
118 } else if (mlock(p
, sz
)) {
119 lm
->emsg
= "error locking memory area: %s";
127 /* --- Make a standard passphrase buffer --- */
133 ll
->emsg
= "locked memory not available on this system";
136 if ((p
= malloc(sz
)) == 0) {
137 lm
->emsg
= "not enough standard memory!";
144 /* --- Initialize the buffer --- */
146 lm
->sz
= lm
->free
= sz
;
149 /* --- Initialize the free list --- */
163 /* --- @l_alloc@ --- *
165 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
166 * @size_t sz@ = size requested
168 * Returns: Pointer to allocated memory.
170 * Use: Allocates @sz@ bytes of locked memory.
173 void *l_alloc(lmem
*lm
, size_t sz
)
177 sz
= (sz
+ 3u) & ~3u;
178 for (l
= lm
->l
; l
; l
= l
->next
) {
185 l_node
*n
= CREATE(l_node
);
193 assert(((void)"Locked buffer space has vanished", lm
->free
>= sz
));
200 /* --- @l_free@ --- *
202 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
203 * @void *p@ = pointer to block
207 * Use: Releases a block of locked memory.
210 void l_free(lmem
*lm
, void *p
)
215 for (l
= lm
->l
; l
; l
= l
->next
) {
218 /* --- If this isn't the block, skip it --- */
224 assert(((void)"Block is already free", l
->f
& LF_ALLOC
));
226 /* --- Coalesce with adjacent free blocks --- */
232 if (ll
&& !(ll
->f
& LF_ALLOC
)) {
233 assert(((void)"Previous block doesn't fit", ll
->p
+ ll
->sz
== p
));
241 if (ll
&& !(ll
->f
& LF_ALLOC
)) {
242 assert(((void)"Next block doesn't fit", ll
->p
== l
->p
+ l
->sz
));
249 assert(((void)"Free lunch", lm
->free
<= lm
->sz
));
252 assert(((void)"Not a locked block", 0));
255 /* --- @l_purge@ --- *
257 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
261 * Use: Purges all the free blocks in the buffer, and clears all of
262 * the locked memory. Memory is not freed back to the system.
265 void l_purge(lmem
*lm
)
271 l_node
*ll
= l
->next
;
275 memset(lm
->p
, 0, lm
->sz
);
285 /* --- @l_report@ --- *
287 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
288 * @dstr *d@ = string to write the error message on
290 * Returns: Zero if the buffer is fine, @+1@ if there was a problem
291 * getting locked memory but insecure stuff could be allocated,
292 * and @-1@ if not even insecure memory could be found.
294 * Use: Returns a user-digestable explanation for the state of a
295 * locked memory buffer. If the return code is zero, no message
296 * is emitted to the string @d@.
299 int l_report(lmem
*lm
, dstr
*d
)
303 dstr_putf(d
, lm
->emsg
, strerror(lm
->err
));
313 /*----- That's all, folks -------------------------------------------------*/