Load plugins after parsing arguments and potentially printing the

version.  That way, an error loading or initializing a plugin doesn't
break "sudo -h" or "sudo -V".
This commit is contained in:
Todd C. Miller
2011-04-06 17:51:36 -04:00
parent 4b4f5bf32c
commit b6d0a28efb
3 changed files with 56 additions and 27 deletions

View File

@@ -131,7 +131,7 @@ done:
/* /*
* Load the plugins listed in conf_file. * Load the plugins listed in conf_file.
*/ */
void int
sudo_load_plugins(const char *conf_file, sudo_load_plugins(const char *conf_file,
struct plugin_container *policy_plugin, struct plugin_container *policy_plugin,
struct plugin_container_list *io_plugins) struct plugin_container_list *io_plugins)
@@ -143,47 +143,65 @@ sudo_load_plugins(const char *conf_file,
struct stat sb; struct stat sb;
void *handle; void *handle;
char path[PATH_MAX]; char path[PATH_MAX];
int rval = FALSE;
/* Parse sudo.conf */ /* Parse sudo.conf */
plugin_list = sudo_read_conf(conf_file); plugin_list = sudo_read_conf(conf_file);
tq_foreach_fwd(plugin_list, info) { tq_foreach_fwd(plugin_list, info) {
if (info->path[0] == '/') { if (info->path[0] == '/') {
if (strlcpy(path, info->path, sizeof(path)) >= sizeof(path)) if (strlcpy(path, info->path, sizeof(path)) >= sizeof(path)) {
errorx(1, "%s: %s", info->path, strerror(ENAMETOOLONG)); warningx("%s: %s", info->path, strerror(ENAMETOOLONG));
goto done;
}
} else { } else {
if (snprintf(path, sizeof(path), "%s%s", _PATH_SUDO_PLUGIN_DIR, if (snprintf(path, sizeof(path), "%s%s", _PATH_SUDO_PLUGIN_DIR,
info->path) >= sizeof(path)) { info->path) >= sizeof(path)) {
errorx(1, "%s%s: %s", _PATH_SUDO_PLUGIN_DIR, info->path, warningx("%s%s: %s", _PATH_SUDO_PLUGIN_DIR, info->path,
strerror(ENAMETOOLONG)); strerror(ENAMETOOLONG));
goto done;
} }
} }
if (stat(path, &sb) != 0) if (stat(path, &sb) != 0) {
error(1, "%s", path); warning("%s", path);
if (sb.st_uid != ROOT_UID) goto done;
errorx(1, "%s must be owned by uid %d", path, ROOT_UID); }
if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) if (sb.st_uid != ROOT_UID) {
errorx(1, "%s must be only be writable by owner", path); warningx("%s must be owned by uid %d", path, ROOT_UID);
goto done;
}
if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
warningx("%s must be only be writable by owner", path);
goto done;
}
/* Open plugin and map in symbol */ /* Open plugin and map in symbol */
handle = dlopen(path, RTLD_LAZY|RTLD_LOCAL); handle = dlopen(path, RTLD_LAZY|RTLD_LOCAL);
if (!handle) if (!handle) {
errorx(1, "unable to dlopen %s: %s", path, dlerror()); warningx("unable to dlopen %s: %s", path, dlerror());
goto done;
}
plugin = dlsym(handle, info->symbol_name); plugin = dlsym(handle, info->symbol_name);
if (!plugin) if (!plugin) {
errorx(1, "unable to find symbol %s in %s", info->symbol_name, path); warningx("unable to find symbol %s in %s", info->symbol_name, path);
goto done;
}
if (plugin->type != SUDO_POLICY_PLUGIN && plugin->type != SUDO_IO_PLUGIN) { if (plugin->type != SUDO_POLICY_PLUGIN && plugin->type != SUDO_IO_PLUGIN) {
errorx(1, "%s: unknown policy type %d", path, plugin->type); warningx("%s: unknown policy type %d", path, plugin->type);
goto done;
} }
if (SUDO_API_VERSION_GET_MAJOR(plugin->version) != SUDO_API_VERSION_MAJOR) { if (SUDO_API_VERSION_GET_MAJOR(plugin->version) != SUDO_API_VERSION_MAJOR) {
errorx(1, "%s: incompatible policy major version %d, expected %d", warningx("%s: incompatible policy major version %d, expected %d",
path, SUDO_API_VERSION_GET_MAJOR(plugin->version), path, SUDO_API_VERSION_GET_MAJOR(plugin->version),
SUDO_API_VERSION_MAJOR); SUDO_API_VERSION_MAJOR);
goto done;
} }
if (plugin->type == SUDO_POLICY_PLUGIN) { if (plugin->type == SUDO_POLICY_PLUGIN) {
if (policy_plugin->handle) if (policy_plugin->handle) {
errorx(1, "only a single policy plugin may be loaded"); warningx("only a single policy plugin may be loaded");
goto done;
}
policy_plugin->handle = handle; policy_plugin->handle = handle;
policy_plugin->name = info->symbol_name; policy_plugin->name = info->symbol_name;
policy_plugin->u.generic = plugin; policy_plugin->u.generic = plugin;
@@ -197,8 +215,18 @@ sudo_load_plugins(const char *conf_file,
tq_append(io_plugins, container); tq_append(io_plugins, container);
} }
} }
if (policy_plugin->handle == NULL) if (policy_plugin->handle == NULL) {
errorx(1, "%s: at least one policy plugin must be specified", conf_file); warningx("%s: at least one policy plugin must be specified", conf_file);
if (policy_plugin->u.policy->check_policy == NULL) goto done;
errorx(1, "policy plugin %s does not include a check_policy method"); }
if (policy_plugin->u.policy->check_policy == NULL) {
warningx("policy plugin %s does not include a check_policy method",
policy_plugin->name);
goto done;
}
rval = TRUE;
done:
return rval;
} }

View File

@@ -199,20 +199,21 @@ main(int argc, char *argv[], char *envp[])
memset(&user_details, 0, sizeof(user_details)); memset(&user_details, 0, sizeof(user_details));
user_info = get_user_info(&user_details); user_info = get_user_info(&user_details);
/* Read sudo.conf and load plugins. */
sudo_load_plugins(_PATH_SUDO_CONF, &policy_plugin, &io_plugins);
/* Parse command line arguments. */ /* Parse command line arguments. */
sudo_mode = parse_args(argc, argv, &nargc, &nargv, &settings, &env_add); sudo_mode = parse_args(argc, argv, &nargc, &nargv, &settings, &env_add);
sudo_debug(9, "sudo_mode %d", sudo_mode); sudo_debug(9, "sudo_mode %d", sudo_mode);
/* Print sudo version early, in case policy plugin init fails. */ /* Print sudo version early, in case of plugin init failure. */
if (ISSET(sudo_mode, MODE_VERSION)) { if (ISSET(sudo_mode, MODE_VERSION)) {
printf("Sudo version %s\n", PACKAGE_VERSION); printf("Sudo version %s\n", PACKAGE_VERSION);
if (user_details.uid == ROOT_UID) if (user_details.uid == ROOT_UID)
(void) printf("Configure args: %s\n", CONFIGURE_ARGS); (void) printf("Configure args: %s\n", CONFIGURE_ARGS);
} }
/* Read sudo.conf and load plugins. */
if (!sudo_load_plugins(_PATH_SUDO_CONF, &policy_plugin, &io_plugins))
errorx(1, "fatal error, unable to load plugins");
/* Open policy plugin. */ /* Open policy plugin. */
ok = policy_open(&policy_plugin, settings, user_info, envp); ok = policy_open(&policy_plugin, settings, user_info, envp);
if (ok != TRUE) { if (ok != TRUE) {

View File

@@ -78,7 +78,7 @@ int sudo_conversation(int num_msgs, const struct sudo_conv_message msgs[],
struct sudo_conv_reply replies[]); struct sudo_conv_reply replies[]);
int _sudo_printf(int msg_type, const char *fmt, ...); int _sudo_printf(int msg_type, const char *fmt, ...);
void sudo_load_plugins(const char *conf_file, int sudo_load_plugins(const char *conf_file,
struct plugin_container *policy_plugin, struct plugin_container *policy_plugin,
struct plugin_container_list *io_plugins); struct plugin_container_list *io_plugins);