From patchwork Fri Sep 28 06:48:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 10619083 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 8A935913 for ; Fri, 28 Sep 2018 07:18:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 714732A5EE for ; Fri, 28 Sep 2018 07:18:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 603E02A62E; Fri, 28 Sep 2018 07:18:11 +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 9E5292A5EE for ; Fri, 28 Sep 2018 07:18:07 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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=oxShrqo0MKia109MEVQSlobcdOW+EN1ilHgcsFfmv6g=; b=Ef7EKK/QNmN9wF qgyw2CznCsZ4/NEeVHE37KNd8aWRuFXFwUgeBjPBCOsdUPBfhcrJlkIlbYApq12nkL2M9J/zFzFKe BxpcrdvvDNoX6iZ+hHpWSrW7acQ3/y3J5LC8Yja1W3eUMTy9MzIY357caFQc+4LwhYdsxsRAoBpOs XiCytNc3pWcfJbH1Cf7dmGWFEy3NSXJItklGKwGCnBThwgYNduPFHkuDE5ccLDF67NF0n5dCX2dfm VUYhpI1TM/je4+b3VhG6qWdvQnKMauji9ATBaXR5lfvL/EezjAjLwNaBLFymRB1oTJNDlj4nTNC2k GA9ab2Xb/h9bFQSbNUYg==; 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 1g5n2K-0003mu-OP; Fri, 28 Sep 2018 07:18:04 +0000 Received: from mail-pg1-x542.google.com ([2607:f8b0:4864:20::542]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1g5mb7-0004yk-NK for linux-arm-kernel@lists.infradead.org; Fri, 28 Sep 2018 06:50:25 +0000 Received: by mail-pg1-x542.google.com with SMTP id b129-v6so3703858pga.13 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=l+XsKTdCYAg28/PzIVirpK3l26mFL0faHAPdTwsPFqWUSR8ipmgu9Szq50GGCWp07C ZuFuuj3DjSfwiT/BRrGOz0hV5hNUpCranzDWYIaq1oQmlhpDXMU/nZzXKW+REK0k3Kis JOFDyC3KbuEoaFCIfCd0fo+EZsE79KHCPhN7OSNCjsx2gXQEGob/DIZXFJi0NtYieMWK YfoA9XlMaQi1+zqoqKw483FAut/42/5KwKvgDBKme7JRqowVVKcf1ZsJOLSssMlU7os+ K46afFcwAaJ4Rz77DvXuabzTKqMAay/gGjtvfQWteFl2DWoyB+dS2GTeGYJ3jeSuciC3 /9Bg== X-Gm-Message-State: ABuFfohSUYfWSYEgbB98WQu0iEs4UVpNDjjqpat0S/ngjqmZJm5LLcvg aduO6yK97FuYcG4CKV2czX8s8A== X-Google-Smtp-Source: ACcGV62mEVMP4yeRl5oKUOSiQjF8auBWlkh6aFE/N6Azq43A328TlDptRmNL7cSZUf8D4XMBy2zjuw== 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 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180927_234957_905292_41FE64F8 X-CRM114-Status: GOOD ( 17.33 ) 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 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 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;