From nobody Thu Apr 2 20:27:04 2026 Received: from frasgout12.his.huawei.com (frasgout12.his.huawei.com [14.137.139.154]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 538D8423A7B; Thu, 26 Mar 2026 17:31:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=14.137.139.154 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774546282; cv=none; b=XHppDZZLiMRkzfIAUpX5/2PonJ1NtoMW1Tp4xYX6r2p9V+sAHwFUqxObYEqrRcNSZjW1fe8KkkSM56fmdSUITCM/iw3b9Nse6dSggxjbFWKZYtGI6RjkvO0UWhtnkJqed1md4xfv9E2E38wsttVGEDc28+ptDjNEDrcxx7hg2MY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774546282; c=relaxed/simple; bh=bBGnTuFjwr4aKleyp99+DjrixZO3wFuqCTblq2t4miw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jqnW1RruqD0r8QbgpVV6nabOLARBPSD/6vG4SWmfC9KLws5Zs3bO+erWgOwXEscI0sDXbpWn/YByUq8DD4LK7G9QF74E/O38CpbAmdcISEbMvGGG9CMCVMdk0IsWZrmyq16v59bC7v6ZBjVK8I2NNlw9LwOFcsx4v0wziTpIetY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=14.137.139.154 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.18.224.235]) by frasgout12.his.huawei.com (SkyGuard) with ESMTPS id 4fhW1l28tmztKQg; Fri, 27 Mar 2026 01:27:39 +0800 (CST) Received: from mail02.huawei.com (unknown [7.182.16.47]) by mail.maildlp.com (Postfix) with ESMTP id B5B9240569; Fri, 27 Mar 2026 01:31:15 +0800 (CST) Received: from huaweicloud.com (unknown [10.204.63.22]) by APP1 (Coremail) with SMTP id LxC2BwB3Yu84bcVppGUvAQ--.1881S6; Thu, 26 Mar 2026 18:31:15 +0100 (CET) From: Roberto Sassu To: corbet@lwn.net, skhan@linuxfoundation.org, zohar@linux.ibm.com, dmitry.kasatkin@gmail.com, eric.snowberg@oracle.com, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, gregorylumen@linux.microsoft.com, chenste@linux.microsoft.com, nramas@linux.microsoft.com, Roberto Sassu Subject: [PATCH v4 04/13] ima: Introduce per binary measurements list type binary_runtime_size value Date: Thu, 26 Mar 2026 18:30:02 +0100 Message-ID: <20260326173011.1191815-5-roberto.sassu@huaweicloud.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260326173011.1191815-1-roberto.sassu@huaweicloud.com> References: <20260326173011.1191815-1-roberto.sassu@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: LxC2BwB3Yu84bcVppGUvAQ--.1881S6 X-Coremail-Antispam: 1UD129KBjvJXoW3Jw15Xr1ftr4kGF1kGrWxWFg_yoW7CrWUpa nxZF18tr4kXay7KFZ5GF97ZFWrW3yrXry7Jas8W3W09Fs7Ar1jqF15tryjkFW5G3s8t3W7 JrWqqr4fAanrt3DanT9S1TB71UUUUUDqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPlb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW8JVW5JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv6xkF7I0E 14v26r4UJVWxJr1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrV C2j2WlYx0E2Ix0cI8IcVAFwI0_Jrv_JF1lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE 7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwACI402YVCY1x02628vn2kIc2xKxwCY1x0262 kKe7AKxVWrXVW3AwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s02 6c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_GF v_WrylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVW8JVW5JwCI42IY6xIIjxv20xvE c7CjxVAFwI0_Gr1j6F4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aV AFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8Jr0_Cr1UYxBIdaVFxhVjvjDU0xZF pf9x07jhXo7UUUUU= X-CM-SenderInfo: purev21wro2thvvxqx5xdzvxpfor3voofrz/1tbiAQAEBGnEnSsT2QACsS Content-Type: text/plain; charset="utf-8" From: Roberto Sassu Make binary_runtime_size as an array, to have separate counters per binary measurements list type. Currently, define the BINARY type for the existing binary measurements list. Introduce ima_update_binary_runtime_size() to facilitate updating a binary_runtime_size value with a given binary measurement list type. Also add the binary measurements list type parameter to ima_get_binary_runtime_size(), to retrieve the desired value. Retrieving the value is now done under the ima_extend_list_mutex, since there can be concurrent updates. No functional change (except for the mutex usage, that fixes the concurrency issue): the BINARY array element is equivalent to the old binary_runtime_size. Link: https://github.com/linux-integrity/linux/issues/1 Signed-off-by: Roberto Sassu --- security/integrity/ima/ima.h | 2 +- security/integrity/ima/ima_kexec.c | 5 ++-- security/integrity/ima/ima_queue.c | 40 +++++++++++++++++++++--------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h index 199237e2d2e3..97b7d6024b5d 100644 --- a/security/integrity/ima/ima.h +++ b/security/integrity/ima/ima.h @@ -318,7 +318,7 @@ int ima_restore_measurement_entry(struct ima_template_e= ntry *entry); int ima_restore_measurement_list(loff_t bufsize, void *buf); int ima_measurements_show(struct seq_file *m, void *v); int __init ima_init_htable(void); -unsigned long ima_get_binary_runtime_size(void); +unsigned long ima_get_binary_runtime_size(enum binary_lists binary_list); int ima_init_template(void); void ima_init_template_list(void); int __init ima_init_digests(void); diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/im= a_kexec.c index 40962dc0ca86..44ebefbdcab0 100644 --- a/security/integrity/ima/ima_kexec.c +++ b/security/integrity/ima/ima_kexec.c @@ -42,7 +42,7 @@ void ima_measure_kexec_event(const char *event_name) long len; int n; =20 - buf_size =3D ima_get_binary_runtime_size(); + buf_size =3D ima_get_binary_runtime_size(BINARY); len =3D atomic_long_read(&ima_num_entries[BINARY]); =20 n =3D scnprintf(ima_kexec_event, IMA_KEXEC_EVENT_LEN, @@ -159,7 +159,8 @@ void ima_add_kexec_buffer(struct kimage *image) else extra_memory =3D CONFIG_IMA_KEXEC_EXTRA_MEMORY_KB * 1024; =20 - binary_runtime_size =3D ima_get_binary_runtime_size() + extra_memory; + binary_runtime_size =3D ima_get_binary_runtime_size(BINARY) + + extra_memory; =20 if (binary_runtime_size >=3D ULONG_MAX - PAGE_SIZE) kexec_segment_size =3D ULONG_MAX; diff --git a/security/integrity/ima/ima_queue.c b/security/integrity/ima/im= a_queue.c index 952172a4905d..b6d10dceb669 100644 --- a/security/integrity/ima/ima_queue.c +++ b/security/integrity/ima/ima_queue.c @@ -27,9 +27,11 @@ static struct tpm_digest *digests; =20 LIST_HEAD(ima_measurements); /* list of all measurements */ #ifdef CONFIG_IMA_KEXEC -static unsigned long binary_runtime_size; +static unsigned long binary_runtime_size[BINARY__LAST]; #else -static unsigned long binary_runtime_size =3D ULONG_MAX; +static unsigned long binary_runtime_size[BINARY__LAST] =3D { + [0 ... BINARY__LAST - 1] =3D ULONG_MAX +}; #endif =20 /* num of stored measurements in the list */ @@ -131,6 +133,20 @@ static int get_binary_runtime_size(struct ima_template= _entry *entry) return size; } =20 +static void ima_update_binary_runtime_size(struct ima_template_entry *entr= y, + enum binary_lists binary_list) +{ + int size; + + if (binary_runtime_size[binary_list] =3D=3D ULONG_MAX) + return; + + size =3D get_binary_runtime_size(entry); + binary_runtime_size[binary_list] =3D + (binary_runtime_size[binary_list] < ULONG_MAX - size) ? + binary_runtime_size[binary_list] + size : ULONG_MAX; +} + /* ima_add_template_entry helper function: * - Add template entry to the measurement list and hash table, for * all entries except those carried across kexec. @@ -163,13 +179,7 @@ static int ima_add_digest_entry(struct ima_template_en= try *entry, hlist_add_head_rcu(&qe->hnext, &htable[key]); } =20 - if (binary_runtime_size !=3D ULONG_MAX) { - int size; - - size =3D get_binary_runtime_size(entry); - binary_runtime_size =3D (binary_runtime_size < ULONG_MAX - size) ? - binary_runtime_size + size : ULONG_MAX; - } + ima_update_binary_runtime_size(entry, BINARY); return 0; } =20 @@ -178,12 +188,18 @@ static int ima_add_digest_entry(struct ima_template_e= ntry *entry, * entire binary_runtime_measurement list, including the ima_kexec_hdr * structure. */ -unsigned long ima_get_binary_runtime_size(void) +unsigned long ima_get_binary_runtime_size(enum binary_lists binary_list) { - if (binary_runtime_size >=3D (ULONG_MAX - sizeof(struct ima_kexec_hdr))) + unsigned long val; + + mutex_lock(&ima_extend_list_mutex); + val =3D binary_runtime_size[binary_list]; + mutex_unlock(&ima_extend_list_mutex); + + if (val >=3D (ULONG_MAX - sizeof(struct ima_kexec_hdr))) return ULONG_MAX; else - return binary_runtime_size + sizeof(struct ima_kexec_hdr); + return val + sizeof(struct ima_kexec_hdr); } =20 static int ima_pcr_extend(struct tpm_digest *digests_arg, int pcr) --=20 2.43.0