From patchwork Fri Sep 7 08:00:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 10591879 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9FB495A4 for ; Fri, 7 Sep 2018 08:18:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8C3222AC91 for ; Fri, 7 Sep 2018 08:18:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7C1732ACAC; Fri, 7 Sep 2018 08:18:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E36EF2AC91 for ; Fri, 7 Sep 2018 08:18:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=GSxWYboBim20iHZWeAsqTnt+M5CEaGDqPpAlb+zUCck=; b=asTzKG6PKpC5HXf1zN/ON8jduh 38gET3i3cO6pLTyRJOISY7Ib7NmKCHU4uHyWGU+FVDACdQWR+xgJoPL2McaOCEcPbUoeCYhQCwMHs zZM5mysjfsBJYbtDwKAsHLaBWMpJYul+LIR0B3IdfujrPRKtiR6xD3qoVcjFvo8idAYTPuc+32lXA 1k9IqVFm3X1kvE483ndvT2CCO+8sczJ9VYw3IxDLSTXvcY1L9z3L3GFB8vY0K9aIdnhnkFpnsQdAo d4vOeQoxyOzWiof9Jm3Oqjev8LW5Po/8C29cLp3KIAvzJPnvXGUJxlqnPYoEJ1u5eTunAMat2MDUn G3Uwhr9w==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fyBxs-0004jU-FY; Fri, 07 Sep 2018 08:18:04 +0000 Received: from mail-pl1-f193.google.com ([209.85.214.193]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fyBjO-0003M0-3y for linux-arm-kernel@lists.infradead.org; Fri, 07 Sep 2018 08:03:07 +0000 Received: by mail-pl1-f193.google.com with SMTP id j8-v6so6224640pll.12 for ; Fri, 07 Sep 2018 01:02:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=hvAWBPxjHLCiuG/UDQDc1Nu8BX1v01gDGjuspChhtac=; b=PUuXrmrhzXKdXLed1SIuT8fjgiUdG2uDYfqr82pc5ulwV00S7s0y1ONHe+4D9P0HS3 Xa74+tT+vF3yEETwBlMtSnKWua/f6afVjTDULUfRHaQMYEoegvwXamIHA+dGYRpr9fXw lp/17gfVbPnFF3OVqd0Ziwek3IQizPPI7r8NE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=hvAWBPxjHLCiuG/UDQDc1Nu8BX1v01gDGjuspChhtac=; b=VZpMiSP2fd2cjn/2YSl1yta7CiqygY7fHaJbNPKcjAT48PoySGqz5g5LW2uehfhYIq 0blf9p0UBzFzXGVUhaxyBZpkNolQ3YmHpOSIeGrPuPlPFZZNyhqVMmBLh7KRmA4zTV++ wtVykm8lFGtO8vE6puAUAUmoEh71Tsn+C8xpdvsJ5ZcqZhqCMgN8CJyVOt+5tIUEUY+d 0oef2XauHA2oWHYCI8HpPymJmLDsxrzoAKsVVWTSOBCqgvn1ajj6oBweUve7YLuHnRPV z4KP2ROker2EF/xscoxFJx/TY93Vv9u687mHENwlqvSlmtnLUvIYv8wqeAgEjdOdxwVe 2rpA== X-Gm-Message-State: APzg51DQS/Kf+26r8qTYOTj46dGxw3dWoDWNqWrp6/5qqc4E9BbE6ZU2 PtnaAg4F32oA6vpySbNTiHJAeg== X-Google-Smtp-Source: ANB0VdaAw4JGA5PGH3LsHjF7IpXsyUJhuSDjAZIZWuFsc1UDQYp/duZAnmUOwq2146Neg1ZDJaaAVw== X-Received: by 2002:a17:902:6b47:: with SMTP id g7-v6mr6800958plt.128.1536307314969; Fri, 07 Sep 2018 01:01:54 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id 22-v6sm11078582pfl.126.2018.09.07.01.01.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Sep 2018 01:01:54 -0700 (PDT) From: AKASHI Takahiro To: catalin.marinas@arm.com, will.deacon@arm.com, dhowells@redhat.com, vgoyal@redhat.com, herbert@gondor.apana.org.au, davem@davemloft.net, dyoung@redhat.com, bhe@redhat.com, arnd@arndb.de, schwidefsky@de.ibm.com, heiko.carstens@de.ibm.com Subject: [PATCH v14 16/16] arm64: kexec_file: add kaslr support Date: Fri, 7 Sep 2018 17:00:40 +0900 Message-Id: <20180907080040.4967-11-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180907080040.4967-1-takahiro.akashi@linaro.org> References: <20180907080040.4967-1-takahiro.akashi@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180907_010306_200540_17A9A015 X-CRM114-Status: GOOD ( 17.15 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: ard.biesheuvel@linaro.org, bhsharma@redhat.com, kexec@lists.infradead.org, linux-kernel@vger.kernel.org, prudo@linux.ibm.com, AKASHI Takahiro , james.morse@arm.com, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Adding "kaslr-seed" to dtb enables triggering kaslr, or kernel virtual address randomization, at secondary kernel boot. We always do this as it will have no harm on kaslr-incapable kernel. We don't have any "switch" to turn off this feature directly, but still can suppress it by passing "nokaslr" as a kernel boot argument. Signed-off-by: AKASHI Takahiro Cc: Catalin Marinas Cc: Will Deacon --- arch/arm64/kernel/machine_kexec_file.c | 45 ++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/machine_kexec_file.c index ecaecb122cad..967db9824e3f 100644 --- a/arch/arm64/kernel/machine_kexec_file.c +++ b/arch/arm64/kernel/machine_kexec_file.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -27,6 +28,7 @@ #define FDT_PSTR_INITRD_STA "linux,initrd-start" #define FDT_PSTR_INITRD_END "linux,initrd-end" #define FDT_PSTR_BOOTARGS "bootargs" +#define FDT_PSTR_KASLR_SEED "kaslr-seed" const struct kexec_file_ops * const kexec_file_loaders[] = { &kexec_image_ops, @@ -45,6 +47,32 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image) return kexec_image_post_load_cleanup_default(image); } +/* crng needs to have been initialized for providing kaslr-seed */ +static int random_ready; + +static void random_ready_notified(struct random_ready_callback *unused) +{ + random_ready = 1; +} + +static struct random_ready_callback random_ready_cb = { + .func = random_ready_notified, +}; + +static __init int init_random_ready_cb(void) +{ + int ret; + + ret = add_random_ready_callback(&random_ready_cb); + if (ret == -EALREADY) + random_ready = 1; + else if (ret) + pr_warn("failed to add a callback for random_ready\n"); + + return 0; +} +late_initcall(init_random_ready_cb) + static int setup_dtb(struct kimage *image, unsigned long initrd_load_addr, unsigned long initrd_len, char *cmdline, unsigned long cmdline_len, @@ -53,6 +81,7 @@ static int setup_dtb(struct kimage *image, void *buf = NULL; size_t buf_size, range_size; int nodeoffset; + u64 value; int ret; /* check ranges against root's #address-cells and #size-cells */ @@ -85,6 +114,8 @@ static int setup_dtb(struct kimage *image, /* can be redundant, but trimmed at the end */ buf_size += fdt_prop_len(FDT_PSTR_BOOTARGS, cmdline_len); + buf_size += fdt_prop_len(FDT_PSTR_KASLR_SEED, sizeof(u64)); + buf = vmalloc(buf_size); if (!buf) { ret = -ENOMEM; @@ -164,6 +195,20 @@ static int setup_dtb(struct kimage *image, } } + /* add kaslr-seed */ + fdt_delprop(buf, nodeoffset, FDT_PSTR_KASLR_SEED); + if (random_ready) { + get_random_bytes(&value, sizeof(value)); + ret = fdt_setprop_u64(buf, nodeoffset, FDT_PSTR_KASLR_SEED, + value); + if (ret) { + ret = -EINVAL; + goto out_err; + } + } else { + pr_notice("kaslr-seed won't be fed\n"); + } + /* trim a buffer */ fdt_pack(buf); *dtb_buf = buf;