Move sudoers search path to struct sudoers_parser_config.

That way we can avoid passing it to init_parser() directly.
We still need sudoers_search_path to be shared between the lexer
and the parser.
This commit is contained in:
Todd C. Miller
2023-05-08 17:04:38 -06:00
parent 9d7c30c5a8
commit 8cbd5462a6
10 changed files with 42 additions and 46 deletions

View File

@@ -770,7 +770,7 @@ parse_sudoers(const char *input_file, struct cvtsudoers_config *conf)
input_file = "stdin"; input_file = "stdin";
} else if ((sudoersin = fopen(input_file, "r")) == NULL) } else if ((sudoersin = fopen(input_file, "r")) == NULL)
sudo_fatal(U_("unable to open %s"), input_file); sudo_fatal(U_("unable to open %s"), input_file);
init_parser(input_file, NULL, NULL); init_parser(input_file, NULL);
if (sudoersparse() && !parse_error) { if (sudoersparse() && !parse_error) {
sudo_warnx(U_("failed to parse %s file, unknown error"), input_file); sudo_warnx(U_("failed to parse %s file, unknown error"), input_file);
parse_error = true; parse_error = true;

View File

@@ -73,10 +73,10 @@ sudo_file_open(struct sudo_nss *nss)
handle = malloc(sizeof(*handle)); handle = malloc(sizeof(*handle));
if (handle != NULL) { if (handle != NULL) {
const char *path_sudoers = policy_path_sudoers(); const struct sudoers_parser_config *conf = policy_sudoers_conf();
handle->fp = open_sudoers(path_sudoers, &outfile, false, NULL); handle->fp = open_sudoers(conf->sudoers_path, &outfile, false, NULL);
if (handle->fp != NULL) { if (handle->fp != NULL) {
init_parser(NULL, path_sudoers, policy_sudoers_conf()); init_parser(NULL, policy_sudoers_conf());
init_parse_tree(&handle->parse_tree, NULL, NULL, nss); init_parse_tree(&handle->parse_tree, NULL, NULL, nss);
if (outfile != NULL) { if (outfile != NULL) {
/* Update path to open sudoers file. */ /* Update path to open sudoers file. */

View File

@@ -3967,8 +3967,7 @@ free_parse_tree(struct sudoers_parse_tree *parse_tree)
* the current sudoers file to path. * the current sudoers file to path.
*/ */
bool bool
init_parser(const char *file, const char *path, init_parser(const char *file, const struct sudoers_parser_config *conf)
const struct sudoers_parser_config *conf)
{ {
bool ret = true; bool ret = true;
debug_decl(init_parser, SUDOERS_DEBUG_PARSER); debug_decl(init_parser, SUDOERS_DEBUG_PARSER);
@@ -3976,6 +3975,15 @@ init_parser(const char *file, const char *path,
free_parse_tree(&parsed_policy); free_parse_tree(&parsed_policy);
parser_leak_init(); parser_leak_init();
init_lexer(); init_lexer();
parse_error = false;
if (conf != NULL) {
parser_conf = *conf;
} else {
const struct sudoers_parser_config def_conf =
SUDOERS_PARSER_CONFIG_INITIALIZER;
parser_conf = def_conf;
}
sudo_rcstr_delref(sudoers); sudo_rcstr_delref(sudoers);
if (file != NULL) { if (file != NULL) {
@@ -3988,8 +3996,9 @@ init_parser(const char *file, const char *path,
} }
sudo_rcstr_delref(sudoers_search_path); sudo_rcstr_delref(sudoers_search_path);
if (path != NULL) { if (parser_conf.sudoers_path != NULL) {
if ((sudoers_search_path = sudo_rcstr_dup(path)) == NULL) { sudoers_search_path = sudo_rcstr_dup(parser_conf.sudoers_path);
if (sudoers_search_path == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
ret = false; ret = false;
} }
@@ -3997,22 +4006,13 @@ init_parser(const char *file, const char *path,
sudoers_search_path = NULL; sudoers_search_path = NULL;
} }
if (conf != NULL) {
parser_conf = *conf;
} else {
const struct sudoers_parser_config def_conf =
SUDOERS_PARSER_CONFIG_INITIALIZER;
parser_conf = def_conf;
}
parse_error = false;
debug_return_bool(ret); debug_return_bool(ret);
} }
bool bool
reset_parser(void) reset_parser(void)
{ {
return init_parser(NULL, NULL, NULL); return init_parser(NULL, NULL);
} }
/* /*

View File

@@ -1784,8 +1784,7 @@ free_parse_tree(struct sudoers_parse_tree *parse_tree)
* the current sudoers file to path. * the current sudoers file to path.
*/ */
bool bool
init_parser(const char *file, const char *path, init_parser(const char *file, const struct sudoers_parser_config *conf)
const struct sudoers_parser_config *conf)
{ {
bool ret = true; bool ret = true;
debug_decl(init_parser, SUDOERS_DEBUG_PARSER); debug_decl(init_parser, SUDOERS_DEBUG_PARSER);
@@ -1793,6 +1792,15 @@ init_parser(const char *file, const char *path,
free_parse_tree(&parsed_policy); free_parse_tree(&parsed_policy);
parser_leak_init(); parser_leak_init();
init_lexer(); init_lexer();
parse_error = false;
if (conf != NULL) {
parser_conf = *conf;
} else {
const struct sudoers_parser_config def_conf =
SUDOERS_PARSER_CONFIG_INITIALIZER;
parser_conf = def_conf;
}
sudo_rcstr_delref(sudoers); sudo_rcstr_delref(sudoers);
if (file != NULL) { if (file != NULL) {
@@ -1805,8 +1813,9 @@ init_parser(const char *file, const char *path,
} }
sudo_rcstr_delref(sudoers_search_path); sudo_rcstr_delref(sudoers_search_path);
if (path != NULL) { if (parser_conf.sudoers_path != NULL) {
if ((sudoers_search_path = sudo_rcstr_dup(path)) == NULL) { sudoers_search_path = sudo_rcstr_dup(parser_conf.sudoers_path);
if (sudoers_search_path == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
ret = false; ret = false;
} }
@@ -1814,22 +1823,13 @@ init_parser(const char *file, const char *path,
sudoers_search_path = NULL; sudoers_search_path = NULL;
} }
if (conf != NULL) {
parser_conf = *conf;
} else {
const struct sudoers_parser_config def_conf =
SUDOERS_PARSER_CONFIG_INITIALIZER;
parser_conf = def_conf;
}
parse_error = false;
debug_return_bool(ret); debug_return_bool(ret);
} }
bool bool
reset_parser(void) reset_parser(void)
{ {
return init_parser(NULL, NULL, NULL); return init_parser(NULL, NULL);
} }
/* /*

View File

@@ -326,6 +326,7 @@ struct cmnd_info {
* Parse configuration settings, passed to init_parser(). * Parse configuration settings, passed to init_parser().
*/ */
struct sudoers_parser_config { struct sudoers_parser_config {
const char *sudoers_path;
bool strict; bool strict;
bool recovery; bool recovery;
int verbose; int verbose;
@@ -334,6 +335,7 @@ struct sudoers_parser_config {
gid_t sudoers_gid; gid_t sudoers_gid;
}; };
#define SUDOERS_PARSER_CONFIG_INITIALIZER { \ #define SUDOERS_PARSER_CONFIG_INITIALIZER { \
NULL, /* sudoers_path */ \
false, /* strict */ \ false, /* strict */ \
true, /* recovery */ \ true, /* recovery */ \
1, /* verbose level 1 */ \ 1, /* verbose level 1 */ \
@@ -392,7 +394,7 @@ int check_aliases(struct sudoers_parse_tree *parse_tree, bool strict, bool quiet
/* gram.y */ /* gram.y */
extern struct sudoers_parse_tree parsed_policy; extern struct sudoers_parse_tree parsed_policy;
extern bool (*sudoers_error_hook)(const char *file, int line, int column, const char *fmt, va_list args); extern bool (*sudoers_error_hook)(const char *file, int line, int column, const char *fmt, va_list args);
bool init_parser(const char *file, const char *path, const struct sudoers_parser_config *conf); bool init_parser(const char *file, const struct sudoers_parser_config *conf);
bool reset_parser(void); bool reset_parser(void);
void free_member(struct member *m); void free_member(struct member *m);
void free_members(struct member_list *members); void free_members(struct member_list *members);

View File

@@ -180,6 +180,7 @@ sudoers_policy_deserialize_info(void *v, struct defaults_list *defaults)
} }
} }
} }
parser_conf.sudoers_path = path_sudoers;
/* Parse command line settings. */ /* Parse command line settings. */
sudo_user.flags = 0; sudo_user.flags = 0;
@@ -630,13 +631,6 @@ policy_sudoers_conf(void)
return &parser_conf; return &parser_conf;
} }
/* Return the path to the sudoers file, which may be set in the plugin args. */
const char *
policy_path_sudoers(void)
{
return path_sudoers;
}
/* Return the path to ldap.conf file, which may be set in the plugin args. */ /* Return the path to ldap.conf file, which may be set in the plugin args. */
const char * const char *
policy_path_ldap_conf(void) policy_path_ldap_conf(void)

View File

@@ -312,7 +312,7 @@ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
/* Initialize defaults and parse sudoers. */ /* Initialize defaults and parse sudoers. */
init_defaults(); init_defaults();
init_parser("sudoers", NULL, NULL); init_parser("sudoers", NULL);
sudoersrestart(fp); sudoersrestart(fp);
sudoersparse(); sudoersparse();
reparent_parse_tree(&parse_tree); reparent_parse_tree(&parse_tree);

View File

@@ -448,7 +448,6 @@ void sudoers_debug_deregister(void);
int sudoers_policy_deserialize_info(void *v, struct defaults_list *defaults); int sudoers_policy_deserialize_info(void *v, struct defaults_list *defaults);
bool sudoers_policy_store_result(bool accepted, char *argv[], char *envp[], mode_t cmnd_umask, char *iolog_path, void *v); bool sudoers_policy_store_result(bool accepted, char *argv[], char *envp[], mode_t cmnd_umask, char *iolog_path, void *v);
const struct sudoers_parser_config *policy_sudoers_conf(void); const struct sudoers_parser_config *policy_sudoers_conf(void);
const char *policy_path_sudoers(void);
const char *policy_path_ldap_conf(void); const char *policy_path_ldap_conf(void);
const char *policy_path_ldap_secret(void); const char *policy_path_ldap_secret(void);

View File

@@ -280,7 +280,7 @@ main(int argc, char *argv[])
/* Initialize the parser and set sudoers filename to "sudoers". */ /* Initialize the parser and set sudoers filename to "sudoers". */
parser_conf.strict = true; parser_conf.strict = true;
parser_conf.verbose = 2; parser_conf.verbose = 2;
init_parser("sudoers", NULL, &parser_conf); init_parser("sudoers", &parser_conf);
/* /*
* Set runas passwd/group entries based on command line or sudoers. * Set runas passwd/group entries based on command line or sudoers.

View File

@@ -291,7 +291,8 @@ main(int argc, char *argv[])
*/ */
parser_conf.strict = true; parser_conf.strict = true;
parser_conf.verbose = quiet ? 0 : 2; parser_conf.verbose = quiet ? 0 : 2;
init_parser(NULL, path_sudoers, &parser_conf); parser_conf.sudoers_path = path_sudoers;
init_parser(NULL, &parser_conf);
if ((sudoersin = open_sudoers(path_sudoers, &sudoers, true, NULL)) == NULL) if ((sudoersin = open_sudoers(path_sudoers, &sudoers, true, NULL)) == NULL)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale); sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
@@ -654,7 +655,7 @@ reparse_sudoers(char *editor, int editor_argc, char **editor_argv,
/* Clean slate for each parse */ /* Clean slate for each parse */
if (!init_defaults()) if (!init_defaults())
sudo_fatalx("%s", U_("unable to initialize sudoers default values")); sudo_fatalx("%s", U_("unable to initialize sudoers default values"));
init_parser(sp->opath, path_sudoers, &parser_conf); init_parser(sp->opath, &parser_conf);
sp->errorline = -1; sp->errorline = -1;
/* Parse the sudoers temp file(s) */ /* Parse the sudoers temp file(s) */
@@ -1071,7 +1072,7 @@ check_syntax(const char *path, bool quiet, bool strict, bool check_owner,
goto done; goto done;
} }
} }
init_parser(fname, path, &parser_conf); init_parser(fname, &parser_conf);
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale); sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
if (sudoersparse() && !parse_error) { if (sudoersparse() && !parse_error) {
if (!quiet) if (!quiet)