git: b15b789d93c8 - main - pwd_mkdb: validate login names

From: Muhammad Moinur Rahman <bofh_at_FreeBSD.org>
Date: Tue, 10 Nov 2026 19:09:24 UTC
The branch main has been updated by bofh:

URL: https://cgit.FreeBSD.org/src/commit/?id=b15b789d93c804f30896ddd568af13a475a0d972

commit b15b789d93c804f30896ddd568af13a475a0d972
Author:     Muhammad Moinur Rahman <bofh@FreeBSD.org>
AuthorDate: 2026-11-10 14:15:10 +0000
Commit:     Muhammad Moinur Rahman <bofh@FreeBSD.org>
CommitDate: 2026-11-10 19:08:10 +0000

    pwd_mkdb: validate login names
    
    vipw(8) never looked at names, it just hands the file to pwd_mkdb,
    and pwd_mkdb only cared abuot the field layout. So something like
    `user1$(id$IFS-A)` in master.passwd went straight into the db.
    
    Do the same check pw(8) does, in scan(), so vipw, chpass and hand
    edits all hit it. NIS +/- entries get checked after the prefix.
    Bail out rather then warn; a file with junk names wont rebuild
    anymore, whcih is kinda the point.
    
    Add tests while here.
    
    Reported by:    Harry Sintonen (Reversec)
    Sponsored by:   Entersekt
    
    Approved by:    bapt
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D61405
---
 etc/mtree/BSD.tests.dist                 |   2 +
 usr.sbin/pwd_mkdb/Makefile               |   5 ++
 usr.sbin/pwd_mkdb/pwd_mkdb.8             |  23 ++++++-
 usr.sbin/pwd_mkdb/pwd_mkdb.c             |  37 +++++++++++
 usr.sbin/pwd_mkdb/tests/Makefile         |   3 +
 usr.sbin/pwd_mkdb/tests/Makefile.depend  |  10 +++
 usr.sbin/pwd_mkdb/tests/pwd_mkdb_test.sh | 108 +++++++++++++++++++++++++++++++
 7 files changed, 187 insertions(+), 1 deletion(-)

diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist
index 3ec8bd0f6942..67ed5dd81199 100644
--- a/etc/mtree/BSD.tests.dist
+++ b/etc/mtree/BSD.tests.dist
@@ -1325,6 +1325,8 @@
         ..
         pw
         ..
+	pwd_mkdb
+	..
         quot
         ..
         rpcbind
diff --git a/usr.sbin/pwd_mkdb/Makefile b/usr.sbin/pwd_mkdb/Makefile
index 708d32d231e3..3bd1136bc163 100644
--- a/usr.sbin/pwd_mkdb/Makefile
+++ b/usr.sbin/pwd_mkdb/Makefile
@@ -1,3 +1,5 @@
+.include <src.opts.mk>
+
 .PATH: ${SRCTOP}/lib/libc/gen		# for pw_scan.c
 
 PACKAGE=	runtime
@@ -10,4 +12,7 @@ CFLAGS+= -I${SRCTOP}/lib/libc/gen		# for pw_scan.h
 CFLAGS+=-I${.CURDIR}/bootstrap
 .endif
 
+HAS_TESTS=
+SUBDIR.${MK_TESTS}+= tests
+
 .include <bsd.prog.mk>
diff --git a/usr.sbin/pwd_mkdb/pwd_mkdb.8 b/usr.sbin/pwd_mkdb/pwd_mkdb.8
index c142a25a9d6c..9361a47fc950 100644
--- a/usr.sbin/pwd_mkdb/pwd_mkdb.8
+++ b/usr.sbin/pwd_mkdb/pwd_mkdb.8
@@ -25,7 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd April 19, 2025
+.Dd November 10, 2026
 .Dt PWD_MKDB 8
 .Os
 .Sh NAME
@@ -56,6 +56,27 @@ The file must be in the correct format (see
 It is important to note that the format used in this system is
 different from the historic Version 7 style format.
 .Pp
+Login names are subject to the same rules as in
+.Xr pw 8 :
+a name may not be empty, may not begin with a hyphen, and may not contain
+whitespace, control characters, 8-bit characters, or any of the following:
+.Bd -literal -offset indent
+, : + & # % $ ^ ( ) ! @ ~ * ? < > = | \e / " ; ' `
+.Ed
+.Pp
+As an exception, a
+.Ql $
+is permitted as the final character.
+For NIS compatibility entries
+.Pq Ql + , Ql +name , Ql -name , Ql +@netgroup , Ql -@netgroup
+the name is checked after the leading
+.Ql +
+or
+.Ql -
+and optional
+.Ql @ .
+A file containing an invalid login name is rejected.
+.Pp
 The options are as follows:
 .Bl -tag -width flag
 .It Fl C
diff --git a/usr.sbin/pwd_mkdb/pwd_mkdb.c b/usr.sbin/pwd_mkdb/pwd_mkdb.c
index 3c27b40044e3..f9b1c09c84f9 100644
--- a/usr.sbin/pwd_mkdb/pwd_mkdb.c
+++ b/usr.sbin/pwd_mkdb/pwd_mkdb.c
@@ -78,6 +78,7 @@ void	error(const char *);
 void	cp(char *, char *, mode_t mode);
 void	mv(char *, char *);
 int	scan(FILE *, struct passwd *);
+static int	name_ok(const char *);
 static void	usage(void);
 
 int
@@ -566,7 +567,43 @@ scan(FILE *fp, struct passwd *pw)
 fmt:		errno = EFTYPE;	/* XXX */
 		error(pname);
 	}
+	if (!name_ok(pw->pw_name)) {
+		warnx("invalid login name at line #%d", lcnt);
+		goto fmt;
+	}
+
+	return (1);
+}
 
+/*
+ * Apply the same character rules as pw_checkname() in usr.sbin/pw so that
+ * names pw(8) refuses cannot be introducde through vipw(8) or a hand edit.
+ * NIS compat entries (+, +name, -name, +@netgroup, -@netgroup) are checked
+ * after their prefix.
+ */
+static int
+name_ok(const char *name)
+{
+	static const char badchars[] = " ,\t:+&#%$^()!@~*?<>=|\\/\";'`";
+	const unsigned char *p;
+
+	p = (const unsigned char *)name;
+	if (*p == '+' || *p == '-') {
+		p++;
+		if (*p == '\0')
+			return (name[0] == '+');
+		if (*p == '@')
+			p++;
+	}
+	if (*p == '\0' || *p == '-')
+		return (0);
+	for (; *p != '\0'; p++) {
+		/* A trailing '$' is allowed for Samba machine accounts. */
+		if (*p == '$' && p[1] == '\0')
+			break;
+		if (*p < ' ' || *p >= 0x7f || strchr(badchars, *p) != NULL)
+			return (0);
+	}
 	return (1);
 }
 
diff --git a/usr.sbin/pwd_mkdb/tests/Makefile b/usr.sbin/pwd_mkdb/tests/Makefile
new file mode 100644
index 000000000000..cca68211d3fb
--- /dev/null
+++ b/usr.sbin/pwd_mkdb/tests/Makefile
@@ -0,0 +1,3 @@
+ATF_TESTS_SH+=	pwd_mkdb_test
+
+.include <bsd.test.mk>
diff --git a/usr.sbin/pwd_mkdb/tests/Makefile.depend b/usr.sbin/pwd_mkdb/tests/Makefile.depend
new file mode 100644
index 000000000000..11aba52f82cf
--- /dev/null
+++ b/usr.sbin/pwd_mkdb/tests/Makefile.depend
@@ -0,0 +1,10 @@
+# Autogenerated - do NOT edit!
+
+DIRDEPS = \
+
+
+.include <dirdeps.mk>
+
+.if ${DEP_RELDIR} == ${_DEP_RELDIR}
+# local dependencies - needed for -jN in clean tree
+.endif
diff --git a/usr.sbin/pwd_mkdb/tests/pwd_mkdb_test.sh b/usr.sbin/pwd_mkdb/tests/pwd_mkdb_test.sh
new file mode 100644
index 000000000000..a5de44bde27b
--- /dev/null
+++ b/usr.sbin/pwd_mkdb/tests/pwd_mkdb_test.sh
@@ -0,0 +1,108 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2026 Moin Rahman <bofh@FreeBSD.org>
+#
+
+# Write a master.passwd containing root foloowed by the given entry.
+mkpasswd()
+{
+	printf '%s\n' 'root:*:0:0::0:0:Charlie &:/root:/bin/sh' "$1" \
+	    > master.passwd
+	chmod 600 master.passwd
+}
+
+# Expect pwd_mkdb to reject the given login name and leave no databases.
+reject()
+{
+	mkpasswd "$1:*:1001:1001::0:0:test:/nonexistent:/usr/sbin/nologin"
+	atf_check -s exit:1 -e match:"invalid login name at line #2" \
+	    pwd_mkdb -p -d "$PWD" "$PWD/master.passwd"
+	atf_check test ! -e pwd.db
+	atf_check test ! -e spwd.db
+	atf_check test ! -e passwd
+}
+
+# Expect pwd_mkdb to accept the given master.passwd entry.
+accept()
+{
+	mkpasswd "$1"
+	atf_check pwd_mkdb -p -d "$PWD" "$PWD/master.passwd"
+	atf_check test -e pwd.db
+	atf_check test -e spwd.db
+	rm -f passwd pwd.db spwd.db
+}
+
+atf_test_case reject_shell_metachars
+reject_shell_metachars_head()
+{
+	atf_set "descr" "Login names with shell metacharacters are rejected"
+}
+reject_shell_metachars_body()
+{
+	reject 'user1$(id$IFS-A)'
+	reject 'a;b'
+	reject 'a|b'
+	reject 'a`id`'
+	reject "a'b"
+	reject 'a&b'
+	reject 'a>b'
+	reject 'a"b'
+	reject 'a\b'
+	reject 'a/b'
+	reject 'a b'
+	reject 'ma$ch'
+}
+
+atf_test_case reject_malformed
+reject_malformed_head()
+{
+	atf_set "descr" "Empty, hyphen-led, control and 8-bit login names are rejected"
+}
+reject_malformed_body()
+{
+	reject ''
+	reject "$(printf 'esc\033[2J')"
+	reject "$(printf 'tab\tname')"
+	reject "$(printf 'caf\303\251')"
+	reject "$(printf 'del\177')"
+}
+
+atf_test_case accept_valid
+accept_valid_head()
+{
+	atf_set "descr" "Valid login names, including a trailing dollar, are accepted"
+}
+accept_valid_body()
+{
+	accept 'first.last_1-x:*:1001:1001::0:0:test:/nonexistent:/usr/sbin/nologin'
+	accept 'mach$:*:1002:1002::0:0:test:/nonexistent:/usr/sbin/nologin'
+	accept '_daemon:*:1003:1003::0:0:test:/nonexistent:/usr/sbin/nologin'
+}
+
+atf_test_case nis_compat
+nis_compat_head()
+{
+	atf_set "descr" "NIS compat entries are accepted and their names checked"
+}
+nis_compat_body()
+{
+	accept '+:::::::::'
+	accept '+user:::::::::'
+	accept '-user:::::::::'
+	accept '+@netgroup:::::::::'
+	accept '-@netgroup:::::::::'
+	reject '-'
+	reject '+@'
+	reject '+@ng$(id)'
+	reject '-x$(id)'
+	reject '+-x'
+}
+
+atf_init_test_cases()
+{
+	atf_add_test_case reject_shell_metachars
+	atf_add_test_case reject_malformed
+	atf_add_test_case accept_valid
+	atf_add_test_case nis_compat
+}