* [Qemu-devel] [PATCH v5 1/5] qemu/compiler: Define QEMU_NONSTRING
2019-01-03 8:56 [Qemu-devel] [PATCH v5 0/5] Fix strncpy() warnings for GCC8 new -Wstringop-truncation Philippe Mathieu-Daudé
@ 2019-01-03 8:56 ` Philippe Mathieu-Daudé
2019-01-03 8:56 ` [Qemu-devel] [PATCH v5 2/5] block/sheepdog: Use QEMU_NONSTRING for non NUL-terminated arrays Philippe Mathieu-Daudé
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Philippe Mathieu-Daudé @ 2019-01-03 8:56 UTC (permalink / raw)
To: qemu-devel
Cc: Dr. David Alan Gilbert, Thomas Huth, David Hildenbrand,
Igor Mammedov, Paolo Bonzini, Markus Armbruster,
Philippe Mathieu-Daudé, Marc-André Lureau, Eric Blake,
Michael S. Tsirkin, Daniel P. Berrangé
GCC 8 introduced the -Wstringop-truncation checker to detect truncation by
the strncat and strncpy functions (closely related to -Wstringop-overflow,
which detect buffer overflow by string-modifying functions declared in
<string.h>).
In tandem of -Wstringop-truncation, the "nonstring" attribute was added:
The nonstring variable attribute specifies that an object or member
declaration with type array of char, signed char, or unsigned char,
or pointer to such a type is intended to store character arrays that
do not necessarily contain a terminating NUL. This is useful in detecting
uses of such arrays or pointers with functions that expect NUL-terminated
strings, and to avoid warnings when such an array or pointer is used as
an argument to a bounded string manipulation function such as strncpy.
From the GCC manual: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-nonstring-variable-attribute
Add the QEMU_NONSTRING macro which checks if the compiler supports this
attribute.
Suggested-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
v4: reordered the commit description to make sens (eblake)
Note this trigger the following checkpatch warning (patchew)
which I'm ignoring because this is consistent with how the
rest of this file uses:
WARNING: architecture specific defines should be avoided
#50: FILE: include/qemu/compiler.h:163:
+#if __has_attribute(nonstring)
---
include/qemu/compiler.h | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h
index 261842beae..2d8f507c73 100644
--- a/include/qemu/compiler.h
+++ b/include/qemu/compiler.h
@@ -151,6 +151,21 @@
# define QEMU_ERROR(X)
#endif
+/*
+ * The nonstring variable attribute specifies that an object or member
+ * declaration with type array of char or pointer to char is intended
+ * to store character arrays that do not necessarily contain a terminating
+ * NUL character. This is useful in detecting uses of such arrays or pointers
+ * with functions that expect NUL-terminated strings, and to avoid warnings
+ * when such an array or pointer is used as an argument to a bounded string
+ * manipulation function such as strncpy.
+ */
+#if __has_attribute(nonstring)
+# define QEMU_NONSTRING __attribute__((nonstring))
+#else
+# define QEMU_NONSTRING
+#endif
+
/* Implement C11 _Generic via GCC builtins. Example:
*
* QEMU_GENERIC(x, (float, sinf), (long double, sinl), sin) (x)
--
2.17.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [Qemu-devel] [PATCH v5 2/5] block/sheepdog: Use QEMU_NONSTRING for non NUL-terminated arrays
2019-01-03 8:56 [Qemu-devel] [PATCH v5 0/5] Fix strncpy() warnings for GCC8 new -Wstringop-truncation Philippe Mathieu-Daudé
2019-01-03 8:56 ` [Qemu-devel] [PATCH v5 1/5] qemu/compiler: Define QEMU_NONSTRING Philippe Mathieu-Daudé
@ 2019-01-03 8:56 ` Philippe Mathieu-Daudé
2019-01-03 8:56 ` [Qemu-devel] [PATCH v5 3/5] hw/acpi: " Philippe Mathieu-Daudé
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Philippe Mathieu-Daudé @ 2019-01-03 8:56 UTC (permalink / raw)
To: qemu-devel
Cc: Dr. David Alan Gilbert, Thomas Huth, David Hildenbrand,
Igor Mammedov, Paolo Bonzini, Markus Armbruster,
Philippe Mathieu-Daudé, Marc-André Lureau, Eric Blake,
Michael S. Tsirkin, Daniel P. Berrangé, Liu Yuan, Jeff Cody,
Kevin Wolf, Max Reitz, open list:Sheepdog
GCC 8 added a -Wstringop-truncation warning:
The -Wstringop-truncation warning added in GCC 8.0 via r254630 for
bug 81117 is specifically intended to highlight likely unintended
uses of the strncpy function that truncate the terminating NUL
character from the source string.
This new warning leads to compilation failures:
CC block/sheepdog.o
qemu/block/sheepdog.c: In function 'find_vdi_name':
qemu/block/sheepdog.c:1239:5: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]
strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make: *** [qemu/rules.mak:69: block/sheepdog.o] Error 1
As described previous to the strncpy() calls, the use of strncpy() is
correct here:
/* This pair of strncpy calls ensures that the buffer is zero-filled,
* which is desirable since we'll soon be sending those bytes, and
* don't want the send_req to read uninitialized data.
*/
strncpy(buf, filename, SD_MAX_VDI_LEN);
strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
Use the QEMU_NONSTRING attribute, since this array is intended to store
character arrays that do not necessarily contain a terminating NUL.
Suggested-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
block/sheepdog.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/block/sheepdog.c b/block/sheepdog.c
index 0125df9d49..5cd9618432 100644
--- a/block/sheepdog.c
+++ b/block/sheepdog.c
@@ -1224,7 +1224,7 @@ static int find_vdi_name(BDRVSheepdogState *s, const char *filename,
SheepdogVdiReq hdr;
SheepdogVdiRsp *rsp = (SheepdogVdiRsp *)&hdr;
unsigned int wlen, rlen = 0;
- char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN];
+ char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN] QEMU_NONSTRING;
fd = connect_to_sdog(s, errp);
if (fd < 0) {
--
2.17.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [Qemu-devel] [PATCH v5 3/5] hw/acpi: Use QEMU_NONSTRING for non NUL-terminated arrays
2019-01-03 8:56 [Qemu-devel] [PATCH v5 0/5] Fix strncpy() warnings for GCC8 new -Wstringop-truncation Philippe Mathieu-Daudé
2019-01-03 8:56 ` [Qemu-devel] [PATCH v5 1/5] qemu/compiler: Define QEMU_NONSTRING Philippe Mathieu-Daudé
2019-01-03 8:56 ` [Qemu-devel] [PATCH v5 2/5] block/sheepdog: Use QEMU_NONSTRING for non NUL-terminated arrays Philippe Mathieu-Daudé
@ 2019-01-03 8:56 ` Philippe Mathieu-Daudé
2019-01-03 8:56 ` [Qemu-devel] [PATCH v5 4/5] migration: Fix stringop-truncation warning Philippe Mathieu-Daudé
2019-01-03 8:56 ` [Qemu-devel] [PATCH v5 5/5] migration: Use strnlen() for fixed-size string Philippe Mathieu-Daudé
4 siblings, 0 replies; 6+ messages in thread
From: Philippe Mathieu-Daudé @ 2019-01-03 8:56 UTC (permalink / raw)
To: qemu-devel
Cc: Dr. David Alan Gilbert, Thomas Huth, David Hildenbrand,
Igor Mammedov, Paolo Bonzini, Markus Armbruster,
Philippe Mathieu-Daudé, Marc-André Lureau, Eric Blake,
Michael S. Tsirkin, Daniel P. Berrangé
GCC 8 added a -Wstringop-truncation warning:
The -Wstringop-truncation warning added in GCC 8.0 via r254630 for
bug 81117 is specifically intended to highlight likely unintended
uses of the strncpy function that truncate the terminating NUL
character from the source string.
This new warning leads to compilation failures:
CC hw/acpi/core.o
In function 'acpi_table_install', inlined from 'acpi_table_add' at qemu/hw/acpi/core.c:296:5:
qemu/hw/acpi/core.c:184:9: error: 'strncpy' specified bound 4 equals destination size [-Werror=stringop-truncation]
strncpy(ext_hdr->sig, hdrs->sig, sizeof ext_hdr->sig);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make: *** [qemu/rules.mak:69: hw/acpi/core.o] Error 1
Use the QEMU_NONSTRING attribute, since ACPI tables don't require the
strings to be NUL-terminated.
Suggested-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
v4: rebased
v5: Fix checkpatch error: space prohibited before open square bracket '['
Note this triggers the following checkpatch warning (patchew),
which is incorrect because this is a multi-line macro,
thus we can not use Linux-kernel-style multiline comments here:
WARNING: Block comments use a leading /* on a separate line
#83: FILE: include/hw/acpi/acpi-defs.h:61:
+ QEMU_NONSTRING; /* OEM identification */ \
---
hw/acpi/core.c | 12 ++++++++----
include/hw/acpi/acpi-defs.h | 13 ++++++++-----
2 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/hw/acpi/core.c b/hw/acpi/core.c
index d6f0709691..47877c0ec1 100644
--- a/hw/acpi/core.c
+++ b/hw/acpi/core.c
@@ -35,14 +35,18 @@
struct acpi_table_header {
uint16_t _length; /* our length, not actual part of the hdr */
/* allows easier parsing for fw_cfg clients */
- char sig[4]; /* ACPI signature (4 ASCII characters) */
+ char sig[4]
+ QEMU_NONSTRING; /* ACPI signature (4 ASCII characters) */
uint32_t length; /* Length of table, in bytes, including header */
uint8_t revision; /* ACPI Specification minor version # */
uint8_t checksum; /* To make sum of entire table == 0 */
- char oem_id[6]; /* OEM identification */
- char oem_table_id[8]; /* OEM table identification */
+ char oem_id[6]
+ QEMU_NONSTRING; /* OEM identification */
+ char oem_table_id[8]
+ QEMU_NONSTRING; /* OEM table identification */
uint32_t oem_revision; /* OEM revision number */
- char asl_compiler_id[4]; /* ASL compiler vendor ID */
+ char asl_compiler_id[4]
+ QEMU_NONSTRING; /* ASL compiler vendor ID */
uint32_t asl_compiler_revision; /* ASL compiler revision number */
} QEMU_PACKED;
diff --git a/include/hw/acpi/acpi-defs.h b/include/hw/acpi/acpi-defs.h
index 5021cb9e79..4ed160afae 100644
--- a/include/hw/acpi/acpi-defs.h
+++ b/include/hw/acpi/acpi-defs.h
@@ -41,8 +41,8 @@ enum {
};
typedef struct AcpiRsdpData {
- uint8_t oem_id[6]; /* OEM identification */
- uint8_t revision; /* Must be 0 for 1.0, 2 for 2.0 */
+ uint8_t oem_id[6] QEMU_NONSTRING; /* OEM identification */
+ uint8_t revision; /* Must be 0 for 1.0, 2 for 2.0 */
unsigned *rsdt_tbl_offset;
unsigned *xsdt_tbl_offset;
@@ -57,10 +57,13 @@ typedef struct AcpiRsdpData {
uint32_t length; /* Length of table, in bytes, including header */ \
uint8_t revision; /* ACPI Specification minor version # */ \
uint8_t checksum; /* To make sum of entire table == 0 */ \
- uint8_t oem_id [6]; /* OEM identification */ \
- uint8_t oem_table_id [8]; /* OEM table identification */ \
+ uint8_t oem_id[6] \
+ QEMU_NONSTRING; /* OEM identification */ \
+ uint8_t oem_table_id[8] \
+ QEMU_NONSTRING; /* OEM table identification */ \
uint32_t oem_revision; /* OEM revision number */ \
- uint8_t asl_compiler_id [4]; /* ASL compiler vendor ID */ \
+ uint8_t asl_compiler_id[4] \
+ QEMU_NONSTRING; /* ASL compiler vendor ID */ \
uint32_t asl_compiler_revision; /* ASL compiler revision number */
--
2.17.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [Qemu-devel] [PATCH v5 4/5] migration: Fix stringop-truncation warning
2019-01-03 8:56 [Qemu-devel] [PATCH v5 0/5] Fix strncpy() warnings for GCC8 new -Wstringop-truncation Philippe Mathieu-Daudé
` (2 preceding siblings ...)
2019-01-03 8:56 ` [Qemu-devel] [PATCH v5 3/5] hw/acpi: " Philippe Mathieu-Daudé
@ 2019-01-03 8:56 ` Philippe Mathieu-Daudé
2019-01-03 8:56 ` [Qemu-devel] [PATCH v5 5/5] migration: Use strnlen() for fixed-size string Philippe Mathieu-Daudé
4 siblings, 0 replies; 6+ messages in thread
From: Philippe Mathieu-Daudé @ 2019-01-03 8:56 UTC (permalink / raw)
To: qemu-devel
Cc: Dr. David Alan Gilbert, Thomas Huth, David Hildenbrand,
Igor Mammedov, Paolo Bonzini, Markus Armbruster,
Philippe Mathieu-Daudé, Marc-André Lureau, Eric Blake,
Michael S. Tsirkin, Daniel P. Berrangé, Juan Quintela
From: Marc-André Lureau <marcandre.lureau@redhat.com>
GCC 8 added a -Wstringop-truncation warning:
The -Wstringop-truncation warning added in GCC 8.0 via r254630 for
bug 81117 is specifically intended to highlight likely unintended
uses of the strncpy function that truncate the terminating NUL
character from the source string.
This new warning leads to compilation failures:
CC migration/global_state.o
qemu/migration/global_state.c: In function 'global_state_store_running':
qemu/migration/global_state.c:45:5: error: 'strncpy' specified bound 100 equals destination size [-Werror=stringop-truncation]
strncpy((char *)global_state.runstate, state, sizeof(global_state.runstate));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make: *** [qemu/rules.mak:69: migration/global_state.o] Error 1
Adding an assert is enough to silence GCC.
(alternatively, we could hard-code "running")
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
[PMD: More verbose commit message]
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
migration/global_state.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/migration/global_state.c b/migration/global_state.c
index 8e8ab5c51e..01805c567a 100644
--- a/migration/global_state.c
+++ b/migration/global_state.c
@@ -42,6 +42,7 @@ int global_state_store(void)
void global_state_store_running(void)
{
const char *state = RunState_str(RUN_STATE_RUNNING);
+ assert(strlen(state) < sizeof(global_state.runstate));
strncpy((char *)global_state.runstate,
state, sizeof(global_state.runstate));
}
--
2.17.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [Qemu-devel] [PATCH v5 5/5] migration: Use strnlen() for fixed-size string
2019-01-03 8:56 [Qemu-devel] [PATCH v5 0/5] Fix strncpy() warnings for GCC8 new -Wstringop-truncation Philippe Mathieu-Daudé
` (3 preceding siblings ...)
2019-01-03 8:56 ` [Qemu-devel] [PATCH v5 4/5] migration: Fix stringop-truncation warning Philippe Mathieu-Daudé
@ 2019-01-03 8:56 ` Philippe Mathieu-Daudé
4 siblings, 0 replies; 6+ messages in thread
From: Philippe Mathieu-Daudé @ 2019-01-03 8:56 UTC (permalink / raw)
To: qemu-devel
Cc: Dr. David Alan Gilbert, Thomas Huth, David Hildenbrand,
Igor Mammedov, Paolo Bonzini, Markus Armbruster,
Philippe Mathieu-Daudé, Marc-André Lureau, Eric Blake,
Michael S. Tsirkin, Daniel P. Berrangé, Juan Quintela
GCC 8 introduced the -Wstringop-overflow, which detect buffer overflow
by string-modifying functions declared in <string.h>, such strncpy(),
used in global_state_store_running().
GCC indeed found an incorrect use of strlen(), because this array
is loaded by VMSTATE_BUFFER(runstate, GlobalState) then parsed
using qapi_enum_parse which does not get the buffer length.
Use strnlen() which returns sizeof(s->runstate) if the array is not
NUL-terminated, assert the size is within range, and enforce the array
to be NUL-terminated to avoid an overflow in qapi_enum_parse().
This fixes:
CC migration/global_state.o
qemu/migration/global_state.c: In function 'global_state_pre_save':
qemu/migration/global_state.c:109:15: error: 'strlen' argument 1 declared attribute 'nonstring' [-Werror=stringop-overflow=]
s->size = strlen((char *)s->runstate) + 1;
^~~~~~~~~~~~~~~~~~~~~~~~~~~
qemu/migration/global_state.c:24:13: note: argument 'runstate' declared here
uint8_t runstate[100] QEMU_NONSTRING;
^~~~~~~~
cc1: all warnings being treated as errors
make: *** [qemu/rules.mak:69: migration/global_state.o] Error 1
Suggested-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
v5: Use Linux-kernel-style multiline comments
---
migration/global_state.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/migration/global_state.c b/migration/global_state.c
index 01805c567a..2c8c447239 100644
--- a/migration/global_state.c
+++ b/migration/global_state.c
@@ -89,6 +89,17 @@ static int global_state_post_load(void *opaque, int version_id)
s->received = true;
trace_migrate_global_state_post_load(runstate);
+ if (strnlen((char *)s->runstate,
+ sizeof(s->runstate)) == sizeof(s->runstate)) {
+ /*
+ * This condition should never happen during migration, because
+ * all runstate names are shorter than 100 bytes (the size of
+ * s->runstate). However, a malicious stream could overflow
+ * the qapi_enum_parse() call, so we force the last character
+ * to a NUL byte.
+ */
+ s->runstate[sizeof(s->runstate) - 1] = '\0';
+ }
r = qapi_enum_parse(&RunState_lookup, runstate, -1, &local_err);
if (r == -1) {
@@ -107,7 +118,8 @@ static int global_state_pre_save(void *opaque)
GlobalState *s = opaque;
trace_migrate_global_state_pre_save((char *)s->runstate);
- s->size = strlen((char *)s->runstate) + 1;
+ s->size = strnlen((char *)s->runstate, sizeof(s->runstate)) + 1;
+ assert(s->size <= sizeof(s->runstate));
return 0;
}
--
2.17.2
^ permalink raw reply related [flat|nested] 6+ messages in thread