Skip to contentSkip to conversation

linux-user: avoid string truncation warnings in uname field copying

PATCH Applied Daniel P. Berrangé 4 participants 5 messages May 1, 2019 – May 2, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 linux-user: avoid string truncation warnings in uname field copying

In file included from /usr/include/string.h:494,

                 from include/qemu/osdep.h:101,
                 from linux-user/uname.c:20:
In function ‘strncpy’,
    inlined from ‘sys_uname’ at linux-user/uname.c:94:3:
/usr/include/bits/string_fortified.h:106:10: warning: ‘__builtin_strncpy’ output may be truncated copying 64 bytes from a string of length 64 [-Wstringop-truncation]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We don't care where the NUL terminator in the original uname field was. It suffices to copy the entire original field and simply force a NUL terminator at the end of the new field.

---

Changed in v2:

- Always use sizeof() in preference to __NEW_UTS_LEN

 linux-user/uname.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
linux-user/uname.c +2 -3
index 313b79dbad..1c05f95387 100644
@@ -72,9 +72,8 @@ const char *cpu_to_uname_machine(void *cpu_env)
 
 #define COPY_UTSNAME_FIELD(dest, src) \
   do { \
-      /* __NEW_UTS_LEN doesn't include terminating null */ \
-      (void) strncpy((dest), (src), __NEW_UTS_LEN); \
-      (dest)[__NEW_UTS_LEN] = '\0'; \
+      memcpy((dest), (src), MIN(sizeof(src), sizeof(dest))); \
+      (dest)[sizeof(dest) - 1] = '\0'; \
   } while (0)
 
 int sys_uname(struct new_utsname *buf)
Signature · 1 line
2.21.0
#1 lore raw
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [Qemu-devel] [PATCH v2] linux-user: avoid string truncation warnings in uname field copying

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
c08b5ed306 linux-user: avoid string truncation warnings in uname field copying

=== OUTPUT BEGIN ===
ERROR: suspect code indent for conditional statements (2, 6) #30: FILE: linux-user/uname.c:74:
do { \
+ memcpy((dest), (src), MIN(sizeof(src), sizeof(dest))); \

total: 1 errors, 0 warnings, 11 lines checked

Commit c08b5ed30674 (linux-user: avoid string truncation warnings in uname field copying) has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

Reviewed-by on v2 1/1
#2 lore raw
In reply to Daniel P. Berrangé
Quoted 9 lines · Daniel P. Berrangé

In file included from /usr/include/string.h:494,

                 from include/qemu/osdep.h:101,
                 from linux-user/uname.c:20:
In function ‘strncpy’,
    inlined from ‘sys_uname’ at linux-user/uname.c:94:3:
/usr/include/bits/string_fortified.h:106:10: warning: ‘__builtin_strncpy’ output may be truncated copying 64 bytes from a string of length 64 [-Wstringop-truncation]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 5 lines

We don't care where the NUL terminator in the original uname field was. It suffices to copy the entire original field and simply force a NUL terminator at the end of the new field.

Quoted 26 lines

---

Changed in v2:

- Always use sizeof() in preference to __NEW_UTS_LEN

 linux-user/uname.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
linux-user/uname.c +2 -3
index 313b79dbad..1c05f95387 100644
@@ -72,9 +72,8 @@ const char *cpu_to_uname_machine(void *cpu_env)
 
 #define COPY_UTSNAME_FIELD(dest, src) \
   do { \
-      /* __NEW_UTS_LEN doesn't include terminating null */ \
-      (void) strncpy((dest), (src), __NEW_UTS_LEN); \
-      (dest)[__NEW_UTS_LEN] = '\0'; \
+      memcpy((dest), (src), MIN(sizeof(src), sizeof(dest))); \
+      (dest)[sizeof(dest) - 1] = '\0'; \
   } while (0)
 
 int sys_uname(struct new_utsname *buf)
#3 lore raw
In reply to Daniel P. Berrangé
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [Qemu-devel] [PATCH v2] linux-user: avoid string truncation warnings in uname field copying

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
t [tag update] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
b97fa7be34 linux-user: avoid string truncation warnings in uname field copying

=== OUTPUT BEGIN ===
ERROR: suspect code indent for conditional statements (2, 6) #31: FILE: linux-user/uname.c:74:
do { \
+ memcpy((dest), (src), MIN(sizeof(src), sizeof(dest))); \

total: 1 errors, 0 warnings, 11 lines checked

Commit b97fa7be344e (linux-user: avoid string truncation warnings in uname field copying) has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#4 lore raw
In reply to Daniel P. Berrangé
Quoted 30 lines · Daniel P. Berrangé

In file included from /usr/include/string.h:494,

                 from include/qemu/osdep.h:101,
                 from linux-user/uname.c:20:
In function ‘strncpy’,
    inlined from ‘sys_uname’ at linux-user/uname.c:94:3:
/usr/include/bits/string_fortified.h:106:10: warning: ‘__builtin_strncpy’ output may be truncated copying 64 bytes from a string of length 64 [-Wstringop-truncation]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We don't care where the NUL terminator in the original uname field was. It suffices to copy the entire original field and simply force a NUL terminator at the end of the new field.

---

Changed in v2:

- Always use sizeof() in preference to __NEW_UTS_LEN

 linux-user/uname.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
linux-user/uname.c +0 -0
index 313b79dbad..1c05f95387 100644
@@ -72,9 +72,8 @@ const char *cpu_to_uname_machine(void *cpu_env)
 

linux-user/uname.c const char *cpu_to_uname_machine(void *cpu_env) · line 79

Quoted 10 lines
linux-user/uname.c +2 -3
 #define COPY_UTSNAME_FIELD(dest, src) \
   do { \
-      /* __NEW_UTS_LEN doesn't include terminating null */ \
-      (void) strncpy((dest), (src), __NEW_UTS_LEN); \
-      (dest)[__NEW_UTS_LEN] = '\0'; \
+      memcpy((dest), (src), MIN(sizeof(src), sizeof(dest))); \
+      (dest)[sizeof(dest) - 1] = '\0'; \
   } while (0)
 
 int sys_uname(struct new_utsname *buf)

Applied to my linux-user branch.

Thanks,
Laurent