Blame SOURCES/kvm-nbd-Don-t-take-address-of-fields-in-packed-structs.patch

383d26
From a2bf5f1541eb073c7c4214e71b0f52d3bcc82914 Mon Sep 17 00:00:00 2001
383d26
From: John Snow <jsnow@redhat.com>
383d26
Date: Fri, 22 Mar 2019 03:22:22 +0100
383d26
Subject: [PATCH 055/163] nbd: Don't take address of fields in packed structs
383d26
383d26
RH-Author: John Snow <jsnow@redhat.com>
383d26
Message-id: <20190322032241.8111-10-jsnow@redhat.com>
383d26
Patchwork-id: 85095
383d26
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH 09/28] nbd: Don't take address of fields in packed structs
383d26
Bugzilla: 1691563
383d26
RH-Acked-by: Max Reitz <mreitz@redhat.com>
383d26
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
383d26
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
383d26
From: Peter Maydell <peter.maydell@linaro.org>
383d26
383d26
Taking the address of a field in a packed struct is a bad idea, because
383d26
it might not be actually aligned enough for that pointer type (and
383d26
thus cause a crash on dereference on some host architectures). Newer
383d26
versions of clang warn about this. Avoid the bug by not using the
383d26
"modify in place" byte swapping functions.
383d26
383d26
This patch was produced with the following spatch script:
383d26
@@
383d26
expression E;
383d26
@@
383d26
-be16_to_cpus(&E);
383d26
+E = be16_to_cpu(E);
383d26
@@
383d26
expression E;
383d26
@@
383d26
-be32_to_cpus(&E);
383d26
+E = be32_to_cpu(E);
383d26
@@
383d26
expression E;
383d26
@@
383d26
-be64_to_cpus(&E);
383d26
+E = be64_to_cpu(E);
383d26
@@
383d26
expression E;
383d26
@@
383d26
-cpu_to_be16s(&E);
383d26
+E = cpu_to_be16(E);
383d26
@@
383d26
expression E;
383d26
@@
383d26
-cpu_to_be32s(&E);
383d26
+E = cpu_to_be32(E);
383d26
@@
383d26
expression E;
383d26
@@
383d26
-cpu_to_be64s(&E);
383d26
+E = cpu_to_be64(E);
383d26
383d26
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
383d26
Message-Id: <20180927164200.15097-1-peter.maydell@linaro.org>
383d26
Reviewed-by: Eric Blake <eblake@redhat.com>
383d26
[eblake: rebase, and squash in missed changes]
383d26
Signed-off-by: Eric Blake <eblake@redhat.com>
383d26
(cherry picked from commit 80c7c2b00d607221bb43815d2c1951d54229b3ee)
383d26
Signed-off-by: John Snow <jsnow@redhat.com>
383d26
383d26
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
---
383d26
 nbd/client.c | 44 ++++++++++++++++++++++----------------------
383d26
 nbd/server.c | 24 ++++++++++++------------
383d26
 2 files changed, 34 insertions(+), 34 deletions(-)
383d26
383d26
diff --git a/nbd/client.c b/nbd/client.c
383d26
index 40b74d9..b4d457a 100644
383d26
--- a/nbd/client.c
383d26
+++ b/nbd/client.c
383d26
@@ -117,10 +117,10 @@ static int nbd_receive_option_reply(QIOChannel *ioc, uint32_t opt,
383d26
         nbd_send_opt_abort(ioc);
383d26
         return -1;
383d26
     }
383d26
-    be64_to_cpus(&reply->magic);
383d26
-    be32_to_cpus(&reply->option);
383d26
-    be32_to_cpus(&reply->type);
383d26
-    be32_to_cpus(&reply->length);
383d26
+    reply->magic = be64_to_cpu(reply->magic);
383d26
+    reply->option = be32_to_cpu(reply->option);
383d26
+    reply->type = be32_to_cpu(reply->type);
383d26
+    reply->length = be32_to_cpu(reply->length);
383d26
 
383d26
     trace_nbd_receive_option_reply(reply->option, nbd_opt_lookup(reply->option),
383d26
                                    reply->type, nbd_rep_lookup(reply->type),
383d26
@@ -396,7 +396,7 @@ static int nbd_opt_go(QIOChannel *ioc, const char *wantname,
383d26
             return -1;
383d26
         }
383d26
         len -= sizeof(type);
383d26
-        be16_to_cpus(&type);
383d26
+        type = be16_to_cpu(type);
383d26
         switch (type) {
383d26
         case NBD_INFO_EXPORT:
383d26
             if (len != sizeof(info->size) + sizeof(info->flags)) {
383d26
@@ -410,13 +410,13 @@ static int nbd_opt_go(QIOChannel *ioc, const char *wantname,
383d26
                 nbd_send_opt_abort(ioc);
383d26
                 return -1;
383d26
             }
383d26
-            be64_to_cpus(&info->size);
383d26
+            info->size = be64_to_cpu(info->size);
383d26
             if (nbd_read(ioc, &info->flags, sizeof(info->flags), errp) < 0) {
383d26
                 error_prepend(errp, "failed to read info flags: ");
383d26
                 nbd_send_opt_abort(ioc);
383d26
                 return -1;
383d26
             }
383d26
-            be16_to_cpus(&info->flags);
383d26
+            info->flags = be16_to_cpu(info->flags);
383d26
             trace_nbd_receive_negotiate_size_flags(info->size, info->flags);
383d26
             break;
383d26
 
383d26
@@ -433,7 +433,7 @@ static int nbd_opt_go(QIOChannel *ioc, const char *wantname,
383d26
                 nbd_send_opt_abort(ioc);
383d26
                 return -1;
383d26
             }
383d26
-            be32_to_cpus(&info->min_block);
383d26
+            info->min_block = be32_to_cpu(info->min_block);
383d26
             if (!is_power_of_2(info->min_block)) {
383d26
                 error_setg(errp, "server minimum block size %" PRIu32
383d26
                            " is not a power of two", info->min_block);
383d26
@@ -447,7 +447,7 @@ static int nbd_opt_go(QIOChannel *ioc, const char *wantname,
383d26
                 nbd_send_opt_abort(ioc);
383d26
                 return -1;
383d26
             }
383d26
-            be32_to_cpus(&info->opt_block);
383d26
+            info->opt_block = be32_to_cpu(info->opt_block);
383d26
             if (!is_power_of_2(info->opt_block) ||
383d26
                 info->opt_block < info->min_block) {
383d26
                 error_setg(errp, "server preferred block size %" PRIu32
383d26
@@ -461,7 +461,7 @@ static int nbd_opt_go(QIOChannel *ioc, const char *wantname,
383d26
                 nbd_send_opt_abort(ioc);
383d26
                 return -1;
383d26
             }
383d26
-            be32_to_cpus(&info->max_block);
383d26
+            info->max_block = be32_to_cpu(info->max_block);
383d26
             if (info->max_block < info->min_block) {
383d26
                 error_setg(errp, "server maximum block size %" PRIu32
383d26
                            " is not valid", info->max_block);
383d26
@@ -668,7 +668,7 @@ static int nbd_negotiate_simple_meta_context(QIOChannel *ioc,
383d26
         if (nbd_read(ioc, &received_id, sizeof(received_id), errp) < 0) {
383d26
             return -1;
383d26
         }
383d26
-        be32_to_cpus(&received_id);
383d26
+        received_id = be32_to_cpu(received_id);
383d26
 
383d26
         reply.length -= sizeof(received_id);
383d26
         name = g_malloc(reply.length + 1);
383d26
@@ -872,13 +872,13 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
383d26
             error_prepend(errp, "Failed to read export length: ");
383d26
             goto fail;
383d26
         }
383d26
-        be64_to_cpus(&info->size);
383d26
+        info->size = be64_to_cpu(info->size);
383d26
 
383d26
         if (nbd_read(ioc, &info->flags, sizeof(info->flags), errp) < 0) {
383d26
             error_prepend(errp, "Failed to read export flags: ");
383d26
             goto fail;
383d26
         }
383d26
-        be16_to_cpus(&info->flags);
383d26
+        info->flags = be16_to_cpu(info->flags);
383d26
     } else if (magic == NBD_CLIENT_MAGIC) {
383d26
         uint32_t oldflags;
383d26
 
383d26
@@ -895,13 +895,13 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,
383d26
             error_prepend(errp, "Failed to read export length: ");
383d26
             goto fail;
383d26
         }
383d26
-        be64_to_cpus(&info->size);
383d26
+        info->size = be64_to_cpu(info->size);
383d26
 
383d26
         if (nbd_read(ioc, &oldflags, sizeof(oldflags), errp) < 0) {
383d26
             error_prepend(errp, "Failed to read export flags: ");
383d26
             goto fail;
383d26
         }
383d26
-        be32_to_cpus(&oldflags);
383d26
+        oldflags = be32_to_cpu(oldflags);
383d26
         if (oldflags & ~0xffff) {
383d26
             error_setg(errp, "Unexpected export flags %0x" PRIx32, oldflags);
383d26
             goto fail;
383d26
@@ -1080,8 +1080,8 @@ static int nbd_receive_simple_reply(QIOChannel *ioc, NBDSimpleReply *reply,
383d26
         return ret;
383d26
     }
383d26
 
383d26
-    be32_to_cpus(&reply->error);
383d26
-    be64_to_cpus(&reply->handle);
383d26
+    reply->error = be32_to_cpu(reply->error);
383d26
+    reply->handle = be64_to_cpu(reply->handle);
383d26
 
383d26
     return 0;
383d26
 }
383d26
@@ -1105,10 +1105,10 @@ static int nbd_receive_structured_reply_chunk(QIOChannel *ioc,
383d26
         return ret;
383d26
     }
383d26
 
383d26
-    be16_to_cpus(&chunk->flags);
383d26
-    be16_to_cpus(&chunk->type);
383d26
-    be64_to_cpus(&chunk->handle);
383d26
-    be32_to_cpus(&chunk->length);
383d26
+    chunk->flags = be16_to_cpu(chunk->flags);
383d26
+    chunk->type = be16_to_cpu(chunk->type);
383d26
+    chunk->handle = be64_to_cpu(chunk->handle);
383d26
+    chunk->length = be32_to_cpu(chunk->length);
383d26
 
383d26
     return 0;
383d26
 }
383d26
@@ -1128,7 +1128,7 @@ int nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, Error **errp)
383d26
         return ret;
383d26
     }
383d26
 
383d26
-    be32_to_cpus(&reply->magic);
383d26
+    reply->magic = be32_to_cpu(reply->magic);
383d26
 
383d26
     switch (reply->magic) {
383d26
     case NBD_SIMPLE_REPLY_MAGIC:
383d26
diff --git a/nbd/server.c b/nbd/server.c
383d26
index a9fec45..df76324 100644
383d26
--- a/nbd/server.c
383d26
+++ b/nbd/server.c
383d26
@@ -333,7 +333,7 @@ static int nbd_opt_read_name(NBDClient *client, char *name, uint32_t *length,
383d26
     if (ret <= 0) {
383d26
         return ret;
383d26
     }
383d26
-    cpu_to_be32s(&len;;
383d26
+    len = cpu_to_be32(len);
383d26
 
383d26
     if (len > NBD_MAX_NAME_SIZE) {
383d26
         return nbd_opt_invalid(client, errp,
383d26
@@ -486,7 +486,7 @@ static int nbd_negotiate_send_info(NBDClient *client,
383d26
     if (rc < 0) {
383d26
         return rc;
383d26
     }
383d26
-    cpu_to_be16s(&info;;
383d26
+    info = cpu_to_be16(info);
383d26
     if (nbd_write(client->ioc, &info, sizeof(info), errp) < 0) {
383d26
         return -EIO;
383d26
     }
383d26
@@ -551,14 +551,14 @@ static int nbd_negotiate_handle_info(NBDClient *client, uint16_t myflags,
383d26
     if (rc <= 0) {
383d26
         return rc;
383d26
     }
383d26
-    be16_to_cpus(&requests);
383d26
+    requests = be16_to_cpu(requests);
383d26
     trace_nbd_negotiate_handle_info_requests(requests);
383d26
     while (requests--) {
383d26
         rc = nbd_opt_read(client, &request, sizeof(request), errp);
383d26
         if (rc <= 0) {
383d26
             return rc;
383d26
         }
383d26
-        be16_to_cpus(&request);
383d26
+        request = be16_to_cpu(request);
383d26
         trace_nbd_negotiate_handle_info_request(request,
383d26
                                                 nbd_info_lookup(request));
383d26
         /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE;
383d26
@@ -618,9 +618,9 @@ static int nbd_negotiate_handle_info(NBDClient *client, uint16_t myflags,
383d26
     /* maximum - At most 32M, but smaller as appropriate. */
383d26
     sizes[2] = MIN(blk_get_max_transfer(exp->blk), NBD_MAX_BUFFER_SIZE);
383d26
     trace_nbd_negotiate_handle_info_block_size(sizes[0], sizes[1], sizes[2]);
383d26
-    cpu_to_be32s(&sizes[0]);
383d26
-    cpu_to_be32s(&sizes[1]);
383d26
-    cpu_to_be32s(&sizes[2]);
383d26
+    sizes[0] = cpu_to_be32(sizes[0]);
383d26
+    sizes[1] = cpu_to_be32(sizes[1]);
383d26
+    sizes[2] = cpu_to_be32(sizes[2]);
383d26
     rc = nbd_negotiate_send_info(client, NBD_INFO_BLOCK_SIZE,
383d26
                                  sizeof(sizes), sizes, errp);
383d26
     if (rc < 0) {
383d26
@@ -904,7 +904,7 @@ static int nbd_negotiate_meta_query(NBDClient *client,
383d26
     if (ret <= 0) {
383d26
         return ret;
383d26
     }
383d26
-    cpu_to_be32s(&len;;
383d26
+    len = cpu_to_be32(len);
383d26
 
383d26
     if (len < ns_len) {
383d26
         trace_nbd_negotiate_meta_query_skip("length too short");
383d26
@@ -971,7 +971,7 @@ static int nbd_negotiate_meta_queries(NBDClient *client,
383d26
     if (ret <= 0) {
383d26
         return ret;
383d26
     }
383d26
-    cpu_to_be32s(&nb_queries);
383d26
+    nb_queries = cpu_to_be32(nb_queries);
383d26
     trace_nbd_negotiate_meta_context(nbd_opt_lookup(client->opt),
383d26
                                      export_name, nb_queries);
383d26
 
383d26
@@ -1049,7 +1049,7 @@ static int nbd_negotiate_options(NBDClient *client, uint16_t myflags,
383d26
         error_prepend(errp, "read failed: ");
383d26
         return -EIO;
383d26
     }
383d26
-    be32_to_cpus(&flags);
383d26
+    flags = be32_to_cpu(flags);
383d26
     trace_nbd_negotiate_options_flags(flags);
383d26
     if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
383d26
         fixedNewstyle = true;
383d26
@@ -1900,8 +1900,8 @@ static int blockstatus_to_extents(BlockDriverState *bs, uint64_t offset,
383d26
     extents_end = extent + 1;
383d26
 
383d26
     for (extent = extents; extent < extents_end; extent++) {
383d26
-        cpu_to_be32s(&extent->flags);
383d26
-        cpu_to_be32s(&extent->length);
383d26
+        extent->flags = cpu_to_be32(extent->flags);
383d26
+        extent->length = cpu_to_be32(extent->length);
383d26
     }
383d26
 
383d26
     *bytes -= remaining_bytes;
383d26
-- 
383d26
1.8.3.1
383d26