From patchwork Tue Apr 18 13:44:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 675331 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CD75FC77B7F for ; Tue, 18 Apr 2023 13:44:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232054AbjDRNoi (ORCPT ); Tue, 18 Apr 2023 09:44:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230033AbjDRNof (ORCPT ); Tue, 18 Apr 2023 09:44:35 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1AD1612C9E; Tue, 18 Apr 2023 06:44:32 -0700 (PDT) Received: from pps.filterd (m0353728.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 33IDgC5r016772; Tue, 18 Apr 2023 13:44:16 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=SxD4DHyfapu38S/pfPZj82NR2pRsC45c+glWfwy19i0=; b=jsQ0WgnsZDgshguk54ppm2VFX33zHYxMHH6iSQcBC9sJJEUyre/5kgBEFHkXwENDMDwe oOdUE13uTOC8tx/IrfiYc/2mYFa5CmwtlTVA0/SXDTpl5eCa1GpL28BDau0Bsn031d8o BQFnhD1Ytzhg78LQB/P3Lj2MeBCTVmYePXXsvQc1twEAyoRDGFbgDqjlo4L1BcJ0WNQY R8yY8Kq6EE1WXhaV/q6s+iCgz5Bve8OdIrtZIylw6v74+SkTzxbL5NmB6im9k0/fzRS6 aykKZy2tJfLhhd2K5yn/+StWNSHf7rjweRyxTvYzfsEY433W6zXPKkhJJWYwfv4twKJ0 fA== Received: from ppma04dal.us.ibm.com (7a.29.35a9.ip4.static.sl-reverse.com [169.53.41.122]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3q1pww294p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Apr 2023 13:44:16 +0000 Received: from pps.filterd (ppma04dal.us.ibm.com [127.0.0.1]) by ppma04dal.us.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 33ID321E029453; Tue, 18 Apr 2023 13:44:15 GMT Received: from smtprelay04.wdc07v.mail.ibm.com ([9.208.129.114]) by ppma04dal.us.ibm.com (PPS) with ESMTPS id 3pykj7dct6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Apr 2023 13:44:15 +0000 Received: from smtpav01.wdc07v.mail.ibm.com (smtpav01.wdc07v.mail.ibm.com [10.39.53.228]) by smtprelay04.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 33IDiDpV47907224 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Apr 2023 13:44:13 GMT Received: from smtpav01.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2753F58063; Tue, 18 Apr 2023 13:44:13 +0000 (GMT) Received: from smtpav01.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1A6EF58055; Tue, 18 Apr 2023 13:44:12 +0000 (GMT) Received: from sbct-3.pok.ibm.com (unknown [9.47.158.153]) by smtpav01.wdc07v.mail.ibm.com (Postfix) with ESMTP; Tue, 18 Apr 2023 13:44:11 +0000 (GMT) From: Stefan Berger To: kexec@lists.infradead.org, devicetree@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Cc: nayna@linux.ibm.com, nasastry@in.ibm.com, mpe@ellerman.id.au, Palmer Dabbelt , Rob Herring , Mimi Zohar Subject: [PATCH v9 1/4] drivers: of: kexec ima: Support 32-bit platforms Date: Tue, 18 Apr 2023 09:44:06 -0400 Message-Id: <20230418134409.177485-2-stefanb@linux.ibm.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230418134409.177485-1-stefanb@linux.ibm.com> References: <20230418134409.177485-1-stefanb@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: nSgQdJ3J38SBpT4xcRMRzP8gtAHBm_No X-Proofpoint-ORIG-GUID: nSgQdJ3J38SBpT4xcRMRzP8gtAHBm_No X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-04-18_09,2023-04-18_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 malwarescore=0 impostorscore=0 adultscore=0 lowpriorityscore=0 mlxlogscore=999 bulkscore=0 clxscore=1011 priorityscore=1501 mlxscore=0 spamscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303200000 definitions=main-2304180118 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org From: Palmer Dabbelt RISC-V recently added kexec_file() support, which uses enables kexec IMA. We're the first 32-bit platform to support this, so we found a build bug. Acked-by: Rob Herring Signed-off-by: Palmer Dabbelt Reviewed-by: Mimi Zohar Reviewed-by: Jerry Snitselaar --- drivers/of/kexec.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c index f26d2ba8a371..1373d7e0a9b3 100644 --- a/drivers/of/kexec.c +++ b/drivers/of/kexec.c @@ -250,8 +250,8 @@ static int setup_ima_buffer(const struct kimage *image, void *fdt, if (ret) return -EINVAL; - pr_debug("IMA buffer at 0x%llx, size = 0x%zx\n", - image->ima_buffer_addr, image->ima_buffer_size); + pr_debug("IMA buffer at 0x%pa, size = 0x%zx\n", + &image->ima_buffer_addr, image->ima_buffer_size); return 0; } From patchwork Tue Apr 18 13:44:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 674666 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D4408C77B78 for ; Tue, 18 Apr 2023 13:44:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232278AbjDRNo5 (ORCPT ); Tue, 18 Apr 2023 09:44:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230463AbjDRNom (ORCPT ); Tue, 18 Apr 2023 09:44:42 -0400 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80C84D325; Tue, 18 Apr 2023 06:44:35 -0700 (PDT) Received: from pps.filterd (m0353724.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 33IDQEB5014851; Tue, 18 Apr 2023 13:44:18 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=TEGBONkyuog+F35viyuYWUr10yzGVlxA8iH4QwsmVSU=; b=QsYDnL4lS3xM5aT8KiHJbr7kZOK1ahu1Y7J00QkOimGxoPN2GKRL6vr7K0ubEJeMSx2O nyqlOQIPaSIR55ZeeZ6p/nVHMuU+i1HBkeJ36vLadtvATdVxTpFDIugeZmVj0MNwudtR 4lIZ79fXWbnxxh4fUyRNZQXaIYN7gszfl7Fdd1IbvXPbfuvpa+YIhO5gvumL/yIvFTh0 rgX7qAmaVEzvrbiiAs0pKTGMwXaJQ8PYEBEmgHZoXzs84vBro2ISdfBWWwy4FL9k1aSE fbkPsE+H2yPCUAvzpX7KQYpTeozCJUC54+9nSZCFlm4EY/GYl+J5BV+7kNSVjKpKz8e3 xg== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3q1q529xx0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Apr 2023 13:44:18 +0000 Received: from m0353724.ppops.net (m0353724.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 33ICK4CU026346; Tue, 18 Apr 2023 13:44:17 GMT Received: from ppma01dal.us.ibm.com (83.d6.3fa9.ip4.static.sl-reverse.com [169.63.214.131]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3q1q529xwg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Apr 2023 13:44:17 +0000 Received: from pps.filterd (ppma01dal.us.ibm.com [127.0.0.1]) by ppma01dal.us.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 33ID0C8J003732; Tue, 18 Apr 2023 13:44:16 GMT Received: from smtprelay05.wdc07v.mail.ibm.com ([9.208.129.117]) by ppma01dal.us.ibm.com (PPS) with ESMTPS id 3pykj7weej-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Apr 2023 13:44:16 +0000 Received: from smtpav01.wdc07v.mail.ibm.com (smtpav01.wdc07v.mail.ibm.com [10.39.53.228]) by smtprelay05.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 33IDiEDE33620586 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Apr 2023 13:44:14 GMT Received: from smtpav01.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A3EF058063; Tue, 18 Apr 2023 13:44:14 +0000 (GMT) Received: from smtpav01.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4F86958066; Tue, 18 Apr 2023 13:44:13 +0000 (GMT) Received: from sbct-3.pok.ibm.com (unknown [9.47.158.153]) by smtpav01.wdc07v.mail.ibm.com (Postfix) with ESMTP; Tue, 18 Apr 2023 13:44:13 +0000 (GMT) From: Stefan Berger To: kexec@lists.infradead.org, devicetree@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Cc: nayna@linux.ibm.com, nasastry@in.ibm.com, mpe@ellerman.id.au, Stefan Berger , Jarkko Sakkinen , Jason Gunthorpe , Rob Herring , Frank Rowand , Mimi Zohar , Nageswara R Sastry , Coiby Xu Subject: [PATCH v9 2/4] tpm: of: Make of-tree specific function commonly available Date: Tue, 18 Apr 2023 09:44:07 -0400 Message-Id: <20230418134409.177485-3-stefanb@linux.ibm.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230418134409.177485-1-stefanb@linux.ibm.com> References: <20230418134409.177485-1-stefanb@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: i92gmyUBa8RFw3E1tvHzitilikJ19_tg X-Proofpoint-ORIG-GUID: BumNmB7KdGlnfVAk3Q2It202VDnZXF5P X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-04-18_09,2023-04-18_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 clxscore=1011 suspectscore=0 impostorscore=0 adultscore=0 bulkscore=0 lowpriorityscore=0 mlxscore=0 priorityscore=1501 mlxlogscore=999 malwarescore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303200000 definitions=main-2304180118 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Simplify tpm_read_log_of() by moving reusable parts of the code into an inline function that makes it commonly available so it can be used also for kexec support. Call the new of_tpm_get_sml_parameters() function from the TPM Open Firmware driver. Signed-off-by: Stefan Berger Cc: Jarkko Sakkinen Cc: Jason Gunthorpe Cc: Rob Herring Cc: Frank Rowand Reviewed-by: Mimi Zohar Tested-by: Nageswara R Sastry Tested-by: Coiby Xu Acked-by: Jarkko Sakkinen --- v9: - Rebased on 6.3-rc7; call tpm_read_log_memory_region if -ENODEV returned v7: - Added original comment back into inlined function v4: - converted to inline function --- drivers/char/tpm/eventlog/of.c | 30 +++++----------------------- include/linux/tpm.h | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c index 930fe43d5daf..41688d9cbd3b 100644 --- a/drivers/char/tpm/eventlog/of.c +++ b/drivers/char/tpm/eventlog/of.c @@ -51,11 +51,10 @@ static int tpm_read_log_memory_region(struct tpm_chip *chip) int tpm_read_log_of(struct tpm_chip *chip) { struct device_node *np; - const u32 *sizep; - const u64 *basep; struct tpm_bios_log *log; u32 size; u64 base; + int ret; log = &chip->log; if (chip->dev.parent && chip->dev.parent->of_node) @@ -66,30 +65,11 @@ int tpm_read_log_of(struct tpm_chip *chip) if (of_property_read_bool(np, "powered-while-suspended")) chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED; - sizep = of_get_property(np, "linux,sml-size", NULL); - basep = of_get_property(np, "linux,sml-base", NULL); - if (sizep == NULL && basep == NULL) + ret = of_tpm_get_sml_parameters(np, &base, &size); + if (ret == -ENODEV) return tpm_read_log_memory_region(chip); - if (sizep == NULL || basep == NULL) - return -EIO; - - /* - * For both vtpm/tpm, firmware has log addr and log size in big - * endian format. But in case of vtpm, there is a method called - * sml-handover which is run during kernel init even before - * device tree is setup. This sml-handover function takes care - * of endianness and writes to sml-base and sml-size in little - * endian format. For this reason, vtpm doesn't need conversion - * but physical tpm needs the conversion. - */ - if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0 && - of_property_match_string(np, "compatible", "IBM,vtpm20") < 0) { - size = be32_to_cpup((__force __be32 *)sizep); - base = be64_to_cpup((__force __be64 *)basep); - } else { - size = *sizep; - base = *basep; - } + if (ret < 0) + return ret; if (size == 0) { dev_warn(&chip->dev, "%s: Event log area empty\n", __func__); diff --git a/include/linux/tpm.h b/include/linux/tpm.h index 4dc97b9f65fb..dd9a376a1dbb 100644 --- a/include/linux/tpm.h +++ b/include/linux/tpm.h @@ -461,4 +461,40 @@ static inline struct tpm_chip *tpm_default_chip(void) return NULL; } #endif + +#ifdef CONFIG_OF +static inline int of_tpm_get_sml_parameters(struct device_node *np, + u64 *base, u32 *size) +{ + const u32 *sizep; + const u64 *basep; + + sizep = of_get_property(np, "linux,sml-size", NULL); + basep = of_get_property(np, "linux,sml-base", NULL); + if (sizep == NULL && basep == NULL) + return -ENODEV; + if (sizep == NULL || basep == NULL) + return -EIO; + + /* + * For both vtpm/tpm, firmware has log addr and log size in big + * endian format. But in case of vtpm, there is a method called + * sml-handover which is run during kernel init even before + * device tree is setup. This sml-handover function takes care + * of endianness and writes to sml-base and sml-size in little + * endian format. For this reason, vtpm doesn't need conversion + * but physical tpm needs the conversion. + */ + if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0 && + of_property_match_string(np, "compatible", "IBM,vtpm20") < 0) { + *size = be32_to_cpup((__force __be32 *)sizep); + *base = be64_to_cpup((__force __be64 *)basep); + } else { + *size = *sizep; + *base = *basep; + } + return 0; +} +#endif + #endif From patchwork Tue Apr 18 13:44:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 675330 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C6D28C77B76 for ; Tue, 18 Apr 2023 13:44:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232186AbjDRNo4 (ORCPT ); Tue, 18 Apr 2023 09:44:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232170AbjDRNon (ORCPT ); Tue, 18 Apr 2023 09:44:43 -0400 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3374EB762; Tue, 18 Apr 2023 06:44:37 -0700 (PDT) Received: from pps.filterd (m0353725.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 33IDZYqv023534; Tue, 18 Apr 2023 13:44:20 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=51cRZTu6UcwHcnS/YEaTbCAFxBFW4qP0HhtDLNdb/t8=; b=qARhLlWamqcSKP1ajprwbSEKilGJjFrq/fkD56J0IS+ttXYNUK9qHpkl8I/fA8Ynx3bW GzR6n7MkxFwUbP+YrJ4J8z7V2k1U7BfAo4A0jTgBocJjSwbTMqhgqdozX205hzSUfZNG CisApsTyjHDtid5HDDHPLb0e6EMswhYYSY7ccp7Bq/ktMtRwYj0uljFE2/lLYgxbq7RR /15WQuIIZGHrxGNSe/wGI8A/l/5XZGrxw4DbZ94j3G0AIV1TerZ3oX7mOaNWzty3gDZ2 l/2pSH/lP/CIRVR3TM5PIPTaTOzSx9kA3b0TUh5s+xguHw6lGOWnlC8/dX9OrRDIzlww SA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3q1ntu4ntq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Apr 2023 13:44:20 +0000 Received: from m0353725.ppops.net (m0353725.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 33IDf3UV013921; Tue, 18 Apr 2023 13:44:19 GMT Received: from ppma03dal.us.ibm.com (b.bd.3ea9.ip4.static.sl-reverse.com [169.62.189.11]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3q1ntu4nt7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Apr 2023 13:44:19 +0000 Received: from pps.filterd (ppma03dal.us.ibm.com [127.0.0.1]) by ppma03dal.us.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 33ID7nqm014077; Tue, 18 Apr 2023 13:44:18 GMT Received: from smtprelay07.wdc07v.mail.ibm.com ([9.208.129.116]) by ppma03dal.us.ibm.com (PPS) with ESMTPS id 3pykj75fnq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Apr 2023 13:44:18 +0000 Received: from smtpav01.wdc07v.mail.ibm.com (smtpav01.wdc07v.mail.ibm.com [10.39.53.228]) by smtprelay07.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 33IDiGZK16122518 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Apr 2023 13:44:16 GMT Received: from smtpav01.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2CEA05805B; Tue, 18 Apr 2023 13:44:16 +0000 (GMT) Received: from smtpav01.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D48E858066; Tue, 18 Apr 2023 13:44:14 +0000 (GMT) Received: from sbct-3.pok.ibm.com (unknown [9.47.158.153]) by smtpav01.wdc07v.mail.ibm.com (Postfix) with ESMTP; Tue, 18 Apr 2023 13:44:14 +0000 (GMT) From: Stefan Berger To: kexec@lists.infradead.org, devicetree@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Cc: nayna@linux.ibm.com, nasastry@in.ibm.com, mpe@ellerman.id.au, Stefan Berger , Rob Herring , Frank Rowand , Mimi Zohar , Rob Herring , Nageswara R Sastry , Coiby Xu Subject: [PATCH v9 3/4] of: kexec: Refactor IMA buffer related functions to make them reusable Date: Tue, 18 Apr 2023 09:44:08 -0400 Message-Id: <20230418134409.177485-4-stefanb@linux.ibm.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230418134409.177485-1-stefanb@linux.ibm.com> References: <20230418134409.177485-1-stefanb@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: Pwp111NWjBf_1uPsxdSi9TX58YRyvY5o X-Proofpoint-ORIG-GUID: qpJqY9r643D89DdQGiclnMF1hI8Argjt X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-04-18_09,2023-04-18_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxscore=0 impostorscore=0 spamscore=0 priorityscore=1501 phishscore=0 lowpriorityscore=0 adultscore=0 clxscore=1015 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303200000 definitions=main-2304180118 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Refactor IMA buffer related functions to make them reusable for carrying TPM logs across kexec. Signed-off-by: Stefan Berger Cc: Rob Herring Cc: Frank Rowand Cc: Mimi Zohar Reviewed-by: Mimi Zohar Reviewed-by: Rob Herring Tested-by: Nageswara R Sastry Tested-by: Coiby Xu --- v6: - Add __init to get_kexec_buffer as suggested by Jonathan v5: - Rebased on Jonathan McDowell's commit "b69a2afd5afc x86/kexec: Carry forward IMA measurement log on kexec" v4: - Move debug output into setup_buffer() --- drivers/of/kexec.c | 126 ++++++++++++++++++++++++++------------------- 1 file changed, 74 insertions(+), 52 deletions(-) diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c index 1373d7e0a9b3..fa8c0c75adf9 100644 --- a/drivers/of/kexec.c +++ b/drivers/of/kexec.c @@ -117,45 +117,57 @@ static int do_get_kexec_buffer(const void *prop, int len, unsigned long *addr, } #ifdef CONFIG_HAVE_IMA_KEXEC -/** - * ima_get_kexec_buffer - get IMA buffer from the previous kernel - * @addr: On successful return, set to point to the buffer contents. - * @size: On successful return, set to the buffer size. - * - * Return: 0 on success, negative errno on error. - */ -int __init ima_get_kexec_buffer(void **addr, size_t *size) +static int __init get_kexec_buffer(const char *name, unsigned long *addr, + size_t *size) { int ret, len; - unsigned long tmp_addr; unsigned long start_pfn, end_pfn; - size_t tmp_size; const void *prop; - prop = of_get_property(of_chosen, "linux,ima-kexec-buffer", &len); + prop = of_get_property(of_chosen, name, &len); if (!prop) return -ENOENT; - ret = do_get_kexec_buffer(prop, len, &tmp_addr, &tmp_size); + ret = do_get_kexec_buffer(prop, len, addr, size); if (ret) return ret; - /* Do some sanity on the returned size for the ima-kexec buffer */ - if (!tmp_size) + /* Do some sanity on the returned size for the kexec buffer */ + if (!*size) return -ENOENT; /* * Calculate the PFNs for the buffer and ensure * they are with in addressable memory. */ - start_pfn = PHYS_PFN(tmp_addr); - end_pfn = PHYS_PFN(tmp_addr + tmp_size - 1); + start_pfn = PHYS_PFN(*addr); + end_pfn = PHYS_PFN(*addr + *size - 1); if (!page_is_ram(start_pfn) || !page_is_ram(end_pfn)) { - pr_warn("IMA buffer at 0x%lx, size = 0x%zx beyond memory\n", - tmp_addr, tmp_size); + pr_warn("%s buffer at 0x%lx, size = 0x%zx beyond memory\n", + name, *addr, *size); return -EINVAL; } + return 0; +} + +/** + * ima_get_kexec_buffer - get IMA buffer from the previous kernel + * @addr: On successful return, set to point to the buffer contents. + * @size: On successful return, set to the buffer size. + * + * Return: 0 on success, negative errno on error. + */ +int __init ima_get_kexec_buffer(void **addr, size_t *size) +{ + int ret; + unsigned long tmp_addr; + size_t tmp_size; + + ret = get_kexec_buffer("linux,ima-kexec-buffer", &tmp_addr, &tmp_size); + if (ret) + return ret; + *addr = __va(tmp_addr); *size = tmp_size; @@ -188,72 +200,82 @@ int __init ima_free_kexec_buffer(void) } #endif -/** - * remove_ima_buffer - remove the IMA buffer property and reservation from @fdt - * - * @fdt: Flattened Device Tree to update - * @chosen_node: Offset to the chosen node in the device tree - * - * The IMA measurement buffer is of no use to a subsequent kernel, so we always - * remove it from the device tree. - */ -static void remove_ima_buffer(void *fdt, int chosen_node) +static int remove_buffer(void *fdt, int chosen_node, const char *name) { int ret, len; unsigned long addr; size_t size; const void *prop; - if (!IS_ENABLED(CONFIG_HAVE_IMA_KEXEC)) - return; - - prop = fdt_getprop(fdt, chosen_node, "linux,ima-kexec-buffer", &len); + prop = fdt_getprop(fdt, chosen_node, name, &len); if (!prop) - return; + return -ENOENT; ret = do_get_kexec_buffer(prop, len, &addr, &size); - fdt_delprop(fdt, chosen_node, "linux,ima-kexec-buffer"); + fdt_delprop(fdt, chosen_node, name); if (ret) - return; + return ret; ret = fdt_find_and_del_mem_rsv(fdt, addr, size); if (!ret) - pr_debug("Removed old IMA buffer reservation.\n"); + pr_debug("Remove old %s buffer reserveration", name); + return ret; } -#ifdef CONFIG_IMA_KEXEC /** - * setup_ima_buffer - add IMA buffer information to the fdt - * @image: kexec image being loaded. - * @fdt: Flattened device tree for the next kernel. - * @chosen_node: Offset to the chosen node. + * remove_ima_buffer - remove the IMA buffer property and reservation from @fdt * - * Return: 0 on success, or negative errno on error. + * @fdt: Flattened Device Tree to update + * @chosen_node: Offset to the chosen node in the device tree + * + * The IMA measurement buffer is of no use to a subsequent kernel, so we always + * remove it from the device tree. */ -static int setup_ima_buffer(const struct kimage *image, void *fdt, - int chosen_node) +static void remove_ima_buffer(void *fdt, int chosen_node) +{ + if (!IS_ENABLED(CONFIG_HAVE_IMA_KEXEC)) + return; + + remove_buffer(fdt, chosen_node, "linux,ima-kexec-buffer"); +} + +#ifdef CONFIG_IMA_KEXEC +static int setup_buffer(void *fdt, int chosen_node, const char *name, + phys_addr_t addr, size_t size) { int ret; - if (!image->ima_buffer_size) + if (!size) return 0; ret = fdt_appendprop_addrrange(fdt, 0, chosen_node, - "linux,ima-kexec-buffer", - image->ima_buffer_addr, - image->ima_buffer_size); + name, addr, size); if (ret < 0) return -EINVAL; - ret = fdt_add_mem_rsv(fdt, image->ima_buffer_addr, - image->ima_buffer_size); + ret = fdt_add_mem_rsv(fdt, addr, size); if (ret) return -EINVAL; - pr_debug("IMA buffer at 0x%pa, size = 0x%zx\n", - &image->ima_buffer_addr, image->ima_buffer_size); + pr_debug("%s at 0x%pa, size = 0x%zx\n", name, &addr, size); return 0; + +} + +/** + * setup_ima_buffer - add IMA buffer information to the fdt + * @image: kexec image being loaded. + * @fdt: Flattened device tree for the next kernel. + * @chosen_node: Offset to the chosen node. + * + * Return: 0 on success, or negative errno on error. + */ +static int setup_ima_buffer(const struct kimage *image, void *fdt, + int chosen_node) +{ + return setup_buffer(fdt, chosen_node, "linux,ima-kexec-buffer", + image->ima_buffer_addr, image->ima_buffer_size); } #else /* CONFIG_IMA_KEXEC */ static inline int setup_ima_buffer(const struct kimage *image, void *fdt, From patchwork Tue Apr 18 13:44:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 674667 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7DC9C77B78 for ; Tue, 18 Apr 2023 13:44:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231980AbjDRNoh (ORCPT ); Tue, 18 Apr 2023 09:44:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230463AbjDRNod (ORCPT ); Tue, 18 Apr 2023 09:44:33 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63F12125B9; Tue, 18 Apr 2023 06:44:31 -0700 (PDT) Received: from pps.filterd (m0353728.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 33ID6PKd017035; Tue, 18 Apr 2023 13:44:21 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=JrXV5WpLPfISz/ogVFmLv6FO9GD/Bx08dWDfErkGGhI=; b=TkVdMp3lPDW1ytfhLP8D1HP44CcwNyioIWekd/mOsM7c/XL3EsucrE+9LctIODJHqXe6 jxIO/e4o++7L/cc0uRZvOsO7FIiLnrBEyccWTbygiK3yV6stovlUBLcE6zS6DJDtMJlg I1z9w0FQTobKMX98zKPQNYWNxdkMuZIt4ais+N4e/7VcpCJGOerrP6nhqERVqKhVVNGq hBQl/ek4YHwo21UpbvGxiDofvjtkmJaxOubvlGYQMLER74jbq50itumRyd+G8qtKh2yr 42TSwY2YfbyefO1CDdAeK3ZIW9we8VQZjuv48HnMt0SlWUiui23/p/NUyZ0L7uwD3o3e Hg== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3q1pww297p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Apr 2023 13:44:21 +0000 Received: from m0353728.ppops.net (m0353728.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 33IDZs6m014354; Tue, 18 Apr 2023 13:44:20 GMT Received: from ppma03dal.us.ibm.com (b.bd.3ea9.ip4.static.sl-reverse.com [169.62.189.11]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3q1pww296t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Apr 2023 13:44:20 +0000 Received: from pps.filterd (ppma03dal.us.ibm.com [127.0.0.1]) by ppma03dal.us.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 33ID7QWb013965; Tue, 18 Apr 2023 13:44:19 GMT Received: from smtprelay02.dal12v.mail.ibm.com ([9.208.130.97]) by ppma03dal.us.ibm.com (PPS) with ESMTPS id 3pykj75fns-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 18 Apr 2023 13:44:19 +0000 Received: from smtpav01.wdc07v.mail.ibm.com (smtpav01.wdc07v.mail.ibm.com [10.39.53.228]) by smtprelay02.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 33IDiHJD29163800 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 18 Apr 2023 13:44:18 GMT Received: from smtpav01.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A0EBB58063; Tue, 18 Apr 2023 13:44:17 +0000 (GMT) Received: from smtpav01.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5560058059; Tue, 18 Apr 2023 13:44:16 +0000 (GMT) Received: from sbct-3.pok.ibm.com (unknown [9.47.158.153]) by smtpav01.wdc07v.mail.ibm.com (Postfix) with ESMTP; Tue, 18 Apr 2023 13:44:16 +0000 (GMT) From: Stefan Berger To: kexec@lists.infradead.org, devicetree@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Cc: nayna@linux.ibm.com, nasastry@in.ibm.com, mpe@ellerman.id.au, Stefan Berger , Rob Herring , Frank Rowand , Eric Biederman , Nageswara R Sastry , Coiby Xu , Rob Herring Subject: [PATCH v9 4/4] tpm/kexec: Duplicate TPM measurement log in of-tree for kexec Date: Tue, 18 Apr 2023 09:44:09 -0400 Message-Id: <20230418134409.177485-5-stefanb@linux.ibm.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230418134409.177485-1-stefanb@linux.ibm.com> References: <20230418134409.177485-1-stefanb@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: yAqmBtBNnpGH6og9BhDxa-knZIwuBfXv X-Proofpoint-ORIG-GUID: VRbGQ6LqH1ZDbze7tFHYSq1iO0wdPNM7 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-04-18_09,2023-04-18_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 malwarescore=0 impostorscore=0 adultscore=0 lowpriorityscore=0 mlxlogscore=999 bulkscore=0 clxscore=1011 priorityscore=1501 mlxscore=0 spamscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303200000 definitions=main-2304180118 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org The memory area of the TPM measurement log is currently not properly duplicated for carrying it across kexec when an Open Firmware Devicetree is used. Therefore, the contents of the log get corrupted. Fix this for the kexec_file_load() syscall by allocating a buffer and copying the contents of the existing log into it. The new buffer is preserved across the kexec and a pointer to it is available when the new kernel is started. To achieve this, store the allocated buffer's address in the flattened device tree (fdt) under the name linux,tpm-kexec-buffer and search for this entry early in the kernel startup before the TPM subsystem starts up. Adjust the pointer in the of-tree stored under linux,sml-base to point to this buffer holding the preserved log. The TPM driver can then read the base address from this entry when making the log available. Invalidate the log by removing 'linux,sml-base' from the devicetree if anything goes wrong with updating the buffer. Use subsys_initcall() to call the function to restore the buffer even if the TPM subsystem or driver are not used. This allows the buffer to be carried across the next kexec without involvement of the TPM subsystem and ensures a valid buffer pointed to by the of-tree. Use the subsys_initcall(), rather than an ealier initcall, since page_is_ram() in get_kexec_buffer() only starts working at this stage. Signed-off-by: Stefan Berger Cc: Rob Herring Cc: Frank Rowand Cc: Eric Biederman Tested-by: Nageswara R Sastry Tested-by: Coiby Xu Reviewed-by: Rob Herring --- v6: - Define prototype for tpm_add_kexec_buffer under same config options as drivers/of/kexec.c is compiled, provide inline function otherwise. (kernel test robot) v4: - Added #include due to parisc - Use phys_addr_t for physical address rather than void * - Remove linux,sml-base if the buffer cannot be updated after a kexec - Added __init to functions where possible --- drivers/of/kexec.c | 216 +++++++++++++++++++++++++++++++++++++++++- include/linux/kexec.h | 6 ++ include/linux/of.h | 6 ++ kernel/kexec_file.c | 6 ++ 4 files changed, 232 insertions(+), 2 deletions(-) diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c index fa8c0c75adf9..9831d25dd83e 100644 --- a/drivers/of/kexec.c +++ b/drivers/of/kexec.c @@ -19,6 +19,8 @@ #include #include #include +#include +#include #define RNG_SEED_SIZE 128 @@ -116,7 +118,6 @@ static int do_get_kexec_buffer(const void *prop, int len, unsigned long *addr, return 0; } -#ifdef CONFIG_HAVE_IMA_KEXEC static int __init get_kexec_buffer(const char *name, unsigned long *addr, size_t *size) { @@ -151,6 +152,7 @@ static int __init get_kexec_buffer(const char *name, unsigned long *addr, return 0; } +#ifdef CONFIG_HAVE_IMA_KEXEC /** * ima_get_kexec_buffer - get IMA buffer from the previous kernel * @addr: On successful return, set to point to the buffer contents. @@ -239,7 +241,6 @@ static void remove_ima_buffer(void *fdt, int chosen_node) remove_buffer(fdt, chosen_node, "linux,ima-kexec-buffer"); } -#ifdef CONFIG_IMA_KEXEC static int setup_buffer(void *fdt, int chosen_node, const char *name, phys_addr_t addr, size_t size) { @@ -263,6 +264,7 @@ static int setup_buffer(void *fdt, int chosen_node, const char *name, } +#ifdef CONFIG_IMA_KEXEC /** * setup_ima_buffer - add IMA buffer information to the fdt * @image: kexec image being loaded. @@ -285,6 +287,213 @@ static inline int setup_ima_buffer(const struct kimage *image, void *fdt, } #endif /* CONFIG_IMA_KEXEC */ +/** + * tpm_get_kexec_buffer - get TPM log buffer from the previous kernel + * @phyaddr: On successful return, set to physical address of buffer + * @size: On successful return, set to the buffer size. + * + * Return: 0 on success, negative errno on error. + */ +static int __init tpm_get_kexec_buffer(phys_addr_t *phyaddr, size_t *size) +{ + unsigned long tmp_addr; + size_t tmp_size; + int ret; + + ret = get_kexec_buffer("linux,tpm-kexec-buffer", &tmp_addr, &tmp_size); + if (ret) + return ret; + + *phyaddr = (phys_addr_t)tmp_addr; + *size = tmp_size; + + return 0; +} + +/** + * tpm_of_remove_kexec_buffer - remove the linux,tpm-kexec-buffer node + */ +static int __init tpm_of_remove_kexec_buffer(void) +{ + struct property *prop; + + prop = of_find_property(of_chosen, "linux,tpm-kexec-buffer", NULL); + if (!prop) + return -ENOENT; + + return of_remove_property(of_chosen, prop); +} + +/** + * remove_tpm_buffer - remove the TPM log buffer property and reservation from @fdt + * + * @fdt: Flattened Device Tree to update + * @chosen_node: Offset to the chosen node in the device tree + * + * The TPM log measurement buffer is of no use to a subsequent kernel, so we always + * remove it from the device tree. + */ +static void remove_tpm_buffer(void *fdt, int chosen_node) +{ + if (!IS_ENABLED(CONFIG_PPC64)) + return; + + remove_buffer(fdt, chosen_node, "linux,tpm-kexec-buffer"); +} + +/** + * setup_tpm_buffer - add TPM measurement log buffer information to the fdt + * @image: kexec image being loaded. + * @fdt: Flattened device tree for the next kernel. + * @chosen_node: Offset to the chosen node. + * + * Return: 0 on success, or negative errno on error. + */ +static int setup_tpm_buffer(const struct kimage *image, void *fdt, + int chosen_node) +{ + if (!IS_ENABLED(CONFIG_PPC64)) + return 0; + + return setup_buffer(fdt, chosen_node, "linux,tpm-kexec-buffer", + image->tpm_buffer_addr, image->tpm_buffer_size); +} + +void tpm_add_kexec_buffer(struct kimage *image) +{ + struct kexec_buf kbuf = { .image = image, .buf_align = 1, + .buf_min = 0, .buf_max = ULONG_MAX, + .top_down = true }; + struct device_node *np; + void *buffer; + u32 size; + u64 base; + int ret; + + if (!IS_ENABLED(CONFIG_PPC64)) + return; + + np = of_find_node_by_name(NULL, "vtpm"); + if (!np) + return; + + if (of_tpm_get_sml_parameters(np, &base, &size) < 0) + return; + + buffer = vmalloc(size); + if (!buffer) + return; + memcpy(buffer, __va(base), size); + + kbuf.buffer = buffer; + kbuf.bufsz = size; + kbuf.memsz = size; + ret = kexec_add_buffer(&kbuf); + if (ret) { + pr_err("Error passing over kexec TPM measurement log buffer: %d\n", + ret); + return; + } + + image->tpm_buffer = buffer; + image->tpm_buffer_addr = kbuf.mem; + image->tpm_buffer_size = size; +} + +/** + * tpm_post_kexec - Make stored TPM log buffer available in of-tree + */ +static int __init tpm_post_kexec(void) +{ + struct property *newprop, *p; + struct device_node *np; + phys_addr_t phyaddr; + u32 oflogsize; + size_t size; + u64 unused; + int ret; + + if (!IS_ENABLED(CONFIG_PPC64)) + return 0; + + np = of_find_node_by_name(NULL, "vtpm"); + if (!np) + return 0; + + if (!of_get_property(of_chosen, "linux,tpm-kexec-buffer", NULL)) { + /* + * linux,tpm-kexec-buffer may be missing on initial boot + * or if previous kernel didn't pass a buffer. + */ + if (of_get_property(of_chosen, "linux,booted-from-kexec", NULL)) { + /* no buffer but kexec'd: remove 'linux,sml-base' */ + ret = -EINVAL; + goto err_remove_sml_base; + } + return 0; + } + + /* + * If any one of the following steps fails we remove linux,sml-base + * to invalidate the TPM log. + */ + ret = tpm_get_kexec_buffer(&phyaddr, &size); + if (ret) + goto err_remove_kexec_buffer; + + /* logsize must not have changed */ + ret = of_tpm_get_sml_parameters(np, &unused, &oflogsize); + if (ret < 0) + goto err_free_memblock; + ret = -EINVAL; + if (oflogsize != size) + goto err_free_memblock; + + /* replace linux,sml-base with new physical address of buffer */ + ret = -ENOMEM; + newprop = kzalloc(sizeof(*newprop), GFP_KERNEL); + if (!newprop) + goto err_free_memblock; + + newprop->name = kstrdup("linux,sml-base", GFP_KERNEL); + newprop->length = sizeof(phyaddr); + newprop->value = kmalloc(sizeof(phyaddr), GFP_KERNEL); + if (!newprop->name || !newprop->value) + goto err_free_newprop_struct; + + if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0 && + of_property_match_string(np, "compatible", "IBM,vtpm20") < 0) { + ret = -ENODEV; + goto err_free_newprop_struct; + } else { + *(phys_addr_t *)newprop->value = phyaddr; + } + + ret = of_update_property(np, newprop); + if (ret) { + pr_err("Could not update linux,sml-base with new address"); + goto err_free_newprop_struct; + } + + return 0; + +err_free_newprop_struct: + kfree(newprop->value); + kfree(newprop->name); + kfree(newprop); +err_free_memblock: + memblock_phys_free((phys_addr_t)phyaddr, size); +err_remove_kexec_buffer: + tpm_of_remove_kexec_buffer(); +err_remove_sml_base: + p = of_find_property(np, "linux,sml-base", NULL); + if (p) + of_remove_property(np, p); + + return ret; +} +subsys_initcall(tpm_post_kexec); + /* * of_kexec_alloc_and_setup_fdt - Alloc and setup a new Flattened Device Tree * @@ -483,6 +692,9 @@ void *of_kexec_alloc_and_setup_fdt(const struct kimage *image, remove_ima_buffer(fdt, chosen_node); ret = setup_ima_buffer(image, fdt, fdt_path_offset(fdt, "/chosen")); + remove_tpm_buffer(fdt, chosen_node); + ret = setup_tpm_buffer(image, fdt, fdt_path_offset(fdt, "/chosen")); + out: if (ret) { kvfree(fdt); diff --git a/include/linux/kexec.h b/include/linux/kexec.h index 6883c5922701..6116abdda590 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -380,6 +380,12 @@ struct kimage { void *elf_headers; unsigned long elf_headers_sz; unsigned long elf_load_addr; + + /* Virtual address of TPM log buffer for kexec syscall */ + void *tpm_buffer; + + phys_addr_t tpm_buffer_addr; + size_t tpm_buffer_size; }; /* kexec interface functions */ diff --git a/include/linux/of.h b/include/linux/of.h index 0af611307db2..9afa99950310 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -1658,4 +1658,10 @@ static inline int of_overlay_notifier_unregister(struct notifier_block *nb) #endif +#if defined(CONFIG_KEXEC_FILE) && defined(CONFIG_OF_FLATTREE) +void tpm_add_kexec_buffer(struct kimage *image); +#else +static inline void tpm_add_kexec_buffer(struct kimage *image) { } +#endif + #endif /* _LINUX_OF_H */ diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index f1a0e4e3fb5c..58c7aaf11883 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "kexec_internal.h" #ifdef CONFIG_KEXEC_SIG @@ -113,6 +114,9 @@ void kimage_file_post_load_cleanup(struct kimage *image) image->ima_buffer = NULL; #endif /* CONFIG_IMA_KEXEC */ + vfree(image->tpm_buffer); + image->tpm_buffer = NULL; + /* See if architecture has anything to cleanup post load */ arch_kimage_file_post_load_cleanup(image); @@ -248,6 +252,8 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd, /* IMA needs to pass the measurement list to the next kernel. */ ima_add_kexec_buffer(image); + /* Pass the TPM measurement log to next kernel */ + tpm_add_kexec_buffer(image); /* Call arch image load handlers */ ldata = arch_kexec_kernel_image_load(image);