Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1852331imm; Thu, 19 Jul 2018 08:53:32 -0700 (PDT) X-Google-Smtp-Source: AAOMgpc/2B3gDCTvzDD0Zk1MeQtyzDRkK934FqE9KmE6uR77VPwkTkayLctamG1XmEp90xXNB2jw X-Received: by 2002:a63:ce12:: with SMTP id y18-v6mr10560449pgf.144.1532015612776; Thu, 19 Jul 2018 08:53:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532015612; cv=none; d=google.com; s=arc-20160816; b=tTM8vb5SoxtqWgu3vy++ApZQNkGjQ4Y//s7A8a8PjbOBNav57W53AOTNv8IG4Dsc8q KFylkjC3JDZD0JQQZg3//1f+chLZc7rVE1fovn7JZEmrfEURGCAfkLdB5KyiOvwJ5a8b Okil5bWmAok+Tit9agsjDHgevcKJfwnWDW0pPthucaxGSgj+P+pvgceEhysCZzGgNvCm Wj74/KLZoP0Pv6sv7zV8y5ajA5bPbf8KHhVBjGIhpol0ItjCzrVhCJrThUPzwaOhysqs kfgAkixeSstZEZ/+u7+oLs8j99VGk25ktoMHUVGRSsfORQSVW9rvchjc37UFy9ftBb8Q vqJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:message-id:date:cc:to:from:subject :arc-authentication-results; bh=TgBGYdzPjhbCK+dVSmDf5DJnstSaKtwHWqOCDNeqN+E=; b=0tUJoR+aV2B3cTATvQWYBtWtXmbW/kG7ae0rJMkoyJc43OvL43bW+n//TswoMCkKxa +yLB+26bW9R2hKqskAXhzawJwUMTr/SSkgb+Ov5+z5xMXZCV6czC7aRaKwjB76ivR5Q+ UWSpuR5kF9ltS2pwzAWnRRfNmyhxK3M4LtFNEiFjDOhZ7IGXEwbHPVfx+aSkG61dePxy PMDRWGsF310yjZ8A9/atlRZh1wpPEtasV7041ve/jtLMyLdKTp6GHyvXShj9Bz4ap1iA N3Cdj0lhDuK9YfJvB0VI3xul9dZLrLNPAZfzecZkWMK2+36/YXKwTAglQlkF8l+UT/vp jKEw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n85-v6si6253269pfj.251.2018.07.19.08.53.17; Thu, 19 Jul 2018 08:53:32 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731968AbeGSQgU (ORCPT + 99 others); Thu, 19 Jul 2018 12:36:20 -0400 Received: from mga06.intel.com ([134.134.136.31]:28092 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727575AbeGSQgU (ORCPT ); Thu, 19 Jul 2018 12:36:20 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Jul 2018 08:52:33 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,375,1526367600"; d="scan'208";a="57808984" Received: from mldahl-mobl2.amr.corp.intel.com (HELO tstruk-mobl1.jf.intel.com) ([10.254.107.75]) by orsmga007.jf.intel.com with ESMTP; 19 Jul 2018 08:52:33 -0700 Subject: [PATCH] tpm: add support for partial reads From: Tadeusz Struk To: jarkko.sakkinen@linux.intel.com Cc: jgg@ziepe.ca, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, tadeusz.struk@intel.com Date: Thu, 19 Jul 2018 08:52:32 -0700 Message-ID: <153201555276.20155.1352499992826895966.stgit@tstruk-mobl1.jf.intel.com> User-Agent: StGit/unknown-version MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently to read a response from the TPM device an application needs provide "big enough" buffer for the whole response and read it in one go. The application doesn't know how big the response it beforehand so it always needs to maintain a 4K buffer and read the max (4K). In case if the user of the TSS library doesn't provide big enough buffer the TCTI spec says that the library should set the required size and return TSS2_TCTI_RC_INSUFFICIENT_BUFFER error code so that the application could allocate a bigger buffer and call receive again. To make it possible in the TSS library this requires being able to do partial reads from the driver. The library would read the header first to get the actual size of the response from the header and then read the rest of the response. This patch adds support for partial reads. The usecase is implemented in this TSS commit: https://github.com/tpm2-software/tpm2-tss/commit/ce982f67a67dc08e24683d30b05800648d8a264c Signed-off-by: Tadeusz Struk --- drivers/char/tpm/tpm-dev-common.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/drivers/char/tpm/tpm-dev-common.c b/drivers/char/tpm/tpm-dev-common.c index 3f2089f75c30..f5b614984dbc 100644 --- a/drivers/char/tpm/tpm-dev-common.c +++ b/drivers/char/tpm/tpm-dev-common.c @@ -99,20 +99,34 @@ ssize_t tpm_common_read(struct file *file, char __user *buf, ssize_t ret_size = 0; int rc; - del_singleshot_timer_sync(&priv->user_read_timer); - flush_work(&priv->timeout_work); mutex_lock(&priv->buffer_mutex); if (priv->data_pending) { - ret_size = min_t(ssize_t, size, priv->data_pending); - if (ret_size > 0) { - rc = copy_to_user(buf, priv->data_buffer, ret_size); - memset(priv->data_buffer, 0, priv->data_pending); - if (rc) - ret_size = -EFAULT; + ret_size = min_t(ssize_t, size + *off, priv->data_pending); + if (ret_size <= 0) { + ret_size = 0; + priv->data_pending = 0; + *off = 0; + goto out; } - priv->data_pending = 0; + rc = copy_to_user(buf, priv->data_buffer + *off, ret_size); + if (rc) { + memset(priv->data_buffer, 0, priv->data_pending); + ret_size = -EFAULT; + priv->data_pending = 0; + *off = 0; + } else { + memset(priv->data_buffer + *off, 0, ret_size); + priv->data_pending -= ret_size; + *off += ret_size; + } + } +out: + if (!priv->data_pending) { + del_singleshot_timer_sync(&priv->user_read_timer); + flush_work(&priv->timeout_work); + *off = 0; } mutex_unlock(&priv->buffer_mutex);