From patchwork Thu Nov 15 05:52:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 10683565 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 E5CB514D6 for ; Thu, 15 Nov 2018 05:58:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D23A72BD8A for ; Thu, 15 Nov 2018 05:58:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C498E2BEF6; Thu, 15 Nov 2018 05:58:34 +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=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW 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 627512BD8A for ; Thu, 15 Nov 2018 05:58:34 +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=48ze4iLWJxXa4UuUYaMfoE6Y5S2NYdIpxV/1+HJ9NgY=; b=Lm8xTIELBfuXwK i1JkMJ72BnSQg+pi6r6zIbnkwtUuXr5LgUcoppzNtGVTpdgV6DKMobcHmvoSzXqNjKoQpC+c7lLIV jG6t8wspJ9sZMFxi2d3U/HdEIZvdDdnHS8vYRypea+01CdseQ30hbC/sOrEAVkvBRfESTwMow8sAv z7y9f9iCRjoWUIQICN9CSNR0EuFTlocpGZMreBWWkG5omxDYx0rlYKiahRxRY+Kt0x5yKQfAAWWCG BQ1hUN+jiMWoomk9Wodo/YlMnoObc55uT/RuPoaGnrVGNo0/7EtMzoQ+83+kFupYHpqrHd8Vxf0Bh lFKScSB6bAqhVMy756JA==; 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 1gNAfb-0003fi-Hp; Thu, 15 Nov 2018 05:58:27 +0000 Received: from mail-yw1-xc43.google.com ([2607:f8b0:4864:20::c43]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gNAbF-0006gC-Ks for linux-arm-kernel@lists.infradead.org; Thu, 15 Nov 2018 05:54:02 +0000 Received: by mail-yw1-xc43.google.com with SMTP id l200so2683288ywe.10 for ; Wed, 14 Nov 2018 21:53:47 -0800 (PST) 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=z6fpkRKvJjJuOzp7wHTBqPrWgQFd+w1vM5L/3Tpwhps=; b=NsKJ0pnbz1wxK7DAjNL7ynj+T731invAf4RnUN/pFf3mEkpgElVGh79yv/MP4BIjW9 oMsjLQBGyoTfTSJrQ+CIv2CP3QDoesQIUj+Wse6zWsLaY2Vx6tWHv7U0Z4oyZXlB/3vU K9LtUxww4zddwC7VpMa1XzXmlpqRwG5oiLy8k= 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=z6fpkRKvJjJuOzp7wHTBqPrWgQFd+w1vM5L/3Tpwhps=; b=eo4ySPThXrNxc68U6wXbqUitLCuhZabhAvTBbbWcAIHHJijYQNpXwHTlAszVKDUdAF nK58yI+tDLzwhvlnNgWzyY2GN21P/QETJW59kWoMDB69jxbPtzxBByLQZgsw4tmui+hn wCNpjwO049YwZgYaxeHa+L7RlTJ3l2VMy9iEM8NzXXVvLw/ImH/Xg3DfIoJcC4BSD7Ij e83koSZlc7FuZEPIGst63wpnwy1OiJ3Mwzh58blHYJNt1+9H0hvVPHZ+DbRqpG2uF4+G lrF6KsEF1eMvEd5uF1spc1lf2Aeu9qUp8gWMH1Z0T7fbrxtsY2QYBE6RN/Q6Jj0xLb7i 9EWw== X-Gm-Message-State: AGRZ1gLTh+N14R2pDjvACNZW0KRT3Wu9OvQeikiPyzK1zP2EC3T0PwQ0 pRziJRcvb26ZmeMpqAiai9Ur6g== X-Google-Smtp-Source: AJdET5dRF8aftl5JbSuc+XrJ2uCh54qihW81ZcVe7oEYrH0/OUXVxTU9B5EZOYh77ZobH769wki/1g== X-Received: by 2002:a81:26d4:: with SMTP id m203-v6mr4471570ywm.327.1542261226677; Wed, 14 Nov 2018 21:53:46 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id m123-v6sm1526751ywe.86.2018.11.14.21.53.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 14 Nov 2018 21:53:46 -0800 (PST) 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 v16 16/16] arm64: kexec_file: add kaslr support Date: Thu, 15 Nov 2018 14:52:55 +0900 Message-Id: <20181115055254.2812-17-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20181115055254.2812-1-takahiro.akashi@linaro.org> References: <20181115055254.2812-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-20181114_215357_693713_087270CC X-CRM114-Status: GOOD ( 15.80 ) 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 | 46 +++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/machine_kexec_file.c index ab296b98d633..a0a730bd9be6 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 @@ -28,6 +29,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, @@ -46,11 +48,38 @@ 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, void *dtb) { int nodeoffset; + u64 value; int ret; nodeoffset = fdt_path_offset(dtb, "/chosen"); @@ -106,12 +135,27 @@ static int setup_dtb(struct kimage *image, return -EINVAL; } + /* add kaslr-seed */ + ret = fdt_delprop(dtb, nodeoffset, FDT_PSTR_KASLR_SEED); + if (ret && (ret != -FDT_ERR_NOTFOUND)) + return -EINVAL; + + if (random_ready) { + get_random_bytes(&value, sizeof(value)); + ret = fdt_setprop_u64(dtb, nodeoffset, FDT_PSTR_KASLR_SEED, + value); + if (ret) + return (ret == -FDT_ERR_NOSPACE ? -ENOMEM : -EINVAL); + } else { + pr_notice("kaslr-seed won't be fed\n"); + } + return 0; } /* * More space needed so that we can add initrd, bootargs, - * userable-memory-range and elfcorehdr. + * userable-memory-range, elfcorehdr and kaslr-seed. */ #define DTB_EXTRA_SPACE 0x1000