Received: by 2002:ac0:b08d:0:0:0:0:0 with SMTP id l13csp4900724imc; Mon, 25 Feb 2019 13:15:50 -0800 (PST) X-Google-Smtp-Source: AHgI3IYnXOefrE/J9xPhjqOlnNin47E1aDIljjbzmikCROHZWeKnr33mlhdVu00UIlRO5qu/Wy7X X-Received: by 2002:a63:20a:: with SMTP id 10mr13287601pgc.346.1551129350638; Mon, 25 Feb 2019 13:15:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551129350; cv=none; d=google.com; s=arc-20160816; b=wdo3zKgPtaf1SvRq9RyfSisF8gxhVXMs0O8Y7T1T3eTkewGTKA1Bzk8qpT8dLp5zzm rZE30G43y+W5mZnhHrjkLQeYhcCkPIkLZnNGEC0peiIgPvDJCPJemGFeOMU1lygLMPGh mN70hvSzt8tacwVxUnDzE5nN1oRVK4Z05ETaU9GxgFgigmJXamku6jcfsB5yNnvCmyvj zv3o8WFKfcEUpuuYRA4o1L5+AfsVLRm3XWe10mCUDMeMiWO67prVgDuVfSMUinxKJn7h ilgk2dwHC+F3e0Upebjx0hwmcMK21V/Q9djciADneoEKUAhZ2Ewz3KWCyTNlo1+eFbOP GjSg== 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=U/UrGPZovnvZrPMdDWZEQoYToKtiXncuErB/WRvWDu0=; b=eysgQYCYFngtxl5ZZcxJyGfq90BKHg3ASvg/MdA+dsCldiGTR+rmiznPo7gcLSZo0s kBqEAk0mq9BLrPq5DUtJLc2AtOYQKWfQWZUsQKp5lp6YhGbKBt+UxCX869TaE7xmIgBz tmtuQv1uYzRCFJ4SYaxKthCblSVQH9z5nJzoy6KWOB9YavF6zgl8Od4k5zfmCubWaTOD sArpPMhiJEC8bjJMvgjmQtbE9mkD0nfrSV1aUF2WzdIppAXDl5js12lWLSma6WmUMWLj t8E5zBWjb9XTA603T7YNsyudkkRJOG0L+VnN678xBCAsYEqBRROgErcNq48SaMMZz1Ey zbxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=X2ODOJYj; 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 v10si131039plg.347.2019.02.25.13.15.35; Mon, 25 Feb 2019 13:15:50 -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=X2ODOJYj; 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 S1729291AbfBYVOf (ORCPT + 99 others); Mon, 25 Feb 2019 16:14:35 -0500 Received: from mail.kernel.org ([198.145.29.99]:45392 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729272AbfBYVOc (ORCPT ); Mon, 25 Feb 2019 16:14:32 -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 C454F2146F; Mon, 25 Feb 2019 21:14:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1551129271; bh=JxtM/6DE8tUX/lLHzFXQeanI1//mZOo/kqIb4acvsbI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X2ODOJYjrIOj0Zp/+O60Wus3e8fCdLh4Um/e8ufn+6gtfsTNXsXDzYXYqSaJRW5rq oiDDgzjKBLx/9uXkED8TJWZlPi/lIlsX/OZfcWySrezPZVWiGaHZ7nOQzu26iA13xd VQXr0WIB/fGrV3KwI+5hEgoyn5aQLGK/8IjZO0RM= 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.9 40/63] ARC: U-boot: check arguments paranoidly Date: Mon, 25 Feb 2019 22:11:40 +0100 Message-Id: <20190225195038.879854616@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190225195035.713274200@linuxfoundation.org> References: <20190225195035.713274200@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.9-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 @@ -381,43 +381,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;