efi-stub: update string operation in efi-stub

Remove unsafe API in efi-stub
1, use Strnlen instead of StrLen except the parameter
   is a static string.
2, strlen() only work on static strings.

Tracked-On: #3276
Signed-off-by: Tianhua Sun <tianhuax.s.sun@intel.com>
Reviewed-by: Yonghua Huang <yonghua.huang@intel.com>
This commit is contained in:
Tianhua Sun 2019-07-17 14:33:47 +08:00 committed by ACRN System Integration
parent 05acc8b705
commit 63e258bd01
4 changed files with 12 additions and 29 deletions

View File

@ -347,7 +347,7 @@ static inline EFI_STATUS isspace(CHAR8 ch)
return ((uint8_t)ch <= ' '); return ((uint8_t)ch <= ' ');
} }
#define DEFAULT_UEFI_OS_LOADER_NAME "\\EFI\\org.clearlinux\\bootloaderx64.efi" #define DEFAULT_UEFI_OS_LOADER_NAME L"\\EFI\\org.clearlinux\\bootloaderx64.efi"
/** /**
* efi_main - The entry point for the OS loader image. * efi_main - The entry point for the OS loader image.
* @image: firmware-allocated handle that identifies the image * @image: firmware-allocated handle that identifies the image
@ -392,13 +392,13 @@ efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *_table)
/* convert the options to cmdline */ /* convert the options to cmdline */
if (options_size > 0) if (options_size > 0)
cmdline = ch16_2_ch8(options); cmdline = ch16_2_ch8(options, StrnLen(options, options_size));
/* First check if we were given a bootloader name /* First check if we were given a bootloader name
* E.g.: "bootloader=\EFI\org.clearlinux\bootloaderx64.efi" * E.g.: "bootloader=\EFI\org.clearlinux\bootloaderx64.efi"
*/ */
cmdline16 = StrDuplicate(options); cmdline16 = StrDuplicate(options);
bootloader_name = strstr_16(cmdline16, bootloader_param); bootloader_name = strstr_16(cmdline16, bootloader_param, StrLen(bootloader_param));
if (bootloader_name) { if (bootloader_name) {
bootloader_name = bootloader_name + StrLen(bootloader_param); bootloader_name = bootloader_name + StrLen(bootloader_param);
n = bootloader_name; n = bootloader_name;
@ -413,11 +413,11 @@ efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *_table)
* bootloader name to be used. Fall back to the default bootloader * bootloader name to be used. Fall back to the default bootloader
* as specified in config.h * as specified in config.h
*/ */
bootloader_name = ch8_2_ch16(DEFAULT_UEFI_OS_LOADER_NAME); bootloader_name = DEFAULT_UEFI_OS_LOADER_NAME;
} }
section = ".hv"; section = ".hv";
err = get_pe_section(info->ImageBase, section, &sec_addr, &sec_size); err = get_pe_section(info->ImageBase, section, strlen(section), &sec_addr, &sec_size);
if (EFI_ERROR(err)) { if (EFI_ERROR(err)) {
Print(L"Unable to locate section of ACRNHV %r ", err); Print(L"Unable to locate section of ACRNHV %r ", err);
goto failed; goto failed;

View File

@ -68,7 +68,7 @@
*msr_val_ptr = ((uint64_t)msrh << 32U) | msrl; \ *msr_val_ptr = ((uint64_t)msrh << 32U) | msrl; \
} }
EFI_STATUS get_pe_section(CHAR8 *base, char *section, UINTN *vaddr, UINTN *size); EFI_STATUS get_pe_section(CHAR8 *base, char *section_name, UINTN section_name_len, UINTN *vaddr, UINTN *size);
typedef void(*hv_func)(int32_t, struct multiboot_info*); typedef void(*hv_func)(int32_t, struct multiboot_info*);
/* /*

View File

@ -107,7 +107,8 @@ struct PeSectionHeader {
} __attribute__((packed)); } __attribute__((packed));
EFI_STATUS get_pe_section(CHAR8 *base, char *section, UINTN *vaddr, UINTN *size) EFI_STATUS get_pe_section(CHAR8 *base, char *section_name,
UINTN section_name_len, UINTN *vaddr, UINTN *size)
{ {
struct PeSectionHeader *ph; struct PeSectionHeader *ph;
struct DosFileHeader *dh; struct DosFileHeader *dh;
@ -132,7 +133,7 @@ EFI_STATUS get_pe_section(CHAR8 *base, char *section, UINTN *vaddr, UINTN *size)
for (i = 0; i < pe->mNumberOfSections; i++) { for (i = 0; i < pe->mNumberOfSections; i++) {
ph = (struct PeSectionHeader *)&base[offset]; ph = (struct PeSectionHeader *)&base[offset];
if (CompareMem(ph->mName, section, strlen(section)) == 0) { if (CompareMem(ph->mName, section_name, section_name_len) == 0) {
*vaddr = (UINTN)ph->mVirtualAddress; *vaddr = (UINTN)ph->mVirtualAddress;
*size = (UINTN)ph->mVirtualSize; *size = (UINTN)ph->mVirtualSize;
break; break;

View File

@ -73,11 +73,10 @@ static inline int32_t strlen(const char *str)
return len; return len;
} }
static inline CHAR16 *strstr_16(CHAR16 *haystack, CHAR16 *needle) static inline CHAR16 *strstr_16(CHAR16 *haystack, CHAR16 *needle, UINTN len)
{ {
CHAR16 *p; CHAR16 *p;
CHAR16 *word = NULL; CHAR16 *word = NULL;
UINTN len = StrLen(needle);
if (!len) if (!len)
return NULL; return NULL;
@ -94,28 +93,11 @@ static inline CHAR16 *strstr_16(CHAR16 *haystack, CHAR16 *needle)
return (CHAR16*)word; return (CHAR16*)word;
} }
static inline CHAR16 *ch8_2_ch16(char *str8) static inline char *ch16_2_ch8(CHAR16 *str16, UINTN len)
{ {
UINTN len, i; UINTN i;
CHAR16 *str16;
len = strlen(str8);
str16 = AllocatePool((len + 1) * sizeof(CHAR16));
for (i = 0; i < len; i++)
str16[i] = str8[i];
str16[len] = 0;
return str16;
}
static inline char *ch16_2_ch8(CHAR16 *str16)
{
UINTN len, i;
char *str8; char *str8;
len = StrLen(str16);
str8 = AllocatePool((len + 1) * sizeof(char)); str8 = AllocatePool((len + 1) * sizeof(char));
for (i = 0; i < len; i++) for (i = 0; i < len; i++)