Control: tags -1 + patch

Hi,

I reproduced this bug in VMs and checked where the fix is in each suite.
A tested patch for trixie is attached.

Where the fix is
----------------

  trixie  4.1.0-1    affected, but only with a kernel >= 6.17 (for example
                     from trixie-backports). Not with the stock 6.12 kernel.
  forky   4.1.8-3    has both upstream commits (checked the shipped
                     profile only, not tested).
  sid     5.0.2-5.1  has the attach_disconnected.path flag (MR 2004), but
                     not the nslcd rule (MR 2170, commit 2df456d8). That
                     commit was merged to apparmor-5.0 (d1d120e7) a week
                     after 5.0.2 was tagged.

Reproduction on trixie
----------------------

Debian 13 genericcloud image, apparmor 4.1.0-1, libpam-modules-bin 1.7.0-5.
slapd, nslcd 0.9.13-1, libnss-ldapd, libpam-ldapd and nscd on the same
host, one LDAP user (posixAccount + shadowAccount), "passwd, group, shadow:
files ldap" in nsswitch.conf, PAM stack as written by pam-auth-update
(setup-ldap.sh; for the login test I also set "PerSourcePenalties no" in
sshd_config, otherwise sshd starts to refuse the source after a few
denied logins). The test is 100 ssh password logins of the LDAP user
(logins.sh) and 300 runs of "unix_chkpwd ldapuser chkexpiry" as root
(loop.sh).

  kernel 6.12.116+deb13 (stock), 1 CPU:   60 of 60 logins succeed,
                                          0 of 300 helper runs fail
  kernel 7.2.12-1~bpo13+1, 1 CPU:         29 to 53 of 100 logins denied
                                          (four runs), 23 to 110 of 300
                                          helper runs fail (five runs)
  kernel 7.2.12-1~bpo13+1, 2 CPUs:        2 to 6 of 300 helper runs fail
                                          (three runs)

The share varies a lot between runs, because it is a race (see below).

The denied logins show the messages from the original report:

  unix_chkpwd: could not obtain user info (ldapuser)
  sshd-session: fatal: Access denied for user ldapuser by PAM account
    configuration [preauth]
  audit: apparmor="ALLOWED" operation="file_perm" class="file"
    info="Failed name lookup - disconnected path" error=-13
    profile="unix-chkpwd" name="run/nslcd/socket" comm="unix_chkpwd"
    requested_mask="r" denied_mask="r"

Why it fails, and only sometimes
--------------------------------

- nslcd.service sets ProtectSystem=strict and PrivateTmp=yes, so nslcd runs
  in its own mount namespace and its socket is a disconnected path for
  unix_chkpwd.
- Since 6.17 the kernel checks the path of the peer socket on every read()
  and write() of a unix socket (aa_unix_file_perm -> unix_fs_perm ->
  aa_path_perm). path_name() returns the failed lookup as an error also
  for a profile in complain mode. That is why the log says "ALLOWED" and
  the read still fails.
- As far as I can tell from the code, the check is skipped once nslcd has
  closed its end of the connection (the peer's path.dentry is NULL then).
  So the result depends on whether unix_chkpwd reads before or after nslcd
  closes, which explains "the same operation succeeds later".
- nscd answers the passwd lookups, but shadow lookups always go to nslcd.
- pam_unix 1.7.0 runs the helper for every user whose passwd field is "x".

One thing that may be worth forwarding to the kernel side: trixie pins
the 4.0 ABI, and for such policy __aa_path_perm() skips the rule check for
PATH_SOCK_COND. But profile_path_perm() calls path_name() before that, so
the failed lookup of a disconnected socket path still denies the access.
On trixie with the flag set and no rule for the nslcd socket, nothing is
denied or logged, also in enforce mode. Current mainline has the same
order. I have not reported this anywhere else.

Patch for trixie
----------------

The attached diff adds two patches to debian/patches (cherry-picks of
upstream 353c5bd9 and 2df456d8, both part of 4.1.8). After
put-all-profiles-in-complain-mode.sh the profile line becomes

  profile unix-chkpwd /{,usr/}{,s}bin/unix_chkpwd 
flags=(complain,attach_disconnected.path=/att/unix-chkpwd/) {

With that profile:

  kernel 7.2.12-1~bpo13+1, 1 CPU:   100 of 100 logins succeed,
                                    0 of 300 helper runs fail
  kernel 6.12.116+deb13, 1 CPU:     profile loads, 60 of 60 logins succeed,
                                    0 of 300 helper runs fail

On trixie the flag alone is enough because of the ABI; the nslcd rule is
there to match 4.1.8. I assembled the source package with dpkg-source -b
and tested the resulting profile, but I did not build the binary packages.

Suggested changelog entry:

  * unix-chkpwd: attach disconnected paths and allow the nslcd socket.
    Fixes LDAP logins with nslcd on kernels >= 6.17, for example from
    trixie-backports (Closes: #1144467)

Would you consider this for a trixie point release? I can file the
trixie-pu bug or change the patch if you prefer it another way.

sid
---

On sid (kernel 7.3.4+deb14, apparmor 5.0.2-5.1) as shipped, logins work,
but every lookup logs

  apparmor="ALLOWED" operation="connect" class="file" profile="unix-chkpwd"
    name="/att/unix-chkpwd/run/nslcd/socket" requested_mask="wr"

With the profile in enforce mode 300 of 300 helper runs fail and the login
is denied with the same sshd message as above. Adding
"/att/unix-chkpwd/run/nslcd/socket rw," fixes it (0 of 300 fail, login
succeeds). Upstream commit 2df456d8 applies to the 5.0.2-5.1 profile
unchanged.

Seen in the same enforce test, and not part of this bug: the helper cannot
log to the journal. The profile allows
/att/unix-chkpwd/run/systemd/journal/dev-log, but on this image (dracut
initrd, systemd 262) the name has no /run:

  apparmor="DENIED" operation="sendmsg" class="file" profile="unix-chkpwd"
    name="/att/unix-chkpwd/systemd/journal/dev-log" requested_mask="w"

I can file that separately if it is useful.

Workaround for trixie with a backports kernel
---------------------------------------------

In /etc/apparmor.d/unix-chkpwd change "flags=(complain)" to
"flags=(complain,attach_disconnected.path=/att/unix-chkpwd/)", then run
"apparmor_parser -r /etc/apparmor.d/unix-chkpwd".

Disclosure: I did the reproduction and the analysis with an AI coding
assistant (Claude) on my own machines. The scripts are attached and I can
send full logs.

Regards,
Karchi
diff -Nru a/debian/patches/series b/debian/patches/series
--- a/debian/patches/series	2025-04-10 17:06:25.000000000 +0200
+++ b/debian/patches/series	2026-11-16 09:20:47.913375372 +0100
@@ -5,6 +5,8 @@
 debian-only/pin-feature-set.patch
 debian-only/aa-notify-point-to-Debian-documentation.patch
 debian-only/Document-which-AppArmor-features-are-not-supported-on-Deb.patch
+upstream/unix-chkpwd-add-disconnected-run-paths.patch
+upstream/unix-chkpwd-allow-the-disconnected-nslcd-socket.patch
 # ubuntu/communitheme-snap-support.patch
 # ubuntu/mimeinfo-snap-support.patch
 # ubuntu/profiles-grant-access-to-systemd-resolved.patch
diff -Nru a/debian/patches/upstream/unix-chkpwd-add-disconnected-run-paths.patch b/debian/patches/upstream/unix-chkpwd-add-disconnected-run-paths.patch
--- a/debian/patches/upstream/unix-chkpwd-add-disconnected-run-paths.patch	1970-01-01 01:00:00.000000000 +0100
+++ b/debian/patches/upstream/unix-chkpwd-add-disconnected-run-paths.patch	2026-11-16 09:20:47.909375362 +0100
@@ -0,0 +1,38 @@
+From: Christian Boltz <[email protected]>
+Date: Mon, 23 Mar 2026 19:20:53 +0100
+Subject: unix-chkpwd: add disconnected /run/ paths
+
+Fixes: https://bugs.launchpad.net/bugs/2145628
+
+Backported to 4.1.0: only the profile flag and the rules are taken, the
+header comment and copyright year changes are left out.
+
+Origin: backport, https://gitlab.com/apparmor/apparmor/-/commit/353c5bd98097a0f8900a197d62fe05f463a03022
+Bug-Debian: https://bugs.debian.org/1144467
+---
+ profiles/apparmor.d/unix-chkpwd | 8 +++++++-
+ 1 file changed, 7 insertions(+), 1 deletion(-)
+
+--- a/profiles/apparmor.d/unix-chkpwd
++++ b/profiles/apparmor.d/unix-chkpwd
+@@ -11,7 +11,7 @@
+ 
+ include <tunables/global>
+ 
+-profile unix-chkpwd /{,usr/}{,s}bin/unix_chkpwd {
++profile unix-chkpwd /{,usr/}{,s}bin/unix_chkpwd flags=(attach_disconnected.path=/att/unix-chkpwd/) {
+   include <abstractions/base>
+   include <abstractions/nameservice>
+ 
+@@ -31,5 +31,11 @@
+   # file_inherit
+   owner /dev/tty[0-9]* rw,
+ 
++  # disconnected paths - their non-disconnected counterpart is allowed via abstractions
++  /att/unix-chkpwd/run/authd.sock rw,
++  /att/unix-chkpwd/run/systemd/journal/dev-log w,
++  /att/unix-chkpwd/run/systemd/userdb/io.systemd.DynamicUser rw,
++  /att/unix-chkpwd/run/systemd/userdb/org.gnome.DisplayManager rw,
++
+   include if exists <local/unix-chkpwd>
+ }
diff -Nru a/debian/patches/upstream/unix-chkpwd-allow-the-disconnected-nslcd-socket.patch b/debian/patches/upstream/unix-chkpwd-allow-the-disconnected-nslcd-socket.patch
--- a/debian/patches/upstream/unix-chkpwd-allow-the-disconnected-nslcd-socket.patch	1970-01-01 01:00:00.000000000 +0100
+++ b/debian/patches/upstream/unix-chkpwd-allow-the-disconnected-nslcd-socket.patch	2026-11-16 09:20:47.909375362 +0100
@@ -0,0 +1,33 @@
+From: =?utf-8?q?J=C3=A9r=C3=B4me_Poulin?= <[email protected]>
+Date: Thu, 16 Jul 2026 15:02:47 -0400
+Subject: unix-chkpwd: allow the disconnected nslcd socket
+
+Hosts using libnss-ldapd resolve users through nslcd, whose socket is
+granted by no abstraction this profile includes, and which unix_chkpwd
+reaches as a disconnected path. pam_unix's account phase therefore
+cannot resolve any LDAP user, and every LDAP login is denied after
+authentication has already succeeded; only local users get in.
+
+  apparmor="DENIED" operation="connect" class="file" profile="unix-chkpwd"
+  name="/att/unix-chkpwd/run/nslcd/socket" pid=70567 comm="unix_chkpwd"
+  requested_mask="wr" denied_mask="wr" fsuid=0 ouid=112
+
+Same class of denial as 6f7c80da5, which made nscd's nslcd socket rule
+relative to its attachment path.
+
+Origin: upstream, https://gitlab.com/apparmor/apparmor/-/commit/2df456d83c9e4476d7d00647e3e6e12720612338
+Bug-Debian: https://bugs.debian.org/1144467
+---
+ profiles/apparmor.d/unix-chkpwd | 1 +
+ 1 file changed, 1 insertion(+)
+
+--- a/profiles/apparmor.d/unix-chkpwd
++++ b/profiles/apparmor.d/unix-chkpwd
+@@ -33,6 +33,7 @@
+ 
+   # disconnected paths - their non-disconnected counterpart is allowed via abstractions
+   /att/unix-chkpwd/run/authd.sock rw,
++  /att/unix-chkpwd/run/nslcd/socket rw,
+   /att/unix-chkpwd/run/systemd/journal/dev-log w,
+   /att/unix-chkpwd/run/systemd/userdb/io.systemd.DynamicUser rw,
+   /att/unix-chkpwd/run/systemd/userdb/org.gnome.DisplayManager rw,
#!/bin/sh
# Throwaway LDAP server + nslcd client on one host, with one LDAP-only user "ldapuser".
set -e
export DEBIAN_FRONTEND=noninteractive
debconf-set-selections <<D
slapd slapd/domain string example.org
slapd shared/organization string Example
slapd slapd/password1 password adminpw
slapd slapd/password2 password adminpw
nslcd nslcd/ldap-uris string ldap://127.0.0.1/
nslcd nslcd/ldap-base string dc=example,dc=org
libnss-ldapd libnss-ldapd/nsswitch multiselect passwd, group, shadow
D
apt-get -q update >/dev/null
apt-get -q -y install slapd ldap-utils nslcd libnss-ldapd libpam-ldapd sshpass >/dev/null
H=$(slappasswd -s userpw)
ldapadd -x -D cn=admin,dc=example,dc=org -w adminpw <<L
dn: ou=people,dc=example,dc=org
objectClass: organizationalUnit
ou: people

dn: uid=ldapuser,ou=people,dc=example,dc=org
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
cn: LDAP User
sn: User
uid: ldapuser
uidNumber: 20001
gidNumber: 100
homeDirectory: /home/ldapuser
loginShell: /bin/bash
userPassword: $H
L
pam-auth-update --enable mkhomedir
mkdir -p /etc/ssh/sshd_config.d
printf 'PasswordAuthentication yes\nKbdInteractiveAuthentication no\n' > /etc/ssh/sshd_config.d/00-test.conf
systemctl restart ssh nslcd
getent passwd ldapuser
#!/bin/sh
# Run the pam_unix helper N times as root; report how many runs failed and what AppArmor logged.
n=${1:-300}; fail=0; dmesg -C
i=0; while [ $i -lt $n ]; do /usr/sbin/unix_chkpwd ldapuser chkexpiry </dev/null >/dev/null 2>&1 || fail=$((fail+1)); i=$((i+1)); done
echo "runs=$n failed=$fail"
dmesg | grep -o 'apparmor="[A-Z]*" operation="[a-z_]*".*profile="unix-chkpwd" name="[^"]*"' | sed 's/ error=-13//' | sort | uniq -c
#!/bin/sh
# N password logins of the LDAP user over ssh; counts how many got a shell and what sshd/unix_chkpwd logged.
n=${1:-60}; ok=0; since=$(date '+%F %T'); sudo dmesg -C
i=0; while [ $i -lt $n ]; do
  sshpass -p userpw ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o PreferredAuthentications=password -o PubkeyAuthentication=no [email protected] true 2>/dev/null && ok=$((ok+1)); i=$((i+1)); done
echo "logins=$n succeeded=$ok denied=$((n-ok))"
sudo journalctl --since "$since" -q --no-pager -o cat -t unix_chkpwd -t sshd-session | grep -E 'could not obtain|Access denied' | sed -E 's/port [0-9]+//' | sort | uniq -c
sudo dmesg | grep -o 'apparmor="[A-Z]*" operation="[a-z_]*".*profile="unix-chkpwd" name="[^"]*"' | sed 's/ error=-13//' | sort | uniq -c

Reply via email to