Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fallback: Fix for BootOrder crash when index returned by find_boot_option() is not in current BootOrder list #422

Merged
merged 2 commits into from
Oct 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 48 additions & 21 deletions fallback.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,15 +486,10 @@ find_boot_option(EFI_DEVICE_PATH *dp, EFI_DEVICE_PATH *fulldp,
first_new_option_size = StrLen(arguments) * sizeof (CHAR16);
}

/* find the index for the matching entry in BootOrder */
UINT16 bootnum = xtoi(varname + 4);
for (*optnum = 0; *optnum < nbootorder; (*optnum)++) {
if (bootorder[*optnum] == bootnum) {
FreePool(candidate);
FreePool(data);
return EFI_SUCCESS;
}
}
*optnum = xtoi(varname + 4);
FreePool(candidate);
FreePool(data);
return EFI_SUCCESS;
}
FreePool(candidate);
FreePool(data);
Expand Down Expand Up @@ -574,7 +569,7 @@ add_to_boot_list(CHAR16 *dirname, CHAR16 *filename, CHAR16 *label, CHAR16 *argum
full_device_path = FileDevicePath(this_image->DeviceHandle, fullpath);
if (!full_device_path) {
efi_status = EFI_OUT_OF_RESOURCES;
goto err;
goto done;
}
dps = DevicePathToStr(full_device_path);
VerbosePrint(L"file DP: %s\n", dps);
Expand All @@ -588,7 +583,7 @@ add_to_boot_list(CHAR16 *dirname, CHAR16 *filename, CHAR16 *label, CHAR16 *argum
dp = full_device_path;
} else {
efi_status = EFI_OUT_OF_RESOURCES;
goto err;
goto done;
}
}

Expand Down Expand Up @@ -617,21 +612,53 @@ add_to_boot_list(CHAR16 *dirname, CHAR16 *filename, CHAR16 *label, CHAR16 *argum
if (EFI_ERROR(efi_status)) {
add_boot_option(dp, full_device_path, fullpath, label,
arguments);
} else if (option != 0) {
CHAR16 *newbootorder;
newbootorder = AllocateZeroPool(sizeof (CHAR16) * nbootorder);
if (!newbootorder)
return EFI_OUT_OF_RESOURCES;
goto done;
}

newbootorder[0] = bootorder[option];
CopyMem(newbootorder + 1, bootorder, sizeof (CHAR16) * option);
CopyMem(newbootorder + option + 1, bootorder + option + 1,
sizeof (CHAR16) * (nbootorder - option - 1));
UINT16 bootnum;
CHAR16 *newbootorder;
/* Search for the option in the current bootorder */
for (bootnum = 0; bootnum < nbootorder; bootnum++)
if (bootorder[bootnum] == option)
break;
if (bootnum == nbootorder) {
/* Option not found, prepend option and copy the rest */
newbootorder = AllocateZeroPool(sizeof(CHAR16)
* (nbootorder + 1));
if (!newbootorder) {
efi_status = EFI_OUT_OF_RESOURCES;
goto done;
}
newbootorder[0] = option;
CopyMem(newbootorder + 1, bootorder,
sizeof(CHAR16) * nbootorder);
FreePool(bootorder);
bootorder = newbootorder;
nbootorder += 1;
} else {
rmetrich marked this conversation as resolved.
Show resolved Hide resolved
/* Option found, put first and slice the rest */
newbootorder = AllocateZeroPool(
sizeof(CHAR16) * nbootorder);
if (!newbootorder) {
efi_status = EFI_OUT_OF_RESOURCES;
goto done;
}
newbootorder[0] = option;
CopyMem(newbootorder + 1, bootorder,
sizeof(CHAR16) * bootnum);
CopyMem(newbootorder + 1 + bootnum,
bootorder + bootnum + 1,
sizeof(CHAR16) * (nbootorder - bootnum - 1));
FreePool(bootorder);
bootorder = newbootorder;
}
VerbosePrint(L"New nbootorder: %d\nBootOrder: ",
nbootorder);
for (int i = 0 ; i < nbootorder ; i++)
VerbosePrintUnprefixed(L"%04x ", bootorder[i]);
VerbosePrintUnprefixed(L"\n");

err:
done:
if (full_device_path)
FreePool(full_device_path);
if (dp && dp != full_device_path)
Expand Down