Skip to contentSkip to conversation

af_unix: Allow passing cred for embryo without SO_PASSCRED/SO_PASSPIDFD.

PATCH Applied Kuniyuki Iwashima 6 participants 6 messages Jun 11, 2025 – Jun 16, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 af_unix: Allow passing cred for embryo without SO_PASSCRED/SO_PASSPIDFD.

From: Kuniyuki Iwashima <[email protected]>

Before the cited commit, the kernel unconditionally embedded SCM credentials to skb for embryo sockets even when both the sender and listener disabled SO_PASSCRED and SO_PASSPIDFD.

Now, the credentials are added to skb only when configured by the sender or the listener.

However, as reported in the link below, it caused a regression for some programs that assume credentials are included in every skb, but sometimes not now.

The only problematic scenario would be that a socket starts listening before setting the option. Then, there will be 2 types of non-small race window, where a client can send skb without credentials, which the peer receives as an "invalid" message (and aborts the connection it seems ?):

Client Server
------ ------

                            s1.listen()  <-- No SO_PASS{CRED,PIDFD}

s2.connect()
s2.send() <-- w/o cred
s1.setsockopt(SO_PASS{CRED,PIDFD})
s2.send() <-- w/ cred

or

Client Server
------ ------

                            s1.listen()  <-- No SO_PASS{CRED,PIDFD}

s2.connect()
s2.send() <-- w/o cred
s3, _ = s1.accept() <-- Inherit cred options
s2.send() <-- w/o cred but not set yet

s3.setsockopt(SO_PASS{CRED,PIDFD})
s2.send() <-- w/ cred

It's unfortunate that buggy programs depend on the behaviour, but let's restore the previous behaviour.

Fixes 3f84d577b79d ("af_unix: Inherit sk_flags at connect().")
---
 net/unix/af_unix.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/unix/af_unix.c +2 -1
index fd6b5e17f6c4..87439d7f965d 100644
@@ -1971,7 +1971,8 @@ static void unix_maybe_add_creds(struct sk_buff *skb, const struct sock *sk,
 	if (UNIXCB(skb).pid)
 		return;
 
-	if (unix_may_passcred(sk) || unix_may_passcred(other)) {
+	if (unix_may_passcred(sk) || unix_may_passcred(other) ||
+	    !other->sk_socket) {
 		UNIXCB(skb).pid = get_pid(task_tgid(current));
 		current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid);
 	}
Signature · 1 line
2.49.0
Tested-by on v1 1/1
#1 lore raw
Quoted 67 lines · Kuniyuki Iwashima

From: Kuniyuki Iwashima <[email protected]>

Before the cited commit, the kernel unconditionally embedded SCM credentials to skb for embryo sockets even when both the sender and listener disabled SO_PASSCRED and SO_PASSPIDFD.

Now, the credentials are added to skb only when configured by the sender or the listener.

However, as reported in the link below, it caused a regression for some programs that assume credentials are included in every skb, but sometimes not now.

The only problematic scenario would be that a socket starts listening before setting the option. Then, there will be 2 types of non-small race window, where a client can send skb without credentials, which the peer receives as an "invalid" message (and aborts the connection it seems ?):

Client Server
------ ------

                            s1.listen()  <-- No SO_PASS{CRED,PIDFD}

s2.connect()
s2.send() <-- w/o cred
s1.setsockopt(SO_PASS{CRED,PIDFD})
s2.send() <-- w/ cred

or

Client Server
------ ------

                            s1.listen()  <-- No SO_PASS{CRED,PIDFD}

s2.connect()
s2.send() <-- w/o cred
s3, _ = s1.accept() <-- Inherit cred options
s2.send() <-- w/o cred but not set yet

s3.setsockopt(SO_PASS{CRED,PIDFD})
s2.send() <-- w/ cred

It's unfortunate that buggy programs depend on the behaviour, but let's restore the previous behaviour.

Fixes 3f84d577b79d ("af_unix: Inherit sk_flags at connect().")
---
 net/unix/af_unix.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/unix/af_unix.c +2 -1
index fd6b5e17f6c4..87439d7f965d 100644
@@ -1971,7 +1971,8 @@ static void unix_maybe_add_creds(struct sk_buff *skb, const struct sock *sk,
 	if (UNIXCB(skb).pid)
 		return;
 
-	if (unix_may_passcred(sk) || unix_may_passcred(other)) {
+	if (unix_may_passcred(sk) || unix_may_passcred(other) ||
+	    !other->sk_socket) {
 		UNIXCB(skb).pid = get_pid(task_tgid(current));
 		current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid);
 	}
Signature · 1 line
2.49.0
Tested-by on v1 1/1
#2 lore raw
In reply to Kuniyuki Iwashima

Em 11/06/2025 17:27, Kuniyuki Iwashima escreveu:

Quoted 40 lines

From: Kuniyuki Iwashima <[email protected]>

Before the cited commit, the kernel unconditionally embedded SCM credentials to skb for embryo sockets even when both the sender and listener disabled SO_PASSCRED and SO_PASSPIDFD.

Now, the credentials are added to skb only when configured by the sender or the listener.

However, as reported in the link below, it caused a regression for some programs that assume credentials are included in every skb, but sometimes not now.

The only problematic scenario would be that a socket starts listening before setting the option. Then, there will be 2 types of non-small race window, where a client can send skb without credentials, which the peer receives as an "invalid" message (and aborts the connection it seems ?):

   Client                    Server
   ------                    ------
                             s1.listen()  <-- No SO_PASS{CRED,PIDFD}
   s2.connect()
   s2.send()  <-- w/o cred
                             s1.setsockopt(SO_PASS{CRED,PIDFD})
   s2.send()  <-- w/  cred

or

   Client                    Server
   ------                    ------
                             s1.listen()  <-- No SO_PASS{CRED,PIDFD}
   s2.connect()
   s2.send()  <-- w/o cred
                             s3, _ = s1.accept()  <-- Inherit cred options
   s2.send()  <-- w/o cred                            but not set yet

                             s3.setsockopt(SO_PASS{CRED,PIDFD})
   s2.send()  <-- w/  cred
Quoted 7 lines

It's unfortunate that buggy programs depend on the behaviour, but let's restore the previous behaviour.

Fixes 3f84d577b79d ("af_unix: Inherit sk_flags at connect().")
Tested-by on v1 1/1
#3 lore raw
In reply to Kuniyuki Iwashima
Quoted 56 lines · Kuniyuki Iwashima

From: Kuniyuki Iwashima <[email protected]>

Before the cited commit, the kernel unconditionally embedded SCM credentials to skb for embryo sockets even when both the sender and listener disabled SO_PASSCRED and SO_PASSPIDFD.

Now, the credentials are added to skb only when configured by the sender or the listener.

However, as reported in the link below, it caused a regression for some programs that assume credentials are included in every skb, but sometimes not now.

The only problematic scenario would be that a socket starts listening before setting the option. Then, there will be 2 types of non-small race window, where a client can send skb without credentials, which the peer receives as an "invalid" message (and aborts the connection it seems ?):

   Client                    Server
   ------                    ------
                             s1.listen()  <-- No SO_PASS{CRED,PIDFD}
   s2.connect()
   s2.send()  <-- w/o cred
                             s1.setsockopt(SO_PASS{CRED,PIDFD})
   s2.send()  <-- w/  cred

or

   Client                    Server
   ------                    ------
                             s1.listen()  <-- No SO_PASS{CRED,PIDFD}
   s2.connect()
   s2.send()  <-- w/o cred
                             s3, _ = s1.accept()  <-- Inherit cred options
   s2.send()  <-- w/o cred                            but not set yet

                             s3.setsockopt(SO_PASS{CRED,PIDFD})
   s2.send()  <-- w/  cred

It's unfortunate that buggy programs depend on the behaviour, but let's restore the previous behaviour.

Fixes 3f84d577b79d ("af_unix: Inherit sk_flags at connect().")
---
  net/unix/af_unix.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
net/unix/af_unix.c +0 -0
index fd6b5e17f6c4..87439d7f965d 100644

net/unix/af_unix.c static void unix_maybe_add_creds(struct sk_buff *skb, const struct sock *sk, · line 1978

Quoted 9 lines
net/unix/af_unix.c +2 -1
@@ -1971,7 +1971,8 @@ static void unix_maybe_add_creds(struct sk_buff *skb, const struct sock *sk,
  	if (UNIXCB(skb).pid)
  		return;
 
-	if (unix_may_passcred(sk) || unix_may_passcred(other)) {
+	if (unix_may_passcred(sk) || unix_may_passcred(other) ||
+	    !other->sk_socket) {
  		UNIXCB(skb).pid = get_pid(task_tgid(current));
  		current_uid_gid(&UNIXCB(skb).uid, &UNIXCB(skb).gid);
  	}
#4 lore raw
In reply to Kuniyuki Iwashima
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Wed, 11 Jun 2025 13:27:35 -0700 you wrote:

From: Kuniyuki Iwashima <[email protected]>

Before the cited commit, the kernel unconditionally embedded SCM credentials to skb for embryo sockets even when both the sender and listener disabled SO_PASSCRED and SO_PASSPIDFD.

Now, the credentials are added to skb only when configured by the sender or the listener.

[...]

Here is the summary with links:
- [v1,net] af_unix: Allow passing cred for embryo without SO_PASSCRED/SO_PASSPIDFD.

    https://git.kernel.org/netdev/net/c/43fb2b30eea7

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#5 lore raw
In reply to Kuniyuki Iwashima
Quoted 37 lines · Kuniyuki Iwashima

From: Kuniyuki Iwashima <[email protected]>

Before the cited commit, the kernel unconditionally embedded SCM credentials to skb for embryo sockets even when both the sender and listener disabled SO_PASSCRED and SO_PASSPIDFD.

Now, the credentials are added to skb only when configured by the sender or the listener.

However, as reported in the link below, it caused a regression for some programs that assume credentials are included in every skb, but sometimes not now.

The only problematic scenario would be that a socket starts listening before setting the option. Then, there will be 2 types of non-small race window, where a client can send skb without credentials, which the peer receives as an "invalid" message (and aborts the connection it seems ?):

Client Server
------ ------

                            s1.listen()  <-- No SO_PASS{CRED,PIDFD}

s2.connect()
s2.send() <-- w/o cred
s1.setsockopt(SO_PASS{CRED,PIDFD})
s2.send() <-- w/ cred

or

Client Server
------ ------

                            s1.listen()  <-- No SO_PASS{CRED,PIDFD}

s2.connect()
s2.send() <-- w/o cred
s3, _ = s1.accept() <-- Inherit cred options
s2.send() <-- w/o cred but not set yet

Quoted 5 lines

s3.setsockopt(SO_PASS{CRED,PIDFD})
s2.send() <-- w/ cred

It's unfortunate that buggy programs depend on the behaviour, but let's restore the previous behaviour.

For the record, this one fixes the wlroots and Xorg crashes on USB-C DisplayPort Altmode hotplug that I hit consistently with 6.16-rc1 on machines like the Lenovo ThinkPad X13s and T14s.

Quoted 4 lines
Fixes 3f84d577b79d ("af_unix: Inherit sk_flags at connect().")

Johan