~ubuntu-branches/ubuntu/raring/libgcrypt11/raring

« back to all changes in this revision

Viewing changes to src/secmem.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2008-07-02 18:32:45 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 lenny)
  • Revision ID: james.westby@ubuntu.com-20080702183245-b1p9zumbhmq9wk4g
Tags: 1.4.1-1ubuntu1
* Merge from Debian unstable.
* Remaining Ubuntu changes:
  - Add libgcrypt11-udeb package.
  - Add clean-la.mk, and add a symlink for the .la
* Ubuntu changes dropped:
  - Build-Depends changes.
  - Drop patch 20_socket_nsl_linkage.diff, basically applied upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* secmem.c  -  memory allocation from a secure heap
2
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3
 
 *               2003 Free Software Foundation, Inc.
 
3
 *               2003, 2007 Free Software Foundation, Inc.
4
4
 *
5
5
 * This file is part of Libgcrypt.
6
6
 *
57
57
} memblock_t;
58
58
 
59
59
/* This flag specifies that the memory block is in use.  */
60
 
#define MB_FLAG_ACTIVE 1 << 0
 
60
#define MB_FLAG_ACTIVE (1 << 0)
61
61
 
62
62
/* The pool of secure memory.  */
63
63
static void *pool;
98
98
#define ADDR_TO_BLOCK(addr) \
99
99
  (memblock_t *) ((char *) addr - BLOCK_HEAD_SIZE)
100
100
 
101
 
/* Check wether MB is a valid block.  */
102
 
#define BLOCK_VALID(mb) \
103
 
  (((char *) mb - (char *) pool) < pool_size)
 
101
/* Check whether P points into the pool.  */
 
102
static int
 
103
ptr_into_pool_p (const void *p)
 
104
{
 
105
  /* We need to convert pointers to addresses.  This is required by
 
106
     C-99 6.5.8 to avoid undefined behaviour.  Using size_t is at
 
107
     least only implementation defined.  See also
 
108
     http://lists.gnupg.org/pipermail/gcrypt-devel/2007-February/001102.html
 
109
  */
 
110
  size_t p_addr = (size_t)p;
 
111
  size_t pool_addr = (size_t)pool;
 
112
 
 
113
  return p_addr >= pool_addr && p_addr <  pool_addr+pool_size;
 
114
}
104
115
 
105
116
/* Update the stats.  */
106
117
static void
126
137
 
127
138
  mb_next = (memblock_t *) ((char *) mb + BLOCK_HEAD_SIZE + mb->size);
128
139
  
129
 
  if (! BLOCK_VALID (mb_next))
 
140
  if (! ptr_into_pool_p (mb_next))
130
141
    mb_next = NULL;
131
142
 
132
143
  return mb_next;
182
193
{
183
194
  memblock_t *mb, *mb_split;
184
195
  
185
 
  for (mb = block; BLOCK_VALID (mb); mb = mb_get_next (mb))
 
196
  for (mb = block; ptr_into_pool_p (mb); mb = mb_get_next (mb))
186
197
    if (! (mb->flags & MB_FLAG_ACTIVE) && mb->size >= size)
187
198
      {
188
199
        /* Found a free block.  */
205
216
        break;
206
217
      }
207
218
 
208
 
  if (! BLOCK_VALID (mb))
 
219
  if (! ptr_into_pool_p (mb))
209
220
    mb = NULL;
210
221
 
211
222
  return mb;
329
340
init_pool (size_t n)
330
341
{
331
342
  size_t pgsize;
 
343
  long int pgsize_val;
332
344
  memblock_t *mb;
333
345
 
334
346
  pool_size = n;
336
348
  if (disable_secmem)
337
349
    log_bug ("secure memory is disabled");
338
350
 
339
 
#ifdef HAVE_GETPAGESIZE
340
 
  pgsize = getpagesize ();
 
351
#if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
 
352
  pgsize_val = sysconf (_SC_PAGESIZE);
 
353
#elif defined(HAVE_GETPAGESIZE)
 
354
  pgsize_val = getpagesize ();
341
355
#else
342
 
  pgsize = DEFAULT_PAGE_SIZE;
 
356
  pgsize_val = -1;
343
357
#endif
 
358
  pgsize = (pgsize_val != -1 && pgsize_val > 0)? pgsize_val:DEFAULT_PAGE_SIZE;
 
359
 
344
360
 
345
361
#if HAVE_MMAP
346
362
  pool_size = (pool_size + pgsize - 1) & ~(pgsize - 1);
360
376
    else
361
377
      {
362
378
        pool = mmap (0, pool_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
 
379
        close (fd);
363
380
      }
364
381
  }
365
382
#endif
477
494
 
478
495
  if (!pool_okay)
479
496
    {
480
 
      log_info (_
 
497
      log_bug (_
481
498
        ("operation is not possible without initialized secure memory\n"));
482
 
      exit (2);
483
499
    }
484
500
  if (show_warning && !suspend_warning)
485
501
    {
587
603
int
588
604
_gcry_private_is_secure (const void *p)
589
605
{
590
 
  return (pool_okay
591
 
          && p >= pool
592
 
          && p <  (void*)((char*)pool+pool_size));
 
606
  return pool_okay && ptr_into_pool_p (p);
593
607
}
594
608
 
595
609
 
639
653
  SECMEM_LOCK;
640
654
 
641
655
  for (i = 0, mb = (memblock_t *) pool;
642
 
       BLOCK_VALID (mb);
 
656
       ptr_into_pool_p (mb);
643
657
       mb = mb_get_next (mb), i++)
644
658
    log_info ("SECMEM: [%s] block: %i; size: %i\n",
645
659
              (mb->flags & MB_FLAG_ACTIVE) ? "used" : "free",