From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.6 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EE3FAC433E0 for ; Fri, 26 Feb 2021 21:51:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 986BE64F22 for ; Fri, 26 Feb 2021 21:51:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229698AbhBZVvx (ORCPT ); Fri, 26 Feb 2021 16:51:53 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:44442 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229622AbhBZVvw (ORCPT ); Fri, 26 Feb 2021 16:51:52 -0500 Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 11QLinKd138580; Fri, 26 Feb 2021 16:51:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=subject : from : to : cc : references : message-id : date : mime-version : in-reply-to : content-type : content-transfer-encoding; s=pp1; bh=rGYxE2dB9tPqpjreuNZchKVBV+FyHd+TBX9en/5R1Ec=; b=XmGCO78lysdAXVMdwa8GvX6reG7WQRkX4d3d7ntCQJJrToPO8scsLWifTGdfUBsE9k8U IHL5KHgyHMPPcq0ArOgyxyo8VuPdRRZpLlc/NIucF++EA+zEIR0OvhSmtj9YpKCE6YTD g/WgWkX0PCwUqUyf7cGqSZQJcISiLlRjq79tNWQ/SMGYoWaz2Fs6RSf8mafSDJvW4X+4 yc2l20qeXW3mqUSkGJAIgDRh/QVbWn2zMJDD9rLvHBLBknAltBQUI4zxb+Buxe2WZbg9 weZurwsN3bnNYqFitbj0V88FJHCsFsRvbr0P8obCIAJRmboF97h7BXdmoP4WgYzqVJt1 6Q== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 36y95tg4tk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 26 Feb 2021 16:51:09 -0500 Received: from m0098404.ppops.net (m0098404.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 11QLjKsU146500; Fri, 26 Feb 2021 16:51:08 -0500 Received: from ppma01dal.us.ibm.com (83.d6.3fa9.ip4.static.sl-reverse.com [169.63.214.131]) by mx0a-001b2d01.pphosted.com with ESMTP id 36y95tg4tc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 26 Feb 2021 16:51:08 -0500 Received: from pps.filterd (ppma01dal.us.ibm.com [127.0.0.1]) by ppma01dal.us.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 11QLhY68023261; Fri, 26 Feb 2021 21:51:07 GMT Received: from b01cxnp22036.gho.pok.ibm.com (b01cxnp22036.gho.pok.ibm.com [9.57.198.26]) by ppma01dal.us.ibm.com with ESMTP id 36tt2ap73b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 26 Feb 2021 21:51:07 +0000 Received: from b01ledav006.gho.pok.ibm.com (b01ledav006.gho.pok.ibm.com [9.57.199.111]) by b01cxnp22036.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 11QLp7dB6095668 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 26 Feb 2021 21:51:07 GMT Received: from b01ledav006.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id ED9C4AC060; Fri, 26 Feb 2021 21:51:06 +0000 (GMT) Received: from b01ledav006.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C9A0FAC05B; Fri, 26 Feb 2021 21:51:06 +0000 (GMT) Received: from sbct-3.pok.ibm.com (unknown [9.47.158.153]) by b01ledav006.gho.pok.ibm.com (Postfix) with ESMTP; Fri, 26 Feb 2021 21:51:06 +0000 (GMT) Subject: Re: [PATCH 3/3] integrity: Load keys from TPM NV onto IMA keyring From: Stefan Berger To: Patrick Uiterwijk , peterhuewe@gmx.de, jarkko@kernel.org, jgg@ziepe.ca, zohar@linux.ibm.com, dmitry.kasatkin@gmail.com, linux-integrity@vger.kernel.org Cc: pbrobinson@gmail.com, kgold@linux.ibm.com References: <20210225203229.363302-1-patrick@puiterwijk.org> <20210225203229.363302-4-patrick@puiterwijk.org> <4dcd8fe8-632a-a60b-e502-2185f61529f3@linux.ibm.com> Message-ID: Date: Fri, 26 Feb 2021 16:51:06 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: <4dcd8fe8-632a-a60b-e502-2185f61529f3@linux.ibm.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.369,18.0.761 definitions=2021-02-26_09:2021-02-26,2021-02-26 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 malwarescore=0 phishscore=0 suspectscore=0 mlxscore=0 impostorscore=0 spamscore=0 adultscore=0 mlxlogscore=999 bulkscore=0 clxscore=1015 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2102260159 Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org On 2/26/21 4:47 PM, Stefan Berger wrote: > On 2/25/21 3:32 PM, Patrick Uiterwijk wrote: >> Allows users to enroll their own public key stored in a specific TPM2 >> NV Index, requiring the absence of the Platform Create and Platform >> Write attributes on the NV Index, to be loaded on the IMA keyring. >> >> Provides a method for users to load keys without the need to recompile >> the kernel or change the kernel binary, which would require a resign of >> the kernel image. >> >> Signed-off-by: Patrick Uiterwijk >> --- >>   security/integrity/ima/Kconfig    | 22 +++++++++++++ >>   security/integrity/ima/ima_init.c | 53 +++++++++++++++++++++++++++++++ >>   2 files changed, 75 insertions(+) >> >> diff --git a/security/integrity/ima/Kconfig >> b/security/integrity/ima/Kconfig >> index 12e9250c1bec..28424b930c81 100644 >> --- a/security/integrity/ima/Kconfig >> +++ b/security/integrity/ima/Kconfig >> @@ -291,6 +291,28 @@ config IMA_BLACKLIST_KEYRING >>          the search is successful the requested operation is rejected >> and >>          an error is returned to the caller. >>   +config IMA_LOAD_CERT_NVINDEX >> +    bool "Load certificate from TPM nvindex into '.ima' trusted >> keyring" >> +    depends on IMA_TRUSTED_KEYRING && TCG_TPM >> +    default n >> +    help >> +       File signature verification is based on the public keys >> +       loaded on the .ima trusted keyring. These public keys are >> +       X509 certificates signed by a trusted key on the >> +       .system keyring.  This option enables X509 certificate >> +       loading by the kernel onto the '.ima' trusted keyring >> +       from a TPM nvindex, bypassing the builtin keyring check. >> + >> +config IMA_LOAD_CERT_NVINDEX_INDEX >> +    hex "The TPM NV Index to load into the '.ima' trusted keyring" >> +    depends on IMA_LOAD_CERT_NVINDEX >> +    default 0x184b520 >> +    help >> +       Defines the index of the NV Index that gets loaded into the >> +       '.ima' keyring. >> +       The default is the "0x18" prefix for a non-TCG specified NV >> Index, >> +       suffixed with ASCII for "KR" (keyring) and then 0 >> + >>   config IMA_LOAD_X509 >>       bool "Load X509 certificate onto the '.ima' trusted keyring" >>       depends on IMA_TRUSTED_KEYRING >> diff --git a/security/integrity/ima/ima_init.c >> b/security/integrity/ima/ima_init.c >> index 6e8742916d1d..ea0949e8df12 100644 >> --- a/security/integrity/ima/ima_init.c >> +++ b/security/integrity/ima/ima_init.c >> @@ -112,6 +112,55 @@ void __init ima_load_x509(void) >>   } >>   #endif >>   +#ifndef CONFIG_IMA_LOAD_CERT_NVINDEX >> +int __init ima_load_key_nvindex(void) >> +{ >> +    return 0; >> +} >> +#else >> +int __init ima_load_key_nvindex(void) >> +{ >> +    void *cert_buffer; >> +    int rc; >> +    key_perm_t perm; >> +    u32 nvindex_attributes = 0; >> + >> +    rc = tpm_nv_read(tpm_default_chip(), > > > You should do chip = tpm_default_chip() so that later on you can do > put_device(&chip->dev). ... or just use ima_tpm_chip if != NULL: https://elixir.bootlin.com/linux/latest/source/security/integrity/ima/ima_init.c#L23 > > >> + CONFIG_IMA_LOAD_CERT_NVINDEX_INDEX, >> +                &nvindex_attributes, &cert_buffer); >> +    if (rc < 0) { >> +        if (rc == -ENODEV)  /* No TPM2 */ >> +            rc = 0; >> +        if (rc == -ENOENT)  /* No certificate in NV Index */ >> +            rc = 0; >> +        goto out; >> +    } >> + >> +    pr_info("Loading IMA key from TPM NV Index 0x%x", >> CONFIG_IMA_LOAD_CERT_NVINDEX_INDEX); >> + >> +    if (nvindex_attributes & TPM2_ATTR_NV_PLATFORMCREATE) { >> +        pr_err("NV Index has the Platform Create attribute"); >> +        rc = -EACCES; >> +        goto out_free; >> +    } >> +    if (nvindex_attributes & TPM2_ATTR_NV_PPWRITE) { >> +        pr_err("NV Index has the Platform Write attribute"); >> +        rc = -EACCES; >> +        goto out_free; >> +    } >> + >> +    perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW | >> KEY_USR_READ; >> +    rc = integrity_load_cert(INTEGRITY_KEYRING_IMA, "TPM NV Index", >> +                 cert_buffer, rc, perm, >> +                 KEY_ALLOC_BYPASS_RESTRICTION); >> + >> +out_free: >> +    kvfree(cert_buffer); > > > kfree? > > >> +out: >> +    return rc; >> +} >> +#endif >> + >>   int __init ima_init(void) >>   { >>       int rc; >> @@ -124,6 +173,10 @@ int __init ima_init(void) >>       if (rc) >>           return rc; >>   +    rc = ima_load_key_nvindex(); >> +    if (rc) >> +        pr_info("Failed to load IMA key from TPM NV Index (%d)", rc); >> + >>       rc = ima_init_crypto(); >>       if (rc) >>           return rc;