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