Received: by 2002:ac0:b08d:0:0:0:0:0 with SMTP id l13csp4917875imc; Mon, 25 Feb 2019 13:36:18 -0800 (PST) X-Google-Smtp-Source: AHgI3IZHg1JjwGGfh9vVTd7Ab1ncLkvPrYKa6aIBF9QF7Y9FH2thMrimaHpZdw4iZL5JTk2Jj+D2 X-Received: by 2002:a63:104e:: with SMTP id 14mr16980886pgq.185.1551130578064; Mon, 25 Feb 2019 13:36:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551130578; cv=none; d=google.com; s=arc-20160816; b=m4SQ+AZE2cdEfp4aJmvET9dOfRD9wRmKpi335qA0ssx79HXn6XL85Oq2KGbXKRGHAU i2gqqKkW2XahjFqShUTaFENsoSUtvnMP332RFmE79faPhX5iSWNgx9nk1/zn6M4V2/vk VbBNQtOSIqODRA41ZvWvYholh38Jg48xAFy+8bJd7TKfQ6lEM1/ehuomXut0SbS7LRB/ 9RYo1RapLDC5JYS/U1BaOGK6e7GtxyPMTwEiYBRZf4Stau3QE+EcZwGLZjo9anm+zt9G AiGYvQPfnTdaYxtoXzFO1SJzjl1tU5YHvvycHgNpf+UVzpENw72SyTCFuYtpyJ1z+k4q Rv7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=k9yEWK5gYCv+QTI6NxQpJSgwZFyb88Y263ejeJEDMo8=; b=pHAiFq/5nStXK106LYfgQ1JbnmqNJyhpb+womBTMUMZIrG5JXYo07iao6H/1qnC7m/ rXg53gz39ciZeDvIY/lCpwTekv9Repr21H37EjFo+scgtNFG5SpvlB9nxaecz4+YCSzP NlFWii1tzFacxKOr4pDRnoHY+FkRZ5bbC0w0SAjSGWcRXMRFmvcO4+H127bKFw7avjIQ +jwZUft/JUou6O3wrVRYn58HGjaFE1tAm/xNSv6kasPrXo0+8Pj0kIAeTDKVaA3uIJeM DLTr5tqaV4FhhQ3bzQDH0jmGt79yMlJyvnPujdtohgegzJ8Ce9HqvKRoOq3Q9Pet3bpp j3Cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=VCOWChcx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u15si11669324pfc.265.2019.02.25.13.36.02; Mon, 25 Feb 2019 13:36:18 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=VCOWChcx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733022AbfBYVfi (ORCPT + 99 others); Mon, 25 Feb 2019 16:35:38 -0500 Received: from mail.kernel.org ([198.145.29.99]:41866 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733012AbfBYVfh (ORCPT ); Mon, 25 Feb 2019 16:35:37 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1C413217F5; Mon, 25 Feb 2019 21:35:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1551130536; bh=TxDEE7rfXEyWd873JIQJHgbsbyBbjkMvJIpODq7TnQw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VCOWChcxoNLXi83DAuAb2P2KseE7v3kVIeo3ZvJMAPkXK+CM5BBVlNA01OxJvFtKz qm7JuSGbmLGQA/HGoes8n6L7j357Kkga5dmfjHprKY+zZpqU5/wwZYaFam7PmKK43p DKRjAdRw3AdJMXQWljX6SXn0gaxlO2BYr5wbRrEo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Corentin LABBE , Eugeniy Paltsev , Vineet Gupta Subject: [PATCH 4.20 153/183] ARC: U-boot: check arguments paranoidly Date: Mon, 25 Feb 2019 22:12:06 +0100 Message-Id: <20190225195120.768424143@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190225195054.748060397@linuxfoundation.org> References: <20190225195054.748060397@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ From: Eugeniy Paltsev commit a66f2e57bd566240d8b3884eedf503928fbbe557 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 Signed-off-by: Eugeniy Paltsev Signed-off-by: Vineet Gupta Signed-off-by: Greg Kroah-Hartman --- arch/arc/kernel/head.S | 4 +- arch/arc/kernel/setup.c | 89 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 65 insertions(+), 28 deletions(-) --- a/arch/arc/kernel/head.S +++ b/arch/arc/kernel/head.S @@ -103,9 +103,9 @@ ENTRY(stext) #ifdef CONFIG_ARC_UBOOT_SUPPORT ; Uboot - kernel ABI ; r0 = [0] No uboot interaction, [1] cmdline in r2, [2] DTB in r2 - ; r1 = magic number (board identity, unused as of now + ; r1 = magic number (always zero as of now) ; r2 = pointer to uboot provided cmdline or external DTB in mem - ; These are handled later in setup_arch() + ; These are handled later in handle_uboot_args() st r0, [@uboot_tag] st r2, [@uboot_arg] #endif --- a/arch/arc/kernel/setup.c +++ b/arch/arc/kernel/setup.c @@ -452,43 +452,80 @@ void setup_processor(void) arc_chk_core_config(); } -static inline int is_kernel(unsigned long addr) +static inline bool uboot_arg_invalid(unsigned long addr) { - if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end) - return 1; - return 0; + /* + * Check that it is a untranslated address (although MMU is not enabled + * yet, it being a high address ensures this is not by fluke) + */ + if (addr < PAGE_OFFSET) + return true; + + /* Check that address doesn't clobber resident kernel image */ + return addr >= (unsigned long)_stext && addr <= (unsigned long)_end; } -void __init setup_arch(char **cmdline_p) +#define IGNORE_ARGS "Ignore U-boot args: " + +/* uboot_tag values for U-boot - kernel ABI revision 0; see head.S */ +#define UBOOT_TAG_NONE 0 +#define UBOOT_TAG_CMDLINE 1 +#define UBOOT_TAG_DTB 2 + +void __init handle_uboot_args(void) { + bool use_embedded_dtb = true; + bool append_cmdline = false; + #ifdef CONFIG_ARC_UBOOT_SUPPORT - /* make sure that uboot passed pointer to cmdline/dtb is valid */ - if (uboot_tag && is_kernel((unsigned long)uboot_arg)) - panic("Invalid uboot arg\n"); - - /* See if u-boot passed an external Device Tree blob */ - machine_desc = setup_machine_fdt(uboot_arg); /* uboot_tag == 2 */ - if (!machine_desc) + /* check that we know this tag */ + if (uboot_tag != UBOOT_TAG_NONE && + uboot_tag != UBOOT_TAG_CMDLINE && + uboot_tag != UBOOT_TAG_DTB) { + pr_warn(IGNORE_ARGS "invalid uboot tag: '%08x'\n", uboot_tag); + goto ignore_uboot_args; + } + + if (uboot_tag != UBOOT_TAG_NONE && + uboot_arg_invalid((unsigned long)uboot_arg)) { + pr_warn(IGNORE_ARGS "invalid uboot arg: '%px'\n", uboot_arg); + goto ignore_uboot_args; + } + + /* see if U-boot passed an external Device Tree blob */ + if (uboot_tag == UBOOT_TAG_DTB) { + machine_desc = setup_machine_fdt((void *)uboot_arg); + + /* external Device Tree blob is invalid - use embedded one */ + use_embedded_dtb = !machine_desc; + } + + if (uboot_tag == UBOOT_TAG_CMDLINE) + append_cmdline = true; + +ignore_uboot_args: #endif - { - /* No, so try the embedded one */ + + if (use_embedded_dtb) { machine_desc = setup_machine_fdt(__dtb_start); if (!machine_desc) panic("Embedded DT invalid\n"); + } - /* - * If we are here, it is established that @uboot_arg didn't - * point to DT blob. Instead if u-boot says it is cmdline, - * append to embedded DT cmdline. - * setup_machine_fdt() would have populated @boot_command_line - */ - if (uboot_tag == 1) { - /* Ensure a whitespace between the 2 cmdlines */ - strlcat(boot_command_line, " ", COMMAND_LINE_SIZE); - strlcat(boot_command_line, uboot_arg, - COMMAND_LINE_SIZE); - } + /* + * NOTE: @boot_command_line is populated by setup_machine_fdt() so this + * append processing can only happen after. + */ + if (append_cmdline) { + /* Ensure a whitespace between the 2 cmdlines */ + strlcat(boot_command_line, " ", COMMAND_LINE_SIZE); + strlcat(boot_command_line, uboot_arg, COMMAND_LINE_SIZE); } +} + +void __init setup_arch(char **cmdline_p) +{ + handle_uboot_args(); /* Save unparsed command line copy for /proc/cmdline */ *cmdline_p = boot_command_line;