Skip to content

cvtsudoers: fix Defaults merge crash and preserve command restrictions in exports - #611

Open
fQwQf wants to merge 3 commits into
sudo-project:mainfrom
fQwQf:cvtsudoers-defaults-merge-export
Open

fQwQf wants to merge 3 commits into
sudo-project:mainfrom
fQwQf:cvtsudoers-defaults-merge-export

Conversation

@fQwQf

@fQwQf fQwQf commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

This fixes two related issues in cvtsudoers:

  1. SIGSEGV when merging Defaults across multiple input files

    When a single Defaults line contains multiple variables, its entries share a refcounted defaults_binding. During multi-file merging, defaults_check_conflict() called TAILQ_CONCAT() twice on the shared binding, emptying members for the remaining entry as well. After the duplicate entry is freed, refcnt drops to 1, merge_defaults() passes the refcnt == 1 check, and dereferences TAILQ_FIRST(&def->binding->members) (NULL).

    Reproduction:

    $ cat c1.sudoers
    Defaults@h1 passwd_tries=3, passwd_timeout=5
    $ cat c2.sudoers
    Defaults@h2 passwd_tries=3
    $ cvtsudoers -f sudoers c1.sudoers c2.sudoers
    Segmentation fault
    #0 merge_defaults (parse_trees=..., merged_tree=..., bound_hosts=...)
        at cvtsudoers_merge.c:951
    951  if (m->type == ALL && !m->negated) {
    (gdb) p *def->binding
    $1 = {members = {tqh_first = 0x0, ...}, refcnt = 1}
    

    Fix: copy-on-write detach shared bindings (unshare_host_binding()) when refcnt > 1 before mutating them in merge_defaults() or defaults_check_conflict(), and guard m != NULL in merge_defaults() after simplify_host_list().

  2. Exported policies silently lose command restrictions and CSV quoting

    The per-exporter continuation check only compared runas, tags, privs, limitprivs, role/type, runchroot, and runcwd (with LDIF also omitting apparmor_profile). Adjacent commands with different TIMEOUT, NOTBEFORE, NOTAFTER, or LDIF APPARMOR_PROFILE values were collapsed into the first command's rule, dropping subsequent restrictions.

    For example (from the existing test17 input):

    user0 ALL = TIMEOUT=7D4H10M30S /usr/bin/id, /usr/bin/who, TIMEOUT=0 /bin/ls
    

    previously exported /bin/ls under the same role with timeout 619830, silently dropping TIMEOUT=0; it is now emitted as a separate role. NOTBEFORE, NOTAFTER, and APPARMOR_PROFILE= differences are likewise preserved (reflected in the updated test17/test19 .ok expectations).

    Fix: extract a shared cmndspec_continues() helper used by the CSV, JSON, and LDIF exporters, including timeout, notbefore, notafter, and apparmor_profile. In CSV exports, print_options_csv() and print_cmnd_option() now escape embedded quotes, print_csv_string() quotes \r/\n as well, and the stray trailing comma after apparmor_profile= is removed.

Testing: full build and make check pass (cvtsudoers: 88 tests, 0 errors); both reproductions above verified before and after the patch.

fQwQf and others added 3 commits December 3, 2026 23:21
Defaults entries on the same line share a refcounted defaults_binding.
Copy the binding on write when refcnt > 1 before mutating it in
merge_defaults() or defaults_check_conflict(), and guard against an
empty member list in merge_defaults() after simplify_host_list().
Share a single cmndspec_continues() helper across the sudoers, JSON,
LDIF and CSV exporters and include timeout, notbefore, notafter and
apparmor_profile when deciding whether consecutive commands can share
a rule.  Also fix CSV quoting for option names, values and newlines,
and drop the stray trailing comma after apparmor_profile.
@fQwQf
fQwQf marked this pull request as ready for review December 4, 2026 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant