Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1345734rwb; Tue, 27 Sep 2022 11:41:21 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4EsGwwFM2wvnTbwfrNkOsBHltf7wcKxAao/Xjxv88VxVmDu6wkdW4Xp8XMNvsqCd8i6JZj X-Received: by 2002:a17:903:245:b0:178:e0ba:e507 with SMTP id j5-20020a170903024500b00178e0bae507mr29462873plh.115.1664304080957; Tue, 27 Sep 2022 11:41:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664304080; cv=none; d=google.com; s=arc-20160816; b=FSkuee3eorzNekljWuWvA99Bekmr+//a2hzTr//rOKZBBPnE8NIF+PwYScCymhoY7n uhcJ7kTplBkyXdgYBSrRfCrsz3FAdXWyF2lYZPKV+CAyczHV+8AWqS3rqcai8P8Blf/C 2OK9r5yFPJXOS3t+6/p74p/J7iKKJh/hCAeISnbcYdBbXJQL6lZICSD2b2WTV2Sm5eV/ N7q/1dGyu19ZSobnc8bxQZm+3SyBsHhBH36bgXNYdAhfgo7JQw9R4qfN+c8QTYjmhsOK IEo0ZIEZ7KBUx7Bs3H1k+dfgFUq5RajfvX58llCjKZ0AdD0lWeTDEQMECJz7OcbYGERk ZauA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JqdilZ5JCg39u52hx5ok1CZxfYUZFtYxLXmj/CVzrSU=; b=Tu4IRycK//EOYVtTNI/04FeDg6nMmlEZzG6AScTRukjOuAmYDav8joF3k3QV2doS44 mY8FjhXlNegEtgp9U6M8NacWvNdHDcP/aniDBuO7KFa45gjffxcwRRzPNSFSRHpZrqcF eS3V6/cI5IymQjoxmGbGbT726c/BVNZkzB2y/yQi8sB1yw+Je9D893IDk+6GD08iznCJ X8OEiljBWZpk0WkIsGIU3p9hv3bwKNXZfka7N4P4XGjg12kTYbDskwxqMJYYetUxg+lK Cyak60PrTWSVNpvDSR7PN3BJ1clpOCioWauntO2pQnxnUbrJQ/vUrtJoAF0rXPxjFHiL M1Fg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=eDGNIqCg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y64-20020a633243000000b00434c99c5359si2517009pgy.754.2022.09.27.11.41.09; Tue, 27 Sep 2022 11:41:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=eDGNIqCg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232896AbiI0QuS (ORCPT + 99 others); Tue, 27 Sep 2022 12:50:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233093AbiI0Qty (ORCPT ); Tue, 27 Sep 2022 12:49:54 -0400 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AEADD2BC0 for ; Tue, 27 Sep 2022 09:49:52 -0700 (PDT) Received: by mail-pf1-x42e.google.com with SMTP id d82so10174349pfd.10 for ; Tue, 27 Sep 2022 09:49:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=JqdilZ5JCg39u52hx5ok1CZxfYUZFtYxLXmj/CVzrSU=; b=eDGNIqCgnrhWjr+PAUC1lfpa1BhvlK7XAuMKA5dwNGoo7dhHVfE+pUcDy85KVIt/EF +oUYvGjEXoTVD/bBSQWk/JlMVOJRaY/xA6v0edGx2yYYiNKwMsckkGIxMfzEqjYEOunS ltInPjFobhSAKdqwXylUVTd2bWljzdj4GmRaw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=JqdilZ5JCg39u52hx5ok1CZxfYUZFtYxLXmj/CVzrSU=; b=jndciYHg3gms0F1C/CLuqFX96gS4Wu7LIq6RlekFn/pJhQi8EiNArF8oQh1Wt/Jzw/ p2rI7PgYiRpk/kEmC4KZaJYTMd0JI+uAWzfe/m+oykb02rnUNCCyxrrBh6IV83KsaJHw SoxID5Yi3jTx+hFaKmXsf7Y3qn4r3uLp6UsP4JkI6l/fD7iyM3FYyf7i75AIQP5akJ0p OJqbvDdceKU5/0ONykjQe/0dR7d9CFHohBeF1SO33ZK55NfoU+Pfe31K8Fyx3b63eJax THOzo0EVgwGXHmiEWeCWi4Dd/inkkukfwfdtUThZrJ8KY8QOyYu8wXCyZboCe8GOW+fK boow== X-Gm-Message-State: ACrzQf1K3vuVZzoADDUip1/zqR0PspvmXPtJdUqCwu5hozhiFBikfX9H oaF31WlbEH/z8KVnuNoWIoCI6jER1dBs9yTX2hE= X-Received: by 2002:a63:1954:0:b0:438:79da:7849 with SMTP id 20-20020a631954000000b0043879da7849mr24709298pgz.527.1664297380346; Tue, 27 Sep 2022 09:49:40 -0700 (PDT) Received: from evgreen-glaptop.lan ([73.231.74.141]) by smtp.gmail.com with ESMTPSA id p13-20020a63950d000000b00434272fe870sm1753509pgd.88.2022.09.27.09.49.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Sep 2022 09:49:39 -0700 (PDT) From: Evan Green To: linux-kernel@vger.kernel.org Cc: linux-integrity@vger.kernel.org, apronin@chromium.org, dlunev@google.com, jarkko@kernel.org, Pavel Machek , Ben Boeckel , rjw@rjwysocki.net, corbet@lwn.net, linux-pm@vger.kernel.org, zohar@linux.ibm.com, Kees Cook , Eric Biggers , jejb@linux.ibm.com, gwendal@chromium.org, Matthew Garrett , Matthew Garrett , Matthew Garrett , Evan Green , Hao Wu , Jason Gunthorpe , Peter Huewe , axelj Subject: [PATCH v3 01/11] tpm: Add support for in-kernel resetting of PCRs Date: Tue, 27 Sep 2022 09:49:12 -0700 Message-Id: <20220927094559.v3.1.I776854f47e3340cc2913ed4d8ecdd328048b73c3@changeid> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20220927164922.3383711-1-evgreen@chromium.org> References: <20220927164922.3383711-1-evgreen@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Matthew Garrett Add an internal command for resetting a PCR. This will be used by the encrypted hibernation code to set PCR23 to a known value. The hibernation code will seal the hibernation key with a policy specifying PCR23 be set to this known value as a mechanism to ensure that the hibernation key is genuine. But to do this repeatedly, resetting the PCR is necessary as well. Link: https://lore.kernel.org/lkml/20210220013255.1083202-2-matthewgarrett@google.com/ Signed-off-by: Matthew Garrett Signed-off-by: Evan Green --- Changes in v3: - Unify tpm1/2_pcr_reset prototypes (Jarkko) - Wait no, remove the TPM1 stuff altogether (Jarkko) - Remove extra From tag and blank in commit msg (Jarkko). drivers/char/tpm/tpm-interface.c | 25 ++++++++++++++++++++++ drivers/char/tpm/tpm.h | 1 + drivers/char/tpm/tpm2-cmd.c | 36 ++++++++++++++++++++++++++++++++ include/linux/tpm.h | 7 +++++++ 4 files changed, 69 insertions(+) diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c index 1621ce8187052c..2ac9079860b1e0 100644 --- a/drivers/char/tpm/tpm-interface.c +++ b/drivers/char/tpm/tpm-interface.c @@ -342,6 +342,31 @@ int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, } EXPORT_SYMBOL_GPL(tpm_pcr_extend); +/** + * tpm_pcr_reset - reset the specified PCR + * @chip: a &struct tpm_chip instance, %NULL for the default chip + * @pcr_idx: the PCR to be reset + * + * Return: same as with tpm_transmit_cmd(), or ENOTTY for TPM1 devices. + */ +int tpm_pcr_reset(struct tpm_chip *chip, u32 pcr_idx) +{ + int rc; + + chip = tpm_find_get_ops(chip); + if (!chip) + return -ENODEV; + + if (chip->flags & TPM_CHIP_FLAG_TPM2) + rc = tpm2_pcr_reset(chip, pcr_idx); + else + rc = -ENOTTY; + + tpm_put_ops(chip); + return rc; +} +EXPORT_SYMBOL_GPL(tpm_pcr_reset); + /** * tpm_send - send a TPM command * @chip: a &struct tpm_chip instance, %NULL for the default chip diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h index 24ee4e1cc452a0..34e20b3192f833 100644 --- a/drivers/char/tpm/tpm.h +++ b/drivers/char/tpm/tpm.h @@ -217,6 +217,7 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, struct tpm_digest *digest, u16 *digest_size_ptr); int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, struct tpm_digest *digests); +int tpm2_pcr_reset(struct tpm_chip *chip, u32 pcr_idx); int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max); ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value, const char *desc); diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c index 65d03867e114c5..69126a6770386e 100644 --- a/drivers/char/tpm/tpm2-cmd.c +++ b/drivers/char/tpm/tpm2-cmd.c @@ -269,6 +269,42 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, return rc; } +/** + * tpm2_pcr_reset() - reset a PCR + * + * @chip: TPM chip to use. + * @pcr_idx: index of the PCR. + * + * Return: Same as with tpm_transmit_cmd. + */ +int tpm2_pcr_reset(struct tpm_chip *chip, u32 pcr_idx) +{ + struct tpm_buf buf; + struct tpm2_null_auth_area auth_area; + int rc; + + rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_RESET); + if (rc) + return rc; + + tpm_buf_append_u32(&buf, pcr_idx); + + auth_area.handle = cpu_to_be32(TPM2_RS_PW); + auth_area.nonce_size = 0; + auth_area.attributes = 0; + auth_area.auth_size = 0; + + tpm_buf_append_u32(&buf, sizeof(struct tpm2_null_auth_area)); + tpm_buf_append(&buf, (const unsigned char *)&auth_area, + sizeof(auth_area)); + + rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to reset a PCR"); + + tpm_buf_destroy(&buf); + + return rc; +} + struct tpm2_get_random_out { __be16 size; u8 buffer[TPM_MAX_RNG_DATA]; diff --git a/include/linux/tpm.h b/include/linux/tpm.h index dfeb25a0362dee..8320cbac6f4009 100644 --- a/include/linux/tpm.h +++ b/include/linux/tpm.h @@ -219,6 +219,7 @@ enum tpm2_command_codes { TPM2_CC_HIERARCHY_CONTROL = 0x0121, TPM2_CC_HIERARCHY_CHANGE_AUTH = 0x0129, TPM2_CC_CREATE_PRIMARY = 0x0131, + TPM2_CC_PCR_RESET = 0x013D, TPM2_CC_SEQUENCE_COMPLETE = 0x013E, TPM2_CC_SELF_TEST = 0x0143, TPM2_CC_STARTUP = 0x0144, @@ -423,6 +424,7 @@ extern ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf, size_t min_rsp_body_length, const char *desc); extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, struct tpm_digest *digest); +extern int tpm_pcr_reset(struct tpm_chip *chip, u32 pcr_idx); extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, struct tpm_digest *digests); extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen); @@ -440,6 +442,11 @@ static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, return -ENODEV; } +static inline int tpm_pcr_reset(struct tpm_chip *chip, int pcr_idx) +{ + return -ENODEV; +} + static inline int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, struct tpm_digest *digests) { -- 2.31.0