diff mbox

[1/2] hvmloader: correctly copy signature to info structures

Message ID 1471593967-29166-2-git-send-email-wei.liu2@citrix.com (mailing list archive)
State New, archived
Headers show

Commit Message

Wei Liu Aug. 19, 2016, 8:06 a.m. UTC
The original code used sizeof(info->signature) as the size parameter for
memcpy, which was wrong.

Fix that by calculating the correct size.

Signed-off-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/firmware/hvmloader/ovmf.c    | 3 ++-
 tools/firmware/hvmloader/seabios.c | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

Comments

Jan Beulich Aug. 19, 2016, 8:25 a.m. UTC | #1
>>> On 19.08.16 at 10:06, <wei.liu2@citrix.com> wrote:
> --- a/tools/firmware/hvmloader/ovmf.c
> +++ b/tools/firmware/hvmloader/ovmf.c
> @@ -67,10 +67,11 @@ struct ovmf_info {
>  static void ovmf_setup_bios_info(void)
>  {
>      struct ovmf_info *info = (void *)OVMF_INFO_PHYSICAL_ADDRESS;
> +    const char sig[] = "XenHVMOVMF";
>  
>      memset(info, 0, sizeof(*info));
>  
> -    memcpy(info->signature, "XenHVMOVMF", sizeof(info->signature));
> +    memcpy(info->signature, sig, sizeof(sig));
>      info->length = sizeof(*info);
>  }

I think using strncpy() would be more natural in cases like this,
as it would at once make clear that the destination can't be
overrun no matter how large the string literal.

Jan
Andrew Cooper Aug. 19, 2016, 9:42 a.m. UTC | #2
On 19/08/16 09:25, Jan Beulich wrote:
>>>> On 19.08.16 at 10:06, <wei.liu2@citrix.com> wrote:
>> --- a/tools/firmware/hvmloader/ovmf.c
>> +++ b/tools/firmware/hvmloader/ovmf.c
>> @@ -67,10 +67,11 @@ struct ovmf_info {
>>  static void ovmf_setup_bios_info(void)
>>  {
>>      struct ovmf_info *info = (void *)OVMF_INFO_PHYSICAL_ADDRESS;
>> +    const char sig[] = "XenHVMOVMF";
>>  
>>      memset(info, 0, sizeof(*info));
>>  
>> -    memcpy(info->signature, "XenHVMOVMF", sizeof(info->signature));
>> +    memcpy(info->signature, sig, sizeof(sig));
>>      info->length = sizeof(*info);
>>  }
> I think using strncpy() would be more natural in cases like this,
> as it would at once make clear that the destination can't be
> overrun no matter how large the string literal.

How about structure assignment?

*info = (struct ovmf_info) { .signature = "XenHVMOVMF", .length =
sizeof(*info) }

which also subsumed the memset()?

~Andrew
Jan Beulich Aug. 19, 2016, 11:58 a.m. UTC | #3
>>> On 19.08.16 at 11:42, <andrew.cooper3@citrix.com> wrote:
> On 19/08/16 09:25, Jan Beulich wrote:
>>>>> On 19.08.16 at 10:06, <wei.liu2@citrix.com> wrote:
>>> --- a/tools/firmware/hvmloader/ovmf.c
>>> +++ b/tools/firmware/hvmloader/ovmf.c
>>> @@ -67,10 +67,11 @@ struct ovmf_info {
>>>  static void ovmf_setup_bios_info(void)
>>>  {
>>>      struct ovmf_info *info = (void *)OVMF_INFO_PHYSICAL_ADDRESS;
>>> +    const char sig[] = "XenHVMOVMF";
>>>  
>>>      memset(info, 0, sizeof(*info));
>>>  
>>> -    memcpy(info->signature, "XenHVMOVMF", sizeof(info->signature));
>>> +    memcpy(info->signature, sig, sizeof(sig));
>>>      info->length = sizeof(*info);
>>>  }
>> I think using strncpy() would be more natural in cases like this,
>> as it would at once make clear that the destination can't be
>> overrun no matter how large the string literal.
> 
> How about structure assignment?
> 
> *info = (struct ovmf_info) { .signature = "XenHVMOVMF", .length =
> sizeof(*info) }
> 
> which also subsumed the memset()?

Fine with me, albeit maybe a little uglier to read.

Jan
diff mbox

Patch

diff --git a/tools/firmware/hvmloader/ovmf.c b/tools/firmware/hvmloader/ovmf.c
index b4bcc93..a4ed661 100644
--- a/tools/firmware/hvmloader/ovmf.c
+++ b/tools/firmware/hvmloader/ovmf.c
@@ -67,10 +67,11 @@  struct ovmf_info {
 static void ovmf_setup_bios_info(void)
 {
     struct ovmf_info *info = (void *)OVMF_INFO_PHYSICAL_ADDRESS;
+    const char sig[] = "XenHVMOVMF";
 
     memset(info, 0, sizeof(*info));
 
-    memcpy(info->signature, "XenHVMOVMF", sizeof(info->signature));
+    memcpy(info->signature, sig, sizeof(sig));
     info->length = sizeof(*info);
 }
 
diff --git a/tools/firmware/hvmloader/seabios.c b/tools/firmware/hvmloader/seabios.c
index 5c9a351..ca092cc 100644
--- a/tools/firmware/hvmloader/seabios.c
+++ b/tools/firmware/hvmloader/seabios.c
@@ -55,10 +55,11 @@  struct seabios_info {
 static void seabios_setup_bios_info(void)
 {
     struct seabios_info *info = (void *)BIOS_INFO_PHYSICAL_ADDRESS;
+    const char sig[] = "XenHVMSeaBIOS";
 
     memset(info, 0, sizeof(*info));
 
-    memcpy(info->signature, "XenHVMSeaBIOS", sizeof(info->signature));
+    memcpy(info->signature, sig, sizeof(sig));
     info->length = sizeof(*info);
 
     info->tables = (uint32_t)scratch_alloc(MAX_TABLES*sizeof(uint32_t), 0);