* Re: [PATCH v2 net] net/sched: cls_flow: pin file with get_file_active before reading f_cred
2026-11-20 23:13 [PATCH v2 net] net/sched: cls_flow: pin file with get_file_active before reading f_cred Tristan Madani
@ 2026-11-24 23:25 ` netdev-bot+sashiko
0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 23:25 UTC (permalink / raw)
To: tristmd
Cc: jhs, jiri, davem, edumazet, kuba, pabeni, horms, brauner, netdev,
linux-kernel, stable, tristan
Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 3 · Low: 2
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] The patch fixes a reader that runs under RCU only, without a
file reference, and reads sk->sk_socket->file->f_cred.
- [Medium] flow_get_skuid() and flow_get_skgid() read sk->sk_socket twice
with plain, unmarked loads: once in `if (!sk || !sk->sk_socket)` and…
- [Medium] Because the classifier now takes and drops a real f_ref from
softirq or BH-disabled context, it can end up holding the last…
- [Low] flow_get_skuid() and flow_get_skgid() in net/sched/cls_flow.c now
do get_file_active() and then fput() on every classified packet.
- [Low] The commit author is 'Tristan Madani <tristmd@gmail.com>', but
the only Signed-off-by is 'Tristan Madani…
--
net/sched: cls_flow: pin file with get_file_active before reading f_cred
This patch changes flow_get_skuid() and flow_get_skgid() in cls_flow so
that they take a reference on sk->sk_socket->file with get_file_active()
before reading f_cred, and drop it with fput() afterwards. The stated
reason is that filp_cache is SLAB_TYPESAFE_BY_RCU, so a concurrent close()
can free and recycle the struct file while the classifier reads it.
> Fixes: 0ede61d8589cc ("file: convert to SLAB_TYPESAFE_BY_RCU")
> Cc: stable@vger.kernel.org
[Severity: Medium]
The Fixes: tag points at the tree-wide conversion to SLAB_TYPESAFE_BY_RCU,
but only the cls_flow readers change here. Should the other readers that
use the same pattern be fixed too?
These still load sock->file without pinning it and then read file->f_cred
under RCU only:
owner_mt() in net/netfilter/xt_owner.c
nft_meta_get_eval_skugid() in net/netfilter/nft_meta.c
the NFQA_UID/NFQA_GID code in net/netfilter/nfnetlink_queue.c
__build_packet_message() in net/netfilter/nfnetlink_log.c
nf_log_dump_sk_uid_gid() in net/netfilter/nf_log_syslog.c
skb_may_tx_timestamp() in net/core/skbuff.c, via file_ns_capable()
Most of them carry this comment:
/* The sk pointer remains valid as long as the skb is. The sk_socket and
* file pointer may become NULL if the socket is closed. Both structures
* (including file->cred) are RCU freed which means they can be accessed
* within a RCU read section.
*/
If this patch's premise is correct, that comment is wrong for struct file.
file_free() calls put_cred() and then kmem_cache_free() right away, and
Documentation/filesystems/files.rst says struct file fields cannot be
checked under RCU lookup without first taking a reference.
The same race applies at those sites:
reader loads sock->file
__sock_release() does WRITE_ONCE(sock->file, NULL)
__fput() frees the file, another task reallocates it
reader loads file->f_cred and gets the new owner's cred
Cred freeing is RCU-deferred, so this is memory safe. The result is still
wrong, though: the xt_owner/nft_meta owner match, or the logged and queued
UID/GID, would use another task's fsuid/fsgid.
With Cc: stable and a tree-wide Fixes: tag, how can a stable backporter
tell whether this bug class is closed? Could the commit message at least
explain why only cls_flow needs the change, or could the sibling sites and
their comments be updated in the same series?
> Signed-off-by: Tristan Madani <tristan@talencesecurity.com>
[Severity: Low]
This isn't a bug, but the author address is tristmd@gmail.com and the only
Signed-off-by uses tristan@talencesecurity.com. checkpatch reports this as
FROM_SIGN_OFF_MISMATCH. Could the patch be sent From: the Signed-off-by
address, or get a matching Signed-off-by?
> diff --git a/net/sched/cls_flow.c b/net/sched/cls_flow.c
> index 2ef449706638b..8653e6fdcfa2a 100644
> --- a/net/sched/cls_flow.c
> +++ b/net/sched/cls_flow.c
[ ... ]
> @@ -208,10 +209,16 @@ static u32 flow_get_rtclassid(const struct sk_buff *skb)
> static u32 flow_get_skuid(const struct sk_buff *skb)
> {
> struct sock *sk = skb_to_full_sk(skb);
> + struct file *filp;
>
> - if (sk && sk->sk_socket && sk->sk_socket->file) {
> - kuid_t skuid = sk->sk_socket->file->f_cred->fsuid;
> + if (!sk || !sk->sk_socket)
> + return 0;
> +
> + filp = get_file_active(&sk->sk_socket->file);
[Severity: Medium]
Is sk->sk_socket read twice here with plain loads? It is read once for the
NULL check and again to build &sk->sk_socket->file. The same applies to
flow_get_skgid().
close() can reach sock_orphan() on another CPU:
include/net/sock.h:sock_orphan() {
write_lock_bh(&sk->sk_callback_lock);
sock_set_flag(sk, SOCK_DEAD);
sk_set_socket(sk, NULL);
...
}
sk_set_socket() does WRITE_ONCE(sk->sk_socket, sock). The classifier does
not hold sk_callback_lock, so KCSAN will report this plain read against
that marked write.
The compiler may also reload sk->sk_socket after the check. If
sock_orphan() runs between the two loads, get_file_active() receives
(struct file **)offsetof(struct socket, file). __get_file_rcu() then
dereferences that near-NULL address in softirq context:
file = rcu_dereference_raw(*f);
The peer readers such as owner_mt() take a single snapshot instead:
sock = READ_ONCE(sk->sk_socket);
filp = sock ? READ_ONCE(sock->file) : NULL;
The old code had the same double read. These lines are being rewritten to
handle a concurrent close(), though, so would something like this work
instead?
struct socket *sock = READ_ONCE(sk->sk_socket);
if (!sock)
return 0;
filp = get_file_active(&sock->file);
struct socket is freed through the sockfs inode RCU callback, so it stays
valid under the caller's RCU-BH section.
> + if (filp) {
> + kuid_t skuid = filp->f_cred->fsuid;
>
> + fput(filp);
[Severity: Medium]
Can this fput() drop the last reference to the socket's file? Consider
get_file_active() succeeding here while close() runs on another CPU:
classifier: get_file_active() succeeds, refcount is 2
close(): drops its reference, returns to userspace
classifier: fput() drops the last reference from BH context
__fput_deferred() then takes the delayed path, because in_interrupt() is
true in softirq and under rcu_read_lock_bh():
fs/file_table.c:__fput_deferred() {
...
if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
...
}
if (llist_add(&file->f_llist, &delayed_fput_list))
schedule_delayed_work(&delayed_fput_work, 1);
}
This moves __fput()->sock_close()->__sock_release()->inet_release() and
tcp_close() into a system_wq kworker. They run at least one jiffy after
close() has returned.
Two effects follow:
- An immediate re-bind of the port can fail with EADDRINUSE.
- close() with SO_LINGER set no longer waits. The linger wait instead
happens in the kworker and blocks a shared worker for up to the linger
timeout.
The same applies when __get_file_rcu() briefly takes a reference on a
recycled, unrelated file and calls fput() on the mismatch path.
Before this patch the classifier never held a file reference. close() in a
single-threaded process therefore released the socket synchronously.
> return from_kuid(&init_user_ns, skuid);
> }
> return 0;
> @@ -220,10 +227,16 @@ static u32 flow_get_skuid(const struct sk_buff *skb)
> static u32 flow_get_skgid(const struct sk_buff *skb)
> {
> struct sock *sk = skb_to_full_sk(skb);
> + struct file *filp;
> +
> + if (!sk || !sk->sk_socket)
> + return 0;
>
> - if (sk && sk->sk_socket && sk->sk_socket->file) {
> - kgid_t skgid = sk->sk_socket->file->f_cred->fsgid;
> + filp = get_file_active(&sk->sk_socket->file);
> + if (filp) {
> + kgid_t skgid = filp->f_cred->fsgid;
>
> + fput(filp);
[Severity: Low]
Is the new per-packet cost intended? Both flow_get_skuid() and
flow_get_skgid() now do an atomic RMW on file->f_ref in __get_file_rcu(),
via file_ref_get(), plus another in fput(), via file_ref_put(). This
happens for every classified packet, where the old path was a plain
lockless read.
A filter keyed on both SKUID and SKGID writes the struct file cacheline
four times per packet. The writes come from whichever CPU is transmitting
or receiving on that socket.
Nearby netfilter readers were made lockless on purpose, for example commit
cdec942ac2006d ("netfilter: xt_owner: no longer acquire sk_callback_lock
in mt_owner()"), which followed 983512f3a87f.
Would a cheaper check work, such as re-checking READ_ONCE(sock->file) ==
filp after loading f_cred? __sock_release() clears sock->file before the
file can be freed, but this would need careful barrier analysis. If the
refcount approach stays, could the commit message mention the fast-path
cost?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120231354.1361803-1-tristan%40talencesecurity.com
^ permalink raw reply [flat|nested] 2+ messages in thread