Skip to contentSkip to conversation

ipv4: netfilter: always let NUL terminated string ended by '\0'

PATCH Superseded Chen Gang 2 participants 8 messages May 17, 2013 – May 24, 2013

Subsystems (3)
lore
Share & tools
lore

Chen Gang

author
#0 lore raw

PATCH 1/1 ipv4: netfilter: always let NUL terminated string ended by '\0'

For NUL terminated string, better always be sure of ended by '\0'.

'pm' is 'struct ulog_packet_msg_t' which may be copied to user mode (defined in "include/uapi/..."), so can not use strlcpy() instead of.

---
 net/ipv4/netfilter/ipt_ULOG.c |   27 ++++++++++++++++-----------
 1 files changed, 16 insertions(+), 11 deletions(-)
net/ipv4/netfilter/ipt_ULOG.c +16 -11
index f8a222cb..4a93382 100644
@@ -231,11 +231,13 @@ static void ipt_ulog_packet(unsigned int hooknum,
 	put_unaligned(tv.tv_usec, &pm->timestamp_usec);
 	put_unaligned(skb->mark, &pm->mark);
 	pm->hook = hooknum;
-	if (prefix != NULL)
-		strncpy(pm->prefix, prefix, sizeof(pm->prefix));
-	else if (loginfo->prefix[0] != '\0')
-		strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix));
-	else
+	if (prefix != NULL) {
+		strncpy(pm->prefix, prefix, sizeof(pm->prefix) - 1);
+		pm->prefix[sizeof(pm->prefix) - 1] = '\0';
+	} else if (loginfo->prefix[0] != '\0') {
+		strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix) - 1);
+		pm->prefix[sizeof(pm->prefix) - 1] = '\0';
+	} else
 		*(pm->prefix) = '\0';
 
 	if (in && in->hard_header_len > 0 &&
@@ -246,14 +248,17 @@ static void ipt_ulog_packet(unsigned int hooknum,
 	} else
 		pm->mac_len = 0;
 
-	if (in)
-		strncpy(pm->indev_name, in->name, sizeof(pm->indev_name));
-	else
+	if (in) {
+		strncpy(pm->indev_name, in->name, sizeof(pm->indev_name) - 1);
+		pm->indev_name[sizeof(pm->indev_name) - 1] = '\0';
+	} else
 		pm->indev_name[0] = '\0';
 
-	if (out)
-		strncpy(pm->outdev_name, out->name, sizeof(pm->outdev_name));
-	else
+	if (out) {
+		strncpy(pm->outdev_name, out->name,
+			sizeof(pm->outdev_name) - 1);
+		pm->outdev_name[sizeof(pm->outdev_name) - 1] = '\0';
+	} else
 		pm->outdev_name[0] = '\0';
 
 	/* copy_len <= skb->len, so can't fail. */
Signature · 1 line
1.7.7.6
#1 lore raw
Quoted 5 lines · Chen Gang

For NUL terminated string, better always be sure of ended by '\0'.

'pm' is 'struct ulog_packet_msg_t' which may be copied to user mode (defined in "include/uapi/..."), so can not use strlcpy() instead of.

loginfo->prefix is always nul-terminated, as this is validated in checkentry. I don't think the kernel will take a non nul-terminated device name either.

author
#2 lore raw
Quoted 11 lines · Pablo Neira Ayuso
Quoted 5 lines · Chen Gang
Quoted 5 lines

For NUL terminated string, better always be sure of ended by '\0'.

'pm' is 'struct ulog_packet_msg_t' which may be copied to user mode (defined in "include/uapi/..."), so can not use strlcpy() instead of.

loginfo->prefix is always nul-terminated, as this is validated in checkentry. I don't think the kernel will take a non nul-terminated device name either.

Really it is.

And 'prefix' max length is 128 (NF_LOG_PREFIXLEN), and 'pm->prefix' max length is 32 (ULOG_PREFIX_LEN), so we still need this patch, but need improved.

So I should send patch v2.

Thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation
#3 lore raw
Quoted 11 lines · Chen Gang
Quoted 9 lines · Pablo Neira Ayuso
Quoted 5 lines · Chen Gang
Quoted 5 lines

For NUL terminated string, better always be sure of ended by '\0'.

'pm' is 'struct ulog_packet_msg_t' which may be copied to user mode (defined in "include/uapi/..."), so can not use strlcpy() instead of.

loginfo->prefix is always nul-terminated, as this is validated in checkentry. I don't think the kernel will take a non nul-terminated device name either.

Quoted 5 lines

Really it is.

And 'prefix' max length is 128 (NF_LOG_PREFIXLEN), and 'pm->prefix' max length is 32 (ULOG_PREFIX_LEN), so we still need this patch, but need improved.

Both are ULOG_PREFIX_LEN long.

Quoted 1 line

From include/uapi/linux/netfilter_ipv4/ipt_ULOG.h:

/* private data structure for each rule with a ULOG target */

struct ipt_ulog_info {
        unsigned int nl_group;
        size_t copy_range;
        size_t qthreshold;
        char prefix[ULOG_PREFIX_LEN];
};

/* Format of the ULOG packets passed through netlink */

typedef struct ulog_packet_msg {
        unsigned long mark;
        long timestamp_sec;
        long timestamp_usec;
        unsigned int hook;
        char indev_name[IFNAMSIZ];
        char outdev_name[IFNAMSIZ];
        size_t data_len;
        char prefix[ULOG_PREFIX_LEN];
        unsigned char mac_len;
        unsigned char mac[ULOG_MAC_LEN;
        unsigned char payload[0];
} ulog_packet_msg_t;
author
#4 lore raw
Quoted 30 lines · Pablo Neira Ayuso
Quoted 11 lines · Chen Gang
Quoted 11 lines · Pablo Neira Ayuso
Quoted 9 lines
Quoted 9 lines · Chen Gang
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

For NUL terminated string, better always be sure of ended by '\0'.

'pm' is 'struct ulog_packet_msg_t' which may be copied to user mode (defined in "include/uapi/..."), so can not use strlcpy() instead of.

loginfo->prefix is always nul-terminated, as this is validated in checkentry. I don't think the kernel will take a non nul-terminated device name either.

Quoted 5 lines
Quoted 5 lines

Really it is.

And 'prefix' max length is 128 (NF_LOG_PREFIXLEN), and 'pm->prefix' max length is 32 (ULOG_PREFIX_LEN), so we still need this patch, but need improved.

Both are ULOG_PREFIX_LEN long.

Quoted 1 line

From include/uapi/linux/netfilter_ipv4/ipt_ULOG.h:

/* private data structure for each rule with a ULOG target */

struct ipt_ulog_info {
        unsigned int nl_group;
        size_t copy_range;
        size_t qthreshold;
        char prefix[ULOG_PREFIX_LEN];
};

/* Format of the ULOG packets passed through netlink */

Quoted 15 lines
typedef struct ulog_packet_msg {
        unsigned long mark;
        long timestamp_sec;
        long timestamp_usec;
        unsigned int hook;
        char indev_name[IFNAMSIZ];
        char outdev_name[IFNAMSIZ];
        size_t data_len;
        char prefix[ULOG_PREFIX_LEN];
        unsigned char mac_len;
        unsigned char mac[ULOG_MAC_LEN;
        unsigned char payload[0];
} ulog_packet_msg_t;

Yes, for 'loginfo->prefix', really it is.

But for 'prefix', it is 128. the call work flow is:

nf_log_packet() -> "char prefix[NF_LOG_PREFIXLEN];"

    logger->logfn() -> "prefix as last parameter"
      ipt_logfn() ->   "prefix as last parameter"
        ipt_ulog_packet() "prefix as last parameter"

netfilter/nf_log.c:16:#define NF_LOG_PREFIXLEN 128

Thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation
author
#5 lore raw

PATCH v2 1/1 ipv4: netfilter: always let NUL terminated string ended by '\0'

In reply to Pablo Neira Ayuso

For NUL terminated string, need always be sure of ended by '\0'.

'prefix' max length is 128 (NF_LOG_PREFIXLEN), and 'pm->prefix' max length is 32 (ULOG_PREFIX_LEN), so really need notice it.

'pm' is 'struct ulog_packet_msg_t' which may be copied to user mode (defined in "include/uapi/..."), so can not use strlcpy() instead of.

---
 net/ipv4/netfilter/ipt_ULOG.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
net/ipv4/netfilter/ipt_ULOG.c +4 -2
index cf08218..ff4b781 100644
@@ -231,8 +231,10 @@ static void ipt_ulog_packet(struct net *net,
 	put_unaligned(tv.tv_usec, &pm->timestamp_usec);
 	put_unaligned(skb->mark, &pm->mark);
 	pm->hook = hooknum;
-	if (prefix != NULL)
-		strncpy(pm->prefix, prefix, sizeof(pm->prefix));
+	if (prefix != NULL) {
+		strncpy(pm->prefix, prefix, sizeof(pm->prefix) - 1);
+		pm->prefix[sizeof(pm->prefix) - 1] = '\0';
+	}
 	else if (loginfo->prefix[0] != '\0')
 		strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix));
 	else
Signature · 1 line
1.7.7.6
#6 lore raw
Quoted 8 lines · Chen Gang

For NUL terminated string, need always be sure of ended by '\0'.

'prefix' max length is 128 (NF_LOG_PREFIXLEN), and 'pm->prefix' max length is 32 (ULOG_PREFIX_LEN), so really need notice it.

'pm' is 'struct ulog_packet_msg_t' which may be copied to user mode (defined in "include/uapi/..."), so can not use strlcpy() instead of.

That's fixing a real bug. We're passing strings that are longer than 32 bytes from nf_conntrack_tcp via nf_log, if ipt_ULOG is used, it will pass a non-null terminated string.

I'm going to rework the patch description to include this and apply this patch.

Thanks Chen.

author
#7 lore raw
Quoted 9 lines · Pablo Neira Ayuso
Quoted 8 lines · Chen Gang
Quoted 8 lines

For NUL terminated string, need always be sure of ended by '\0'.

'prefix' max length is 128 (NF_LOG_PREFIXLEN), and 'pm->prefix' max length is 32 (ULOG_PREFIX_LEN), so really need notice it.

'pm' is 'struct ulog_packet_msg_t' which may be copied to user mode (defined in "include/uapi/..."), so can not use strlcpy() instead of.

Quoted 10 lines

That's fixing a real bug. We're passing strings that are longer than 32 bytes from nf_conntrack_tcp via nf_log, if ipt_ULOG is used, it will pass a non-null terminated string.

I'm going to rework the patch description to include this and apply this patch.

Thanks Chen.

Thank you too.

Signature · 3 lines
Chen Gang

Asianux Corporation