Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3252829imu; Fri, 18 Jan 2019 07:21:02 -0800 (PST) X-Google-Smtp-Source: ALg8bN6kdJ6V5mgPwEToGvTVDW+lDP0ll0+/F0VKnYTD36W+sWhZqYa/R6kYU9Vf4WxAaSXVgAfb X-Received: by 2002:a62:e0d8:: with SMTP id d85mr19436726pfm.214.1547824862665; Fri, 18 Jan 2019 07:21:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547824862; cv=none; d=google.com; s=arc-20160816; b=l32KVU7zZO7MXfrh2VEpQEIy15bpEBN9RKnGPk+frho8e/3Z9DFkhpkzs0BBc72Kg/ og+IbpfFLX4/FEp21k7OdvTZTNyvATYpLEmlq+iTAk+REjYPoZNEFoFvozeRHHEglvTz 2AGUb6suixDTacRJNPvj5yxaAJcXHNaLYd7YkE43ANBKO1H5m9lYZ1TrjL1lBUDui0Yy nBobXEkQFs2Whyus/Qp53uayzl0lGOBTFDitHhrVMg/JL5KGRbQRKDvtN6/zPC+3NC8a HfNy9zh6SCoRZRphbA/O1lvxKvoYL1/gIccEnvP4QQIHNkerD4cnUXy5+OOw9cpfWrC0 g5Bg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:organization:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=RKFgJhwqEllbevA9ib0cPQAMNhF4RWnfm/p0XkkcaMU=; b=AHWaoYaBz/ckMnjNsplWrdZJSLgKJcSsxkQggENYxOomjboDX+SwlQUNJDfvs6jwz6 muflLzJF4sgHrqk/3liO1nTA3B9t2KdWT6tLE0a24AOMK/Cm25GnXR4y4ghusQ0MQV1G EtZjpAm3HN3FejthgRbZPrXXz+fszrND2isuWX6UnFquDjH7Zz4buGn1Bd79SzJJxhER ud6XDfjr4JtaF3scGcE2FRHaDlbVq0DPYIk3ovg37DGV+tnt/ScVr2UvPth1YO57I2Xh Jn3+acngvIhdPdtU28dPPL8+/UAnuxGEvpjeEjwhZGcfs+9G3GU0zv6MwXcWDos5JkQx M4mQ== 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 p12si4831928pgj.56.2019.01.18.07.20.42; Fri, 18 Jan 2019 07:21:02 -0800 (PST) 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 S1727721AbfARPSe (ORCPT + 99 others); Fri, 18 Jan 2019 10:18:34 -0500 Received: from mga18.intel.com ([134.134.136.126]:3620 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727524AbfARPSe (ORCPT ); Fri, 18 Jan 2019 10:18:34 -0500 X-Amp-Result: UNSCANNABLE X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Jan 2019 07:18:33 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,491,1539673200"; d="scan'208";a="139412029" Received: from unknown (HELO localhost) ([10.249.254.249]) by fmsmga001.fm.intel.com with ESMTP; 18 Jan 2019 07:18:30 -0800 Date: Fri, 18 Jan 2019 17:18:27 +0200 From: Jarkko Sakkinen To: Jia Zhang Cc: peterhuewe@gmx.de, jgg@ziepe.ca, tweek@google.com, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/2] tpm/eventlog/tpm1: Simplify walking over *pos measurements Message-ID: <20190118151827.GK4080@linux.intel.com> References: <1547197173-52826-1-git-send-email-zhang.jia@linux.alibaba.com> <1547197173-52826-2-git-send-email-zhang.jia@linux.alibaba.com> <20190116220952.GH25803@linux.intel.com> <8709dd61-2422-1c20-9937-d6003fa0354e@linux.alibaba.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <8709dd61-2422-1c20-9937-d6003fa0354e@linux.alibaba.com> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jan 17, 2019 at 09:32:55AM +0800, Jia Zhang wrote: > > > On 2019/1/17 上午6:09, Jarkko Sakkinen wrote: > > Please use "tpm:" tag for commits, not "tpm/eventlog/tpm1". > > > > On Fri, Jan 11, 2019 at 04:59:32PM +0800, Jia Zhang wrote: > >> The responsibility of tpm1_bios_measurements_start() is to walk > >> over the first *pos measurements, ensuring the skipped and > >> to-be-read measurements are not out-of-boundary. > >> > >> Current logic is complicated a bit. Just employ a do-while loop > >> with necessary sanity check, and then get the goal. > >> > >> Signed-off-by: Jia Zhang > > > > What does this fix? Even if the current logic is "complicated", it is > > still a pretty simple functiion. > > > OK. Let me point out the fix part. Here is the original implementation: > > 87 /* read over *pos measurements */ > 88 for (i = 0; i < *pos; i++) { > 89 event = addr; > 90 > 91 converted_event_size = > 92 do_endian_conversion(event->event_size); > 93 converted_event_type = > 94 do_endian_conversion(event->event_type); > 95 > 96 if ((addr + sizeof(struct tcpa_event)) < limit) { > 97 if ((converted_event_type == 0) && > 98 (converted_event_size == 0)) > 99 return NULL; > 100 addr += (sizeof(struct tcpa_event) + > 101 converted_event_size); > 102 } > 103 } > > The problem (just ignore all off-by-1 issues) is that accessing to > event_size and event_type is not pre-checked carefully. In the latter > part of tpm1_bios_measurements_start() and > tpm1_bios_measurements_next(), there is a fixed patter to do the sanity > check like this: > > 136 /* now check if current entry is valid */ > 137 if ((v + sizeof(struct tcpa_event)) >= limit) > 138 return NULL; > > So if we simply change this read-over chunk with sanity check like this: > > /* read over *pos measurements */ > for (i = 0; i < *pos; i++) { > event = addr; > > if ((addr + sizeof(struct tcpa_event)) >= limit) > return NULL; > > converted_event_size = > do_endian_conversion(event->event_size); > converted_event_type = > do_endian_conversion(event->event_type); > > if ((converted_event_type == 0) && > (converted_event_size == 0)) > return NULL; > addr += (sizeof(struct tcpa_event) + > converted_event_size); > } > > We will get two highly similar code chunks in > tpm1_bios_measurements_start(). Here is the latter part: > > 106 /* now check if current entry is valid */ > 107 if ((addr + sizeof(struct tcpa_event)) >= limit) > 108 return NULL; > 109 > 110 event = addr; > 111 > 112 converted_event_size = do_endian_conversion(event->event_size); > 113 converted_event_type = do_endian_conversion(event->event_type); > 114 > 115 if (((converted_event_type == 0) && (converted_event_size == 0)) > 116 || ((addr + sizeof(struct tcpa_event) + > converted_event_size) > 117 >= limit)) > 118 return NULL; > 119 > 120 return addr; > > So using a do while logic can simply merge them together and thus simply > and optimize the logic of walking over *pos measurements. > > Sorry I admit my initial motivation is to fix up the sanity check > problem. If you would like to accept the optimization part, I will split > this patch. OK, got it now. I think I will apply this! Will take a while because of https://lkml.org/lkml/2019/1/18/485. Will not apply new patches before that is rooted. /Jarkko