Received: by 10.213.65.68 with SMTP id h4csp43946imn; Mon, 19 Mar 2018 19:00:13 -0700 (PDT) X-Google-Smtp-Source: AG47ELs6o2uruf2W7PYDysNQYUQm8444EFRNzkxLrGsRGLY9VkqcLaTiHRwMtGwwt0pZHDlL3sGC X-Received: by 10.98.78.21 with SMTP id c21mr58924pfb.107.1521511213387; Mon, 19 Mar 2018 19:00:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521511213; cv=none; d=google.com; s=arc-20160816; b=uJ9ZzVxzgV0OyFWx94+AxM8cmC2aa2sHTJR3gwHZqACWRbwXnrr190nXxlnAAvrVkm X+DWo4vwaO05uF9g4WljcNFVf/gAGKQOOWM+AWIwETTyJcRCVy4l6EqM7DIvIBD/d9Tj v/d1dWF3wV86BqxsE17zk2CrgNClTCNoCXOHDx1HztggbQKzTaC74oav6Kv7ey406OW6 LYe4cc5wy2MCt8+x3H1XXfO1SAxmy2OYH9RcNxNQ7eGfL1jHI40nFkX4oqmwqshHsIx4 NlecZLcVNQBWFBdMqOrv/1dXVyahqSVrvRUIzzZwCVF0Dl0n7nXcdyQh4ER6RaabW0bU llDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=jN2IHt24fCR6So++eEIb2dkZYm0FQ63T2qh6TiVs6OU=; b=p5MdpDuC18Z8zsBAQWYr8xIFakYnnrj2sQi9aFGd3+TbhJ1LqeNd4v/8Pgc3XNtuU+ qXAO5HSmw+esyYL+WJeJ1rgREkoMpdQ4esSa/wcekbiT6go6GzUFVOECdFsejxMkTB3Y 9LZeVKEnPytpfNNB3qpO9XbDqDrlSXNt/+QLgx+S1zodsBmZX/2cxErz36OW6k0JoML5 BRt700FsE0m7U2x1lWCezkQpEA4XfJIkgaLNqcAfg5v+nJiemU9QbBf4gs5HzTjkUZaE D1Ymv+Q6XEHjnzwD0ZYGhzVSfskWRE78vFCNmNUlbJjKYmUH8HdUeeY+5EpP4YqvHCBz 46hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=gPYncI+n; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a74si430938pfj.287.2018.03.19.18.59.59; Mon, 19 Mar 2018 19:00:13 -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; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=gPYncI+n; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S936554AbeCSW1T (ORCPT + 99 others); Mon, 19 Mar 2018 18:27:19 -0400 Received: from mail-sn1nam02on0103.outbound.protection.outlook.com ([104.47.36.103]:38940 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933848AbeCSPs4 (ORCPT ); Mon, 19 Mar 2018 11:48:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=jN2IHt24fCR6So++eEIb2dkZYm0FQ63T2qh6TiVs6OU=; b=gPYncI+nIrMWQ4oTSFznfY1sQ55hSrsMZNg167yDMjZVXhPApifKO0s2rx4XBm/3aZMYrgPd0GHtf6n7hDlXRW02x1BD6PCpRdloc/BzDX3ZkrzSgek4TufizyhOKhBsEYjIboX2yKd5wGLmRPrMPNPZDWrL2MX36gw6SmYAARE= Received: from DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) by DM5PR2101MB0936.namprd21.prod.outlook.com (52.132.131.166) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.609.2; Mon, 19 Mar 2018 15:48:50 +0000 Received: from DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::3d9b:79e7:94eb:5d62]) by DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::3d9b:79e7:94eb:5d62%5]) with mapi id 15.20.0631.004; Mon, 19 Mar 2018 15:48:50 +0000 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Javier Martinez Canillas , Jarkko Sakkinen , Sasha Levin Subject: [PATCH AUTOSEL for 4.15 064/124] tpm: return a TPM_RC_COMMAND_CODE response if command is not implemented Thread-Topic: [PATCH AUTOSEL for 4.15 064/124] tpm: return a TPM_RC_COMMAND_CODE response if command is not implemented Thread-Index: AQHTv5mtSq9yOIO9yk+agjT/FcuLlQ== Date: Mon, 19 Mar 2018 15:48:09 +0000 Message-ID: <20180319154645.11350-64-alexander.levin@microsoft.com> References: <20180319154645.11350-1-alexander.levin@microsoft.com> In-Reply-To: <20180319154645.11350-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;DM5PR2101MB0936;7:HmAl0M5pDJytsTHszQ4dA554iOtbauGEmbIgT8t53GcJk7kYTAivWt2OI5la5DteWg0wmwnznaOkce36gqzcopww5vpnHbVVaNjkOvg3E2lnMecEDDOpJHEn+ZowELw0ZO+mo/RO3KF+qvkJxdQz9K1e5dTJdBlM/QaPNPQK/KyMVe/UoikKgXjyckf3wiiYPc4UyGy7pG8GFBmJnW15I7xOTju/NSJNXJI9gM3Lsgi4+V2mdphw4o0UIFciLzpO;20:IiNUsiTvZxc5XrdVzrnCrWUrtnRBzafocwuFphv4//QH708RosQ7zdasH8UodV+0Vy0CJpj6NpHKJMZ4qPu9X5/sh878Qf+LTCpVPP7gjn+y2+bZ/Na0aAK6iPgs5I2V1/aBaAR6YUHZ/wIyL04PNFpGFNPwFtQnB16FOQW4eHE= x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: 8b3ac393-3e1c-4ca6-ec9d-08d58db0e934 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7193020);SRVR:DM5PR2101MB0936; x-ms-traffictypediagnostic: DM5PR2101MB0936: x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171)(228905959029699); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(61425038)(6040522)(2401047)(5005006)(8121501046)(10201501046)(3002001)(93006095)(93001095)(3231221)(944501300)(52105095)(6055026)(61426038)(61427038)(6041310)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(20161123560045)(20161123562045)(6072148)(201708071742011);SRVR:DM5PR2101MB0936;BCL:0;PCL:0;RULEID:;SRVR:DM5PR2101MB0936; x-forefront-prvs: 06167FAD59 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(39380400002)(346002)(39860400002)(366004)(376002)(396003)(199004)(189003)(2900100001)(3280700002)(2950100002)(10290500003)(66066001)(106356001)(76176011)(86612001)(6666003)(478600001)(5250100002)(6512007)(6486002)(107886003)(36756003)(97736004)(72206003)(25786009)(6436002)(53936002)(7736002)(2501003)(54906003)(110136005)(105586002)(10090500001)(2906002)(86362001)(305945005)(5660300001)(316002)(8676002)(6346003)(1076002)(3846002)(3660700001)(81156014)(4326008)(14454004)(26005)(6116002)(102836004)(8936002)(81166006)(6506007)(186003)(22452003)(59450400001)(99286004)(68736007)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR2101MB0936;H:DM5PR2101MB1032.namprd21.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-message-info: 5UjPaNt+tmpnCWfrS72lde6RZLEALGPlVs7zP+gzjsg+N4McpdAf31KVMMlbs+CWMT8QcklaAUabgcd9jTer32DN83HzhJC/u1/2FnYZw3plGVPbSYW1nYPOxmX9e7FWGVq/RBO92EDt6A/+4ax2CaN3Rw/4cZC7oyjgXcR0HKBfUs68+RTZIy+yfM8KzYpFGVCMqunStVsIZ18nOOx2HG/jXXupavj5uekPZEA/Hw7vwG1SjlmQlKJbPOcjyfgz9460lguM1vFCEWE1G/iAPoUF8FCzBB76fEqFRkuwERPFZUW5Sl04MKK7aI3dZ4dYXZlGHFVT/p+eKHry2n3tKw== spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8b3ac393-3e1c-4ca6-ec9d-08d58db0e934 X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Mar 2018 15:48:09.3142 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB0936 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Javier Martinez Canillas [ Upstream commit 095531f891e627e408606f2da4008d3d53e6748a ] According to the TPM Library Specification, a TPM device must do a command header validation before processing and return a TPM_RC_COMMAND_CODE code if the command is not implemented. So user-space will expect to handle that response as an error. But if the in-kernel resource manager is used (/dev/tpmrm?), an -EINVAL errno code is returned instead if the command isn't implemented. This confuses userspace since it doesn't expect that error value. This also isn't consistent with the behavior when not using TPM spaces and accessing the TPM directly (/dev/tpm?). In this case, the command is sent to the TPM even when not implemented and the TPM responds with an error. Instead of returning an -EINVAL errno code when the tpm_validate_command() function fails, synthesize a TPM command response so user-space can get a TPM_RC_COMMAND_CODE as expected when a chip doesn't implement the command. The TPM only sets 12 of the 32 bits in the TPM_RC response, so the TSS and TAB specifications define that higher layers in the stack should use some of the unused 20 bits to specify from which level of the stack the error is coming from. Since the TPM_RC_COMMAND_CODE response code is sent by the kernel resource manager, set the error level to the TAB/RM layer so user-space is aware of this. Suggested-by: Jason Gunthorpe Signed-off-by: Javier Martinez Canillas Reviewed-by: William Roberts Reviewed-by: Philip Tricca Reviewed-by: Jarkko Sakkinen Tested-by: Jarkko Sakkinen Signed-off-by: Jarkko Sakkinen Signed-off-by: Sasha Levin --- drivers/char/tpm/tpm-interface.c | 28 ++++++++++++++++++++-------- drivers/char/tpm/tpm.h | 5 +++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interf= ace.c index 5294442505cb..0f1dc35e7078 100644 --- a/drivers/char/tpm/tpm-interface.c +++ b/drivers/char/tpm/tpm-interface.c @@ -328,7 +328,7 @@ unsigned long tpm_calc_ordinal_duration(struct tpm_chip= *chip, } EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration); =20 -static bool tpm_validate_command(struct tpm_chip *chip, +static int tpm_validate_command(struct tpm_chip *chip, struct tpm_space *space, const u8 *cmd, size_t len) @@ -340,10 +340,10 @@ static bool tpm_validate_command(struct tpm_chip *chi= p, unsigned int nr_handles; =20 if (len < TPM_HEADER_SIZE) - return false; + return -EINVAL; =20 if (!space) - return true; + return 0; =20 if (chip->flags & TPM_CHIP_FLAG_TPM2 && chip->nr_commands) { cc =3D be32_to_cpu(header->ordinal); @@ -352,7 +352,7 @@ static bool tpm_validate_command(struct tpm_chip *chip, if (i < 0) { dev_dbg(&chip->dev, "0x%04X is an invalid command\n", cc); - return false; + return -EOPNOTSUPP; } =20 attrs =3D chip->cc_attrs_tbl[i]; @@ -362,11 +362,11 @@ static bool tpm_validate_command(struct tpm_chip *chi= p, goto err_len; } =20 - return true; + return 0; err_len: dev_dbg(&chip->dev, "%s: insufficient command length %zu", __func__, len); - return false; + return -EINVAL; } =20 /** @@ -391,8 +391,20 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm= _space *space, unsigned long stop; bool need_locality; =20 - if (!tpm_validate_command(chip, space, buf, bufsiz)) - return -EINVAL; + rc =3D tpm_validate_command(chip, space, buf, bufsiz); + if (rc =3D=3D -EINVAL) + return rc; + /* + * If the command is not implemented by the TPM, synthesize a + * response with a TPM2_RC_COMMAND_CODE return for user-space. + */ + if (rc =3D=3D -EOPNOTSUPP) { + header->length =3D cpu_to_be32(sizeof(*header)); + header->tag =3D cpu_to_be16(TPM2_ST_NO_SESSIONS); + header->return_code =3D cpu_to_be32(TPM2_RC_COMMAND_CODE | + TSS2_RESMGR_TPM_RC_LAYER); + return bufsiz; + } =20 if (bufsiz > TPM_BUFSIZE) bufsiz =3D TPM_BUFSIZE; diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h index 528cffbd49d3..f6f56dfda6c7 100644 --- a/drivers/char/tpm/tpm.h +++ b/drivers/char/tpm/tpm.h @@ -93,12 +93,17 @@ enum tpm2_structures { TPM2_ST_SESSIONS =3D 0x8002, }; =20 +/* Indicates from what layer of the software stack the error comes from */ +#define TSS2_RC_LAYER_SHIFT 16 +#define TSS2_RESMGR_TPM_RC_LAYER (11 << TSS2_RC_LAYER_SHIFT) + enum tpm2_return_codes { TPM2_RC_SUCCESS =3D 0x0000, TPM2_RC_HASH =3D 0x0083, /* RC_FMT1 */ TPM2_RC_HANDLE =3D 0x008B, TPM2_RC_INITIALIZE =3D 0x0100, /* RC_VER1 */ TPM2_RC_DISABLED =3D 0x0120, + TPM2_RC_COMMAND_CODE =3D 0x0143, TPM2_RC_TESTING =3D 0x090A, /* RC_WARN */ TPM2_RC_REFERENCE_H0 =3D 0x0910, }; --=20 2.14.1