All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ipv4: netfilter: always let NUL terminated string ended by '\0'
@ 2013-05-17  8:39 Chen Gang
  2013-05-23 11:08 ` Pablo Neira Ayuso
  0 siblings, 1 reply; 9+ messages in thread
From: Chen Gang @ 2013-05-17  8:39 UTC (permalink / raw)
  To: pablo, kaber, kuznet, jmorris, yoshfuji
  Cc: David Miller, netfilter-devel, netfilter, coreteam, netdev


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.


Signed-off-by: Chen Gang <gang.chen@asianux.com>
---
 net/ipv4/netfilter/ipt_ULOG.c |   27 ++++++++++++++++-----------
 1 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/net/ipv4/netfilter/ipt_ULOG.c b/net/ipv4/netfilter/ipt_ULOG.c
index f8a222cb..4a93382 100644
--- a/net/ipv4/netfilter/ipt_ULOG.c
+++ b/net/ipv4/netfilter/ipt_ULOG.c
@@ -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. */
-- 
1.7.7.6

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

* Re: [PATCH] ipv4: netfilter: always let NUL terminated string ended by '\0'
  2013-05-17  8:39 [PATCH] ipv4: netfilter: always let NUL terminated string ended by '\0' Chen Gang
@ 2013-05-23 11:08 ` Pablo Neira Ayuso
  2013-05-23 11:39   ` Chen Gang
  2013-05-23 11:50   ` [PATCH v2] " Chen Gang
  0 siblings, 2 replies; 9+ messages in thread
From: Pablo Neira Ayuso @ 2013-05-23 11:08 UTC (permalink / raw)
  To: Chen Gang
  Cc: kaber, kuznet, jmorris, yoshfuji, David Miller, netfilter-devel,
	netfilter, coreteam, netdev

On Fri, May 17, 2013 at 04:39:15PM +0800, Chen Gang wrote:
> 
> 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.

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

* Re: [PATCH] ipv4: netfilter: always let NUL terminated string ended by '\0'
  2013-05-23 11:08 ` Pablo Neira Ayuso
@ 2013-05-23 11:39   ` Chen Gang
  2013-05-23 11:49       ` Pablo Neira Ayuso
  2013-05-23 11:50   ` [PATCH v2] " Chen Gang
  1 sibling, 1 reply; 9+ messages in thread
From: Chen Gang @ 2013-05-23 11:39 UTC (permalink / raw)
  To: Pablo Neira Ayuso
  Cc: kaber, kuznet, jmorris, yoshfuji, David Miller, netfilter-devel,
	netfilter, coreteam, netdev

On 05/23/2013 07:08 PM, Pablo Neira Ayuso wrote:
> On Fri, May 17, 2013 at 04:39:15PM +0800, Chen Gang wrote:
>> > 
>> > 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.
-- 
Chen Gang

Asianux Corporation

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

* Re: [PATCH] ipv4: netfilter: always let NUL terminated string ended by '\0'
  2013-05-23 11:39   ` Chen Gang
@ 2013-05-23 11:49       ` Pablo Neira Ayuso
  0 siblings, 0 replies; 9+ messages in thread
From: Pablo Neira Ayuso @ 2013-05-23 11:49 UTC (permalink / raw)
  To: Chen Gang
  Cc: kaber, kuznet, jmorris, yoshfuji, David Miller, netfilter-devel,
	netfilter, coreteam, netdev

On Thu, May 23, 2013 at 07:39:39PM +0800, Chen Gang wrote:
> On 05/23/2013 07:08 PM, Pablo Neira Ayuso wrote:
> > On Fri, May 17, 2013 at 04:39:15PM +0800, Chen Gang wrote:
> >> >
> >> > 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.

Both are ULOG_PREFIX_LEN long.

>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;

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

* Re: [PATCH] ipv4: netfilter: always let NUL terminated string ended by '\0'
@ 2013-05-23 11:49       ` Pablo Neira Ayuso
  0 siblings, 0 replies; 9+ messages in thread
From: Pablo Neira Ayuso @ 2013-05-23 11:49 UTC (permalink / raw)
  To: Chen Gang
  Cc: kaber, kuznet, jmorris, yoshfuji, David Miller, netfilter-devel,
	netfilter, coreteam, netdev

On Thu, May 23, 2013 at 07:39:39PM +0800, Chen Gang wrote:
> On 05/23/2013 07:08 PM, Pablo Neira Ayuso wrote:
> > On Fri, May 17, 2013 at 04:39:15PM +0800, Chen Gang wrote:
> >> >
> >> > 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.

Both are ULOG_PREFIX_LEN long.

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;

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

* [PATCH v2] ipv4: netfilter: always let NUL terminated string ended by '\0'
  2013-05-23 11:08 ` Pablo Neira Ayuso
  2013-05-23 11:39   ` Chen Gang
@ 2013-05-23 11:50   ` Chen Gang
  2013-05-23 12:26     ` Pablo Neira Ayuso
  1 sibling, 1 reply; 9+ messages in thread
From: Chen Gang @ 2013-05-23 11:50 UTC (permalink / raw)
  To: Pablo Neira Ayuso
  Cc: kaber, kuznet, jmorris, yoshfuji, David Miller, netfilter-devel,
	netfilter, coreteam, netdev


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.


Signed-off-by: Chen Gang <gang.chen@asianux.com>
---
 net/ipv4/netfilter/ipt_ULOG.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/net/ipv4/netfilter/ipt_ULOG.c b/net/ipv4/netfilter/ipt_ULOG.c
index cf08218..ff4b781 100644
--- a/net/ipv4/netfilter/ipt_ULOG.c
+++ b/net/ipv4/netfilter/ipt_ULOG.c
@@ -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
-- 
1.7.7.6

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

* Re: [PATCH] ipv4: netfilter: always let NUL terminated string ended by '\0'
  2013-05-23 11:49       ` Pablo Neira Ayuso
  (?)
@ 2013-05-23 11:59       ` Chen Gang
  -1 siblings, 0 replies; 9+ messages in thread
From: Chen Gang @ 2013-05-23 11:59 UTC (permalink / raw)
  To: Pablo Neira Ayuso
  Cc: kaber, kuznet, jmorris, yoshfuji, David Miller, netfilter-devel,
	netfilter, coreteam, netdev

On 05/23/2013 07:49 PM, Pablo Neira Ayuso wrote:
> On Thu, May 23, 2013 at 07:39:39PM +0800, Chen Gang wrote:
>> > On 05/23/2013 07:08 PM, Pablo Neira Ayuso wrote:
>>> > > On Fri, May 17, 2013 at 04:39:15PM +0800, Chen Gang wrote:
>>>>> > >> >
>>>>> > >> > 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.
> Both are ULOG_PREFIX_LEN long.
> 
>>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;
> 
> 

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.
-- 
Chen Gang

Asianux Corporation

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

* Re: [PATCH v2] ipv4: netfilter: always let NUL terminated string ended by '\0'
  2013-05-23 11:50   ` [PATCH v2] " Chen Gang
@ 2013-05-23 12:26     ` Pablo Neira Ayuso
  2013-05-24  1:11       ` Chen Gang
  0 siblings, 1 reply; 9+ messages in thread
From: Pablo Neira Ayuso @ 2013-05-23 12:26 UTC (permalink / raw)
  To: Chen Gang
  Cc: kaber, kuznet, jmorris, yoshfuji, David Miller, netfilter-devel,
	netfilter, coreteam, netdev

On Thu, May 23, 2013 at 07:50:46PM +0800, Chen Gang wrote:
> 
> 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.

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

* Re: [PATCH v2] ipv4: netfilter: always let NUL terminated string ended by '\0'
  2013-05-23 12:26     ` Pablo Neira Ayuso
@ 2013-05-24  1:11       ` Chen Gang
  0 siblings, 0 replies; 9+ messages in thread
From: Chen Gang @ 2013-05-24  1:11 UTC (permalink / raw)
  To: Pablo Neira Ayuso
  Cc: kaber, kuznet, jmorris, yoshfuji, David Miller, netfilter-devel,
	netfilter, coreteam, netdev

On 05/23/2013 08:26 PM, Pablo Neira Ayuso wrote:
> On Thu, May 23, 2013 at 07:50:46PM +0800, Chen Gang wrote:
>> > 
>> > 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.
> 
> 

Thank you too.

-- 
Chen Gang

Asianux Corporation

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

end of thread, other threads:[~2013-05-24  1:11 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-17  8:39 [PATCH] ipv4: netfilter: always let NUL terminated string ended by '\0' Chen Gang
2013-05-23 11:08 ` Pablo Neira Ayuso
2013-05-23 11:39   ` Chen Gang
2013-05-23 11:49     ` Pablo Neira Ayuso
2013-05-23 11:49       ` Pablo Neira Ayuso
2013-05-23 11:59       ` Chen Gang
2013-05-23 11:50   ` [PATCH v2] " Chen Gang
2013-05-23 12:26     ` Pablo Neira Ayuso
2013-05-24  1:11       ` Chen Gang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.