summaryrefslogtreecommitdiffstats
path: root/src/load_plugins.c
blob: 8cac77b8a2b9d6eda9ba35700b966d9f7c964710 (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
/*
 * Copyright (c) 2009-2018 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 <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRING_H
# include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif /* HAVE_STRINGS_H */
#include <unistd.h>
#include <errno.h>

#include "sudo.h"
#include "sudo_plugin.h"
#include "sudo_plugin_int.h"
#include "sudo_dso.h"

/* We always use the same name for the sudoers plugin, regardless of the OS */
#define SUDOERS_PLUGIN	"sudoers.so"

#ifdef ENABLE_SUDO_PLUGIN_API
static int
sudo_stat_plugin(struct plugin_info *info, char *fullpath,
    size_t pathsize, struct stat *sb)
{
    int status = -1;
    debug_decl(sudo_stat_plugin, SUDO_DEBUG_PLUGIN)

    if (info->path[0] == '/') {
	if (strlcpy(fullpath, info->path, pathsize) >= pathsize) {
	    sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
		_PATH_SUDO_CONF, info->lineno, info->symbol_name);
	    sudo_warnx(U_("%s: %s"), info->path, strerror(ENAMETOOLONG));
	    goto done;
	}
	status = stat(fullpath, sb);
    } else {
	int len;

#ifdef STATIC_SUDOERS_PLUGIN
	/* Check static symbols. */
	if (strcmp(info->path, SUDOERS_PLUGIN) == 0) {
	    if (strlcpy(fullpath, info->path, pathsize) >= pathsize) {
		sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
		    _PATH_SUDO_CONF, info->lineno, info->symbol_name);
		sudo_warnx(U_("%s: %s"), info->path, strerror(ENAMETOOLONG));
		goto done;
	    }
	    /* Plugin is static, fake up struct stat. */
	    memset(sb, 0, sizeof(*sb));
	    sb->st_uid = ROOT_UID;
	    sb->st_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
	    status = 0;
	    goto done;
	}
#endif /* STATIC_SUDOERS_PLUGIN */

	if (sudo_conf_plugin_dir_path() == NULL) {
	    errno = ENOENT;
	    goto done;
	}

	len = snprintf(fullpath, pathsize, "%s%s", sudo_conf_plugin_dir_path(),
	    info->path);
	if (len <= 0 || (size_t)len >= pathsize) {
	    sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
		_PATH_SUDO_CONF, info->lineno, info->symbol_name);
	    sudo_warnx(U_("%s%s: %s"), sudo_conf_plugin_dir_path(), info->path,
		strerror(ENAMETOOLONG));
	    goto done;
	}
	/* Try parent dir for compatibility with old plugindir default. */
	if ((status = stat(fullpath, sb)) != 0) {
	    char *cp = strrchr(fullpath, '/');
	    if (cp > fullpath + 4 && cp[-5] == '/' && cp[-4] == 's' &&
		cp[-3] == 'u' && cp[-2] == 'd' && cp[-1] == 'o') {
		int serrno = errno;
		strlcpy(cp - 4, info->path, pathsize - (cp - 4 - fullpath));
		if ((status = stat(fullpath, sb)) != 0)
		    errno = serrno;
	    }
	}
    }
done:
    debug_return_int(status);
}

static bool
sudo_check_plugin(struct plugin_info *info, char *fullpath, size_t pathsize)
{
    struct stat sb;
    bool ret = false;
    debug_decl(sudo_check_plugin, SUDO_DEBUG_PLUGIN)

    if (sudo_stat_plugin(info, fullpath, pathsize, &sb) != 0) {
	sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
	    _PATH_SUDO_CONF, info->lineno, info->symbol_name);
	sudo_warn("%s%s",
	    sudo_conf_plugin_dir_path() ? sudo_conf_plugin_dir_path() : "",
	    info->path);
	goto done;
    }
    if (sb.st_uid != ROOT_UID) {
	sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
	    _PATH_SUDO_CONF, info->lineno, info->symbol_name);
	sudo_warnx(U_("%s must be owned by uid %d"), fullpath, ROOT_UID);
	goto done;
    }
    if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
	sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
	    _PATH_SUDO_CONF, info->lineno, info->symbol_name);
	sudo_warnx(U_("%s must be only be writable by owner"), fullpath);
	goto done;
    }
    ret = true;

done:
    debug_return_bool(ret);
}
#else
static bool
sudo_check_plugin(struct plugin_info *info, char *fullpath, size_t pathsize)
{
    debug_decl(sudo_check_plugin, SUDO_DEBUG_PLUGIN)
    (void)strlcpy(fullpath, info->path, pathsize);
    debug_return_bool(true);
}
#endif /* ENABLE_SUDO_PLUGIN_API */

/*
 * Load the plugin specified by "info".
 */
static bool
sudo_load_plugin(struct plugin_container *policy_plugin,
    struct plugin_container_list *io_plugins, struct plugin_info *info)
{
    struct plugin_container *container = NULL;
    struct generic_plugin *plugin;
    char path[PATH_MAX];
    void *handle = NULL;
    debug_decl(sudo_load_plugin, SUDO_DEBUG_PLUGIN)

    /* Sanity check plugin and fill in path */
    if (!sudo_check_plugin(info, path, sizeof(path)))
	goto bad;

    /* Open plugin and map in symbol */
    handle = sudo_dso_load(path, SUDO_DSO_LAZY|SUDO_DSO_GLOBAL);
    if (!handle) {
	const char *errstr = sudo_dso_strerror();
	sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
	    _PATH_SUDO_CONF, info->lineno, info->symbol_name);
	sudo_warnx(U_("unable to load %s: %s"), path,
	    errstr ? errstr : "unknown error");
	goto bad;
    }
    plugin = sudo_dso_findsym(handle, info->symbol_name);
    if (!plugin) {
	sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
	    _PATH_SUDO_CONF, info->lineno, info->symbol_name);
	sudo_warnx(U_("unable to find symbol \"%s\" in %s"), info->symbol_name, path);
	goto bad;
    }

    if (plugin->type != SUDO_POLICY_PLUGIN && plugin->type != SUDO_IO_PLUGIN) {
	sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
	    _PATH_SUDO_CONF, info->lineno, info->symbol_name);
	sudo_warnx(U_("unknown policy type %d found in %s"), plugin->type, path);
	goto bad;
    }
    if (SUDO_API_VERSION_GET_MAJOR(plugin->version) != SUDO_API_VERSION_MAJOR) {
	sudo_warnx(U_("error in %s, line %d while loading plugin \"%s\""),
	    _PATH_SUDO_CONF, info->lineno, info->symbol_name);
	sudo_warnx(U_("incompatible plugin major version %d (expected %d) found in %s"),
	    SUDO_API_VERSION_GET_MAJOR(plugin->version),
	    SUDO_API_VERSION_MAJOR, path);
	goto bad;
    }
    if (plugin->type == SUDO_POLICY_PLUGIN) {
	if (policy_plugin->handle != NULL) {
	    /* Ignore duplicate entries. */
	    if (strcmp(policy_plugin->name, info->symbol_name) != 0) {
		sudo_warnx(U_("ignoring policy plugin \"%s\" in %s, line %d"),
		    info->symbol_name, _PATH_SUDO_CONF, info->lineno);
		sudo_warnx(U_("only a single policy plugin may be specified"));
		goto bad;
	    }
	    sudo_warnx(U_("ignoring duplicate policy plugin \"%s\" in %s, line %d"),
		info->symbol_name, _PATH_SUDO_CONF, info->lineno);
	    goto bad;
	}
	policy_plugin->handle = handle;
	policy_plugin->path = strdup(path);
	if (policy_plugin->path == NULL) {
	    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
	    goto bad;
	}
	policy_plugin->name = info->symbol_name;
	policy_plugin->options = info->options;
	policy_plugin->debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER;
	policy_plugin->u.generic = plugin;
	policy_plugin->debug_files = sudo_conf_debug_files(path);
    } else if (plugin->type == SUDO_IO_PLUGIN) {
	/* Check for duplicate entries. */
	TAILQ_FOREACH(container, io_plugins, entries) {
	    if (strcmp(container->name, info->symbol_name) == 0) {
		sudo_warnx(U_("ignoring duplicate I/O plugin \"%s\" in %s, line %d"),
		    info->symbol_name, _PATH_SUDO_CONF, info->lineno);
		sudo_dso_unload(handle);
		handle = NULL;
		break;
	    }
	}
	container = calloc(1, sizeof(*container));
	if (container == NULL || (container->path = strdup(path)) == NULL) {
	    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
	    goto bad;
	}
	container->handle = handle;
	container->name = info->symbol_name;
	container->options = info->options;
	container->debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER;
	container->u.generic = plugin;
	container->debug_files = sudo_conf_debug_files(path);
	TAILQ_INSERT_TAIL(io_plugins, container, entries);
    }

    /* Zero out info strings that we now own (see above). */
    info->symbol_name = NULL;
    info->options = NULL;

    debug_return_bool(true);
bad:
    free(container);
    if (handle != NULL)
	sudo_dso_unload(handle);
    debug_return_bool(false);
}

static void
free_plugin_info(struct plugin_info *info)
{
    free(info->path);
    free(info->symbol_name);
    if (info->options != NULL) {
	int i = 0;
	while (info->options[i] != NULL)
	    free(info->options[i++]);
	free(info->options);
    }
    free(info);
}

/*
 * Load the plugins listed in sudo.conf.
 */
bool
sudo_load_plugins(struct plugin_container *policy_plugin,
    struct plugin_container_list *io_plugins)
{
    struct plugin_container *container;
    struct plugin_info_list *plugins;
    struct plugin_info *info, *next;
    bool ret = false;
    debug_decl(sudo_load_plugins, SUDO_DEBUG_PLUGIN)

    /* Walk the plugin list from sudo.conf, if any and free it. */
    plugins = sudo_conf_plugins();
    TAILQ_FOREACH_SAFE(info, plugins, entries, next) {
	ret = sudo_load_plugin(policy_plugin, io_plugins, info);
	if (!ret)
	    goto done;
	free_plugin_info(info);
    }
    TAILQ_INIT(plugins);

    /*
     * If no policy plugin, fall back to the default (sudoers).
     * If there is also no I/O log plugin, use sudoers for that too.
     */
    if (policy_plugin->handle == NULL) {
	/* Default policy plugin */
	info = calloc(1, sizeof(*info));
	if (info == NULL) {
	    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
	    goto done;
	}
	info->symbol_name = strdup("sudoers_policy");
	info->path = strdup(SUDOERS_PLUGIN);
	if (info->symbol_name == NULL || info->path == NULL) {
	    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
	    free_plugin_info(info);
	    goto done;
	}
	/* info->options = NULL; */
	ret = sudo_load_plugin(policy_plugin, io_plugins, info);
	free_plugin_info(info);
	if (!ret)
	    goto done;

	/* Default I/O plugin */
	if (TAILQ_EMPTY(io_plugins)) {
	    info = calloc(1, sizeof(*info));
	    if (info == NULL) {
		sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
		goto done;
	    }
	    info->symbol_name = strdup("sudoers_io");
	    info->path = strdup(SUDOERS_PLUGIN);
	    if (info->symbol_name == NULL || info->path == NULL) {
		sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
		free_plugin_info(info);
		goto done;
	    }
	    /* info->options = NULL; */
	    ret = sudo_load_plugin(policy_plugin, io_plugins, info);
	    free_plugin_info(info);
	    if (!ret)
		goto done;
	}
    }
    if (policy_plugin->u.policy->check_policy == NULL) {
	sudo_warnx(U_("policy plugin %s does not include a check_policy method"),
	    policy_plugin->name);
	ret = false;
	goto done;
    }

    /* Install hooks (XXX - later). */
    sudo_debug_set_active_instance(SUDO_DEBUG_INSTANCE_INITIALIZER);
    if (policy_plugin->u.policy->version >= SUDO_API_MKVERSION(1, 2)) {
	if (policy_plugin->u.policy->register_hooks != NULL)
	    policy_plugin->u.policy->register_hooks(SUDO_HOOK_VERSION, register_hook);
    }
    TAILQ_FOREACH(container, io_plugins, entries) {
	if (container->u.io->version >= SUDO_API_MKVERSION(1, 2)) {
	    if (container->u.io->register_hooks != NULL)
		container->u.io->register_hooks(SUDO_HOOK_VERSION, register_hook);
	}
    }
    sudo_debug_set_active_instance(sudo_debug_instance);

done:
    debug_return_bool(ret);
}