Skip to contentSkip to conversation

net: inet: diag: expose the socket mark to privileged processes.

PATCH Applied Lorenzo Colitti 3 participants 7 messages Sep 7, 2016 – Sep 14, 2016

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

PATCH v3 1/1 net: inet: diag: expose the socket mark to privileged processes.

This adds the capability for a process that has CAP_NET_ADMIN on a socket to see the socket mark in socket dumps.

Commit a52e95abf772 ("net: diag: allow socket bytecode filters to match socket marks") recently gave privileged processes the ability to filter socket dumps based on mark. This patch is
complementary: it ensures that the mark is also passed to userspace in the socket's netlink attributes. It is useful for tools like ss which display information about sockets.

Tested: https://android-review.googlesource.com/270210

---
 include/linux/inet_diag.h      |  4 ++--
 include/uapi/linux/inet_diag.h |  1 +
 net/ipv4/inet_diag.c           | 49 ++++++++++++++++++++++++++++--------------
 net/ipv4/udp_diag.c            | 10 +++++----
 net/sctp/sctp_diag.c           | 20 +++++++++++------
 5 files changed, 56 insertions(+), 28 deletions(-)
include/linux/inet_diag.h +2 -2
index feb04ea..65da430 100644
@@ -37,7 +37,7 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
 		      struct sk_buff *skb, const struct inet_diag_req_v2 *req,
 		      struct user_namespace *user_ns,
 		      u32 pid, u32 seq, u16 nlmsg_flags,
-		      const struct nlmsghdr *unlh);
+		      const struct nlmsghdr *unlh, bool net_admin);
 void inet_diag_dump_icsk(struct inet_hashinfo *h, struct sk_buff *skb,
 			 struct netlink_callback *cb,
 			 const struct inet_diag_req_v2 *r,
@@ -56,7 +56,7 @@ void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk);
 
 int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
 			     struct inet_diag_msg *r, int ext,
-			     struct user_namespace *user_ns);
+			     struct user_namespace *user_ns, bool net_admin);
 
 extern int  inet_diag_register(const struct inet_diag_handler *handler);
 extern void inet_diag_unregister(const struct inet_diag_handler *handler);
include/uapi/linux/inet_diag.h +1 -0
index 5581206..b5c366f 100644
@@ -123,6 +123,7 @@ enum {
 	INET_DIAG_LOCALS,
 	INET_DIAG_PEERS,
 	INET_DIAG_PAD,
+	INET_DIAG_MARK,
 	__INET_DIAG_MAX,
 };
 
net/ipv4/inet_diag.c +33 -16

155 lines not on this page · raw message

net/ipv4/udp_diag.c +6 -4
index 58b79c0..9a89c10 100644
@@ -20,7 +20,7 @@
 static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
 			struct netlink_callback *cb,
 			const struct inet_diag_req_v2 *req,
-			struct nlattr *bc)
+			struct nlattr *bc, bool net_admin)
 {
 	if (!inet_diag_bc_sk(bc, sk))
 		return 0;
@@ -28,7 +28,7 @@ static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
 	return inet_sk_diag_fill(sk, NULL, skb, req,
 			sk_user_ns(NETLINK_CB(cb->skb).sk),
 			NETLINK_CB(cb->skb).portid,
-			cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
+			cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh, net_admin);
 }
 
 static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
@@ -76,7 +76,8 @@ static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
 	err = inet_sk_diag_fill(sk, NULL, rep, req,
 			   sk_user_ns(NETLINK_CB(in_skb).sk),
 			   NETLINK_CB(in_skb).portid,
-			   nlh->nlmsg_seq, 0, nlh);
+			   nlh->nlmsg_seq, 0, nlh,
+			   netlink_net_capable(in_skb, CAP_NET_ADMIN));
 	if (err < 0) {
 		WARN_ON(err == -EMSGSIZE);
 		kfree_skb(rep);
@@ -97,6 +98,7 @@ static void udp_dump(struct udp_table *table, struct sk_buff *skb,
 		     struct netlink_callback *cb,
 		     const struct inet_diag_req_v2 *r, struct nlattr *bc)
 {
+	bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
 	struct net *net = sock_net(skb->sk);
 	int num, s_num, slot, s_slot;
 
@@ -132,7 +134,7 @@ static void udp_dump(struct udp_table *table, struct sk_buff *skb,
 			    r->id.idiag_dport)
 				goto next;
 
-			if (sk_diag_dump(sk, skb, cb, r, bc) < 0) {
+			if (sk_diag_dump(sk, skb, cb, r, bc, net_admin) < 0) {
 				spin_unlock_bh(&hslot->lock);
 				goto done;
 			}
net/sctp/sctp_diag.c +14 -6
index f3508aa..807158e3 100644
@@ -106,7 +106,8 @@ static int inet_sctp_diag_fill(struct sock *sk, struct sctp_association *asoc,
 			       const struct inet_diag_req_v2 *req,
 			       struct user_namespace *user_ns,
 			       int portid, u32 seq, u16 nlmsg_flags,
-			       const struct nlmsghdr *unlh)
+			       const struct nlmsghdr *unlh,
+			       bool net_admin)
 {
 	struct sctp_endpoint *ep = sctp_sk(sk)->ep;
 	struct list_head *addr_list;
@@ -133,7 +134,7 @@ static int inet_sctp_diag_fill(struct sock *sk, struct sctp_association *asoc,
 		r->idiag_retrans = 0;
 	}
 
-	if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns))
+	if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns, net_admin))
 		goto errout;
 
 	if (ext & (1 << (INET_DIAG_SKMEMINFO - 1))) {
@@ -203,6 +204,7 @@ struct sctp_comm_param {
 	struct netlink_callback *cb;
 	const struct inet_diag_req_v2 *r;
 	const struct nlmsghdr *nlh;
+	bool net_admin;
 };
 
 static size_t inet_assoc_attr_size(struct sctp_association *asoc)
@@ -219,6 +221,7 @@ static size_t inet_assoc_attr_size(struct sctp_association *asoc)
 		+ nla_total_size(1) /* INET_DIAG_SHUTDOWN */
 		+ nla_total_size(1) /* INET_DIAG_TOS */
 		+ nla_total_size(1) /* INET_DIAG_TCLASS */
+		+ nla_total_size(4) /* INET_DIAG_MARK */
 		+ nla_total_size(addrlen * asoc->peer.transport_count)
 		+ nla_total_size(addrlen * addrcnt)
 		+ nla_total_size(sizeof(struct inet_diag_meminfo))
@@ -256,7 +259,8 @@ static int sctp_tsp_dump_one(struct sctp_transport *tsp, void *p)
 	err = inet_sctp_diag_fill(sk, assoc, rep, req,
 				  sk_user_ns(NETLINK_CB(in_skb).sk),
 				  NETLINK_CB(in_skb).portid,
-				  nlh->nlmsg_seq, 0, nlh);
+				  nlh->nlmsg_seq, 0, nlh,
+				  commp->net_admin);
 	release_sock(sk);
 	if (err < 0) {
 		WARN_ON(err == -EMSGSIZE);
@@ -310,7 +314,8 @@ static int sctp_tsp_dump(struct sctp_transport *tsp, void *p)
 					sk_user_ns(NETLINK_CB(cb->skb).sk),
 					NETLINK_CB(cb->skb).portid,
 					cb->nlh->nlmsg_seq,
-					NLM_F_MULTI, cb->nlh) < 0) {
+					NLM_F_MULTI, cb->nlh,
+					commp->net_admin) < 0) {
 			cb->args[3] = 1;
 			err = 2;
 			goto release;
@@ -320,7 +325,8 @@ static int sctp_tsp_dump(struct sctp_transport *tsp, void *p)
 		if (inet_sctp_diag_fill(sk, assoc, skb, r,
 					sk_user_ns(NETLINK_CB(cb->skb).sk),
 					NETLINK_CB(cb->skb).portid,
-					cb->nlh->nlmsg_seq, 0, cb->nlh) < 0) {
+					cb->nlh->nlmsg_seq, 0, cb->nlh,
+					commp->net_admin) < 0) {
 			err = 2;
 			goto release;
 		}
@@ -375,7 +381,7 @@ static int sctp_ep_dump(struct sctp_endpoint *ep, void *p)
 				sk_user_ns(NETLINK_CB(cb->skb).sk),
 				NETLINK_CB(cb->skb).portid,
 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
-				cb->nlh) < 0) {
+				cb->nlh, commp->net_admin) < 0) {
 		err = 2;
 		goto out;
 	}
@@ -412,6 +418,7 @@ static int sctp_diag_dump_one(struct sk_buff *in_skb,
 		.skb = in_skb,
 		.r = req,
 		.nlh = nlh,
+		.net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN),
 	};
 
 	if (req->sdiag_family == AF_INET) {
@@ -447,6 +454,7 @@ static void sctp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
 		.skb = skb,
 		.cb = cb,
 		.r = r,
+		.net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN),
 	};
 
 	/* eps hashtable dumps
Signature · 1 line
2.8.0.rc3.226.g39d4020
maintainer Applied
#1 lore raw

From: Lorenzo Colitti <[email protected]>
Date: Thu, 8 Sep 2016 00:42:25 +0900

Quoted 10 lines

This adds the capability for a process that has CAP_NET_ADMIN on a socket to see the socket mark in socket dumps.

Commit a52e95abf772 ("net: diag: allow socket bytecode filters to match socket marks") recently gave privileged processes the ability to filter socket dumps based on mark. This patch is
complementary: it ensures that the mark is also passed to userspace in the socket's netlink attributes. It is useful for tools like ss which display information about sockets.

Quoted 2 lines

Tested: https://android-review.googlesource.com/270210

Applied, but the argument list of inet_sk_diag_fill is starting to get out of control.

#2 lore raw
Quoted 5 lines · David Miller
Quoted 2 lines

This adds the capability for a process that has CAP_NET_ADMIN on a socket to see the socket mark in socket dumps.

Applied, but the argument list of inet_sk_diag_fill is starting to get out of control.

I think a lot of the parameters it takes are just a couple of pointer lookups away from in_skb. I assumed it did not take in_skb directly for performance reasons: this way the calling functions can calculate many of these arguments just once per dump instead of once per socket. But thinking about it some more the cost of those pointer lookups is negligible compared to the cost of iterating over the hashtables, marshalling the attributes, etc.

I'll see if I can send something out to pass in in_skb instead.

maintainer
#3 lore raw

From: Lorenzo Colitti <[email protected]>
Date: Fri, 9 Sep 2016 09:48:27 +0900

Quoted 15 lines · David Miller
Quoted 5 lines
Quoted 2 lines

This adds the capability for a process that has CAP_NET_ADMIN on a socket to see the socket mark in socket dumps.

Applied, but the argument list of inet_sk_diag_fill is starting to get out of control.

I think a lot of the parameters it takes are just a couple of pointer lookups away from in_skb. I assumed it did not take in_skb directly for performance reasons: this way the calling functions can calculate many of these arguments just once per dump instead of once per socket. But thinking about it some more the cost of those pointer lookups is negligible compared to the cost of iterating over the hashtables, marshalling the attributes, etc.

Quoted 1 line

I'll see if I can send something out to pass in in_skb instead.

The other option is to have a "struct foo_info" object on the callers stack that holds all of these values, then pass a pointer to the foo_info to inet_sk_diag_fill.

#4 lore raw
Quoted 6 lines · David Miller
Quoted 2 lines

I'll see if I can send something out to pass in in_skb instead.

The other option is to have a "struct foo_info" object on the callers stack that holds all of these values, then pass a pointer to the foo_info to inet_sk_diag_fill.

RFC patch sent out as http://patchwork.ozlabs.org/patch/667892/ . This achieves a fair bit of simplification with no or negligible performance impact, because there was a lot of redundancy in the parameters that were passed in.

Further simplification could be achieved by removing the "bool net_admin" parameter. I didn't do this in this patch because I don't know the performance impact of calling netlink_ns_capable once per socket instead of once per dump. My guess is that that's in the noise given that we're already doing lots of copying; if it is, I can send out a v2 that removes the net_admin parameter as well.

Didn't try the struct since this seemed a good starting point. It could be done later, of course.

#5 lore raw
Quoted 4 lines · Lorenzo Colitti

RFC patch sent out as http://patchwork.ozlabs.org/patch/667892/ . This achieves a fair bit of simplification with no or negligible performance impact, because there was a lot of redundancy in the parameters that were passed in.

David, any thoughts on that patch? I submitted it as RFC because I wasn't sure what you wanted. Should I have sent it as non-RFC instead?

#6 lore raw
Quoted 9 lines · Lorenzo Colitti
Quoted 4 lines · Lorenzo Colitti

RFC patch sent out as http://patchwork.ozlabs.org/patch/667892/ . This achieves a fair bit of simplification with no or negligible performance impact, because there was a lot of redundancy in the parameters that were passed in.

David, any thoughts on that patch? I submitted it as RFC because I wasn't sure what you wanted. Should I have sent it as non-RFC instead?

I realize you meant DaveM, but this one has been accepted. It's your other 2 that are marked RFC by you and in patchwork.