diff --git a/plugins/sudoers/cvtsudoers.c b/plugins/sudoers/cvtsudoers.c index 3c8d158b29..dcf09b1903 100644 --- a/plugins/sudoers/cvtsudoers.c +++ b/plugins/sudoers/cvtsudoers.c @@ -97,6 +97,21 @@ static bool alias_prune(struct sudoers_parse_tree *parse_tree, struct cvtsudoers sudo_noreturn static void help(void); sudo_noreturn static void usage(void); +/* Does the next entry differ only in the command itself? */ +bool +cmndspec_continues(const struct cmndspec *cs, const struct cmndspec *next) +{ + bool ret = next != NULL && + !RUNAS_CHANGED(cs, next) && !TAGS_CHANGED(cs->tags, next->tags) + && cs->timeout == next->timeout && cs->notbefore == next->notbefore + && cs->notafter == next->notafter + && cs->privs == next->privs && cs->limitprivs == next->limitprivs + && cs->role == next->role && cs->type == next->type + && cs->apparmor_profile == next->apparmor_profile + && cs->runchroot == next->runchroot && cs->runcwd == next->runcwd; + return ret; +} + int main(int argc, char *argv[]) { diff --git a/plugins/sudoers/cvtsudoers.h b/plugins/sudoers/cvtsudoers.h index 09d0e9bf62..826f5b97f1 100644 --- a/plugins/sudoers/cvtsudoers.h +++ b/plugins/sudoers/cvtsudoers.h @@ -86,6 +86,7 @@ struct cvtsudoers_filter { }; /* cvtsudoers.c */ +bool cmndspec_continues(const struct cmndspec *cs, const struct cmndspec *next); extern struct cvtsudoers_filter *filters; void log_warnx(const char * restrict fmt, ...) sudo_printflike(1, 2); diff --git a/plugins/sudoers/cvtsudoers_csv.c b/plugins/sudoers/cvtsudoers_csv.c index 2bf1c955d2..64d4a5dd8b 100644 --- a/plugins/sudoers/cvtsudoers_csv.c +++ b/plugins/sudoers/cvtsudoers_csv.c @@ -31,32 +31,7 @@ #include #include -static bool print_member_list_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree, struct member_list *members, bool negated, short alias_type, bool expand_aliases); - -/* - * Print sudoOptions from a defaults_list. - */ -static bool -print_options_csv(FILE *fp, struct defaults_list *options, bool need_comma) -{ - struct defaults *opt; - debug_decl(print_options_csv, SUDOERS_DEBUG_UTIL); - - TAILQ_FOREACH(opt, options, entries) { - if (opt->val != NULL) { - /* There is no need to double quote values here. */ - fprintf(fp, "%s%s%s%s", need_comma ? "," : "", opt->var, - opt->op == '+' ? "+=" : opt->op == '-' ? "-=" : "=", opt->val); - } else { - /* Boolean flag. */ - fprintf(fp, "%s%s%s", need_comma ? "," : "", - opt->op == false ? "!" : "", opt->var); - } - need_comma = true; - } - - debug_return_bool(!ferror(fp)); -} +static bool print_member_list_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree, struct member_list *members, bool negated, bool quoted, short alias_type, bool expand_aliases); /* * Map a Defaults type to string. @@ -104,49 +79,62 @@ defaults_to_alias_type(int defaults_type) * Print a string, performing quoting as needed. * If a field includes a comma it must be double-quoted. * Double quotes are replaced by a pair of double-quotes. - * XXX - rewrite this */ static bool print_csv_string(FILE *fp, const char *str, bool quoted) { - const char *src = str; - char *dst, *newstr; - size_t len, newsize; - bool quote_it = false; - bool ret = true; + bool quote_it = !quoted && strpbrk(str, "\",\r\n") != NULL; debug_decl(print_csv_string, SUDOERS_DEBUG_UTIL); - len = strcspn(str, quoted ? "\"" : "\","); - if (str[len] == '\0') { - /* nothing to escape */ - debug_return_bool(fputs(str, fp) != EOF); + if (quote_it) + putc('"', fp); + for (; *str != '\0'; str++) { + if (*str == '"') + putc('"', fp); + putc(*str, fp); } + if (quote_it) + putc('"', fp); + debug_return_bool(!ferror(fp)); +} - if (!quoted && strchr(str + len, ',') != NULL) - quote_it = true; +/* Append a name=value pair to the quoted options field. */ +static void +print_cmnd_option(FILE *fp, const char *name, const char *val, bool *need_comma) +{ + if (*need_comma) + putc(',', fp); + fputs(name, fp); + putc('=', fp); + print_csv_string(fp, val, true); + *need_comma = true; +} - /* String includes characters we need to escape. */ - newsize = len + 2 + (strlen(len + str) * 2) + 1; - if ((newstr = malloc(newsize)) == NULL) - debug_return_bool(false); - dst = newstr; +/* + * Print sudoOptions from a defaults_list. + */ +static bool +print_options_csv(FILE *fp, struct defaults_list *options, bool need_comma) +{ + struct defaults *opt; + debug_decl(print_options_csv, SUDOERS_DEBUG_UTIL); - if (quote_it) - *dst++ = '"'; - while (*src != '\0') { - if (*src == '"') - *dst++ = '"'; - *dst++ = *src++; + TAILQ_FOREACH(opt, options, entries) { + if (need_comma) + putc(',', fp); + if (opt->val == NULL && !opt->op) + putc('!', fp); + if (!print_csv_string(fp, opt->var, true)) + debug_return_bool(false); + if (opt->val != NULL) { + fputs(opt->op == '+' ? "+=" : opt->op == '-' ? "-=" : "=", fp); + if (!print_csv_string(fp, opt->val, true)) + debug_return_bool(false); + } + need_comma = true; } - if (quote_it) - *dst++ = '"'; - *dst = '\0'; - - if (fputs(newstr, fp) == EOF) - ret = false; - free(newstr); - debug_return_bool(ret); + debug_return_bool(!ferror(fp)); } /* @@ -234,7 +222,7 @@ print_member_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree, case ALIAS: if (expand_aliases) { if ((a = alias_get(parse_tree, name, alias_type)) != NULL) { - if (!print_member_list_csv(fp, parse_tree, &a->members, negated, + if (!print_member_list_csv(fp, parse_tree, &a->members, negated, quoted, alias_type, expand_aliases)) { alias_put(a); debug_return_bool(false); @@ -267,27 +255,29 @@ print_member_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree, */ static bool print_member_list_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree, - struct member_list *members, bool negated, short alias_type, + struct member_list *members, bool negated, bool quoted, short alias_type, bool expand_aliases) { struct member *m, *next; + bool quote_list; debug_decl(print_member_list_csv, SUDOERS_DEBUG_UTIL); if (TAILQ_EMPTY(members)) debug_return_bool(true); - if (TAILQ_FIRST(members) != TAILQ_LAST(members, member_list)) + quote_list = !quoted && TAILQ_FIRST(members) != TAILQ_LAST(members, member_list); + if (quote_list) putc('"', fp); TAILQ_FOREACH_SAFE(m, members, entries, next) { if (!print_member_csv(fp, parse_tree, m->name, m->type, - negated ? !m->negated : m->negated, true, alias_type, + negated ? !m->negated : m->negated, quoted || quote_list, alias_type, expand_aliases)) { debug_return_bool(false); } if (next != NULL) putc(',', fp); } - if (TAILQ_FIRST(members) != TAILQ_LAST(members, member_list)) + if (quote_list) putc('"', fp); debug_return_bool(!ferror(fp)); @@ -307,7 +297,7 @@ print_defaults_binding_csv(FILE *fp, if (type != DEFAULTS) { /* Print each member object in binding. */ alias_type = defaults_to_alias_type(type); - if (!print_member_list_csv(fp, parse_tree, &binding->members, false, + if (!print_member_list_csv(fp, parse_tree, &binding->members, false, false, alias_type, expand_aliases)) { debug_return_bool(false); } @@ -404,8 +394,8 @@ print_alias_csv(struct sudoers_parse_tree *parse_tree, struct alias *a, void *v) } fprintf(fp, "%s,%s,", title, a->name); - if (!print_member_list_csv(fp, parse_tree, &a->members, false, a->type, - false)) { + if (!print_member_list_csv(fp, parse_tree, &a->members, false, false, + a->type, false)) { debug_return_int(-1); } putc('\n', fp); @@ -454,7 +444,7 @@ print_cmndspec_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree, debug_decl(print_cmndspec_csv, SUDOERS_DEBUG_UTIL); if (cs->runasuserlist != NULL) { - if (!print_member_list_csv(fp, parse_tree, cs->runasuserlist, false, + if (!print_member_list_csv(fp, parse_tree, cs->runasuserlist, false, false, RUNASALIAS, expand_aliases)) { debug_return_bool(false); } @@ -462,7 +452,7 @@ print_cmndspec_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree, putc(',', fp); if (cs->runasgrouplist != NULL) { - if (!print_member_list_csv(fp, parse_tree, cs->runasgrouplist, false, + if (!print_member_list_csv(fp, parse_tree, cs->runasgrouplist, false, false, RUNASALIAS, expand_aliases)) { debug_return_bool(false); } @@ -559,40 +549,20 @@ print_cmndspec_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree, if (!TAILQ_EMPTY(options)) need_comma = true; - /* Print runchroot and runcwd. */ - if (cs->runchroot != NULL) { - fprintf(fp, "%srunchroot=%s", need_comma ? "," : "", cs->runchroot); - need_comma = true; - } - if (cs->runcwd != NULL) { - fprintf(fp, "%sruncwd=%s", need_comma ? "," : "", cs->runcwd); - need_comma = true; - } - - /* Print SELinux role/type */ + if (cs->runchroot != NULL) + print_cmnd_option(fp, "runchroot", cs->runchroot, &need_comma); + if (cs->runcwd != NULL) + print_cmnd_option(fp, "runcwd", cs->runcwd, &need_comma); if (cs->role != NULL && cs->type != NULL) { - fprintf(fp, "%srole=%s,type=%s", need_comma ? "," : "", - cs->role, cs->type); - need_comma = true; - } - - if (cs->apparmor_profile != NULL) { - fprintf(fp, "%sapparmor_profile=%s,", need_comma ? "," : "", - cs->apparmor_profile); - need_comma = true; - } - - /* Print Solaris privs/limitprivs */ - if (cs->privs != NULL || cs->limitprivs != NULL) { - if (cs->privs != NULL) { - fprintf(fp, "%sprivs=%s", need_comma ? "," : "", cs->privs); - need_comma = true; - } - if (cs->limitprivs != NULL) { - fprintf(fp, "%slimitprivs=%s", need_comma ? "," : "", cs->limitprivs); - need_comma = true; - } - } + print_cmnd_option(fp, "role", cs->role, &need_comma); + print_cmnd_option(fp, "type", cs->type, &need_comma); + } + if (cs->apparmor_profile != NULL) + print_cmnd_option(fp, "apparmor_profile", cs->apparmor_profile, &need_comma); + if (cs->privs != NULL) + print_cmnd_option(fp, "privs", cs->privs, &need_comma); + if (cs->limitprivs != NULL) + print_cmnd_option(fp, "limitprivs", cs->limitprivs, &need_comma); #ifdef __clang_analyzer__ (void)&need_comma; #endif @@ -604,15 +574,7 @@ print_cmndspec_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree, * role/type, AppArmor profiles and Solaris priv settings. */ for (;;) { - /* Does the next entry differ only in the command itself? */ - /* XXX - move into a function that returns bool */ - /* XXX - TAG_SET does not account for implied SETENV */ - last_one = next == NULL || - RUNAS_CHANGED(cs, next) || TAGS_CHANGED(cs->tags, next->tags) - || cs->privs != next->privs || cs->limitprivs != next->limitprivs - || cs->role != next->role || cs->type != next->type - || cs->apparmor_profile != next->apparmor_profile - || cs->runchroot != next->runchroot || cs->runcwd != next->runcwd; + last_one = !cmndspec_continues(cs, next); if (!quoted && !last_one) { quoted = true; @@ -654,13 +616,13 @@ print_userspec_csv(FILE *fp, const struct sudoers_parse_tree *parse_tree, TAILQ_FOREACH(priv, &us->privileges, entries) { TAILQ_FOREACH_SAFE(cs, &priv->cmndlist, entries, next) { fputs("rule,", fp); - if (!print_member_list_csv(fp, parse_tree, &us->users, false, + if (!print_member_list_csv(fp, parse_tree, &us->users, false, false, USERALIAS, expand_aliases)) { debug_return_bool(false); } putc(',', fp); - if (!print_member_list_csv(fp, parse_tree, &priv->hostlist, false, + if (!print_member_list_csv(fp, parse_tree, &priv->hostlist, false, false, HOSTALIAS, expand_aliases)) { debug_return_bool(false); } diff --git a/plugins/sudoers/cvtsudoers_json.c b/plugins/sudoers/cvtsudoers_json.c index dfd0892af5..c50206e82b 100644 --- a/plugins/sudoers/cvtsudoers_json.c +++ b/plugins/sudoers/cvtsudoers_json.c @@ -660,19 +660,6 @@ print_aliases_json(struct json_container *jsonc, debug_return_bool(true); } -/* Does the next entry differ only in the command itself? */ -static bool -cmndspec_continues(struct cmndspec *cs, struct cmndspec *next) -{ - bool ret = next != NULL && - !RUNAS_CHANGED(cs, next) && !TAGS_CHANGED(cs->tags, next->tags) - && cs->privs == next->privs && cs->limitprivs == next->limitprivs - && cs->role == next->role && cs->type == next->type - && cs->apparmor_profile == next->apparmor_profile - && cs->runchroot == next->runchroot && cs->runcwd == next->runcwd; - return ret; -} - /* * Print a Cmnd_Spec in JSON format at the correct indent level. * A pointer to the next Cmnd_Spec is passed in to make it possible to diff --git a/plugins/sudoers/cvtsudoers_ldif.c b/plugins/sudoers/cvtsudoers_ldif.c index e2ae84c2a3..78c592380c 100644 --- a/plugins/sudoers/cvtsudoers_ldif.c +++ b/plugins/sudoers/cvtsudoers_ldif.c @@ -524,14 +524,7 @@ print_cmndspec_ldif(FILE *fp, const struct sudoers_parse_tree *parse_tree, * role/type and Solaris priv settings. */ for (;;) { - /* Does the next entry differ only in the command itself? */ - /* XXX - move into a function that returns bool */ - /* XXX - TAG_SET does not account for implied SETENV */ - last_one = next == NULL || - RUNAS_CHANGED(cs, next) || TAGS_CHANGED(cs->tags, next->tags) - || cs->privs != next->privs || cs->limitprivs != next->limitprivs - || cs->role != next->role || cs->type != next->type - || cs->runchroot != next->runchroot || cs->runcwd != next->runcwd; + last_one = !cmndspec_continues(cs, next); if (!print_member_ldif(fp, parse_tree, cs->cmnd->name, cs->cmnd->type, cs->cmnd->negated, CMNDALIAS, "sudoCommand")) { diff --git a/plugins/sudoers/cvtsudoers_merge.c b/plugins/sudoers/cvtsudoers_merge.c index 38fe5e0d2a..06b9ac5d38 100644 --- a/plugins/sudoers/cvtsudoers_merge.c +++ b/plugins/sudoers/cvtsudoers_merge.c @@ -799,6 +799,38 @@ enum cvtsudoers_conflict { CONFLICT_ERROR }; +/* Host bindings may be shared by several Defaults on the same line. */ +static bool +unshare_host_binding(struct defaults *def) +{ + struct defaults_binding *binding; + struct member *m, *copy; + debug_decl(unshare_host_binding, SUDOERS_DEBUG_DEFAULTS); + + if (def->binding->refcnt == 1) + debug_return_bool(true); + binding = malloc(sizeof(*binding)); + if (binding == NULL) { + sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); + debug_return_bool(false); + } + TAILQ_INIT(&binding->members); + TAILQ_FOREACH(m, &def->binding->members, entries) { + copy = new_member(m->name, m->type); + if (copy == NULL) { + free_members(&binding->members); + free(binding); + debug_return_bool(false); + } + copy->negated = m->negated; + TAILQ_INSERT_TAIL(&binding->members, copy, entries); + } + def->binding->refcnt--; + binding->refcnt = 1; + def->binding = binding; + debug_return_bool(true); +} + /* * Check for duplicate and conflicting Defaults entries in later sudoers files. * Returns true if we find a conflict or duplicate, else false. @@ -827,6 +859,8 @@ defaults_check_conflict(struct defaults *def, if (defaults_val_matches(def, d)) { /* Duplicate Defaults entry (may need to merge binding). */ if (mergeable) { + if (!unshare_host_binding(def) || !unshare_host_binding(d)) + debug_return_int(CONFLICT_ERROR); if (d->type != def->type && (d->type == DEFAULTS || def->type == DEFAULTS)) { /* @@ -894,6 +928,8 @@ merge_defaults(struct sudoers_parse_tree_list *parse_trees, */ TAILQ_FOREACH(def, &parse_tree->defaults, entries) { if (parse_tree->lhost != NULL && def->type == DEFAULTS) { + if (!unshare_host_binding(def)) + debug_return_bool(false); m = new_member(parse_tree->lhost, WORD); if (m == NULL) debug_return_bool(false); @@ -948,7 +984,7 @@ merge_defaults(struct sudoers_parse_tree_list *parse_trees, debug_return_bool(false); } m = TAILQ_FIRST(&def->binding->members); - if (m->type == ALL && !m->negated) { + if (m != NULL && m->type == ALL && !m->negated) { if (TAILQ_NEXT(m, entries) == NULL) { /* Convert Defaults@ALL -> Defaults */ def->type = DEFAULTS; diff --git a/plugins/sudoers/fmtsudoers.c b/plugins/sudoers/fmtsudoers.c index 083393f4b9..bdea13fa86 100644 --- a/plugins/sudoers/fmtsudoers.c +++ b/plugins/sudoers/fmtsudoers.c @@ -247,6 +247,9 @@ sudoers_format_cmndspec(struct sudo_lbuf *lbuf, char numbuf[STRLEN_MAX_SIGNED(int) + 1]; (void)snprintf(numbuf, sizeof(numbuf), "%d", cs->timeout); sudo_lbuf_append(lbuf, "TIMEOUT=%s ", numbuf); + } else if (cs->timeout <= 0 && prev_cs != NULL && prev_cs->timeout > 0) { + /* Prevent the previous command's timeout from being inherited. */ + sudo_lbuf_append(lbuf, "TIMEOUT=0 "); } if (cs->notbefore != UNSPEC && FIELD_CHANGED(prev_cs, cs, notbefore)) { char buf[sizeof("CCYYMMDDHHMMSSZ")] = ""; diff --git a/plugins/sudoers/regress/cvtsudoers/test44.out.ok b/plugins/sudoers/regress/cvtsudoers/test44.out.ok index 127008f883..8ec1d3f3de 100644 --- a/plugins/sudoers/regress/cvtsudoers/test44.out.ok +++ b/plugins/sudoers/regress/cvtsudoers/test44.out.ok @@ -1,5 +1,6 @@ alias_type,alias_name,members User_Alias,ADMINS,alice +Cmnd_Alias,SAMPLE,"/bin/echo a,b" rule,user,host,runusers,rungroups,options,command rule,ADMINS,ALL,root,,"!log_input",/bin/ls diff --git a/plugins/sudoers/regress/cvtsudoers/test44.sh b/plugins/sudoers/regress/cvtsudoers/test44.sh index 2d5d07a017..ca1aaf2363 100644 --- a/plugins/sudoers/regress/cvtsudoers/test44.sh +++ b/plugins/sudoers/regress/cvtsudoers/test44.sh @@ -8,6 +8,7 @@ $CVTSUDOERS -c "" -f csv <