Skip to content

Commit 7779566

Browse files
Eugeniy Paltsevgregkh
authored andcommitted
ARC: U-boot: check arguments paranoidly
commit a66f2e5 upstream. 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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5f7814c commit 7779566

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
@@ -449,43 +449,80 @@ void setup_processor(void)
449449
arc_chk_core_config();
450450
}
451451

452-
static inline int is_kernel(unsigned long addr)
452+
static inline bool uboot_arg_invalid(unsigned long addr)
453453
{
454-
if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
455-
return 1;
456-
return 0;
454+
/*
455+
* Check that it is a untranslated address (although MMU is not enabled
456+
* yet, it being a high address ensures this is not by fluke)
457+
*/
458+
if (addr < PAGE_OFFSET)
459+
return true;
460+
461+
/* Check that address doesn't clobber resident kernel image */
462+
return addr >= (unsigned long)_stext && addr <= (unsigned long)_end;
457463
}
458464

459-
void __init setup_arch(char **cmdline_p)
465+
#define IGNORE_ARGS "Ignore U-boot args: "
466+
467+
/* uboot_tag values for U-boot - kernel ABI revision 0; see head.S */
468+
#define UBOOT_TAG_NONE 0
469+
#define UBOOT_TAG_CMDLINE 1
470+
#define UBOOT_TAG_DTB 2
471+
472+
void __init handle_uboot_args(void)
460473
{
474+
bool use_embedded_dtb = true;
475+
bool append_cmdline = false;
476+
461477
#ifdef CONFIG_ARC_UBOOT_SUPPORT
462-
/* make sure that uboot passed pointer to cmdline/dtb is valid */
463-
if (uboot_tag && is_kernel((unsigned long)uboot_arg))
464-
panic("Invalid uboot arg\n");
478+
/* check that we know this tag */
479+
if (uboot_tag != UBOOT_TAG_NONE &&
480+
uboot_tag != UBOOT_TAG_CMDLINE &&
481+
uboot_tag != UBOOT_TAG_DTB) {
482+
pr_warn(IGNORE_ARGS "invalid uboot tag: '%08x'\n", uboot_tag);
483+
goto ignore_uboot_args;
484+
}
485+
486+
if (uboot_tag != UBOOT_TAG_NONE &&
487+
uboot_arg_invalid((unsigned long)uboot_arg)) {
488+
pr_warn(IGNORE_ARGS "invalid uboot arg: '%px'\n", uboot_arg);
489+
goto ignore_uboot_args;
490+
}
491+
492+
/* see if U-boot passed an external Device Tree blob */
493+
if (uboot_tag == UBOOT_TAG_DTB) {
494+
machine_desc = setup_machine_fdt((void *)uboot_arg);
465495

466-
/* See if u-boot passed an external Device Tree blob */
467-
machine_desc = setup_machine_fdt(uboot_arg); /* uboot_tag == 2 */
468-
if (!machine_desc)
496+
/* external Device Tree blob is invalid - use embedded one */
497+
use_embedded_dtb = !machine_desc;
498+
}
499+
500+
if (uboot_tag == UBOOT_TAG_CMDLINE)
501+
append_cmdline = true;
502+
503+
ignore_uboot_args:
469504
#endif
470-
{
471-
/* No, so try the embedded one */
505+
506+
if (use_embedded_dtb) {
472507
machine_desc = setup_machine_fdt(__dtb_start);
473508
if (!machine_desc)
474509
panic("Embedded DT invalid\n");
510+
}
475511

476-
/*
477-
* If we are here, it is established that @uboot_arg didn't
478-
* point to DT blob. Instead if u-boot says it is cmdline,
479-
* append to embedded DT cmdline.
480-
* setup_machine_fdt() would have populated @boot_command_line
481-
*/
482-
if (uboot_tag == 1) {
483-
/* Ensure a whitespace between the 2 cmdlines */
484-
strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
485-
strlcat(boot_command_line, uboot_arg,
486-
COMMAND_LINE_SIZE);
487-
}
512+
/*
513+
* NOTE: @boot_command_line is populated by setup_machine_fdt() so this
514+
* append processing can only happen after.
515+
*/
516+
if (append_cmdline) {
517+
/* Ensure a whitespace between the 2 cmdlines */
518+
strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
519+
strlcat(boot_command_line, uboot_arg, COMMAND_LINE_SIZE);
488520
}
521+
}
522+
523+
void __init setup_arch(char **cmdline_p)
524+
{
525+
handle_uboot_args();
489526

490527
/* Save unparsed command line copy for /proc/cmdline */
491528
*cmdline_p = boot_command_line;

0 commit comments

Comments
 (0)