]> git.ipfire.org Git - thirdparty/shadow.git/log
thirdparty/shadow.git
19 months agolib/, src/: Use SNPRINTF() instead of its pattern
Alejandro Colomar [Sat, 26 Aug 2023 10:32:32 +0000 (12:32 +0200)] 
lib/, src/: Use SNPRINTF() instead of its pattern

The variable declarations for the buffers have been aligned in this
commit, so that they appear in the diff, making it easier to review.

Some important but somewhat tangent changes included in this commit:

-  lib/nss.c: The size was being defined as 65, but then used as 64.
   That was a bug, although not an important one; we were just wasting
   one byte.  Fix that while we replace snprintf() by SNPRINTF(), which
   will get the size from sizeof(), and thus will use the real size.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
19 months agolib/string/sprintf.[ch]: Add [v]snprintf_()
Alejandro Colomar [Sat, 26 Aug 2023 12:54:44 +0000 (14:54 +0200)] 
lib/string/sprintf.[ch]: Add [v]snprintf_()

These functions are like [v]snprintf(3), but return -1 on truncation,
which makes it easier to test.  In fact, the API of swprintf(3), which
was invented later than snprintf(3), and is the wide-character version
of it, is identical to this snprintf_().

snprintf(3) is iseful in two cases:

-  We don't care if the output is truncated.  snprintf(3) is fine for
   those, and the return value can be ignored.  But snprintf_() is also
   fine for those.

-  Truncation is bad.  In that case, it's as bad as a hard error (-1)
   from snprintf, so merging both problems into the same error code
   makes it easier to handle errors.  Return the length if no truncation
   so that we can use it if necessary.

Not returning the whole length before truncation makes a better API,
which need not read the entire input, so it's less vulnerable to DoS
attacks when a malicious user controls the input.

Use these functions to implement SNPRINTF().

Cc: Samanta Navarro <ferivoz@riseup.net>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
19 months agolib/string/sprintf.h: Add SNPRINTF() macro
Alejandro Colomar [Sat, 26 Aug 2023 10:11:59 +0000 (12:11 +0200)] 
lib/string/sprintf.h: Add SNPRINTF() macro

It wraps snprintf(3) so that it performs some steps that one might
forget, or might be prone to accidents:

-  It calculates the size of the destination buffer, and makes sure it's
   an array (otherwise, using sizeof(s) would be very bad).

-  It calculates if there's truncation or an error, returning -1 if so.

BTW, this macro doesn't have any issues of double evaluation, because
sizeof() doesn't evaluate its argument (unless it's a VLA, but then the
static_assert(3) within NITEMS() makes sure VLAs are not allowed).

This macro is very similar to STRTCPY(), defined in
<lib/string/strtcpy.h>.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
19 months agosrc/chfn,chpasswd,newusers: declare fatal_exit() NORETURN
Christian Göttsche [Mon, 11 Dec 2023 17:12:24 +0000 (18:12 +0100)] 
src/chfn,chpasswd,newusers: declare fatal_exit() NORETURN

Help static analyzers to understand fatal_exit() does never return.

19 months agolib: avoid format truncation
Christian Göttsche [Mon, 11 Dec 2023 16:53:28 +0000 (17:53 +0100)] 
lib: avoid format truncation

    commonio.c: In function 'commonio_unlock':
    commonio.c:487:49: warning: '.lock' directive output may be truncated writing 5 bytes into a region of size between 1 and 1024 [-Wformat-truncation=]
      487 |                 snprintf (lock, sizeof lock, "%s.lock", db->filename);
          |                                                 ^~~~~
    commonio.c:487:17: note: 'snprintf' output between 6 and 1029 bytes into a destination of size 1024
      487 |                 snprintf (lock, sizeof lock, "%s.lock", db->filename);
          |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

19 months agolib: avoid double close on error
Christian Göttsche [Mon, 11 Dec 2023 16:45:26 +0000 (17:45 +0100)] 
lib: avoid double close on error

    log.c:90:24: warning: double 'close' of file descriptor 'fd' [CWE-1341] [-Wanalyzer-fd-double-close]
    failure.c:94:24: warning: double 'close' of file descriptor 'fd' [CWE-1341] [-Wanalyzer-fd-double-close]
    failure.c:193:32: warning: double 'close' of file descriptor 'fd' [CWE-1341] [-Wanalyzer-fd-double-close]
    utmp.c:103:24: warning: double 'close' of file descriptor 'fd' [CWE-1341] [-Wanalyzer-fd-double-close]

19 months agoUpdate close(2) checking
Christian Göttsche [Mon, 11 Dec 2023 16:32:13 +0000 (17:32 +0100)] 
Update close(2) checking

Check for close(2) failure at more places closing a file descriptor
written to.

Also ignore failures with errno set to EINTR (see man:close(2) for
details).

19 months agosrc/useradd: free string
Christian Göttsche [Mon, 11 Dec 2023 16:27:44 +0000 (17:27 +0100)] 
src/useradd: free string

    useradd.c:2329:10: warning: Potential leak of memory pointed to by 'btrfs_check' [unix.Malloc]

19 months agolib/failure,utmp: update error messages
Christian Göttsche [Mon, 11 Dec 2023 16:21:25 +0000 (17:21 +0100)] 
lib/failure,utmp: update error messages

Include errno description.

19 months agolib/utmp: merge file access
Christian Göttsche [Mon, 11 Dec 2023 16:18:38 +0000 (17:18 +0100)] 
lib/utmp: merge file access

Avoid checking if the file exists before opening it.

Resolves a CodeQL report of Time-of-check time-of-use filesystem race
condition.

19 months agosrc/useradd: avoid usage of sprintf
Christian Göttsche [Mon, 11 Dec 2023 16:13:43 +0000 (17:13 +0100)] 
src/useradd: avoid usage of sprintf

sprintf(3) does not take the destination buffer into account. Although
the destination in these case is large enough, sprintf(3) indicates a
code smell.

Use the xasprintf() wrapper.

19 months agosrc/usermod,groups: use checked malloc
Christian Göttsche [Mon, 11 Dec 2023 16:09:06 +0000 (17:09 +0100)] 
src/usermod,groups: use checked malloc

    usermod.c:2165:24: warning: dereference of possibly-NULL ‘user_groups’ [CWE-690] [-Wanalyzer-possible-null-dereference]

19 months agolib/, src/: Align variable definitions
Alejandro Colomar [Sat, 26 Aug 2023 11:16:42 +0000 (13:16 +0200)] 
lib/, src/: Align variable definitions

This is just a cosmetic patch in preparation for others.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
19 months agosrc/login.c: Group preprocessor conditionals
Alejandro Colomar [Sat, 26 Aug 2023 10:35:53 +0000 (12:35 +0200)] 
src/login.c: Group preprocessor conditionals

Group them at the end of the list of variable definitions, and use
'#if defined()' instead of '#if[n]def'.  Also indent nested ones.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
19 months agolib/defines.h: Remove ITI_AGING
Tobias Stoeckmann [Tue, 12 Dec 2023 16:37:30 +0000 (17:37 +0100)] 
lib/defines.h: Remove ITI_AGING

ITI_AGING is not set through any build environment. If it would be set,
then timings in /etc/shadow would not fit anymore.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
19 months agosrc/su.c: Fix type of variable
Alejandro Colomar [Wed, 13 Dec 2023 14:46:53 +0000 (15:46 +0100)] 
src/su.c: Fix type of variable

su.c:678:26: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘const void *’ [-Wformat=]
su.c:681:44: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘const void *’ [-Wformat=]
su.c:683:46: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘const void *’ [-Wformat=]

Reported-by: Christian Göttsche <cgzones@googlemail.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
19 months agolib/, src/: snprintf(3) already terminates strings with NUL
Alejandro Colomar [Sat, 26 Aug 2023 09:53:25 +0000 (11:53 +0200)] 
lib/, src/: snprintf(3) already terminates strings with NUL

We don't need to terminate them manually after the call.  Remove all
that paranoid code, which in some cases was even wrong.  While at it,
let's do a few more things:

-  Use sizeof(buf) for the size of the buffer.  I found that a few cases
   were passing one less byte (probably because the last one was
   manually zeroed later).  This caused a double NUL.  snprintf(3) wants
   the size of the entire buffer to properly terminate it.  Passing the
   exact value hardcoded is brittle, so use sizeof().

-  Align and improve style of variable declarations.  This makes them
   appear in this diff, which will help review the patch.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/: Use ATTR_STRING() on stpecpy() and strtcpy()
Alejandro Colomar [Sun, 26 Nov 2023 17:52:56 +0000 (18:52 +0100)] 
lib/: Use ATTR_STRING() on stpecpy() and strtcpy()

These functions consume a source string.  Document that.  There's no way
to mark that they also produce a string in dst, though.  That will be up
to the static analyzer to guess.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/attr.h: Add ATTR_STRING() attribute macro
Alejandro Colomar [Sun, 26 Nov 2023 17:38:40 +0000 (18:38 +0100)] 
lib/attr.h: Add ATTR_STRING() attribute macro

It signals that a function parameter is a string _before_ the call.

Suggested-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/, src/: Fix error handling after strto[u]l[l](3)
Alejandro Colomar [Fri, 1 Dec 2023 18:31:16 +0000 (19:31 +0100)] 
lib/, src/: Fix error handling after strto[u]l[l](3)

-  Set errno = 0 before the call.  Otherwise, it may contain anything.
-  ERANGE is not the only possible errno value of these functions.  They
   can also set it to EINVAL.
-  Any errno value after these calls is bad; just compare against 0.
-  Don't check for the return value; just errno.  This function is
   guaranteed to not modify errno on success (POSIX).
-  Check endptr == str, which may or may not set EINVAL.

Suggested-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/prefix_flag.c: Invert conditional to remove a branch
Alejandro Colomar [Fri, 1 Dec 2023 17:57:31 +0000 (18:57 +0100)] 
lib/prefix_flag.c: Invert conditional to remove a branch

This simplifies the code, and is preparation for a following commit.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/string/strtcpy.h: Don't use a ternary op, to silence a -Wsign-compare warning
Alejandro Colomar [Sat, 2 Sep 2023 12:33:47 +0000 (14:33 +0200)] 
lib/string/strtcpy.h: Don't use a ternary op, to silence a -Wsign-compare warning

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agosrc/gpasswd.c: Simplify cpp conditional
Alejandro Colomar [Sat, 2 Sep 2023 14:19:58 +0000 (16:19 +0200)] 
src/gpasswd.c: Simplify cpp conditional

Since failure() is [[noreturn]], we can invert the conditional so that
we don't need an else.  This silences a -Wunused-parameter warning.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agosrc/gpasswd.c: Reduce scope of cpp conditional
Alejandro Colomar [Sat, 2 Sep 2023 14:19:58 +0000 (16:19 +0200)] 
src/gpasswd.c: Reduce scope of cpp conditional

This prepares for the next patch, which will invert the logic of the
conditional.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agosrc/gpasswd.c: Mark failure() as [[noreturn]]
Alejandro Colomar [Sat, 2 Sep 2023 14:17:54 +0000 (16:17 +0200)] 
src/gpasswd.c: Mark failure() as [[noreturn]]

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agosrc/gpasswd.c: Move if out of cpp conditional
Alejandro Colomar [Sat, 2 Sep 2023 14:14:03 +0000 (16:14 +0200)] 
src/gpasswd.c: Move if out of cpp conditional

This simplifies the code a little bit, and prepares for the next
commits, which will clean up further.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agosrc/login_nopam.c: Add missing 'const'
Alejandro Colomar [Sat, 2 Sep 2023 14:10:32 +0000 (16:10 +0200)] 
src/login_nopam.c: Add missing 'const'

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agoautogen.sh: CFLAGS: Add -Wno-expansion-to-defined
Alejandro Colomar [Sat, 2 Sep 2023 13:59:08 +0000 (15:59 +0200)] 
autogen.sh: CFLAGS: Add -Wno-expansion-to-defined

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/obscure.c: Mark parameter as [[maybe_unused]]
Alejandro Colomar [Sat, 2 Sep 2023 13:50:43 +0000 (15:50 +0200)] 
lib/obscure.c: Mark parameter as [[maybe_unused]]

It's only used in certain builds.  This is to silence a -Wunused-parameter warning.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/loginprompt.c: Remove dead code
Alejandro Colomar [Sat, 2 Sep 2023 13:46:56 +0000 (15:46 +0200)] 
lib/loginprompt.c: Remove dead code

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/limits.c: Check for overflow without invoking UB
Alejandro Colomar [Sat, 2 Sep 2023 13:43:24 +0000 (15:43 +0200)] 
lib/limits.c: Check for overflow without invoking UB

The multiplication was already invoking UB.  The test was flawed.
Use __builtin_mul_overflow() instead.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/limits.c: Fix wrong error check
Alejandro Colomar [Sat, 2 Sep 2023 13:24:06 +0000 (15:24 +0200)] 
lib/limits.c: Fix wrong error check

strtol(3) doesn't specify a return value if (value == endptr).
It is always an error, if (value==endptr).

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/copydir.c: Cosmetic
Alejandro Colomar [Sat, 2 Sep 2023 12:58:55 +0000 (14:58 +0200)] 
lib/copydir.c: Cosmetic

I was investigating a warning in this function, but the code was
inscrutable.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/commonio.c: Use uintmax_t to print nlink_t
Alejandro Colomar [Sat, 2 Sep 2023 12:29:07 +0000 (14:29 +0200)] 
lib/commonio.c: Use uintmax_t to print nlink_t

See uintmax_t(3type).

While at it, remove the useless cast to (void).

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/chkname.c: Use tmp variable to avoid a -Wsign-compare warning
Alejandro Colomar [Sat, 2 Sep 2023 12:15:43 +0000 (14:15 +0200)] 
lib/chkname.c: Use tmp variable to avoid a -Wsign-compare warning

I used size_t because:

sysconf(3) can return -1 if the value is not supported, but then it can
only mean that there's no limit.  Having no limit is the same as having
a limit of SIZE_MAX (to which -1 is converted).

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agoautogen.sh: CFLAGS: Add -Wextra
Alejandro Colomar [Sat, 2 Sep 2023 11:40:12 +0000 (13:40 +0200)] 
autogen.sh: CFLAGS: Add -Wextra

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/string/: Fortify source of strtcpy(), stpecpy(), and zustr2stp()
Alejandro Colomar [Fri, 24 Nov 2023 22:56:17 +0000 (23:56 +0100)] 
lib/string/: Fortify source of strtcpy(), stpecpy(), and zustr2stp()

By writing the terminating null byte via stpcpy(3), we take advantage of
_FORTIFY_SOURCE for the last byte, which was unprotected before this
commit.

Reported-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/env.c: Replace strncpy(3) call by stpcpy(mempcpy(), "")
Alejandro Colomar [Wed, 15 Nov 2023 21:49:13 +0000 (22:49 +0100)] 
lib/env.c: Replace strncpy(3) call by stpcpy(mempcpy(), "")

We were using strncpy(3), which is designed to copy from a string into a
(null-padded) fixed-size character array.  However, we were doing the
opposite: copying from a known-size array (which was a prefix of a
string), into a string.  That's why we had to manually zero the buffer
afterwards.

Use instead mempcpy(3) to copy the non-null bytes, and then terminate
with a null byte with stpcpy(..., "").

Cc: "Serge E. Hallyn" <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/string/: Move string-related files to string/ subdir
Alejandro Colomar [Tue, 28 Nov 2023 01:48:37 +0000 (02:48 +0100)] 
lib/string/: Move string-related files to string/ subdir

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agoconfigure.ac: AM_INIT_AUTOMAKE: Use [subdir-objects]
Alejandro Colomar [Sat, 2 Dec 2023 20:55:02 +0000 (21:55 +0100)] 
configure.ac: AM_INIT_AUTOMAKE: Use [subdir-objects]

This will allow using subdirs.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/, src/: Say 'long' instead of 'long int'
Alejandro Colomar [Tue, 28 Nov 2023 01:05:45 +0000 (02:05 +0100)] 
lib/, src/: Say 'long' instead of 'long int'

We were using 'long' in most places, so be consistent and use it
everywhere.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/attr.h, lib/, src/: Move attributes to new header file
Alejandro Colomar [Tue, 28 Nov 2023 01:27:08 +0000 (02:27 +0100)] 
lib/attr.h, lib/, src/: Move attributes to new header file

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agosrc: add missing declaration of `getdef_bool`
Sergei Trofimovich [Fri, 1 Dec 2023 23:04:43 +0000 (23:04 +0000)] 
src: add missing declaration of `getdef_bool`

Upcoming `gcc-14` enabled a few warnings into errors, like
`-Wimplicit-function-declaration`. This caused `shadow` build to fail
as:

    pwunconv.c: In function 'main':
    pwunconv.c:132:13: error: implicit declaration of function 'getdef_bool' [-Wimplicit-function-declaration]
      132 |         if (getdef_bool("USE_TCB")) {
          |             ^~~~~~~~~~~

The change adds missing include headers.

20 months agolib/defines.h: Remove condition on __STRICT_ANSI__
Alejandro Colomar [Mon, 27 Nov 2023 23:12:28 +0000 (00:12 +0100)] 
lib/defines.h: Remove condition on __STRICT_ANSI__

We require C11 since a few releases ago.  It seems I missed this
reminder of ANSI C (C89) back then.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agotests/unit/test_chkname.c: Test is_valid_user_name()
Alejandro Colomar [Sun, 26 Nov 2023 22:21:44 +0000 (23:21 +0100)] 
tests/unit/test_chkname.c: Test is_valid_user_name()

Suggested-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agochsh: limit acceptable shells to absolute paths
Tobias Stoeckmann [Sat, 11 Nov 2023 22:10:55 +0000 (23:10 +0100)] 
chsh: limit acceptable shells to absolute paths

If an entry in /etc/shells is not an absolute path (comments or
partial reads due to fgets), the line should not be considered as
a valid login shell.

In general all systems should have getusershells, but let's better
be safe than sorry.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
20 months agolib/: Use NITEMS() instead of SIZEOF_ARRAY() where number of elements is meant
Alejandro Colomar [Thu, 23 Nov 2023 00:06:52 +0000 (01:06 +0100)] 
lib/: Use NITEMS() instead of SIZEOF_ARRAY() where number of elements is meant

For arrays of char, both NITEMS() and SIZEOF_ARRAY() return the same
value.  However, NITEMS() is more appropriate.  Think of wide-character
equivalents of the same code; with NITEMS(), they would continue to be
valid, while with SIZEOF_ARRAY(), they would be wrong.

In the implementation of ZUSTR2STP(), we want SIZEOF_ARRAY() within the
static assert, because we're just comparing the sizes of the source and
destination buffers, and we don't care if we compare sizes or numbers of
elements, and using sizes is just simpler.  But we want NITEMS() in the
zustr2stp() call, where we want to copy a specific number of characters.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/chkname.c: Update regex for valid names
Alejandro Colomar [Mon, 13 Nov 2023 12:33:37 +0000 (13:33 +0100)] 
lib/chkname.c: Update regex for valid names

The maximum length of 32 wasn't being enforced in the code, and POSIX
doesn't specify that maximum length either, so it seems it was an
arbitrary limit of the past that doesn't exist any more.  Use a regex
that has no length limit.

Closes: <https://github.com/shadow-maint/shadow/issues/836>
Link: <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html>
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agotests/unit/test_strncpy.c: Test STRNCPY()
Alejandro Colomar [Thu, 16 Nov 2023 10:42:26 +0000 (11:42 +0100)] 
tests/unit/test_strncpy.c: Test STRNCPY()

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/: Use STRNCPY() instead of strncpy(3)
Alejandro Colomar [Wed, 15 Nov 2023 21:36:19 +0000 (22:36 +0100)] 
lib/: Use STRNCPY() instead of strncpy(3)

We've recently fixed several bugs in the calculation of the size in this
function call.  Use this wrapper to prevent similar mistakes in the
future.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/strncpy.h: Add STRNCPY() wrapper for strncpy(3)
Alejandro Colomar [Wed, 15 Nov 2023 21:31:02 +0000 (22:31 +0100)] 
lib/strncpy.h: Add STRNCPY() wrapper for strncpy(3)

This wrapper calculates the destination buffer's size, to avoid errors
in the size calculation.

A curious fact: this macro did exist in Version 7 Unix (with a slightly
different name).  I found it by chance, investigating the origins of
strncpy(3) and strncat(3) in V7, after Branden suggested me to do so,
related to recent discussions about string_copying(7).

alx@debian:~/src/unix/unix/Research-V7$ grepc SCPYN .
./usr/src/cmd/login.c:#define SCPYN(a, b) strncpy(a, b, sizeof(a))

Our implementation is slightly better, because using nitems() we're
protected against passing a pointer instead of an array, and it's also
conceptually more appropriate: for wide characters, it would be

#define WCSNCPY(dst, src)  wcsncpy(dst, src, NITEMS(dst))

Cc: "G. Branden Robinson" <branden@debian.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/: Remove off-by-one bugs in calls to strncpy(3)
Alejandro Colomar [Wed, 15 Nov 2023 21:14:18 +0000 (22:14 +0100)] 
lib/: Remove off-by-one bugs in calls to strncpy(3)

We're not even zeroing the last byte after this call.  This was a
completely gratuitous truncation of one byte, and the resulting
character array still wasn't guaranteed to be null terminated, because
strncpy(3) can't do that.

Just to clarify, none of these structures needed zeroing, as they are
treated as null-padded fixed-size character arrays.  Calling strncpy(3)
was actually the correct call, and the only problem was unnecessarily
truncating strings by one byte more than necessary.

Cc: Matthew House <mattlloydhouse@gmail.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/log.c: Replace strncpy(3) call by STRTCPY()
Alejandro Colomar [Wed, 15 Nov 2023 23:08:47 +0000 (00:08 +0100)] 
lib/log.c: Replace strncpy(3) call by STRTCPY()

This call was too clever.  It relied on the last byte of ll_line
being 0 due to a previous memzero() and not writing to it later.
Write an explicit terminating null byte, by using STRTCPY().

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/failure.c: Replace strncpy(3) call by STRTCPY()
Alejandro Colomar [Wed, 15 Nov 2023 22:58:23 +0000 (23:58 +0100)] 
lib/failure.c: Replace strncpy(3) call by STRTCPY()

This call was way too clever.  It relied on the last byte of fail_line
being 0 due to it being in a static structure and never writing to it.
Write an explicit terminating null byte, by using STRTCPY().

Cc: Matthew House <mattlloydhouse@gmail.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/utmp.c: Replace strncpy(3) call by ZUSTR2STP()
Alejandro Colomar [Tue, 7 Nov 2023 17:03:08 +0000 (18:03 +0100)] 
lib/utmp.c: Replace strncpy(3) call by ZUSTR2STP()

We were copying from a (zero-padded) fixed-width character array to a
string, but strncpy(3) is meant to do the opposite thing.  ZUSTR2STP()
is designed to be used in this case (like strncat(3)).

Fixes: f40bdfa66a3a ("libmisc: implement `get_session_host()`")
Cc: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agotests/: Remove references to cracklib
Alejandro Colomar [Mon, 30 Oct 2023 12:36:04 +0000 (13:36 +0100)] 
tests/: Remove references to cracklib

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agoRemove libcrack support
Alejandro Colomar [Mon, 30 Oct 2023 12:31:42 +0000 (13:31 +0100)] 
Remove libcrack support

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agoRemove FascistHistory() and FascistHistoryPw() calls
Alejandro Colomar [Mon, 30 Oct 2023 11:53:37 +0000 (12:53 +0100)] 
Remove FascistHistory() and FascistHistoryPw() calls

These functions don't seem to exist anymore.  I can't find them in
Debian, nor in a web search.  They probably were functions from an
ancient implementation of cracklib that doesn't exist anymore.

$ git remote -v
origin git@github.com:cracklib/cracklib.git (fetch)
origin git@github.com:cracklib/cracklib.git (push)
$ grep -rni fascisthistory
$ git log --grep FascistHistory
$ git log -S FascistHistory

Closes: <https://codesearch.debian.net/search?q=FascistHistory&literal=1>
Cc: Mike Frysinger <vapier@gentoo.org>
Acked-by: Michael Vetter <jubalh@iodoru.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/date_to_str.c: strftime(3) leaves the buffer undefined on failure
Alejandro Colomar [Thu, 16 Nov 2023 14:56:25 +0000 (15:56 +0100)] 
lib/date_to_str.c: strftime(3) leaves the buffer undefined on failure

strftime(3) makes no guarantees about the contents of the buffer if the
formatted string wouldn't fit in the buffer.  It simply returns 0, and
it's the programmer's responsibility to do the right thing after that.

Let's write the string "future" if there's an error, similar to what we
do with gmtime(3)'s errors.

Also, `buf[size - 1] = '\0';` didn't make sense.  If the copy fits,
strftime(3) guarantees to terminate with NUL.  If it doesn't, the entire
contents of buf are undefined, so adding a NUL at the end of the buffer
would be dangerous: the string could contain anything, such as
"gimme root access now".  Remove that, now that we set the string to
"future", as with gmtime(3) errors.  This setting to '\0' comes from the
times when we used strncpy(3) in the implementation, and should have
been removed when I changed it to use strlcpy(3); however, I didn't
check we didn't need it anymore.

Cc: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agosrc/logoutd.c: Fix theoretical buffer overrun
Alejandro Colomar [Wed, 15 Nov 2023 23:26:23 +0000 (00:26 +0100)] 
src/logoutd.c: Fix theoretical buffer overrun

ut_line doesn't hold a string.  It is a null-padded fixed-width array.
Luckily, I don't think there has ever existed a ut_line ("/dev/tty*")
that was 32 bytes long.  That would have resulted in a buffer overrun.
Anyway, do the right thing, which is copying into a temporary string.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/date_to_str.c, configure.ac: Replace calls to strlcpy(3) by strtcpy(3)
Alejandro Colomar [Sun, 12 Nov 2023 13:10:57 +0000 (14:10 +0100)] 
lib/date_to_str.c, configure.ac: Replace calls to strlcpy(3) by strtcpy(3)

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/date_to_str.c: Add missing include <config.h>
Alejandro Colomar [Sun, 12 Nov 2023 22:20:15 +0000 (23:20 +0100)] 
lib/date_to_str.c: Add missing include <config.h>

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/Makefile.am: Add missing source file
Alejandro Colomar [Sun, 12 Nov 2023 13:10:07 +0000 (14:10 +0100)] 
lib/Makefile.am: Add missing source file

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agosrc/, lib/, tests/: Rename files defining strtcpy()
Alejandro Colomar [Sun, 12 Nov 2023 13:08:42 +0000 (14:08 +0100)] 
src/, lib/, tests/: Rename files defining strtcpy()

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agosrc/, lib/, tests/: Rename STRLCPY() to STRTCPY()
Alejandro Colomar [Sun, 12 Nov 2023 13:00:47 +0000 (14:00 +0100)] 
src/, lib/, tests/: Rename STRLCPY() to STRTCPY()

It is a wrapper around STRTCPY(), so use a proper name.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/strlcpy.[ch]: Implement strtcpy(3) to replace strlcpy_()
Alejandro Colomar [Sun, 12 Nov 2023 12:43:32 +0000 (13:43 +0100)] 
lib/strlcpy.[ch]: Implement strtcpy(3) to replace strlcpy_()

There's been a very long and interesting discussion in linux-man@ and
libc-alpha@, where we've discussed all the string-copying functions,
their pros and cons, when should each be used and avoided, etc.

Paul Eggert pointed out an important problem of strlcpy(3): it is
vulnerable to DoS attacks if an attacker controls the length of the
source string.  And even if it doesn't control it, the function is dead
slow (because its API forces it to calculate strlen(src)).

We've agreed that the general solution for a truncating string-copying
function is to write a wrapper over strnlen(3)+memcpy(3), which is
limited to strnlen(src, sizeof(dst)).  This is not vulnerable to DoS,
and is very fast for all buffer sizes.  string_copying(7) has been
updated to reflect this, and provides a reference implementation for
this wrapper function.

This strtcpy(3) (t for truncation) wrapper happens to have the same API
that our strlcpy_() function had, so replace it with the better
implementation.  We don't need to update callers nor tests, since the
API is the same.

A future commit will rename STRLCPY() to STRTCPY(), and replace
remaining calls to strlcpy(3) by calls to this strtcpy(3).

Link: <https://lore.kernel.org/linux-man/ZU4SDh-Se5gjPny5@debian/T/#mfb5a3fdeb35487dec6f8d9e3d8548bd0d92c4975/>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agolib/strlcpy.[ch]: Fix return type
Alejandro Colomar [Sat, 2 Sep 2023 12:21:49 +0000 (14:21 +0200)] 
lib/strlcpy.[ch]: Fix return type

To return an error code, we need ssize_t.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agotests/unit/test_strlcpy.c: Test strlcpy_() and STRLCPY()
Alejandro Colomar [Fri, 20 Oct 2023 12:58:29 +0000 (14:58 +0200)] 
tests/unit/test_strlcpy.c: Test strlcpy_() and STRLCPY()

This test fails now, due to a bug: the return type of strlcpy_() is
size_t, but it should be ssize_t.  The next commit will pass the test,
by fixing the bug.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agoREADME.md, STABLE.md: record the stable branch URL(s).
Alejandro Colomar [Wed, 27 Sep 2023 16:45:51 +0000 (11:45 -0500)] 
README.md, STABLE.md: record the stable branch URL(s).

Acked-by: Serge Hallyn <serge@hallyn.com>
Acked-by: Iker Pedrosa <ipedrosa@redhat.com>
Cc: Sam James <sam@gentoo.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
20 months agoDefine SUBUID_FILE/SUBGID_FILE
Joakim Tjernlund [Wed, 1 Nov 2023 23:00:03 +0000 (00:00 +0100)] 
Define SUBUID_FILE/SUBGID_FILE

These where hard coded, make them definable like SHADOW_FILE

Signed-off-by: Joakim Tjernlund <joakim.tjernlund@infinera.com>
20 months agoCI: fix Fedora 39 build
Iker Pedrosa [Mon, 13 Nov 2023 08:53:10 +0000 (09:53 +0100)] 
CI: fix Fedora 39 build

libbsd is unwanted in Fedora and RHEL, and the recently released Fedora
39 doesn't contain this dependency in the base image.

shadow removed libbsd from its dependencies for Fedora 39, so let's
build without it to avoid compilation errors.

Resolves: https://github.com/shadow-maint/shadow/issues/839

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
Reviewed-by: Alejandro Colomar <alx@kernel.org>
21 months agolib/utmp.c: Don't check for NULL before free(3)
Alejandro Colomar [Sun, 29 Oct 2023 18:46:02 +0000 (19:46 +0100)] 
lib/utmp.c: Don't check for NULL before free(3)

free(NULL) is valid; there's no need to check for NULL.  Simplify.

Fixes: 5178f8c5afb6 ("utmp: call prepare_utmp() even if utent is NULL")
Signed-off-by: Alejandro Colomar <alx@kernel.org>
21 months agoAdd keys/ directory with public keys for maintainers 825/head
Serge Hallyn [Thu, 26 Oct 2023 21:40:50 +0000 (16:40 -0500)] 
Add keys/ directory with public keys for maintainers

These can be used to verify releases.

Signed-off-by: Serge Hallyn <serge@hallyn.com>
21 months agoman: document --prefix option in chage, chpasswd and passwd
Michael Vetter [Fri, 20 Oct 2023 13:22:35 +0000 (15:22 +0200)] 
man: document --prefix option in chage, chpasswd and passwd

Support for `--prefix` was added in
https://github.com/shadow-maint/shadow/pull/714 and is available since
shadow 4.14.0.

Close https://github.com/shadow-maint/shadow/issues/822

21 months agolibmisc/copydir: do not forget errors from directory copy
Christian Göttsche [Thu, 26 Jan 2023 20:37:30 +0000 (21:37 +0100)] 
libmisc/copydir: do not forget errors from directory copy

    copydir.c:429:4: warning: Value stored to 'err' is never read [deadcode.DeadStores]

Also reduce indentation by bailing out early.

(cherry picked from commit d89f2fb06d1b81b56299f9d0bfe7a927a2282f19)

21 months agoImprove the login.defs unknown item error message
Serge Hallyn [Wed, 4 Oct 2023 15:38:48 +0000 (10:38 -0500)] 
Improve the login.defs unknown item error message

Closes #746

Only print the 'unknown item' message to syslog if we are
actually parsing a login.defs.  Prefix it with "shadow:" to make
it clear in syslog where it came from.

Also add the source filename to the console message.  I'm not
quite clear on the econf API, so not sure whether in that path we
will end up actually having the path, or printing ''.

Signed-off-by: Serge Hallyn <serge@hallyn.com>
21 months agoautogen.sh: Prepare CFLAGS before ./configure
Alejandro Colomar [Sat, 2 Sep 2023 15:31:15 +0000 (17:31 +0200)] 
autogen.sh: Prepare CFLAGS before ./configure

Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
21 months agolib/: Add missing #include <config.h>
Alejandro Colomar [Fri, 1 Sep 2023 17:03:05 +0000 (19:03 +0200)] 
lib/: Add missing #include <config.h>

Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
21 months agoautogen.sh: CFLAGS: Add -Werror=implicit-function-declaration
Alejandro Colomar [Fri, 1 Sep 2023 16:57:41 +0000 (18:57 +0200)] 
autogen.sh: CFLAGS: Add -Werror=implicit-function-declaration

This is not just a style issue.  This should be a hard error, and never
compile.  ISO C89 already had this feature as deprecated.  ISO C99
removed this deprecated feature, for good reasons.  If we compile
ignoring this warning, shadow is not going to behave well.

Cc: Sam James <sam@gentoo.org>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
21 months agolib/, src/: Use xasprintf() instead of its pattern
Alejandro Colomar [Sat, 2 Sep 2023 16:29:26 +0000 (18:29 +0200)] 
lib/, src/: Use xasprintf() instead of its pattern

Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
21 months agolib/, src/: Use asprintf(3) instead of strlen(3)+malloc(3)+snprintf(3)
Alejandro Colomar [Fri, 25 Aug 2023 19:27:05 +0000 (21:27 +0200)] 
lib/, src/: Use asprintf(3) instead of strlen(3)+malloc(3)+snprintf(3)

asprintf(3) is non-standard, but is provided by GNU, the BSDs, and musl.
That makes it portable enough for us to use.

This function is much simpler than the burdensome code for allocating
the right size.  Being simpler, it's thus safer.

I took the opportunity to fix the style to my preferred one in the
definitions of variables used in these calls, and also in the calls to
free(3) with these pointers.  That isn't gratuituous, but has a reason:
it makes those appear in the diff for this patch, which helps review it.
Oh, well, I had an excuse :)

Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
21 months agolib/copydir.c: Use goto to reduce a conditional branch
Alejandro Colomar [Wed, 4 Oct 2023 16:46:48 +0000 (18:46 +0200)] 
lib/copydir.c: Use goto to reduce a conditional branch

Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
21 months agotests/unit/test_xasprintf.c: Test x[v]asprintf()
Alejandro Colomar [Fri, 6 Oct 2023 15:44:21 +0000 (17:44 +0200)] 
tests/unit/test_xasprintf.c: Test x[v]asprintf()

Link: <https://github.com/shadow-maint/shadow/pull/816>
Suggested-by: Iker Pedrosa <ipedrosa@redhat.com>
Acked-by: Andreas Schneider <https://github.com/cryptomilk>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
21 months agolib/sprintf.[ch]: Add x[v]asprintf()
Alejandro Colomar [Fri, 25 Aug 2023 23:27:12 +0000 (01:27 +0200)] 
lib/sprintf.[ch]: Add x[v]asprintf()

As other x...() wrappers around functions that allocate, these wrappers
are like [v]asprintf(3), but exit on failure.

Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
21 months agolib/copydir.c: Invert conditional to reduce nesting
Alejandro Colomar [Fri, 25 Aug 2023 20:23:24 +0000 (22:23 +0200)] 
lib/copydir.c: Invert conditional to reduce nesting

Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
21 months agoFix badname option to be singular just like useradd.
Dimitri John Ledkov [Fri, 13 Oct 2023 00:44:11 +0000 (01:44 +0100)] 
Fix badname option to be singular just like useradd.

Badnames still accepted, note that previously usage already stated
singular form, whilst manpage and real one was plural only.

Fixes: 45d6746219 ("src: correct "badname" option")
Signed-off-by: Dimitri John Ledkov <dimitri.ledkov@canonical.com>
21 months agoFix mixed-whitespace
Dimitri John Ledkov [Fri, 13 Oct 2023 00:43:00 +0000 (01:43 +0100)] 
Fix mixed-whitespace

Signed-off-by: Dimitri John Ledkov <dimitri.ledkov@canonical.com>
22 months agoRemove TODO
Iker Pedrosa [Tue, 5 Sep 2023 14:13:44 +0000 (16:13 +0200)] 
Remove TODO

Sad to remove this file, but things are going on and it doesn't seem to
be up to date.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
22 months agoRemove shadow.spec.in
Iker Pedrosa [Tue, 5 Sep 2023 14:11:08 +0000 (16:11 +0200)] 
Remove shadow.spec.in

The file isn't up to date with the latest development, the last change
was made 15 years ago, so I'm removing it.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
22 months agoRemove .travis.yml
Iker Pedrosa [Mon, 4 Sep 2023 14:44:42 +0000 (16:44 +0200)] 
Remove .travis.yml

It isn't used anywhere so let's remove it.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
22 months agodoc: remove WISHLIST
Iker Pedrosa [Fri, 1 Sep 2023 14:18:31 +0000 (16:18 +0200)] 
doc: remove WISHLIST

Another file that I remove with sadness. We were unable to complete the
first item but we are working hard on it.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
22 months agodoc: remove README.platforms
Iker Pedrosa [Fri, 1 Sep 2023 14:11:06 +0000 (16:11 +0200)] 
doc: remove README.platforms

I remove this file with sadness, as it contains data from old times.
Unfortunately, this data is no longer relevant. The source code
management tool will keep it in memory.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
22 months agodoc: remove cracklib26.diff
Iker Pedrosa [Fri, 1 Sep 2023 14:04:44 +0000 (16:04 +0200)] 
doc: remove cracklib26.diff

Keeping a patch for a file no longer maintained is a bad idea, so I'm
removing it.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
22 months agodoc: remove console.c.spec.txt
Iker Pedrosa [Fri, 1 Sep 2023 14:01:16 +0000 (16:01 +0200)] 
doc: remove console.c.spec.txt

I guess we are keeping this for historical purposes more than anything
else. If so, anybody can check the git history to recover the
specification.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
22 months agocontrib: remove udbachk.tgz
Iker Pedrosa [Fri, 1 Sep 2023 13:58:50 +0000 (15:58 +0200)] 
contrib: remove udbachk.tgz

Having source code in a compressed file doesn't seem like a good idea. I
checked several distributions and they don't distribute this binary, so
let's remove it.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
22 months agocontrib: remove shadow-anonftp.patch
Iker Pedrosa [Fri, 1 Sep 2023 13:47:25 +0000 (15:47 +0200)] 
contrib: remove shadow-anonftp.patch

The patch is never applied upstream. If I were to take a gamble, I would
even say that it throws an error when trying to patch.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
22 months agocontrib: remove groupmems.shar
Iker Pedrosa [Fri, 1 Sep 2023 13:40:17 +0000 (15:40 +0200)] 
contrib: remove groupmems.shar

Not sure what this file is exactly, but there's already a groupmems.c
that should generate the binary responsible for managing  the members of
a user's primary group.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
22 months agocontrib: remove atudel
Iker Pedrosa [Thu, 31 Aug 2023 14:30:02 +0000 (16:30 +0200)] 
contrib: remove atudel

AFAIK, it isn't included in any distribution and it isn't used
internally in the project, so let's remove it.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
22 months agoCI: remove .builds folder
Iker Pedrosa [Thu, 31 Aug 2023 14:20:54 +0000 (16:20 +0200)] 
CI: remove .builds folder

We stopped using the CI relying on this folder and moved to Github's, so
I'm removing these files.

Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
22 months agouseradd: Set proper SELinux labels for def_usrtemplate
Johannes Segitz [Tue, 26 Sep 2023 13:14:14 +0000 (15:14 +0200)] 
useradd: Set proper SELinux labels for def_usrtemplate

Fixes: 74c17c716 ("Add support for skeleton files from /usr/etc/skel")
Signed-off-by: Johannes Segitz <jsegitz@suse.com>