Commit graph

68 commits

Author SHA1 Message Date
Lennart Poettering efb9b3bab2 env-util: rename getenv_bool_secure() → secure_getenv_bool()
The glibc API is behind the wrapper is called "secure_getenv()", hence
our wrapper really should keep the order too, otherwise things are just
too confusing.
2024-02-28 15:38:00 +01:00
Jan Janssen 90461ef56f meson: Fix version script handling
Build targets should have a link dependency on the version scripts they
use. This also uses absolute paths in anticipation for meson 1.3
needlessly deprecating file to string conversions.
2023-09-26 19:41:53 +02:00
Yu Watanabe 2080c0978d meson: move declarations of nss modules 2023-07-31 22:17:34 +09:00
Cristian Rodríguez 5545f336fd Include <threads.h> if possible to get thread_local definition
IN C23, thread_local is a reserved keyword and we shall therefore
do nothing to redefine it. glibc has it defined for older standard
version with the right conditions.

v2 by Yu Watanabe:
Move the definition to missing_threads.h like the way we define e.g.
missing syscalls or missing definitions, and include it by the users.

Co-authored-by: Yu Watanabe <watanabe.yu+github@gmail.com>
2023-03-06 10:04:39 +09:00
Zbigniew Jędrzejewski-Szmek 8a7adccbdb various: try to use DEFAULT_USER_SHELL for root too
/bin/sh as a shell is punishing. There is no good reason to make
the occasional root login unpleasant.

Since /bin/sh is usually /bin/bash in compat mode, i.e. if one is
available, the other will be too, /bin/bash is almost as good as a default.
But to avoid a regression in the situation where /bin/bash (or
DEFAULT_USER_SHELL) is not installed, we check with access() and fall back
to /bin/sh. This should make this change in behaviour less risky.

(FWIW, e.g. Fedora/RHEL use /bin/bash as default for root.)

This is a follow-up of sorts for 53350c7bba,
which added the default-user-shell option, but most likely with the idea
of using /bin/bash less ;)

Fixes #24369.
2022-08-24 10:02:46 +02:00
Zbigniew Jędrzejewski-Szmek 94b84a0703 Use descriptive name for nobody
This matches the changes pushed to Fedora [1,2].

[1] https://fedoraproject.org/wiki/Changes/RenameNobodyUser
[2] https://pagure.io/setup/c/f6fdb5ffc87fc8f1acc211867fef4e3f0856edfc
2022-05-27 22:09:24 +01:00
Yu Watanabe de010b0b2e strv: make iterator in STRV_FOREACH() declaread in the loop
This also avoids multiple evaluations in STRV_FOREACH_BACKWARDS()
2022-03-19 08:33:33 +09:00
Zbigniew Jędrzejewski-Szmek a7d15a2465 nss: only read logging config from environment variables
log_parse_environment() uses should_parse_proc_cmdline() to determine whether
it should parse settings from the kernel command line. But the checks that
should_parse_proc_cmdline() apply to the whole process, and we could get a positive
answer also when log_parse_environment() was called from one of the nss modules.
In case of nss-modules, we don't want to look at the kernel command line.

log_parse_environment_variables() that only looks at the environment variables
is split out and used in the nss modules.

Fixes #22020.
2022-01-11 13:39:52 +01:00
Zbigniew Jędrzejewski-Szmek 56a5f4969b nss: drop dummy setup_logging() helpers
log_parse_environment() stopped being a macro in 9fdee66f2d.
As reported by @bauen1 in https://github.com/systemd/systemd/issues/22020,
the comment was out of date.
2022-01-11 13:39:52 +01:00
Yu Watanabe 420a35c1fa nss-systemd: fix alignment of gr_mem
Follow-up for 1e65eb8f9b.

Fixes #21935.
2021-12-31 09:05:16 +09:00
Yu Watanabe 1e65eb8f9b nss-systemd: fix required buffer size calculation
This also fixes the pointer assigned to the gr_mem element of struct group.

Fixes a bug introduced by 47fd7fa6c6.

Fixes #21935.
2021-12-31 03:12:09 +09:00
Frantisek Sumsal d7ac09520b tree-wide: mark set-but-not-used variables as unused to make LLVM happy
LLVM 13 introduced `-Wunused-but-set-variable` diagnostic flag, which
trips over some intentionally set-but-not-used variables or variables
attached to cleanup handlers with side effects (`_cleanup_umask_`,
`_cleanup_(notify_on_cleanup)`, `_cleanup_(restore_sigsetp)`, etc.):

```
../src/basic/process-util.c:1257:46: error: variable 'saved_ssp' set but not used [-Werror,-Wunused-but-set-variable]
        _cleanup_(restore_sigsetp) sigset_t *saved_ssp = NULL;
                                                     ^
                                                     1 error generated.
```
2021-09-15 13:09:45 +02:00
Michael Catanzaro 47fd7fa6c6 nss-systemd: ensure returned strings point into provided buffer
Jamie Bainbridge found an issue where glib's g_get_user_database_entry()
may crash after doing:

```
error = getpwnam_r (logname, &pwd, buffer, bufsize, &pw);
// ...
pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);
```

in order to uppercase the first letter of the user's real name. This is
a glib bug, because there is a different codepath that gets the pwd from
vanilla getpwnam instead of getpwnam_r as shown here. When the pwd
struct is returned by getpwnam, its fields point to static data owned by
glibc/NSS, and so it must not be modified by the caller. After much
debugging, Jamie Bainbridge has fixed this in https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2244
by making a copy of the data before modifying it, and that resolves all
problems for glib. Yay!

However, glib is crashing even when getpwnam_r is used instead of
getpwnam! According to getpwnam_r(3), the strings in the pwd struct are
supposed to be pointers into the buffer passed by the caller, so glib
should be able to safely edit it directly in this case, so long as it
doesn't try to increase the size of any of the strings.

Problem is various functions throughout nss-systemd.c return synthesized
records declared at the top of the file. These records are returned
directly and so contain pointers to static strings owned by
libsystemd-nss. systemd must instead copy all the strings into the
provided buffer.

This crash is reproducible if nss-systemd is listed first on the passwd
line in /etc/nsswitch.conf, and the application looks up one of the
synthesized user accounts "root" or "nobody", and finally the
application attempts to edit one of the strings in the returned struct.
All our synthesized records for the other struct types have the same
problem, so this commit fixes them all at once.

Fixes #20679
2021-09-09 15:07:57 -05:00
Michael Catanzaro 92b264676c nss-systemd: pack pw_passwd result into supplied buffer
getpwnam_r() guarantees that the strings in the struct passwd that it
returns are pointers into the buffer allocated by the application and
passed to getpwnam_r(). This means applications may choose to modify the
strings in place, as long as the length of the strings is not increased.
So it's wrong for us to return a static string here, we really do have
to copy it into the application-provided buffer like we do for all the
other strings.

This is only a theoretical problem since it would be very weird for an
application to modify the pw_passwd field, but I spotted this when
investigating a similar crash caused by glib editing a different field.
See also:

https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2244
2021-09-08 16:19:28 -05:00
Yu Watanabe 4301cb32f2 fix typo 2021-05-14 19:51:37 +02:00
Lennart Poettering f43a19ecd6 nss-systemd: synthesize NSS shadow/gshadow records from userdb, as well
This ensures we not only synthesize regular paswd/group records of
userdb records, but shadow records as well. This should make sure that
userdb can be used as comprehensive superset of the classic
passwd/group/shadow/gshadow functionality.
2021-05-08 14:35:28 +02:00
Lennart Poettering 09001dbdc8 nss-systemd: set USERDB_SUPPRESS_SHADOW flag when looking up user records
Setting the flags means we won#t try to read the data from /etc/shadow
when reading a user record, thus slightly making conversion quicker and
reducing the chance of generating MAC faults, because we needlessly
access a privileged resource. Previously, passing the flag didn't
matter, when converting our JSON records to NSS since the flag only had
an effect on whether to use NSS getspnam() and related calls or not. But
given that we turn off NSS anyway as backend for this conversion (since
we want to avoid NSS loops, where we turn NSS data to our JSON user
records, and then to NSS forever and ever) it was unnecessary to pass
it.

This changed in one of the previous commits however, where we added
support for reading user definitions from drop-in files, with separate
drop-in files for the shadow data.
2021-05-08 14:25:24 +02:00
Lennart Poettering 80d88a8267 userdb: rename userdb lookup flags a bit
Let's use "exclude" for flags that really exclude records from our
lookup. Let's use "avoid" referring to concepts that when flag is set
we'll not use but we have a fallback path for that should yield the same
result. Let' use "suppress" for suppressing partial info, even if we
return the record otherwise.

So far we used "avoid" for all these cases, which was confusing.

Whiel we are at it, let's reassign the bits a bit, leaving some space
for bits follow-up commits are going to add.
2021-05-07 22:19:07 +02:00
Lennart Poettering 2d882d3581 nss-systemd: make llvm work-around for used _cleanup_ explicit 2021-05-07 16:44:03 +02:00
Lennart Poettering a1aa41e4e1 nss-systemd: properly handle empty membership lists
When we are queried for membership lists on a system that has exactly
zero, then we'll return ESRCH immediately instead of at EOF. Which is
OK, but we need to handle this in various places, and not get confused
by it.
2021-05-07 09:15:40 +02:00
Lennart Poettering 1fdfca4da7 nss-systemd: reset the right field 2021-05-06 22:35:49 +02:00
Lennart Poettering 53c25ac968 user-util: add generic definition for special password hash values in /etc/passwd + /etc/shadow
Let's add three defines for the 3 special cases of passwords.

Some of our tools used different values for the "locked"/"invalid" case,
let's settle on using "!*" which means the password is both locked *and*
invalid.

Other tools like to use "!!" for this case, which however is less than
ideal I think, since the this could also be a considered an entry with
an empty password, that can be enabled again by unlocking it twice.
2021-05-06 21:55:58 +02:00
Zbigniew Jędrzejewski-Szmek 3339381f22 nss-systemd: initialize logging 2020-12-10 20:51:59 +01:00
Yu Watanabe db9ecf0501 license: LGPL-2.1+ -> LGPL-2.1-or-later 2020-11-09 13:23:58 +09:00
Frantisek Sumsal d7a0f1f4f9 tree-wide: assorted coccinelle fixes 2020-10-09 15:02:23 +02:00
Zbigniew Jędrzejewski-Szmek e60775cb7b shared: merge {user,group}-record-nss.{c,h}
They both are both short and contain similar parts and various helper will be
shared between both parts of the code so it's easier to use a single file.
2020-09-01 16:48:40 +02:00
Lennart Poettering 037b0a47b0 userdb: replace recursion lock
Previously we'd used the existance of a specific AF_UNIX socket in the
abstract namespace as lock for disabling lookup recursions. (for
breaking out of the loop: userdb synthesized from nss → nss synthesized
from userdb → userdb synthesized from nss → …)

I did it like that because it promised to work the same both in static
and in dynmically linked environments and is accessible easily from any
programming language.

However, it has a weakness regarding reuse attacks: the socket is
securely hashed (siphash) from the thread ID in combination with the
AT_RANDOM secret. Thus it should not be guessable from an attacker in
advance. That's only true if a thread takes the lock only once and
keeps it forever. However, if a thread takes and releases it multiple
times an attacker might monitor that and quickly take the lock
after the first iteration for follow-up iterations.

It's not a big issue given that userdb (as the primary user for this)
never released the lock and we never made the concept a public
interface, and it was only included in one release so far, but it's
something that deserves fixing. (moreover it's a local DoS only, only
permitting to disable native userdb lookups)

With this rework the libnss_systemd.so.2 module will now export two
additional symbols. These symbols are not used by glibc, but can be used
by arbitrary programs: one can be used to disable nss-systemd, the other
to check if it is currently disabled.

The lock is per-thread. It's slightly less pretty, since it requires
people to manually link against C code via dlopen()/dlsym(), but it
should work safely without the aforementioned weakness.
2020-06-23 17:24:24 +02:00
Lennart Poettering 6e78726e20 nss-systemd: skip /etc/gshadow look-ups when we just need the GID of a group 2020-06-23 17:24:24 +02:00
Lennart Poettering 9494da41c2 nss-systemd: don't synthesize root/nobody when iterating
Fixes: #15160
2020-04-23 23:07:08 +02:00
Zbigniew Jędrzejewski-Szmek 162392b75a tree-wide: spellcheck using codespell
Fixes #15436.
2020-04-16 18:00:40 +02:00
Zbigniew Jędrzejewski-Szmek 2d9123cebd
Merge pull request #15377 from poettering/userdb-no-shadow
don't try to access shadow from logind
2020-04-11 16:08:33 +02:00
Lennart Poettering ed30170ea0 userdb: when doing client-side NSS look-ups optionally avoid shadow look-ups 2020-04-09 14:38:02 +02:00
Lennart Poettering 7a8867abfa user-util: rework how we validate user names
This reworks the user validation infrastructure. There are now two
modes. In regular mode we are strict and test against a strict set of
valid chars. And in "relaxed" mode we just filter out some really
obvious, dangerous stuff. i.e. strict is whitelisting what is OK, but
"relaxed" is blacklisting what is really not OK.

The idea is that we use strict mode whenver we allocate a new user
(i.e. in sysusers.d or homed), while "relaxed" mode is when we process
users registered elsewhere, (i.e. userdb, logind, …)

The requirements on user name validity vary wildly. SSSD thinks its fine
to embedd "@" for example, while the suggested NAME_REGEX field on
Debian does not even allow uppercase chars…

This effectively liberaralizes a lot what we expect from usernames.

The code that warns about questionnable user names is now optional and
only used at places such as unit file parsing, so that it doesn't show
up on every userdb query, but only when processing configuration files
that know better.

Fixes: #15149 #15090
2020-04-08 17:11:20 +02:00
Zbigniew Jędrzejewski-Szmek 37bc9dcc09 nss-systemd: use _cleanup_ for pthread_mutex_{lock,unlock}
v2: separate the declaration from the assignment to appease clang.
2020-03-28 17:03:21 +01:00
Zbigniew Jędrzejewski-Szmek e7e9a9d0dc nss-systemd: add missing jump to unlock mutex
CID#1412415.
2020-03-28 13:03:06 +01:00
Zbigniew Jędrzejewski-Szmek 6f22d57235 userdb: fix lookup of groups defined by homed 2020-03-01 12:24:56 +01:00
Zbigniew Jędrzejewski-Szmek 3e93027b5b Fix two typos 2020-03-01 12:24:25 +01:00
Lennart Poettering 1684c56f40 nss: hook up nss-systemd with userdb varlink bits
This changes nss-systemd to use the new varlink user/group APIs for
looking up everything.

(This also changes the factory /etc/nsswitch.conf line to use for
hooking up nss-system to use glibc's [SUCCESS=merge] feature so that we
can properly merge group membership lists).

Fixes: #12492
2020-01-15 15:29:07 +01:00
Michael Biebl 6db904625d meson: make nologin path build time configurable
Some distros install nologin as /usr/sbin/nologin, others as
/sbin/nologin.
Since we can't really on merged-usr everywhere (where the path wouldn't
matter), make the path build time configurable via -Dnologin-path=.

Closes #13028
2019-07-18 12:46:35 +02:00
Zbigniew Jędrzejewski-Szmek ca78ad1de9 headers: remove unneeded includes from util.h
This means we need to include many more headers in various files that simply
included util.h before, but it seems cleaner to do it this way.
2019-03-27 11:53:12 +01:00
Lennart Poettering cdccd29f39 nss: unportect errno before writing to NSS' *errnop
Fixes: #11321
2019-02-08 10:34:47 +01:00
Zbigniew Jędrzejewski-Szmek 0192cbdb2c Revert "nss: prevent PROTECT_ERRNO from squashing changes to *errnop"
This reverts commit b26c904113.

I don't see anythign wrong, but Ubuntu autopkgtest CI started failing fairly
consistently since this was merged. Let's see if reverting fixes things.
2019-01-10 21:23:14 +01:00
Sam Morris b26c904113 nss: prevent PROTECT_ERRNO from squashing changes to *errnop
glibc passes in &errno for errnop, which means PROTECT_ERRNO ends up
squashing our intentional changes to *errnop.

Fixes #11321.
2019-01-10 11:08:42 +01:00
Yu Watanabe 06202b9e65 nss: do not modify errno when NSS_STATUS_NOTFOUND or NSS_STATUS_SUCCESS
This also adds PROTECT_ERRNO for all nss module functions.

C.f. glibc NSS documents https://www.gnu.org/software/libc/manual/html_node/NSS-Modules-Interface.html
and discussion in https://sourceware.org/bugzilla/show_bug.cgi?id=23410.

Fixes #9585.
2018-07-25 10:23:22 +02:00
Lennart Poettering 0c69794138 tree-wide: remove Lennart's copyright lines
These lines are generally out-of-date, incomplete and unnecessary. With
SPDX and git repository much more accurate and fine grained information
about licensing and authorship is available, hence let's drop the
per-file copyright notice. Of course, removing copyright lines of others
is problematic, hence this commit only removes my own lines and leaves
all others untouched. It might be nicer if sooner or later those could
go away too, making git the only and accurate source of authorship
information.
2018-06-14 10:20:20 +02:00
Lennart Poettering 818bf54632 tree-wide: drop 'This file is part of systemd' blurb
This part of the copyright blurb stems from the GPL use recommendations:

https://www.gnu.org/licenses/gpl-howto.en.html

The concept appears to originate in times where version control was per
file, instead of per tree, and was a way to glue the files together.
Ultimately, we nowadays don't live in that world anymore, and this
information is entirely useless anyway, as people are very welcome to
copy these files into any projects they like, and they shouldn't have to
change bits that are part of our copyright header for that.

hence, let's just get rid of this old cruft, and shorten our codebase a
bit.
2018-06-14 10:20:20 +02:00
Zbigniew Jędrzejewski-Szmek 11a1589223 tree-wide: drop license boilerplate
Files which are installed as-is (any .service and other unit files, .conf
files, .policy files, etc), are left as is. My assumption is that SPDX
identifiers are not yet that well known, so it's better to retain the
extended header to avoid any doubt.

I also kept any copyright lines. We can probably remove them, but it'd nice to
obtain explicit acks from all involved authors before doing that.
2018-04-06 18:58:55 +02:00
Yu Watanabe 12c2c56dcb nss-systemd: make dynamic users enumerable by getent
This adds `setpwent()`, `getpwent_r()`, `endpwent()`, `setgrent()`,
`getgrent_r()`, and `endgrent()` interfaces to nss-systemd library.
Thus, dynamic users can be enumerated by e.g. `getent passwd` command.
2018-03-21 13:39:03 +09:00
Yu Watanabe 9b5eaef3d1 nss-systemd: define dynamic user properties 2018-03-21 13:11:30 +09:00
Yu Watanabe 2458541961 nss-systemd: cleanup bypassing dbus logic 2018-03-21 13:11:17 +09:00