netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Cc: davem@davemloft.net, netdev@vger.kernel.org
Subject: [PATCH 11/17] netfilter: nft_meta: move sk uid/git handling to helper
Date: Mon, 30 Dec 2019 12:21:37 +0100	[thread overview]
Message-ID: <20191230112143.121708-12-pablo@netfilter.org> (raw)
In-Reply-To: <20191230112143.121708-1-pablo@netfilter.org>

From: Florian Westphal <fw@strlen.de>

Not a hot path.  Also, both have copy&paste case statements,
so use a common helper for both.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 net/netfilter/nft_meta.c | 65 +++++++++++++++++++++++++++---------------------
 1 file changed, 36 insertions(+), 29 deletions(-)

diff --git a/net/netfilter/nft_meta.c b/net/netfilter/nft_meta.c
index fe49b27dfa87..1b32440ec2e6 100644
--- a/net/netfilter/nft_meta.c
+++ b/net/netfilter/nft_meta.c
@@ -126,6 +126,41 @@ nft_meta_get_eval_pkttype_lo(const struct nft_pktinfo *pkt,
 	return true;
 }
 
+static noinline bool
+nft_meta_get_eval_skugid(enum nft_meta_keys key,
+			 u32 *dest,
+			 const struct nft_pktinfo *pkt)
+{
+	struct sock *sk = skb_to_full_sk(pkt->skb);
+	struct socket *sock;
+
+	if (!sk || !sk_fullsock(sk) || !net_eq(nft_net(pkt), sock_net(sk)))
+		return false;
+
+	read_lock_bh(&sk->sk_callback_lock);
+	sock = sk->sk_socket;
+	if (!sock || !sock->file) {
+		read_unlock_bh(&sk->sk_callback_lock);
+		return false;
+	}
+
+	switch (key) {
+	case NFT_META_SKUID:
+		*dest = from_kuid_munged(&init_user_ns,
+					 sock->file->f_cred->fsuid);
+		break;
+	case NFT_META_SKGID:
+		*dest =	from_kgid_munged(&init_user_ns,
+					 sock->file->f_cred->fsgid);
+		break;
+	default:
+		break;
+	}
+
+	read_unlock_bh(&sk->sk_callback_lock);
+	return true;
+}
+
 void nft_meta_get_eval(const struct nft_expr *expr,
 		       struct nft_regs *regs,
 		       const struct nft_pktinfo *pkt)
@@ -180,37 +215,9 @@ void nft_meta_get_eval(const struct nft_expr *expr,
 		nft_reg_store16(dest, out->type);
 		break;
 	case NFT_META_SKUID:
-		sk = skb_to_full_sk(skb);
-		if (!sk || !sk_fullsock(sk) ||
-		    !net_eq(nft_net(pkt), sock_net(sk)))
-			goto err;
-
-		read_lock_bh(&sk->sk_callback_lock);
-		if (sk->sk_socket == NULL ||
-		    sk->sk_socket->file == NULL) {
-			read_unlock_bh(&sk->sk_callback_lock);
-			goto err;
-		}
-
-		*dest =	from_kuid_munged(&init_user_ns,
-				sk->sk_socket->file->f_cred->fsuid);
-		read_unlock_bh(&sk->sk_callback_lock);
-		break;
 	case NFT_META_SKGID:
-		sk = skb_to_full_sk(skb);
-		if (!sk || !sk_fullsock(sk) ||
-		    !net_eq(nft_net(pkt), sock_net(sk)))
+		if (!nft_meta_get_eval_skugid(priv->key, dest, pkt))
 			goto err;
-
-		read_lock_bh(&sk->sk_callback_lock);
-		if (sk->sk_socket == NULL ||
-		    sk->sk_socket->file == NULL) {
-			read_unlock_bh(&sk->sk_callback_lock);
-			goto err;
-		}
-		*dest =	from_kgid_munged(&init_user_ns,
-				 sk->sk_socket->file->f_cred->fsgid);
-		read_unlock_bh(&sk->sk_callback_lock);
 		break;
 #ifdef CONFIG_IP_ROUTE_CLASSID
 	case NFT_META_RTCLASSID: {
-- 
2.11.0


  parent reply	other threads:[~2019-12-30 11:22 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-30 11:21 [PATCH 00/17] Netfilter updates for net-next Pablo Neira Ayuso
2019-12-30 11:21 ` [PATCH 01/17] netfilter: Clean up unnecessary #ifdef Pablo Neira Ayuso
2019-12-30 11:21 ` [PATCH 02/17] netfilter: Document ingress hook Pablo Neira Ayuso
2019-12-30 11:21 ` [PATCH 03/17] netfilter: nft_tunnel: no need to call htons() when dumping ports Pablo Neira Ayuso
2019-12-30 11:21 ` [PATCH 04/17] netfilter: nft_tunnel: add the missing ERSPAN_VERSION nla_policy Pablo Neira Ayuso
2019-12-30 11:21 ` [PATCH 05/17] netfilter: nft_tunnel: also dump ERSPAN_VERSION Pablo Neira Ayuso
2019-12-30 11:21 ` [PATCH 06/17] netfilter: nft_tunnel: also dump OPTS_ERSPAN/VXLAN Pablo Neira Ayuso
2019-12-30 11:21 ` [PATCH 07/17] netfilter: nft_tunnel: add the missing nla_nest_cancel() Pablo Neira Ayuso
2019-12-30 11:21 ` [PATCH 08/17] netfilter: conntrack: remove two export symbols Pablo Neira Ayuso
2019-12-30 11:21 ` [PATCH 09/17] netfilter: nft_meta: move time handling to helper Pablo Neira Ayuso
2019-12-30 11:21 ` [PATCH 10/17] netfilter: nft_meta: move pkttype " Pablo Neira Ayuso
2019-12-30 11:21 ` Pablo Neira Ayuso [this message]
2019-12-30 11:21 ` [PATCH 12/17] netfilter: nft_meta: move cgroup " Pablo Neira Ayuso
2019-12-30 11:21 ` [PATCH 13/17] netfilter: nft_meta: move interface kind " Pablo Neira Ayuso
2019-12-30 11:21 ` [PATCH 14/17] netfilter: nft_meta: move all interface related keys " Pablo Neira Ayuso
2019-12-30 11:21 ` [PATCH 15/17] netfilter: nft_meta: place prandom handling in a helper Pablo Neira Ayuso
2019-12-30 11:21 ` [PATCH 16/17] netfilter: nft_meta: place rtclassid " Pablo Neira Ayuso
2019-12-30 11:21 ` [PATCH 17/17] netfilter: nft_meta: add support for slave device ifindex matching Pablo Neira Ayuso
2019-12-30 22:29 ` [PATCH 00/17] Netfilter updates for net-next David Miller

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=20191230112143.121708-12-pablo@netfilter.org \
    --to=pablo@netfilter.org \
    --cc=davem@davemloft.net \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).