summaryrefslogtreecommitdiffstats
path: root/src/exec_preload.c
blob: f82de7ebe94d6d3bcdba73028ab030d8aad113dc (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
/*
 * SPDX-License-Identifier: ISC
 *
 * Copyright (c) 2009-2022 Todd C. Miller <Todd.Miller@sudo.ws>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
 */

#include <config.h>

#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "sudo.h"
#include "sudo_exec.h"
#include "sudo_util.h"

#ifdef RTLD_PRELOAD_VAR
typedef void * (*sudo_alloc_fn_t)(size_t, size_t);
typedef void (*sudo_free_fn_t)(void *);

static void *
sudo_allocarray(size_t nmemb, size_t size)
{
    return reallocarray(NULL, nmemb, size);
}

/*
 * Allocate space for the string described by fmt and return it,
 * or NULL on error.
 * Currently only supports %%, %c, %d, and %s escapes.
 */
static char *
fmtstr(sudo_alloc_fn_t alloc_fn, sudo_free_fn_t free_fn, const char *ofmt, ...)
{
    char *cp, *cur, *newstr = NULL;
    size_t len, size = 1;
    const char *fmt;
    va_list ap;
    debug_decl(fmtstr, SUDO_DEBUG_UTIL);

    /* Determine size. */
    va_start(ap, ofmt);
    for (fmt = ofmt; *fmt != '\0'; ) {
	if (fmt[0] == '%') {
	    switch (fmt[1]) {
	    case 'c':
		(void)va_arg(ap, int);
		FALLTHROUGH;
	    case '%':
		size++;
		fmt += 2;
		continue;
	    case 's':
		cp = va_arg(ap, char *);
		size += strlen(cp ? cp : "(NULL)");
		fmt += 2;
		continue;
	    case 'd': {
		char numbuf[(((sizeof(int) * 8) + 2) / 3) + 2];
		len = snprintf(numbuf, sizeof(numbuf), "%d", va_arg(ap, int));
		if (len >= sizeof(numbuf)) {
		    goto oflow;
		}
		size += len;
		fmt += 2;
		continue;
	    }
	    default:
		/* Treat as literal. */
		break;
	    }
	}
	size++;
	fmt++;
    }
    va_end(ap);

    newstr = alloc_fn(1, size);
    if (newstr == NULL)
	debug_return_str(NULL);

    /* Format/copy data. */
    cur = newstr;
    va_start(ap, ofmt);
    for (fmt = ofmt; *fmt != '\0'; ) {
	if (fmt[0] == '%') {
	    switch (fmt[1]) {
	    case '%':
		if (size < 2) {
		    goto oflow;
		}
		*cur++ = '%';
		size--;
		fmt += 2;
		continue;
	    case 'c':
		if (size < 2) {
		    goto oflow;
		}
		*cur++ = va_arg(ap, int);
		size--;
		fmt += 2;
		continue;
	    case 's':
		cp = va_arg(ap, char *);
		len = strlcpy(cur, cp ? cp : "(NULL)", size);
		if (len >= size) {
		    goto oflow;
		}
		cur += len;
		size -= len;
		fmt += 2;
		continue;
	    case 'd':
		len = snprintf(cur, size, "%d", va_arg(ap, int));
		if (len >= size) {
		    goto oflow;
		}
		cur += len;
		size -= len;
		fmt += 2;
		continue;
	    default:
		/* Treat as literal. */
		break;
	    }
	}
	if (size < 2) {
	    goto oflow;
	}
	*cur++ = *fmt++;
	size++;
    }

    if (size < 1) {
	goto oflow;
    }
    *cur = '\0';
    va_end(ap);

    debug_return_str(newstr);

oflow:
    /* We pre-allocate enough space, so this should never happen. */
    va_end(ap);
    free_fn(newstr);
    sudo_warnx(U_("internal error, %s overflow"), __func__);
    debug_return_str(NULL);
}

/*
 * Add a DSO file to LD_PRELOAD or the system equivalent.
 */
static char **
sudo_preload_dso_alloc(char *const envp[], const char *dso_file,
    int intercept_fd, sudo_alloc_fn_t alloc_fn, sudo_free_fn_t free_fn)
{
    char *preload = NULL;
    char **nep, **nenvp = NULL;
    char *const *ep;
    char **preload_ptr = NULL;
    char **intercept_ptr = NULL;
    char *const empty[1] = { NULL };
    bool fd_present = false;
    bool dso_present = false;
# ifdef RTLD_PRELOAD_ENABLE_VAR
    bool dso_enabled = false;
# else
    const bool dso_enabled = true;
# endif
# ifdef _PATH_ASAN_LIB
    char *dso_buf = NULL;
# endif
    size_t env_size;
    debug_decl(sudo_preload_dso_alloc, SUDO_DEBUG_UTIL);

# ifdef _PATH_ASAN_LIB
    /*
     * The address sanitizer DSO needs to be first in the list.
     */
    dso_buf = fmtstr(alloc_fn, free_fn, "%s%c%s", _PATH_ASAN_LIB,
	RTLD_PRELOAD_DELIM, dso_file);
    if (dso_buf == NULL) {
	goto oom;
    }
    dso_file = dso_buf;
# endif

    /*
     * Preload a DSO file.  For a list of LD_PRELOAD-alikes, see
     * http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html
     * XXX - need to support 32-bit and 64-bit variants
     */

    /* Treat a NULL envp as empty, thanks Linux. */
    if (envp == NULL)
	envp = empty;

    /* Determine max size for new envp. */
    for (env_size = 0; envp[env_size] != NULL; env_size++)
	continue;
    if (!dso_enabled)
	env_size++;
    if (intercept_fd != -1)
	env_size++;
    env_size += 2;	/* dso_file + terminating NULL */

    /* Allocate new envp. */
    nenvp = alloc_fn(env_size, sizeof(*nenvp));
    if (nenvp == NULL)
	goto oom;

    /*
     * Shallow copy envp, with special handling for RTLD_PRELOAD_VAR,
     * RTLD_PRELOAD_ENABLE_VAR and SUDO_INTERCEPT_FD.
     */
    for (ep = envp, nep = nenvp; *ep != NULL; ep++) {
	if (strncmp(*ep, RTLD_PRELOAD_VAR "=", sizeof(RTLD_PRELOAD_VAR)) == 0) {
	    const char *cp = *ep + sizeof(RTLD_PRELOAD_VAR);
	    const size_t dso_len = strlen(dso_file);

	    /* Skip duplicates. */
	    if (preload_ptr != NULL)
		continue;

	    /*
	     * Check to see if dso_file is already first in the list.
	     * We don't bother checking for it later in the list.
	     */
	    if (strncmp(cp, dso_file, dso_len) == 0) {
		if (cp[dso_len] == '\0' || cp[dso_len] == RTLD_PRELOAD_DELIM)
		    dso_present = true;
	    }

	    /* Save pointer to LD_PRELOAD variable. */
	    preload_ptr = nep;

	    goto copy;
	}
	if (intercept_fd != -1 && strncmp(*ep, "SUDO_INTERCEPT_FD=",
		sizeof("SUDO_INTERCEPT_FD=") - 1) == 0) {
	    const char *cp = *ep + sizeof("SUDO_INTERCEPT_FD=") - 1;
	    const char *errstr;
	    int fd;

	    /* Skip duplicates. */
	    if (intercept_ptr != NULL)
		continue;

	    fd = sudo_strtonum(cp, 0, INT_MAX, &errstr);
	    if (fd == intercept_fd && errstr == NULL)
		fd_present = true;

	    /* Save pointer to SUDO_INTERCEPT_FD variable. */
	    intercept_ptr = nep;

	    goto copy;
	}
# ifdef RTLD_PRELOAD_ENABLE_VAR
	if (strncmp(*ep, RTLD_PRELOAD_ENABLE_VAR "=",
		sizeof(RTLD_PRELOAD_ENABLE_VAR)) == 0) {
	    dso_enabled = true;
	}
# endif
copy:
	*nep++ = *ep;	/* shallow copy */
    }

    /* Prepend our LD_PRELOAD to existing value or add new entry at the end. */
    if (!dso_present) {
	if (preload_ptr == NULL) {
# ifdef RTLD_PRELOAD_DEFAULT
	    preload = fmtstr(alloc_fn, free_fn, "%s=%s%c%s", RTLD_PRELOAD_VAR,
		dso_file, RTLD_PRELOAD_DELIM, RTLD_PRELOAD_DEFAULT);
	    if (preload == NULL) {
		goto oom;
	    }
# else
	    preload = fmtstr(alloc_fn, free_fn, "%s=%s", RTLD_PRELOAD_VAR,
		dso_file);
	    if (preload == NULL) {
		goto oom;
	    }
# endif
	    *nep++ = preload;
	} else {
	    const char *old_val = *preload_ptr + sizeof(RTLD_PRELOAD_VAR);
	    preload = fmtstr(alloc_fn, free_fn, "%s=%s%c%s", RTLD_PRELOAD_VAR,
		dso_file, RTLD_PRELOAD_DELIM, old_val);
	    if (preload == NULL) {
		goto oom;
	    }
	    *preload_ptr = preload;
	}
    }
# ifdef RTLD_PRELOAD_ENABLE_VAR
    if (!dso_enabled) {
	*nenvp++ = RTLD_PRELOAD_ENABLE_VAR "=";
    }
# endif
    if (!fd_present && intercept_fd != -1) {
	char *fdstr = fmtstr(alloc_fn, free_fn, "SUDO_INTERCEPT_FD=%d",
	    intercept_fd);
	if (fdstr == NULL) {
	    goto oom;
	}
	if (intercept_ptr != NULL) {
	    *intercept_ptr = fdstr;
	} else {
	    *nep++ = fdstr;
	}
    }

    /* NULL terminate nenvp at last. */
    *nep = NULL;

# ifdef _PATH_ASAN_LIB
    free_fn(dso_buf);
# endif

    debug_return_ptr(nenvp);
oom:
    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
# ifdef _PATH_ASAN_LIB
    free_fn(dso_buf);
# endif
    free_fn(preload);
    free_fn(nenvp);
    debug_return_ptr(NULL);
}

char **
sudo_preload_dso_mmap(char *const envp[], const char *dso_file,
    int intercept_fd)
{
    return sudo_preload_dso_alloc(envp, dso_file, intercept_fd,
	sudo_mmap_allocarray_v1, sudo_mmap_free_v1);
}

char **
sudo_preload_dso(char *const envp[], const char *dso_file,
    int intercept_fd)
{
    return sudo_preload_dso_alloc(envp, dso_file, intercept_fd,
	sudo_allocarray, free);
}
#endif /* RTLD_PRELOAD_VAR */