Skip to contentSkip to conversation

xen: Add EFI_LOAD_OPTION support

PATCH Superseded Tamas K Lengyel 2 participants 5 messages Jan 4, 2018 – Jan 19, 2018 1 message not in the archive

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen: Add EFI_LOAD_OPTION support

When booting Xen via UEFI the Xen config file can contain multiple sections each describing different boot options. It is currently only possible to choose which section to boot with if the buffer contains a string. UEFI provides a different standard to pass optional arguments to an application, and in this patch we make Xen properly parse this buffer, thus making it possible to have separate EFI boot options present for the different config sections.

---
Cc: Jan Beulich <[email protected]>
Cc: [email protected]

v2: move EFI_LOAD_OPTION definition into file that uses it
add more sanity checks to validate the buffer

---
 xen/common/efi/boot.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 56 insertions(+), 6 deletions(-)
xen/common/efi/boot.c +56 -6
index 469bf980cc..8f4fafa3da 100644
@@ -88,6 +88,16 @@ typedef struct _EFI_APPLE_PROPERTIES {
     EFI_APPLE_PROPERTIES_GETALL GetAll;
 } EFI_APPLE_PROPERTIES;
 
+typedef struct _EFI_LOAD_OPTION {
+    UINT32 Attributes;
+    UINT16 FilePathListLength;
+    CHAR16 Description[];
+} EFI_LOAD_OPTION;
+
+#define LOAD_OPTION_ACTIVE              0x00000001
+#define LOAD_OPTION_FORCE_RECONNECT     0x00000002
+#define LOAD_OPTION_HIDDEN              0x00000008
+
 union string {
     CHAR16 *w;
     char *s;
@@ -375,12 +385,52 @@ static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode)
 
 static unsigned int __init get_argv(unsigned int argc, CHAR16 **argv,
                                     CHAR16 *cmdline, UINTN cmdsize,
-                                    CHAR16 **options)
+                                    CHAR16 **options, bool *elo_active)
 {
     CHAR16 *ptr = (CHAR16 *)(argv + argc + 1), *prev = NULL;
     bool prev_sep = true;
 
-    for ( ; cmdsize > sizeof(*cmdline) && *cmdline;
+    if ( cmdsize > sizeof(EFI_LOAD_OPTION) )
+    {
+        /* See include/efi/efiapi.h for more info about the following */
+        const EFI_LOAD_OPTION *elo = (const EFI_LOAD_OPTION *)cmdline;
+
+        /* The absolute minimum the size of the buffer it needs to be */
+        size_t size_check = sizeof(elo->Attributes) +
+                            sizeof(elo->FilePathListLength) +
+                            elo->FilePathListLength +
+                            sizeof(CHAR16);
+
+        if ( (elo->Attributes & LOAD_OPTION_ACTIVE) && size_check < cmdsize )
+        {
+            const CHAR16 *desc = elo->Description;
+            const UINT8 *opts = (const UINT8 *)desc;
+            size_t i = 0;
+
+            /* Find Description string length in its possible space */
+            while ( i < cmdsize - size_check && *desc++ != L'\0')
+                i += sizeof(CHAR16);
+
+            /* The Description has to end with a NULL char */
+            if ( *desc == L'\0' )
+            {
+                UINTN new_cmdsize = cmdsize;
+
+                opts += i + sizeof(CHAR16) + elo->FilePathListLength;
+                new_cmdsize -= opts - (UINT8 *)elo;
+
+                /* Sanity check the new cmdsize to avoid an underflow */
+                if ( new_cmdsize < cmdsize )
+                {
+                    *elo_active = true;
+                    cmdline = (CHAR16 *)opts;
+                    cmdsize = new_cmdsize;
+                }
+            }
+        }
+    }
+
+    for ( ; cmdsize >= sizeof(*cmdline) && *cmdline;
             cmdsize -= sizeof(*cmdline), ++cmdline )
     {
         bool cur_sep = *cmdline == L' ' || *cmdline == L'\t';
@@ -1071,7 +1121,7 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
     EFI_SHIM_LOCK_PROTOCOL *shim_lock;
     EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = NULL;
     union string section = { NULL }, name;
-    bool base_video = false;
+    bool base_video = false, elo_active = false;
     char *option_str;
     bool use_cfg_file;
 
@@ -1096,17 +1146,17 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
     if ( use_cfg_file )
     {
         argc = get_argv(0, NULL, loaded_image->LoadOptions,
-                        loaded_image->LoadOptionsSize, NULL);
+                        loaded_image->LoadOptionsSize, NULL, &elo_active);
         if ( argc > 0 &&
              efi_bs->AllocatePool(EfiLoaderData,
                                   (argc + 1) * sizeof(*argv) +
                                       loaded_image->LoadOptionsSize,
                                   (void **)&argv) == EFI_SUCCESS )
             get_argv(argc, argv, loaded_image->LoadOptions,
-                     loaded_image->LoadOptionsSize, &options);
+                     loaded_image->LoadOptionsSize, &options, &elo_active);
         else
             argc = 0;
-        for ( i = 1; i < argc; ++i )
+        for ( i = !elo_active; i < argc; ++i )
         {
             CHAR16 *ptr = argv[i];
 
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 21:33, <[email protected]> wrote:
xen/common/efi/boot.c +0 -1
@@ -375,12 +385,52 @@ static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode)
 
 static unsigned int __init get_argv(unsigned int argc, CHAR16 **argv,
                                     CHAR16 *cmdline, UINTN cmdsize,
-                                    CHAR16 **options)

xen/common/efi/boot.c static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode) · line 396

Quoted 10 lines
xen/common/efi/boot.c +5 -1
+                                    CHAR16 **options, bool *elo_active)
 {
     CHAR16 *ptr = (CHAR16 *)(argv + argc + 1), *prev = NULL;
     bool prev_sep = true;
 
-    for ( ; cmdsize > sizeof(*cmdline) && *cmdline;
+    if ( cmdsize > sizeof(EFI_LOAD_OPTION) )
+    {
+        /* See include/efi/efiapi.h for more info about the following */
+        const EFI_LOAD_OPTION *elo = (const EFI_LOAD_OPTION *)cmdline;

The comment looks to be stale now.

xen/common/efi/boot.c static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode) · line 402

Quoted 5 lines
xen/common/efi/boot.c +5 -0
+        /* The absolute minimum the size of the buffer it needs to be */
+        size_t size_check = sizeof(elo->Attributes) +
+                            sizeof(elo->FilePathListLength) +
+                            elo->FilePathListLength +
+                            sizeof(CHAR16);

offsetof(EFI_LOAD_OPTION, Description) + elo->FilePathListLength
+ sizeof(<whatever-the-CHAR16-refers-to>)

I.e. perhaps

offsetof(EFI_LOAD_OPTION, Description[1]) + elo->FilePathListLength

xen/common/efi/boot.c static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode) · line 407

Quoted 4 lines
xen/common/efi/boot.c +4 -0
+        if ( (elo->Attributes & LOAD_OPTION_ACTIVE) && size_check < cmdsize )
+        {
+            const CHAR16 *desc = elo->Description;
+            const UINT8 *opts = (const UINT8 *)desc;

With gcc's void pointer arithmetic allowing it, some of the code would end up easier to read if this was const void *. Also this variable should move into the more narrow scope it's used in.

xen/common/efi/boot.c static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode) · line 412

Quoted 5 lines
xen/common/efi/boot.c +5 -0
+            size_t i = 0;
+
+            /* Find Description string length in its possible space */
+            while ( i < cmdsize - size_check && *desc++ != L'\0')
+                i += sizeof(CHAR16);

sizeof(*desc) (also elsewhere - please always prefer using the type of the actual object over an explicit type).

xen/common/efi/boot.c static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode) · line 415

Quoted 2 lines
xen/common/efi/boot.c +2 -0
+            /* The Description has to end with a NULL char */
+            if ( *desc == L'\0' )

How does this work? The loop above has incremented desc already past the nul terminator afaict.

xen/common/efi/boot.c static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode) · line 419

Quoted 4 lines
xen/common/efi/boot.c +4 -0
+            {
+                UINTN new_cmdsize = cmdsize;
+
+                opts += i + sizeof(CHAR16) + elo->FilePathListLength;

If you checked that this is within bounds, ...

xen/common/efi/boot.c static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode) · line 423

Quoted 4 lines
xen/common/efi/boot.c +4 -0
+                new_cmdsize -= opts - (UINT8 *)elo;
+
+                /* Sanity check the new cmdsize to avoid an underflow */
+                if ( new_cmdsize < cmdsize )

... you wouldn't need this check, and you wouldn't need the extra new_cmdsize variable at all, likely making more obvious what you're doing here.

And then - how about a different heuristic altogether: Current code scans the pointed to memory for a nul character, being happy if it is found before having exhausted cmdsize. Why not simply scan for the first nul and conclude it's an EFI_LOAD_OPTION structure if that nul isn't right at the end of the buffer? One could even consider scanning for more than just nul, e.g. any non-blank character with a numeric value below 0x0020.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 47 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 04.01.18 at 21:33, <[email protected]> wrote:
xen/common/efi/boot.c +0 -1
@@ -375,12 +385,52 @@ static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode)
 
 static unsigned int __init get_argv(unsigned int argc, CHAR16 **argv,
                                     CHAR16 *cmdline, UINTN cmdsize,
-                                    CHAR16 **options)

xen/common/efi/boot.c static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode) · line 396

Quoted 10 lines
xen/common/efi/boot.c +5 -1
+                                    CHAR16 **options, bool *elo_active)
 {
     CHAR16 *ptr = (CHAR16 *)(argv + argc + 1), *prev = NULL;
     bool prev_sep = true;
 
-    for ( ; cmdsize > sizeof(*cmdline) && *cmdline;
+    if ( cmdsize > sizeof(EFI_LOAD_OPTION) )
+    {
+        /* See include/efi/efiapi.h for more info about the following */
+        const EFI_LOAD_OPTION *elo = (const EFI_LOAD_OPTION *)cmdline;

The comment looks to be stale now.

xen/common/efi/boot.c static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode) · line 402

Quoted 5 lines
xen/common/efi/boot.c +5 -0
+        /* The absolute minimum the size of the buffer it needs to be */
+        size_t size_check = sizeof(elo->Attributes) +
+                            sizeof(elo->FilePathListLength) +
+                            elo->FilePathListLength +
+                            sizeof(CHAR16);

offsetof(EFI_LOAD_OPTION, Description) + elo->FilePathListLength
+ sizeof(<whatever-the-CHAR16-refers-to>)

I.e. perhaps

offsetof(EFI_LOAD_OPTION, Description[1]) + elo->FilePathListLength

xen/common/efi/boot.c static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode) · line 407

Quoted 4 lines
xen/common/efi/boot.c +4 -0
+        if ( (elo->Attributes & LOAD_OPTION_ACTIVE) && size_check < cmdsize )
+        {
+            const CHAR16 *desc = elo->Description;
+            const UINT8 *opts = (const UINT8 *)desc;

With gcc's void pointer arithmetic allowing it, some of the code would end up easier to read if this was const void *. Also this variable should move into the more narrow scope it's used in.

xen/common/efi/boot.c static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode) · line 412

Quoted 5 lines
xen/common/efi/boot.c +5 -0
+            size_t i = 0;
+
+            /* Find Description string length in its possible space */
+            while ( i < cmdsize - size_check && *desc++ != L'\0')
+                i += sizeof(CHAR16);
Quoted 8 lines

sizeof(*desc) (also elsewhere - please always prefer using the type of the actual object over an explicit type).

xen/common/efi/boot.c static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode) · line 415

Quoted 2 lines
xen/common/efi/boot.c +2 -0
+            /* The Description has to end with a NULL char */
+            if ( *desc == L'\0' )

How does this work? The loop above has incremented desc already past the nul terminator afaict.

Odd, it did work when I tested it. But yes, I'll just check that i is still within the search space.

Quoted 13 lines

xen/common/efi/boot.c static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode) · line 419

Quoted 5 lines
xen/common/efi/boot.c +4 -0
+            {
+                UINTN new_cmdsize = cmdsize;
+
+                opts += i + sizeof(CHAR16) + elo->FilePathListLength;

If you checked that this is within bounds, ...

xen/common/efi/boot.c static void __init PrintErrMesg(const CHAR16 *mesg, EFI_STATUS ErrCode) · line 423

Quoted 4 lines
xen/common/efi/boot.c +4 -0
+                new_cmdsize -= opts - (UINT8 *)elo;
+
+                /* Sanity check the new cmdsize to avoid an underflow */
+                if ( new_cmdsize < cmdsize )
Quoted 7 lines

... you wouldn't need this check, and you wouldn't need the extra new_cmdsize variable at all, likely making more obvious what you're doing here.

And then - how about a different heuristic altogether: Current code scans the pointed to memory for a nul character, being happy if it is found before having exhausted cmdsize.

I'm not sure what you are talking about. The nul char is not at the end of the EFI_LOAD_OPTION, it is only at the end of the Description array. And we have to scan for it because the length of the Description array is not recorded anywhere else. So we scan for the nul char in the part of the buffer where it would be warranted by the EFI spec, then compute the location of the actual option buffer based on that checking that it is within bounds of the buffer.

Quoted 3 lines

Why not
simply scan for the first nul and conclude it's an EFI_LOAD_OPTION structure if that nul isn't right at the end of the buffer?

I don't follow, how would that be more robust then checking that NULL is in the right space within the EFI_LOAD_OPTION according to spec?

Quoted 3 lines

One could
even consider scanning for more than just nul, e.g. any non-blank character with a numeric value below 0x0020.

Perhaps, but I think that's an overkill. Again, what are the chances that a non-EFI loader will pass a string buffer that happens to also properly parse as an EFI_LOAD_OPTION?

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 19.01.18 at 17:25, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich

And then - how about a different heuristic altogether: Current code scans the pointed to memory for a nul character, being happy if it is found before having exhausted cmdsize.

Quoted 7 lines

I'm not sure what you are talking about. The nul char is not at the end of the EFI_LOAD_OPTION, it is only at the end of the Description array. And we have to scan for it because the length of the Description array is not recorded anywhere else. So we scan for the nul char in the part of the buffer where it would be warranted by the EFI spec, then compute the location of the actual option buffer based on that checking that it is within bounds of the buffer.

By "current code" I mean what's upstream at present, not yours. I.e. I'm suggesting to consider proving it's a nul-terminated string instead of proving its an EFI_LOAD_OPTION structure.

Jan

Quoted 11 lines
Quoted 3 lines

Why not
simply scan for the first nul and conclude it's an EFI_LOAD_OPTION structure if that nul isn't right at the end of the buffer?

I don't follow, how would that be more robust then checking that NULL is in the right space within the EFI_LOAD_OPTION according to spec?

Quoted 3 lines

One could
even consider scanning for more than just nul, e.g. any non-blank character with a numeric value below 0x0020.

Quoted 5 lines

Perhaps, but I think that's an overkill. Again, what are the chances that a non-EFI loader will pass a string buffer that happens to also properly parse as an EFI_LOAD_OPTION?

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 14 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 19.01.18 at 17:25, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich

And then - how about a different heuristic altogether: Current code scans the pointed to memory for a nul character, being happy if it is found before having exhausted cmdsize.

Quoted 7 lines

I'm not sure what you are talking about. The nul char is not at the end of the EFI_LOAD_OPTION, it is only at the end of the Description array. And we have to scan for it because the length of the Description array is not recorded anywhere else. So we scan for the nul char in the part of the buffer where it would be warranted by the EFI spec, then compute the location of the actual option buffer based on that checking that it is within bounds of the buffer.

Quoted 4 lines

By "current code" I mean what's upstream at present, not yours. I.e. I'm suggesting to consider proving it's a nul-terminated string instead of proving its an EFI_LOAD_OPTION structure.

Ah, yes, that makes more sense. For the EFI_LOAD_OPTION case we would still have to do all the parsing and sanity checking we currently do though. But adding a check to see whether the buffer ends in a nul and if it does skipping the EFI_LOAD_OPTION parsing would be fine.

Tamas

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel