chiark / gitweb /
Version bump.
[catacomb] / lmem.c
1 /* -*-c-*-
2  *
3  * $Id: lmem.c,v 1.2 2000/06/17 11:29:20 mdw Exp $
4  *
5  * Locked memory allocation (Unix-specific)
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Catacomb.
13  *
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.
18  * 
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.
23  * 
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,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: lmem.c,v $
33  * Revision 1.2  2000/06/17 11:29:20  mdw
34  * Add arena support.
35  *
36  * Revision 1.1  1999/12/22 16:02:52  mdw
37  * Interface to allocating `locked' memory (which isn't paged out).
38  *
39  */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include "config.h"
44
45 #include <assert.h>
46 #include <errno.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50
51 #include <sys/types.h>
52 #include <unistd.h>
53
54 #ifdef HAVE_MLOCK
55 #  include <sys/mman.h>
56 #endif
57
58 #include <mLib/arena.h>
59 #include <mLib/dstr.h>
60 #include <mLib/sub.h>
61
62 #include "lmem.h"
63
64 /*----- Arena operations --------------------------------------------------*/
65
66 static void *aalloc(arena *a, size_t sz) { return l_alloc((lmem *)a, sz); }
67 static void afree(arena *a, void *p) { l_free((lmem *)a, p); }
68 static void apurge(arena *a) { l_purge((lmem *)a); }
69
70 static arena_ops l_ops = { aalloc, arena_fakerealloc, afree, apurge };
71
72 /*----- Main code ---------------------------------------------------------*/
73
74 /* --- @l_init@ --- *
75  *
76  * Arguments:   @lmem *lm@ = pointer to locked memory descriptor
77  *              @size_t sz@ = size of locked memory area requested
78  *
79  * Returns:     Zero if everything is fine, @+1@ if some insecure memory was
80  *              allocated, and @-1@ if everything went horribly wrong.
81  *
82  * Use:         Initializes the locked memory manager.  This function is safe
83  *              to call in a privileged program; privileges should usually be
84  *              dropped after allocating the locked memory block.
85  *
86  *              You must call @sub_init@ before allocating locked memory
87  *              buffers.
88  */
89
90 int l_init(lmem *lm, size_t sz)
91 {
92   char *p;
93   int rc = 0;
94   l_node *l;
95
96   /* --- Preliminaries --- */
97
98   lm->a.ops = &l_ops;
99   lm->err = 0;
100
101   /* --- Try making a secure locked passphrase buffer --- *
102    *
103    * Drop privileges before emitting diagnostic messages.
104    */
105
106 #ifdef HAVE_MLOCK
107
108   /* --- Memory-map a page from somewhere --- */
109
110 #  ifdef MAP_ANON
111   p = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
112 #  else
113   {
114     int fd;
115     if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
116       p = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
117       close(fd);
118     }
119   }
120 #  endif
121
122   /* --- Lock the page in memory --- *
123    *
124    * Why does @mmap@ return such a stupid result if it fails?
125    */
126
127   if (p == 0 || p == MAP_FAILED) {
128     lm->emsg = "couldn't map locked memory area: %s";
129     lm->err = errno;
130     p = 0;
131   } else if (mlock(p, sz)) {
132     lm->emsg = "error locking memory area: %s";
133     lm->err = errno;
134     munmap(p, sz);
135     p = 0;
136   }
137
138 #endif
139
140   /* --- Make a standard passphrase buffer --- */
141
142 #ifdef HAVE_MLOCK
143   if (!p)
144 #else
145   ll->err = 0;
146   ll->emsg = "locked memory not available on this system";
147 #endif
148   {
149     if ((p = malloc(sz)) == 0) {
150       lm->emsg = "not enough standard memory!";
151       lm->err = ENOMEM;
152       return (-1);
153     }
154     rc = +1;
155   }
156
157   /* --- Initialize the buffer --- */
158
159   lm->sz = lm->free = sz;
160   lm->p = p;
161
162   /* --- Initialize the free list --- */
163
164   l = CREATE(l_node);
165   l->next = 0;
166   l->p = p;
167   l->sz = sz;
168   l->f = 0;
169   lm->l = l;
170
171   /* --- Done --- */
172
173   return (rc);
174 }
175
176 /* --- @l_alloc@ --- *
177  *
178  * Arguments:   @lmem *lm@ = pointer to locked memory descriptor
179  *              @size_t sz@ = size requested
180  *
181  * Returns:     Pointer to allocated memory.
182  *
183  * Use:         Allocates @sz@ bytes of locked memory.
184  */
185
186 void *l_alloc(lmem *lm, size_t sz)
187 {
188   l_node *l;
189
190   sz = (sz + 3u) & ~3u;
191   for (l = lm->l; l; l = l->next) {
192     if (l->f & LF_ALLOC)
193       continue;
194     if (l->sz < sz)
195       continue;
196     l->f |= LF_ALLOC;
197     if (l->sz > sz) {
198       l_node *n = CREATE(l_node);
199       n->next = l->next;
200       n->p = l->p + sz;
201       n->sz = l->sz - sz;
202       l->sz = sz;
203       n->f = 0;
204       l->next = n;
205     }
206     assert(((void)"Locked buffer space has vanished", lm->free >= sz));
207     lm->free -= sz;
208     return (l->p);
209   }
210   return (0);
211 }
212
213 /* --- @l_free@ --- *
214  *
215  * Arguments:   @lmem *lm@ = pointer to locked memory descriptor
216  *              @void *p@ = pointer to block
217  *
218  * Returns:     ---
219  *
220  * Use:         Releases a block of locked memory.
221  */
222
223 void l_free(lmem *lm, void *p)
224 {
225   l_node *l;
226   l_node *ll = 0;
227
228   for (l = lm->l; l; l = l->next) {
229     size_t sz;
230
231     /* --- If this isn't the block, skip it --- */
232
233     if (l->p != p) {
234       ll = l;
235       continue;
236     }
237     assert(((void)"Block is already free", l->f & LF_ALLOC));
238
239     /* --- Coalesce with adjacent free blocks --- */
240
241     l->f &= ~LF_ALLOC;
242     sz = l->sz;
243     memset(p, 0, sz);
244
245     if (ll && !(ll->f & LF_ALLOC)) {
246       assert(((void)"Previous block doesn't fit", ll->p + ll->sz == p));
247       ll->sz += sz;
248       ll->next = l->next;
249       DESTROY(l);
250       l = ll;
251     }
252
253     ll = l->next;
254     if (ll && !(ll->f & LF_ALLOC)) {
255       assert(((void)"Next block doesn't fit", ll->p == l->p + l->sz));
256       l->sz += ll->sz;
257       l->next = ll->next;
258       DESTROY(ll);
259     }
260
261     lm->free += sz;
262     assert(((void)"Free lunch", lm->free <= lm->sz));
263     return;
264   }
265   assert(((void)"Not a locked block", 0));
266 }
267
268 /* --- @l_purge@ --- *
269  *
270  * Arguments:   @lmem *lm@ = pointer to locked memory descriptor
271  *
272  * Returns:     ---
273  *
274  * Use:         Purges all the free blocks in the buffer, and clears all of
275  *              the locked memory.  Memory is not freed back to the system.
276  */
277
278 void l_purge(lmem *lm)
279 {
280   l_node *l;
281
282   l = lm->l;
283   while (l) {
284     l_node *ll = l->next;
285     DESTROY(l);
286     l = ll;
287   }
288   memset(lm->p, 0, lm->sz);
289   l = CREATE(l_node);
290   l->next = 0;
291   l->p = lm->p;
292   l->sz = lm->sz;
293   l->f = 0;
294   lm->l = l;
295   lm->free = l->sz;
296 }
297
298 /* --- @l_report@ --- *
299  *
300  * Arguments:   @lmem *lm@ = pointer to locked memory descriptor
301  *              @dstr *d@ = string to write the error message on
302  *
303  * Returns:     Zero if the buffer is fine, @+1@ if there was a problem
304  *              getting locked memory but insecure stuff could be allocated,
305  *              and @-1@ if not even insecure memory could be found.
306  *
307  * Use:         Returns a user-digestable explanation for the state of a
308  *              locked memory buffer.  If the return code is zero, no message
309  *              is emitted to the string @d@.
310  */
311
312 int l_report(lmem *lm, dstr *d)
313 {
314   int rc;
315   if (lm->err)
316     dstr_putf(d, lm->emsg, strerror(lm->err));
317   if (!lm->p)
318     rc = -1;
319   else if (lm->err)
320     rc = +1;
321   else
322     rc = 0;
323   return (rc);
324 }
325
326 /*----- That's all, folks -------------------------------------------------*/