File: memalloc.c

package info (click to toggle)
nessus-libraries 1.0.10-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 9,536 kB
  • ctags: 12,585
  • sloc: ansic: 72,626; asm: 25,921; sh: 19,570; makefile: 1,974; cpp: 560; pascal: 536; yacc: 234; lex: 203; lisp: 186; perl: 76; fortran: 24
file content (342 lines) | stat: -rw-r--r-- 8,457 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 *          Copyright (c) mjh-EDV Beratung, 1996-1999
 *     mjh-EDV Beratung - 63263 Neu-Isenburg - Rosenstrasse 12
 *          Tel +49 6102 328279 - Fax +49 6102 328278
 *                Email info@mjh.teddy-net.com
 *
 *   Author: Jordan Hrycaj <jordan@mjh.teddy-net.com>
 *
 *   $Id: memalloc.c,v 1.7 2000/08/14 21:18:05 jordan Exp $
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Library General Public
 *   License as published by the Free Software Foundation; either
 *   version 2 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this library; if not, write to the Free
 *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *   PEKS - private exponent key stuff for Diffie Hellman and El Gamal
 */

#include "common-stuff.h"
#include "memalloc.h"
#include "rand/crandom.h"
#include "gmp.h"

/* ------------------------------------------------------------------------ *
 *                        global variables                                  *
 * ------------------------------------------------------------------------ */

int _gmp2_alloc_flag = 0 ;

/* ------------------------------------------------------------------------ *
 *                        private definitions                               *
 * ------------------------------------------------------------------------ */
typedef
struct _chunk {
  unsigned length ; /* total size of memory chunk */
  unsigned  class ; /* type of memory: secure/public */

  /* structure aligned custom memory access */
  struct { void *data [1] ; } aligned ;
} chunk ;

#define _PUB_STORAGE 0xAAAAAAAAL
#define _PRIVATE_MEM 0x5A555A55L
#define _SECURE_MEM  0x55555555L

/* get the byte offset of a field */
#define STRUCT_OFFSET(type, field) \
	((char*)(&((type*)0)->field)-(char*)0)

/* given a pointer to field "field" from a structure of type "type",
   get the structure pointer */
#define REVERT_FIELD_PTR(p, type, field)  \
	((type*)(((char*)p) - STRUCT_OFFSET (type,field)))

/* ------------------------------------------------------------------------ *
 *                        private variables                                 *
 * ------------------------------------------------------------------------ */

static size_t mem_block_max = MEM_BLOCK_MAX_DEFAULT ;

/* ------------------------------------------------------------------------ *
 *            private functions: posix therads stuff                        *
 * ------------------------------------------------------------------------ */

#undef  SYSNAME /* solaris wants that sometimes */
#define SYSNAME "memalloc"
#include "peks-sema.h"

/* ------------------------------------------------------------------------ *
 *                   private functions                                      *
 * ------------------------------------------------------------------------ */

static void
warning
  (const char *s,
   unsigned    u)
{
  fprintf (stderr, s, u);
  fputc ('!', stderr);
# ifdef _WIN32
  fputc ('\r', stderr);
# endif 
  fputc ('\n', stderr);
}

static void
fatal
  (const char *s,
   unsigned    u)
{
  warning (s, u);
  exit (2);
}

static void
fatal_alloc
  (const char *s,
   unsigned    u)
{
  fputs ("Could not allocate ", stderr);
  fatal (s, u);
}

static void
fatal_realloc
  (const char *s,
   unsigned    u)
{
  fputs ("Could not re-allocate to ", stderr);
  fatal (s, u);
}

static void
reclassify
  (void              *p,
   unsigned long class)
{
  chunk *q ;

  if (p == 0) {
    warning ("Attempt to reclassify the NULL pointer", 0);
    return ;
  }

  q = REVERT_FIELD_PTR (p, chunk, aligned) ;

  switch (q->class) {
  case  _SECURE_MEM :
  case _PRIVATE_MEM :
  case _PUB_STORAGE :
    q->class = class ;
    return ;
  }
  
  warning ("Cannot reclassify corrupt memory at 0x%u", (unsigned)p);
}

/* needed for gmp2 customization */
static void *
xrealloc2
  (void    *p, 
   size_t old,
   size_t new)
{
  return xrealloc (p, new);
}

static void
xfree2
  (void    *p,
   size_t old)
{
  xfree (p);
}

/* ------------------------------------------------------------------------ *
 *                     public functions: memory allocation                  *
 * ------------------------------------------------------------------------ */

/* The malloc () replacement returns 0-initialized space, 
   never ever returns NULL. */

void *
pmalloc 
  (size_t n)
{
  chunk      *q ;
  unsigned size ;
  if (n > mem_block_max)
    fatal_alloc ("%u bytes (too large a size)", n);
  size = n + (sizeof (chunk) - sizeof (void*)) ;
  if ((q = calloc (1, size)) == 0)
    fatal_alloc ("%u bytes", n);
  q->class  = _PUB_STORAGE ;
  q->length = size ;
  return &q->aligned ;
}

void *
vmalloc
  (size_t n)
{
  chunk      *q ;
  unsigned size ;
  if (n > mem_block_max)
    fatal_alloc ("%u private bytes (too large a size)", n);
  size = n + (sizeof (chunk) - sizeof (void*)) ;
  if ((q = calloc (1, size)) == 0)
    fatal_alloc ("%u private bytes", n);
  q->class  = _PRIVATE_MEM ;
  q->length = size ;
  return &q->aligned ;
}

void *
smalloc
  (size_t n)
{
  chunk      *q ;
  unsigned size ;
  if (n > mem_block_max) 
    fatal_alloc ("%u secure bytes (too large a size)", n);
  size = n + (sizeof (chunk) - sizeof (void*)) ;
  if ((q = calloc (1, size)) == 0)
    fatal_alloc ("%u secure bytes", n);
  q->class  = _SECURE_MEM ;
  q->length = size ;
  return &q->aligned ;
}

void preclassify (void *p) { reclassify (p, _PUB_STORAGE) ; }
void vreclassify (void *p) { reclassify (p, _PRIVATE_MEM) ; }
void sreclassify (void *p) { reclassify (p, _SECURE_MEM)  ; }

/* ------------------------------------------------------------------------ *
 *                public functions: any memory class                        *
 * ------------------------------------------------------------------------ */

void
_init_gmp2_alloc
  (void)
{
  if (_gmp2_alloc_flag)
    return ;

  /* set up custom memory manager */
  __enter_lock_semaphore () ;
  mp_set_memory_functions (smalloc, xrealloc2, xfree2);
  _gmp2_alloc_flag ++ ;
  __release_lock_semaphore () ;
}

unsigned
set_memblock_max
  (size_t new)
{
  unsigned l ;

  if (new < MEM_BLOCK_MIN_HARD)
    new = MEM_BLOCK_MIN_HARD ;
  else if (new > MEM_BLOCK_MAX_HARD)
    new = MEM_BLOCK_MAX_HARD ;

  __enter_lock_semaphore () ;
  l = mem_block_max ;
  mem_block_max = new ;
  __release_lock_semaphore () ;

  return l;
}


void *
xrealloc 
  (void  *p, 
   size_t n)
{
  chunk  *r = 0, *q ;
  unsigned size, osize ;

  if (p == 0) 
    fatal_realloc ("%u bytes at the NULL pointer", n);
  
  if (n > mem_block_max)
    fatal_realloc ("%u bytes (too large a size)", n);
  
  size = n + (sizeof (chunk) - sizeof (void*)) ;

  q = REVERT_FIELD_PTR (p, chunk, aligned) ;

  osize = q->length ;

  switch (q->class) {
  case _PUB_STORAGE :
    if ((r = realloc (q, size)) == 0)
      fatal_realloc ("%u bytes", n);
    break ;
    
  case _PRIVATE_MEM :
    if ((r = malloc (size)) == 0)
      fatal_realloc ("%u private bytes", n);
    memcpy (r, q, size > osize ? osize : size);
    memset (q, -1, q->length);  
    free (q);
    break ;

  case _SECURE_MEM :
    /* allocate a new memory block while cleaning the old one */
    if ((r = malloc (size)) == 0)
      fatal_realloc ("%u secure bytes", n);
    memcpy (r, q, size > osize ? osize : size);
    fast_random_bytes ((char*)q, osize);
    free (q);
    break ;

  default:
    fatal_realloc ("%u bytes (corrupt memory)", n);
  }

  if (osize < size)  /* initialize extended memory */
    memset (((char*)r) + osize, 0, size - osize);
  r->length = size ;
  return &r->aligned ;
}


void
xfree
  (void *p)
{
  chunk *q ;

  if (p == 0) {
    warning ("Attempt to free the NULL pointer", 0);
    return ;
  }

  q = REVERT_FIELD_PTR (p, chunk, aligned) ;

  switch (q->class) {
  case  _SECURE_MEM :
    fast_random_bytes ((char*)q, q->length);
    free (q);
    return ;
  case _PRIVATE_MEM :
    memset (q, -1, q->length);
  case _PUB_STORAGE :
    free (q);
    return ;
  }
  
  fatal ("Cannot free corrupt memory at 0x%u", (unsigned)p);
}