Blame SOURCES/0023-parser_json-Fix-for-ineffective-family-value-checks.patch

9ff721
From c40b8c22beef6011d79c60742bc4043d681198c1 Mon Sep 17 00:00:00 2001
9ff721
From: Phil Sutter <phil@nwl.cc>
9ff721
Date: Fri, 12 Oct 2018 17:23:24 +0200
9ff721
Subject: [PATCH] parser_json: Fix for ineffective family value checks
9ff721
9ff721
Since handle->family is unsigned, checking for value < 0 never yields
9ff721
true. Overcome this by changing parse_family() to return an error code
9ff721
and write the parsed family value into a pointer passed as parameter.
9ff721
9ff721
The above change required a bit more cleanup to avoid passing pointers
9ff721
to signed variables to the function. Also leverage json_parse_family() a
9ff721
bit more to reduce code side.
9ff721
9ff721
Signed-off-by: Phil Sutter <phil@nwl.cc>
9ff721
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9ff721
(cherry picked from commit c7a5401943df8b6b96f6b5eedd9a1e0013e01d86)
9ff721
9ff721
Conflicts:
9ff721
	src/parser_json.c
9ff721
-> Missing ct timeout support
9ff721
-> missing tproxy support in JSON
9ff721
-> Missing ipsec expression
9ff721
9ff721
Signed-off-by: Phil Sutter <psutter@redhat.com>
9ff721
---
9ff721
 src/parser_json.c | 130 ++++++++++++++++++++++------------------------
9ff721
 1 file changed, 62 insertions(+), 68 deletions(-)
9ff721
9ff721
diff --git a/src/parser_json.c b/src/parser_json.c
9ff721
index af57b3025a104..30de17f8a1e26 100644
9ff721
--- a/src/parser_json.c
9ff721
+++ b/src/parser_json.c
9ff721
@@ -173,7 +173,7 @@ static int json_unpack_stmt(struct json_ctx *ctx, json_t *root,
9ff721
 	return 1;
9ff721
 }
9ff721
 
9ff721
-static int parse_family(const char *name)
9ff721
+static int parse_family(const char *name, uint32_t *family)
9ff721
 {
9ff721
 	unsigned int i;
9ff721
 	struct {
9ff721
@@ -188,13 +188,37 @@ static int parse_family(const char *name)
9ff721
 		{ "netdev", NFPROTO_NETDEV }
9ff721
 	};
9ff721
 
9ff721
+	assert(family);
9ff721
+
9ff721
 	for (i = 0; i < array_size(family_tbl); i++) {
9ff721
-		if (!strcmp(name, family_tbl[i].name))
9ff721
-			return family_tbl[i].val;
9ff721
+		if (strcmp(name, family_tbl[i].name))
9ff721
+			continue;
9ff721
+
9ff721
+		*family = family_tbl[i].val;
9ff721
+		return 0;
9ff721
 	}
9ff721
 	return -1;
9ff721
 }
9ff721
 
9ff721
+static int json_parse_family(struct json_ctx *ctx, json_t *root)
9ff721
+{
9ff721
+	const char *family;
9ff721
+
9ff721
+	if (!json_unpack(root, "{s:s}", "family", &family)) {
9ff721
+		uint32_t familyval;
9ff721
+
9ff721
+		if (parse_family(family, &familyval) ||
9ff721
+		    (familyval != NFPROTO_IPV6 &&
9ff721
+		     familyval != NFPROTO_IPV4)) {
9ff721
+			json_error(ctx, "Invalid family '%s'.", family);
9ff721
+			return -1;
9ff721
+		}
9ff721
+		return familyval;
9ff721
+	}
9ff721
+
9ff721
+	return NFPROTO_UNSPEC;
9ff721
+}
9ff721
+
9ff721
 static bool is_keyword(const char *keyword)
9ff721
 {
9ff721
 	const char *keywords[] = {
9ff721
@@ -594,19 +618,15 @@ static struct expr *json_parse_rt_expr(struct json_ctx *ctx,
9ff721
 		{ "nexthop", NFT_RT_NEXTHOP4 },
9ff721
 		{ "mtu", NFT_RT_TCPMSS },
9ff721
 	};
9ff721
-	unsigned int i, familyval = NFPROTO_UNSPEC;
9ff721
-	const char *key, *family = NULL;
9ff721
+	const char *key;
9ff721
+	unsigned int i;
9ff721
+	int familyval;
9ff721
 
9ff721
 	if (json_unpack_err(ctx, root, "{s:s}", "key", &key))
9ff721
 		return NULL;
9ff721
-	if (!json_unpack(root, "{s:s}", "family", &family)) {
9ff721
-		familyval = parse_family(family);
9ff721
-		if (familyval != NFPROTO_IPV4 &&
9ff721
-		    familyval != NFPROTO_IPV6) {
9ff721
-			json_error(ctx, "Invalid RT family '%s'.", family);
9ff721
-			return NULL;
9ff721
-		}
9ff721
-	}
9ff721
+	familyval = json_parse_family(ctx, root);
9ff721
+	if (familyval < 0)
9ff721
+		return NULL;
9ff721
 
9ff721
 	for (i = 0; i < array_size(rt_key_tbl); i++) {
9ff721
 		int val = rt_key_tbl[i].val;
9ff721
@@ -653,9 +673,9 @@ static bool ct_key_is_dir(enum nft_ct_keys key)
9ff721
 static struct expr *json_parse_ct_expr(struct json_ctx *ctx,
9ff721
 				       const char *type, json_t *root)
9ff721
 {
9ff721
-	const char *key, *dir, *family;
9ff721
+	const char *key, *dir;
9ff721
 	unsigned int i;
9ff721
-	int dirval = -1, familyval = NFPROTO_UNSPEC, keyval = -1;
9ff721
+	int dirval = -1, familyval, keyval = -1;
9ff721
 
9ff721
 	if (json_unpack_err(ctx, root, "{s:s}", "key", &key))
9ff721
 		return NULL;
9ff721
@@ -672,14 +692,9 @@ static struct expr *json_parse_ct_expr(struct json_ctx *ctx,
9ff721
 		return NULL;
9ff721
 	}
9ff721
 
9ff721
-	if (!json_unpack(root, "{s:s}", "family", &family)) {
9ff721
-		familyval = parse_family(family);
9ff721
-		if (familyval != NFPROTO_IPV4 &&
9ff721
-		    familyval != NFPROTO_IPV6) {
9ff721
-			json_error(ctx, "Invalid CT family '%s'.", family);
9ff721
-			return NULL;
9ff721
-		}
9ff721
-	}
9ff721
+	familyval = json_parse_family(ctx, root);
9ff721
+	if (familyval < 0)
9ff721
+		return NULL;
9ff721
 
9ff721
 	if (!json_unpack(root, "{s:s}", "dir", &dir)) {
9ff721
 		if (!strcmp(dir, "original")) {
9ff721
@@ -1562,7 +1577,6 @@ static struct stmt *json_parse_fwd_stmt(struct json_ctx *ctx,
9ff721
 					const char *key, json_t *value)
9ff721
 {
9ff721
 	json_t *jaddr, *jdev;
9ff721
-	const char *family;
9ff721
 	struct stmt *stmt;
9ff721
 	int familyval;
9ff721
 
9ff721
@@ -1577,21 +1591,15 @@ static struct stmt *json_parse_fwd_stmt(struct json_ctx *ctx,
9ff721
 		goto out_err;
9ff721
 	}
9ff721
 
9ff721
-	if (json_unpack(value, "{s:s, s:o}",
9ff721
-			"family", &family, "addr", &jaddr))
9ff721
-		return stmt;
9ff721
-
9ff721
-	familyval = parse_family(family);
9ff721
-	switch (familyval) {
9ff721
-	case NFPROTO_IPV4:
9ff721
-	case NFPROTO_IPV6:
9ff721
-		stmt->fwd.family = familyval;
9ff721
-		break;
9ff721
-	default:
9ff721
-		json_error(ctx, "Invalid fwd family value '%s'.", family);
9ff721
+	familyval = json_parse_family(ctx, value);
9ff721
+	if (familyval < 0)
9ff721
 		goto out_err;
9ff721
-	}
9ff721
 
9ff721
+	if (familyval == NFPROTO_UNSPEC ||
9ff721
+	    json_unpack(value, "{s:o}", "addr", &jaddr))
9ff721
+		return stmt;
9ff721
+
9ff721
+	stmt->fwd.family = familyval;
9ff721
 	stmt->fwd.addr = json_parse_stmt_expr(ctx, jaddr);
9ff721
 	if (!stmt->fwd.addr) {
9ff721
 		json_error(ctx, "Invalid fwd addr value.");
9ff721
@@ -2137,8 +2145,7 @@ static struct cmd *json_parse_cmd_add_table(struct json_ctx *ctx, json_t *root,
9ff721
 		json_error(ctx, "Either name or handle required to delete a table.");
9ff721
 		return NULL;
9ff721
 	}
9ff721
-	h.family = parse_family(family);
9ff721
-	if (h.family < 0) {
9ff721
+	if (parse_family(family, &h.family)) {
9ff721
 		json_error(ctx, "Unknown family '%s'.", family);
9ff721
 		return NULL;
9ff721
 	}
9ff721
@@ -2178,8 +2185,7 @@ static struct cmd *json_parse_cmd_add_chain(struct json_ctx *ctx, json_t *root,
9ff721
 		json_error(ctx, "Either name or handle required to delete a chain.");
9ff721
 		return NULL;
9ff721
 	}
9ff721
-	h.family = parse_family(family);
9ff721
-	if (h.family < 0) {
9ff721
+	if (parse_family(family, &h.family)) {
9ff721
 		json_error(ctx, "Unknown family '%s'.", family);
9ff721
 		return NULL;
9ff721
 	}
9ff721
@@ -2240,8 +2246,7 @@ static struct cmd *json_parse_cmd_add_rule(struct json_ctx *ctx, json_t *root,
9ff721
 		 json_unpack_err(ctx, root, "{s:I}", "handle", &h.handle.id))
9ff721
 		return NULL;
9ff721
 
9ff721
-	h.family = parse_family(family);
9ff721
-	if (h.family < 0) {
9ff721
+	if (parse_family(family, &h.family)) {
9ff721
 		json_error(ctx, "Unknown family '%s'.", family);
9ff721
 		return NULL;
9ff721
 	}
9ff721
@@ -2347,8 +2352,7 @@ static struct cmd *json_parse_cmd_add_set(struct json_ctx *ctx, json_t *root,
9ff721
 		return NULL;
9ff721
 	}
9ff721
 
9ff721
-	h.family = parse_family(family);
9ff721
-	if (h.family < 0) {
9ff721
+	if (parse_family(family, &h.family)) {
9ff721
 		json_error(ctx, "Unknown family '%s'.", family);
9ff721
 		return NULL;
9ff721
 	}
9ff721
@@ -2459,8 +2463,7 @@ static struct cmd *json_parse_cmd_add_element(struct json_ctx *ctx,
9ff721
 			    "elem", &tmp))
9ff721
 		return NULL;
9ff721
 
9ff721
-	h.family = parse_family(family);
9ff721
-	if (h.family < 0) {
9ff721
+	if (parse_family(family, &h.family)) {
9ff721
 		json_error(ctx, "Unknown family '%s'.", family);
9ff721
 		return NULL;
9ff721
 	}
9ff721
@@ -2523,8 +2526,7 @@ static struct cmd *json_parse_cmd_add_flowtable(struct json_ctx *ctx,
9ff721
 			    "name", &h.flowtable))
9ff721
 		return NULL;
9ff721
 
9ff721
-	h.family = parse_family(family);
9ff721
-	if (h.family < 0) {
9ff721
+	if (parse_family(family, &h.family)) {
9ff721
 		json_error(ctx, "Unknown family '%s'.", family);
9ff721
 		return NULL;
9ff721
 	}
9ff721
@@ -2568,6 +2570,7 @@ static struct cmd *json_parse_cmd_add_object(struct json_ctx *ctx,
9ff721
 					     enum cmd_obj cmd_obj)
9ff721
 {
9ff721
 	const char *family, *tmp, *rate_unit = "packets", *burst_unit = "bytes";
9ff721
+	uint32_t l3proto = NFPROTO_IPV4;
9ff721
 	struct handle h = { 0 };
9ff721
 	struct obj *obj;
9ff721
 	int inv = 0;
9ff721
@@ -2588,8 +2591,7 @@ static struct cmd *json_parse_cmd_add_object(struct json_ctx *ctx,
9ff721
 		return NULL;
9ff721
 	}
9ff721
 
9ff721
-	h.family = parse_family(family);
9ff721
-	if (h.family < 0) {
9ff721
+	if (parse_family(family, &h.family)) {
9ff721
 		json_error(ctx, "Unknown family '%s'.", family);
9ff721
 		return NULL;
9ff721
 	}
9ff721
@@ -2647,18 +2649,13 @@ static struct cmd *json_parse_cmd_add_object(struct json_ctx *ctx,
9ff721
 				return NULL;
9ff721
 			}
9ff721
 		}
9ff721
-		if (!json_unpack(root, "{s:s}", "l3proto", &tmp)) {
9ff721
-			int family = parse_family(tmp);
9ff721
-
9ff721
-			if (family < 0) {
9ff721
-				json_error(ctx, "Invalid ct helper l3proto '%s'.", tmp);
9ff721
-				obj_free(obj);
9ff721
-				return NULL;
9ff721
-			}
9ff721
-			obj->ct_helper.l3proto = family;
9ff721
-		} else {
9ff721
-			obj->ct_helper.l3proto = NFPROTO_IPV4;
9ff721
+		if (!json_unpack(root, "{s:s}", "l3proto", &tmp) &&
9ff721
+		    parse_family(tmp, &l3proto)) {
9ff721
+			json_error(ctx, "Invalid ct helper l3proto '%s'.", tmp);
9ff721
+			obj_free(obj);
9ff721
+			return NULL;
9ff721
 		}
9ff721
+		obj->ct_helper.l3proto = l3proto;
9ff721
 		break;
9ff721
 	case CMD_OBJ_LIMIT:
9ff721
 		obj->type = NFT_OBJECT_LIMIT;
9ff721
@@ -2770,8 +2767,7 @@ static struct cmd *json_parse_cmd_replace(struct json_ctx *ctx,
9ff721
 		h.handle.id = 0;
9ff721
 	}
9ff721
 
9ff721
-	h.family = parse_family(family);
9ff721
-	if (h.family < 0) {
9ff721
+	if (parse_family(family, &h.family)) {
9ff721
 		json_error(ctx, "Unknown family '%s'.", family);
9ff721
 		return NULL;
9ff721
 	}
9ff721
@@ -2825,8 +2821,7 @@ static struct cmd *json_parse_cmd_list_multiple(struct json_ctx *ctx,
9ff721
 	const char *tmp;
9ff721
 
9ff721
 	if (!json_unpack(root, "{s:s}", "family", &tmp)) {
9ff721
-		h.family = parse_family(tmp);
9ff721
-		if (h.family < 0) {
9ff721
+		if (parse_family(tmp, &h.family)) {
9ff721
 			json_error(ctx, "Unknown family '%s'.", tmp);
9ff721
 			return NULL;
9ff721
 		}
9ff721
@@ -2981,8 +2976,7 @@ static struct cmd *json_parse_cmd_rename(struct json_ctx *ctx,
9ff721
 			    "name", &h.chain.name,
9ff721
 			    "newname", &newname))
9ff721
 		return NULL;
9ff721
-	h.family = parse_family(family);
9ff721
-	if (h.family < 0) {
9ff721
+	if (parse_family(family, &h.family)) {
9ff721
 		json_error(ctx, "Unknown family '%s'.", family);
9ff721
 		return NULL;
9ff721
 	}
9ff721
-- 
bacbc8
2.21.0
9ff721