From nobody Tue Nov 10 19:09:24 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jPGRT0gZyz6vHKq for ; Tue, 10 Nov 2026 19:09:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jPGRS5bVTz5796 for ; Tue, 10 Nov 2026 19:09:24 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794337764; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7Dsbuk9rKd2x9Rtf0RTUR3MzUulN3lhAPNjDUTvy3FI=; b=fnivK37QKV7rR9lGMghx3pFhAiUfzu96EcirUizlmWCOPGXiTvLUWbB3MRc82m1viV2lsn +hcipjIo7e6i28PxPIbxQmDvqzBYqyQMQ9K9I2hvKERoPNsvljXssrEGHfK4VpHb8Maf4o bBP56LPb86vQ9ccgiKzZYLdK0MaSm7migatvRGaPKY/2pXrjCUTuN/fiYUfYsbZAqaGnaf wh9XdV9eDs6WId4Lf9VSsBKRoVLp3yOx+4qiCN9+WgPRtDcCT5XqYqbASRFgKV4UNhY+PW ux4C3BunuOPm7FAYa+mOKU6MIA2omKroovd+QXoUNVwdRdfFaI554Co2jahIbA== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794337764; b=hhNxBzJqhICGyIKCjyJbg/WgOR/5AX5qHSM2269Fa8A12sKdEINpMDhuoctk9GS9/GCtdi bBc07gFLt9Wz038OGzEG1I8abgFKZcaqQD3TXmnih0Urjc2mlXfgNuOAST2qOAANfjR55V j0KgwLr/BtuLHG0DbiAs6t0wc3wIo3Zy8WQpa69v9MEjHRZVzyCDjh3vpCYCxuh5wsbcog x6g+hfBTBcKmIMJdWCKheSH+/7imUuDWgE40auoEabbxqCcVY5B9OeUA8cGFGEghT8NN04 y+tGTs7FaoQbZyzrn/6OHZuMvfLoZD1VpOvmgYhfwf4d8Zc9xHJVcM+LcFNogA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794337764; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7Dsbuk9rKd2x9Rtf0RTUR3MzUulN3lhAPNjDUTvy3FI=; b=f8L9APc6Po9GEBbUsx54b45eJIWzO5spCXOcjqRiIP0ouX+l+M3UGJxS6GYLnfgP9904OS cMvhCDnHjK6Ba1fWesiU+k9tmjsN+pIv4u4GresLcuTV1V3jK+ipj9PKqjA/u0kEdOz0SI iKJogqBt3ZS405hAL7U5GxyVrk38vifvnVTHDdPxDorDuc6yn30LNUNmVbawAm1/TVTZ0c kjKCuQ8D/ziq52RFJA94xSaASOx1HjTupLZPeJYlz2jXxPZrB97SsOQWBls7RntfQzp7WN GZvLuwHDErGXuo1E7qwvtb2tfptkr4qNq3staeVJPOyBMfSIlJLVLoxkcEoixw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jPGRS4WPqzhWN for ; Tue, 10 Nov 2026 19:09:24 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1c814 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 10 Nov 2026 19:09:24 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Muhammad Moinur Rahman Subject: git: b15b789d93c8 - main - pwd_mkdb: validate login names List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bofh X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: b15b789d93c804f30896ddd568af13a475a0d972 Auto-Submitted: auto-generated Date: Tue, 10 Nov 2026 19:09:24 +0000 Message-Id: <6af36be4.1c814.1793feaa@gitrepo.freebsd.org> The branch main has been updated by bofh: URL: https://cgit.FreeBSD.org/src/commit/?id=b15b789d93c804f30896ddd568af13a475a0d972 commit b15b789d93c804f30896ddd568af13a475a0d972 Author: Muhammad Moinur Rahman AuthorDate: 2026-11-10 14:15:10 +0000 Commit: Muhammad Moinur Rahman 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 + .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 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 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 + +.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 +# + +# 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 +}