Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp553874imm; Wed, 1 Aug 2018 01:03:45 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfJOluccm4jAO8M4oFXsq3RZlYBrwh8pm0A/QCUnh9K4GeuXGy0e42Paf543EfrhaOc0Ccd X-Received: by 2002:a63:460d:: with SMTP id t13-v6mr23373908pga.201.1533110625476; Wed, 01 Aug 2018 01:03:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533110625; cv=none; d=google.com; s=arc-20160816; b=OeHxfQNKR+7MqRqBaWF4B17u6dTJvsnln4JMyrn38bhLzMlD+ydbi8C/NGRnzyvfl3 6GaqHlWtDWFvYXQ6kOKtVVMeerx7Sh/sHvaNF1ejhEyh6ITmj9/isY56rr4RduBg9Gs1 6uyr6iu4a3JWgf26iXEWOtRLchn0zta8iziqtvgrpZSbAS9wegynHMf1rD6RKfOZug/Z wfn27Dqe8raXJ4out9BidwuJSnJ0eTP7vXx2HGD3Wtsn8eeAoulAMokeI20xR+zMqjQQ XfDBfF6oBZ4VNwwjjxgD7raNpgk9DffsbytJ/Xw3DWsWaEPUKtqG/6bbhAuCDwqsaGjb BvDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=hvAWBPxjHLCiuG/UDQDc1Nu8BX1v01gDGjuspChhtac=; b=K2xCQQgObrN3LfsX3LjSkcPo/a29tk5fdMawjmzc9iT66JcJ2NCM35cxW7VXkPc9lo zCyoW1FTFiNV4T/Wqg9tYr+fMTNjYE3GymwrQoNoumczcNvBAE5F1dGnf5l65s5FPZc6 8HWsU/2PZiV1bow5c7g8HF8xCMyIySOQg6VNgoCvEciKnsKiWtWAZ98oCTt8RYhIplTg k2kRIMCGhXcPMQm4ITA+sXuQ0Bz8tSEw/9ncj31I/GJbn6L0DUpLS2aB7ni0wuz/EVCk TMYGRybzKsGBJjFhf7dMc9Ny/Pd6dzZ0CMlYrZRJK0fRATJKcY/U90MNLQKJCWHEIU68 Jx0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WU9YMzQP; 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 190-v6si18175045pfu.343.2018.08.01.01.03.31; Wed, 01 Aug 2018 01:03:45 -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=WU9YMzQP; 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 S2388577AbeHAJqH (ORCPT + 99 others); Wed, 1 Aug 2018 05:46:07 -0400 Received: from mail-pl0-f68.google.com ([209.85.160.68]:41264 "EHLO mail-pl0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387673AbeHAJqH (ORCPT ); Wed, 1 Aug 2018 05:46:07 -0400 Received: by mail-pl0-f68.google.com with SMTP id w8-v6so8412783ply.8 for ; Wed, 01 Aug 2018 01:01:40 -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=WU9YMzQPUTnoNa5HnOw5eBPp1T5wlr/z+viQAp2LZ9cSIxO8ipv2mPcyWDp4ZgzUnT WyDkooAOOzxEqlB8kLCDABQO2QVOlRMhZB3KkEfJ6khFhMadGcYSqYRkkKUVjBl0VlbF VK+VT5wZGhXDfPxb5LJtU9vUnaUx9zApOz56g= 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=saAK6TY3DBTkCmVUqdzuwQ9SNCC1150rj9tHYqhSEqDZPkc2vpCIqFJ82eNjOLiyTf lOR8sVKQCYZWgGddpvOHDXRvHVZjKuI8tbsm/6ufKk/9TOEBGHiamIkfKLju+vNo9+Ot nOWVCvExs4cYCCf/4CflHlBal2Q3tCpWkMxQi2eGWw8/l2QjSdgujhpRpLVcDVwnmHyw i+VG4R3P+v8V++ilnldcg6i/aLhiTTedwQ/y5iSkWJ+q30JDRZ2P6FX6160SsA7NfcY5 6f/4i/T12u73FECziBdkch475pqDCS/Jq3AarnwmsEqyfEpowL7Qwhps/YlviHfzW/vh ZXCA== X-Gm-Message-State: AOUpUlFT3+jXRyRPEotMf90jPl1T7/pbZd4nShDVe60Ptcde0oQaFOEm BQGgKIpJDuCqAq7de7Jz8cppMA== X-Received: by 2002:a17:902:e005:: with SMTP id ca5-v6mr23681465plb.224.1533110500350; Wed, 01 Aug 2018 01:01:40 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id y3-v6sm46126520pfi.24.2018.08.01.01.01.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 01 Aug 2018 01:01:39 -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 v13 16/16] arm64: kexec_file: add kaslr support Date: Wed, 1 Aug 2018 16:58:20 +0900 Message-Id: <20180801075820.3753-17-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180801075820.3753-1-takahiro.akashi@linaro.org> References: <20180801075820.3753-1-takahiro.akashi@linaro.org> 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 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; -- 2.18.0