Skip to contentSkip to conversation

libnftables: NULL pointer dereference in nft_expr_ops_lookup

BUG Applied Jiri Benc 2 participants 4 messages Oct 25, 2013 – Oct 27, 2013 netfilter

lore
Share & tools
lore

Jiri Benc

author
#0 lore raw

Hi,

using both the latest (git) libnftables and nftables, I got:

[root@localhost ~]# nft add rule ip filter input tcp dport 81 reject Segmentation fault (core dumped)

(gdb) bt

#0  0x00007fb7890c1364 in __strcmp_sse2 () from /lib64/libc.so.6
#1  0x00007fb78960bfa4 in nft_expr_ops_lookup (name=name@entry=0x0) at expr_ops.c:18
#2  0x00007fb78960bc56 in nft_rule_expr_alloc (name=name@entry=0x0) at expr.c:34
#3  0x000000000040db29 in alloc_nft_expr (name=name@entry=0x0) at src/netlink.c:118
#4  0x0000000000410117 in netlink_gen_reject_stmt (ctx=<optimized out>, stmt=<optimized out>)

at src/netlink_linearize.c:564
#5 netlink_gen_stmt (stmt=0xb106f0, ctx=0x7fff734c4690) at src/netlink_linearize.c:651
#6 netlink_linearize_rule (ctx=ctx@entry=0x7fff734c4730, nlr=nlr@entry=0xb10520, rule=rule@entry=0xb10760)
at src/netlink_linearize.c:670
#7 0x000000000040e25b in netlink_add_rule_batch (ctx=0x7fff734c4730, h=<optimized out>, rule=0xb10760, flags=2048)

    at src/netlink.c:320
#8  0x00000000004056ec in nft_netlink (msgs=0x7fff734c48b0, state=0x7fff734c48c0) at src/main.c:167
#9  nft_run (scanner=scanner@entry=0xb102c0, state=state@entry=0x7fff734c48c0, msgs=msgs@entry=0x7fff734c48b0)
    at src/main.c:214

#10 0x00000000004052e5 in main (argc=10, argv=<optimized out>) at src/main.c:321

Looking into the code, netlink_gen_reject_stmt calls alloc_nft_expr(NULL), the NULL propagates to strcmp in nft_expr_ops_lookup.

Sorry for not providing a patch, I'm only starting looking into the code and this seems to require at least certain degree of understanding those functions.

Jiri

Signature · 1 line
Jiri Benc
#1 lore raw

Hi Jiri,

Quoted 22 lines · Jiri Benc

Hi,

using both the latest (git) libnftables and nftables, I got:

[root@localhost ~]# nft add rule ip filter input tcp dport 81 reject Segmentation fault (core dumped)

(gdb) bt

#0  0x00007fb7890c1364 in __strcmp_sse2 () from /lib64/libc.so.6
#1  0x00007fb78960bfa4 in nft_expr_ops_lookup (name=name@entry=0x0) at expr_ops.c:18
#2  0x00007fb78960bc56 in nft_rule_expr_alloc (name=name@entry=0x0) at expr.c:34
#3  0x000000000040db29 in alloc_nft_expr (name=name@entry=0x0) at src/netlink.c:118
#4  0x0000000000410117 in netlink_gen_reject_stmt (ctx=<optimized out>, stmt=<optimized out>)

at src/netlink_linearize.c:564
#5 netlink_gen_stmt (stmt=0xb106f0, ctx=0x7fff734c4690) at src/netlink_linearize.c:651
#6 netlink_linearize_rule (ctx=ctx@entry=0x7fff734c4730, nlr=nlr@entry=0xb10520, rule=rule@entry=0xb10760)
at src/netlink_linearize.c:670
#7 0x000000000040e25b in netlink_add_rule_batch (ctx=0x7fff734c4730, h=<optimized out>, rule=0xb10760, flags=2048)

    at src/netlink.c:320
#8  0x00000000004056ec in nft_netlink (msgs=0x7fff734c48b0, state=0x7fff734c48c0) at src/main.c:167
#9  nft_run (scanner=scanner@entry=0xb102c0, state=state@entry=0x7fff734c48c0, msgs=msgs@entry=0x7fff734c48b0)
    at src/main.c:214
Quoted 6 lines

#10 0x00000000004052e5 in main (argc=10, argv=<optimized out>) at src/main.c:321

Looking into the code, netlink_gen_reject_stmt calls alloc_nft_expr(NULL), the NULL propagates to strcmp in nft_expr_ops_lookup.

reject support was never finished. Please, find enclosed patches for libnftables and nft.

ICMP code support is still missing, perhaps you want to investigate how to add it to nft. It should be a small follow up patch.

Quoted 1 line

From f619e81d6cc10caec8a9c5ff1b0e4a8ea081646d Mon Sep 17 00:00:00 2001

From: Pablo Neira Ayuso <[email protected]>
Date: Fri, 25 Oct 2013 16:48:36 +0200
Subject: [PATCH libnftables] expr: add reject

This patch adds support for the reject expression.

---
 include/libnftables/expr.h |    5 ++
 src/Makefile.am            |    1 +
 src/expr/reject.c          |  213 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 219 insertions(+)
 create mode 100644 src/expr/reject.c
include/libnftables/expr.h +5 -0
index 232a810..64c407c 100644
@@ -137,6 +137,11 @@ enum {
 	NFT_EXPR_LIMIT_UNIT,
 };
 
+enum {
+	NFT_EXPR_REJECT_TYPE	= NFT_RULE_EXPR_ATTR_BASE,
+	NFT_EXPR_REJECT_CODE,
+};
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
src/Makefile.am +1 -0
index 474dbf0..5f64db2 100644
@@ -30,6 +30,7 @@ libnftables_la_SOURCES = utils.c		\
 			 expr/meta.c		\
 			 expr/nat.c		\
 			 expr/payload.c		\
+			 expr/reject.c		\
 			 expr/target.c		\
 			 libnftables.map	\
 			 internal.h
src/expr/reject.c new file +213 -0

213 lines not on this page · raw message

Signature · 1 line
1.7.10.4
Quoted 1 line

From 41fbf2fd89a166bb3bb8d7d11bf790aa6011fcd1 Mon Sep 17 00:00:00 2001

From: Pablo Neira Ayuso <[email protected]>
Date: Fri, 25 Oct 2013 17:01:58 +0200
Subject: [PATCH nft] netlink_linearize: finish reject support

This patch finishes the reject support.

---
 src/netlink_linearize.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
src/netlink_linearize.c +3 -1
index c1d1a9a..96ffe68 100644
@@ -692,7 +692,9 @@ static void netlink_gen_reject_stmt(struct netlink_linearize_ctx *ctx,
 {
 	struct nft_rule_expr *nle;
 
-	nle = alloc_nft_expr(NULL);
+	nle = alloc_nft_expr("reject");
+	nft_rule_expr_set_u32(nle, NFT_EXPR_REJECT_TYPE, stmt->reject.type);
+	nft_rule_expr_set_u8(nle, NFT_EXPR_REJECT_CODE, 0);
 	nft_rule_add_expr(ctx->nlr, nle);
 }
 
Signature · 1 line
1.7.10.4
author Tested-by
#2 lore raw
Quoted 2 lines · Pablo Neira Ayuso

reject support was never finished. Please, find enclosed patches for libnftables and nft.

That was fast, thanks! Seems to work.

Quoted 2 lines

ICMP code support is still missing, perhaps you want to investigate how to add it to nft. It should be a small follow up patch.

Yes, that would be useful. I may look into that but no promises as of when. Feel free to beat me.

Jiri

Signature · 1 line
Jiri Benc
maintainer Applied
#3 lore raw
Quoted 7 lines · Jiri Benc
Quoted 2 lines · Pablo Neira Ayuso

reject support was never finished. Please, find enclosed patches for libnftables and nft.

That was fast, thanks! Seems to work.

Applied, thanks.