netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/2] Add UID/GID info to NFQUEUE
@ 2013-12-20 16:28 valentina.giusti
  2013-12-20 16:28 ` [PATCH v4 1/2] netfilter_queue: enable UID/GID socket info retrieval valentina.giusti
  2013-12-20 16:28 ` [PATCH v4 2/2] libnetfilter_queue: add support for UID/GID socket info valentina.giusti
  0 siblings, 2 replies; 13+ messages in thread
From: valentina.giusti @ 2013-12-20 16:28 UTC (permalink / raw)
  To: netfilter-devel; +Cc: netdev, jpa, pablo, fw, daniel.wagner

From: Valentina Giusti <valentina.giusti@bmw-carit.de>

Hi,

I corrected the return value of nfqnl_put_sk_uidgid for timewait packets, as
Florian Westphal recommended.

--
BR,
Val

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v4 1/2] netfilter_queue: enable UID/GID socket info retrieval
  2013-12-20 16:28 [PATCH v4 0/2] Add UID/GID info to NFQUEUE valentina.giusti
@ 2013-12-20 16:28 ` valentina.giusti
  2013-12-23 14:21   ` Pablo Neira Ayuso
  2013-12-20 16:28 ` [PATCH v4 2/2] libnetfilter_queue: add support for UID/GID socket info valentina.giusti
  1 sibling, 1 reply; 13+ messages in thread
From: valentina.giusti @ 2013-12-20 16:28 UTC (permalink / raw)
  To: netfilter-devel; +Cc: netdev, jpa, pablo, fw, daniel.wagner

From: Valentina Giusti <valentina.giusti@bmw-carit.de>

Thanks to commits 41063e9 (ipv4: Early TCP socket demux) and 421b388 (udp: ipv4:
Add udp early demux) it is now possible to parse UID and GID socket info
also for incoming TCP and UDP connections. Having this info available, it
is convenient to let NFQUEUE parse it in order to improve and refine the
traffic analysis in userspace.

Signed-off-by: Valentina Giusti <valentina.giusti@bmw-carit.de>
---
 include/uapi/linux/netfilter/nfnetlink_queue.h |  5 +++-
 net/netfilter/nfnetlink_queue_core.c           | 34 ++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/include/uapi/linux/netfilter/nfnetlink_queue.h b/include/uapi/linux/netfilter/nfnetlink_queue.h
index 0132bad..8dd819e 100644
--- a/include/uapi/linux/netfilter/nfnetlink_queue.h
+++ b/include/uapi/linux/netfilter/nfnetlink_queue.h
@@ -47,6 +47,8 @@ enum nfqnl_attr_type {
 	NFQA_CAP_LEN,			/* __u32 length of captured packet */
 	NFQA_SKB_INFO,			/* __u32 skb meta information */
 	NFQA_EXP,			/* nf_conntrack_netlink.h */
+	NFQA_UID,			/* __u32 sk uid */
+	NFQA_GID,			/* __u32 sk gid */
 
 	__NFQA_MAX
 };
@@ -99,7 +101,8 @@ enum nfqnl_attr_config {
 #define NFQA_CFG_F_FAIL_OPEN			(1 << 0)
 #define NFQA_CFG_F_CONNTRACK			(1 << 1)
 #define NFQA_CFG_F_GSO				(1 << 2)
-#define NFQA_CFG_F_MAX				(1 << 3)
+#define NFQA_CFG_F_UID_GID			(1 << 3)
+#define NFQA_CFG_F_MAX				(1 << 4)
 
 /* flags for NFQA_SKB_INFO */
 /* packet appears to have wrong checksums, but they are ok */
diff --git a/net/netfilter/nfnetlink_queue_core.c b/net/netfilter/nfnetlink_queue_core.c
index 21258cf..267ac36 100644
--- a/net/netfilter/nfnetlink_queue_core.c
+++ b/net/netfilter/nfnetlink_queue_core.c
@@ -297,6 +297,32 @@ nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet,
 	return flags ? nla_put_be32(nlskb, NFQA_SKB_INFO, htonl(flags)) : 0;
 }
 
+static int nfqnl_put_sk_uidgid(struct sk_buff *skb, struct sock *sk)
+{
+	const struct cred *cred;
+
+	if (sk->sk_state == TCP_TIME_WAIT)
+		return 0;
+
+	read_lock_bh(&sk->sk_callback_lock);
+	if (sk->sk_socket && sk->sk_socket->file) {
+		cred = sk->sk_socket->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;
+	}
+	read_unlock_bh(&sk->sk_callback_lock);
+
+	return 0;
+
+nla_put_failure:
+	read_unlock_bh(&sk->sk_callback_lock);
+	return -1;
+}
+
 static struct sk_buff *
 nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
 			   struct nf_queue_entry *entry,
@@ -372,6 +398,10 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
 	if (queue->flags & NFQA_CFG_F_CONNTRACK)
 		ct = nfqnl_ct_get(entskb, &size, &ctinfo);
 
+	if (queue->flags & NFQA_CFG_F_UID_GID)
+		size +=  (nla_total_size(sizeof(u_int32_t))	/* uid */
+			+ nla_total_size(sizeof(u_int32_t)));	/* gid */
+
 	skb = nfnetlink_alloc_skb(net, size, queue->peer_portid,
 				  GFP_ATOMIC);
 	if (!skb)
@@ -484,6 +514,10 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
 			goto nla_put_failure;
 	}
 
+	if ((queue->flags & NFQA_CFG_F_UID_GID) && entskb->sk)
+		if (nfqnl_put_sk_uidgid(skb, entskb->sk))
+			goto nla_put_failure;
+
 	if (ct && nfqnl_ct_put(skb, ct, ctinfo) < 0)
 		goto nla_put_failure;
 
-- 
1.8.5.1

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v4 2/2] libnetfilter_queue: add support for UID/GID socket info
  2013-12-20 16:28 [PATCH v4 0/2] Add UID/GID info to NFQUEUE valentina.giusti
  2013-12-20 16:28 ` [PATCH v4 1/2] netfilter_queue: enable UID/GID socket info retrieval valentina.giusti
@ 2013-12-20 16:28 ` valentina.giusti
  2013-12-21 11:38   ` Pablo Neira Ayuso
  2013-12-21 12:11   ` [PATCH v4 2/2] libnetfilter_queue: add support for UID/GID socket info Pablo Neira Ayuso
  1 sibling, 2 replies; 13+ messages in thread
From: valentina.giusti @ 2013-12-20 16:28 UTC (permalink / raw)
  To: netfilter-devel; +Cc: netdev, jpa, pablo, fw, daniel.wagner

From: Valentina Giusti <Valentina.Giusti@bmw-carit.de>

With this patch libnetfilter_queue is able to parse UID/GID
socket information.

Signed-off-by: Valentina Giusti <Valentina.Giusti@bmw-carit.de>
---
 include/libnetfilter_queue/libnetfilter_queue.h    |  4 ++++
 include/libnetfilter_queue/linux_nfnetlink_queue.h |  5 +++-
 include/linux/netfilter/nfnetlink_queue.h          |  6 ++++-
 src/libnetfilter_queue.c                           | 28 ++++++++++++++++++++++
 src/nlmsg.c                                        |  2 ++
 5 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/include/libnetfilter_queue/libnetfilter_queue.h b/include/libnetfilter_queue/libnetfilter_queue.h
index b9f16e2..b4e2679 100644
--- a/include/libnetfilter_queue/libnetfilter_queue.h
+++ b/include/libnetfilter_queue/libnetfilter_queue.h
@@ -103,6 +103,8 @@ extern u_int32_t nfq_get_indev(struct nfq_data *nfad);
 extern u_int32_t nfq_get_physindev(struct nfq_data *nfad);
 extern u_int32_t nfq_get_outdev(struct nfq_data *nfad);
 extern u_int32_t nfq_get_physoutdev(struct nfq_data *nfad);
+extern u_int32_t nfq_get_uid(struct nfq_data *nfad);
+extern u_int32_t nfq_get_gid(struct nfq_data *nfad);
 
 extern int nfq_get_indev_name(struct nlif_handle *nlif_handle,
 			      struct nfq_data *nfad, char *name);
@@ -125,6 +127,8 @@ enum {
 	NFQ_XML_PHYSDEV	= (1 << 3),
 	NFQ_XML_PAYLOAD	= (1 << 4),
 	NFQ_XML_TIME	= (1 << 5),
+	NFQ_XML_UID	= (1 << 6),
+	NFQ_XML_GID	= (1 << 7),
 	NFQ_XML_ALL	= ~0U,
 };
 
diff --git a/include/libnetfilter_queue/linux_nfnetlink_queue.h b/include/libnetfilter_queue/linux_nfnetlink_queue.h
index 81a485b..884ab0e 100644
--- a/include/libnetfilter_queue/linux_nfnetlink_queue.h
+++ b/include/libnetfilter_queue/linux_nfnetlink_queue.h
@@ -50,6 +50,8 @@ enum nfqnl_attr_type {
 	NFQA_CAP_LEN,                   /* __u32 length of captured packet */
 	NFQA_SKB_INFO,                  /* __u32 skb meta information */
 
+	NFQA_UID,			/* __u32 sk uid */
+	NFQA_GID,			/* __u32 sk gid */
 	__NFQA_MAX
 };
 #define NFQA_MAX (__NFQA_MAX - 1)
@@ -101,7 +103,8 @@ enum nfqnl_attr_config {
 #define NFQA_CFG_F_FAIL_OPEN		(1 << 0)
 #define NFQA_CFG_F_CONNTRACK		(1 << 1)
 #define NFQA_CFG_F_GSO			(1 << 2)
-#define NFQA_CFG_F_MAX			(1 << 3)
+#define NFQA_CFG_F_UID_GID		(1 << 3)
+#define NFQA_CFG_F_MAX			(1 << 4)
 
 /* flags for NFQA_SKB_INFO */
 /* packet appears to have wrong checksums, but they are ok */
diff --git a/include/linux/netfilter/nfnetlink_queue.h b/include/linux/netfilter/nfnetlink_queue.h
index a2308ae..22f5d45 100644
--- a/include/linux/netfilter/nfnetlink_queue.h
+++ b/include/linux/netfilter/nfnetlink_queue.h
@@ -46,6 +46,9 @@ enum nfqnl_attr_type {
 	NFQA_CT_INFO,			/* enum ip_conntrack_info */
 	NFQA_CAP_LEN,			/* __u32 length of captured packet */
 	NFQA_SKB_INFO,			/* __u32 skb meta information */
+	NFQA_EXP,			/* nf_conntrack_netlink.h */
+	NFQA_UID,			/* __u32 sk uid */
+	NFQA_GID,			/* __u32 sk gid */
 
 	__NFQA_MAX
 };
@@ -98,7 +101,8 @@ enum nfqnl_attr_config {
 #define NFQA_CFG_F_FAIL_OPEN			(1 << 0)
 #define NFQA_CFG_F_CONNTRACK			(1 << 1)
 #define NFQA_CFG_F_GSO				(1 << 2)
-#define NFQA_CFG_F_MAX				(1 << 3)
+#define NFQA_CFG_F_UID_GID			(1 << 3)
+#define NFQA_CFG_F_MAX				(1 << 4)
 
 /* flags for NFQA_SKB_INFO */
 /* packet appears to have wrong checksums, but they are ok */
diff --git a/src/libnetfilter_queue.c b/src/libnetfilter_queue.c
index fa8efe7..bf1266b 100644
--- a/src/libnetfilter_queue.c
+++ b/src/libnetfilter_queue.c
@@ -668,6 +668,9 @@ EXPORT_SYMBOL(nfq_set_mode);
  *  if this bit is set, the layer 3/4 checksums of the packet appear incorrect,
  *  but are not (because they will be corrected later by the kernel).
  *
+ *  - NFQA_CFG_F_UID_GID: the kernel will dump UID and GID of the socket to
+ *  which each packet belongs.
+ *
  * Here's a little code snippet to show how to use this API:
  * \verbatim
 	uint32_t flags = NFQA_CFG_F_FAIL_OPEN;
@@ -1180,6 +1183,18 @@ struct nfqnl_msg_packet_hw *nfq_get_packet_hw(struct nfq_data *nfad)
 }
 EXPORT_SYMBOL(nfq_get_packet_hw);
 
+uint32_t nfq_get_uid(struct nfq_data *nfad)
+{
+	return ntohl(nfnl_get_data(nfad->data, NFQA_UID, u_int32_t));
+}
+EXPORT_SYMBOL(nfq_get_uid);
+
+uint32_t nfq_get_gid(struct nfq_data *nfad)
+{
+	return ntohl(nfnl_get_data(nfad->data, NFQA_GID, u_int32_t));
+}
+EXPORT_SYMBOL(nfq_get_gid);
+
 /**
  * nfq_get_payload - get payload 
  * \param nfad Netlink packet data handle passed to callback function
@@ -1250,6 +1265,7 @@ int nfq_snprintf_xml(char *buf, size_t rem, struct nfq_data *tb, int flags)
 	struct nfqnl_msg_packet_hdr *ph;
 	struct nfqnl_msg_packet_hw *hwph;
 	u_int32_t mark, ifi;
+	u_int32_t uid, gid;
 	int size, offset = 0, len = 0, ret;
 	unsigned char *data;
 
@@ -1365,6 +1381,18 @@ int nfq_snprintf_xml(char *buf, size_t rem, struct nfq_data *tb, int flags)
 		SNPRINTF_FAILURE(size, rem, offset, len);
 	}
 
+	uid = nfq_get_uid(tb);
+	if (uid && (flags & NFQ_XML_UID)) {
+		size = snprintf(buf + offset, rem, "<uid>%u</uid>", uid);
+		SNPRINTF_FAILURE(size, rem, offset, len);
+	}
+
+	gid = nfq_get_gid(tb);
+	if (gid && (flags & NFQ_XML_GID)) {
+		size = snprintf(buf + offset, rem, "<gid>%u</gid>", gid);
+		SNPRINTF_FAILURE(size, rem, offset, len);
+	}
+
 	ret = nfq_get_payload(tb, &data);
 	if (ret >= 0 && (flags & NFQ_XML_PAYLOAD)) {
 		int i;
diff --git a/src/nlmsg.c b/src/nlmsg.c
index e7a30e0..81e170e 100644
--- a/src/nlmsg.c
+++ b/src/nlmsg.c
@@ -134,6 +134,8 @@ static int nfq_pkt_parse_attr_cb(const struct nlattr *attr, void *data)
 	case NFQA_IFINDEX_PHYSOUTDEV:
 	case NFQA_CAP_LEN:
 	case NFQA_SKB_INFO:
+	case NFQA_UID:
+	case NFQA_GID:
 		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
 			return MNL_CB_ERROR;
 		break;
-- 
1.8.5.1

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH v4 2/2] libnetfilter_queue: add support for UID/GID socket info
  2013-12-20 16:28 ` [PATCH v4 2/2] libnetfilter_queue: add support for UID/GID socket info valentina.giusti
@ 2013-12-21 11:38   ` Pablo Neira Ayuso
  2014-01-07 13:30     ` [PATCH 0/2] " valentina.giusti
  2013-12-21 12:11   ` [PATCH v4 2/2] libnetfilter_queue: add support for UID/GID socket info Pablo Neira Ayuso
  1 sibling, 1 reply; 13+ messages in thread
From: Pablo Neira Ayuso @ 2013-12-21 11:38 UTC (permalink / raw)
  To: valentina.giusti; +Cc: netfilter-devel, netdev, jpa, fw, daniel.wagner

Hi,

On Fri, Dec 20, 2013 at 05:28:54PM +0100, valentina.giusti@bmw-carit.de wrote:
[...]
> diff --git a/include/libnetfilter_queue/linux_nfnetlink_queue.h b/include/libnetfilter_queue/linux_nfnetlink_queue.h
> index 81a485b..884ab0e 100644
> --- a/include/libnetfilter_queue/linux_nfnetlink_queue.h
> +++ b/include/libnetfilter_queue/linux_nfnetlink_queue.h
> @@ -50,6 +50,8 @@ enum nfqnl_attr_type {
>  	NFQA_CAP_LEN,                   /* __u32 length of captured packet */
>  	NFQA_SKB_INFO,                  /* __u32 skb meta information */
>  
> +	NFQA_UID,			/* __u32 sk uid */
> +	NFQA_GID,			/* __u32 sk gid */

This update is wrong. See below the reason why.

>  	__NFQA_MAX
>  };
>  #define NFQA_MAX (__NFQA_MAX - 1)
> @@ -101,7 +103,8 @@ enum nfqnl_attr_config {
>  #define NFQA_CFG_F_FAIL_OPEN		(1 << 0)
>  #define NFQA_CFG_F_CONNTRACK		(1 << 1)
>  #define NFQA_CFG_F_GSO			(1 << 2)
> -#define NFQA_CFG_F_MAX			(1 << 3)
> +#define NFQA_CFG_F_UID_GID		(1 << 3)
> +#define NFQA_CFG_F_MAX			(1 << 4)
>  
>  /* flags for NFQA_SKB_INFO */
>  /* packet appears to have wrong checksums, but they are ok */
> diff --git a/include/linux/netfilter/nfnetlink_queue.h b/include/linux/netfilter/nfnetlink_queue.h
> index a2308ae..22f5d45 100644
> --- a/include/linux/netfilter/nfnetlink_queue.h
> +++ b/include/linux/netfilter/nfnetlink_queue.h
> @@ -46,6 +46,9 @@ enum nfqnl_attr_type {
>  	NFQA_CT_INFO,			/* enum ip_conntrack_info */
>  	NFQA_CAP_LEN,			/* __u32 length of captured packet */
>  	NFQA_SKB_INFO,			/* __u32 skb meta information */
> +	NFQA_EXP,			/* nf_conntrack_netlink.h */
> +	NFQA_UID,			/* __u32 sk uid */
> +	NFQA_GID,			/* __u32 sk gid */

You have manually updated libnetfilter_queue/linux_nfnetlink_queue.h,
but you forgot to include NFQA_EXP. The result is that your
nfq_get_uid() returns the NFQA_EXP attribute and nfq_get_gid() returns
the NFQA_UID attribute.

You should have noticed it with a simple run of utils/nfqnl_test run
and a couple of printf to test it. I'm afraid that you're not giving
sufficient testing to your patches.

Fix it and resubmit, thanks.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v4 2/2] libnetfilter_queue: add support for UID/GID socket info
  2013-12-20 16:28 ` [PATCH v4 2/2] libnetfilter_queue: add support for UID/GID socket info valentina.giusti
  2013-12-21 11:38   ` Pablo Neira Ayuso
@ 2013-12-21 12:11   ` Pablo Neira Ayuso
  1 sibling, 0 replies; 13+ messages in thread
From: Pablo Neira Ayuso @ 2013-12-21 12:11 UTC (permalink / raw)
  To: valentina.giusti; +Cc: netfilter-devel, netdev, jpa, fw, daniel.wagner

On Fri, Dec 20, 2013 at 05:28:54PM +0100, valentina.giusti@bmw-carit.de wrote:
> @@ -1180,6 +1183,18 @@ struct nfqnl_msg_packet_hw *nfq_get_packet_hw(struct nfq_data *nfad)
>  }
>  EXPORT_SYMBOL(nfq_get_packet_hw);
>  
> +uint32_t nfq_get_uid(struct nfq_data *nfad)
> +{
> +	return ntohl(nfnl_get_data(nfad->data, NFQA_UID, u_int32_t));
> +}
> +EXPORT_SYMBOL(nfq_get_uid);

Given that we'll have cases in which uid would not be present, I think
that the following interface would be better:

        int nfq_get_uid(struct nfq_data *nfad, uint32_t *uid)

The function returns 1 if uid has been set (ie. it's available so you
can access it), otherwise it returns 0.

Same thing for _get_gid().

Thanks.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v4 1/2] netfilter_queue: enable UID/GID socket info retrieval
  2013-12-20 16:28 ` [PATCH v4 1/2] netfilter_queue: enable UID/GID socket info retrieval valentina.giusti
@ 2013-12-23 14:21   ` Pablo Neira Ayuso
  2014-01-07  8:11     ` Valentina Giusti
  0 siblings, 1 reply; 13+ messages in thread
From: Pablo Neira Ayuso @ 2013-12-23 14:21 UTC (permalink / raw)
  To: valentina.giusti; +Cc: netfilter-devel, netdev, jpa, fw, daniel.wagner

On Fri, Dec 20, 2013 at 05:28:53PM +0100, valentina.giusti@bmw-carit.de wrote:
> From: Valentina Giusti <valentina.giusti@bmw-carit.de>
> 
> Thanks to commits 41063e9 (ipv4: Early TCP socket demux) and 421b388 (udp: ipv4:
> Add udp early demux) it is now possible to parse UID and GID socket info
> also for incoming TCP and UDP connections. Having this info available, it
> is convenient to let NFQUEUE parse it in order to improve and refine the
> traffic analysis in userspace.

Applied the kernel patch, wait for a new patch for libnetfilter_queue. Thanks.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v4 1/2] netfilter_queue: enable UID/GID socket info retrieval
  2013-12-23 14:21   ` Pablo Neira Ayuso
@ 2014-01-07  8:11     ` Valentina Giusti
  0 siblings, 0 replies; 13+ messages in thread
From: Valentina Giusti @ 2014-01-07  8:11 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel, netdev, jpa, fw, daniel.wagner

On 12/23/2013 03:21 PM, Pablo Neira Ayuso wrote:
> On Fri, Dec 20, 2013 at 05:28:53PM +0100, valentina.giusti@bmw-carit.de wrote:
>> From: Valentina Giusti <valentina.giusti@bmw-carit.de>
>>
>> Thanks to commits 41063e9 (ipv4: Early TCP socket demux) and 421b388 (udp: ipv4:
>> Add udp early demux) it is now possible to parse UID and GID socket info
>> also for incoming TCP and UDP connections. Having this info available, it
>> is convenient to let NFQUEUE parse it in order to improve and refine the
>> traffic analysis in userspace.
>
> Applied the kernel patch, wait for a new patch for libnetfilter_queue. Thanks.
>

Hi Pablo,

thanks for applying the kernel patch and also for the comments on the 
userspace patch, I'll send a new patch that addresses them ASAP.
Sorry for the delay, I was away during the past 2 weeks.

BR,
- Val


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 0/2] libnetfilter_queue: add support for UID/GID socket info
  2013-12-21 11:38   ` Pablo Neira Ayuso
@ 2014-01-07 13:30     ` valentina.giusti
  2014-01-07 13:30       ` [PATCH 1/2] src: " valentina.giusti
  2014-01-07 13:30       ` [PATCH 2/2] utils: add test for nfq_get_uid and nfq_get_gid valentina.giusti
  0 siblings, 2 replies; 13+ messages in thread
From: valentina.giusti @ 2014-01-07 13:30 UTC (permalink / raw)
  To: pablo, netfilter-devel; +Cc: netdev, fw, daniel.wagner

From: Valentina Giusti <Valentina.Giusti@bmw-carit.de>

This patchset contains the following changes:
* re-implemented nfq_get_uid and nfq_get_gid as suggested by Pablo Neira Ayuso
* corrected enum nfqnl_attr_type in linux_nfnetlink_queue.h
* added a test for the aforementioned new APIs in utils/nfqnl_test.c

Best Regards,

Valentina Giusti (2):
  src: add support for UID/GID socket info
  utils: add test for nfq_get_uid and nfq_get_gid

 include/libnetfilter_queue/libnetfilter_queue.h    |  4 ++
 include/libnetfilter_queue/linux_nfnetlink_queue.h |  6 ++-
 include/linux/netfilter/nfnetlink_queue.h          |  6 ++-
 src/libnetfilter_queue.c                           | 60 ++++++++++++++++++++++
 src/nlmsg.c                                        |  2 +
 utils/nfqnl_test.c                                 | 14 ++++-
 6 files changed, 89 insertions(+), 3 deletions(-)

-- 
1.8.5.1

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 1/2] src: add support for UID/GID socket info
  2014-01-07 13:30     ` [PATCH 0/2] " valentina.giusti
@ 2014-01-07 13:30       ` valentina.giusti
  2014-01-07 23:42         ` Pablo Neira Ayuso
  2014-01-07 13:30       ` [PATCH 2/2] utils: add test for nfq_get_uid and nfq_get_gid valentina.giusti
  1 sibling, 1 reply; 13+ messages in thread
From: valentina.giusti @ 2014-01-07 13:30 UTC (permalink / raw)
  To: pablo, netfilter-devel; +Cc: netdev, fw, daniel.wagner

From: Valentina Giusti <Valentina.Giusti@bmw-carit.de>

With this patch libnetfilter_queue is able to parse UID/GID
socket information.

Signed-off-by: Valentina Giusti <Valentina.Giusti@bmw-carit.de>
---
 include/libnetfilter_queue/libnetfilter_queue.h    |  4 ++
 include/libnetfilter_queue/linux_nfnetlink_queue.h |  6 ++-
 include/linux/netfilter/nfnetlink_queue.h          |  6 ++-
 src/libnetfilter_queue.c                           | 46 ++++++++++++++++++++++
 src/nlmsg.c                                        |  2 +
 5 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/include/libnetfilter_queue/libnetfilter_queue.h b/include/libnetfilter_queue/libnetfilter_queue.h
index b9f16e2..122cd10 100644
--- a/include/libnetfilter_queue/libnetfilter_queue.h
+++ b/include/libnetfilter_queue/libnetfilter_queue.h
@@ -103,6 +103,8 @@ extern u_int32_t nfq_get_indev(struct nfq_data *nfad);
 extern u_int32_t nfq_get_physindev(struct nfq_data *nfad);
 extern u_int32_t nfq_get_outdev(struct nfq_data *nfad);
 extern u_int32_t nfq_get_physoutdev(struct nfq_data *nfad);
+extern int nfq_get_uid(struct nfq_data *nfad, u_int32_t *uid);
+extern int nfq_get_gid(struct nfq_data *nfad, u_int32_t *gid);
 
 extern int nfq_get_indev_name(struct nlif_handle *nlif_handle,
 			      struct nfq_data *nfad, char *name);
@@ -125,6 +127,8 @@ enum {
 	NFQ_XML_PHYSDEV	= (1 << 3),
 	NFQ_XML_PAYLOAD	= (1 << 4),
 	NFQ_XML_TIME	= (1 << 5),
+	NFQ_XML_UID	= (1 << 6),
+	NFQ_XML_GID	= (1 << 7),
 	NFQ_XML_ALL	= ~0U,
 };
 
diff --git a/include/libnetfilter_queue/linux_nfnetlink_queue.h b/include/libnetfilter_queue/linux_nfnetlink_queue.h
index 81a485b..f732201 100644
--- a/include/libnetfilter_queue/linux_nfnetlink_queue.h
+++ b/include/libnetfilter_queue/linux_nfnetlink_queue.h
@@ -49,6 +49,9 @@ enum nfqnl_attr_type {
 	NFQA_CT_INFO,			/* enum ip_conntrack_info */
 	NFQA_CAP_LEN,                   /* __u32 length of captured packet */
 	NFQA_SKB_INFO,                  /* __u32 skb meta information */
+	NFQA_EXP,			/* nf_conntrack_netlink.h */
+	NFQA_UID,			/* __u32 sk uid */
+	NFQA_GID,			/* __u32 sk gid */
 
 	__NFQA_MAX
 };
@@ -101,7 +104,8 @@ enum nfqnl_attr_config {
 #define NFQA_CFG_F_FAIL_OPEN		(1 << 0)
 #define NFQA_CFG_F_CONNTRACK		(1 << 1)
 #define NFQA_CFG_F_GSO			(1 << 2)
-#define NFQA_CFG_F_MAX			(1 << 3)
+#define NFQA_CFG_F_UID_GID		(1 << 3)
+#define NFQA_CFG_F_MAX			(1 << 4)
 
 /* flags for NFQA_SKB_INFO */
 /* packet appears to have wrong checksums, but they are ok */
diff --git a/include/linux/netfilter/nfnetlink_queue.h b/include/linux/netfilter/nfnetlink_queue.h
index a2308ae..22f5d45 100644
--- a/include/linux/netfilter/nfnetlink_queue.h
+++ b/include/linux/netfilter/nfnetlink_queue.h
@@ -46,6 +46,9 @@ enum nfqnl_attr_type {
 	NFQA_CT_INFO,			/* enum ip_conntrack_info */
 	NFQA_CAP_LEN,			/* __u32 length of captured packet */
 	NFQA_SKB_INFO,			/* __u32 skb meta information */
+	NFQA_EXP,			/* nf_conntrack_netlink.h */
+	NFQA_UID,			/* __u32 sk uid */
+	NFQA_GID,			/* __u32 sk gid */
 
 	__NFQA_MAX
 };
@@ -98,7 +101,8 @@ enum nfqnl_attr_config {
 #define NFQA_CFG_F_FAIL_OPEN			(1 << 0)
 #define NFQA_CFG_F_CONNTRACK			(1 << 1)
 #define NFQA_CFG_F_GSO				(1 << 2)
-#define NFQA_CFG_F_MAX				(1 << 3)
+#define NFQA_CFG_F_UID_GID			(1 << 3)
+#define NFQA_CFG_F_MAX				(1 << 4)
 
 /* flags for NFQA_SKB_INFO */
 /* packet appears to have wrong checksums, but they are ok */
diff --git a/src/libnetfilter_queue.c b/src/libnetfilter_queue.c
index fa8efe7..32725d1 100644
--- a/src/libnetfilter_queue.c
+++ b/src/libnetfilter_queue.c
@@ -668,6 +668,9 @@ EXPORT_SYMBOL(nfq_set_mode);
  *  if this bit is set, the layer 3/4 checksums of the packet appear incorrect,
  *  but are not (because they will be corrected later by the kernel).
  *
+ *  - NFQA_CFG_F_UID_GID: the kernel will dump UID and GID of the socket to
+ *  which each packet belongs.
+ *
  * Here's a little code snippet to show how to use this API:
  * \verbatim
 	uint32_t flags = NFQA_CFG_F_FAIL_OPEN;
@@ -1181,6 +1184,38 @@ struct nfqnl_msg_packet_hw *nfq_get_packet_hw(struct nfq_data *nfad)
 EXPORT_SYMBOL(nfq_get_packet_hw);
 
 /**
+ * nfq_get_uid - get the UID of the user the packet belongs to
+ * \param nfad Netlink packet data handle passed to callback function
+ *
+ * \return 1 if there is a UID available, 0 otherwise.
+ */
+int nfq_get_uid(struct nfq_data *nfad, u_int32_t *uid)
+{
+	if (!nfnl_attr_present(nfad->data, NFQA_UID))
+		return 0;
+
+	*uid = ntohl(nfnl_get_data(nfad->data, NFQA_UID, u_int32_t));
+	return 1;
+}
+EXPORT_SYMBOL(nfq_get_uid);
+
+/**
+ * nfq_get_gid - get the GID of the user the packet belongs to
+ * \param nfad Netlink packet data handle passed to callback function
+ *
+ * \return 1 if there is a GID available, 0 otherwise.
+ */
+int nfq_get_gid(struct nfq_data *nfad, u_int32_t *gid)
+{
+	if (!nfnl_attr_present(nfad->data, NFQA_GID))
+		return 0;
+
+	*gid = ntohl(nfnl_get_data(nfad->data, NFQA_GID, u_int32_t));
+	return 1;
+}
+EXPORT_SYMBOL(nfq_get_gid);
+
+/**
  * nfq_get_payload - get payload 
  * \param nfad Netlink packet data handle passed to callback function
  * \param data Pointer of pointer that will be pointed to the payload
@@ -1250,6 +1285,7 @@ int nfq_snprintf_xml(char *buf, size_t rem, struct nfq_data *tb, int flags)
 	struct nfqnl_msg_packet_hdr *ph;
 	struct nfqnl_msg_packet_hw *hwph;
 	u_int32_t mark, ifi;
+	u_int32_t uid, gid;
 	int size, offset = 0, len = 0, ret;
 	unsigned char *data;
 
@@ -1365,6 +1401,16 @@ int nfq_snprintf_xml(char *buf, size_t rem, struct nfq_data *tb, int flags)
 		SNPRINTF_FAILURE(size, rem, offset, len);
 	}
 
+	if (nfq_get_uid(tb, &uid) && (flags & NFQ_XML_UID)) {
+		size = snprintf(buf + offset, rem, "<uid>%u</uid>", uid);
+		SNPRINTF_FAILURE(size, rem, offset, len);
+	}
+
+	if (nfq_get_gid(tb, &gid) && (flags & NFQ_XML_GID)) {
+		size = snprintf(buf + offset, rem, "<gid>%u</gid>", gid);
+		SNPRINTF_FAILURE(size, rem, offset, len);
+	}
+
 	ret = nfq_get_payload(tb, &data);
 	if (ret >= 0 && (flags & NFQ_XML_PAYLOAD)) {
 		int i;
diff --git a/src/nlmsg.c b/src/nlmsg.c
index e7a30e0..81e170e 100644
--- a/src/nlmsg.c
+++ b/src/nlmsg.c
@@ -134,6 +134,8 @@ static int nfq_pkt_parse_attr_cb(const struct nlattr *attr, void *data)
 	case NFQA_IFINDEX_PHYSOUTDEV:
 	case NFQA_CAP_LEN:
 	case NFQA_SKB_INFO:
+	case NFQA_UID:
+	case NFQA_GID:
 		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
 			return MNL_CB_ERROR;
 		break;
-- 
1.8.5.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 2/2] utils: add test for nfq_get_uid and nfq_get_gid
  2014-01-07 13:30     ` [PATCH 0/2] " valentina.giusti
  2014-01-07 13:30       ` [PATCH 1/2] src: " valentina.giusti
@ 2014-01-07 13:30       ` valentina.giusti
  2014-01-07 23:43         ` Pablo Neira Ayuso
  1 sibling, 1 reply; 13+ messages in thread
From: valentina.giusti @ 2014-01-07 13:30 UTC (permalink / raw)
  To: pablo, netfilter-devel; +Cc: netdev, fw, daniel.wagner

From: Valentina Giusti <Valentina.Giusti@bmw-carit.de>

Signed-off-by: Valentina Giusti <Valentina.Giusti@bmw-carit.de>
---
 utils/nfqnl_test.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/utils/nfqnl_test.c b/utils/nfqnl_test.c
index a554f2d..c6b97c6 100644
--- a/utils/nfqnl_test.c
+++ b/utils/nfqnl_test.c
@@ -15,7 +15,7 @@ static u_int32_t print_pkt (struct nfq_data *tb)
 	int id = 0;
 	struct nfqnl_msg_packet_hdr *ph;
 	struct nfqnl_msg_packet_hw *hwph;
-	u_int32_t mark,ifi; 
+	u_int32_t mark, ifi, uid, gid;
 	int ret;
 	unsigned char *data;
 
@@ -55,6 +55,12 @@ static u_int32_t print_pkt (struct nfq_data *tb)
 	if (ifi)
 		printf("physoutdev=%u ", ifi);
 
+	if (nfq_get_uid(tb, &uid))
+		printf("uid=%u ", uid);
+
+	if (nfq_get_gid(tb, &gid))
+		printf("gid=%u ", gid);
+
 	ret = nfq_get_payload(tb, &data);
 	if (ret >= 0)
 		printf("payload_len=%d ", ret);
@@ -114,6 +120,10 @@ int main(int argc, char **argv)
 		exit(1);
 	}
 
+	printf("setting flags to request UID and GID\n");
+	if (nfq_set_queue_flags(qh, NFQA_CFG_F_UID_GID, NFQA_CFG_F_UID_GID))
+		fprintf(stderr, "can't request UID_GID flag\n");
+
 	fd = nfq_fd(h);
 
 	for (;;) {
-- 
1.8.5.1

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/2] src: add support for UID/GID socket info
  2014-01-07 13:30       ` [PATCH 1/2] src: " valentina.giusti
@ 2014-01-07 23:42         ` Pablo Neira Ayuso
  0 siblings, 0 replies; 13+ messages in thread
From: Pablo Neira Ayuso @ 2014-01-07 23:42 UTC (permalink / raw)
  To: valentina.giusti; +Cc: netfilter-devel, netdev, fw, daniel.wagner

On Tue, Jan 07, 2014 at 02:30:19PM +0100, valentina.giusti@bmw-carit.de wrote:
> From: Valentina Giusti <Valentina.Giusti@bmw-carit.de>
> 
> With this patch libnetfilter_queue is able to parse UID/GID
> socket information.

applied, thanks.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 2/2] utils: add test for nfq_get_uid and nfq_get_gid
  2014-01-07 13:30       ` [PATCH 2/2] utils: add test for nfq_get_uid and nfq_get_gid valentina.giusti
@ 2014-01-07 23:43         ` Pablo Neira Ayuso
  2014-01-08  9:36           ` Valentina Giusti
  0 siblings, 1 reply; 13+ messages in thread
From: Pablo Neira Ayuso @ 2014-01-07 23:43 UTC (permalink / raw)
  To: valentina.giusti; +Cc: netfilter-devel, netdev, fw, daniel.wagner

Applied, but mangled the error message to avoid confusing users in
case they use an old kernel.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 2/2] utils: add test for nfq_get_uid and nfq_get_gid
  2014-01-07 23:43         ` Pablo Neira Ayuso
@ 2014-01-08  9:36           ` Valentina Giusti
  0 siblings, 0 replies; 13+ messages in thread
From: Valentina Giusti @ 2014-01-08  9:36 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel, netdev, fw, daniel.wagner

On 01/08/2014 12:43 AM, Pablo Neira Ayuso wrote:
> Applied, but mangled the error message to avoid confusing users in
> case they use an old kernel.
>
Thanks Pablo!

BR,
- Val

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2014-01-08  9:37 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-20 16:28 [PATCH v4 0/2] Add UID/GID info to NFQUEUE valentina.giusti
2013-12-20 16:28 ` [PATCH v4 1/2] netfilter_queue: enable UID/GID socket info retrieval valentina.giusti
2013-12-23 14:21   ` Pablo Neira Ayuso
2014-01-07  8:11     ` Valentina Giusti
2013-12-20 16:28 ` [PATCH v4 2/2] libnetfilter_queue: add support for UID/GID socket info valentina.giusti
2013-12-21 11:38   ` Pablo Neira Ayuso
2014-01-07 13:30     ` [PATCH 0/2] " valentina.giusti
2014-01-07 13:30       ` [PATCH 1/2] src: " valentina.giusti
2014-01-07 23:42         ` Pablo Neira Ayuso
2014-01-07 13:30       ` [PATCH 2/2] utils: add test for nfq_get_uid and nfq_get_gid valentina.giusti
2014-01-07 23:43         ` Pablo Neira Ayuso
2014-01-08  9:36           ` Valentina Giusti
2013-12-21 12:11   ` [PATCH v4 2/2] libnetfilter_queue: add support for UID/GID socket info Pablo Neira Ayuso

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).