summaryrefslogtreecommitdiff
path: root/tests/t-secmem.c
blob: a6557dce3f99fc98d44721e69aef75cdc0be4a96 (plain)
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
/* t-secmem.c - Test the secmem memory allocator
 * Copyright (C) 2016 g10 Code GmbH
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#define PGM "t-secmem"

#include "t-common.h"
#include "../src/gcrypt-testapi.h"


#define DEFAULT_PAGE_SIZE 4096
#define MINIMUM_POOL_SIZE 16384
static size_t pool_size;
static size_t chunk_size;
static int in_fips_mode;

static void
test_secmem (void)
{
  void *a[28];
  void *b;
  int i;
  int i_max;

  memset (a, 0, sizeof a);

  /* Allocating 28*512=14k should work in the default 16k pool even
   * with extra alignment requirements.
   */
  i_max = DIM(a);

  for (i=0; i < i_max; i++)
    a[i] = gcry_xmalloc_secure (chunk_size);

  /* Allocating another 2k should fail for the default 16k pool.  */
  b = gcry_malloc_secure (chunk_size*4);
  if (b)
    fail ("allocation did not fail as expected\n");

  for (i=0; i < DIM(a); i++)
    xfree (a[i]);
  xfree (b);
}


static void
test_secmem_overflow (void)
{
  void *a[150];
  int i;

  memset (a, 0, sizeof a);

  /* Allocating 150*512=75k should require more than one overflow buffer.  */
  for (i=0; i < DIM(a); i++)
    {
      a[i] = gcry_xmalloc_secure (chunk_size);
      if (verbose && !(i %40))
        xgcry_control ((GCRYCTL_DUMP_SECMEM_STATS, 0 , 0));
    }

  if (debug)
    xgcry_control ((PRIV_CTL_DUMP_SECMEM_STATS, 0 , 0));
  if (verbose)
    xgcry_control ((GCRYCTL_DUMP_SECMEM_STATS, 0 , 0));
  for (i=0; i < DIM(a); i++)
    xfree (a[i]);
}


/* This function is called when we ran out of core and there is no way
 * to return that error to the caller (xmalloc or mpi allocation).  */
static int
outofcore_handler (void *opaque, size_t req_n, unsigned int flags)
{
  static int been_here;  /* Used to protect against recursive calls. */

  (void)opaque;

  /* Protect against a second call.  */
  if (been_here)
    return 0; /* Let libgcrypt call its own fatal error handler.  */
  been_here = 1;

  info ("outofcore handler invoked");
  xgcry_control ((PRIV_CTL_DUMP_SECMEM_STATS, 0 , 0));
  fail ("out of core%s while allocating %lu bytes",
       (flags & 1)?" in secure memory":"", (unsigned long)req_n);

  die ("stopped");
  /*NOTREACHED*/
  return 0;
}


int
main (int argc, char **argv)
{
  int last_argc = -1;
  long int pgsize_val = -1;
  size_t pgsize;

  if (getenv ("GCRYPT_IN_ASAN_TEST"))
    {
      /* 'mlock' is not available when build with address sanitizer,
       * so skip test. */
      fputs ("Note: " PGM " skipped because running with ASAN.\n", stdout);
      return 0;
    }

#if HAVE_MMAP
# if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
  pgsize_val = sysconf (_SC_PAGESIZE);
# elif defined(HAVE_GETPAGESIZE)
  pgsize_val = getpagesize ();
# endif
#endif
  pgsize = (pgsize_val > 0)? pgsize_val : DEFAULT_PAGE_SIZE;

  pool_size = (MINIMUM_POOL_SIZE + pgsize - 1) & ~(pgsize - 1);
  chunk_size = pool_size / 32;

  if (argc)
    { argc--; argv++; }

  while (argc && last_argc != argc )
    {
      last_argc = argc;
      if (!strcmp (*argv, "--"))
        {
          argc--; argv++;
          break;
        }
      else if (!strcmp (*argv, "--help"))
        {
          fputs ("usage: " PGM " [options]\n"
                 "Options:\n"
                 "  --verbose       print timings etc.\n"
                 "  --debug         flyswatter\n"
                 , stdout);
          exit (0);
        }
      else if (!strcmp (*argv, "--verbose"))
        {
          verbose++;
          argc--; argv++;
        }
      else if (!strcmp (*argv, "--debug"))
        {
          verbose += 2;
          debug++;
          argc--; argv++;
        }
      else if (!strncmp (*argv, "--", 2))
        die ("unknown option '%s'", *argv);
    }

  if (!gcry_check_version (GCRYPT_VERSION))
    die ("version mismatch; pgm=%s, library=%s\n",
         GCRYPT_VERSION, gcry_check_version (NULL));
  if (debug)
    xgcry_control ((GCRYCTL_SET_DEBUG_FLAGS, 1u , 0));
  xgcry_control ((GCRYCTL_ENABLE_QUICK_RANDOM, 0));
  xgcry_control ((GCRYCTL_INIT_SECMEM, pool_size, 0));
  /* This is ignored in FIPS Mode */
  gcry_set_outofcore_handler (outofcore_handler, NULL);
  xgcry_control ((GCRYCTL_INITIALIZATION_FINISHED, 0));
  if (gcry_fips_mode_active ())
    in_fips_mode = 1;

  /* Libgcrypt prints a warning when the first overflow is allocated;
   * we do not want to see that.  */
  if (!verbose)
    xgcry_control ((GCRYCTL_DISABLE_SECMEM_WARN, 0));


  test_secmem ();
  if (!in_fips_mode)
    test_secmem_overflow ();
  /* FIXME: We need to improve the tests, for example by registering
   * our own log handler and comparing the output of
   * PRIV_CTL_DUMP_SECMEM_STATS to expected pattern.  */

  if (verbose)
    {
      xgcry_control ((PRIV_CTL_DUMP_SECMEM_STATS, 0 , 0));
      xgcry_control ((GCRYCTL_DUMP_SECMEM_STATS, 0 , 0));
    }

  info ("All tests completed.  Errors: %d\n", error_count);
  xgcry_control ((GCRYCTL_TERM_SECMEM, 0 , 0));
  return !!error_count;
}