summaryrefslogtreecommitdiff
path: root/libpam/pam_malloc.c
blob: f899c28350ef1e1b7a8d1a1339b7f66f343f7ee7 (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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * $Id$
 */

/*
 * This pair of files helps to locate memory leaks. It is a wrapper for
 * the malloc family of calls. (Actutally, it currently only deals
 * with calloc, malloc, realloc, free, strdup and exit)
 *
 * To use these functions the header "pam_malloc.h" must be included
 * in all parts of the code (that use the malloc functions) and this
 * file must be linked with the result. The pam_malloc_flags can be
 * set from another function and determine the level of logging.
 *
 * The output is via the macros defined in _pam_macros.h
 *
 * It is a debugging tool and should be turned off in released code.
 *
 * This suite was written by Andrew Morgan <morgan@kernel.org> for
 * Linux-PAM.
 */

#ifndef DEBUG
#define DEBUG
#endif
#include "pam_private.h"

#include <security/pam_malloc.h>
#include <security/_pam_macros.h>

/* this must be done to stop infinite recursion! */
#undef malloc
#undef calloc
#undef free
#undef realloc
#undef exit
#undef strdup

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/*
 * default debugging level
 */

int pam_malloc_flags = PAM_MALLOC_ALL;
int pam_malloc_delay_length = 4;

#define on(x) ((pam_malloc_flags&(x))==(x))

/*
 * the implementation
 */

static const char *last_fn=NULL;
static const char *last_file=NULL;
static const char *last_call=NULL;
static int last_line = 1;

#define err(x) { _pam_output_xdebug_info(); _pam_output_debug x ; }

static void set_last_(const char *x, const char *f
		      , const char *fn, const int l)
{
     last_fn   = x  ? x : "error-in-pam_malloc..";
     last_file = f  ? f : "*bad-file*";
     last_call = fn ? fn: "*bad-fn*";
     last_line = l;
}

static void _pam_output_xdebug_info(void)
{
    FILE *logfile;
    int must_close = 1, fd;

#ifdef O_NOFOLLOW
    if ((fd = open(_PAM_LOGFILE, O_WRONLY|O_NOFOLLOW|O_APPEND)) != -1) {
#else
    if ((fd = open(_PAM_LOGFILE, O_WRONLY|O_APPEND)) != -1) {
#endif
	if (!(logfile = fdopen(fd,"a"))) {
	    logfile = stderr;
	    must_close = 0;
	    close(fd);
	}
    } else {
	logfile = stderr;
	must_close = 0;
    }
    fprintf(logfile, "[%s:%s(%d)->%s()] ",
           last_file, last_call, last_line, last_fn);
    fflush(logfile);
    if (must_close)
        fclose(logfile);
}

static void hinder(void)
{
     if (on(PAM_MALLOC_PAUSE)) {
	  if (on(0)) err(("pause requested"));
	  sleep(pam_malloc_delay_length);
     }

     if (on(PAM_MALLOC_STOP)) {
	  if (on(0)) err(("stop requested"));
	  exit(1);
     }
}

/*
 * here are the memory pointer registering functions.. these actually
 * use malloc(!) but that's ok! ;^)
 */

struct reference {
     void *ptr;          /* pointer */
     int nelements;      /* number of elements */
     int size;           /* - each of this size */
     char *file;         /* where it was requested - filename */
     char *function;     /*                        - function */
     int line;           /*                        - line number */
/*
 * linking info
 */
     struct reference *next;
};

static void _dump(const char *say, const struct reference *ref)
{
    _pam_output_debug(" <%s: %p (#%d of %d) req. by %s(); %s line %d>"
		      , say
		      , ref->ptr,ref->nelements,ref->size
		      , ref->function,ref->file,ref->line);
}

static struct reference *root=NULL;

static char *_strdup(const char *x)
{
     char *s;

     s = (char *)malloc(strlen(x)+1);
     if (s == NULL) {
	  if (on(0)) err(("_strdup failed"));
	  exit(1);
     }

     strcpy(s,x);
     return s;
}

static void add_new_ref(void *new, int n, int size)
{
     struct reference *ref=NULL;

     ref = (struct reference *) malloc( sizeof(struct reference) );
     if (new == NULL || ref == NULL) {
	  if (on(0)) err(("internal error {add_new_ref}"));
	  exit(1);
     }

     ref->ptr = new;
     ref->nelements = n;
     ref->size = size;

     ref->file = _strdup(last_file);
     ref->function = _strdup(last_call);
     ref->line = last_line;

     ref->next = root;

     if (on(PAM_MALLOC_REQUEST)) {
	  _dump("new_ptr", ref);
     }

     root = ref;
}

static void del_old_ref(void *old)
{
     struct reference *this,*last;

     if (old == NULL) {
	  if (on(0)) err(("internal error {del_old_ref}"));
	  exit(1);
     }

     /* locate old pointer */

     last = NULL;
     this = root;
     while (this) {
	  if (this->ptr == old)
	       break;
	  last = this;
	  this = this->next;
     }

     /* Did we find a reference ? */

     if (this) {
	  if (on(PAM_MALLOC_FREE)) {
	       _dump("free old_ptr", this);
	  }
	  if (last == NULL) {
	       root = this->next;
	  } else {
	       last->next = this->next;
	  }
	  free(this->file);
	  free(this->function);
	  free(this);
     } else {
	  if (on(0)) err(("ERROR!: bad memory"));
	  hinder();
     }
}

static void verify_old_ref(void *old)
{
     struct reference *this;

     if (old == NULL) {
	  if (on(0)) err(("internal error {verify_old_ref}"));
	  exit(1);
     }

     /* locate old pointer */

     this = root;
     while (this) {
	  if (this->ptr == old)
	       break;
	  this = this->next;
     }

     /* Did we find a reference ? */

     if (this) {
	  if (on(PAM_MALLOC_VERIFY)) {
	       _dump("verify_ptr", this);
	  }
     } else {
	  if (on(0)) err(("ERROR!: bad request"));
	  hinder();
     }
}

static void dump_memory_list(const char *dump)
{
     struct reference *this;

     this = root;
     if (this) {
	  if (on(0)) err(("un-free()'d memory"));
	  while (this) {
	       _dump(dump, this);
	       this = this->next;
	  }
     } else {
	  if (on(0)) err(("no memory allocated"));
     }
}

/* now for the wrappers */

#define _fn(x)  set_last_(x,file,fn,line)

void *pam_malloc(size_t size, const char *file, const char *fn, const int line)
{
     void *new;

     _fn("malloc");

     if (on(PAM_MALLOC_FUNC)) err(("request for %d", size));

     new = malloc(size);
     if (new == NULL) {
	  if (on(PAM_MALLOC_FAIL)) err(("returned NULL"));
     } else {
	  if (on(PAM_MALLOC_REQUEST)) err(("request new"));
	  add_new_ref(new, 1, size);
     }

     return new;
}

void *pam_calloc(size_t nelm, size_t size
		, const char *file, const char *fn, const int line)
{
     void *new;

     _fn("calloc");

     if (on(PAM_MALLOC_FUNC)) err(("request for %d of %d", nelm, size));

     new = calloc(nelm,size);
     if (new == NULL) {
	  if (on(PAM_MALLOC_FAIL)) err(("returned NULL"));
     } else {
	  if (on(PAM_MALLOC_REQUEST)) err(("request new"));
	  add_new_ref(new, nelm, size);
     }

     return new;
}

void  pam_free(void *ptr
	      , const char *file, const char *fn, const int line)
{
     _fn("free");

     if (on(PAM_MALLOC_FUNC))
	 err(("request (%s:%s():%d) to free %p", file, fn, line, ptr));

     if (ptr == NULL) {
	  if (on(PAM_MALLOC_NULL)) err(("passed NULL pointer"));
     } else {
	  if (on(PAM_MALLOC_FREE)) err(("deleted old"));
	  del_old_ref(ptr);
	  free(ptr);
     }
}

void *pam_realloc(void *ptr, size_t size
		, const char *file, const char *fn, const int line)
{
     void *new;

     _fn("realloc");

     if (on(PAM_MALLOC_FUNC)) err(("resize %p to %d", ptr, size));

     if (ptr == NULL) {
	  if (on(PAM_MALLOC_NULL)) err(("passed NULL pointer"));
     } else {
	  verify_old_ref(ptr);
     }

     new = realloc(ptr, size);
     if (new == NULL) {
	  if (on(PAM_MALLOC_FAIL)) err(("returned NULL"));
     } else {
	  if (ptr) {
	       if (on(PAM_MALLOC_FREE)) err(("deleted old"));
	       del_old_ref(ptr);
	  } else {
	       if (on(PAM_MALLOC_NULL)) err(("old is NULL"));
	  }
	  if (on(PAM_MALLOC_REQUEST)) err(("request new"));
	  add_new_ref(new, 1, size);
     }

     return new;
}

void pam_exit(int i
	      , const char *file, const char *fn, const int line)
{
     D(("time to exit"));

     _fn("exit");

     if (on(0)) err(("passed (%d)", i));
     if (on(PAM_MALLOC_LEAKED)) {
	  dump_memory_list("leaked");
     }
     exit(i);
}

char *pam_strdup(const char *orig,
		 const char *file, const char *fn, const int line)
{
    char *new;

    _fn("strdup");

    if (on(PAM_MALLOC_FUNC)) err(("request for dup of [%s]", orig));

    new = strdup(orig);
    if (new == NULL) {
	if (on(PAM_MALLOC_FAIL)) err(("returned NULL"));
    } else {
	if (on(PAM_MALLOC_REQUEST)) err(("request dup of [%s]", orig));
	add_new_ref(new, 1, strlen(new)+1);
    }

    return new;
}

/* end of file */