net: dccp: fix kernel crash on module load [Linux 5.0]

net: dccp: fix kernel crash on module load [Linux 5.0]

This Linux kernel change "net: dccp: fix kernel crash on module load" is included in the Linux 5.0 release. This change is authored by Peter Oskolkov <posk [at] google.com> on Mon Dec 24 12:57:17 2018 -0800. The commit for this change in Linux stable tree is c92c81d (patch).

net: dccp: fix kernel crash on module load

Patch eedbbb0d98b2 "net: dccp: initialize (addr,port) ..."
added calling to inet_hashinfo2_init() from dccp_init().

However, inet_hashinfo2_init() is marked as __init(), and
thus the kernel panics when dccp is loaded as module. Removing
__init() tag from inet_hashinfo2_init() is not feasible because
it calls into __init functions in mm.

This patch adds inet_hashinfo2_init_mod() function that can
be called after the init phase is done; changes dccp_init() to
call the new function; un-marks inet_hashinfo2_init() as
exported.

Fixes: eedbbb0d98b2 ("net: dccp: initialize (addr,port) ...")
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Peter Oskolkov <posk@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

There are 43 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 include/net/inet_hashtables.h |  1 +
 net/dccp/proto.c              |  8 ++++----
 net/ipv4/inet_hashtables.c    | 34 ++++++++++++++++++++++++++--------
 3 files changed, 31 insertions(+), 12 deletions(-)

diff --git a/include/net/inet_hashtables.h b/include/net/inet_hashtables.h
index 0ce460e..babb141 100644
--- a/include/net/inet_hashtables.h
+++ b/include/net/inet_hashtables.h
@@ -237,6 +237,7 @@ void inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
             unsigned long numentries, int scale,
             unsigned long low_limit,
             unsigned long high_limit);
+int inet_hashinfo2_init_mod(struct inet_hashinfo *h);

 bool inet_ehash_insert(struct sock *sk, struct sock *osk);
 bool inet_ehash_nolisten(struct sock *sk, struct sock *osk);
diff --git a/net/dccp/proto.c b/net/dccp/proto.c
index be0b223..2cc5fbb 100644
--- a/net/dccp/proto.c
+++ b/net/dccp/proto.c
@@ -1139,11 +1139,11 @@ static int __init dccp_init(void)
    rc = percpu_counter_init(&dccp_orphan_count, 0, GFP_KERNEL);
    if (rc)
        goto out_fail;
-   rc = -ENOBUFS;
    inet_hashinfo_init(&dccp_hashinfo);
-   inet_hashinfo2_init(&dccp_hashinfo, "dccp_listen_portaddr_hash",
-               INET_LHTABLE_SIZE, 21,  /* one slot per 2 MB*/
-               0, 64 * 1024);
+   rc = inet_hashinfo2_init_mod(&dccp_hashinfo);
+   if (rc)
+       goto out_fail;
+   rc = -ENOBUFS;
    dccp_hashinfo.bind_bucket_cachep =
        kmem_cache_create("dccp_bind_bucket",
                  sizeof(struct inet_bind_bucket), 0,
diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c
index 2445614..942265d 100644
--- a/net/ipv4/inet_hashtables.c
+++ b/net/ipv4/inet_hashtables.c
@@ -762,13 +762,22 @@ void inet_hashinfo_init(struct inet_hashinfo *h)
 }
 EXPORT_SYMBOL_GPL(inet_hashinfo_init);

+static void init_hashinfo_lhash2(struct inet_hashinfo *h)
+{
+   int i;
+
+   for (i = 0; i <= h->lhash2_mask; i++) {
+       spin_lock_init(&h->lhash2[i].lock);
+       INIT_HLIST_HEAD(&h->lhash2[i].head);
+       h->lhash2[i].count = 0;
+   }
+}
+
 void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
                unsigned long numentries, int scale,
                unsigned long low_limit,
                unsigned long high_limit)
 {
-   unsigned int i;
-
    h->lhash2 = alloc_large_system_hash(name,
                        sizeof(*h->lhash2),
                        numentries,
@@ -778,14 +787,23 @@ void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
                        &h->lhash2_mask,
                        low_limit,
                        high_limit);
+   init_hashinfo_lhash2(h);
+}

-   for (i = 0; i <= h->lhash2_mask; i++) {
-       spin_lock_init(&h->lhash2[i].lock);
-       INIT_HLIST_HEAD(&h->lhash2[i].head);
-       h->lhash2[i].count = 0;
-   }
+int inet_hashinfo2_init_mod(struct inet_hashinfo *h)
+{
+   h->lhash2 = kmalloc_array(INET_LHTABLE_SIZE, sizeof(*h->lhash2), GFP_KERNEL);
+   if (!h->lhash2)
+       return -ENOMEM;
+
+   h->lhash2_mask = INET_LHTABLE_SIZE - 1;
+   /* INET_LHTABLE_SIZE must be a power of 2 */
+   BUG_ON(INET_LHTABLE_SIZE & h->lhash2_mask);
+
+   init_hashinfo_lhash2(h);
+   return 0;
 }
-EXPORT_SYMBOL_GPL(inet_hashinfo2_init);
+EXPORT_SYMBOL_GPL(inet_hashinfo2_init_mod);

 int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo)
 {

Leave a Reply

Your email address will not be published. Required fields are marked *