netfilter: nft_ct: fix OOB in NFT_CT_SRC/DST eval
Subsystems (2)
PATCH 1/1 netfilter: nft_ct: fix OOB in NFT_CT_SRC/DST eval
I noticed this issue while looking at a historic syzbot report [1].
syzbot forces dreg[19] to be used as the storage for the ipv4 address, together with a raw priority chain, which makes nf_ct_l3num(ct) be 0 so that 16 bytes get copied into dreg[19]. Even when the dreg is not [19], the same larger-than-expected copy can clobber other regs.
I am not sure whether there are other paths; here we add a check to fix the deprecated NFT_CT_SRC and NFT_CT_DST branches.
[1]: https://syzkaller.appspot.com/bug?id=389cf09cb72926114fce90dc85a2c3231dcb647c
--- net/netfilter/nft_ct.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
net/netfilter/nft_ct.c +8 -4
@@ -61,6 +61,7 @@ static void nft_ct_get_eval(const struct nft_expr *expr,const struct nf_conntrack_tuple *tuple;const struct nf_conntrack_helper *helper;unsigned int state; +u8 addr_len; ct =nf_ct_get (pkt->skb, &ctinfo);
@@ -178,14 +179,17 @@ static void nft_ct_get_eval(const struct nft_expr *expr, } tuple = &ct->tuplehash[priv->dir].tuple; + addr_len =nf_ct_l3num (ct) ==NFPROTO_IPV4 ?4 :16 ;switch (priv->key) {case NFT_CT_SRC : -memcpy (dest, tuple->src.u3.all, -nf_ct_l3num (ct) ==NFPROTO_IPV4 ?4 :16 ); +if (priv->len != addr_len) + goto err; +memcpy (dest, tuple->src.u3.all, addr_len);return ;case NFT_CT_DST : -memcpy (dest, tuple->dst.u3.all, -nf_ct_l3num (ct) ==NFPROTO_IPV4 ?4 :16 ); +if (priv->len != addr_len) + goto err; +memcpy (dest, tuple->dst.u3.all, addr_len);return ;case NFT_CT_PROTO_SRC :nft_reg_store16 (dest, (__force u16 )tuple->src.u.all);
Signature · 1 line
2.43.0
Florian Westphal
maintainerQuoted 1 line
which makes
nf_ct_l3num(ct)be 0
How?
Florian Westphal
maintainerQuoted 3 lines · Florian Westphal
Quoted 1 line
which makes
nf_ct_l3num(ct)be 0How?
Wild guess:
net/netfilter/nft_ct.c +1 -1
@@ -78,7 +78,7 @@ static void nft_ct_get_eval(const struct nft_expr *expr,break ; } -if (ct ==NULL ) +if (!ct ||nf_ct_is_template (ct)) goto err;switch (priv->key) {
net/netfilter/nft_ct_fast.c +1 -1
@@ -30,7 +30,7 @@ void nft_ct_get_fast_eval(const struct nft_expr *expr,break ; } -if (!ct) { +if (!ct ||nf_ct_is_template (ct)) { regs->verdict.code =NFT_BREAK ;return ; }
.... might also make sense to invertnf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16), i.e.:nf_ct_l3num(ct) == NFPROTO_IPV6 ? 16 : 4);
Quoted 3 lines · Florian Westphal
Quoted 2 lines · Florian Westphal
Quoted 1 line
which makes
nf_ct_l3num(ct)be 0How?
Yes, it's the template ct path. The triggering rule is e.g.:
table ip t {
chain pre {
type filter hook prerouting priority raw; ct zone set 1
ct original saddr 1.2.3.4 accept }
}
Quoted 19 lines
Wild guess:
net/netfilter/nft_ct.c +1
-1@@ -78,7 +78,7 @@ static void nft_ct_get_eval(const struct nft_expr *expr,break ; } -if (ct ==NULL ) +if (!ct ||nf_ct_is_template (ct)) goto err;switch (priv->key) {
net/netfilter/nft_ct_fast.c void nft_ct_get_fast_eval(const struct nft_expr *expr, · line 36
Quoted 8 lines
net/netfilter/nft_ct_fast.c +1
-1@@ -30,7 +30,7 @@ void nft_ct_get_fast_eval(const struct nft_expr *expr,break ; } -if (!ct) { +if (!ct ||nf_ct_is_template (ct)) { regs->verdict.code =NFT_BREAK ;return ; }
It looks more general and also covers the other GET keys that would equally misbehave on a template.
Quoted 3 lines
.... might also make sense to invert
nf_ct_l3num(ct)==NFPROTO_IPV4? 4 : 16), i.e.:nf_ct_l3num(ct)==NFPROTO_IPV6? 16 : 4);
As defense-in-depth, IIUC?
Florian Westphal
maintainerQuoted 17 lines · Jiayuan Chen
Quoted 17 lines
net/netfilter/nft_ct.c +1
-1@@ -78,7 +78,7 @@ static void nft_ct_get_eval(const struct nft_expr *expr,break ; } -if (ct ==NULL ) +if (!ct ||nf_ct_is_template (ct)) goto err;switch (priv->key) {net/netfilter/nft_ct_fast.c +0
-1@@ -30,7 +30,7 @@ void nft_ct_get_fast_eval(const struct nft_expr *expr,break ; } -if (!ct) {
Quoted 8 lines
net/netfilter/nft_ct_fast.c void nft_ct_get_fast_eval(const struct nft_expr *expr,
Quoted 5 lines
net/netfilter/nft_ct_fast.c +1
-0+if (!ct ||nf_ct_is_template (ct)) { regs->verdict.code =NFT_BREAK ;return ; }It looks more general and also covers the other GET keys that would equally misbehave on a template.
Would you mind sending a v2?
Quoted 5 lines
Quoted 3 lines
.... might also make sense to invert
nf_ct_l3num(ct)==NFPROTO_IPV4? 4 : 16), i.e.:nf_ct_l3num(ct)==NFPROTO_IPV6? 16 : 4);As defense-in-depth, IIUC?
Yes, alternatively merge your v1 with the template check. I don't see how we can ever have nf_ct_l3num(ct) != nft_pf(pkt) outside of the template bug.
Quoted 25 lines · Florian Westphal
Quoted 16 lines · Jiayuan Chen
Quoted 16 lines
net/netfilter/nft_ct.c +1
-1@@ -78,7 +78,7 @@ static void nft_ct_get_eval(const struct nft_expr *expr,break ; } -if (ct ==NULL ) +if (!ct ||nf_ct_is_template (ct)) goto err;switch (priv->key) {net/netfilter/nft_ct_fast.c +0
-0@@ -30,7 +30,7 @@ void nft_ct_get_fast_eval(const struct nft_expr *expr,break ; }Quoted 8 lines
net/netfilter/nft_ct_fast.c void nft_ct_get_fast_eval(const struct nft_expr *expr,
Quoted 6 lines
net/netfilter/nft_ct_fast.c +1
-1-if (!ct) { +if (!ct ||nf_ct_is_template (ct)) { regs->verdict.code =NFT_BREAK ;return ; }It looks more general and also covers the other GET keys that would equally misbehave on a template.
Quoted 1 line
Would you mind sending a v2?
No problem, will do.
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
.... might also make sense to invert
nf_ct_l3num(ct)==NFPROTO_IPV4? 4 : 16), i.e.:nf_ct_l3num(ct)==NFPROTO_IPV6? 16 : 4);As defense-in-depth, IIUC?
Yes, alternatively merge your v1 with the template check. I don't see how we can ever have
nf_ct_l3num(ct)!=nft_pf(pkt)outside of the template bug.
I think the template check plus the family check (nf_ct_l3num(ct) != nft_pf(pkt)) is enough as defense-in-depth.
Florian Westphal
maintainerQuoted 7 lines · Jiayuan Chen
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
nf_ct_l3num(ct)==NFPROTO_IPV6? 16 : 4);As defense-in-depth, IIUC?
Yes, alternatively merge your v1 with the template check. I don't see how we can ever have
nf_ct_l3num(ct)!=nft_pf(pkt)outside of the template bug.I think the template check plus the family check (
nf_ct_l3num(ct)!=nft_pf(pkt)) is enough as defense-in-depth.
Actually, I think we need to fix this to copy priv->len unconditionally. Or, alternatively, add a memcpy wrapper that zero-pads the remainder of the registers.
https://sashiko.dev/#/patchset/20260528042620.263828-1-jiayuan.chen%40linux.dev
"This is a pre-existing issue, but does copying only addr_len bytes when priv->len is larger leave the remainder of the register uninitialized? In nft_do_chain(), the register array is allocated on the kernel stack without zero-initialization. If priv->len is 16 and addr_len is 4, only the first 4 bytes are written."
Quoted 12 lines · Florian Westphal
Quoted 7 lines · Jiayuan Chen
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
nf_ct_l3num(ct)==NFPROTO_IPV6? 16 : 4);As defense-in-depth, IIUC?
Yes, alternatively merge your v1 with the template check. I don't see how we can ever have
nf_ct_l3num(ct)!=nft_pf(pkt)outside of the template bug.I think the template check plus the family check (
nf_ct_l3num(ct)!=nft_pf(pkt)) is enough as defense-in-depth.Actually, I think we need to fix this to copy
priv->lenunconditionally. Or, alternatively, add a memcpy wrapper that zero-pads the remainder of the registers.
Quoted 7 lines
https://sashiko.dev/#/patchset/20260528042620.263828-1-jiayuan.chen%40linux.dev
"This is a pre-existing issue, but does copying only
addr_lenbytes whenpriv->lenis larger leave the remainder of the register uninitialized? Innft_do_chain(), the register array is allocated on the kernel stack without zero-initialization. Ifpriv->lenis 16 andaddr_lenis 4, only the first 4 bytes are written."
I just spotted that too. I think copying priv->len unconditionally is enough -- tuple->{src,dst} is zeroed in nf_ct_get_tuple() before the protocol pkt_to_tuple callback fills in only the relevant leading bytes, so the trailing bytes of tuple->{src,dst}.u3.all are well-defined zeros and no wrapper is needed.
Florian Westphal
maintainerJiayuan Chen <[email protected]> wrote:
[ CC Pablo ]
Quoted 8 lines
Quoted 7 lines
https://sashiko.dev/#/patchset/20260528042620.263828-1-jiayuan.chen%40linux.dev
"This is a pre-existing issue, but does copying only
addr_lenbytes whenpriv->lenis larger leave the remainder of the register uninitialized? Innft_do_chain(), the register array is allocated on the kernel stack without zero-initialization. Ifpriv->lenis 16 andaddr_lenis 4, only the first 4 bytes are written."
Quoted 5 lines
I just spotted that too. I think copying
priv->lenunconditionally is enough -- tuple->{src,dst} is zeroed innf_ct_get_tuple()before the protocolpkt_to_tuplecallback fills in only the relevant leading bytes, so the trailing bytes of tuple->{src,dst}.u3.all are well-defined zeros and no wrapper is needed.
Pablo, whats your take?
chain c {
type filter hook output priority -300; policy accept; ct zone set 1
ct original saddr 0.0.0.0 counter accept
}
Then: ping -c 1 127.0.0.1
should the rule match the template or not?
If not, we need:
net/netfilter/nft_ct.c +1 -1
@@ -78,7 +78,7 @@ static void nft_ct_get_eval(const struct nft_expr *expr,break ; } -if (ct ==NULL ) +if (!ct ||nf_ct_is_template (ct)) goto err;switch (priv->key) {
net/netfilter/nft_ct_fast.c +1 -1
@@ -30,7 +30,7 @@ void nft_ct_get_fast_eval(const struct nft_expr *expr,break ; } -if (!ct) { +if (!ct ||nf_ct_is_template (ct)) { regs->verdict.code =NFT_BREAK ;return ; }
If it should match, we need something like this:
net/netfilter/nft_ct.c +6 -4
@@ -180,12 +180,14 @@ static void nft_ct_get_eval(const struct nft_expr *expr, tuple = &ct->tuplehash[priv->dir].tuple;switch (priv->key) {case NFT_CT_SRC : -memcpy (dest, tuple->src.u3.all, -nf_ct_l3num (ct) ==NFPROTO_IPV4 ?4 :16 ); +if (nf_ct_l3num (ct) !=nft_pf (pkt)) + goto err; +memcpy (dest, tuple->src.u3.all, priv->len);return ;case NFT_CT_DST : -memcpy (dest, tuple->dst.u3.all, -nf_ct_l3num (ct) ==NFPROTO_IPV4 ?4 :16 ); +if (nf_ct_l3num (ct) !=nft_pf (pkt)) + goto err; +memcpy (dest, tuple->dst.u3.all, priv->len);return ;case NFT_CT_PROTO_SRC :nft_reg_store16 (dest, (__force u16 )tuple->src.u.all);
I am leaning towards both changes. What do you think?
Pablo Neira Ayuso
maintainerQuoted 26 lines · Florian Westphal
Jiayuan Chen <[email protected]> wrote:
[ CC Pablo ]
Quoted 8 lines
Quoted 7 lines
https://sashiko.dev/#/patchset/20260528042620.263828-1-jiayuan.chen%40linux.dev
"This is a pre-existing issue, but does copying only
addr_lenbytes whenpriv->lenis larger leave the remainder of the register uninitialized? Innft_do_chain(), the register array is allocated on the kernel stack without zero-initialization. Ifpriv->lenis 16 andaddr_lenis 4, only the first 4 bytes are written."Quoted 5 lines
I just spotted that too. I think copying
priv->lenunconditionally is enough -- tuple->{src,dst} is zeroed innf_ct_get_tuple()before the protocolpkt_to_tuplecallback fills in only the relevant leading bytes, so the trailing bytes of tuple->{src,dst}.u3.all are well-defined zeros and no wrapper is needed.Pablo, whats your take?
chain c {
type filter hook output priority -300; policy accept; ct zone set 1
ct original saddr 0.0.0.0 counter accept
}
Quoted 3 lines
Then: ping -c 1 127.0.0.1
should the rule match the template or not?
I don't think so, no matching on the template conntrack.
Quoted 20 lines
If not, we need:
net/netfilter/nft_ct.c +1
-1@@ -78,7 +78,7 @@ static void nft_ct_get_eval(const struct nft_expr *expr,break ; } -if (ct ==NULL ) +if (!ct ||nf_ct_is_template (ct)) goto err;switch (priv->key) {net/netfilter/nft_ct_fast.c +0
-0
net/netfilter/nft_ct_fast.c void nft_ct_get_fast_eval(const struct nft_expr *expr, · line 36
Quoted 8 lines
net/netfilter/nft_ct_fast.c +1
-1@@ -30,7 +30,7 @@ void nft_ct_get_fast_eval(const struct nft_expr *expr,break ; } -if (!ct) { +if (!ct ||nf_ct_is_template (ct)) { regs->verdict.code =NFT_BREAK ;return ; }
This patch LGTM.
Florian Westphal
maintainerQuoted 7 lines · Pablo Neira Ayuso
Quoted 6 lines · Florian Westphal
Jiayuan Chen <[email protected]> wrote:
type filter hook output priority -300; policy accept; ct zone set 1
ct original saddr 0.0.0.0 counter accept
}
Quoted 5 lines
Quoted 3 lines
Then: ping -c 1 127.0.0.1
should the rule match the template or not?
I don't think so, no matching on the template conntrack.
Great, I will make a test case for nftables. Jiayuan, would you send a v2 that fixes the OOB+register leak and restricts template matching?
Thanks!
Quoted 10 lines · Florian Westphal
Quoted 9 lines · Pablo Neira Ayuso
Quoted 8 lines · Florian Westphal
Jiayuan Chen <[email protected]> wrote:
type filter hook output priority -300; policy accept; ct zone set 1
ct original saddr 0.0.0.0 counter accept
}Then: ping -c 1 127.0.0.1
Quoted 7 lines
Quoted 2 lines
Quoted 1 line
should the rule match the template or not?
I don't think so, no matching on the template conntrack.
Great, I will make a test case for nftables. Jiayuan, would you send a v2 that fixes the OOB+register leak and restricts template matching?
Thanks!
Sure. I will send V2 since everything is clear.