Patchwork [1/2] cpu: microcode: ensure we don't overflow with strncpy

login
register
mail settings
Submitter Colin King
Date Nov. 6, 2012, 4:19 p.m.
Message ID <1352218770-13472-2-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/197497/
State Accepted
Headers show

Comments

Colin King - Nov. 6, 2012, 4:19 p.m.
From: Colin Ian King <colin.king@canonical.com>

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/cpu/microcode/microcode.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Keng-Yu Lin - Nov. 8, 2012, 5:53 a.m.
On Wed, Nov 7, 2012 at 12:19 AM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/cpu/microcode/microcode.c |    3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/src/cpu/microcode/microcode.c b/src/cpu/microcode/microcode.c
> index 50b19e1..7d9a607 100644
> --- a/src/cpu/microcode/microcode.c
> +++ b/src/cpu/microcode/microcode.c
> @@ -75,7 +75,8 @@ static void gather_info(fwts_framework *fw, fwts_list *cpus)
>                                         fwts_log_error(fw, "Cannot allocate memory.");
>                                         break;
>                                 }
> -                               strncpy(cpu->cpu, entry->d_name, 16);
> +                               strncpy(cpu->cpu, entry->d_name, sizeof(cpu->cpu) - 1);
> +                               cpu->cpu[sizeof(cpu->cpu) - 1] = '\0';
>                                 cpu->version = strtoul(data, NULL, 16);
>                                 free(data);
>                                 fwts_list_append(cpus, cpu);
> --
> 1.7.10.4
>
Acked-by: Keng-Yu Lin <kengyu@canonical.com>
Alex Hung - Nov. 21, 2012, 3:39 a.m.
On 11/07/2012 12:19 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/cpu/microcode/microcode.c |    3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/src/cpu/microcode/microcode.c b/src/cpu/microcode/microcode.c
> index 50b19e1..7d9a607 100644
> --- a/src/cpu/microcode/microcode.c
> +++ b/src/cpu/microcode/microcode.c
> @@ -75,7 +75,8 @@ static void gather_info(fwts_framework *fw, fwts_list *cpus)
>   					fwts_log_error(fw, "Cannot allocate memory.");
>   					break;
>   				}
> -				strncpy(cpu->cpu, entry->d_name, 16);
> +				strncpy(cpu->cpu, entry->d_name, sizeof(cpu->cpu) - 1);
> +				cpu->cpu[sizeof(cpu->cpu) - 1] = '\0';
>   				cpu->version = strtoul(data, NULL, 16);
>   				free(data);
>   				fwts_list_append(cpus, cpu);
>
Acked-by: Alex Hung <alex.hung@canonical.com>

Patch

diff --git a/src/cpu/microcode/microcode.c b/src/cpu/microcode/microcode.c
index 50b19e1..7d9a607 100644
--- a/src/cpu/microcode/microcode.c
+++ b/src/cpu/microcode/microcode.c
@@ -75,7 +75,8 @@  static void gather_info(fwts_framework *fw, fwts_list *cpus)
 					fwts_log_error(fw, "Cannot allocate memory.");
 					break;
 				}
-				strncpy(cpu->cpu, entry->d_name, 16);
+				strncpy(cpu->cpu, entry->d_name, sizeof(cpu->cpu) - 1);
+				cpu->cpu[sizeof(cpu->cpu) - 1] = '\0';
 				cpu->version = strtoul(data, NULL, 16);
 				free(data);
 				fwts_list_append(cpus, cpu);