From patchwork Mon Apr 15 21:08:07 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 236727 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 95A322C00F3 for ; Tue, 16 Apr 2013 07:09:12 +1000 (EST) Received: from localhost ([::1]:49159 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1URqeE-0002gj-Ov for incoming@patchwork.ozlabs.org; Mon, 15 Apr 2013 17:09:10 -0400 Received: from eggs.gnu.org ([208.118.235.92]:50713) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1URqdw-0002fl-7T for qemu-devel@nongnu.org; Mon, 15 Apr 2013 17:08:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1URqdv-0003yB-5F for qemu-devel@nongnu.org; Mon, 15 Apr 2013 17:08:52 -0400 Received: from e32.co.us.ibm.com ([32.97.110.150]:48036) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1URqdu-0003y4-Uh for qemu-devel@nongnu.org; Mon, 15 Apr 2013 17:08:51 -0400 Received: from /spool/local by e32.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 15 Apr 2013 15:08:48 -0600 Received: from d03dlp02.boulder.ibm.com (9.17.202.178) by e32.co.us.ibm.com (192.168.1.132) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 15 Apr 2013 15:08:10 -0600 Received: from d03relay03.boulder.ibm.com (d03relay03.boulder.ibm.com [9.17.195.228]) by d03dlp02.boulder.ibm.com (Postfix) with ESMTP id 394B33E4004F for ; Mon, 15 Apr 2013 15:07:56 -0600 (MDT) Received: from d03av06.boulder.ibm.com (d03av06.boulder.ibm.com [9.17.195.245]) by d03relay03.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r3FL8898119390 for ; Mon, 15 Apr 2013 15:08:08 -0600 Received: from d03av06.boulder.ibm.com (loopback [127.0.0.1]) by d03av06.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r3FLAuC1006496 for ; Mon, 15 Apr 2013 15:10:57 -0600 Received: from [9.2.141.150] (k-d941e-10.watson.ibm.com [9.2.141.150]) by d03av06.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r3FLAual006456; Mon, 15 Apr 2013 15:10:56 -0600 Message-ID: <1366060087.13870.2.camel@d941e-10> From: Stefan Berger To: "qemu-devel@nongnu.org" Date: Mon, 15 Apr 2013 17:08:07 -0400 X-Mailer: Evolution 3.2.3 (3.2.3-3.fc16) Mime-Version: 1.0 X-TM-AS-MML: No X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13041521-5406-0000-0000-0000077F8CAE X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 32.97.110.150 Cc: coreyb@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH] tpm: Simplify creation of cancel path X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Simplify the creation of the cancel path given the TPM's device path. Given the path /dev/tpm0 build the path /sys/class/misc/tpm0/device/cancel. Signed-off-by: Stefan Berger Reviewed-by: Eric Blake --- tpm/tpm_passthrough.c | 74 +++++++++++--------------------------------------- 1 file changed, 17 insertions(+), 57 deletions(-) Index: qemu-git.pt/tpm/tpm_passthrough.c =================================================================== --- qemu-git.pt.orig/tpm/tpm_passthrough.c +++ qemu-git.pt/tpm/tpm_passthrough.c @@ -336,45 +336,17 @@ static int tpm_passthrough_test_tpmdev(i } /* - * Check whether the given base path, e.g., /sys/class/misc/tpm0/device, - * is the sysfs directory of a TPM. A TPM sysfs directory should be uniquely - * recognizable by the file entries 'pcrs' and 'cancel'. - * Upon success 'true' is returned and the basebath buffer has '/cancel' - * appended. - */ -static bool tpm_passthrough_check_sysfs_cancel(char *basepath, size_t bufsz) -{ - char path[PATH_MAX]; - struct stat statbuf; - - snprintf(path, sizeof(path), "%s/pcrs", basepath); - if (stat(path, &statbuf) == -1 || !S_ISREG(statbuf.st_mode)) { - return false; - } - - snprintf(path, sizeof(path), "%s/cancel", basepath); - if (stat(path, &statbuf) == -1 || !S_ISREG(statbuf.st_mode)) { - return false; - } - - strncpy(basepath, path, bufsz); - - return true; -} - -/* * Unless path or file descriptor set has been provided by user, * determine the sysfs cancel file following kernel documentation * in Documentation/ABI/stable/sysfs-class-tpm. + * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel */ static int tpm_passthrough_open_sysfs_cancel(TPMBackend *tb) { + TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); int fd = -1; - unsigned int idx; - DIR *pnp_dir; + char *dev; char path[PATH_MAX]; - struct dirent entry, *result; - int len; if (tb->cancel_path) { fd = qemu_open(tb->cancel_path, O_WRONLY); @@ -385,34 +357,22 @@ static int tpm_passthrough_open_sysfs_ca return fd; } - snprintf(path, sizeof(path), "/sys/class/misc"); - pnp_dir = opendir(path); - if (pnp_dir != NULL) { - while (readdir_r(pnp_dir, &entry, &result) == 0 && - result != NULL) { - /* - * only allow /sys/class/misc/tpm%u type of paths - */ - if (sscanf(entry.d_name, "tpm%u%n", &idx, &len) < 1 || - len <= strlen("tpm") || - len != strlen(entry.d_name)) { - continue; - } - - snprintf(path, sizeof(path), "/sys/class/misc/%s/device", - entry.d_name); - if (!tpm_passthrough_check_sysfs_cancel(path, sizeof(path))) { - continue; - } - + dev = strrchr(tpm_pt->tpm_dev, '/'); + if (dev) { + dev++; + if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel", + dev) < sizeof(path)) { fd = qemu_open(path, O_WRONLY); - break; + if (fd >= 0) { + tb->cancel_path = g_strdup(path); + } else { + error_report("tpm_passthrough: Could not open TPM cancel " + "path %s : %s", path, strerror(errno)); + } } - closedir(pnp_dir); - } - - if (fd >= 0) { - tb->cancel_path = g_strdup(path); + } else { + error_report("tpm_passthrough: Bad TPM device path %s", + tpm_pt->tpm_dev); } return fd;