From: Tristan Madani Since commit 0ede61d8589cc ("file: convert to SLAB_TYPESAFE_BY_RCU"), struct file objects can have their slab slot reused while RCU readers still hold pointers to them. The xt_owner match reads file->f_cred to check fsuid/fsgid/group_info in the packet path without pinning the file. If the file is freed and the slab slot is reused between reading sock->file and dereferencing f_cred, the credential check will operate on stale or unrelated credential data. Use get_file_rcu() to take a reference on the file before accessing its credentials, and fput() to release it on all return paths. Fixes: 0ede61d8589cc ("file: convert to SLAB_TYPESAFE_BY_RCU") Cc: stable@vger.kernel.org Signed-off-by: Tristan Madani --- net/netfilter/xt_owner.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/net/netfilter/xt_owner.c b/net/netfilter/xt_owner.c index 8f2e57b2a5868..6386b52147d87 100644 --- a/net/netfilter/xt_owner.c +++ b/net/netfilter/xt_owner.c @@ -65,8 +65,8 @@ owner_mt(const struct sk_buff *skb, struct xt_action_param *par) const struct xt_owner_match_info *info = par->matchinfo; struct sock *sk = skb_to_full_sk(skb); struct net *net = xt_net(par); - const struct socket *sock; - const struct file *filp; + struct socket *sock; + struct file *filp; if (!sk || !READ_ONCE(sk->sk_socket) || !net_eq(net, sock_net(sk))) return (info->match ^ info->invert) == 0; @@ -77,13 +77,17 @@ owner_mt(const struct sk_buff *skb, struct xt_action_param *par) */ return false; - /* 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. + /* 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. Since struct file uses SLAB_TYPESAFE_BY_RCU, pin the + * file with get_file_rcu() to ensure f_cred is not from a + * reused slab slot. */ sock = READ_ONCE(sk->sk_socket); - filp = sock ? READ_ONCE(sock->file) : NULL; + if (sock) + filp = get_file_rcu(&sock->file); + else + filp = NULL; if (filp == NULL) return ((info->match ^ info->invert) & (XT_OWNER_UID | XT_OWNER_GID)) == 0; @@ -94,8 +98,10 @@ owner_mt(const struct sk_buff *skb, struct xt_action_param *par) if ((uid_gte(filp->f_cred->fsuid, uid_min) && uid_lte(filp->f_cred->fsuid, uid_max)) ^ - !(info->invert & XT_OWNER_UID)) + !(info->invert & XT_OWNER_UID)) { + fput(filp); return false; + } } if (info->match & XT_OWNER_GID) { @@ -120,10 +126,13 @@ owner_mt(const struct sk_buff *skb, struct xt_action_param *par) } } - if (match ^ !(info->invert & XT_OWNER_GID)) + if (match ^ !(info->invert & XT_OWNER_GID)) { + fput(filp); return false; + } } + fput(filp); return true; } -- 2.47.3