Skip to contentSkip to conversation

examples/vm_power: fix strcpy buffer overrun

PATCH Superseded David Hunt 4 participants 9 messages Jul 12, 2019 – Jul 16, 2019

lore
Share & tools
lore

David Hunt

author
#0 lore raw

PATCH 1/1 examples/vm_power: fix strcpy buffer overrun

replace strcpy with rte_strlcpy to prevent buffer overrun With fix, attempting to use a VERY lonng vm name results in a nicely truncated 32 character name rather than a segfault:
Setting VM Name to [sdfdsfsfsdffdsdsasdsadasdakjshd]

Fixes 59287933a0bb ("examples/vm_power: add options to guest app")
---
 examples/vm_power_manager/guest_cli/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
examples/vm_power_manager/guest_cli/main.c +1 -1
index 36365b124..a18eb214a 100644
@@ -65,7 +65,7 @@ parse_args(int argc, char **argv)
 		switch (opt) {
 		/* portmask */
 		case 'n':
-			strcpy(policy->vm_name, optarg);
+			rte_strlcpy(policy->vm_name, optarg, VM_MAX_NAME_SZ);
 			printf("Setting VM Name to [%s]\n", policy->vm_name);
 			break;
 		case 'b':
Signature · 1 line
2.17.1
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · David Hunt

replace strcpy with rte_strlcpy to prevent buffer overrun With fix, attempting to use a VERY lonng vm name results in a nicely truncated 32 character name rather than a segfault:
Setting VM Name to [sdfdsfsfsdffdsdsasdsadasdakjshd]

Fixes 59287933a0bb ("examples/vm_power: add options to guest app")

---

Signature · 2 lines
Thanks,
Anatoly
#2 lore raw
In reply to David Hunt
Quoted 18 lines · David Hunt

replace strcpy with rte_strlcpy to prevent buffer overrun With fix, attempting to use a VERY lonng vm name results in a nicely truncated 32 character name rather than a segfault:
Setting VM Name to [sdfdsfsfsdffdsdsasdsadasdakjshd]

Fixes 59287933a0bb ("examples/vm_power: add options to guest app")
---
 examples/vm_power_manager/guest_cli/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
examples/vm_power_manager/guest_cli/main.c +0 -0
index 36365b124..a18eb214a 100644

examples/vm_power_manager/guest_cli/main.c parse_args(int argc, char **argv) · line 71

Quoted 8 lines
examples/vm_power_manager/guest_cli/main.c +1 -1
@@ -65,7 +65,7 @@ parse_args(int argc, char **argv)
 		switch (opt) {
 		/* portmask */
 		case 'n':
-			strcpy(policy->vm_name, optarg);
+			rte_strlcpy(policy->vm_name, optarg, VM_MAX_NAME_SZ);
 			printf("Setting VM Name to [%s]\n", policy->vm_name);
 			break;
 		case 'b':

You can just use "strlcpy" without the "rte_" prefix. The rte_ version is just a fallback used when a standard strlcpy - either natively or from libbsd - is unavailable.

#3 lore raw

12/07/2019 16:47, Bruce Richardson:

Quoted 9 lines · David Hunt
Quoted 8 lines

replace strcpy with rte_strlcpy to prevent buffer overrun With fix, attempting to use a VERY lonng vm name results in a nicely truncated 32 character name rather than a segfault:
Setting VM Name to [sdfdsfsfsdffdsdsasdsadasdakjshd]

Fixes 59287933a0bb ("examples/vm_power: add options to guest app")

It should be in this order:

Fixes 59287933a0bb ("examples/vm_power: add options to guest app")

When in doubt about formatting, please check the git history. You will find that the recent title prefix in use was "examples/power".

Quoted 8 lines

examples/vm_power_manager/guest_cli/main.c parse_args(int argc, char **argv) · line 68

Quoted 4 lines
examples/vm_power_manager/guest_cli/main.c +1 -1
-			strcpy(policy->vm_name, optarg);
+			rte_strlcpy(policy->vm_name, optarg, VM_MAX_NAME_SZ);

You can just use "strlcpy" without the "rte_" prefix. The rte_ version is just a fallback used when a standard strlcpy - either natively or from libbsd - is unavailable.

Please replace the 2 other occurences of rte_strlcpy in this example (can be in the same patch with a small comment in the commit log).

author
#4 lore raw

PATCH v2 1/1 examples/vm_power: fix strcpy buffer overrun

In reply to David Hunt

replace strcpy with strlcpy to prevent buffer overrun With fix, attempting to use a VERY lonng vm name results in a nicely truncated 32 character name rather than a segfault:
Setting VM Name to [sdfdsfsfsdffdsdsasdsadasdakjshd]

Using strlcpy rather than rte_strlcpy, as the rte_ version is only a fallback.

As well as the fix in main.c, this patch also changes an occurrence of rte_strlcpy in channel_manager.c and channel_monitor.c to strlcpy.

Fixes 59287933a0bb ("examples/vm_power: add options to guest app")
---
 examples/vm_power_manager/channel_manager.c | 2 +-
 examples/vm_power_manager/channel_monitor.c | 2 +-
 examples/vm_power_manager/guest_cli/main.c  | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
examples/vm_power_manager/channel_manager.c +1 -1
index 2c1332257..4db225755 100644
@@ -588,7 +588,7 @@ add_host_channels(void)
 			goto error;
 		}
 		chan_infos[i] = chan_info;
-		rte_strlcpy(chan_info->channel_path, socket_path,
+		strlcpy(chan_info->channel_path, socket_path,
 				sizeof(chan_info->channel_path));
 
 		if (setup_host_channel_info(&chan_info, i) < 0) {
examples/vm_power_manager/channel_monitor.c +1 -1
index 9d7474da0..496772f8a 100644
@@ -309,7 +309,7 @@ parse_json_to_pkt(json_t *element, struct channel_packet *pkt,
 				vm_name);
 			return -1;
 		}
-		rte_strlcpy(pkt->vm_name, vm_name, VM_MAX_NAME_SZ);
+		strlcpy(pkt->vm_name, vm_name, VM_MAX_NAME_SZ);
 		pkt->resource_id = resource_id;
 	}
 	return 0;
examples/vm_power_manager/guest_cli/main.c +1 -1
index 36365b124..a18eb214a 100644
@@ -65,7 +65,7 @@ parse_args(int argc, char **argv)
 		switch (opt) {
 		/* portmask */
 		case 'n':
-			strcpy(policy->vm_name, optarg);
+			rte_strlcpy(policy->vm_name, optarg, VM_MAX_NAME_SZ);
 			printf("Setting VM Name to [%s]\n", policy->vm_name);
 			break;
 		case 'b':
Signature · 1 line
2.17.1
#5 lore raw

16/07/2019 10:24, David Hunt:

Quoted 10 lines

replace strcpy with strlcpy to prevent buffer overrun With fix, attempting to use a VERY lonng vm name results in a nicely truncated 32 character name rather than a segfault:
Setting VM Name to [sdfdsfsfsdffdsdsasdsadasdakjshd]

Using strlcpy rather than rte_strlcpy, as the rte_ version is only a fallback.

As well as the fix in main.c, this patch also changes an occurrence of rte_strlcpy in channel_manager.c and channel_monitor.c to strlcpy.

[...]

examples/vm_power_manager/guest_cli/main.c parse_args(int argc, char **argv) · line 68

Quoted 4 lines
examples/vm_power_manager/guest_cli/main.c +1 -1
-			strcpy(policy->vm_name, optarg);
+			rte_strlcpy(policy->vm_name, optarg, VM_MAX_NAME_SZ);

This is still using rte_strlcpy !!

#6 lore raw
Quoted 9 lines · Thomas Monjalon

16/07/2019 10:24, David Hunt:

Quoted 8 lines

replace strcpy with strlcpy to prevent buffer overrun With fix, attempting to use a VERY lonng vm name results in a nicely truncated 32 character name rather than a segfault:
Setting VM Name to [sdfdsfsfsdffdsdsasdsadasdakjshd]

Using strlcpy rather than rte_strlcpy, as the rte_ version is only a fallback.

Quoted 8 lines
Quoted 2 lines

As well as the fix in main.c, this patch also changes an occurrence of rte_strlcpy in channel_manager.c and channel_monitor.c to strlcpy.

[...]

examples/vm_power_manager/guest_cli/main.c parse_args(int argc, char **argv) · line 68

Quoted 4 lines
examples/vm_power_manager/guest_cli/main.c +1 -1
-			strcpy(policy->vm_name, optarg);
+			rte_strlcpy(policy->vm_name, optarg, VM_MAX_NAME_SZ);

This is still using rte_strlcpy !!

Apologies, too many balls in the air.  v3 pushed.

author
#7 lore raw

PATCH v3 1/1 examples/vm_power: fix strcpy buffer overrun

In reply to David Hunt

replace strcpy with strlcpy to prevent buffer overrun With fix, attempting to use a VERY lonng vm name results in a nicely truncated 32 character name rather than a segfault:
Setting VM Name to [sdfdsfsfsdffdsdsasdsadasdakjshd]

Using strlcpy rather than rte_strlcpy, as the rte_ version is only a fallback.

As well as the fix in main.c, this patch also changes an occurrence of rte_strlcpy in channel_manager.c and channel_monitor.c to strlcpy.

Fixes 59287933a0bb ("examples/vm_power: add options to guest app")
---
 examples/vm_power_manager/channel_manager.c | 2 +-
 examples/vm_power_manager/channel_monitor.c | 2 +-
 examples/vm_power_manager/guest_cli/main.c  | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
examples/vm_power_manager/channel_manager.c +1 -1
index 2c1332257..4db225755 100644
@@ -588,7 +588,7 @@ add_host_channels(void)
 			goto error;
 		}
 		chan_infos[i] = chan_info;
-		rte_strlcpy(chan_info->channel_path, socket_path,
+		strlcpy(chan_info->channel_path, socket_path,
 				sizeof(chan_info->channel_path));
 
 		if (setup_host_channel_info(&chan_info, i) < 0) {
examples/vm_power_manager/channel_monitor.c +1 -1
index 9d7474da0..496772f8a 100644
@@ -309,7 +309,7 @@ parse_json_to_pkt(json_t *element, struct channel_packet *pkt,
 				vm_name);
 			return -1;
 		}
-		rte_strlcpy(pkt->vm_name, vm_name, VM_MAX_NAME_SZ);
+		strlcpy(pkt->vm_name, vm_name, VM_MAX_NAME_SZ);
 		pkt->resource_id = resource_id;
 	}
 	return 0;
examples/vm_power_manager/guest_cli/main.c +1 -1
index 36365b124..2094145eb 100644
@@ -65,7 +65,7 @@ parse_args(int argc, char **argv)
 		switch (opt) {
 		/* portmask */
 		case 'n':
-			strcpy(policy->vm_name, optarg);
+			strlcpy(policy->vm_name, optarg, VM_MAX_NAME_SZ);
 			printf("Setting VM Name to [%s]\n", policy->vm_name);
 			break;
 		case 'b':
Signature · 1 line
2.17.1
#8 lore raw

16/07/2019 13:19, David Hunt:

Quoted 8 lines

replace strcpy with strlcpy to prevent buffer overrun With fix, attempting to use a VERY lonng vm name results in a nicely truncated 32 character name rather than a segfault:
Setting VM Name to [sdfdsfsfsdffdsdsasdsadasdakjshd]

Using strlcpy rather than rte_strlcpy, as the rte_ version is only a fallback.

Quoted 7 lines

As well as the fix in main.c, this patch also changes an occurrence of rte_strlcpy in channel_manager.c and channel_monitor.c to strlcpy.

Fixes 59287933a0bb ("examples/vm_power: add options to guest app")

Applied, thanks