netfilter: replace -EEXIST with -EBUSY
Subsystems (4)
PATCH 1/1 netfilter: replace -EEXIST with -EBUSY
From: Daniel Gomez <[email protected]>
The -EEXIST error code is reserved by the module loading infrastructure to indicate that a module is already loaded. When a module's init function returns -EEXIST, userspace tools like kmod interpret this as "module already loaded" and treat the operation as successful, returning 0 to the user even though the module initialization actually failed.
This follows the precedent set by commit 54416fd76770 ("netfilter:
conntrack: helper: Replace -EEXIST by -EBUSY") which fixed the same issue in nf_conntrack_helper_register().
Affected modules:
* ebtable_broute ebtable_filter ebtable_nat arptable_filter
* ip6table_filter ip6table_mangle ip6table_nat ip6table_raw
* ip6table_security iptable_filter iptable_mangle iptable_nat
* iptable_raw iptable_security
---
The error code -EEXIST is reserved by the kernel module loader to indicate that a module with the same name is already loaded. When a module's init function returns -EEXIST, kmod interprets this as "module already loaded" and reports success instead of failure [1].
The kernel module loader will include a safety net that provides -EEXIST to -EBUSY with a warning [2], and a documentation patch has been sent to prevent future occurrences [3].
These affected code paths were identified using a static analysis tool
[4] that traces -EEXIST returns to module_init(). The tool was developed with AI assistance and all findings were manually validated.
--- net/bridge/netfilter/ebtables.c | 2 +- net/netfilter/nf_log.c | 4 ++-- net/netfilter/x_tables.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-)
net/bridge/netfilter/ebtables.c +1 -1
@@ -1299,7 +1299,7 @@ int ebt_register_template(const struct ebt_table *t, int (*table_init)(struct nelist_for_each_entry (tmpl, &template_tables, list) {if (WARN_ON_ONCE (strcmp (t->name, tmpl->name) ==0 )) {mutex_unlock (&ebt_mutex); -return -EEXIST ; +return -EBUSY ; } }
net/netfilter/nf_log.c +2 -2
@@ -89,7 +89,7 @@ int nf_log_register(u_int8_t pf, struct nf_logger *logger)if (pf ==NFPROTO_UNSPEC ) {for (i =NFPROTO_UNSPEC ; i <NFPROTO_NUMPROTO ; i++) {if (rcu_access_pointer (loggers[i][logger->type])) { - ret = -EEXIST ; + ret = -EBUSY ; goto unlock; } }
@@ -97,7 +97,7 @@ int nf_log_register(u_int8_t pf, struct nf_logger *logger)rcu_assign_pointer (loggers[i][logger->type], logger); }else {if (rcu_access_pointer (loggers[pf][logger->type])) { - ret = -EEXIST ; + ret = -EBUSY ; goto unlock; }rcu_assign_pointer (loggers[pf][logger->type], logger);
net/netfilter/x_tables.c +1 -1
@@ -1764,7 +1764,7 @@ EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);int xt_register_template (const struct xt_table *table,int (*table_init)(struct net *net)) { -int ret = -EEXIST , af = table->af; +int ret = -EBUSY , af = table->af;struct xt_template *t;mutex_lock (&xt[af].mutex);
---
base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
change-id: 20251218-dev-module-init-eexists-netfilter-56f4fb352dcb
Best regards,
Signature · 1 line
Daniel Gomez <[email protected]>
Florian Westphal
maintainerQuoted 12 lines · Daniel Gomez
From: Daniel Gomez <[email protected]>
The
-EEXISTerror code is reserved by the module loading infrastructure to indicate that a module is already loaded. When a module's init function returns-EEXIST, userspace tools like kmod interpret this as "module already loaded" and treat the operation as successful, returning 0 to the user even though the module initialization actually failed.This follows the precedent set by commit
54416fd76770("netfilter:
conntrack: helper: Replace-EEXISTby-EBUSY") which fixed the same issue innf_conntrack_helper_register().
Quoted 5 lines
Affected modules:
*ebtable_brouteebtable_filterebtable_natarptable_filter
*ip6table_filterip6table_mangleip6table_natip6table_raw
*ip6table_securityiptable_filteriptable_mangleiptable_nat
*iptable_rawiptable_security
But this is very different from what 54416fd76770 fixes.
Before 54416fd76770. userspace can make a configuration entry that prevents and unrelated module from getting loaded but at the same time it doesn't provide any error to userspace.
All these -EEXIST should not be possible unless the module is already loaded.
net/bridge/netfilter/ebtables.c int ebt_register_template(const struct ebt_table *t, int (*table_init)(struct ne · line 1302
Quoted 10 lines
net/bridge/netfilter/ebtables.c +1
-1@@ -1299,7 +1299,7 @@ int ebt_register_template(const struct ebt_table *t, int (*table_init)(struct nelist_for_each_entry (tmpl, &template_tables, list) {if (WARN_ON_ONCE (strcmp (t->name, tmpl->name) ==0 )) {mutex_unlock (&ebt_mutex); -return -EEXIST ; +return -EBUSY ;
As you can see from the WARN_ON, this cannot happen unless someone adds a new ebt kernel table module that tries to register the same name.
net/netfilter/nf_log.c int nf_log_register(u_int8_t pf, struct nf_logger *logger) · line 93
Quoted 11 lines
net/netfilter/nf_log.c +1
-1@@ -89,7 +89,7 @@ int nf_log_register(u_int8_t pf, struct nf_logger *logger)if (pf ==NFPROTO_UNSPEC ) {for (i =NFPROTO_UNSPEC ; i <NFPROTO_NUMPROTO ; i++) {if (rcu_access_pointer (loggers[i][logger->type])) { - ret = -EEXIST ; + ret = -EBUSY ; goto unlock;
I don't see how this can happen, unless someone adds a new kernel module that claims the same type as an existing kernel module.
net/netfilter/x_tables.c EXPORT_SYMBOL_GPL(xt_hook_ops_alloc); · line 1768
Quoted 11 lines
net/netfilter/x_tables.c +1
-1@@ -1764,7 +1764,7 @@ EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);int xt_register_template (const struct xt_table *table,int (*table_init)(struct net *net)) { -int ret = -EEXIST , af = table->af; +int ret = -EBUSY , af = table->af;struct xt_template *t;
Same, this requires someone adding a new kernel module with clashing name.
I'll apply this patch but its not related to 54416fd76770 afaics.
Quoted 19 lines · Florian Westphal
Quoted 12 lines · Daniel Gomez
From: Daniel Gomez <[email protected]>
The
-EEXISTerror code is reserved by the module loading infrastructure to indicate that a module is already loaded. When a module's init function returns-EEXIST, userspace tools like kmod interpret this as "module already loaded" and treat the operation as successful, returning 0 to the user even though the module initialization actually failed.This follows the precedent set by commit
54416fd76770("netfilter:
conntrack: helper: Replace-EEXISTby-EBUSY") which fixed the same issue innf_conntrack_helper_register().Quoted 5 lines
Affected modules:
*ebtable_brouteebtable_filterebtable_natarptable_filter
*ip6table_filterip6table_mangleip6table_natip6table_raw
*ip6table_securityiptable_filteriptable_mangleiptable_nat
*iptable_rawiptable_security
Quoted 8 lines
But this is very different from what
54416fd76770fixes.Before
54416fd76770. userspace can make a configuration entry that prevents and unrelated module from getting loaded but at the same time it doesn't provide any error to userspace.All these
-EEXISTshould not be possible unless the module is already loaded.
I see.
Quoted 1 line
I'll apply this patch but its not related to
54416fd76770afaics.
Thanks.
Then, what about removing that paragraph that mentions the commit and add something like:
Replace -EEXIST with -EBUSY to ensure correct error reporting in the module initialization path.
Florian Westphal
maintainerQuoted 4 lines · Daniel Gomez
something like:
Replace
-EEXISTwith-EBUSYto ensure correct error reporting in the module initialization path.
Makes sense, I will mangle it locally when I apply this. Thanks!