Skip to contentSkip to conversation

netfilter: ebtables: fix OOB read in compat_mtw_from_user

PATCH In review Ren Wei 2 participants 2 messages Apr 24 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

Ren Wei

author
#1 lore raw

PATCH 1/1 netfilter: ebtables: fix OOB read in compat_mtw_from_user

In reply to a message not in the archive

From: Luxiao Xu <[email protected]>

The function compat_mtw_from_user() converts ebtables extensions from 32-bit user structures to kernel native structures. However, it lacks proper validation of the user-supplied match_size/target_size.

When certain extensions are processed, the kernel-side translation logic may perform memory accesses based on the extension's expected size. If the user provides a size smaller than what the extension requires, it results in an out-of-bounds read as reported by KASAN.

This fix introduces a check to ensure match_size is at least as large as the extension's required compatsize. This covers matches, watchers, and targets, while maintaining compatibility with standard targets.

Fixes 81e675c227ec ("netfilter: ebtables: add CONFIG_COMPAT support")
---
 net/bridge/netfilter/ebtables.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/bridge/netfilter/ebtables.c +11 -0
index aea3e19875c6..80cd0233c088 100644
@@ -1977,6 +1977,11 @@ static int compat_mtw_from_user(const struct compat_ebt_entry_mwt *mwt,
 		if (IS_ERR(match))
 			return PTR_ERR(match);
 
+		if (match_size < match->compatsize) {
+			module_put(match->me);
+			return -EINVAL;
+		}
+
 		off = ebt_compat_match_offset(match, match_size);
 		if (dst) {
 			if (match->compat_from_user)
@@ -1996,6 +2001,12 @@ static int compat_mtw_from_user(const struct compat_ebt_entry_mwt *mwt,
 					    mwt->u.revision);
 		if (IS_ERR(wt))
 			return PTR_ERR(wt);
+
+		if (match_size < wt->compatsize) {
+			module_put(wt->me);
+			return -EINVAL;
+		}
+
 		off = xt_compat_target_offset(wt);
 
 		if (dst) {
Signature · 1 line
2.43.0
maintainer
#2 lore raw
Quoted 32 lines · Ren Wei

From: Luxiao Xu <[email protected]>

The function compat_mtw_from_user() converts ebtables extensions from 32-bit user structures to kernel native structures. However, it lacks proper validation of the user-supplied match_size/target_size.

When certain extensions are processed, the kernel-side translation logic may perform memory accesses based on the extension's expected size. If the user provides a size smaller than what the extension requires, it results in an out-of-bounds read as reported by KASAN.

This fix introduces a check to ensure match_size is at least as large as the extension's required compatsize. This covers matches, watchers, and targets, while maintaining compatibility with standard targets.

Fixes 81e675c227ec ("netfilter: ebtables: add CONFIG_COMPAT support")
---
 net/bridge/netfilter/ebtables.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/bridge/netfilter/ebtables.c +0 -0
index aea3e19875c6..80cd0233c088 100644

net/bridge/netfilter/ebtables.c static int compat_mtw_from_user(const struct compat_ebt_entry_mwt *mwt, · line 1984

Quoted 8 lines
net/bridge/netfilter/ebtables.c +5 -0
@@ -1977,6 +1977,11 @@ static int compat_mtw_from_user(const struct compat_ebt_entry_mwt *mwt,
 		if (IS_ERR(match))
 			return PTR_ERR(match);
 
+		if (match_size < match->compatsize) {
+			module_put(match->me);
+			return -EINVAL;
+		}
+

Are you sure this catches all bad requests? AFAIR compatsize is 0 in most cases, which bypasses this test.

should this be:

u16 csize = match->compatsize ? : match->matchsize;
...
if (match_size < csize) {
...

?

@Pablo: I think the 32bit compat layer should be removed in -next, or at least strongly discouraged and slated for removal soon.