Skip to contentSkip to conversation

sheepdog: Replace strncpy() by g_strlcpy()

PATCH In review Philippe Mathieu-Daudé 3 participants 4 messages Aug 18, 2018 – Aug 20, 2018 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sheepdog: Replace strncpy() by g_strlcpy()

Fedora 29 comes with GCC 8.1 which added the 'stringop-truncation' checks.

Replace the strncpy() calls by g_strlcpy() to avoid the following warning:

block/sheepdog.c: In function 'find_vdi_name':
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);
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---
See http://lists.nongnu.org/archive/html/qemu-devel/2018-07/msg03723.html

 block/sheepdog.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
block/sheepdog.c +7 -7
index b229a664d9..5dc3d0c39e 100644
@@ -1224,19 +1224,19 @@ 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];
+    /* 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.
+     */
+    char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN] = { };
 
     fd = connect_to_sdog(s, errp);
     if (fd < 0) {
         return fd;
     }
 
-    /* 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);
+    g_strlcpy(buf, filename, SD_MAX_VDI_LEN);
+    g_strlcpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
 
     memset(&hdr, 0, sizeof(hdr));
     if (lock) {
Signature · 1 line
2.18.0
#1 lore raw
Quoted 23 lines · Philippe Mathieu-Daudé

Fedora 29 comes with GCC 8.1 which added the 'stringop-truncation' checks.

Replace the strncpy() calls by g_strlcpy() to avoid the following warning:

block/sheepdog.c: In function 'find_vdi_name':
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);
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---
See http://lists.nongnu.org/archive/html/qemu-devel/2018-07/msg03723.html

 block/sheepdog.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
block/sheepdog.c +0 -0
index b229a664d9..5dc3d0c39e 100644

block/sheepdog.c static int find_vdi_name(BDRVSheepdogState *s, const char *filename, · line 1230

Quoted 8 lines
block/sheepdog.c +4 -1
@@ -1224,19 +1224,19 @@ 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];
+    /* 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.
+     */

I this really necessary? The two g_strlcpy should still fill the whole buffer, no?

Quoted 19 lines
block/sheepdog.c +3 -6
+    char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN] = { };
 
     fd = connect_to_sdog(s, errp);
     if (fd < 0) {
         return fd;
     }
 
-    /* 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);
+    g_strlcpy(buf, filename, SD_MAX_VDI_LEN);
+    g_strlcpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
 
     memset(&hdr, 0, sizeof(hdr));
     if (lock) {
Signature · 3 lines
Thanks,

David / dhildenb
#2 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 40 lines · Philippe Mathieu-Daudé

Fedora 29 comes with GCC 8.1 which added the 'stringop-truncation' checks.

Replace the strncpy() calls by g_strlcpy() to avoid the following warning:

block/sheepdog.c: In function 'find_vdi_name':
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);
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---
See http://lists.nongnu.org/archive/html/qemu-devel/2018-07/msg03723.html

 block/sheepdog.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
block/sheepdog.c +5 -2
index b229a664d9..5dc3d0c39e 100644
@@ -1224,19 +1224,19 @@ 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];
+    /* 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.
+     */
+    char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN] = { };
 
     fd = connect_to_sdog(s, errp);
     if (fd < 0) {
         return fd;
     }
 
-    /* This pair of strncpy calls ensures that the buffer is zero-filled,

block/sheepdog.c static int find_vdi_name(BDRVSheepdogState *s, const char *filename, · line 1242

Quoted 10 lines
block/sheepdog.c +2 -5
-     * 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);
+    g_strlcpy(buf, filename, SD_MAX_VDI_LEN);
+    g_strlcpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
 
     memset(&hdr, 0, sizeof(hdr));
     if (lock) {

The protocol doesn't require (as far as I can see) the strings to be NULL-terminated, therefore strncpy is the right function to use here.

However, we should have a check on the length of filename and tag, so that no truncation is done. This applies to both strncpy and g_strlcpy.

Indeed I find g_strlcpy to be harmful because it encourages a style where truncations happen silently. There are very few cases where silent truncation is the right thing to do, and in several cases where you _have to have_ fixed-size buffers, those buffers are sent on the wire---and then g_strlcpy is wrong, while strncpy is just as good as memset+strlen+memcpy (and shorter).

Paolo

#3 lore raw
Quoted 58 lines · Paolo Bonzini
Quoted 40 lines · Philippe Mathieu-Daudé

Fedora 29 comes with GCC 8.1 which added the 'stringop-truncation' checks.

Replace the strncpy() calls by g_strlcpy() to avoid the following warning:

block/sheepdog.c: In function 'find_vdi_name':
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);
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---
See http://lists.nongnu.org/archive/html/qemu-devel/2018-07/msg03723.html

 block/sheepdog.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
block/sheepdog.c +5 -2
index b229a664d9..5dc3d0c39e 100644
@@ -1224,19 +1224,19 @@ 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];
+    /* 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.
+     */
+    char buf[SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN] = { };
 
     fd = connect_to_sdog(s, errp);
     if (fd < 0) {
         return fd;
     }
 
-    /* This pair of strncpy calls ensures that the buffer is zero-filled,

block/sheepdog.c static int find_vdi_name(BDRVSheepdogState *s, const char *filename, · line 1242

Quoted 10 lines
block/sheepdog.c +2 -5
-     * 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);
+    g_strlcpy(buf, filename, SD_MAX_VDI_LEN);
+    g_strlcpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
 
     memset(&hdr, 0, sizeof(hdr));
     if (lock) {

The protocol doesn't require (as far as I can see) the strings to be NULL-terminated, therefore strncpy is the right function to use here.

However, we should have a check on the length of filename and tag, so that no truncation is done. This applies to both strncpy and g_strlcpy.

Quoted 6 lines

Indeed I find g_strlcpy to be harmful because it encourages a style where truncations happen silently. There are very few cases where silent truncation is the right thing to do, and in several cases where you _have to have_ fixed-size buffers, those buffers are sent on the wire---and then g_strlcpy is wrong, while strncpy is just as good as memset+strlen+memcpy (and shorter).

Yes, convinced, let's disable the warning.

Quoted 3 lines

Paolo

Signature · 3 lines
Thanks,

David / dhildenb