From 33940b7646bfe655ea4eb2898b24e7bd3b0485ec Mon Sep 17 00:00:00 2001 From: Hailiang Ma Date: Mon, 30 Nov 2026 15:25:55 +0800 Subject: [PATCH] Fix sudoers.d file name filtering Directories included via @includedir skipped any file whose name contained a ".", which also ignored legitimate files such as myrules.conf. Only skip editor and package manager temporary or backup files: names beginning with "." or ending in "~", ".bak", ".orig", ".rej", or ".swp". Update the sudoers manual and add a regression test. Fixes: https://github.com/sudo-project/sudo/issues/486 Signed-off-by: Hailiang Ma --- docs/sudoers.man.in | 13 +++++--- docs/sudoers.mdoc.in | 13 +++++--- plugins/sudoers/regress/visudo/test12.out.ok | 0 plugins/sudoers/regress/visudo/test12.sh | 29 ++++++++++++++++ plugins/sudoers/toke.c | 35 ++++++++++++++------ plugins/sudoers/toke.l | 35 ++++++++++++++------ 6 files changed, 95 insertions(+), 30 deletions(-) create mode 100644 plugins/sudoers/regress/visudo/test12.out.ok create mode 100755 plugins/sudoers/regress/visudo/test12.sh diff --git a/docs/sudoers.man.in b/docs/sudoers.man.in index 65b205e732..dd8bf95571 100644 --- a/docs/sudoers.man.in +++ b/docs/sudoers.man.in @@ -2589,11 +2589,16 @@ For example, given: \fBsudo\fR will suspend processing of the current file and read each file in \fI/etc/sudoers.d\fR, -skipping file names that end in -\(oq~\(cq -or contain a +skipping file names that begin with \(oq.\&\(cq -character to avoid causing problems with package manager or editor +or end in +\(oq~\(cq, +\(oq.bak\(cq, +\(oq.orig\(cq, +\(oq.rej\(cq, +or +\(oq.swp\(cq +to avoid causing problems with package manager or editor temporary/backup files. .PP Files are parsed in sorted lexical order. diff --git a/docs/sudoers.mdoc.in b/docs/sudoers.mdoc.in index 4c03fe9c45..2305612920 100644 --- a/docs/sudoers.mdoc.in +++ b/docs/sudoers.mdoc.in @@ -2442,11 +2442,16 @@ For example, given: .Nm sudo will suspend processing of the current file and read each file in .Pa /etc/sudoers.d , -skipping file names that end in -.Ql ~ -or contain a +skipping file names that begin with .Ql .\& -character to avoid causing problems with package manager or editor +or end in +.Ql ~ , +.Ql .bak , +.Ql .orig , +.Ql .rej , +or +.Ql .swp +to avoid causing problems with package manager or editor temporary/backup files. .Pp Files are parsed in sorted lexical order. diff --git a/plugins/sudoers/regress/visudo/test12.out.ok b/plugins/sudoers/regress/visudo/test12.out.ok new file mode 100644 index 0000000000..e69de29bb2 diff --git a/plugins/sudoers/regress/visudo/test12.sh b/plugins/sudoers/regress/visudo/test12.sh new file mode 100755 index 0000000000..a29c6494a1 --- /dev/null +++ b/plugins/sudoers/regress/visudo/test12.sh @@ -0,0 +1,29 @@ +#!/bin/sh +# +# Test that includedir files with a '.' in the name are parsed +# and editor backup files are ignored. +# + +: ${VISUDO=visudo} + +TESTDIR="${TMPDIR:-/tmp}/sudo-visudo-test12.$$" +trap 'rm -rf "$TESTDIR"' 0 1 2 3 15 +mkdir -p "$TESTDIR/sudoers.d" || exit 1 + +cat >"$TESTDIR/sudoers" <"$TESTDIR/sudoers.d/myrules.conf" <"$TESTDIR/sudoers.d/$file" <"$TESTDIR/out" 2>&1 || exit 1 +grep -q "myrules.conf: parsed OK" "$TESTDIR/out" || exit 1 diff --git a/plugins/sudoers/toke.c b/plugins/sudoers/toke.c index 544fdfe88b..2c93e85e08 100644 --- a/plugins/sudoers/toke.c +++ b/plugins/sudoers/toke.c @@ -5706,9 +5706,29 @@ pl_compare(const void *v1, const void *v2) return strcmp((*p2)->path, (*p1)->path); } +/* + * Return true if name is a package manager or editor temporary/backup + * file that should be ignored in an includedir. + */ +static bool +ignore_sudoers_file(const char *name, size_t namelen) +{ + static const char * const suffixes[] = { ".bak", ".orig", ".rej", ".swp" }; + size_t i; + + if (namelen == 0 || name[0] == '.' || name[namelen - 1] == '~') + return true; + for (i = 0; i < nitems(suffixes); i++) { + const size_t len = strlen(suffixes[i]); + if (namelen > len && strcmp(&name[namelen - len], suffixes[i]) == 0) + return true; + } + return false; +} + /* * Open dirpath and fill in pathsp with an array of regular files - * that do not end in '~' or contain a '.'. + * that are not package manager or editor temporary/backup files. * Returns the number of files or SIZE_MAX (-1) on error. * If zero files are found, NULL is stored in pathsp. */ @@ -5742,18 +5762,11 @@ read_dir_files(const char *dirpath, struct path_list ***pathsp, int verbose) size_t len; char *path; - /* Ignore files that end in '~' or have a '.' in them. */ - if (namelen == 0 || name[namelen - 1] == '~' || strchr(name, '.') != NULL) { + if (ignore_sudoers_file(name, namelen)) { /* Warn about ignored files not starting with '.' if verbose. */ if (namelen > 0 && name[0] != '.' && verbose > 1) { - if (name[namelen - 1] == '~' || - (namelen > 4 && strcmp(&name[namelen - 4], ".bak") == 0)) { - fprintf(stderr, U_("%s/%s: %s"), dirpath, name, - U_("ignoring editor backup file")); - } else { - fprintf(stderr, U_("%s/%s: %s"), dirpath, name, - U_("ignoring file name containing '.'")); - } + fprintf(stderr, U_("%s/%s: %s"), dirpath, name, + U_("ignoring editor backup file")); fputc('\n', stderr); } continue; diff --git a/plugins/sudoers/toke.l b/plugins/sudoers/toke.l index 250fea7e8c..9ed292c111 100644 --- a/plugins/sudoers/toke.l +++ b/plugins/sudoers/toke.l @@ -931,9 +931,29 @@ pl_compare(const void *v1, const void *v2) return strcmp((*p2)->path, (*p1)->path); } +/* + * Return true if name is a package manager or editor temporary/backup + * file that should be ignored in an includedir. + */ +static bool +ignore_sudoers_file(const char *name, size_t namelen) +{ + static const char * const suffixes[] = { ".bak", ".orig", ".rej", ".swp" }; + size_t i; + + if (namelen == 0 || name[0] == '.' || name[namelen - 1] == '~') + return true; + for (i = 0; i < nitems(suffixes); i++) { + const size_t len = strlen(suffixes[i]); + if (namelen > len && strcmp(&name[namelen - len], suffixes[i]) == 0) + return true; + } + return false; +} + /* * Open dirpath and fill in pathsp with an array of regular files - * that do not end in '~' or contain a '.'. + * that are not package manager or editor temporary/backup files. * Returns the number of files or SIZE_MAX (-1) on error. * If zero files are found, NULL is stored in pathsp. */ @@ -967,18 +987,11 @@ read_dir_files(const char *dirpath, struct path_list ***pathsp, int verbose) size_t len; char *path; - /* Ignore files that end in '~' or have a '.' in them. */ - if (namelen == 0 || name[namelen - 1] == '~' || strchr(name, '.') != NULL) { + if (ignore_sudoers_file(name, namelen)) { /* Warn about ignored files not starting with '.' if verbose. */ if (namelen > 0 && name[0] != '.' && verbose > 1) { - if (name[namelen - 1] == '~' || - (namelen > 4 && strcmp(&name[namelen - 4], ".bak") == 0)) { - fprintf(stderr, U_("%s/%s: %s"), dirpath, name, - U_("ignoring editor backup file")); - } else { - fprintf(stderr, U_("%s/%s: %s"), dirpath, name, - U_("ignoring file name containing '.'")); - } + fprintf(stderr, U_("%s/%s: %s"), dirpath, name, + U_("ignoring editor backup file")); fputc('\n', stderr); } continue;