Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp396925imm; Thu, 27 Sep 2018 23:51:54 -0700 (PDT) X-Google-Smtp-Source: ACcGV61bQxaV1WaiUmy1HbNyo5WWZdV58DSQ4rSsGWgSqyD0eP/sgN4ep4qOg9K6I0BRf4n002ok X-Received: by 2002:a62:2b50:: with SMTP id r77-v6mr15030208pfr.51.1538117514624; Thu, 27 Sep 2018 23:51:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538117514; cv=none; d=google.com; s=arc-20160816; b=g8z7tTyNBNyRgvYXFFpSob8yeDxZzBMLr6RfsQZfeRhcg8IcwZ0ziT6rQaAQPTNTm3 kTAlx40iD9EWbh+dvPVNf4xSyyjVeNLcuWzM3yvGnDWRh+VVzjU/Ge/Je9HoS9gUyJao w7M66EjXC9a1DgWXYbdiJEzY387XqYY0zgf3x774uASH+8lqE91TuMh4QXWUBGUCyn7o B8vne5Nb/dAPTx1JkVB0rkxt33JD3RQfZUUbc83ESqsmdEF7IRAQYJkwX2znXqPmQT+q A/GDV4cC4OlSutAVD4ekELc7BCaD5WJJ3ds7qRa0U53ALsm2/uLUuZJ29ZqF4J9oqSTy zpXw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=A1to76Muk1fdla+xjmMFJtb5BFlQOFVgZ1KKX32hm3g=; b=Dw1WdsJux67/ygpTboVvLQsYbI/QtsZkAPao7gn+NgAVGB0xyd+niItbrftkUybOCO ApaU19yJX6QfeAcVOEVuTBb/lhxIAg3hSrtK9M2TibCFUx5g1ptdH/7cj6cB//5lqXz6 Uio/DKiVxXs1X8z9Vnfw5/0ItjYPVc8OJ4Im/bvblOxcWFH0l4T43BwteSjzTQZpvzHw z3cX9M3MhsGuZfWeZ7cBnZkVdsKiNcxCu+cxXD8b/vxBoBQe3oBUasQShBkkFfmcAcbO HvPglul2r6qd9+Yx4Pwz4yP/0cOLxHaxeKDkvWjsu7AZ9/bkzk0R0tm35zYVBP0P6EvL 9Kqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WPP9Dm8h; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ay4-v6si4041048plb.410.2018.09.27.23.51.39; Thu, 27 Sep 2018 23:51:54 -0700 (PDT) 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=@linaro.org header.s=google header.b=WPP9Dm8h; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729233AbeI1NLv (ORCPT + 99 others); Fri, 28 Sep 2018 09:11:51 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:36368 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728765AbeI1NLv (ORCPT ); Fri, 28 Sep 2018 09:11:51 -0400 Received: by mail-pf1-f193.google.com with SMTP id b7-v6so3642319pfo.3 for ; Thu, 27 Sep 2018 23:49:36 -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 :mime-version:content-transfer-encoding; bh=A1to76Muk1fdla+xjmMFJtb5BFlQOFVgZ1KKX32hm3g=; b=WPP9Dm8h5cUbvm4Vfgv7CG3sbSL7MvhM+E7czCqoCAP7d5W0p+jJMUvGc6ekYRON7Q WjHEuVNSOUEXW6cep8nc4kw9iwIZywVej83PvFnvtFRAHMXDWfv8pwR4EjzmQK/Mxkp0 mCmlN2yGuQ9kXGuD2d1zUrHD3hAQAk7WOD/H0= 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:mime-version:content-transfer-encoding; bh=A1to76Muk1fdla+xjmMFJtb5BFlQOFVgZ1KKX32hm3g=; b=H9gHTr7VW7Kp1HeMACiptZ2S5D77PSZTmCiInRlOkjD/sPmndzuuS0Fc20nMQehK6n R9m3JIrZwazThMDWbjZDKfmEHPXvQuk4ss17gV9Hh6TTWZI0KAbl/sp8E6R7OVbzPJE7 FSkcHc52+goYlRfbjLZxp7iAoUb1UqmiFThMwT6KJ+Sy6pfHxBEs0ufr2+2lvhEp2mCC JNl6aefv8rixaIu3PTrOULRXh2xRZ6gNJYQgk4HhMX+00AHju70z07LYUXSfw0vblgUq Vy6nNRYrXMIsMR4QITTZ6HfInatXGczuSj5kpMBQ7/VjJSypEE5ycCoO0KspGTGjM4is CdmA== X-Gm-Message-State: ABuFfojclXGjdVuwFOJnSuu3pnAFhoAeMEWryYYajM+CCdgh6QUTSfkM fJh0GBjo8Ltk1TMcyYra+BD3Og== X-Received: by 2002:a63:5816:: with SMTP id m22-v6mr13507559pgb.332.1538117376233; Thu, 27 Sep 2018 23:49:36 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id r12-v6sm5024997pfh.79.2018.09.27.23.49.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Sep 2018 23:49:35 -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 Cc: prudo@linux.ibm.com, ard.biesheuvel@linaro.org, james.morse@arm.com, bhsharma@redhat.com, kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v15 16/16] arm64: kexec_file: add kaslr support Date: Fri, 28 Sep 2018 15:48:41 +0900 Message-Id: <20180928064841.14117-17-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180928064841.14117-1-takahiro.akashi@linaro.org> References: <20180928064841.14117-1-takahiro.akashi@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 4b5ea256c84c..c2ca7c921e7a 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, @@ -54,6 +82,7 @@ static int setup_dtb(struct kimage *image, int addr_cells, size_cells; size_t buf_size, range_size; int nodeoffset; + u64 value; int ret; /* duplicate dt blob */ @@ -81,6 +110,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; @@ -160,6 +191,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; -- 2.19.0