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")
Cc: [email protected]
Reported-by: Yuan Tan <[email protected]>
Reported-by: Yifan Wu <[email protected]>
Reported-by: Juefei Pu <[email protected]>
Reported-by: Xin Liu <[email protected]>
Signed-off-by: Luxiao Xu <[email protected]>
Signed-off-by: Ren Wei <[email protected]>
---
 net/bridge/netfilter/ebtables.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/net/bridge/netfilter/ebtables.c b/net/bridge/netfilter/ebtables.c
index aea3e19875c6..80cd0233c088 100644
--- a/net/bridge/netfilter/ebtables.c
+++ b/net/bridge/netfilter/ebtables.c
@@ -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) {
-- 
2.43.0


Reply via email to