Skip to contentSkip to conversation

migration/dirtyrate: Silence warning about strcpy() on OpenBSD

PATCH Superseded Thomas Huth 5 participants 6 messages Oct 16, 2024 – Oct 17, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 migration/dirtyrate: Silence warning about strcpy() on OpenBSD

The linker on OpenBSD complains:

ld: warning: dirtyrate.c:447 (../src/migration/dirtyrate.c:447)(...):
warning: strcpy() is almost always misused, please use strlcpy()

It's currently not a real problem in this case since both arrays have the same size (256 bytes). But just in case somebody changes the size of the source array in the future, let's better play safe and use g_strlcpy() here instead.

---
 migration/dirtyrate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/dirtyrate.c +1 -1
index 233acb0855..090c76e934 100644
@@ -444,7 +444,7 @@ static void get_ramblock_dirty_info(RAMBlock *block,
     info->ramblock_pages = qemu_ram_get_used_length(block) >>
                            qemu_target_page_bits();
     info->ramblock_addr = qemu_ram_get_host_addr(block);
-    strcpy(info->idstr, qemu_ram_get_idstr(block));
+    g_strlcpy(info->idstr, qemu_ram_get_idstr(block), sizeof(info->idstr));
 }
 
 static void free_ramblock_dirty_info(struct RamblockDirtyInfo *infos, int count)
Signature · 1 line
2.47.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Thomas Huth

The linker on OpenBSD complains:

ld: warning: dirtyrate.c:447 (../src/migration/dirtyrate.c:447)(...):
warning: strcpy() is almost always misused, please use strlcpy()

Is that the only place it complains ? We use 'strcpy' in almost 100 places across the codebase....

Quoted 15 lines

It's currently not a real problem in this case since both arrays have the same size (256 bytes). But just in case somebody changes the size of the source array in the future, let's better play safe and use g_strlcpy() here instead.

---
 migration/dirtyrate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/dirtyrate.c +0 -0
index 233acb0855..090c76e934 100644

migration/dirtyrate.c static void get_ramblock_dirty_info(RAMBlock *block, · line 448

Quoted 6 lines
migration/dirtyrate.c +1 -1
@@ -444,7 +444,7 @@ static void get_ramblock_dirty_info(RAMBlock *block,
     info->ramblock_pages = qemu_ram_get_used_length(block) >>
                            qemu_target_page_bits();
     info->ramblock_addr = qemu_ram_get_host_addr(block);
-    strcpy(info->idstr, qemu_ram_get_idstr(block));
+    g_strlcpy(info->idstr, qemu_ram_get_idstr(block), sizeof(info->idstr));
 }

Is it worth also adding

  G_STATIC_ASSERT(sizeof((struct RamblockDirtyInfo){}.idstr) ==
                  sizeof((struct RAMBlock){}.idstr));

at the top of this file, since both of these fields are expected to be the same size by this code, to avoid truncation.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#2 lore raw
Quoted 8 lines · Daniel P. Berrangé
Quoted 4 lines · Thomas Huth

The linker on OpenBSD complains:

ld: warning: dirtyrate.c:447 (../src/migration/dirtyrate.c:447)(...):
warning: strcpy() is almost always misused, please use strlcpy()

Is that the only place it complains ? We use 'strcpy' in almost 100 places across the codebase....

I remember we switched some places to pstrcpy(), which seems superior, where I saw tha g_strlcpy() is mostly strlcpy() and the man page says:

strlcpy(3bsd) and strlcat(3bsd) are similar, but have important performance problems; see BUGS.

...

BUGS

...

strlcpy(3) and strlcat(3) need to read the entire src string, even if the destination buffer is small. This makes them vulnerable to Denial of Service (DoS) attacks if an attacker can control the length of the src string. And if not, they're still unnecessarily slow.

Quoted 26 lines
Quoted 16 lines

It's currently not a real problem in this case since both arrays have the same size (256 bytes). But just in case somebody changes the size of the source array in the future, let's better play safe and use g_strlcpy() here instead.

---
 migration/dirtyrate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/dirtyrate.c +0 -0
index 233acb0855..090c76e934 100644

migration/dirtyrate.c static void get_ramblock_dirty_info(RAMBlock *block, · line 448

Quoted 6 lines
migration/dirtyrate.c +1 -1
@@ -444,7 +444,7 @@ static void get_ramblock_dirty_info(RAMBlock *block,
     info->ramblock_pages = qemu_ram_get_used_length(block) >>
                            qemu_target_page_bits();
     info->ramblock_addr = qemu_ram_get_host_addr(block);
-    strcpy(info->idstr, qemu_ram_get_idstr(block));
+    g_strlcpy(info->idstr, qemu_ram_get_idstr(block), sizeof(info->idstr));
 }
Quoted 7 lines

Is it worth also adding

  G_STATIC_ASSERT(sizeof((struct RamblockDirtyInfo){}.idstr) ==
                  sizeof((struct RAMBlock){}.idstr));

at the top of this file, since both of these fields are expected to be the same size by this code, to avoid truncation.

Or we could define a size macro for RAMBlock.idstr and use it everywhere where it's fundamentally the same thing.

Quoted 9 lines

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Signature · 1 line
Peter Xu
#3 lore raw
In reply to Daniel P. Berrangé
Quoted 8 lines · Daniel P. Berrangé
Quoted 4 lines · Thomas Huth

The linker on OpenBSD complains:

ld: warning: dirtyrate.c:447 (../src/migration/dirtyrate.c:447)(...):
warning: strcpy() is almost always misused, please use strlcpy()

Is that the only place it complains ? We use 'strcpy' in almost 100 places across the codebase....

There are only a fistful of other warnings. I guess most of the spots are turned into inlined code by the compiler, so the linker never sees those other occurrences.

Quoted 25 lines
Quoted 15 lines

It's currently not a real problem in this case since both arrays have the same size (256 bytes). But just in case somebody changes the size of the source array in the future, let's better play safe and use g_strlcpy() here instead.

---
  migration/dirtyrate.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
migration/dirtyrate.c +0 -0
index 233acb0855..090c76e934 100644

migration/dirtyrate.c static void get_ramblock_dirty_info(RAMBlock *block, · line 448

Quoted 6 lines
migration/dirtyrate.c +1 -1
@@ -444,7 +444,7 @@ static void get_ramblock_dirty_info(RAMBlock *block,
      info->ramblock_pages = qemu_ram_get_used_length(block) >>
                             qemu_target_page_bits();
      info->ramblock_addr = qemu_ram_get_host_addr(block);
-    strcpy(info->idstr, qemu_ram_get_idstr(block));
+    g_strlcpy(info->idstr, qemu_ram_get_idstr(block), sizeof(info->idstr));
  }
Quoted 7 lines

Is it worth also adding

   G_STATIC_ASSERT(sizeof((struct RamblockDirtyInfo){}.idstr) ==
                   sizeof((struct RAMBlock){}.idstr));

at the top of this file, since both of these fields are expected to be the same size by this code, to avoid truncation.

... or alternatively check the return value of g_strlcpy() ? ... but that wouldn't work if pstrcpy() if we switch to that function instead.

I don't mind either way - Peter, Fabiano, Hyman, what's your opinion here?

Thomas

#4 lore raw
Quoted 48 lines · Thomas Huth
Quoted 8 lines · Daniel P. Berrangé
Quoted 4 lines · Thomas Huth

The linker on OpenBSD complains:

ld: warning: dirtyrate.c:447 (../src/migration/dirtyrate.c:447)(...):
warning: strcpy() is almost always misused, please use strlcpy()

Is that the only place it complains ? We use 'strcpy' in almost 100 places across the codebase....

There are only a fistful of other warnings. I guess most of the spots are turned into inlined code by the compiler, so the linker never sees those other occurrences.

Quoted 10 lines
Quoted 10 lines

It's currently not a real problem in this case since both arrays have the same size (256 bytes). But just in case somebody changes the size of the source array in the future, let's better play safe and use g_strlcpy() here instead.

---
  migration/dirtyrate.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 10 lines

migration/dirtyrate.c static void get_ramblock_dirty_info(RAMBlock *block, · line 447

Quoted 10 lines
migration/dirtyrate.c +1 -1
index 233acb0855..090c76e934 100644
@@ -444,7 +444,7 @@ static void get_ramblock_dirty_info(RAMBlock *block,
      info->ramblock_pages = qemu_ram_get_used_length(block) >>
                             qemu_target_page_bits();
      info->ramblock_addr = qemu_ram_get_host_addr(block);
-    strcpy(info->idstr, qemu_ram_get_idstr(block));
+    g_strlcpy(info->idstr, qemu_ram_get_idstr(block),

sizeof(info->idstr));

Quoted 5 lines

migration/dirtyrate.c static void get_ramblock_dirty_info(RAMBlock *block, · line 448

Quoted 1 line
migration/dirtyrate.c +0 -0
  }
Quoted 7 lines

Is it worth also adding

   G_STATIC_ASSERT(sizeof((struct RamblockDirtyInfo){}.idstr) ==
                   sizeof((struct RAMBlock){}.idstr));

at the top of this file, since both of these fields are expected to be the same size by this code, to avoid truncation.

Quoted 8 lines

... or alternatively check the return value of g_strlcpy() ? ... but that wouldn't work if pstrcpy() if we switch to that function instead.

I don't mind either way - Peter, Fabiano, Hyman, what's your opinion here?

Thomas

Since RamblockDirtyInfo is only used in dirtyrate.c, I'm inclined to just check the return value of g_strlcpy to avoid DoS attack, and fix this warning case by case.

Thanks,

Yong

Signature · 1 line
Best regards
#5 lore raw
In reply to Thomas Huth
Quoted 8 lines · Thomas Huth
Quoted 6 lines · Daniel P. Berrangé
Quoted 4 lines · Thomas Huth

The linker on OpenBSD complains:

ld: warning: dirtyrate.c:447 (../src/migration/dirtyrate.c:447)(...):
warning: strcpy() is almost always misused, please use strlcpy()

Quoted 6 lines
Quoted 2 lines

Is that the only place it complains ? We use 'strcpy' in almost 100 places across the codebase....

There are only a fistful of other warnings. I guess most of the spots are turned into inlined code by the compiler, so the linker never sees those other occurrences.

From my vm-build-openbsd buildlog:

$ < /tmp/par0L_eB.par grep 'almost always misused'| wc -l
305

(I've had these in my "ignore this type of warning" filter for grepping the build logs for years.)

-- PMM