1 /*
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2016 Todd C. Miller <Todd.Miller@sudo.ws>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * This is an open source non-commercial project. Dear PVS-Studio, please check it.
21  * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
22  */
23 
24 #include <config.h>
25 
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "sudoers.h"
30 
31 struct sudoers_gc_entry {
32     SLIST_ENTRY(sudoers_gc_entry) entries;
33     enum sudoers_gc_types type;
34     union {
35 	char **vec;
36 	void *ptr;
37     } u;
38 };
39 SLIST_HEAD(sudoers_gc_list, sudoers_gc_entry);
40 #ifdef NO_LEAKS
41 static struct sudoers_gc_list sudoers_gc_list =
42     SLIST_HEAD_INITIALIZER(sudoers_gc_list);
43 #endif
44 
45 bool
sudoers_gc_add(enum sudoers_gc_types type,void * v)46 sudoers_gc_add(enum sudoers_gc_types type, void *v)
47 {
48 #ifdef NO_LEAKS
49     struct sudoers_gc_entry *gc;
50     debug_decl(sudoers_gc_add, SUDOERS_DEBUG_UTIL);
51 
52     if (v == NULL)
53 	debug_return_bool(false);
54 
55     gc = calloc(1, sizeof(*gc));
56     if (gc == NULL) {
57 	sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
58 	debug_return_bool(false);
59     }
60     switch (type) {
61     case GC_PTR:
62 	gc->u.ptr = v;
63 	break;
64     case GC_VECTOR:
65 	gc->u.vec = v;
66 	break;
67     default:
68 	free(gc);
69 	sudo_warnx("unexpected garbage type %d", type);
70 	debug_return_bool(false);
71     }
72     gc->type = type;
73     SLIST_INSERT_HEAD(&sudoers_gc_list, gc, entries);
74     debug_return_bool(true);
75 #else
76     return true;
77 #endif /* NO_LEAKS */
78 }
79 
80 bool
sudoers_gc_remove(enum sudoers_gc_types type,void * v)81 sudoers_gc_remove(enum sudoers_gc_types type, void *v)
82 {
83 #ifdef NO_LEAKS
84     struct sudoers_gc_entry *gc, *prev = NULL;
85     debug_decl(sudoers_gc_remove, SUDOERS_DEBUG_UTIL);
86 
87     if (v == NULL)
88 	debug_return_bool(false);
89 
90     SLIST_FOREACH(gc, &sudoers_gc_list, entries) {
91 	switch (gc->type) {
92 	case GC_PTR:
93 	    if (gc->u.ptr == v)
94 	    	goto found;
95 	    break;
96 	case GC_VECTOR:
97 	    if (gc->u.vec == v)
98 	    	goto found;
99 	    break;
100 	default:
101 	    sudo_warnx("unexpected garbage type %d in %p", gc->type, gc);
102 	}
103 	prev = gc;
104     }
105     /* If this happens, there is a bug in the g/c code. */
106     sudo_warnx("%s: unable to find %p, type %d", __func__, v, type);
107 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
108     abort();
109 #else
110     debug_return_bool(false);
111 #endif
112 found:
113     if (prev == NULL)
114 	SLIST_REMOVE_HEAD(&sudoers_gc_list, entries);
115     else
116 	SLIST_REMOVE_AFTER(prev, entries);
117     free(gc);
118 
119     debug_return_bool(true);
120 #else
121     return true;
122 #endif /* NO_LEAKS */
123 }
124 
125 void
sudoers_gc_run(void)126 sudoers_gc_run(void)
127 {
128 #ifdef NO_LEAKS
129     struct sudoers_gc_entry *gc;
130     char **cur;
131     debug_decl(sudoers_gc_run, SUDOERS_DEBUG_UTIL);
132 
133     /* Collect garbage. */
134     while ((gc = SLIST_FIRST(&sudoers_gc_list)) != NULL) {
135 	SLIST_REMOVE_HEAD(&sudoers_gc_list, entries);
136 	switch (gc->type) {
137 	case GC_PTR:
138 	    free(gc->u.ptr);
139 	    free(gc);
140 	    break;
141 	case GC_VECTOR:
142 	    for (cur = gc->u.vec; *cur != NULL; cur++)
143 		free(*cur);
144 	    free(gc->u.vec);
145 	    free(gc);
146 	    break;
147 	default:
148 	    sudo_warnx("unexpected garbage type %d", gc->type);
149 	}
150     }
151 
152     debug_return;
153 #endif /* NO_LEAKS */
154 }
155 
156 #ifndef notyet
157 void
sudoers_gc_init(void)158 sudoers_gc_init(void)
159 {
160 #ifdef NO_LEAKS
161     atexit(sudoers_gc_run);
162 #endif
163 }
164 #endif
165