Skip to contentSkip to conversation

extra: use inet_ntop instead of inet_ntoa

PATCH Applied [email protected] 2 participants 7 messages Jun 20, 2014 – Jun 30, 2014 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 extra: use inet_ntop instead of inet_ntoa

From: Ting-Wei Lan <[email protected]>

The result of inet_ntoa() will be overwritten by the next call to inet_ntoa(), so using it twice in the same snprintf() call causes wrong result.

---
 src/extra/ipv4.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
src/extra/ipv4.c +5 -1
index 0fe716b..a93d113 100644
@@ -134,9 +134,13 @@ int nfq_ip_snprintf(char *buf, size_t size, const struct iphdr *iph)
 	struct in_addr src = { iph->saddr };
 	struct in_addr dst = { iph->daddr };
 
+	char src_str[INET_ADDRSTRLEN];
+	char dst_str[INET_ADDRSTRLEN];
+
 	ret = snprintf(buf, size, "SRC=%s DST=%s LEN=%u TOS=0x%X "
 				  "PREC=0x%X TTL=%u ID=%u PROTO=%u ",
-			inet_ntoa(src), inet_ntoa(dst),
+			inet_ntop(AF_INET, &src, src_str, INET_ADDRSTRLEN),
+			inet_ntop(AF_INET, &dst, dst_str, INET_ADDRSTRLEN),
 			ntohs(iph->tot_len), IPTOS_TOS(iph->tos),
 			IPTOS_PREC(iph->tos), iph->ttl, ntohs(iph->id),
 			iph->protocol);
Signature · 1 line
1.9.3
#1 lore raw

PATCH 2/3 extra: fix wrong implementation in nfq_udp_get_payload

From: Ting-Wei Lan <[email protected]>

---
 src/extra/udp.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
src/extra/udp.c +8 -4
index eee732e..2a17a2f 100644
@@ -56,13 +56,17 @@ EXPORT_SYMBOL(nfq_udp_get_hdr);
  */
 void *nfq_udp_get_payload(struct udphdr *udph, struct pkt_buff *pktb)
 {
-	unsigned int doff = udph->len;
+	uint16_t len = ntohs (udph->len);
 
-	/* malformed UDP data offset. */
-	if (pktb->transport_header + doff > pktb->tail)
+	/* the UDP packet is too short. */
+	if (len < sizeof(struct udphdr))
 		return NULL;
 
-	return pktb->transport_header + doff;
+	/* malformed UDP packet. */
+	if (pktb->transport_header + len > pktb->tail)
+		return NULL;
+
+	return pktb->transport_header + sizeof(struct udphdr);
 }
 EXPORT_SYMBOL(nfq_udp_get_payload);
 
Signature · 1 line
1.9.3
maintainer Applied
#2 lore raw

Re: [PATCH 2/3] extra: fix wrong implementation in nfq_udp_get_payload

Quoted 11 lines · On Fri, Jun 20, 2014 at 06:27:00PM +0800, [email protected] wrote:

From: Ting-Wei Lan <[email protected]>

---
 src/extra/udp.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
src/extra/udp.c +0 -0
index eee732e..2a17a2f 100644

src/extra/udp.c EXPORT_SYMBOL(nfq_udp_get_hdr); · line 59

Quoted 5 lines
src/extra/udp.c +1 -1
@@ -56,13 +56,17 @@ EXPORT_SYMBOL(nfq_udp_get_hdr);
  */
 void *nfq_udp_get_payload(struct udphdr *udph, struct pkt_buff *pktb)
 {
-	unsigned int doff = udph->len;
+	uint16_t len = ntohs (udph->len);
                            ^

removed this space.

Applied, thanks.

author
#3 lore raw

Re: [PATCH 2/3] extra: fix wrong implementation in nfq_udp_get_payload

於 一,2014-06-30 於 12:02 +0200,Pablo Neira Ayuso 提到:

Quoted 19 lines · On Fri, Jun 20, 2014 at 06:27:00PM +0800, [email protected] wrote:
Quoted 11 lines

From: Ting-Wei Lan <[email protected]>

---
 src/extra/udp.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
src/extra/udp.c +0 -0
index eee732e..2a17a2f 100644

src/extra/udp.c EXPORT_SYMBOL(nfq_udp_get_hdr); · line 59

Quoted 5 lines
src/extra/udp.c +1 -1
@@ -56,13 +56,17 @@ EXPORT_SYMBOL(nfq_udp_get_hdr);
  */
 void *nfq_udp_get_payload(struct udphdr *udph, struct pkt_buff *pktb)
 {
-	unsigned int doff = udph->len;
+	uint16_t len = ntohs (udph->len);
                            ^
Quoted 3 lines

removed this space.

Applied, thanks.

but I cannot find this patch in libnetfilter_queue git log ...

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw

PATCH 3/3 extra: fix wrong implementation in nfq_udp_get_payload_len

In reply to [email protected]

From: Ting-Wei Lan <[email protected]>

---
 src/extra/udp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/extra/udp.c +1 -1
index 2a17a2f..64d3db4 100644
@@ -76,7 +76,7 @@ EXPORT_SYMBOL(nfq_udp_get_payload);
  */
 unsigned int nfq_udp_get_payload_len(struct udphdr *udph, struct pkt_buff *pktb)
 {
-	return pktb->tail - pktb->transport_header;
+	return pktb->tail - pktb->transport_header - sizeof(struct udphdr);
 }
 EXPORT_SYMBOL(nfq_udp_get_payload_len);
 
Signature · 1 line
1.9.3
#5 lore raw

Re: [PATCH 3/3] extra: fix wrong implementation in nfq_udp_get_payload_len

Quoted 11 lines · On Fri, Jun 20, 2014 at 06:27:01PM +0800, [email protected] wrote:

From: Ting-Wei Lan <[email protected]>

---
 src/extra/udp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/extra/udp.c +0 -0
index 2a17a2f..64d3db4 100644

src/extra/udp.c EXPORT_SYMBOL(nfq_udp_get_payload); · line 79

Quoted 5 lines
src/extra/udp.c +1 -1
@@ -76,7 +76,7 @@ EXPORT_SYMBOL(nfq_udp_get_payload);
  */
 unsigned int nfq_udp_get_payload_len(struct udphdr *udph, struct pkt_buff *pktb)
 {
-	return pktb->tail - pktb->transport_header;
+	return pktb->tail - pktb->transport_header - sizeof(struct udphdr);

I agree that this should substract the UDP header size, but:

1) This underflows the unsigned int if the UDP header is truncated.
2) This breaks the current behaviour, but we don't have any client in the tree for this code yet though.

Let's do it well, deprecate this interface and introduce a new one:

int nfq_udp_get_payload_length(struct udphdr *udph, struct pkt_buff *pktb)

Same thing for the tcp variant.

The deprecation can be achieved through this:

in __attribute__((deprecated) nfq_udp_get_payload_len(...)

in the header file.

Would you send me a patch for that, please?

BTW, please Signed-off-by your userspace patches too. Thanks.