Skip to contentSkip to conversation

netfilter: pin file with get_file_active before reading f_cred

PATCH New Tristan Madani 1 participant 6 messages Nov 27

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

v2 cover netfilter: pin file with get_file_active before reading f_cred

From: Tristan Madani <[email protected]>

Five netfilter callsites read file->f_cred through the sk->sk_socket->file chain under RCU without pinning the file reference. Since commit 0ede61d8589c ("file: convert to SLAB_TYPESAFE_BY_RCU"), the file slab slot can be freed and reallocated within an RCU grace period, so these readers may observe stale credential data from a recycled file object.

This series pins the file with get_file_active() before reading f_cred and releases it with fput() afterwards, matching the pattern used by the recently merged cls_flow fix (commit 59cf9e6f48e5a).

Changes in v2:
- Switch from get_file_rcu() to get_file_active() per API change (get_file_rcu was removed; get_file_active is the current API)
- Reference merged cls_flow fix (59cf9e6f48e5a) which uses the same pattern for the same bug class

Tristan Madani (5):
netfilter: xt_owner: pin file with get_file_active before reading
f_cred
netfilter: nft_meta: pin file with get_file_active before reading
f_cred
netfilter: nfnetlink_log: pin file with get_file_active before reading
f_cred
netfilter: nfnetlink_queue: pin file with get_file_active before
reading f_cred
netfilter: nf_log_syslog: pin file with get_file_active before reading
f_cred

 net/netfilter/nf_log_syslog.c   | 28 +++++++++++++-------------
 net/netfilter/nfnetlink_log.c   | 35 ++++++++++++++++-----------------
 net/netfilter/nfnetlink_queue.c | 35 ++++++++++++++++-----------------
 net/netfilter/nft_meta.c        | 16 +++++++--------
 net/netfilter/xt_owner.c        | 27 ++++++++++++++-----------
 5 files changed, 72 insertions(+), 69 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v2 1/5 netfilter: xt_owner: pin file with get_file_active before reading f_cred

From: Tristan Madani <[email protected]>

owner_mt() reads sk->sk_socket->file->f_cred without holding a reference to the file. Since the file slab uses SLAB_TYPESAFE_BY_RCU, a concurrent close() may free and recycle the file object, causing stale credential data to be read through f_cred.

Pin the file with get_file_active() before accessing its credentials and release with fput() afterwards.

Found by manual code inspection; not triggered at runtime.

Fixes 0ede61d8589c ("file: convert to SLAB_TYPESAFE_BY_RCU")
---
 net/netfilter/xt_owner.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
net/netfilter/xt_owner.c +16 -11
index 8f2e57b2a5868..75fcff0ce21aa 100644
@@ -10,6 +10,7 @@
 #include <linux/module.h>
 #include <linux/skbuff.h>
 #include <linux/file.h>
+#include <linux/fs.h>
 #include <linux/cred.h>
 
 #include <net/sock.h>
@@ -65,8 +66,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,14 +78,13 @@ 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.
-	 */
 	sock = READ_ONCE(sk->sk_socket);
-	filp = sock ? READ_ONCE(sock->file) : NULL;
-	if (filp == NULL)
+	if (!sock)
+		return ((info->match ^ info->invert) &
+		       (XT_OWNER_UID | XT_OWNER_GID)) == 0;
+
+	filp = get_file_active(&sock->file);
+	if (!filp)
 		return ((info->match ^ info->invert) &
 		       (XT_OWNER_UID | XT_OWNER_GID)) == 0;
 
@@ -94,8 +94,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 +122,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;
 }
 
Signature · 1 line
2.47.3
#2 lore raw

PATCH v2 2/5 netfilter: nft_meta: pin file with get_file_active before reading f_cred

In reply to Tristan Madani

From: Tristan Madani <[email protected]>

nft_meta_get_eval_skugid() reads sk->sk_socket->file->f_cred without holding a reference to the file. Since the file slab uses SLAB_TYPESAFE_BY_RCU, a concurrent close() may free and recycle the file object, causing stale credential data to be read through f_cred.

Pin the file with get_file_active() before accessing its credentials and release with fput() afterwards.

Found by manual code inspection; not triggered at runtime.

Fixes 0ede61d8589c ("file: convert to SLAB_TYPESAFE_BY_RCU")
---
 net/netfilter/nft_meta.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
net/netfilter/nft_meta.c +8 -8
index 01cfbaa365250..abf0bbce9bfda 100644
@@ -17,6 +17,7 @@
 #include <linux/random.h>
 #include <linux/smp.h>
 #include <linux/static_key.h>
+#include <linux/fs.h>
 #include <net/dst.h>
 #include <net/ip.h>
 #include <net/sock.h>
@@ -135,19 +136,17 @@ nft_meta_get_eval_skugid(enum nft_meta_keys key,
 			 const struct nft_pktinfo *pkt)
 {
 	const struct sock *sk = skb_to_full_sk(pkt->skb);
-	const struct socket *sock;
-	const struct file *file;
+	struct socket *sock;
+	struct file *file;
 
 	if (!sk || !sk_fullsock(sk) || !net_eq(nft_net(pkt), sock_net(sk)))
 		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.
-	 */
 	sock = READ_ONCE(sk->sk_socket);
-	file = sock ? READ_ONCE(sock->file) : NULL;
+	if (!sock)
+		return false;
+
+	file = get_file_active(&sock->file);
 	if (!file)
 		return false;
 
@@ -164,6 +163,7 @@ nft_meta_get_eval_skugid(enum nft_meta_keys key,
 		break;
 	}
 
+	fput(file);
 	return true;
 }
 
Signature · 1 line
2.47.3
#3 lore raw

PATCH v2 3/5 netfilter: nfnetlink_log: pin file with get_file_active before reading f_cred

In reply to Tristan Madani

From: Tristan Madani <[email protected]>

__build_packet_message() reads sk->sk_socket->file->f_cred without holding a reference to the file. Since the file slab uses SLAB_TYPESAFE_BY_RCU, a concurrent close() may free and recycle the file object, causing stale credential data to be read through f_cred.

Pin the file with get_file_active() before accessing its credentials and release with fput() afterwards.

Found by manual code inspection; not triggered at runtime.

Fixes 0ede61d8589c ("file: convert to SLAB_TYPESAFE_BY_RCU")
---
 net/netfilter/nfnetlink_log.c | 35 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 18 deletions(-)
net/netfilter/nfnetlink_log.c +17 -18
index d923f2cb13980..d4fe9a12088f4 100644
@@ -28,6 +28,7 @@
 #include <linux/spinlock.h>
 #include <linux/sysctl.h>
 #include <linux/proc_fs.h>
+#include <linux/fs.h>
 #include <linux/security.h>
 #include <linux/list.h>
 #include <linux/slab.h>
@@ -632,25 +633,23 @@ __build_packet_message(struct nfnl_log_net *log,
 	/* UID */
 	sk = skb->sk;
 	if (sk && sk_fullsock(sk)) {
-		const struct socket *sock;
-		const struct file *file;
-
-		/* 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.
-		 */
+		struct socket *sock;
+		struct file *file;
+
 		sock = READ_ONCE(sk->sk_socket);
-		file = sock ? READ_ONCE(sock->file) : NULL;
-		if (file) {
-			const struct cred *cred = file->f_cred;
-			struct user_namespace *user_ns = inst->peer_user_ns;
-			__be32 uid = htonl(from_kuid_munged(user_ns, cred->fsuid));
-			__be32 gid = htonl(from_kgid_munged(user_ns, cred->fsgid));
-			if (nla_put_be32(inst->skb, NFULA_UID, uid) ||
-			    nla_put_be32(inst->skb, NFULA_GID, gid))
-				goto nla_put_failure;
+		if (sock) {
+			file = get_file_active(&sock->file);
+			if (file) {
+				const struct cred *cred = file->f_cred;
+				struct user_namespace *user_ns = inst->peer_user_ns;
+				__be32 uid = htonl(from_kuid_munged(user_ns, cred->fsuid));
+				__be32 gid = htonl(from_kgid_munged(user_ns, cred->fsgid));
+
+				fput(file);
+				if (nla_put_be32(inst->skb, NFULA_UID, uid) ||
+				    nla_put_be32(inst->skb, NFULA_GID, gid))
+					goto nla_put_failure;
+			}
 		}
 	}
 
Signature · 1 line
2.47.3
#4 lore raw

PATCH v2 4/5 netfilter: nfnetlink_queue: pin file with get_file_active before reading f_cred

In reply to Tristan Madani

From: Tristan Madani <[email protected]>

nfqnl_put_sk_uidgid() reads sk->sk_socket->file->f_cred without holding a reference to the file. Since the file slab uses SLAB_TYPESAFE_BY_RCU, a concurrent close() may free and recycle the file object, causing stale credential data to be read through f_cred.

Pin the file with get_file_active() before accessing its credentials and release with fput() afterwards.

Found by manual code inspection; not triggered at runtime.

Fixes 0ede61d8589c ("file: convert to SLAB_TYPESAFE_BY_RCU")
---
 net/netfilter/nfnetlink_queue.c | 35 ++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 18 deletions(-)
net/netfilter/nfnetlink_queue.c +17 -18
index a8aa7ad832c3f..7a31267ae38f6 100644
@@ -22,6 +22,7 @@
 #include <linux/netdevice.h>
 #include <linux/netfilter.h>
 #include <linux/proc_fs.h>
+#include <linux/fs.h>
 #include <linux/netfilter_ipv4.h>
 #include <linux/netfilter_ipv6.h>
 #include <linux/netfilter_bridge.h>
@@ -578,29 +579,27 @@ nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet,
 
 static int nfqnl_put_sk_uidgid(struct sk_buff *skb, struct sock *sk)
 {
-	const struct socket *sock;
-	const struct file *file;
-	const struct cred *cred;
+	struct socket *sock;
+	struct file *file;
 
 	if (!sk_fullsock(sk))
 		return 0;
 
-	/* 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.
-	 */
 	sock = READ_ONCE(sk->sk_socket);
-	file = sock ? READ_ONCE(sock->file) : NULL;
-	if (file) {
-		cred = file->f_cred;
-		if (nla_put_be32(skb, NFQA_UID,
-		    htonl(from_kuid_munged(&init_user_ns, cred->fsuid))))
-			goto nla_put_failure;
-		if (nla_put_be32(skb, NFQA_GID,
-		    htonl(from_kgid_munged(&init_user_ns, cred->fsgid))))
-			goto nla_put_failure;
+	if (sock) {
+		file = get_file_active(&sock->file);
+		if (file) {
+			const struct cred *cred = file->f_cred;
+			__be32 uid = htonl(from_kuid_munged(&init_user_ns,
+							cred->fsuid));
+			__be32 gid = htonl(from_kgid_munged(&init_user_ns,
+							cred->fsgid));
+
+			fput(file);
+			if (nla_put_be32(skb, NFQA_UID, uid) ||
+			    nla_put_be32(skb, NFQA_GID, gid))
+				goto nla_put_failure;
+		}
 	}
 	return 0;
 
Signature · 1 line
2.47.3
#5 lore raw

PATCH v2 5/5 netfilter: nf_log_syslog: pin file with get_file_active before reading f_cred

In reply to Tristan Madani

From: Tristan Madani <[email protected]>

nf_log_dump_sk_uid_gid() reads sk->sk_socket->file->f_cred without holding a reference to the file. Since the file slab uses SLAB_TYPESAFE_BY_RCU, a concurrent close() may free and recycle the file object, causing stale credential data to be read through f_cred.

Pin the file with get_file_active() before accessing its credentials and release with fput() afterwards.

Found by manual code inspection; not triggered at runtime.

Fixes 0ede61d8589c ("file: convert to SLAB_TYPESAFE_BY_RCU")
---
 net/netfilter/nf_log_syslog.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
net/netfilter/nf_log_syslog.c +14 -14
index c3fd398ffcd77..dd15409100eba 100644
@@ -6,6 +6,7 @@
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/kernel.h>
+#include <linux/fs.h>
 #include <linux/module.h>
 #include <linux/spinlock.h>
 #include <linux/skbuff.h>
@@ -168,25 +169,24 @@ static struct nf_logger nf_arp_logger __read_mostly = {
 static void nf_log_dump_sk_uid_gid(struct net *net, struct nf_log_buf *m,
 				   struct sock *sk)
 {
-	const struct socket *sock;
-	const struct file *file;
+	struct socket *sock;
+	struct file *file;
 
 	if (!sk || !sk_fullsock(sk) || !net_eq(net, sock_net(sk)))
 		return;
 
-	/* 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.
-	 */
 	sock = READ_ONCE(sk->sk_socket);
-	file = sock ? READ_ONCE(sock->file) : NULL;
-	if (file) {
-		const struct cred *cred = file->f_cred;
-
-		nf_log_buf_add(m, "UID=%u GID=%u ",
-			       from_kuid_munged(&init_user_ns, cred->fsuid),
-			       from_kgid_munged(&init_user_ns, cred->fsgid));
+	if (sock) {
+		file = get_file_active(&sock->file);
+		if (file) {
+			uid_t uid = from_kuid_munged(&init_user_ns,
+						    file->f_cred->fsuid);
+			gid_t gid = from_kgid_munged(&init_user_ns,
+						    file->f_cred->fsgid);
+
+			fput(file);
+			nf_log_buf_add(m, "UID=%u GID=%u ", uid, gid);
+		}
 	}
 }
 
Signature · 1 line
2.47.3