Blame SOURCES/0012-netlink_delinearize-Fix-resource-leaks.patch

a05102
From 5e9e2dc7e972f6bbbc0156ad97b4ee9d11fcb837 Mon Sep 17 00:00:00 2001
a05102
From: Phil Sutter <psutter@redhat.com>
a05102
Date: Wed, 20 Jun 2018 09:22:47 +0200
a05102
Subject: [PATCH] netlink_delinearize: Fix resource leaks
a05102
a05102
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1504157
a05102
Upstream Status: nftables commit 671851617c8d8
a05102
a05102
commit 671851617c8d8c1dfe9822eee8dcc7b827fff850
a05102
Author: Phil Sutter <phil@nwl.cc>
a05102
Date:   Thu Mar 1 15:00:32 2018 +0100
a05102
a05102
    netlink_delinearize: Fix resource leaks
a05102
a05102
    Most of the cases are basically the same: Error path fails to free the
a05102
    previously allocated statement or expression. A few cases received
a05102
    special treatment though:
a05102
a05102
    - In netlink_parse_payload_stmt(), the leak is easily avoided by code
a05102
      reordering.
a05102
a05102
    - In netlink_parse_exthdr(), there's no point in introducing a goto
a05102
      label since there is but a single affected error check.
a05102
a05102
    - In netlink_parse_hash() non-error path leaked as well if sreg
a05102
      contained a concatenated expression.
a05102
a05102
    Signed-off-by: Phil Sutter <phil@nwl.cc>
a05102
    Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
a05102
---
a05102
 src/netlink_delinearize.c | 144 +++++++++++++++++++++++++++++-----------------
a05102
 1 file changed, 92 insertions(+), 52 deletions(-)
a05102
a05102
diff --git a/src/netlink_delinearize.c b/src/netlink_delinearize.c
a05102
index 61cba52..e25160a 100644
a05102
--- a/src/netlink_delinearize.c
a05102
+++ b/src/netlink_delinearize.c
a05102
@@ -470,15 +470,15 @@ static void netlink_parse_payload_stmt(struct netlink_parse_ctx *ctx,
a05102
 	offset = nftnl_expr_get_u32(nle, NFTNL_EXPR_PAYLOAD_OFFSET) * BITS_PER_BYTE;
a05102
 	len    = nftnl_expr_get_u32(nle, NFTNL_EXPR_PAYLOAD_LEN) * BITS_PER_BYTE;
a05102
 
a05102
-	expr = payload_expr_alloc(loc, NULL, 0);
a05102
-	payload_init_raw(expr, base, offset, len);
a05102
-
a05102
 	sreg = netlink_parse_register(nle, NFTNL_EXPR_PAYLOAD_SREG);
a05102
 	val  = netlink_get_register(ctx, loc, sreg);
a05102
 	if (val == NULL)
a05102
 		return netlink_error(ctx, loc,
a05102
 				     "payload statement has no expression");
a05102
 
a05102
+	expr = payload_expr_alloc(loc, NULL, 0);
a05102
+	payload_init_raw(expr, base, offset, len);
a05102
+
a05102
 	stmt = payload_stmt_alloc(loc, expr, val);
a05102
 
a05102
 	list_add_tail(&stmt->list, &ctx->rule->stmts);
a05102
@@ -523,9 +523,11 @@ static void netlink_parse_exthdr(struct netlink_parse_ctx *ctx,
a05102
 
a05102
 		sreg = netlink_parse_register(nle, NFTNL_EXPR_EXTHDR_SREG);
a05102
 		val = netlink_get_register(ctx, loc, sreg);
a05102
-		if (val == NULL)
a05102
+		if (val == NULL) {
a05102
+			xfree(expr);
a05102
 			return netlink_error(ctx, loc,
a05102
 					     "exthdr statement has no expression");
a05102
+		}
a05102
 
a05102
 		expr_set_type(val, expr->dtype, expr->byteorder);
a05102
 
a05102
@@ -556,22 +558,27 @@ static void netlink_parse_hash(struct netlink_parse_ctx *ctx,
a05102
 		sreg = netlink_parse_register(nle, NFTNL_EXPR_HASH_SREG);
a05102
 		hexpr = netlink_get_register(ctx, loc, sreg);
a05102
 
a05102
-		if (hexpr == NULL)
a05102
-			return
a05102
+		if (hexpr == NULL) {
a05102
 			netlink_error(ctx, loc,
a05102
 				      "hash statement has no expression");
a05102
+			goto out_err;
a05102
+		}
a05102
 		len = nftnl_expr_get_u32(nle,
a05102
 					 NFTNL_EXPR_HASH_LEN) * BITS_PER_BYTE;
a05102
 		if (hexpr->len < len) {
a05102
+			xfree(hexpr);
a05102
 			hexpr = netlink_parse_concat_expr(ctx, loc, sreg, len);
a05102
 			if (hexpr == NULL)
a05102
-				return;
a05102
+				goto out_err;
a05102
 		}
a05102
 		expr->hash.expr = hexpr;
a05102
 	}
a05102
 
a05102
 	dreg = netlink_parse_register(nle, NFTNL_EXPR_HASH_DREG);
a05102
 	netlink_set_register(ctx, dreg, expr);
a05102
+	return;
a05102
+out_err:
a05102
+	xfree(expr);
a05102
 }
a05102
 
a05102
 static void netlink_parse_fib(struct netlink_parse_ctx *ctx,
a05102
@@ -853,10 +860,11 @@ static void netlink_parse_nat(struct netlink_parse_ctx *ctx,
a05102
 	reg1 = netlink_parse_register(nle, NFTNL_EXPR_NAT_REG_ADDR_MIN);
a05102
 	if (reg1) {
a05102
 		addr = netlink_get_register(ctx, loc, reg1);
a05102
-		if (addr == NULL)
a05102
-			return netlink_error(ctx, loc,
a05102
-					     "NAT statement has no address "
a05102
-					     "expression");
a05102
+		if (addr == NULL) {
a05102
+			netlink_error(ctx, loc,
a05102
+				      "NAT statement has no address expression");
a05102
+			goto out_err;
a05102
+		}
a05102
 
a05102
 		if (family == AF_INET)
a05102
 			expr_set_type(addr, &ipaddr_type, BYTEORDER_BIG_ENDIAN);
a05102
@@ -869,10 +877,11 @@ static void netlink_parse_nat(struct netlink_parse_ctx *ctx,
a05102
 	reg2 = netlink_parse_register(nle, NFTNL_EXPR_NAT_REG_ADDR_MAX);
a05102
 	if (reg2 && reg2 != reg1) {
a05102
 		addr = netlink_get_register(ctx, loc, reg2);
a05102
-		if (addr == NULL)
a05102
-			return netlink_error(ctx, loc,
a05102
-					     "NAT statement has no address "
a05102
-					     "expression");
a05102
+		if (addr == NULL) {
a05102
+			netlink_error(ctx, loc,
a05102
+				      "NAT statement has no address expression");
a05102
+			goto out_err;
a05102
+		}
a05102
 
a05102
 		if (family == AF_INET)
a05102
 			expr_set_type(addr, &ipaddr_type, BYTEORDER_BIG_ENDIAN);
a05102
@@ -887,10 +896,11 @@ static void netlink_parse_nat(struct netlink_parse_ctx *ctx,
a05102
 	reg1 = netlink_parse_register(nle, NFTNL_EXPR_NAT_REG_PROTO_MIN);
a05102
 	if (reg1) {
a05102
 		proto = netlink_get_register(ctx, loc, reg1);
a05102
-		if (proto == NULL)
a05102
-			return netlink_error(ctx, loc,
a05102
-					     "NAT statement has no proto "
a05102
-					     "expression");
a05102
+		if (proto == NULL) {
a05102
+			netlink_error(ctx, loc,
a05102
+				      "NAT statement has no proto expression");
a05102
+			goto out_err;
a05102
+		}
a05102
 
a05102
 		expr_set_type(proto, &inet_service_type, BYTEORDER_BIG_ENDIAN);
a05102
 		stmt->nat.proto = proto;
a05102
@@ -899,10 +909,11 @@ static void netlink_parse_nat(struct netlink_parse_ctx *ctx,
a05102
 	reg2 = netlink_parse_register(nle, NFTNL_EXPR_NAT_REG_PROTO_MAX);
a05102
 	if (reg2 && reg2 != reg1) {
a05102
 		proto = netlink_get_register(ctx, loc, reg2);
a05102
-		if (proto == NULL)
a05102
-			return netlink_error(ctx, loc,
a05102
-					     "NAT statement has no proto "
a05102
-					     "expression");
a05102
+		if (proto == NULL) {
a05102
+			netlink_error(ctx, loc,
a05102
+				      "NAT statement has no proto expression");
a05102
+			goto out_err;
a05102
+		}
a05102
 
a05102
 		expr_set_type(proto, &inet_service_type, BYTEORDER_BIG_ENDIAN);
a05102
 		if (stmt->nat.proto != NULL)
a05102
@@ -911,6 +922,9 @@ static void netlink_parse_nat(struct netlink_parse_ctx *ctx,
a05102
 	}
a05102
 
a05102
 	ctx->stmt = stmt;
a05102
+	return;
a05102
+out_err:
a05102
+	xfree(stmt);
a05102
 }
a05102
 
a05102
 static void netlink_parse_masq(struct netlink_parse_ctx *ctx,
a05102
@@ -931,10 +945,11 @@ static void netlink_parse_masq(struct netlink_parse_ctx *ctx,
a05102
 	reg1 = netlink_parse_register(nle, NFTNL_EXPR_MASQ_REG_PROTO_MIN);
a05102
 	if (reg1) {
a05102
 		proto = netlink_get_register(ctx, loc, reg1);
a05102
-		if (proto == NULL)
a05102
-			return netlink_error(ctx, loc,
a05102
-					     "MASQUERADE statement"
a05102
-					     "has no proto expression");
a05102
+		if (proto == NULL) {
a05102
+			netlink_error(ctx, loc,
a05102
+				      "MASQUERADE statement has no proto expression");
a05102
+			goto out_err;
a05102
+		}
a05102
 		expr_set_type(proto, &inet_service_type, BYTEORDER_BIG_ENDIAN);
a05102
 		stmt->masq.proto = proto;
a05102
 	}
a05102
@@ -942,10 +957,11 @@ static void netlink_parse_masq(struct netlink_parse_ctx *ctx,
a05102
 	reg2 = netlink_parse_register(nle, NFTNL_EXPR_MASQ_REG_PROTO_MAX);
a05102
 	if (reg2 && reg2 != reg1) {
a05102
 		proto = netlink_get_register(ctx, loc, reg2);
a05102
-		if (proto == NULL)
a05102
-			return netlink_error(ctx, loc,
a05102
-					     "MASQUERADE statement"
a05102
-					     "has no proto expression");
a05102
+		if (proto == NULL) {
a05102
+			netlink_error(ctx, loc,
a05102
+				      "MASQUERADE statement has no proto expression");
a05102
+			goto out_err;
a05102
+		}
a05102
 		expr_set_type(proto, &inet_service_type, BYTEORDER_BIG_ENDIAN);
a05102
 		if (stmt->masq.proto != NULL)
a05102
 			proto = range_expr_alloc(loc, stmt->masq.proto, proto);
a05102
@@ -953,6 +969,9 @@ static void netlink_parse_masq(struct netlink_parse_ctx *ctx,
a05102
 	}
a05102
 
a05102
 	ctx->stmt = stmt;
a05102
+	return;
a05102
+out_err:
a05102
+	xfree(stmt);
a05102
 }
a05102
 
a05102
 static void netlink_parse_redir(struct netlink_parse_ctx *ctx,
a05102
@@ -974,10 +993,11 @@ static void netlink_parse_redir(struct netlink_parse_ctx *ctx,
a05102
 	reg1 = netlink_parse_register(nle, NFTNL_EXPR_REDIR_REG_PROTO_MIN);
a05102
 	if (reg1) {
a05102
 		proto = netlink_get_register(ctx, loc, reg1);
a05102
-		if (proto == NULL)
a05102
-			return netlink_error(ctx, loc,
a05102
-					     "redirect statement has no proto "
a05102
-					     "expression");
a05102
+		if (proto == NULL) {
a05102
+			netlink_error(ctx, loc,
a05102
+				      "redirect statement has no proto expression");
a05102
+			goto out_err;
a05102
+		}
a05102
 
a05102
 		expr_set_type(proto, &inet_service_type, BYTEORDER_BIG_ENDIAN);
a05102
 		stmt->redir.proto = proto;
a05102
@@ -986,10 +1006,11 @@ static void netlink_parse_redir(struct netlink_parse_ctx *ctx,
a05102
 	reg2 = netlink_parse_register(nle, NFTNL_EXPR_REDIR_REG_PROTO_MAX);
a05102
 	if (reg2 && reg2 != reg1) {
a05102
 		proto = netlink_get_register(ctx, loc, reg2);
a05102
-		if (proto == NULL)
a05102
-			return netlink_error(ctx, loc,
a05102
-					     "redirect statement has no proto "
a05102
-					     "expression");
a05102
+		if (proto == NULL) {
a05102
+			netlink_error(ctx, loc,
a05102
+				      "redirect statement has no proto expression");
a05102
+			goto out_err;
a05102
+		}
a05102
 
a05102
 		expr_set_type(proto, &inet_service_type, BYTEORDER_BIG_ENDIAN);
a05102
 		if (stmt->redir.proto != NULL)
a05102
@@ -999,6 +1020,9 @@ static void netlink_parse_redir(struct netlink_parse_ctx *ctx,
a05102
 	}
a05102
 
a05102
 	ctx->stmt = stmt;
a05102
+	return;
a05102
+out_err:
a05102
+	xfree(stmt);
a05102
 }
a05102
 
a05102
 static void netlink_parse_dup(struct netlink_parse_ctx *ctx,
a05102
@@ -1014,9 +1038,11 @@ static void netlink_parse_dup(struct netlink_parse_ctx *ctx,
a05102
 	reg1 = netlink_parse_register(nle, NFTNL_EXPR_DUP_SREG_ADDR);
a05102
 	if (reg1) {
a05102
 		addr = netlink_get_register(ctx, loc, reg1);
a05102
-		if (addr == NULL)
a05102
-			return netlink_error(ctx, loc,
a05102
-					     "DUP statement has no destination expression");
a05102
+		if (addr == NULL) {
a05102
+			netlink_error(ctx, loc,
a05102
+				      "DUP statement has no destination expression");
a05102
+			goto out_err;
a05102
+		}
a05102
 
a05102
 		switch (ctx->table->handle.family) {
a05102
 		case NFPROTO_IPV4:
a05102
@@ -1033,9 +1059,11 @@ static void netlink_parse_dup(struct netlink_parse_ctx *ctx,
a05102
 	reg2 = netlink_parse_register(nle, NFTNL_EXPR_DUP_SREG_DEV);
a05102
 	if (reg2) {
a05102
 		dev = netlink_get_register(ctx, loc, reg2);
a05102
-		if (dev == NULL)
a05102
-			return netlink_error(ctx, loc,
a05102
-					     "DUP statement has no output expression");
a05102
+		if (dev == NULL) {
a05102
+			netlink_error(ctx, loc,
a05102
+				      "DUP statement has no output expression");
a05102
+			goto out_err;
a05102
+		}
a05102
 
a05102
 		expr_set_type(dev, &ifindex_type, BYTEORDER_HOST_ENDIAN);
a05102
 		if (stmt->dup.to == NULL)
a05102
@@ -1045,6 +1073,9 @@ static void netlink_parse_dup(struct netlink_parse_ctx *ctx,
a05102
 	}
a05102
 
a05102
 	ctx->stmt = stmt;
a05102
+	return;
a05102
+out_err:
a05102
+	xfree(stmt);
a05102
 }
a05102
 
a05102
 static void netlink_parse_fwd(struct netlink_parse_ctx *ctx,
a05102
@@ -1060,15 +1091,20 @@ static void netlink_parse_fwd(struct netlink_parse_ctx *ctx,
a05102
 	reg1 = netlink_parse_register(nle, NFTNL_EXPR_FWD_SREG_DEV);
a05102
 	if (reg1) {
a05102
 		dev = netlink_get_register(ctx, loc, reg1);
a05102
-		if (dev == NULL)
a05102
-			return netlink_error(ctx, loc,
a05102
-					     "fwd statement has no output expression");
a05102
+		if (dev == NULL) {
a05102
+			netlink_error(ctx, loc,
a05102
+				      "fwd statement has no output expression");
a05102
+			goto out_err;
a05102
+		}
a05102
 
a05102
 		expr_set_type(dev, &ifindex_type, BYTEORDER_HOST_ENDIAN);
a05102
 		stmt->fwd.to = dev;
a05102
 	}
a05102
 
a05102
 	ctx->stmt = stmt;
a05102
+	return;
a05102
+out_err:
a05102
+	xfree(stmt);
a05102
 }
a05102
 
a05102
 static void netlink_parse_queue(struct netlink_parse_ctx *ctx,
a05102
@@ -1135,10 +1171,11 @@ static void netlink_parse_dynset(struct netlink_parse_ctx *ctx,
a05102
 	dnle = nftnl_expr_get(nle, NFTNL_EXPR_DYNSET_EXPR, NULL);
a05102
 	if (dnle != NULL) {
a05102
 		if (netlink_parse_expr(dnle, ctx) < 0)
a05102
-			return;
a05102
-		if (ctx->stmt == NULL)
a05102
-			return netlink_error(ctx, loc,
a05102
-					     "Could not parse dynset stmt");
a05102
+			goto out_err;
a05102
+		if (ctx->stmt == NULL) {
a05102
+			netlink_error(ctx, loc, "Could not parse dynset stmt");
a05102
+			goto out_err;
a05102
+		}
a05102
 		dstmt = ctx->stmt;
a05102
 	}
a05102
 
a05102
@@ -1155,6 +1192,9 @@ static void netlink_parse_dynset(struct netlink_parse_ctx *ctx,
a05102
 	}
a05102
 
a05102
 	ctx->stmt = stmt;
a05102
+	return;
a05102
+out_err:
a05102
+	xfree(expr);
a05102
 }
a05102
 
a05102
 static void netlink_parse_objref(struct netlink_parse_ctx *ctx,
a05102
-- 
a05102
1.8.3.1
a05102