From nobody Wed May  7 05:47:36 2025
Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id E7E662397B9;
	Wed,  2 Apr 2025 12:47:38 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
 arc=none smtp.client-ip=13.77.154.182
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1743598060; cv=none;
 b=jwaFpYcrHyaMvmqAEEP4rjFKhc6+peVh+pf8xCQMbdX3AyJoGFOxmhvi0M96RWFYy6okeqOqhYfAaK1zooSJbaxzUc6bCHtA01Yx2lW08XG7FE7YxmApfO+GNqOG8ZGnKud8M6t6syUjanCjKxbjAquoDvtUOOHo07wJqmHwiM8=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1743598060; c=relaxed/simple;
	bh=hZi9oXKtafjfMDBtwL81ZeBozwb0pXIsgwsMccGcsbQ=;
	h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
	 MIME-Version;
 b=QG+794sATI2ykntrxh0OvNF/OI+ziAwQ6pXMe6FxtQqp4TWYom31MyyS4CIfZZ9T/FfWLk03C2da00LXfMSeqGspRy9/RfNDUn3bcneGBqxQ849yRTTTgSwzNcsBzHupsFqqgLuyJ8HrjF/0LF4Cfn5SVIHBJ5trdfelWYKAxY0=
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com;
 spf=pass smtp.mailfrom=linux.microsoft.com;
 dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b=jAILEI5p;
 arc=none smtp.client-ip=13.77.154.182
Authentication-Results: smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
 spf=pass smtp.mailfrom=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b="jAILEI5p"
Received: from DESKTOP-VOT081N.hsd1.ga.comcast.net (unknown [20.114.144.49])
	by linux.microsoft.com (Postfix) with ESMTPSA id 219FD2025659;
	Wed,  2 Apr 2025 05:47:36 -0700 (PDT)
DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 219FD2025659
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;
	s=default; t=1743598058;
	bh=h/cpgKcbrvyKJYGjsn7+e/Tvw7I5+jxPdp1ilsIXavc=;
	h=From:To:Cc:Subject:Date:In-Reply-To:References:From;
	b=jAILEI5pItipCfyuKGVNeMXN1b9ysS7Iw139vrqnCCH1ygchjkRpoJP5O7C1+cH4N
	 4kqeT2Zu5SngVoo3Rb8/ijhtnav0K3bO+MU6yAKnFh9jYlnLFGRFRTY19Cm2ezgHCa
	 HgUTgeLPcymBB01YjkL7CRb0OZMNCKPSHZUkyjfU=
From: steven chen <chenste@linux.microsoft.com>
To: zohar@linux.ibm.com,
	stefanb@linux.ibm.com,
	roberto.sassu@huaweicloud.com,
	roberto.sassu@huawei.com,
	eric.snowberg@oracle.com,
	ebiederm@xmission.com,
	paul@paul-moore.com,
	code@tyhicks.com,
	bauermann@kolabnow.com,
	linux-integrity@vger.kernel.org,
	kexec@lists.infradead.org,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: madvenka@linux.microsoft.com,
	nramas@linux.microsoft.com,
	James.Bottomley@HansenPartnership.com,
	bhe@redhat.com,
	vgoyal@redhat.com,
	dyoung@redhat.com
Subject: [PATCH v11 1/9] ima: rename variable the set_file "file" to
 "ima_kexec_file"
Date: Wed,  2 Apr 2025 05:47:14 -0700
Message-ID: <20250402124725.5601-2-chenste@linux.microsoft.com>
X-Mailer: git-send-email 2.43.0
In-Reply-To: <20250402124725.5601-1-chenste@linux.microsoft.com>
References: <20250402124725.5601-1-chenste@linux.microsoft.com>
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id: <linux-kernel.vger.kernel.org>
List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="utf-8"

The current kernel behavior is IMA measurements snapshot is taken at
kexec 'load' and not at kexec 'execute'. IMA log is then carried
over to the new kernel after kexec 'execute'. However, the time gap
between kexec load and kexec reboot can be very long. During this
time window, new events extended into TPM PCRs miss the chance
to be carried over to the second kernel.
=20
To address the above, the following approach is proposed:
  - Allocate the necessary buffer during the kexec load phase.
  - Populate this buffer with the IMA measurements during
    the kexec execute phase.

In the current implementation, a local variable "file" of type seq_file
is used in the API ima_dump_measurement_list() to store the IMA measurements
to be carried over across kexec system call. To make this buffer accessible
at kexec 'execute' time, rename it to "ima_kexec_file" before making it
a file variable to better reflect its purpose.

Renaming the local variable "file" of type seq_file defined in the=20
ima_dump_measurement_list function to "ima_kexec_file" will improve code
readability and maintainability by making the variable's role more explicit.

Suggested-by: Mimi Zohar <zohar@linux.ibm.com>
Signed-off-by: steven chen <chenste@linux.microsoft.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
---
 security/integrity/ima/ima_kexec.c | 31 +++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/im=
a_kexec.c
index 9d45f4d26f73..650beb74346c 100644
--- a/security/integrity/ima/ima_kexec.c
+++ b/security/integrity/ima/ima_kexec.c
@@ -18,30 +18,30 @@
 static int ima_dump_measurement_list(unsigned long *buffer_size, void **bu=
ffer,
 				     unsigned long segment_size)
 {
+	struct seq_file ima_kexec_file;
 	struct ima_queue_entry *qe;
-	struct seq_file file;
 	struct ima_kexec_hdr khdr;
 	int ret =3D 0;
=20
 	/* segment size can't change between kexec load and execute */
-	file.buf =3D vmalloc(segment_size);
-	if (!file.buf) {
+	ima_kexec_file.buf =3D vmalloc(segment_size);
+	if (!ima_kexec_file.buf) {
 		ret =3D -ENOMEM;
 		goto out;
 	}
=20
-	file.file =3D NULL;
-	file.size =3D segment_size;
-	file.read_pos =3D 0;
-	file.count =3D sizeof(khdr);	/* reserved space */
+	ima_kexec_file.file =3D NULL;
+	ima_kexec_file.size =3D segment_size;
+	ima_kexec_file.read_pos =3D 0;
+	ima_kexec_file.count =3D sizeof(khdr);	/* reserved space */
=20
 	memset(&khdr, 0, sizeof(khdr));
 	khdr.version =3D 1;
 	/* This is an append-only list, no need to hold the RCU read lock */
 	list_for_each_entry_rcu(qe, &ima_measurements, later, true) {
-		if (file.count < file.size) {
+		if (ima_kexec_file.count < ima_kexec_file.size) {
 			khdr.count++;
-			ima_measurements_show(&file, qe);
+			ima_measurements_show(&ima_kexec_file, qe);
 		} else {
 			ret =3D -EINVAL;
 			break;
@@ -55,23 +55,24 @@ static int ima_dump_measurement_list(unsigned long *buf=
fer_size, void **buffer,
 	 * fill in reserved space with some buffer details
 	 * (eg. version, buffer size, number of measurements)
 	 */
-	khdr.buffer_size =3D file.count;
+	khdr.buffer_size =3D ima_kexec_file.count;
 	if (ima_canonical_fmt) {
 		khdr.version =3D cpu_to_le16(khdr.version);
 		khdr.count =3D cpu_to_le64(khdr.count);
 		khdr.buffer_size =3D cpu_to_le64(khdr.buffer_size);
 	}
-	memcpy(file.buf, &khdr, sizeof(khdr));
+	memcpy(ima_kexec_file.buf, &khdr, sizeof(khdr));
=20
 	print_hex_dump_debug("ima dump: ", DUMP_PREFIX_NONE, 16, 1,
-			     file.buf, file.count < 100 ? file.count : 100,
+			     ima_kexec_file.buf, ima_kexec_file.count < 100 ?
+			     ima_kexec_file.count : 100,
 			     true);
=20
-	*buffer_size =3D file.count;
-	*buffer =3D file.buf;
+	*buffer_size =3D ima_kexec_file.count;
+	*buffer =3D ima_kexec_file.buf;
 out:
 	if (ret =3D=3D -EINVAL)
-		vfree(file.buf);
+		vfree(ima_kexec_file.buf);
 	return ret;
 }
=20
--=20
2.25.1
From nobody Wed May  7 05:47:36 2025
Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 4844223C361;
	Wed,  2 Apr 2025 12:47:41 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
 arc=none smtp.client-ip=13.77.154.182
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1743598063; cv=none;
 b=Aa4ZUoloRWjwlZ+SmLqbT7RIEIVJEWq8RQzH1fOyT/gHJ//b+2T4xJLm8S6+HSoDij8HETcTFMhDjrjUgktbEsUF2OH77BS89wMA0cmjA+elPx045hnawsmCHRrBUR5YDcQavSlyx84uYCar8W232n68SWVjDe1mLDocjcKdVsw=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1743598063; c=relaxed/simple;
	bh=SyM0DZtwigOEgpFBa9WxtOXzwXbfno/O0ZN2nxa+6+c=;
	h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
	 MIME-Version;
 b=J5b9TIFRJvlk2Ftphz/2OLa1cuFA9l7ZtQGSsPNt31M+kKyhxhQ7Nzew86e3noTpRQaugjizmBuBPND3DGFdc64wZ8ANNmtcoNcwY+nCtTzt3CNEq2omsRNDAHDEEf4oht1sSKGG9Uzrvmg0LhiOxZ32MNma/WIfWooo7UaiDKE=
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com;
 spf=pass smtp.mailfrom=linux.microsoft.com;
 dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b=GBp977W2;
 arc=none smtp.client-ip=13.77.154.182
Authentication-Results: smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
 spf=pass smtp.mailfrom=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b="GBp977W2"
Received: from DESKTOP-VOT081N.hsd1.ga.comcast.net (unknown [20.114.144.49])
	by linux.microsoft.com (Postfix) with ESMTPSA id DC82A2041310;
	Wed,  2 Apr 2025 05:47:38 -0700 (PDT)
DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com DC82A2041310
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;
	s=default; t=1743598060;
	bh=c/RS/PAb2CclZX+321SCa66PSDIvCelNRFFOQ2o7wf8=;
	h=From:To:Cc:Subject:Date:In-Reply-To:References:From;
	b=GBp977W2a8y50FQq8iP0ZPDfbqsoMIp55WRJGHcKH9M/umzkVgUBH/FizQPCXl3xf
	 j8etCR89uA+7HKmwJBubkR4vGp+xe1bqwKqmwiYvto8LwzQM0pNLF4e56xmn/LXxes
	 LcV0T0O0+B3gyXnLjvS2XgZJG3/oRu38Ii4RIY9Y=
From: steven chen <chenste@linux.microsoft.com>
To: zohar@linux.ibm.com,
	stefanb@linux.ibm.com,
	roberto.sassu@huaweicloud.com,
	roberto.sassu@huawei.com,
	eric.snowberg@oracle.com,
	ebiederm@xmission.com,
	paul@paul-moore.com,
	code@tyhicks.com,
	bauermann@kolabnow.com,
	linux-integrity@vger.kernel.org,
	kexec@lists.infradead.org,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: madvenka@linux.microsoft.com,
	nramas@linux.microsoft.com,
	James.Bottomley@HansenPartnership.com,
	bhe@redhat.com,
	vgoyal@redhat.com,
	dyoung@redhat.com
Subject: [PATCH v11 2/9] ima: define and call ima_alloc_kexec_file_buf()
Date: Wed,  2 Apr 2025 05:47:15 -0700
Message-ID: <20250402124725.5601-3-chenste@linux.microsoft.com>
X-Mailer: git-send-email 2.43.0
In-Reply-To: <20250402124725.5601-1-chenste@linux.microsoft.com>
References: <20250402124725.5601-1-chenste@linux.microsoft.com>
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id: <linux-kernel.vger.kernel.org>
List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="utf-8"

In the current implementation, the ima_dump_measurement_list() API is=20
called during the kexec "load" phase, where a buffer is allocated and=20
the measurement records are copied. Due to this, new events added after
kexec load but before kexec execute are not carried over to the new kernel
during kexec operation

To allow the buffer allocation and population to be separated into distinct
steps, make the function local seq_file "ima_kexec_file" to a file variable.

Carrying the IMA measurement list across kexec requires allocating a
buffer and copying the measurement records.  Separate allocating the
buffer and copying the measurement records into separate functions in
order to allocate the buffer at kexec 'load' and copy the measurements
at kexec 'execute'.

Signed-off-by: Tushar Sugandhi <tusharsu@linux.microsoft.com>
Signed-off-by: steven chen <chenste@linux.microsoft.com>
Acked-by: Baoquan He <bhe@redhat.com>
---
 security/integrity/ima/ima_kexec.c | 46 +++++++++++++++++++++++-------
 1 file changed, 35 insertions(+), 11 deletions(-)

diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/im=
a_kexec.c
index 650beb74346c..b12ac3619b8f 100644
--- a/security/integrity/ima/ima_kexec.c
+++ b/security/integrity/ima/ima_kexec.c
@@ -15,26 +15,46 @@
 #include "ima.h"
=20
 #ifdef CONFIG_IMA_KEXEC
+static struct seq_file ima_kexec_file;
+
+static void ima_free_kexec_file_buf(struct seq_file *sf)
+{
+	vfree(sf->buf);
+	sf->buf =3D NULL;
+	sf->size =3D 0;
+	sf->read_pos =3D 0;
+	sf->count =3D 0;
+}
+
+static int ima_alloc_kexec_file_buf(size_t segment_size)
+{
+	ima_free_kexec_file_buf(&ima_kexec_file);
+
+	/* segment size can't change between kexec load and execute */
+	ima_kexec_file.buf =3D vmalloc(segment_size);
+	if (!ima_kexec_file.buf)
+		return -ENOMEM;
+
+	ima_kexec_file.size =3D segment_size;
+	ima_kexec_file.read_pos =3D 0;
+	ima_kexec_file.count =3D sizeof(struct ima_kexec_hdr);	/* reserved space =
*/
+
+	return 0;
+}
+
 static int ima_dump_measurement_list(unsigned long *buffer_size, void **bu=
ffer,
 				     unsigned long segment_size)
 {
-	struct seq_file ima_kexec_file;
 	struct ima_queue_entry *qe;
 	struct ima_kexec_hdr khdr;
 	int ret =3D 0;
=20
 	/* segment size can't change between kexec load and execute */
-	ima_kexec_file.buf =3D vmalloc(segment_size);
 	if (!ima_kexec_file.buf) {
-		ret =3D -ENOMEM;
-		goto out;
+		pr_err("Kexec file buf not allocated\n");
+		return -EINVAL;
 	}
=20
-	ima_kexec_file.file =3D NULL;
-	ima_kexec_file.size =3D segment_size;
-	ima_kexec_file.read_pos =3D 0;
-	ima_kexec_file.count =3D sizeof(khdr);	/* reserved space */
-
 	memset(&khdr, 0, sizeof(khdr));
 	khdr.version =3D 1;
 	/* This is an append-only list, no need to hold the RCU read lock */
@@ -71,8 +91,6 @@ static int ima_dump_measurement_list(unsigned long *buffe=
r_size, void **buffer,
 	*buffer_size =3D ima_kexec_file.count;
 	*buffer =3D ima_kexec_file.buf;
 out:
-	if (ret =3D=3D -EINVAL)
-		vfree(ima_kexec_file.buf);
 	return ret;
 }
=20
@@ -111,6 +129,12 @@ void ima_add_kexec_buffer(struct kimage *image)
 		return;
 	}
=20
+	ret =3D ima_alloc_kexec_file_buf(kexec_segment_size);
+	if (ret < 0) {
+		pr_err("Not enough memory for the kexec measurement buffer.\n");
+		return;
+	}
+
 	ima_dump_measurement_list(&kexec_buffer_size, &kexec_buffer,
 				  kexec_segment_size);
 	if (!kexec_buffer) {
--=20
2.25.1
From nobody Wed May  7 05:47:36 2025
Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id C94AD23C8AD;
	Wed,  2 Apr 2025 12:47:43 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
 arc=none smtp.client-ip=13.77.154.182
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1743598065; cv=none;
 b=Sb+Q454HEick9621piQiddy00F8vfxonnvnvdY8k6APvDnA9krYtGtWtfybBaE9PUZppAMk8zR2ZEsc4enAcv5HmsvJt13npOix2UbrM1g9zhccVsMNrUbQp/LkLy/6o6ikF6YM7tV8pR6FTG/Khxw5MiFJUYN2469woMmwam9o=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1743598065; c=relaxed/simple;
	bh=sObjz7SqWq+sFEj3YUzSHtEK4BCJvAAQ5QgRpbWXFCU=;
	h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
	 MIME-Version;
 b=hKP5KAMIwRZdwJ0x7BXJmxanlnJkXO1oEGDLsUGPbSwkqZLgbwpONnkZw/4IjtyOOir4tGsQjQP8p4zX1TM2eJCYAWsfDh2opP19z5PY4dagNPYQ7wTr1Q40xSQYvCdSRPFQ8Dj5NeaWZ+7TzNxTdDTYtnp665zzthIl8G2I5g0=
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com;
 spf=pass smtp.mailfrom=linux.microsoft.com;
 dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b=N0z7C6O6;
 arc=none smtp.client-ip=13.77.154.182
Authentication-Results: smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
 spf=pass smtp.mailfrom=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b="N0z7C6O6"
Received: from DESKTOP-VOT081N.hsd1.ga.comcast.net (unknown [20.114.144.49])
	by linux.microsoft.com (Postfix) with ESMTPSA id 5AB832041314;
	Wed,  2 Apr 2025 05:47:41 -0700 (PDT)
DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 5AB832041314
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;
	s=default; t=1743598063;
	bh=pL3ZRyfio2uIX3TrzfP4lwxC86hOBPNScu2MCFNBrag=;
	h=From:To:Cc:Subject:Date:In-Reply-To:References:From;
	b=N0z7C6O6DQzbIyvrwGX05zSK0RLYv9bQfd7pijoDyzANgXnejWRJlKt7Zw7j2vcG0
	 eIuIPl4SSoThDxG282nMSlyldt5q4hopNmiXM25x6DBtRP2Ws/vMk+V8XZ8dhJT3AV
	 yxXuxieAfDmgkz97+P7odpZ+nSyNBcToweL3PyIg=
From: steven chen <chenste@linux.microsoft.com>
To: zohar@linux.ibm.com,
	stefanb@linux.ibm.com,
	roberto.sassu@huaweicloud.com,
	roberto.sassu@huawei.com,
	eric.snowberg@oracle.com,
	ebiederm@xmission.com,
	paul@paul-moore.com,
	code@tyhicks.com,
	bauermann@kolabnow.com,
	linux-integrity@vger.kernel.org,
	kexec@lists.infradead.org,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: madvenka@linux.microsoft.com,
	nramas@linux.microsoft.com,
	James.Bottomley@HansenPartnership.com,
	bhe@redhat.com,
	vgoyal@redhat.com,
	dyoung@redhat.com
Subject: [PATCH v11 3/9] kexec: define functions to map and unmap segments
Date: Wed,  2 Apr 2025 05:47:16 -0700
Message-ID: <20250402124725.5601-4-chenste@linux.microsoft.com>
X-Mailer: git-send-email 2.43.0
In-Reply-To: <20250402124725.5601-1-chenste@linux.microsoft.com>
References: <20250402124725.5601-1-chenste@linux.microsoft.com>
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id: <linux-kernel.vger.kernel.org>
List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="utf-8"

Currently, the kernel behavior during kexec load is to fetch the IMA
measurements logs and store logs in kernel memory. When a kexec reboot is
triggered, these stored logs in the kernel memory are carried over to the
second kernel. However, the time gap between kexec load and kexec reboot
can be very long. During this time window, new events extended into TPM=20
PCRs miss the chance to be carried over to the second kernel. This results=20
in a mismatch between TPM PCR quotes and the actual IMA measurements list
after kexec reboot, leading to remote attestation failure.

To solve this problem, the new design defers reading the IMA measurements
logs into the kexec buffer to the kexec reboot phase, while still allocating
the necessary buffer at kexec load time because it is not appropriate to=20
allocate memory at the kexec reboot moment.

The content of memory segments carried over to the new kernel during the
kexec system call can be changed at the kexec 'execute' stage, but the size
of the memory segments cannot be changed at the kexec 'execute' stage.

To copy IMA measurement logs during the kexec operation, IMA allocates=20
memory at the kexec 'load' stage and map the segments to the kimage=20
structure. The mapped address will then be used to copy IMA measurements=20
during the kexec 'execute' stage.

Currently, the mechanism to map and unmap segments to the kimage structure
is not available to subsystems outside of kexec.

Implement kimage_map_segment() to enable IMA to map the measurement log=20
list to the kimage structure during the kexec 'load' stage. This function
takes a kimage pointer, a memory address, and a size, then gathers the
source pages within the specified address range, creates an array of page
pointers, and maps these to a contiguous virtual address range. The
function returns the start virtual address of this range if successful,=20
or NULL on failure.

Implement kimage_unmap_segment() for unmapping segments using vunmap().

From: Tushar Sugandhi <tusharsu@linux.microsoft.com>
Signed-off-by: Tushar Sugandhi <tusharsu@linux.microsoft.com>
Cc: Eric Biederman <ebiederm@xmission.com>
Cc: Baoquan He <bhe@redhat.com>=20
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Dave Young <dyoung@redhat.com>
Signed-off-by: steven chen <chenste@linux.microsoft.com>
Acked-by: Baoquan He <bhe@redhat.com>
---
 include/linux/kexec.h |  6 +++++
 kernel/kexec_core.c   | 54 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/include/linux/kexec.h b/include/linux/kexec.h
index f0e9f8eda7a3..7d6b12f8b8d0 100644
--- a/include/linux/kexec.h
+++ b/include/linux/kexec.h
@@ -467,13 +467,19 @@ extern bool kexec_file_dbg_print;
 #define kexec_dprintk(fmt, arg...) \
         do { if (kexec_file_dbg_print) pr_info(fmt, ##arg); } while (0)
=20
+extern void *kimage_map_segment(struct kimage *image, unsigned long addr, =
unsigned long size);
+extern void kimage_unmap_segment(void *buffer);
 #else /* !CONFIG_KEXEC_CORE */
 struct pt_regs;
 struct task_struct;
+struct kimage;
 static inline void __crash_kexec(struct pt_regs *regs) { }
 static inline void crash_kexec(struct pt_regs *regs) { }
 static inline int kexec_should_crash(struct task_struct *p) { return 0; }
 static inline int kexec_crash_loaded(void) { return 0; }
+static inline void *kimage_map_segment(struct kimage *image, unsigned long=
 addr, unsigned long size)
+{ return NULL; }
+static inline void kimage_unmap_segment(void *buffer) { }
 #define kexec_in_progress false
 #endif /* CONFIG_KEXEC_CORE */
=20
diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c
index c0bdc1686154..a5e378e1dc7f 100644
--- a/kernel/kexec_core.c
+++ b/kernel/kexec_core.c
@@ -867,6 +867,60 @@ int kimage_load_segment(struct kimage *image,
 	return result;
 }
=20
+void *kimage_map_segment(struct kimage *image,
+			 unsigned long addr, unsigned long size)
+{
+	unsigned long src_page_addr, dest_page_addr =3D 0;
+	unsigned long eaddr =3D addr + size;
+	kimage_entry_t *ptr, entry;
+	struct page **src_pages;
+	unsigned int npages;
+	void *vaddr =3D NULL;
+	int i;
+
+	/*
+	 * Collect the source pages and map them in a contiguous VA range.
+	 */
+	npages =3D PFN_UP(eaddr) - PFN_DOWN(addr);
+	src_pages =3D kmalloc_array(npages, sizeof(*src_pages), GFP_KERNEL);
+	if (!src_pages) {
+		pr_err("Could not allocate ima pages array.\n");
+		return NULL;
+	}
+
+	i =3D 0;
+	for_each_kimage_entry(image, ptr, entry) {
+		if (entry & IND_DESTINATION) {
+			dest_page_addr =3D entry & PAGE_MASK;
+		} else if (entry & IND_SOURCE) {
+			if (dest_page_addr >=3D addr && dest_page_addr < eaddr) {
+				src_page_addr =3D entry & PAGE_MASK;
+				src_pages[i++] =3D
+					virt_to_page(__va(src_page_addr));
+				if (i =3D=3D npages)
+					break;
+				dest_page_addr +=3D PAGE_SIZE;
+			}
+		}
+	}
+
+	/* Sanity check. */
+	WARN_ON(i < npages);
+
+	vaddr =3D vmap(src_pages, npages, VM_MAP, PAGE_KERNEL);
+	kfree(src_pages);
+
+	if (!vaddr)
+		pr_err("Could not map ima buffer.\n");
+
+	return vaddr;
+}
+
+void kimage_unmap_segment(void *segment_buffer)
+{
+	vunmap(segment_buffer);
+}
+
 struct kexec_load_limit {
 	/* Mutex protects the limit count. */
 	struct mutex mutex;
--=20
2.25.1
From nobody Wed May  7 05:47:36 2025
Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 12AB823CEE7;
	Wed,  2 Apr 2025 12:47:46 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
 arc=none smtp.client-ip=13.77.154.182
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1743598067; cv=none;
 b=G3EmwY6Z8BiiMBESsn3SGQFGomR4P2d7jutXoFj/DgH3lRdeZppdc3/QZrVePgTikDeDIPbyMZcHIc40f0tlXddVx4bbw91izYmKp9Qk4T+98kqEloa5veICSQXPGsSVAVz7XXKciaRdSX+i+k9NZD6guI7LpG7lk7DqFymd8G4=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1743598067; c=relaxed/simple;
	bh=T3Hfr90BRed4n5w5wi1LRWAKc4YqrbCTD1TtzLonDDk=;
	h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
	 MIME-Version;
 b=DhZ4IiaBTytuQZ0aGgoQm43tNhVrNsUoIhvyYltzfyPQRBlFG2bwAm34/6OTxCQkA47JTvxjoiOqKzHMZqbcYQT52eLpmPV6mBQDFLUjMJfA4HsqZfacm35jat6PKfCjO0Jk/A+NZUxs0IyQUeb08wzOHMO+V3YVp4gqzCoPFls=
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com;
 spf=pass smtp.mailfrom=linux.microsoft.com;
 dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b=VWtKRD9h;
 arc=none smtp.client-ip=13.77.154.182
Authentication-Results: smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
 spf=pass smtp.mailfrom=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b="VWtKRD9h"
Received: from DESKTOP-VOT081N.hsd1.ga.comcast.net (unknown [20.114.144.49])
	by linux.microsoft.com (Postfix) with ESMTPSA id CA1142041313;
	Wed,  2 Apr 2025 05:47:43 -0700 (PDT)
DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com CA1142041313
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;
	s=default; t=1743598065;
	bh=zDG3eUVOxWNYbh+dKB76Ss704ucd6Sf4P9j4KBe+zSk=;
	h=From:To:Cc:Subject:Date:In-Reply-To:References:From;
	b=VWtKRD9hs4KfHbL785HIVTK2K+oSUajcz5knH7cam+io/9ERQFTln+MaK6zxgpUe8
	 w+p4RRlXVLVHs3C0yt5qquaQWKblcp+FyQYDEIjUP1k/S1RPRdrhUVpUxu5wy1rLNP
	 4PStfWXTyTP+uHwUZx0zrzoGkf1x2LZW3D8TbnQs=
From: steven chen <chenste@linux.microsoft.com>
To: zohar@linux.ibm.com,
	stefanb@linux.ibm.com,
	roberto.sassu@huaweicloud.com,
	roberto.sassu@huawei.com,
	eric.snowberg@oracle.com,
	ebiederm@xmission.com,
	paul@paul-moore.com,
	code@tyhicks.com,
	bauermann@kolabnow.com,
	linux-integrity@vger.kernel.org,
	kexec@lists.infradead.org,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: madvenka@linux.microsoft.com,
	nramas@linux.microsoft.com,
	James.Bottomley@HansenPartnership.com,
	bhe@redhat.com,
	vgoyal@redhat.com,
	dyoung@redhat.com
Subject: [PATCH v11 4/9] ima: kexec: skip IMA segment validation after kexec
 soft reboot
Date: Wed,  2 Apr 2025 05:47:17 -0700
Message-ID: <20250402124725.5601-5-chenste@linux.microsoft.com>
X-Mailer: git-send-email 2.43.0
In-Reply-To: <20250402124725.5601-1-chenste@linux.microsoft.com>
References: <20250402124725.5601-1-chenste@linux.microsoft.com>
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id: <linux-kernel.vger.kernel.org>
List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="utf-8"

The kexec_calculate_store_digests() function calculates and stores the
digest of the segment during the kexec_file_load syscall, where the=20
IMA segment is also allocated.

With this series, the IMA segment will be updated with the measurement=20
log at the kexec execute stage when a soft reboot is initiated.=20
Therefore, the digests should be updated for the IMA segment in the=20
normal case.

The content of memory segments carried over to the new kernel during the
kexec systemcall can be changed at kexec 'execute' stage, but the size
and the location of the memory segments cannot be changed at kexec
'execute' stage.

However, during the kexec execute stage, if kexec_calculate_store_digests()
API is called to update the digest, it does not reuse the same memory
segment allocated during the kexec 'load' stage and the new memory segment
required cannot be transferred/mapped to the new kernel.

As a result, digest verification will fail in verify_sha256_digest()=20
after a kexec soft reboot into the new kernel. Therefore, the digest
calculation/verification of the IMA segment needs to be skipped.

To address this, skip the calculation and storage of the digest for the
IMA segment in kexec_calculate_store_digests() so that it is not added=20
to the purgatory_sha_regions.

Since verify_sha256_digest() only verifies 'purgatory_sha_regions',
no change is needed in verify_sha256_digest() in this context.

With this change, the IMA segment is not included in the digest
calculation, storage, and verification.

Signed-off-by: Tushar Sugandhi <tusharsu@linux.microsoft.com>
Cc: Eric Biederman <ebiederm@xmission.com>
Cc: Baoquan He <bhe@redhat.com>=20
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Dave Young <dyoung@redhat.com>
Signed-off-by: steven chen <chenste@linux.microsoft.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
Acked-by: Baoquan He <bhe@redhat.com>
---
 include/linux/kexec.h              |  3 +++
 kernel/kexec_file.c                | 22 ++++++++++++++++++++++
 security/integrity/ima/ima_kexec.c |  3 +++
 3 files changed, 28 insertions(+)

diff --git a/include/linux/kexec.h b/include/linux/kexec.h
index 7d6b12f8b8d0..107e726f2ef3 100644
--- a/include/linux/kexec.h
+++ b/include/linux/kexec.h
@@ -362,6 +362,9 @@ struct kimage {
=20
 	phys_addr_t ima_buffer_addr;
 	size_t ima_buffer_size;
+
+	unsigned long ima_segment_index;
+	bool is_ima_segment_index_set;
 #endif
=20
 	/* Core ELF header buffer */
diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 3eedb8c226ad..606132253c79 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -38,6 +38,21 @@ void set_kexec_sig_enforced(void)
 }
 #endif
=20
+#ifdef CONFIG_IMA_KEXEC
+static bool check_ima_segment_index(struct kimage *image, int i)
+{
+	if (image->is_ima_segment_index_set && i =3D=3D image->ima_segment_index)
+		return true;
+	else
+		return false;
+}
+#else
+static bool check_ima_segment_index(struct kimage *image, int i)
+{
+	return false;
+}
+#endif
+
 static int kexec_calculate_store_digests(struct kimage *image);
=20
 /* Maximum size in bytes for kernel/initrd files. */
@@ -764,6 +779,13 @@ static int kexec_calculate_store_digests(struct kimage=
 *image)
 		if (ksegment->kbuf =3D=3D pi->purgatory_buf)
 			continue;
=20
+		/*
+		 * Skip the segment if ima_segment_index is set and matches
+		 * the current index
+		 */
+		if (check_ima_segment_index(image, i))
+			continue;
+
 		ret =3D crypto_shash_update(desc, ksegment->kbuf,
 					  ksegment->bufsz);
 		if (ret)
diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/im=
a_kexec.c
index b12ac3619b8f..7e0a19c3483f 100644
--- a/security/integrity/ima/ima_kexec.c
+++ b/security/integrity/ima/ima_kexec.c
@@ -145,6 +145,7 @@ void ima_add_kexec_buffer(struct kimage *image)
 	kbuf.buffer =3D kexec_buffer;
 	kbuf.bufsz =3D kexec_buffer_size;
 	kbuf.memsz =3D kexec_segment_size;
+	image->is_ima_segment_index_set =3D false;
 	ret =3D kexec_add_buffer(&kbuf);
 	if (ret) {
 		pr_err("Error passing over kexec measurement buffer.\n");
@@ -155,6 +156,8 @@ void ima_add_kexec_buffer(struct kimage *image)
 	image->ima_buffer_addr =3D kbuf.mem;
 	image->ima_buffer_size =3D kexec_segment_size;
 	image->ima_buffer =3D kexec_buffer;
+	image->ima_segment_index =3D image->nr_segments - 1;
+	image->is_ima_segment_index_set =3D true;
=20
 	kexec_dprintk("kexec measurement buffer for the loaded kernel at 0x%lx.\n=
",
 		      kbuf.mem);
--=20
2.25.1
From nobody Wed May  7 05:47:36 2025
Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id CA95623E321;
	Wed,  2 Apr 2025 12:47:48 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
 arc=none smtp.client-ip=13.77.154.182
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1743598070; cv=none;
 b=ZceM3prtnm/F/zGqubCmGCGSYys2cx4G6Ll3qzw2MF7WwCRvEoFKOYG9ZSeJA9H5Sk92RRUtbBxhSUscACJo4YOB1Allr6p1ENV+AlRqCNzpHJheGd1AliEIao20uluy7lDlRVWfFI7Bl6XuMKVOOP2BsnSx83FAciISgKB/+nw=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1743598070; c=relaxed/simple;
	bh=1kvwnO3H6Jss1uxMPsuR/y6ko+f8O5oM2Dd+ugazDhs=;
	h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
	 MIME-Version;
 b=td3x415c1uYZfmgnX5oBlvbpHd5ZX7gcmrq8HnL1ZUPQ9jsU4Q9R0S7QIYY0LzZsCj2MXGIZ/RfbQDexDsiI7ratETClV51wkfXAdIucUBQJGMBMdFA9KYR24sLeqFLXJdOeIBe/s1EhDXad0Ttf6LXq5IEvOneZGZveCV26UsE=
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com;
 spf=pass smtp.mailfrom=linux.microsoft.com;
 dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b=P5T2QWeJ;
 arc=none smtp.client-ip=13.77.154.182
Authentication-Results: smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
 spf=pass smtp.mailfrom=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b="P5T2QWeJ"
Received: from DESKTOP-VOT081N.hsd1.ga.comcast.net (unknown [20.114.144.49])
	by linux.microsoft.com (Postfix) with ESMTPSA id 52A4B2041315;
	Wed,  2 Apr 2025 05:47:46 -0700 (PDT)
DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 52A4B2041315
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;
	s=default; t=1743598068;
	bh=c7xlCRrPrh0uSEOIDsBq+I5m+1pDMPQECAnp8rbF0oQ=;
	h=From:To:Cc:Subject:Date:In-Reply-To:References:From;
	b=P5T2QWeJRhqYxLPKlnx5GmyWPp1wX7x9pWcSJBteHGJestyj/PjJimXt+rwAnyEgJ
	 XUsoXOkWgrCzG8b+Lavk7cqtnpzu0QiJuXdWyevgdzYeNF/hEoo84rG90F1YS6mSm/
	 WQZExhNNptEGi8pirH5PbuJM2sI8Jt857nBESSL0=
From: steven chen <chenste@linux.microsoft.com>
To: zohar@linux.ibm.com,
	stefanb@linux.ibm.com,
	roberto.sassu@huaweicloud.com,
	roberto.sassu@huawei.com,
	eric.snowberg@oracle.com,
	ebiederm@xmission.com,
	paul@paul-moore.com,
	code@tyhicks.com,
	bauermann@kolabnow.com,
	linux-integrity@vger.kernel.org,
	kexec@lists.infradead.org,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: madvenka@linux.microsoft.com,
	nramas@linux.microsoft.com,
	James.Bottomley@HansenPartnership.com,
	bhe@redhat.com,
	vgoyal@redhat.com,
	dyoung@redhat.com
Subject: [PATCH v11 5/9] ima: kexec: define functions to copy IMA log at soft
 boot
Date: Wed,  2 Apr 2025 05:47:18 -0700
Message-ID: <20250402124725.5601-6-chenste@linux.microsoft.com>
X-Mailer: git-send-email 2.43.0
In-Reply-To: <20250402124725.5601-1-chenste@linux.microsoft.com>
References: <20250402124725.5601-1-chenste@linux.microsoft.com>
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id: <linux-kernel.vger.kernel.org>
List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="utf-8"

The IMA log is currently copied to the new kernel during kexec 'load'=20
using ima_dump_measurement_list(). However, the log copied at kexec=20
'load' may result in loss of IMA measurements that only occurred after
kexec "load'. Therefore, the log needs to be copied during kexec=20
'execute'. Setup the needed infrastructure to move the IMA log copy from
kexec 'load' to 'execute'.

Define a new IMA hook ima_update_kexec_buffer() as a stub function.
It will be used to call ima_dump_measurement_list() during kexec 'execute'.

Implement ima_kexec_post_load() function to be invoked after the new=20
Kernel image has been loaded for kexec. ima_kexec_post_load() maps the=20
IMA buffer to a segment in the newly loaded Kernel.  It also registers=20
the reboot notifier_block to trigger ima_update_kexec_buffer() at=20
kexec 'execute'.

Set the priority of register_reboot_notifier to INT_MIN to ensure that the
IMA log copy operation will happen at the end of the operation chain, which
is crucial for maintaining the integrity of the logs

Signed-off-by: Tushar Sugandhi <tusharsu@linux.microsoft.com>
Cc: Eric Biederman <ebiederm@xmission.com>
Cc: Baoquan He <bhe@redhat.com>=20
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Dave Young <dyoung@redhat.com>
Signed-off-by: steven chen <chenste@linux.microsoft.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
---
 include/linux/ima.h                |  3 ++
 security/integrity/ima/ima_kexec.c | 47 ++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/include/linux/ima.h b/include/linux/ima.h
index 0bae61a15b60..8e29cb4e6a01 100644
--- a/include/linux/ima.h
+++ b/include/linux/ima.h
@@ -32,6 +32,9 @@ static inline void ima_appraise_parse_cmdline(void) {}
=20
 #ifdef CONFIG_IMA_KEXEC
 extern void ima_add_kexec_buffer(struct kimage *image);
+extern void ima_kexec_post_load(struct kimage *image);
+#else
+static inline void ima_kexec_post_load(struct kimage *image) {}
 #endif
=20
 #else
diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/im=
a_kexec.c
index 7e0a19c3483f..e79f6caf895b 100644
--- a/security/integrity/ima/ima_kexec.c
+++ b/security/integrity/ima/ima_kexec.c
@@ -12,10 +12,14 @@
 #include <linux/kexec.h>
 #include <linux/of.h>
 #include <linux/ima.h>
+#include <linux/reboot.h>
+#include <asm/page.h>
 #include "ima.h"
=20
 #ifdef CONFIG_IMA_KEXEC
+static bool ima_kexec_update_registered;
 static struct seq_file ima_kexec_file;
+static void *ima_kexec_buffer;
=20
 static void ima_free_kexec_file_buf(struct seq_file *sf)
 {
@@ -162,6 +166,49 @@ void ima_add_kexec_buffer(struct kimage *image)
 	kexec_dprintk("kexec measurement buffer for the loaded kernel at 0x%lx.\n=
",
 		      kbuf.mem);
 }
+
+/*
+ * Called during kexec execute so that IMA can update the measurement list.
+ */
+static int ima_update_kexec_buffer(struct notifier_block *self,
+				   unsigned long action, void *data)
+{
+	return NOTIFY_OK;
+}
+
+static struct notifier_block update_buffer_nb =3D {
+	.notifier_call =3D ima_update_kexec_buffer,
+	.priority =3D INT_MIN
+};
+
+/*
+ * Create a mapping for the source pages that contain the IMA buffer
+ * so we can update it later.
+ */
+void ima_kexec_post_load(struct kimage *image)
+{
+	if (ima_kexec_buffer) {
+		kimage_unmap_segment(ima_kexec_buffer);
+		ima_kexec_buffer =3D NULL;
+	}
+
+	if (!image->ima_buffer_addr)
+		return;
+
+	ima_kexec_buffer =3D kimage_map_segment(image,
+					      image->ima_buffer_addr,
+					      image->ima_buffer_size);
+	if (!ima_kexec_buffer) {
+		pr_err("Could not map measurements buffer.\n");
+		return;
+	}
+
+	if (!ima_kexec_update_registered) {
+		register_reboot_notifier(&update_buffer_nb);
+		ima_kexec_update_registered =3D true;
+	}
+}
+
 #endif /* IMA_KEXEC */
=20
 /*
--=20
2.25.1
From nobody Wed May  7 05:47:36 2025
Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 4ABE623A9BB;
	Wed,  2 Apr 2025 12:47:51 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
 arc=none smtp.client-ip=13.77.154.182
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1743598072; cv=none;
 b=At+bhczFbqPqToJV4+Z9809ZeyB+Y+Bz/RwCBpd2/IOBbj/eZ0URBViAQprYDwKFMmVNKQqYrb9lNwcT4vadFl2iPiV/VxOW0Uhr/sXssi1F1vjE3MwlaHE3SPn9XgLB8PgOJU+WB+KsumGhec8EIEQ7ZFHuxwPoE3ChS3DhVe8=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1743598072; c=relaxed/simple;
	bh=UJARWayehLsjZgGqf0RmGLlvGyEW1VUvommtCqeX4Sg=;
	h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
	 MIME-Version;
 b=HSQyYGzNXqzplFPN8mMq1dhLT3F4hu9IOaTCDb4LazFF9bvcegLTGj+XhmjAzJ9/VHoz1kg2QtBaw1E8unF5YOFZcbx2VCJ2ZLRGNndwIXBzf5oLtq/N0F9/4p9T0bu39/RjASncIYGvAPN7ZmPZgFHlSQ5x6Qw8B6d1nJLSi+s=
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com;
 spf=pass smtp.mailfrom=linux.microsoft.com;
 dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b=GtRTizlI;
 arc=none smtp.client-ip=13.77.154.182
Authentication-Results: smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
 spf=pass smtp.mailfrom=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b="GtRTizlI"
Received: from DESKTOP-VOT081N.hsd1.ga.comcast.net (unknown [20.114.144.49])
	by linux.microsoft.com (Postfix) with ESMTPSA id D4A412041304;
	Wed,  2 Apr 2025 05:47:48 -0700 (PDT)
DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com D4A412041304
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;
	s=default; t=1743598070;
	bh=vEw/LreqQOxRk2U8fSqzR4thwxp7A3QOtHmi4q7KOMw=;
	h=From:To:Cc:Subject:Date:In-Reply-To:References:From;
	b=GtRTizlIYLiPu/gEQNWmihR/pySHx3nmYZ4ceLusbrt3hTu79kPiO3ZEhNoVY1l/F
	 gwibjdMU9ojf1gSoope+Up2tNXS+wzmwkZl5KDK/RVzcVpxgFubtvGqdAj7uJUTlh5
	 r+8O74/ReycP1R6AIAhUH6H4Y3XqLPWUrk+sJlck=
From: steven chen <chenste@linux.microsoft.com>
To: zohar@linux.ibm.com,
	stefanb@linux.ibm.com,
	roberto.sassu@huaweicloud.com,
	roberto.sassu@huawei.com,
	eric.snowberg@oracle.com,
	ebiederm@xmission.com,
	paul@paul-moore.com,
	code@tyhicks.com,
	bauermann@kolabnow.com,
	linux-integrity@vger.kernel.org,
	kexec@lists.infradead.org,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: madvenka@linux.microsoft.com,
	nramas@linux.microsoft.com,
	James.Bottomley@HansenPartnership.com,
	bhe@redhat.com,
	vgoyal@redhat.com,
	dyoung@redhat.com
Subject: [PATCH v11 6/9] ima: kexec: move IMA log copy from kexec load to
 execute
Date: Wed,  2 Apr 2025 05:47:19 -0700
Message-ID: <20250402124725.5601-7-chenste@linux.microsoft.com>
X-Mailer: git-send-email 2.43.0
In-Reply-To: <20250402124725.5601-1-chenste@linux.microsoft.com>
References: <20250402124725.5601-1-chenste@linux.microsoft.com>
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id: <linux-kernel.vger.kernel.org>
List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="utf-8"

ima_dump_measurement_list() is called during kexec 'load', which may
result in loss of IMA measurements during kexec soft reboot. Due to=20
missed measurements that only occurred after kexec 'load', this function=20
needs to be called during kexec 'execute'.

Make the kexec_segment_size variable a local static variable within the=20
file, so it can be accessed during both kexec 'load' and 'execute'.
=20
Implement the kexec_post_load() function to be invoked after the new kernel
image has been loaded for kexec. Instead of calling machine_kexec_post_load=
()
directly from the kexec_file_load() syscall, call kexec_post_load(), which =
in
turn calls machine_kexec_post_load() to maintain the original image process=
ing.
=20
Invoke ima_kexec_post_load() within the kexec_post_load() API only for kexe=
c=20
soft reboot scenarios, excluding KEXEC_FILE_ON_CRASH.
=20
Register a reboot notifier for the ima_update_kexec_buffer() API within=20
ima_kexec_post_load() to ensure it is called upon receiving a reboot=20
notification.
=20
Move the ima_dump_measurement_list() call from ima_add_kexec_buffer() to=20
ima_update_kexec_buffer() to copy the IMA log at the kexec 'execute' stage.
=20
When there is insufficient memory to copy all the measurement logs, copy as
much of the measurement list as possible.

Signed-off-by: Tushar Sugandhi <tusharsu@linux.microsoft.com>
Cc: Eric Biederman <ebiederm@xmission.com>
Cc: Baoquan He <bhe@redhat.com>=20
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: Dave Young <dyoung@redhat.com>
Signed-off-by: steven chen <chenste@linux.microsoft.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
---
 kernel/kexec_file.c                | 11 +++++++-
 security/integrity/ima/ima_kexec.c | 43 ++++++++++++++++++++----------
 2 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 606132253c79..b3eb515ca051 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -201,6 +201,15 @@ kimage_validate_signature(struct kimage *image)
 }
 #endif
=20
+static int kexec_post_load(struct kimage *image, unsigned long flags)
+{
+#ifdef CONFIG_IMA_KEXEC
+	if (!(flags & KEXEC_FILE_ON_CRASH))
+		ima_kexec_post_load(image);
+#endif
+	return machine_kexec_post_load(image);
+}
+
 /*
  * In file mode list of segments is prepared by kernel. Copy relevant
  * data from user space, do error checking, prepare segment list
@@ -428,7 +437,7 @@ SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, i=
nitrd_fd,
=20
 	kimage_terminate(image);
=20
-	ret =3D machine_kexec_post_load(image);
+	ret =3D kexec_post_load(image, flags);
 	if (ret)
 		goto out;
=20
diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/im=
a_kexec.c
index e79f6caf895b..5c3b3e0b2186 100644
--- a/security/integrity/ima/ima_kexec.c
+++ b/security/integrity/ima/ima_kexec.c
@@ -19,6 +19,7 @@
 #ifdef CONFIG_IMA_KEXEC
 static bool ima_kexec_update_registered;
 static struct seq_file ima_kexec_file;
+static size_t kexec_segment_size;
 static void *ima_kexec_buffer;
=20
 static void ima_free_kexec_file_buf(struct seq_file *sf)
@@ -72,9 +73,6 @@ static int ima_dump_measurement_list(unsigned long *buffe=
r_size, void **buffer,
 		}
 	}
=20
-	if (ret < 0)
-		goto out;
-
 	/*
 	 * fill in reserved space with some buffer details
 	 * (eg. version, buffer size, number of measurements)
@@ -94,7 +92,7 @@ static int ima_dump_measurement_list(unsigned long *buffe=
r_size, void **buffer,
=20
 	*buffer_size =3D ima_kexec_file.count;
 	*buffer =3D ima_kexec_file.buf;
-out:
+
 	return ret;
 }
=20
@@ -112,9 +110,8 @@ void ima_add_kexec_buffer(struct kimage *image)
 	unsigned long binary_runtime_size;
=20
 	/* use more understandable variable names than defined in kbuf */
+	size_t kexec_buffer_size =3D 0;
 	void *kexec_buffer =3D NULL;
-	size_t kexec_buffer_size;
-	size_t kexec_segment_size;
 	int ret;
=20
 	/*
@@ -139,13 +136,6 @@ void ima_add_kexec_buffer(struct kimage *image)
 		return;
 	}
=20
-	ima_dump_measurement_list(&kexec_buffer_size, &kexec_buffer,
-				  kexec_segment_size);
-	if (!kexec_buffer) {
-		pr_err("Not enough memory for the kexec measurement buffer.\n");
-		return;
-	}
-
 	kbuf.buffer =3D kexec_buffer;
 	kbuf.bufsz =3D kexec_buffer_size;
 	kbuf.memsz =3D kexec_segment_size;
@@ -173,7 +163,32 @@ void ima_add_kexec_buffer(struct kimage *image)
 static int ima_update_kexec_buffer(struct notifier_block *self,
 				   unsigned long action, void *data)
 {
-	return NOTIFY_OK;
+	size_t buf_size =3D 0;
+	int ret =3D NOTIFY_OK;
+	void *buf =3D NULL;
+
+	if (!kexec_in_progress) {
+		pr_info("No kexec in progress.\n");
+		return ret;
+	}
+
+	if (!ima_kexec_buffer) {
+		pr_err("Kexec buffer not set.\n");
+		return ret;
+	}
+
+	ret =3D ima_dump_measurement_list(&buf_size, &buf, kexec_segment_size);
+
+	if (ret)
+		pr_err("Dump measurements failed. Error:%d\n", ret);
+
+	if (buf_size !=3D 0)
+		memcpy(ima_kexec_buffer, buf, buf_size);
+
+	kimage_unmap_segment(ima_kexec_buffer);
+	ima_kexec_buffer =3D NULL;
+
+	return ret;
 }
=20
 static struct notifier_block update_buffer_nb =3D {
--=20
2.25.1
From nobody Wed May  7 05:47:36 2025
Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id B6F0223AE6D;
	Wed,  2 Apr 2025 12:47:53 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
 arc=none smtp.client-ip=13.77.154.182
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1743598075; cv=none;
 b=enaVJ9ZOg0H1ZNsOcoEADkDV1zxHvvUp8gbvt4b8X+x29oYf3U3RcIs02FmTkPohLftpWkLsdE8ebcnU9ST4SsRqGwuy4ME4yLSGaffz8RSf4l0EQeFXovhG0+aFSwegVaegT5idN/N8uc2EADXnSqz89VhDYbXVdZtPzj+Qvto=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1743598075; c=relaxed/simple;
	bh=8zKS2SB/m2lwC0ERhyJbF2sczuoV87vLywnBYYhrQ0s=;
	h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
	 MIME-Version;
 b=CfmeeBkkNOHv6JnzrqeoEiTAxFrkptY8HtduNmfkcrzpN+nByMpYDeSopnf4xVW735QTMBZgPSfiLw/2UyTUszmCeoTr4G3+fHdY//D5cjhLSimzNYDOGslNABnUDb3rzMSMrZYT0IcIWB5sbZug12K1BkzrpFSCABfN/jVNj+U=
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com;
 spf=pass smtp.mailfrom=linux.microsoft.com;
 dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b=fm+qKJrV;
 arc=none smtp.client-ip=13.77.154.182
Authentication-Results: smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
 spf=pass smtp.mailfrom=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b="fm+qKJrV"
Received: from DESKTOP-VOT081N.hsd1.ga.comcast.net (unknown [20.114.144.49])
	by linux.microsoft.com (Postfix) with ESMTPSA id 552AB2025659;
	Wed,  2 Apr 2025 05:47:51 -0700 (PDT)
DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 552AB2025659
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;
	s=default; t=1743598073;
	bh=TIYkqv731cfeL17ltrsNIxcY2eH8QDnF2VgGdZyqueA=;
	h=From:To:Cc:Subject:Date:In-Reply-To:References:From;
	b=fm+qKJrVOUXOB6o0pxrl+eGZ/HXdBBwWrzeph9P1dlK5GSO1qXjnHXPMtOGHknSv1
	 3UWNES/7HuZmbIe5oaKBNlt8pX+yf1f+IokQowtBgO/lUJiGla6cr0wnK/m04PQJHV
	 srjip7h7MAtTReathD6kpxwApnASVV//qH2HqIJo=
From: steven chen <chenste@linux.microsoft.com>
To: zohar@linux.ibm.com,
	stefanb@linux.ibm.com,
	roberto.sassu@huaweicloud.com,
	roberto.sassu@huawei.com,
	eric.snowberg@oracle.com,
	ebiederm@xmission.com,
	paul@paul-moore.com,
	code@tyhicks.com,
	bauermann@kolabnow.com,
	linux-integrity@vger.kernel.org,
	kexec@lists.infradead.org,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: madvenka@linux.microsoft.com,
	nramas@linux.microsoft.com,
	James.Bottomley@HansenPartnership.com,
	bhe@redhat.com,
	vgoyal@redhat.com,
	dyoung@redhat.com
Subject: [PATCH v11 7/9] ima: verify if the segment size has changed
Date: Wed,  2 Apr 2025 05:47:20 -0700
Message-ID: <20250402124725.5601-8-chenste@linux.microsoft.com>
X-Mailer: git-send-email 2.43.0
In-Reply-To: <20250402124725.5601-1-chenste@linux.microsoft.com>
References: <20250402124725.5601-1-chenste@linux.microsoft.com>
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id: <linux-kernel.vger.kernel.org>
List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="utf-8"

kexec 'load' may be called multiple times. Free and realloc the buffer
only if the segment_size is changed from the previous kexec 'load' call.

Signed-off-by: steven chen <chenste@linux.microsoft.com>
Acked-by: Baoquan He <bhe@redhat.com>
Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
---
 security/integrity/ima/ima_kexec.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/im=
a_kexec.c
index 5c3b3e0b2186..ed867734ee70 100644
--- a/security/integrity/ima/ima_kexec.c
+++ b/security/integrity/ima/ima_kexec.c
@@ -33,6 +33,14 @@ static void ima_free_kexec_file_buf(struct seq_file *sf)
=20
 static int ima_alloc_kexec_file_buf(size_t segment_size)
 {
+	/*
+	 * kexec 'load' may be called multiple times.
+	 * Free and realloc the buffer only if the segment_size is
+	 * changed from the previous kexec 'load' call.
+	 */
+	if (ima_kexec_file.buf && ima_kexec_file.size =3D=3D segment_size)
+		goto out;
+
 	ima_free_kexec_file_buf(&ima_kexec_file);
=20
 	/* segment size can't change between kexec load and execute */
@@ -41,6 +49,8 @@ static int ima_alloc_kexec_file_buf(size_t segment_size)
 		return -ENOMEM;
=20
 	ima_kexec_file.size =3D segment_size;
+
+out:
 	ima_kexec_file.read_pos =3D 0;
 	ima_kexec_file.count =3D sizeof(struct ima_kexec_hdr);	/* reserved space =
*/
=20
--=20
2.25.1
From nobody Wed May  7 05:47:36 2025
Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 4DA3F23F26B;
	Wed,  2 Apr 2025 12:47:56 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
 arc=none smtp.client-ip=13.77.154.182
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1743598077; cv=none;
 b=FxnyM6VKZnjZsQet4L/GO1yMOTil0Ude9raKVoqqRiKc+GUAja4WD/VsoVpTSgflXgO+b0KBrnWZ97eX1P0pbMpT7mLXWq5Box3ynxqknfjqwSmrnSpFzy7A7FmzDaSlmiPX3Oj9uGhbJ3KiNx21OY1MSE4AierzPXVO3GDADpw=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1743598077; c=relaxed/simple;
	bh=CKWrEBn3PtQ8x/toSXWJ4Dxu6mytJnYkI6qhOJskz78=;
	h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
	 MIME-Version;
 b=F3LRuoS4q70BM2tdyR+GDtDQY21upW6ojYGu25uduIkVsNDTfefGSgRpnUmWljEFrqn+y0jho9GdHAA0KEfrOhwM0BxeS+FMwxKCtlyh8lho3urIVarUSZollpbL4m4rhSLGwDDTFHYhJMgvUSqi/KEeiorALtZtOlucd4yv9bE=
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com;
 spf=pass smtp.mailfrom=linux.microsoft.com;
 dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b=rNCzdP9d;
 arc=none smtp.client-ip=13.77.154.182
Authentication-Results: smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
 spf=pass smtp.mailfrom=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b="rNCzdP9d"
Received: from DESKTOP-VOT081N.hsd1.ga.comcast.net (unknown [20.114.144.49])
	by linux.microsoft.com (Postfix) with ESMTPSA id D3F0C2041310;
	Wed,  2 Apr 2025 05:47:53 -0700 (PDT)
DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com D3F0C2041310
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;
	s=default; t=1743598075;
	bh=akIJhi5dOsbPwjFtHYsWPzeWZ2NiSa/DdRwiLCQ9Fbg=;
	h=From:To:Cc:Subject:Date:In-Reply-To:References:From;
	b=rNCzdP9dHIJ0owIlx8+IhWD9u+n9u7qJfksu7jR/sDVdDhlBaAj9YGY+qIKJu6qe+
	 m0CV0LLATNeXBWXvKHF9nLIIdf6iyPRtCYZBOlSju/5NftIG59OdbEUSwNDpp6HgJ8
	 UlGkVOlj4WuolTyrf5WwPtYeb5Db5uqM8DxwDrec=
From: steven chen <chenste@linux.microsoft.com>
To: zohar@linux.ibm.com,
	stefanb@linux.ibm.com,
	roberto.sassu@huaweicloud.com,
	roberto.sassu@huawei.com,
	eric.snowberg@oracle.com,
	ebiederm@xmission.com,
	paul@paul-moore.com,
	code@tyhicks.com,
	bauermann@kolabnow.com,
	linux-integrity@vger.kernel.org,
	kexec@lists.infradead.org,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: madvenka@linux.microsoft.com,
	nramas@linux.microsoft.com,
	James.Bottomley@HansenPartnership.com,
	bhe@redhat.com,
	vgoyal@redhat.com,
	dyoung@redhat.com
Subject: [PATCH v11 8/9] ima: make the kexec extra memory configurable
Date: Wed,  2 Apr 2025 05:47:21 -0700
Message-ID: <20250402124725.5601-9-chenste@linux.microsoft.com>
X-Mailer: git-send-email 2.43.0
In-Reply-To: <20250402124725.5601-1-chenste@linux.microsoft.com>
References: <20250402124725.5601-1-chenste@linux.microsoft.com>
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id: <linux-kernel.vger.kernel.org>
List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="utf-8"

The extra memory allocated for carrying the IMA measurement list across
kexec is hard-coded as half a PAGE.  Make it configurable.

Define a Kconfig option, IMA_KEXEC_EXTRA_MEMORY_KB, to configure the
extra memory (in kb) to be allocated for IMA measurements added during
kexec soft reboot.  Ensure the default value of the option is set such
that extra half a page of memory for additional measurements is allocated
for the additional measurements.

Update ima_add_kexec_buffer() function to allocate memory based on the
Kconfig option value, rather than the currently hard-coded one.

Suggested-by: Stefan Berger <stefanb@linux.ibm.com>
Signed-off-by: Tushar Sugandhi <tusharsu@linux.microsoft.com>
Signed-off-by: steven chen <chenste@linux.microsoft.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
---
 security/integrity/ima/Kconfig     | 10 ++++++++++
 security/integrity/ima/ima_kexec.c | 16 +++++++++++-----
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/security/integrity/ima/Kconfig b/security/integrity/ima/Kconfig
index 475c32615006..d73c96c3c1c9 100644
--- a/security/integrity/ima/Kconfig
+++ b/security/integrity/ima/Kconfig
@@ -321,4 +321,14 @@ config IMA_DISABLE_HTABLE
 	help
 	   This option disables htable to allow measurement of duplicate records.
=20
+config IMA_KEXEC_EXTRA_MEMORY_KB
+	int "Extra memory for IMA measurements added during kexec soft reboot"
+	depends on IMA_KEXEC
+	default 0
+	help
+	  IMA_KEXEC_EXTRA_MEMORY_KB determines the extra memory to be
+	  allocated (in kb) for IMA measurements added during kexec soft reboot.
+	  If set to the default value of 0, an extra half page of memory for those
+	  additional measurements will be allocated.
+
 endif
diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/im=
a_kexec.c
index ed867734ee70..d1c9d369ba08 100644
--- a/security/integrity/ima/ima_kexec.c
+++ b/security/integrity/ima/ima_kexec.c
@@ -118,6 +118,7 @@ void ima_add_kexec_buffer(struct kimage *image)
 				  .buf_min =3D 0, .buf_max =3D ULONG_MAX,
 				  .top_down =3D true };
 	unsigned long binary_runtime_size;
+	unsigned long extra_memory;
=20
 	/* use more understandable variable names than defined in kbuf */
 	size_t kexec_buffer_size =3D 0;
@@ -125,15 +126,20 @@ void ima_add_kexec_buffer(struct kimage *image)
 	int ret;
=20
 	/*
-	 * Reserve an extra half page of memory for additional measurements
-	 * added during the kexec load.
+	 * Reserve extra memory for measurements added during kexec.
 	 */
-	binary_runtime_size =3D ima_get_binary_runtime_size();
+	if (CONFIG_IMA_KEXEC_EXTRA_MEMORY_KB <=3D 0)
+		extra_memory =3D PAGE_SIZE / 2;
+	else
+		extra_memory =3D CONFIG_IMA_KEXEC_EXTRA_MEMORY_KB * 1024;
+
+	binary_runtime_size =3D ima_get_binary_runtime_size() + extra_memory;
+
 	if (binary_runtime_size >=3D ULONG_MAX - PAGE_SIZE)
 		kexec_segment_size =3D ULONG_MAX;
 	else
-		kexec_segment_size =3D ALIGN(ima_get_binary_runtime_size() +
-					   PAGE_SIZE / 2, PAGE_SIZE);
+		kexec_segment_size =3D ALIGN(binary_runtime_size, PAGE_SIZE);
+
 	if ((kexec_segment_size =3D=3D ULONG_MAX) ||
 	    ((kexec_segment_size >> PAGE_SHIFT) > totalram_pages() / 2)) {
 		pr_err("Binary measurement list too large.\n");
--=20
2.25.1
From nobody Wed May  7 05:47:36 2025
Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 9CFEE23F41A;
	Wed,  2 Apr 2025 12:47:58 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
 arc=none smtp.client-ip=13.77.154.182
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1743598080; cv=none;
 b=CfZZuIW1v51xmrea21tR10IoWP/0UQQMcrUfcDV/2gXrLKspkisNZgMWfpJBlFd5Yoyr/ax3bROot/eOLrtj0gYCu+zf4FCSoj+dJ2R5Anr6V1DO1N8CNLM+2h1K0ZQ7cWfp6p899eysJ8inI/FxMGhGbOnnvPw04mmYHgW3Nek=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1743598080; c=relaxed/simple;
	bh=a7Y67Ezcs0uoJZtr8MYFzNKr69g69RsgDYsE1YLKUCg=;
	h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
	 MIME-Version;
 b=lOJ101VqV2IpNKD5Fxi0lnhKFzFu4cwbm7U/TvB5x6OEGIqFKwsaGsHnn+O5+kMvdyrRLwl0t7qulP6WzVF1gBqVgMiXojszMHlsJCr84ErLkYA5msx9SDJBaEFWfcEt129qOEISJ65bV2P6rSjibUzDPiK//q6nyjTthLb7i64=
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com;
 spf=pass smtp.mailfrom=linux.microsoft.com;
 dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b=kxGfbjaH;
 arc=none smtp.client-ip=13.77.154.182
Authentication-Results: smtp.subspace.kernel.org;
 dmarc=pass (p=none dis=none) header.from=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
 spf=pass smtp.mailfrom=linux.microsoft.com
Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key) header.d=linux.microsoft.com
 header.i=@linux.microsoft.com header.b="kxGfbjaH"
Received: from DESKTOP-VOT081N.hsd1.ga.comcast.net (unknown [20.114.144.49])
	by linux.microsoft.com (Postfix) with ESMTPSA id 5EB142041317;
	Wed,  2 Apr 2025 05:47:56 -0700 (PDT)
DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 5EB142041317
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com;
	s=default; t=1743598078;
	bh=hVxQ6Kg9GsCvIcW+Huga3a+Zb9j2IJqZ3DFDTnkF5rI=;
	h=From:To:Cc:Subject:Date:In-Reply-To:References:From;
	b=kxGfbjaHVRlu8UyhJav0auww1bCs3mcX4GvckI7MbbH+HB/yrsk9xwGHFlwmE7BMn
	 EQTD5z6Y3wdStplhmXYJOK6kv+Lf0z1WRKOIMV4teDxu5omEXaxhuHCGQxS87xahCg
	 woEjUMMFYYUlj70qdUpzo2FGiPfFkbo54ZbgFfAc=
From: steven chen <chenste@linux.microsoft.com>
To: zohar@linux.ibm.com,
	stefanb@linux.ibm.com,
	roberto.sassu@huaweicloud.com,
	roberto.sassu@huawei.com,
	eric.snowberg@oracle.com,
	ebiederm@xmission.com,
	paul@paul-moore.com,
	code@tyhicks.com,
	bauermann@kolabnow.com,
	linux-integrity@vger.kernel.org,
	kexec@lists.infradead.org,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: madvenka@linux.microsoft.com,
	nramas@linux.microsoft.com,
	James.Bottomley@HansenPartnership.com,
	bhe@redhat.com,
	vgoyal@redhat.com,
	dyoung@redhat.com
Subject: [PATCH v11 9/9] ima: measure kexec load and exec events as critical
 data
Date: Wed,  2 Apr 2025 05:47:22 -0700
Message-ID: <20250402124725.5601-10-chenste@linux.microsoft.com>
X-Mailer: git-send-email 2.43.0
In-Reply-To: <20250402124725.5601-1-chenste@linux.microsoft.com>
References: <20250402124725.5601-1-chenste@linux.microsoft.com>
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id: <linux-kernel.vger.kernel.org>
List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="utf-8"

The amount of memory allocated at kexec load, even with the extra memory
allocated, might not be large enough for the entire measurement list.  The
indeterminate interval between kexec 'load' and 'execute' could exacerbate
this problem.

Define two new IMA events, 'kexec_load' and 'kexec_execute', to be=20
measured as critical data at kexec 'load' and 'execute' respectively.
Report the allocated kexec segment size, IMA binary log size and the
runtime measurements count as part of those events.

These events, and the values reported through them, serve as markers in
the IMA log to verify the IMA events are captured during kexec soft
reboot.  The presence of a 'kexec_load' event in between the last two
'boot_aggregate' events in the IMA log implies this is a kexec soft
reboot, and not a cold-boot. And the absence of 'kexec_execute' event
after kexec soft reboot implies missing events in that window which
results in inconsistency with TPM PCR quotes, necessitating a cold boot
for a successful remote attestation.

These critical data events are displayed as hex encoded ascii in the
ascii_runtime_measurement_list.  Verifying the critical data hash requires=20
calculating the hash of the decoded ascii string. =20

For example, to verify the 'kexec_load' data hash:

sudo cat /sys/kernel/security/integrity/ima/ascii_runtime_measurements=20
| grep  kexec_load | cut -d' ' -f 6 | xxd -r -p | sha256sum


To verify the 'kexec_execute' data hash:

sudo cat /sys/kernel/security/integrity/ima/ascii_runtime_measurements=20
| grep kexec_execute | cut -d' ' -f 6 | xxd -r -p | sha256sum

Signed-off-by: Tushar Sugandhi <tusharsu@linux.microsoft.com>
Signed-off-by: steven chen <chenste@linux.microsoft.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
---
 security/integrity/ima/ima.h       |  6 ++++++
 security/integrity/ima/ima_kexec.c | 21 +++++++++++++++++++++
 security/integrity/ima/ima_queue.c |  5 +++++
 3 files changed, 32 insertions(+)

diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h
index 24d09ea91b87..34815baf5e21 100644
--- a/security/integrity/ima/ima.h
+++ b/security/integrity/ima/ima.h
@@ -240,6 +240,12 @@ void ima_post_key_create_or_update(struct key *keyring=
, struct key *key,
 				   unsigned long flags, bool create);
 #endif
=20
+#ifdef CONFIG_IMA_KEXEC
+void ima_measure_kexec_event(const char *event_name);
+#else
+static inline void ima_measure_kexec_event(const char *event_name) {}
+#endif
+
 /*
  * The default binary_runtime_measurements list format is defined as the
  * platform native format.  The canonical format is defined as little-endi=
an.
diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/im=
a_kexec.c
index d1c9d369ba08..38cb2500f4c3 100644
--- a/security/integrity/ima/ima_kexec.c
+++ b/security/integrity/ima/ima_kexec.c
@@ -17,6 +17,8 @@
 #include "ima.h"
=20
 #ifdef CONFIG_IMA_KEXEC
+#define IMA_KEXEC_EVENT_LEN 256
+
 static bool ima_kexec_update_registered;
 static struct seq_file ima_kexec_file;
 static size_t kexec_segment_size;
@@ -31,6 +33,24 @@ static void ima_free_kexec_file_buf(struct seq_file *sf)
 	sf->count =3D 0;
 }
=20
+void ima_measure_kexec_event(const char *event_name)
+{
+	char ima_kexec_event[IMA_KEXEC_EVENT_LEN];
+	size_t buf_size =3D 0;
+	long len;
+	int n;
+
+	buf_size =3D ima_get_binary_runtime_size();
+	len =3D atomic_long_read(&ima_htable.len);
+
+	n =3D scnprintf(ima_kexec_event, IMA_KEXEC_EVENT_LEN,
+		      "kexec_segment_size=3D%lu;ima_binary_runtime_size=3D%lu;"
+		      "ima_runtime_measurements_count=3D%ld;",
+		      kexec_segment_size, buf_size, len);
+
+	ima_measure_critical_data("ima_kexec", event_name, ima_kexec_event, n, fa=
lse, NULL, 0);
+}
+
 static int ima_alloc_kexec_file_buf(size_t segment_size)
 {
 	/*
@@ -53,6 +73,7 @@ static int ima_alloc_kexec_file_buf(size_t segment_size)
 out:
 	ima_kexec_file.read_pos =3D 0;
 	ima_kexec_file.count =3D sizeof(struct ima_kexec_hdr);	/* reserved space =
*/
+	ima_measure_kexec_event("kexec_load");
=20
 	return 0;
 }
diff --git a/security/integrity/ima/ima_queue.c b/security/integrity/ima/im=
a_queue.c
index 83d53824aa98..590637e81ad1 100644
--- a/security/integrity/ima/ima_queue.c
+++ b/security/integrity/ima/ima_queue.c
@@ -241,6 +241,11 @@ static int ima_reboot_notifier(struct notifier_block *=
nb,
 			       unsigned long action,
 			       void *data)
 {
+#ifdef CONFIG_IMA_KEXEC
+	if (action =3D=3D SYS_RESTART && data && !strcmp(data, "kexec reboot"))
+		ima_measure_kexec_event("kexec_execute");
+#endif
+
 	ima_measurements_suspend();
=20
 	return NOTIFY_DONE;
--=20
2.25.1