From patchwork Mon Mar 5 16:56:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 10259331 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 44D8860211 for ; Mon, 5 Mar 2018 16:56:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 343F828B1B for ; Mon, 5 Mar 2018 16:56:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 28EF928B67; Mon, 5 Mar 2018 16:56:58 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B878628B1D for ; Mon, 5 Mar 2018 16:56:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752781AbeCEQ4z (ORCPT ); Mon, 5 Mar 2018 11:56:55 -0500 Received: from mga03.intel.com ([134.134.136.65]:9455 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752586AbeCEQ4t (ORCPT ); Mon, 5 Mar 2018 11:56:49 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 05 Mar 2018 08:56:48 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.47,427,1515484800"; d="scan'208";a="35627973" Received: from bmaguire-mobl.ger.corp.intel.com (HELO localhost) ([10.249.254.153]) by fmsmga001.fm.intel.com with ESMTP; 05 Mar 2018 08:56:44 -0800 From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org Cc: linux-security-module@vger.kernel.org, keyrings@vger.kernel.org, Jarkko Sakkinen , Peter Huewe , Jason Gunthorpe , Arnd Bergmann , Greg Kroah-Hartman , linux-kernel@vger.kernel.org (open list) Subject: [PATCH v3 5/5] tpm: migrate tpm2_get_random() to use struct tpm_buf Date: Mon, 5 Mar 2018 18:56:14 +0200 Message-Id: <20180305165614.5469-6-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180305165614.5469-1-jarkko.sakkinen@linux.intel.com> References: <20180305165614.5469-1-jarkko.sakkinen@linux.intel.com> Sender: linux-integrity-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In order to make struct tpm_buf the first class object for constructing TPM commands, migrate tpm2_get_random() to use it. In addition, removed remaining references to struct tpm2_cmd. All of them use it to acquire the length of the response, which can be achieved by using tpm_buf_length(). Signed-off-by: Jarkko Sakkinen --- drivers/char/tpm/tpm.h | 19 ++++----- drivers/char/tpm/tpm2-cmd.c | 98 ++++++++++++++++++--------------------------- 2 files changed, 49 insertions(+), 68 deletions(-) diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h index cccd5994a0e1..29c0717437bc 100644 --- a/drivers/char/tpm/tpm.h +++ b/drivers/char/tpm/tpm.h @@ -412,23 +412,24 @@ struct tpm_buf { u8 *data; }; -static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal) +static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal) { struct tpm_input_header *head; + head = (struct tpm_input_header *)buf->data; + head->tag = cpu_to_be16(tag); + head->length = cpu_to_be32(sizeof(*head)); + head->ordinal = cpu_to_be32(ordinal); +} +static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal) +{ buf->data_page = alloc_page(GFP_HIGHUSER); if (!buf->data_page) return -ENOMEM; buf->flags = 0; buf->data = kmap(buf->data_page); - - head = (struct tpm_input_header *) buf->data; - - head->tag = cpu_to_be16(tag); - head->length = cpu_to_be32(sizeof(*head)); - head->ordinal = cpu_to_be32(ordinal); - + tpm_buf_reset(buf, tag, ordinal); return 0; } @@ -557,7 +558,7 @@ static inline u32 tpm2_rc_value(u32 rc) int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf); int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count, struct tpm2_digest *digests); -int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max); +int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max); void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle, unsigned int flags); int tpm2_seal_trusted(struct tpm_chip *chip, diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c index e02f7d46e9ac..9e687b4a325a 100644 --- a/drivers/char/tpm/tpm2-cmd.c +++ b/drivers/char/tpm/tpm2-cmd.c @@ -27,25 +27,6 @@ enum tpm2_session_attributes { TPM2_SA_CONTINUE_SESSION = BIT(0), }; -struct tpm2_get_random_in { - __be16 size; -} __packed; - -struct tpm2_get_random_out { - __be16 size; - u8 buffer[TPM_MAX_RNG_DATA]; -} __packed; - -union tpm2_cmd_params { - struct tpm2_get_random_in getrandom_in; - struct tpm2_get_random_out getrandom_out; -}; - -struct tpm2_cmd { - tpm_cmd_header header; - union tpm2_cmd_params params; -} __packed; - struct tpm2_hash { unsigned int crypto_id; unsigned int tpm_id; @@ -298,67 +279,70 @@ int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count, } -#define TPM2_GETRANDOM_IN_SIZE \ - (sizeof(struct tpm_input_header) + \ - sizeof(struct tpm2_get_random_in)) - -static const struct tpm_input_header tpm2_getrandom_header = { - .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS), - .length = cpu_to_be32(TPM2_GETRANDOM_IN_SIZE), - .ordinal = cpu_to_be32(TPM2_CC_GET_RANDOM) -}; +struct tpm2_get_random_out { + __be16 size; + u8 buffer[TPM_MAX_RNG_DATA]; +} __packed; /** * tpm2_get_random() - get random bytes from the TPM RNG * * @chip: TPM chip to use - * @out: destination buffer for the random bytes + * @dest: destination buffer for the random bytes * @max: the max number of bytes to write to @out * * Return: - * Size of the output buffer, or -EIO on error. + * size of the output buffer when the operation is successful. + * A negative number for system errors (errno). */ -int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max) +int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) { - struct tpm2_cmd cmd; - u32 recd, rlength; - u32 num_bytes; + struct tpm2_get_random_out *out; + struct tpm_buf buf; + u32 recd; + u32 num_bytes = max; int err; int total = 0; int retries = 5; - u8 *dest = out; - - num_bytes = min_t(u32, max, sizeof(cmd.params.getrandom_out.buffer)); + u8 *dest_ptr = dest; - if (!out || !num_bytes || - max > sizeof(cmd.params.getrandom_out.buffer)) + if (!num_bytes || max > TPM_MAX_RNG_DATA) return -EINVAL; - do { - cmd.header.in = tpm2_getrandom_header; - cmd.params.getrandom_in.size = cpu_to_be16(num_bytes); + err = tpm_buf_init(&buf, 0, 0); + if (err) + return err; - err = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), + do { + tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM); + tpm_buf_append_u16(&buf, num_bytes); + err = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, offsetof(struct tpm2_get_random_out, buffer), 0, "attempting get random"); if (err) - break; + goto out; - recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size), - num_bytes); - rlength = be32_to_cpu(cmd.header.out.length); - if (rlength < offsetof(struct tpm2_get_random_out, buffer) + - recd) - return -EFAULT; - memcpy(dest, cmd.params.getrandom_out.buffer, recd); + out = (struct tpm2_get_random_out *) + &buf.data[TPM_HEADER_SIZE]; + recd = min_t(u32, be16_to_cpu(out->size), num_bytes); + if (tpm_buf_length(&buf) < + offsetof(struct tpm2_get_random_out, buffer) + recd) { + err = -EFAULT; + goto out; + } + memcpy(dest_ptr, out->buffer, recd); - dest += recd; + dest_ptr += recd; total += recd; num_bytes -= recd; } while (retries-- && total < max); + tpm_buf_destroy(&buf); return total ? total : -EIO; +out: + tpm_buf_destroy(&buf); + return err; } /** @@ -432,7 +416,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip, { unsigned int blob_len; struct tpm_buf buf; - u32 hash, rlength; + u32 hash; int i; int rc; @@ -507,8 +491,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip, rc = -E2BIG; goto out; } - rlength = be32_to_cpu(((struct tpm2_cmd *)&buf)->header.out.length); - if (rlength < TPM_HEADER_SIZE + 4 + blob_len) { + if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 4 + blob_len) { rc = -EFAULT; goto out; } @@ -618,7 +601,6 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, u16 data_len; u8 *data; int rc; - u32 rlength; rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL); if (rc) @@ -646,9 +628,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, goto out; } - rlength = be32_to_cpu(((struct tpm2_cmd *)&buf) - ->header.out.length); - if (rlength < TPM_HEADER_SIZE + 6 + data_len) { + if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 6 + data_len) { rc = -EFAULT; goto out; }