All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jan Koester" <jan.koester@tuxist.de>,
	"Christian Brauner" <brauner@kernel.org>,
	"Alexander Viro" <viro@zeniv.linux.org.uk>,
	"Serge E. Hallyn" <serge@hallyn.com>
Cc: "Jan Kara" <jack@suse.cz>, "Theodore Y. Ts'o" <tytso@mit.edu>,
	"Paul Moore" <paul@paul-moore.com>,
	"Arnd Bergmann" <arnd@arndb.de>,
	linux-security-module@vger.kernel.org,
	linux-ext4@vger.kernel.org, audit@vger.kernel.org,
	keyrings@vger.kernel.org, "Chuck Lever" <cel@kernel.org>,
	"Jeff Layton" <jlayton@kernel.org>,
	"Anna Schumaker" <anna@kernel.org>
Subject: Re: [RFC PATCH v2 04/41] net: sock: read sk_uid through a seqcount
Date: Thu, 22 Oct 2026 03:17:27 +0000	[thread overview]
Message-ID: <sashiko-outbox-180905@kernel.org> (raw)
In-Reply-To: <3ace21531501a80a31350f9a178828212fc7a224.1792552161.git.jan.koester@tuxist.de>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] net: sock: write_seqcount_begin() called in preemptible sock_init_data_uid()
- [High] net: sock: raw seqcount_t under sk_callback_lock livelocks on PREEMPT_RT
--

commit e15d18b6d3adc57553d8944b44a103206aaa6a91
Author: Jan Koester <jan.koester@tuxist.de>

net: sock: read sk_uid through a seqcount

This commit introduces a sequence counter (sk_uid_seq) in struct sock to
protect lockless reads of sk_uid against concurrent updates in sock_graft()
and sockfs_setattr(). Writers update sk_uid via __sk_set_uid(), while readers
use sk_uid() to retry until a consistent value is read.

> diff --git a/include/net/sock.h b/include/net/sock.h
> index 60ea55dc18854..0986d946659a8 100644
> --- a/include/net/sock.h
> +++ b/include/net/sock.h

[ ... ]

> @@ -509,6 +511,7 @@ struct sock {
>  	u32			sk_priority;
>  	u32			sk_mark;
>  	kuid_t			sk_uid;
> +	seqcount_t		sk_uid_seq;
>  	u16			sk_protocol;
>  	u16			sk_type;
>  	struct dst_entry __rcu	*sk_dst_cache;
> @@ -2109,11 +2112,23 @@ static inline int sk_rx_queue_get(const struct sock *sk)
>  	return -1;
>  }
>  
> +/*
> + * sk_uid is too big for WRITE_ONCE(): writers hold sk_callback_lock (or own
> + * the socket alone, before it is published) and bump sk_uid_seq around the
> + * store, so sk_uid() can read it without a lock.
> + */
> +static inline void __sk_set_uid(struct sock *sk, kuid_t uid)
> +{
> +	write_seqcount_begin(&sk->sk_uid_seq);
> +	sk->sk_uid = uid;
> +	write_seqcount_end(&sk->sk_uid_seq);
> +}
> +
>  static inline void sk_set_socket(struct sock *sk, struct socket *sock)
>  {
>  	WRITE_ONCE(sk->sk_socket, sock);
>  	if (sock) {
> -		WRITE_ONCE(sk->sk_uid, SOCK_INODE(sock)->i_uid);
> +		__sk_set_uid(sk, SOCK_INODE(sock)->i_uid);
>  		WRITE_ONCE(sk->sk_ino, SOCK_INODE(sock)->i_ino);
>  	} else {
>  		/* Note: sk_uid is unchanged. */
> @@ -2161,8 +2176,15 @@ static inline u64 sock_i_ino(const struct sock *sk)
>  
>  static inline kuid_t sk_uid(const struct sock *sk)
>  {
> -	/* Paired with WRITE_ONCE() in sockfs_setattr() */
> -	return READ_ONCE(sk->sk_uid);
> +	unsigned int seq;
> +	kuid_t uid;
> +
> +	/* Paired with __sk_set_uid() */
> +	do {
> +		seq = read_seqcount_begin(&sk->sk_uid_seq);
> +		uid = sk->sk_uid;
> +	} while (read_seqcount_retry(&sk->sk_uid_seq, seq));
> +	return uid;
>  }

[Severity: High]
Could using a raw seqcount_t for sk_uid_seq cause preemption warnings and a
reader livelock on preempt-rt kernels?

Runtime writers such as sockfs_setattr(), sock_graft(), and
mptcp_sock_graft() serialize updates to sk->sk_uid with
write_lock_bh(&sk->sk_callback_lock) before calling __sk_set_uid() (directly
or via sk_set_socket()). On preempt-rt kernels, write_lock_bh() on an
rwlock_t does not disable preemption:

include/linux/rwlock_rt.h:write_lock_bh() {
	local_bh_disable();
	rt_write_lock(rwlock);
}

Because sk_uid_seq is a raw seqcount_t rather than a seqcount_rwlock_t
associated with sk_callback_lock, write_seqcount_begin(&sk->sk_uid_seq) does
not disable preemption and triggers lockdep_assert_preemption_disabled() in
__seqprop_assert():

include/linux/seqlock.h:__seqprop_assert() {
	lockdep_assert_preemption_disabled();
}

In addition, if a higher-priority task preempts the writer between
write_seqcount_begin() and write_seqcount_end() while sk_uid_seq.sequence is
odd and calls sk_uid(), read_seqcount_begin(&sk->sk_uid_seq) will spin
indefinitely in __read_seqcount_begin():

include/linux/seqlock.h:__read_seqcount_begin() {
	while (unlikely((__seq = seqprop_sequence(s)) & 1))
		cpu_relax();
}

With a raw seqcount_t, the reader has no associated rwlock_t to block on and
priority-boost the preempted writer. Should sk_uid_seq be a
seqcount_rwlock_t initialized with sk_callback_lock, or should preemption be
disabled around the write section?

> diff --git a/net/core/sock.c b/net/core/sock.c
> index e8551df8330ff..300bc75ce13e6 100644
> --- a/net/core/sock.c
> +++ b/net/core/sock.c

[ ... ]

> @@ -3773,7 +3776,7 @@ void sock_init_data_uid(struct socket *sock, struct sock *sk, kuid_t uid)
>  	} else {
>  		RCU_INIT_POINTER(sk->sk_wq, NULL);
>  	}
> -	sk->sk_uid	=	uid;
> +	__sk_set_uid(sk, uid);
>  
>  	sk->sk_state_change	=	sock_def_wakeup;
>  	sk->sk_data_ready	=	sock_def_readable;

[Severity: High]
What happens when sock_init_data_uid() calls sk_set_socket() and
__sk_set_uid() in preemptible process context with softirqs enabled?

During socket creation (for example, inet_create() -> sock_init_data() ->
sock_init_data_uid()), sock_init_data_uid() runs in normal process context
with preemption and bottom halves enabled and no locks held. Calling
__sk_set_uid() (both via sk_set_socket() when sock is non-null and directly
in sock_init_data_uid()) invokes write_seqcount_begin(&sk->sk_uid_seq) on a
raw seqcount_t while preemption is enabled, which immediately triggers the
warning in lockdep_assert_preemption_disabled().

Furthermore, write_seqcount_begin(&sk->sk_uid_seq) records a lockdep write
acquisition on the sk_uid_seq class with softirqs enabled. When sk_uid() is
later called from softirq context (such as during tcp_v4_rcv() ->
tcp_v4_syn_recv_sock() -> sk_clone(), or during routing and reuseport
lookups), read_seqcount_begin(&sk->sk_uid_seq) records a softirq read on the
same class in mark_usage() in kernel/locking/lockdep.c, triggering an
inconsistent lock state warning (softirq-on-w to in-softirq-r).

Since the socket is not yet published in sock_init_data_uid(), should
sk->sk_uid be initialized with a plain assignment when the socket is still
unshared, only using __sk_set_uid() under write_lock_bh(&sk->sk_callback_lock)?

[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1792552161.git.jan.koester@tuxist.de?part=4

  reply	other threads:[~2026-10-22  3:17 UTC|newest]

Thread overview: 89+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-21  3:15 [RFC PATCH v2 00/41] UUID and GUID user and group ids (*_r system calls) Jan Koester
2026-10-21  3:15 ` [RFC PATCH v2 01/41] uuid: move uuid_t and guid_t into <linux/uuid_types.h> Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 02/41] uapi: add <linux/uidgid_r.h> for 128 bit capable user and group ids Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 03/41] uidgid: add uid_is_legacy() and gid_is_legacy() Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 04/41] net: sock: read sk_uid through a seqcount Jan Koester
2026-10-22  3:17   ` sashiko-bot [this message]
2026-10-21  3:15 ` [RFC PATCH v2 05/41] af_unix: keep sender ids in skb->cb only when they are classic ids Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 06/41] netlink: keep classic sender ids in skb->cb Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 07/41] Bluetooth: " Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 08/41] smb: client: use __kuid_val() for the unix SIDs of the caller Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 09/41] uidgid: let kuid_t and kgid_t hold a UUID or a GUID Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 10/41] uidgid: convert between kernel ids and the ids of the *_r system calls Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 11/41] fscrypt: show the whole uid in the description of user key claims Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 12/41] keys: name per-user keyrings of UUID and GUID users by the whole uid Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 13/41] ublk: refuse unprivileged devices to UUID and GUID users Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 14/41] nfsd: don't hand out uid 0 for read layouts of UUID-owned files Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 15/41] SUNRPC: name UUID and GUID users to gssd as uid_r= Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 16/41] ecryptfs: hash daemons by kuid_hash() Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 17/41] quota: don't account UUID and GUID owners Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 18/41] fs: keep UUID and GUID owners off filesystems that can't store them Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 19/41] cred: add the *_r variants of the get/set*id() system calls Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 20/41] groups: add getgroups_r() and setgroups_r() Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 21/41] fs: add chown_r(), lchown_r(), fchown_r() and fchownat_r() Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 22/41] arch: wire up the *_r uid and gid system calls Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 23/41] statx: report UUID and GUID owners Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 24/41] shmem, ramfs: allow " Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 25/41] uidgid: parse and print ids in their text form Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 26/41] proc: show UUID and GUID ids in status, accept them for loginuid Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 27/41] audit: log UUID and GUID ids in full Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 28/41] net: add SO_PEERCRED_R, SO_PEERGROUPS_R and SCM_CREDENTIALS_R Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 29/41] signal: report the sender's UUID or GUID uid in si_uid_r Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 30/41] ipc: add IPC_STAT_R and IPC_SET_R Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 31/41] ext4: add the uid_r feature for UUID and GUID owners Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 32/41] ext4: let uid_r be turned on while the filesystem is mounted Jan Koester
2026-10-22  3:17   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 33/41] posix_acl: support UUID and GUID entries Jan Koester
2026-10-22  3:18   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 34/41] ext4: store ACLs with UUID and GUID entries on uid_r filesystems Jan Koester
2026-10-22  3:18   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 35/41] Documentation: describe UUID and GUID user and group ids Jan Koester
2026-10-22  3:18   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 36/41] selftests: add tests for UUID and GUID ids Jan Koester
2026-10-22  3:18   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 37/41] ovl: allow UUID and GUID owners when the upper layer does Jan Koester
2026-10-22  3:18   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 38/41] devpts: allow UUID and GUID owners Jan Koester
2026-10-22  3:18   ` sashiko-bot
2026-10-21  3:15 ` [RFC PATCH v2 39/41] ksmbd: log in UUID and GUID users and map them to SIDs Jan Koester
2026-10-22  3:18   ` sashiko-bot
2026-10-21  3:16 ` [RFC PATCH v2 40/41] nfsd, nfs: carry UUID and GUID owners as their text form Jan Koester
2026-10-21 17:35   ` Chuck Lever
2026-10-22  2:52     ` Takeshi Nishimura
2026-10-22  3:18   ` sashiko-bot
2026-10-21  3:16 ` [RFC PATCH v2 41/41] Documentation: uid_r: describe SMB and NFSv4 Jan Koester
2026-10-22  3:18   ` sashiko-bot
2026-10-21  3:20 ` [RFC PATCH v2 00/41] UUID and GUID user and group ids (*_r system calls) jan Koester
2026-10-21 14:26 ` Adding dreaded Windows SID-style, or. Repeating history, the bad chapters. UUID/GUIDRe: " Lionel Cons
2026-10-22 17:00 ` Casey Schaufler
2026-10-22 18:26   ` Theodore Tso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-180905@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=anna@kernel.org \
    --cc=arnd@arndb.de \
    --cc=audit@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=cel@kernel.org \
    --cc=jack@suse.cz \
    --cc=jan.koester@tuxist.de \
    --cc=jlayton@kernel.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=paul@paul-moore.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=serge@hallyn.com \
    --cc=tytso@mit.edu \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.