3 * Locked memory allocation (Unix-specific)
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
38 #include <sys/types.h>
42 # include <sys/mman.h>
45 #include <mLib/arena.h>
46 #include <mLib/dstr.h>
51 /*----- Arena operations --------------------------------------------------*/
53 static void *aalloc(arena *a, size_t sz) { return l_alloc((lmem *)a, sz); }
54 static void afree(arena *a, void *p) { l_free((lmem *)a, p); }
55 static void apurge(arena *a) { l_purge((lmem *)a); }
57 static const arena_ops l_ops = { aalloc, arena_fakerealloc, afree, apurge };
59 /*----- Main code ---------------------------------------------------------*/
63 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
64 * @size_t sz@ = size of locked memory area requested
66 * Returns: Zero if everything is fine, @+1@ if some insecure memory was
67 * allocated, and @-1@ if everything went horribly wrong.
69 * Use: Initializes the locked memory manager. This function is safe
70 * to call in a privileged program; privileges should usually be
71 * dropped after allocating the locked memory block.
73 * You must call @sub_init@ before allocating locked memory
77 int l_init(lmem *lm, size_t sz)
83 /* --- Preliminaries --- */
89 /* --- Try making a secure locked passphrase buffer --- *
91 * Drop privileges before emitting diagnostic messages.
96 /* --- Memory-map a page from somewhere --- */
99 p = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
103 if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
104 p = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
110 /* --- Lock the page in memory --- *
112 * Why does @mmap@ return such a stupid result if it fails?
115 if (p == 0 || p == MAP_FAILED) {
116 lm->emsg = "couldn't map locked memory area: %s";
119 } else if (mlock(p, sz)) {
120 lm->emsg = "error locking memory area: %s";
129 /* --- Make a standard passphrase buffer --- */
135 lm->emsg = "locked memory not available on this system";
138 if ((p = malloc(sz)) == 0) {
139 lm->emsg = "not enough standard memory!";
146 /* --- Initialize the buffer --- */
148 lm->sz = lm->free = sz;
151 /* --- Initialize the free list --- */
165 /* --- @l_alloc@ --- *
167 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
168 * @size_t sz@ = size requested
170 * Returns: Pointer to allocated memory.
172 * Use: Allocates @sz@ bytes of locked memory.
175 void *l_alloc(lmem *lm, size_t sz)
179 sz = (sz + 3u) & ~3u;
180 for (l = lm->l; l; l = l->next) {
187 l_node *n = CREATE(l_node);
195 assert(((void)"Locked buffer space has vanished", lm->free >= sz));
202 /* --- @l_free@ --- *
204 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
205 * @void *p@ = pointer to block
209 * Use: Releases a block of locked memory.
212 void l_free(lmem *lm, void *p)
217 for (l = lm->l; l; l = l->next) {
220 /* --- If this isn't the block, skip it --- */
226 assert(((void)"Block is already free", l->f & LF_ALLOC));
228 /* --- Coalesce with adjacent free blocks --- */
234 if (ll && !(ll->f & LF_ALLOC)) {
235 assert(((void)"Previous block doesn't fit", ll->p + ll->sz == p));
243 if (ll && !(ll->f & LF_ALLOC)) {
244 assert(((void)"Next block doesn't fit", ll->p == l->p + l->sz));
251 assert(((void)"Free lunch", lm->free <= lm->sz));
254 assert(((void)"Not a locked block", 0));
257 /* --- @l_purge@ --- *
259 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
263 * Use: Purges all the free blocks in the buffer, and clears all of
264 * the locked memory. Memory is not freed back to the system.
267 void l_purge(lmem *lm)
273 l_node *ll = l->next;
277 memset(lm->p, 0, lm->sz);
287 /* --- @l_destroy@ --- *
289 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
293 * Use: Disposes of a locked memory arena permanently.
296 void l_destroy(lmem *lm)
302 l_node *ll = l->next;
306 memset(lm->p, 0, lm->sz);
309 if (lm->f & LF_LOCKED)
310 munmap(lm->p, lm->sz);
316 /* --- @l_report@ --- *
318 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
319 * @dstr *d@ = string to write the error message on
321 * Returns: Zero if the buffer is fine, @+1@ if there was a problem
322 * getting locked memory but insecure stuff could be allocated,
323 * and @-1@ if not even insecure memory could be found.
325 * Use: Returns a user-digestable explanation for the state of a
326 * locked memory buffer. If the return code is zero, no message
327 * is emitted to the string @d@.
330 int l_report(lmem *lm, dstr *d)
334 dstr_putf(d, lm->emsg, strerror(lm->err));
344 /*----- That's all, folks -------------------------------------------------*/