Skip to content

Commit b0ade85

Browse files
geertuummakynes
authored andcommitted
netfilter: nat: Do not use ARRAY_SIZE() on spinlocks to fix zero div
If no spinlock debugging options (CONFIG_GENERIC_LOCKBREAK, CONFIG_DEBUG_SPINLOCK, CONFIG_DEBUG_LOCK_ALLOC) are enabled on a UP platform (e.g. m68k defconfig), arch_spinlock_t is an empty struct, hence using ARRAY_SIZE(nf_nat_locks) causes a division by zero: net/netfilter/nf_nat_core.c: In function ‘nf_nat_setup_info’: net/netfilter/nf_nat_core.c:432: warning: division by zero net/netfilter/nf_nat_core.c: In function ‘__nf_nat_cleanup_conntrack’: net/netfilter/nf_nat_core.c:535: warning: division by zero net/netfilter/nf_nat_core.c:537: warning: division by zero net/netfilter/nf_nat_core.c: In function ‘nf_nat_init’: net/netfilter/nf_nat_core.c:810: warning: division by zero net/netfilter/nf_nat_core.c:811: warning: division by zero net/netfilter/nf_nat_core.c:824: warning: division by zero Fix this by using the CONNTRACK_LOCKS definition instead. Suggested-by: Florian Westphal <[email protected]> Fixes: 8073e96 ("netfilter: nat: use keyed locks") Signed-off-by: Geert Uytterhoeven <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 2bd6bf0 commit b0ade85

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

net/netfilter/nf_nat_core.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ nf_nat_setup_info(struct nf_conn *ct,
429429

430430
srchash = hash_by_src(net,
431431
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
432-
lock = &nf_nat_locks[srchash % ARRAY_SIZE(nf_nat_locks)];
432+
lock = &nf_nat_locks[srchash % CONNTRACK_LOCKS];
433433
spin_lock_bh(lock);
434434
hlist_add_head_rcu(&ct->nat_bysource,
435435
&nf_nat_bysource[srchash]);
@@ -532,9 +532,9 @@ static void __nf_nat_cleanup_conntrack(struct nf_conn *ct)
532532
unsigned int h;
533533

534534
h = hash_by_src(nf_ct_net(ct), &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
535-
spin_lock_bh(&nf_nat_locks[h % ARRAY_SIZE(nf_nat_locks)]);
535+
spin_lock_bh(&nf_nat_locks[h % CONNTRACK_LOCKS]);
536536
hlist_del_rcu(&ct->nat_bysource);
537-
spin_unlock_bh(&nf_nat_locks[h % ARRAY_SIZE(nf_nat_locks)]);
537+
spin_unlock_bh(&nf_nat_locks[h % CONNTRACK_LOCKS]);
538538
}
539539

540540
static int nf_nat_proto_clean(struct nf_conn *ct, void *data)
@@ -807,8 +807,8 @@ static int __init nf_nat_init(void)
807807

808808
/* Leave them the same for the moment. */
809809
nf_nat_htable_size = nf_conntrack_htable_size;
810-
if (nf_nat_htable_size < ARRAY_SIZE(nf_nat_locks))
811-
nf_nat_htable_size = ARRAY_SIZE(nf_nat_locks);
810+
if (nf_nat_htable_size < CONNTRACK_LOCKS)
811+
nf_nat_htable_size = CONNTRACK_LOCKS;
812812

813813
nf_nat_bysource = nf_ct_alloc_hashtable(&nf_nat_htable_size, 0);
814814
if (!nf_nat_bysource)
@@ -821,7 +821,7 @@ static int __init nf_nat_init(void)
821821
return ret;
822822
}
823823

824-
for (i = 0; i < ARRAY_SIZE(nf_nat_locks); i++)
824+
for (i = 0; i < CONNTRACK_LOCKS; i++)
825825
spin_lock_init(&nf_nat_locks[i]);
826826

827827
nf_ct_helper_expectfn_register(&follow_master_nat);

0 commit comments

Comments
 (0)