-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

=============================================================================
FreeBSD-EN-24:02.libutil                                        Errata Notice
                                                          The FreeBSD Project

Topic:          Login class resource limits and CPU mask bypass

Category:       core
Module:         libutil
Announced:      2024-02-14
Credits:        Olivier Certner
Affects:        All supported versions of FreeBSD.
Corrected:      2023-10-24 00:57:11 UTC (stable/14, 14.0-STABLE)
                2023-02-14 06:05:41 UTC (releng/14.0, 14.0-RELEASE-p5)
                2023-12-21 13:39:03 UTC (stable/13, 13.2-STABLE)
                2023-02-14 06:05:57 UTC (releng/13.2, 13.2-RELEASE-p10)

For general information regarding FreeBSD Errata Notices and Security
Advisories, including descriptions of the fields above, security
branches, and the following sections, please visit
<URL:https://security.FreeBSD.org/>.

I.   Background

setusercontext() is a high-level API generally used by login-like programs to
set the general environment of new processes launched on behalf of other
users, including the credentials (users, groups, MAC security label), resource
limits, CPU mask and process priority.

This function only applies the settings of the types requested by the caller
via flags (e.g., LOGIN_SETALL for all types, LOGIN_SETUSER to set the real,
effective and saved user IDs, etc.), and for some of them requires privileges
to do so.  Among these, the resource limits (flag LOGIN_SETRESOURCES) and CPU
mask (flag LOGIN_SETCPUMASK) types are set not only based on the target user's
login class, which is controlled by the system administrator, but also on his
personal configuration file '~/.login_conf' (see login.conf(5)).

In order to prevent unprivileged users from overriding the administrator
settings, setusercontext() applies a personal configuration file only if the
real user ID of the process that runs it matches that of the target user, with
the goal to avoid applying the user-controlled settings with privileges.

II.  Problem Description

When deciding to apply a target user's personal configuration file,
setusetcontext() checks the real user ID of the process whereas it should
instead check the effective user ID, which is the one affecting the process'
privileges and consequently which settings it can change and to which values.

III. Impact

An unprivileged user may bypass the administrator's resource limits and/or CPU
mask settings stemming from his login class provided he can run a (setuid)
login-like program that:
- - Calls setusercontext() with the LOGIN_SETRESOURCES and/or LOGIN_SETCPUMASK
  flags but without LOGIN_SETUSER (which excludes the use of LOGIN_SETALL),
  and with a non-NULL 'pwd' argument.
- - Does so before changing the effective user ID to the target user.

No programs in FreeBSD's base system, including login(1) and su(1), meet these
requirements, but third-party programs may.  In particular, sudo(8) does when
using the default sudoers(5) plugin configured with the 'use_loginclass' flag
enabled.  doas(8) does not.

IV.  Workaround

There are at least two possible workarounds.

The first one is for an administrator is to prepare for all users a
'~/.login_conf' they can't write or replace, e.g., using filesystem flags
'schg' or 'sunlnk' (see chflags(1)), defeating user's own customizations.

The second one is to review setuid login programs accessible to users,
determine if they meet the requirements above, and deactivate those that do or
reconfigure them when possible, as mentioned above for sudo(8).

V.   Solution

Upgrade your system to a supported FreeBSD stable or release / security
branch (releng) dated after the correction date.

Perform one of the following:

1) To update your system via a binary patch:

Systems running a RELEASE version of FreeBSD on the amd64 or arm64 platforms,
or the i386 platform on FreeBSD 13, can be updated via the freebsd-update(8)
utility:

# freebsd-update fetch
# freebsd-update install

It should be followed by a restart of all third-party daemons that use the
'libutil' library, or a reboot of the system.

2) To update your system via a source code patch:

The following patches have been verified to apply to the applicable
FreeBSD release branches.

a) Download the relevant patch from the location below, and verify the
detached PGP signature using your PGP utility.

# fetch https://security.FreeBSD.org/patches/EN-24:02/libutil.patch
# fetch https://security.FreeBSD.org/patches/EN-24:02/libutil.patch.asc
# gpg --verify libutil.patch.asc

b) Apply the patch.  Execute the following commands as root:

# cd /usr/src
# patch < /path/to/patch

c) Recompile the operating system using buildworld and installworld as
described in <URL:https://www.FreeBSD.org/handbook/makeworld.html>.

Restart of all third-party daemons that use the 'libutil' library, or reboot the
system.

VI.  Correction details

This issue is corrected as of the corresponding Git commit hash in the
following stable and release branches:

Branch/path                             Hash                     Revision
- -------------------------------------------------------------------------
stable/14/                              ede6fd06726c    stable/14-n265587
releng/14.0/                            c2a9cfc55046  releng/14.0-n265403
stable/13/                              9fcf54d3750e    stable/13-n256941
releng/13.2/                            9deb5ca77beb  releng/13.2-n254655
- -------------------------------------------------------------------------

Run the following command to see which files were modified by a
particular commit:

# git show --stat <commit hash>

Or visit the following URL, replacing NNNNNN with the hash:

<URL:https://cgit.freebsd.org/src/commit/?id=NNNNNN>

To determine the commit count in a working tree (for comparison against
nNNNNNN in the table above), run:

# git rev-list --count --first-parent HEAD

VII. References

<URL:https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=271750>

The latest revision of this advisory is available at
<URL:https://security.FreeBSD.org/advisories/FreeBSD-EN-24:02.libutil.asc>
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCgAdFiEEthUnfoEIffdcgYM7bljekB8AGu8FAmXMYQoACgkQbljekB8A
Gu8m9Q//cmgbS/PZPMBjARTQa2kkEpIy7zYgDq9/oriREfUBgbN+hFdxlwN5q59r
t+lJGJYSynMQDFglQcsD61nECP6fnjco1RxLPpzf+aBmP/VebOh7irsI7QElisY+
SoiCHhZrpXcZGU5OBTA0Nd7NbKVmCflF6aJN0bOCZHvONSUH+ijsXPd98Pjx6TgF
0yQV3ryMYtEBbIaXdR751HLe011hcQYBnlU+/0B9bzL5JCr67NaYM3MDkMkwvXSs
zJaefj9xxMlJdB4EvkJGtcau4Kw/qdM0iFllUMmOPl3QK+s4LKguaVtuWWI0bSvL
VlFbGVCoRmaVzV+ZaCrDZrsl3NOC92Trhg5QdLV5HJUP3sSRAo5PGNostdWB6VsT
mfgJp0owv7LSSt/irDgtY2OGFb3Y/RZmqTBXR7ScFAguuA5dJva44eDkUX8YXBU/
7ZlXMuF94dmaTmcDqOqWBmfeIWlIKdVsol6fzoKQhLjtZuUg5vdl2rUlj6GSNSL9
6GLU2/LiobuBhfc0qL/mmtyovqHO2HDLsNX54zusBEzy7lI2URvTcCjcHX0Tbwwi
cuj6b/XzvAnQ2qFyA4l8bhCSpECkGybLgar+ig199K077HrwRUjLt666JQtMBkKQ
LZafucjfGCSpDJFcVjfGfliYnYQFyAd4NAfDsnR15xz9Pxw7MOg=
=mDl9
-----END PGP SIGNATURE-----