Skip to content

Commit a66f2e5

Browse files
Eugeniy Paltsevvineetgarc
authored andcommitted
ARC: U-boot: check arguments paranoidly
Handle U-boot arguments paranoidly: * don't allow to pass unknown tag. * try to use external device tree blob only if corresponding tag (TAG_DTB) is set. * don't check uboot_tag if kernel build with no ARC_UBOOT_SUPPORT. NOTE: If U-boot args are invalid we skip them and try to use embedded device tree blob. We can't panic on invalid U-boot args as we really pass invalid args due to bug in U-boot code. This happens if we don't provide external DTB to U-boot and don't set 'bootargs' U-boot environment variable (which is default case at least for HSDK board) In that case we will pass {r0 = 1 (bootargs in r2); r1 = 0; r2 = 0;} to linux which is invalid. While I'm at it refactor U-boot arguments handling code. Cc: stable@vger.kernel.org Tested-by: Corentin LABBE <clabbe@baylibre.com> Signed-off-by: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com> Signed-off-by: Vineet Gupta <vgupta@synopsys.com>
1 parent e494239 commit a66f2e5

2 files changed

Lines changed: 64 additions & 27 deletions

File tree

‎arch/arc/kernel/head.S‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ ENTRY(stext)
103103
#ifdef CONFIG_ARC_UBOOT_SUPPORT
104104
; Uboot - kernel ABI
105105
; r0 = [0] No uboot interaction, [1] cmdline in r2, [2] DTB in r2
106-
; r1 = magic number (board identity, unused as of now
106+
; r1 = magic number (always zero as of now)
107107
; r2 = pointer to uboot provided cmdline or external DTB in mem
108-
; These are handled later in setup_arch()
108+
; These are handled later in handle_uboot_args()
109109
st r0, [@uboot_tag]
110110
st r2, [@uboot_arg]
111111
#endif

‎arch/arc/kernel/setup.c‎

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -462,43 +462,80 @@ void setup_processor(void)
462462
arc_chk_core_config();
463463
}
464464

465-
static inline int is_kernel(unsigned long addr)
465+
static inline bool uboot_arg_invalid(unsigned long addr)
466466
{
467-
if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
468-
return 1;
469-
return 0;
467+
/*
468+
* Check that it is a untranslated address (although MMU is not enabled
469+
* yet, it being a high address ensures this is not by fluke)
470+
*/
471+
if (addr < PAGE_OFFSET)
472+
return true;
473+
474+
/* Check that address doesn't clobber resident kernel image */
475+
return addr >= (unsigned long)_stext && addr <= (unsigned long)_end;
470476
}
471477

472-
void __init setup_arch(char **cmdline_p)
478+
#define IGNORE_ARGS "Ignore U-boot args: "
479+
480+
/* uboot_tag values for U-boot - kernel ABI revision 0; see head.S */
481+
#define UBOOT_TAG_NONE 0
482+
#define UBOOT_TAG_CMDLINE 1
483+
#define UBOOT_TAG_DTB 2
484+
485+
void __init handle_uboot_args(void)
473486
{
487+
bool use_embedded_dtb = true;
488+
bool append_cmdline = false;
489+
474490
#ifdef CONFIG_ARC_UBOOT_SUPPORT
475-
/* make sure that uboot passed pointer to cmdline/dtb is valid */
476-
if (uboot_tag && is_kernel((unsigned long)uboot_arg))
477-
panic("Invalid uboot arg\n");
491+
/* check that we know this tag */
492+
if (uboot_tag != UBOOT_TAG_NONE &&
493+
uboot_tag != UBOOT_TAG_CMDLINE &&
494+
uboot_tag != UBOOT_TAG_DTB) {
495+
pr_warn(IGNORE_ARGS "invalid uboot tag: '%08x'\n", uboot_tag);
496+
goto ignore_uboot_args;
497+
}
498+
499+
if (uboot_tag != UBOOT_TAG_NONE &&
500+
uboot_arg_invalid((unsigned long)uboot_arg)) {
501+
pr_warn(IGNORE_ARGS "invalid uboot arg: '%px'\n", uboot_arg);
502+
goto ignore_uboot_args;
503+
}
504+
505+
/* see if U-boot passed an external Device Tree blob */
506+
if (uboot_tag == UBOOT_TAG_DTB) {
507+
machine_desc = setup_machine_fdt((void *)uboot_arg);
478508

479-
/* See if u-boot passed an external Device Tree blob */
480-
machine_desc = setup_machine_fdt(uboot_arg); /* uboot_tag == 2 */
481-
if (!machine_desc)
509+
/* external Device Tree blob is invalid - use embedded one */
510+
use_embedded_dtb = !machine_desc;
511+
}
512+
513+
if (uboot_tag == UBOOT_TAG_CMDLINE)
514+
append_cmdline = true;
515+
516+
ignore_uboot_args:
482517
#endif
483-
{
484-
/* No, so try the embedded one */
518+
519+
if (use_embedded_dtb) {
485520
machine_desc = setup_machine_fdt(__dtb_start);
486521
if (!machine_desc)
487522
panic("Embedded DT invalid\n");
523+
}
488524

489-
/*
490-
* If we are here, it is established that @uboot_arg didn't
491-
* point to DT blob. Instead if u-boot says it is cmdline,
492-
* append to embedded DT cmdline.
493-
* setup_machine_fdt() would have populated @boot_command_line
494-
*/
495-
if (uboot_tag == 1) {
496-
/* Ensure a whitespace between the 2 cmdlines */
497-
strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
498-
strlcat(boot_command_line, uboot_arg,
499-
COMMAND_LINE_SIZE);
500-
}
525+
/*
526+
* NOTE: @boot_command_line is populated by setup_machine_fdt() so this
527+
* append processing can only happen after.
528+
*/
529+
if (append_cmdline) {
530+
/* Ensure a whitespace between the 2 cmdlines */
531+
strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
532+
strlcat(boot_command_line, uboot_arg, COMMAND_LINE_SIZE);
501533
}
534+
}
535+
536+
void __init setup_arch(char **cmdline_p)
537+
{
538+
handle_uboot_args();
502539

503540
/* Save unparsed command line copy for /proc/cmdline */
504541
*cmdline_p = boot_command_line;

0 commit comments

Comments
 (0)