Skip to content

Commit cefa31a

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nft_log: perform module load from nf_tables
modprobe calls from the nf_logger_find_get() API causes deadlock in very special cases because they occur with the nf_tables transaction mutex held. In the specific case of nf_log, deadlock is via: A nf_tables -> transaction mutex -> nft_log -> modprobe -> nf_log_syslog \ -> pernet_ops rwsem -> wait for C B netlink event -> rtnl_mutex -> nf_tables transaction mutex -> wait for A C close() -> ip6mr_sk_done -> rtnl_mutex -> wait for B Earlier patch added NFLOG/xt_LOG module softdeps to avoid the need to load the backend module during a transaction. For nft_log we would have to add a softdep for both nfnetlink_log or nf_log_syslog, since we do not know in advance which of the two backends are going to be configured. This defers the modprobe op until after the transaction mutex is released. Tested-by: Phil Sutter <phil@nwl.cc> Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent a38b5b5 commit cefa31a

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

include/net/netfilter/nf_tables.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,4 +1562,9 @@ void nf_tables_trans_destroy_flush_work(void);
15621562
int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
15631563
__be64 nf_jiffies64_to_msecs(u64 input);
15641564

1565+
#ifdef CONFIG_MODULES
1566+
__printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...);
1567+
#else
1568+
static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; }
1569+
#endif
15651570
#endif /* _NET_NF_TABLES_H */

net/netfilter/nf_log.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,6 @@ int nf_logger_find_get(int pf, enum nf_log_type type)
170170
return 0;
171171
}
172172

173-
if (rcu_access_pointer(loggers[pf][type]) == NULL)
174-
request_module("nf-logger-%u-%u", pf, type);
175-
176173
rcu_read_lock();
177174
logger = rcu_dereference(loggers[pf][type]);
178175
if (logger == NULL)

net/netfilter/nf_tables_api.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,8 @@ struct nft_module_request {
586586
};
587587

588588
#ifdef CONFIG_MODULES
589-
static __printf(2, 3) int nft_request_module(struct net *net, const char *fmt,
590-
...)
589+
__printf(2, 3) int nft_request_module(struct net *net, const char *fmt,
590+
...)
591591
{
592592
char module_name[MODULE_NAME_LEN];
593593
struct nft_module_request *req;
@@ -620,6 +620,7 @@ static __printf(2, 3) int nft_request_module(struct net *net, const char *fmt,
620620

621621
return -EAGAIN;
622622
}
623+
EXPORT_SYMBOL_GPL(nft_request_module);
623624
#endif
624625

625626
static void lockdep_nfnl_nft_mutex_not_held(void)

net/netfilter/nft_log.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ static const struct nla_policy nft_log_policy[NFTA_LOG_MAX + 1] = {
128128
[NFTA_LOG_FLAGS] = { .type = NLA_U32 },
129129
};
130130

131+
static int nft_log_modprobe(struct net *net, enum nf_log_type t)
132+
{
133+
switch (t) {
134+
case NF_LOG_TYPE_LOG:
135+
return nft_request_module(net, "%s", "nf_log_syslog");
136+
case NF_LOG_TYPE_ULOG:
137+
return nft_request_module(net, "%s", "nfnetlink_log");
138+
case NF_LOG_TYPE_MAX:
139+
break;
140+
}
141+
142+
return -ENOENT;
143+
}
144+
131145
static int nft_log_init(const struct nft_ctx *ctx,
132146
const struct nft_expr *expr,
133147
const struct nlattr * const tb[])
@@ -197,8 +211,12 @@ static int nft_log_init(const struct nft_ctx *ctx,
197211
return 0;
198212

199213
err = nf_logger_find_get(ctx->family, li->type);
200-
if (err < 0)
214+
if (err < 0) {
215+
if (nft_log_modprobe(ctx->net, li->type) == -EAGAIN)
216+
err = -EAGAIN;
217+
201218
goto err1;
219+
}
202220

203221
return 0;
204222

0 commit comments

Comments
 (0)