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 /*----- Header files ------------------------------------------------------*/
40 #include <sys/types.h>
44 # include <sys/mman.h>
47 #include <mLib/arena.h>
48 #include <mLib/dstr.h>
53 /*----- Arena operations --------------------------------------------------*/
55 static void *aalloc(arena
*a
, size_t sz
) { return l_alloc((lmem
*)a
, sz
); }
56 static void afree(arena
*a
, void *p
) { l_free((lmem
*)a
, p
); }
57 static void apurge(arena
*a
) { l_purge((lmem
*)a
); }
59 static const arena_ops l_ops
= { aalloc
, arena_fakerealloc
, afree
, apurge
};
61 /*----- Main code ---------------------------------------------------------*/
65 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
66 * @size_t sz@ = size of locked memory area requested
68 * Returns: Zero if everything is fine, @+1@ if some insecure memory was
69 * allocated, and @-1@ if everything went horribly wrong.
71 * Use: Initializes the locked memory manager. This function is safe
72 * to call in a privileged program; privileges should usually be
73 * dropped after allocating the locked memory block.
75 * You must call @sub_init@ before allocating locked memory
79 int l_init(lmem
*lm
, size_t sz
)
85 /* --- Preliminaries --- */
91 /* --- Try making a secure locked passphrase buffer --- *
93 * Drop privileges before emitting diagnostic messages.
98 /* --- Memory-map a page from somewhere --- */
101 p
= mmap(0, sz
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANON
, -1, 0);
105 if ((fd
= open("/dev/zero", O_RDWR
)) >= 0) {
106 p
= mmap(0, sz
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
112 /* --- Lock the page in memory --- *
114 * Why does @mmap@ return such a stupid result if it fails?
117 if (p
== 0 || p
== MAP_FAILED
) {
118 lm
->emsg
= "couldn't map locked memory area: %s";
121 } else if (mlock(p
, sz
)) {
122 lm
->emsg
= "error locking memory area: %s";
131 /* --- Make a standard passphrase buffer --- */
137 lm
->emsg
= "locked memory not available on this system";
140 if ((p
= malloc(sz
)) == 0) {
141 lm
->emsg
= "not enough standard memory!";
148 /* --- Initialize the buffer --- */
150 lm
->sz
= lm
->free
= sz
;
153 /* --- Initialize the free list --- */
167 /* --- @l_alloc@ --- *
169 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
170 * @size_t sz@ = size requested
172 * Returns: Pointer to allocated memory.
174 * Use: Allocates @sz@ bytes of locked memory.
177 void *l_alloc(lmem
*lm
, size_t sz
)
181 sz
= (sz
+ 3u) & ~3u;
182 for (l
= lm
->l
; l
; l
= l
->next
) {
189 l_node
*n
= CREATE(l_node
);
197 assert(((void)"Locked buffer space has vanished", lm
->free
>= sz
));
204 /* --- @l_free@ --- *
206 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
207 * @void *p@ = pointer to block
211 * Use: Releases a block of locked memory.
214 void l_free(lmem
*lm
, void *p
)
219 for (l
= lm
->l
; l
; l
= l
->next
) {
222 /* --- If this isn't the block, skip it --- */
228 assert(((void)"Block is already free", l
->f
& LF_ALLOC
));
230 /* --- Coalesce with adjacent free blocks --- */
236 if (ll
&& !(ll
->f
& LF_ALLOC
)) {
237 assert(((void)"Previous block doesn't fit", ll
->p
+ ll
->sz
== p
));
245 if (ll
&& !(ll
->f
& LF_ALLOC
)) {
246 assert(((void)"Next block doesn't fit", ll
->p
== l
->p
+ l
->sz
));
253 assert(((void)"Free lunch", lm
->free
<= lm
->sz
));
256 assert(((void)"Not a locked block", 0));
259 /* --- @l_purge@ --- *
261 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
265 * Use: Purges all the free blocks in the buffer, and clears all of
266 * the locked memory. Memory is not freed back to the system.
269 void l_purge(lmem
*lm
)
275 l_node
*ll
= l
->next
;
279 memset(lm
->p
, 0, lm
->sz
);
289 /* --- @l_destroy@ --- *
291 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
295 * Use: Disposes of a locked memory arena permanently.
298 void l_destroy(lmem
*lm
)
304 l_node
*ll
= l
->next
;
308 memset(lm
->p
, 0, lm
->sz
);
311 if (lm
->f
& LF_LOCKED
)
312 munmap(lm
->p
, lm
->sz
);
318 /* --- @l_report@ --- *
320 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
321 * @dstr *d@ = string to write the error message on
323 * Returns: Zero if the buffer is fine, @+1@ if there was a problem
324 * getting locked memory but insecure stuff could be allocated,
325 * and @-1@ if not even insecure memory could be found.
327 * Use: Returns a user-digestable explanation for the state of a
328 * locked memory buffer. If the return code is zero, no message
329 * is emitted to the string @d@.
332 int l_report(lmem
*lm
, dstr
*d
)
336 dstr_putf(d
, lm
->emsg
, strerror(lm
->err
));
346 /*----- That's all, folks -------------------------------------------------*/