Bug 177698 - [libutil] sshd sets the user's MAC label at the same time it attempts to set the login class, which can cause the latter to fail if mac_biba is used.
Summary: [libutil] sshd sets the user's MAC label at the same time it attempts to set ...
Status: Open
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 12.2-RELEASE
Hardware: Any Any
: Normal Affects Only Me
Assignee: Kyle Evans
URL:
Keywords:
Depends on:
Blocks: 192900
  Show dependency treegraph
 
Reported: 2013-04-07 22:50 UTC by Kevin Barry
Modified: 2025-11-13 00:33 UTC (History)
5 users (show)

See Also:
linimon: mfc-stable15?
linimon: mfc-stable14?


Attachments
file.diff (947 bytes, patch)
2013-04-07 22:50 UTC, Kevin Barry
no flags Details | Diff
login_class.c.txt (2.02 KB, patch)
2013-04-08 04:50 UTC, Kevin Barry
no flags Details | Diff
login_class.c.txt (2.22 KB, patch)
2013-04-12 20:20 UTC, Kevin Barry
no flags Details | Diff
libutil patch updated for 12.2-RELEASE (2.40 KB, patch)
2020-12-28 23:59 UTC, Kevin Barry
no flags Details | Diff
libutil patch updated for 12.2-RELEASE (2.39 KB, patch)
2025-02-25 02:38 UTC, Mark Linimon
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Kevin Barry 2013-04-07 22:50:01 UTC
When using the mac_biba module, only root processes that are capable of
attaining biba/equal status are considered privileged. Among the privileges
to be lost for processes that cannot attain biba/equal is the ability to
set the login class of the process.

When sshd sets the user's context upon successful authentication
(session.c:1494), both the user's login class and the corresponding MAC
label are set in the same call to setusercontext. Since this isn't an
atomic function, it's possible for the MAC label to be set before the
login class (in my case, this certainly happens.) Therefore, if the
login class of the user logging in has a MAC label in /etc/login.conf
incompatible with biba/equal (e.g. biba/high(high-high)) then the login
class might not be set (in my case it *doesn't* get set.) This can cause
unexpected behavior for tools that can be configured by login class,
e.g. rctl.

My proposed solution is to separate context-setting into two calls to
setusercontext, with the first call setting all but LOGIN_SETMAC, and
the second call setting only LOGIN_SETMAC. I've included a patch, which
works on my system.

Fix: Patch /usr/src/crypto/openssh/session.c so that the MAC label is explicitly
set after the login class is set.

Patch attached with submission follows:
How-To-Repeat: - Enable mac_biba.
- Set the MAC label of a login class to ":label=biba/high(high-high):"
  in /etc/login.conf and run cap_mkdb. By default, sshd will probably
  run under the "default" login class; therefore, use a different login
  class so that a change in login class is required upon login.
- Log in remotely as a user who is a member of the login class modified
  in the step above. The login should succeed.
- Look at /var/log/messages for a message regarding setloginclass, and/or
  use `ps -o class -aux | grep sshd` to verify that the login class hasn't
  been set properly for the logged-in user.
Comment 1 Kevin Barry 2014-08-21 23:08:26 UTC
(The following comment did not carry over when the bug report was migrated to the new system.)

From: 	Kevin Barry <ta0kira@gmail.com> [submitter]
To: 	bug-followup@FreeBSD.org, ta0kira@gmail.com
Date: 	Sun, 7 Apr 2013 23:50:35 -0400
I submitted this bug report earlier, and since then I've noticed that
/usr/bin/login suffers from the same problem. I've therefore made a change
to libutil to make setusercontext set the MAC label right before the uid
change. I've attached a separate patch that should universally fix the
problem. This also makes my previous sshd patch obsolete. Incidentally,
this should be reclassified as a bug in libutil.
Comment 2 Kevin Barry 2014-08-21 23:08:43 UTC
(The following comment did not carry over when the bug report was migrated to the new system.)

From: 	Kevin Barry <ta0kira@gmail.com> [submitter]
To: 	bug-followup@FreeBSD.org, ta0kira@gmail.com
Date: 	Fri, 12 Apr 2013 15:20:10 -0400
Here's a new patch for login_class.c. As far as I can tell there is no
reason to require that a passwd entry be specified in order to set the MAC
label; therefore, I removed that requirement. Additionally, the current
implementation silently fails to set the MAC label when the pwd argument is
NULL, and silent failure when it comes to security isn't a good thing.
While not directly related to the original problem, it's related to the
underlying issue, which is that the handling of MAC labels in
setusercontext has several bugs in need of fixing.
Comment 3 Dag-Erling Smørgrav freebsd_committer freebsd_triage 2015-12-17 19:58:47 UTC
I will try to look into this.
Comment 4 Kevin Barry 2020-12-28 23:59:47 UTC
Created attachment 221063 [details]
libutil patch updated for 12.2-RELEASE
Comment 5 Graham Perrin freebsd_committer freebsd_triage 2022-10-17 12:39:57 UTC
Keyword: 

    patch
or  patch-ready

– in lieu of summary line prefix: 

    [patch]

* bulk change for the keyword
* summary lines may be edited manually (not in bulk). 

Keyword descriptions and search interface: 

    <https://bugs.freebsd.org/bugzilla/describekeywords.cgi>
Comment 6 Mark Linimon freebsd_committer freebsd_triage 2025-02-25 02:38:18 UTC
Created attachment 257858 [details]
libutil patch updated for 12.2-RELEASE

^Triage: rebase patch.
Comment 7 commit-hook freebsd_committer freebsd_triage 2025-10-29 02:38:21 UTC
A commit in branch main references this bug:

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

commit 98edcbcce0a4650084bd86e704cfa38bf590250c
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2025-10-29 02:37:21 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2025-10-29 02:37:21 +0000

    libutil: defer setting the MAC label until after the login class

    MAC policies, like mac_biba(4), may forbid changing the login class once
    a label has been applied.  For setting up the initial login context,
    this isn't really expected and in-fact may break some class-based
    configuration.

    Defer setting the MAC label until after the login class is set, and
    remove the requirement that we have a pwd entry since the label is
    pulled from the login class -- we only use pwd for syslog in this path.

    Patch is largely by Kevin Barry, with some modifications and this commit
    message by kevans@.

    PR:             177698
    Reviewed by:    des, olce
    MFC after:      3 days
    Co-authored-by: Kevin Barry <ta0kira gmail com>
    Differential Revision:  https://reviews.freebsd.org/D53362

 lib/libutil/login_class.c | 55 ++++++++++++++++++++++++++---------------------
 1 file changed, 30 insertions(+), 25 deletions(-)
Comment 8 Mark Linimon freebsd_committer freebsd_triage 2025-11-13 00:33:45 UTC
^Triage: assign to committer and set MFC flags (if desired).