qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Marcel Apfelbaum <marcel.apfelbaum@gmail.com>
To: qemu-devel@nongnu.org, peter.maydell@linaro.org
Cc: yuval.shaia@oracle.com, marcel.apfelbaum@gmail.com,
	kamalheib1@gmail.com, philmd@redhat.com, dgilbert@redhat.com
Subject: [Qemu-devel] [PATCH PULL 01/11] contrib/rdmacm-mux: Fix out-of-bounds risk
Date: Sat, 23 Feb 2019 16:16:34 +0200	[thread overview]
Message-ID: <20190223141644.6609-2-marcel.apfelbaum@gmail.com> (raw)
In-Reply-To: <20190223141644.6609-1-marcel.apfelbaum@gmail.com>

From: Yuval Shaia <yuval.shaia@oracle.com>

The function get_fd extract context from the received MAD message and
uses it as a key to fetch the destination fd from the mapping table.
A context can be dgid in case of CM request message or comm_id in case
of CM SIDR response message.

When MAD message with a smaller size as expected for the message type
received we are hitting out-of-bounds where we are looking for the
context out of message boundaries.

Fix it by validating the message size.

Reported-by Sam Smith <sam.j.smith@oracle.com>
Signed-off-by: Yuval Shaia <yuval.shaia@oracle.com>
Message-Id: <20190212112347.1605-1-yuval.shaia@oracle.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Marcel Apfelbaum <marcel.apfelbaum@gmail.com>
---
 contrib/rdmacm-mux/main.c | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

diff --git a/contrib/rdmacm-mux/main.c b/contrib/rdmacm-mux/main.c
index ae88c77a1e..21cc804367 100644
--- a/contrib/rdmacm-mux/main.c
+++ b/contrib/rdmacm-mux/main.c
@@ -300,7 +300,7 @@ static void hash_tbl_remove_fd_ifid_pair(int fd)
     pthread_rwlock_unlock(&server.lock);
 }
 
-static int get_fd(const char *mad, int *fd, __be64 *gid_ifid)
+static int get_fd(const char *mad, int umad_len, int *fd, __be64 *gid_ifid)
 {
     struct umad_hdr *hdr = (struct umad_hdr *)mad;
     char *data = (char *)hdr + sizeof(*hdr);
@@ -308,13 +308,35 @@ static int get_fd(const char *mad, int *fd, __be64 *gid_ifid)
     uint16_t attr_id = be16toh(hdr->attr_id);
     int rc = 0;
 
+    if (umad_len <= sizeof(*hdr)) {
+        rc = -EINVAL;
+        syslog(LOG_DEBUG, "Ignoring MAD packets with header only\n");
+        goto out;
+    }
+
     switch (attr_id) {
     case UMAD_CM_ATTR_REQ:
+        if (unlikely(umad_len < sizeof(*hdr) + CM_REQ_DGID_POS +
+            sizeof(*gid_ifid))) {
+            rc = -EINVAL;
+            syslog(LOG_WARNING,
+                   "Invalid MAD packet size (%d) for attr_id 0x%x\n", umad_len,
+                    attr_id);
+            goto out;
+        }
         memcpy(gid_ifid, data + CM_REQ_DGID_POS, sizeof(*gid_ifid));
         rc = hash_tbl_search_fd_by_ifid(fd, gid_ifid);
         break;
 
     case UMAD_CM_ATTR_SIDR_REQ:
+        if (unlikely(umad_len < sizeof(*hdr) + CM_SIDR_REQ_DGID_POS +
+            sizeof(*gid_ifid))) {
+            rc = -EINVAL;
+            syslog(LOG_WARNING,
+                   "Invalid MAD packet size (%d) for attr_id 0x%x\n", umad_len,
+                    attr_id);
+            goto out;
+        }
         memcpy(gid_ifid, data + CM_SIDR_REQ_DGID_POS, sizeof(*gid_ifid));
         rc = hash_tbl_search_fd_by_ifid(fd, gid_ifid);
         break;
@@ -331,6 +353,13 @@ static int get_fd(const char *mad, int *fd, __be64 *gid_ifid)
         data += sizeof(comm_id);
         /* Fall through */
     case UMAD_CM_ATTR_SIDR_REP:
+        if (unlikely(umad_len < sizeof(*hdr) + sizeof(comm_id))) {
+            rc = -EINVAL;
+            syslog(LOG_WARNING,
+                   "Invalid MAD packet size (%d) for attr_id 0x%x\n", umad_len,
+                   attr_id);
+            goto out;
+        }
         memcpy(&comm_id, data, sizeof(comm_id));
         if (comm_id) {
             rc = hash_tbl_search_fd_by_comm_id(comm_id, fd, gid_ifid);
@@ -344,6 +373,7 @@ static int get_fd(const char *mad, int *fd, __be64 *gid_ifid)
 
     syslog(LOG_DEBUG, "mad_to_vm: %d 0x%x 0x%x\n", *fd, attr_id, comm_id);
 
+out:
     return rc;
 }
 
@@ -372,7 +402,8 @@ static void *umad_recv_thread_func(void *args)
         } while (rc && server.run);
 
         if (server.run) {
-            rc = get_fd(msg.umad.mad, &fd, &msg.hdr.sgid.global.interface_id);
+            rc = get_fd(msg.umad.mad, msg.umad_len, &fd,
+                        &msg.hdr.sgid.global.interface_id);
             if (rc) {
                 continue;
             }
-- 
2.17.1

  reply	other threads:[~2019-02-23 14:16 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-23 14:16 [Qemu-devel] [PATCH PULL 00/11] RDMA queue Marcel Apfelbaum
2019-02-23 14:16 ` Marcel Apfelbaum [this message]
2019-02-23 14:16 ` [Qemu-devel] [PATCH PULL 02/11] hw/rdma: Switch to generic error reporting way Marcel Apfelbaum
2019-02-23 14:16 ` [Qemu-devel] [PATCH PULL 03/11] hw/rdma: Introduce protected qlist Marcel Apfelbaum
2019-02-23 14:16 ` [Qemu-devel] [PATCH PULL 04/11] hw/rdma: Protect against concurrent execution of poll_cq Marcel Apfelbaum
2019-02-23 14:16 ` [Qemu-devel] [PATCH PULL 05/11] {monitor, hw/pvrdma}: Expose device internals via monitor interface Marcel Apfelbaum
2019-02-23 14:16 ` [Qemu-devel] [PATCH PULL 06/11] hw/rdma: Free all MAD receive buffers when device is closed Marcel Apfelbaum
2019-02-23 14:16 ` [Qemu-devel] [PATCH PULL 07/11] hw/rdma: Free all receive buffers when QP is destroyed Marcel Apfelbaum
2019-02-23 14:16 ` [Qemu-devel] [PATCH PULL 08/11] hw/pvrdma: Delete unneeded function argument Marcel Apfelbaum
2019-02-23 14:16 ` [Qemu-devel] [PATCH PULL 09/11] hw/pvrdma: Delete pvrdma_exit function Marcel Apfelbaum
2019-02-23 14:16 ` [Qemu-devel] [PATCH PULL 10/11] hw/pvrdma: Unregister from shutdown notifier when device goes down Marcel Apfelbaum
2019-02-23 14:16 ` [Qemu-devel] [PATCH PULL 11/11] hw/rdma: another clang compilation fix Marcel Apfelbaum
2019-02-26 16:48 ` [Qemu-devel] [PATCH PULL 00/11] RDMA queue Peter Maydell
2019-02-26 18:40   ` Marcel Apfelbaum
2019-02-27  9:46   ` Yuval Shaia

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=20190223141644.6609-2-marcel.apfelbaum@gmail.com \
    --to=marcel.apfelbaum@gmail.com \
    --cc=dgilbert@redhat.com \
    --cc=kamalheib1@gmail.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=yuval.shaia@oracle.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).