From: Leon Romanovsky <leon@kernel.org>
To: Doug Ledford <dledford@redhat.com>, Jason Gunthorpe <jgg@mellanox.com>
Cc: Jack Morgenstein <jackm@dev.mellanox.co.il>, linux-rdma@vger.kernel.org
Subject: [PATCH rdma-rc] IB/core: Fix potential NULL pointer dereference in pkey cache
Date: Sun, 26 Apr 2020 10:58:11 +0300 [thread overview]
Message-ID: <20200426075811.129814-1-leon@kernel.org> (raw)
From: Jack Morgenstein <jackm@dev.mellanox.co.il>
The IB core pkey cache is populated by procedure ib_cache_update().
Initially, the pkey cache pointer is NULL. ib_cache_update allocates
a buffer and populates it with the device's pkeys, via repeated calls
to procedure ib_query_pkey().
If there is a failure in populating the pkey buffer via ib_query_pkey(),
ib_cache_update does not replace the old pkey buffer cache with the
updated one -- it leaves the old cache as is.
Since initially the pkey buffer cache is NULL, when calling
ib_cache_update the first time, a failure in ib_query_pkey() will cause
the pkey buffer cache pointer to remain NULL.
In this situation, any calls subsequent to ib_get_cached_pkey(),
ib_find_cached_pkey(), or ib_find_cached_pkey_exact() will try to
dereference the NULL pkey cache pointer, causing a kernel panic.
Fix this by testing if the cache pointer is NULL. If yes,
return -ENOENT.
Fixes: 8faea9fd4a39 ("RDMA/cache: Move the cache per-port data into the main ib_port_data")
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Jack Morgenstein <jackm@dev.mellanox.co.il>
Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
---
drivers/infiniband/core/cache.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/drivers/infiniband/core/cache.c b/drivers/infiniband/core/cache.c
index 717b798cddad..4263a482ecab 100644
--- a/drivers/infiniband/core/cache.c
+++ b/drivers/infiniband/core/cache.c
@@ -1054,11 +1054,17 @@ int ib_get_cached_pkey(struct ib_device *device,
cache = device->port_data[port_num].cache.pkey;
+ if (!cache) {
+ ret = -ENOENT;
+ goto out;
+ }
+
if (index < 0 || index >= cache->table_len)
ret = -EINVAL;
else
*pkey = cache->table[index];
+out:
read_unlock_irqrestore(&device->cache_lock, flags);
return ret;
@@ -1101,6 +1107,8 @@ int ib_find_cached_pkey(struct ib_device *device,
cache = device->port_data[port_num].cache.pkey;
*index = -1;
+ if (!cache)
+ goto out;
for (i = 0; i < cache->table_len; ++i)
if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
@@ -1117,6 +1125,7 @@ int ib_find_cached_pkey(struct ib_device *device,
ret = 0;
}
+out:
read_unlock_irqrestore(&device->cache_lock, flags);
return ret;
@@ -1141,6 +1150,8 @@ int ib_find_exact_cached_pkey(struct ib_device *device,
cache = device->port_data[port_num].cache.pkey;
*index = -1;
+ if (!cache)
+ goto out;
for (i = 0; i < cache->table_len; ++i)
if (cache->table[i] == pkey) {
@@ -1149,6 +1160,7 @@ int ib_find_exact_cached_pkey(struct ib_device *device,
break;
}
+out:
read_unlock_irqrestore(&device->cache_lock, flags);
return ret;
--
2.25.3
next reply other threads:[~2020-04-26 7:58 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-26 7:58 Leon Romanovsky [this message]
2020-05-04 17:58 ` [PATCH rdma-rc] IB/core: Fix potential NULL pointer dereference in pkey cache Jason Gunthorpe
2020-05-05 6:56 ` Leon Romanovsky
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200426075811.129814-1-leon@kernel.org \
--to=leon@kernel.org \
--cc=dledford@redhat.com \
--cc=jackm@dev.mellanox.co.il \
--cc=jgg@mellanox.com \
--cc=linux-rdma@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.