netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH nft] parser_json: fix segfault in translating string to nft object
@ 2019-04-11  8:59 Laura Garcia Liebana
  2019-04-11  9:15 ` Florian Westphal
  0 siblings, 1 reply; 3+ messages in thread
From: Laura Garcia Liebana @ 2019-04-11  8:59 UTC (permalink / raw)
  To: netfilter-devel; +Cc: pablo

A segmentation fault is produced when applying an input JSON file
like the following:

{"nftables": [
	{ "add":
		{"map":
			{"family": "ip",
				"name": "persistencia",
				"table": "nftlb",
				"type": "ipv4_addr",
				"map": "mark",
				"size": 65535,
				"flags": ["timeout"],
				"timeout": 44
			}
		}
	}
]}

The captured error is:

 Program received signal SIGSEGV, Segmentation fault.
 #1  0x00007ffff7f734f9 in string_to_nft_object (str=0x55555555f410
  "mark") at parser_json.c:2513
 2513			if (!strcmp(str, obj_tbl[i]))

The obj_tbl array is allocated with the maximum element index even
if lower indexes are not populated, so it produces null pointer
items.

This patch ensures that the maximum number of possible indexes
but also the element is not comparing a null pointer.

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
---
 src/parser_json.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/parser_json.c b/src/parser_json.c
index 827604b..d0eacb6 100644
--- a/src/parser_json.c
+++ b/src/parser_json.c
@@ -2500,17 +2500,18 @@ static struct cmd *json_parse_cmd_add_rule(struct json_ctx *ctx, json_t *root,
 
 static int string_to_nft_object(const char *str)
 {
-	const char *obj_tbl[] = {
+	const char *obj_tbl[__NFT_OBJECT_MAX] = {
 		[NFT_OBJECT_COUNTER] = "counter",
 		[NFT_OBJECT_QUOTA] = "quota",
 		[NFT_OBJECT_CT_HELPER] = "ct helper",
 		[NFT_OBJECT_LIMIT] = "limit",
 		[NFT_OBJECT_SECMARK] = "secmark",
 	};
+
 	unsigned int i;
 
-	for (i = 1; i < array_size(obj_tbl); i++) {
-		if (!strcmp(str, obj_tbl[i]))
+	for (i = 0; i < NFT_OBJECT_MAX; i++) {
+		if (obj_tbl[i] && !strcmp(str, obj_tbl[i]))
 			return i;
 	}
 	return 0;
-- 
2.11.0


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

* Re: [PATCH nft] parser_json: fix segfault in translating string to nft object
  2019-04-11  8:59 [PATCH nft] parser_json: fix segfault in translating string to nft object Laura Garcia Liebana
@ 2019-04-11  9:15 ` Florian Westphal
  2019-04-11 10:34   ` Phil Sutter
  0 siblings, 1 reply; 3+ messages in thread
From: Florian Westphal @ 2019-04-11  9:15 UTC (permalink / raw)
  To: Laura Garcia Liebana; +Cc: netfilter-devel, pablo, phil

Laura Garcia Liebana <nevola@gmail.com> wrote:
> The obj_tbl array is allocated with the maximum element index even
> if lower indexes are not populated, so it produces null pointer
> items.
> 
> This patch ensures that the maximum number of possible indexes
> but also the element is not comparing a null pointer.

Applied, thanks Laura.

>  static int string_to_nft_object(const char *str)
>  {
> -	const char *obj_tbl[] = {
> +	const char *obj_tbl[__NFT_OBJECT_MAX] = {
>  		[NFT_OBJECT_COUNTER] = "counter",
>  		[NFT_OBJECT_QUOTA] = "quota",
>  		[NFT_OBJECT_CT_HELPER] = "ct helper",
>  		[NFT_OBJECT_LIMIT] = "limit",
>  		[NFT_OBJECT_SECMARK] = "secmark",
>  	};

Phil, does this need updating?

It looks to me as if this should also init NFT_OBJECT_CT_TIMEOUT and so on.

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

* Re: [PATCH nft] parser_json: fix segfault in translating string to nft object
  2019-04-11  9:15 ` Florian Westphal
@ 2019-04-11 10:34   ` Phil Sutter
  0 siblings, 0 replies; 3+ messages in thread
From: Phil Sutter @ 2019-04-11 10:34 UTC (permalink / raw)
  To: Florian Westphal; +Cc: Laura Garcia Liebana, netfilter-devel, pablo

Hi,

On Thu, Apr 11, 2019 at 11:15:58AM +0200, Florian Westphal wrote:
> Laura Garcia Liebana <nevola@gmail.com> wrote:
> > The obj_tbl array is allocated with the maximum element index even
> > if lower indexes are not populated, so it produces null pointer
> > items.
> > 
> > This patch ensures that the maximum number of possible indexes
> > but also the element is not comparing a null pointer.
> 
> Applied, thanks Laura.

Thanks for catching this, Laura!

> >  static int string_to_nft_object(const char *str)
> >  {
> > -	const char *obj_tbl[] = {
> > +	const char *obj_tbl[__NFT_OBJECT_MAX] = {
> >  		[NFT_OBJECT_COUNTER] = "counter",
> >  		[NFT_OBJECT_QUOTA] = "quota",
> >  		[NFT_OBJECT_CT_HELPER] = "ct helper",
> >  		[NFT_OBJECT_LIMIT] = "limit",
> >  		[NFT_OBJECT_SECMARK] = "secmark",
> >  	};
> 
> Phil, does this need updating?
> 
> It looks to me as if this should also init NFT_OBJECT_CT_TIMEOUT and so on.

Actually, it is not strict enough. The function is used when handling
'add map' command. In bison, only counter, quota, limit and secmark are
allowed as stateful object "destination". I suspect ct helper is a
leftover from reusing the function somewhere else. I'll send a patch to
remove it.

Cheers, Phil

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

end of thread, other threads:[~2019-04-11 10:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-04-11  8:59 [PATCH nft] parser_json: fix segfault in translating string to nft object Laura Garcia Liebana
2019-04-11  9:15 ` Florian Westphal
2019-04-11 10:34   ` Phil Sutter

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