From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from frasgout11.his.huawei.com (frasgout11.his.huawei.com [14.137.139.23]) (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 5DB9735674C; Thu, 21 May 2026 08:45:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=14.137.139.23 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779353149; cv=none; b=DfgtiGEnlD1jgHMX9xWbh9RLHPPiGpfDUODCNY8WKiDA8jd4airMK6fDQcOZV6egFiPG613vhvIz7ClKmj9iQ14IlZyuidqB0VDlB+VdCwe9k6h7a2Qqhvl5Wpae6X13LBVaaZfvVGcyhiFcFm99RQM5mXV7gaxVb3Dkhd57tsA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779353149; c=relaxed/simple; bh=XVpa3gPs4WHuWXOXdevcBTIxStwkVCXV0ajpL5/HWc4=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=HGmpfl/lnrLk8MODmPnNh+7cfazliQ9c1bUoxg0wgScZ53DvisBzy14gbnUqBvVv+fIdZG62f9xlnPpfltDY4mEmP4V4Y/DSDHrESRZvVJf7RS3RCPGKNoxChBOpsOwSehlMJNaSQs4gIusyYr7JC6E+wPXJ1LJ5m81rFw+pBnc= 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.23 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 frasgout11.his.huawei.com (SkyGuard) with ESMTPS id 4gLhL55nbJz1HCnv; Thu, 21 May 2026 16:25:17 +0800 (CST) Received: from mail02.huawei.com (unknown [7.182.16.47]) by mail.maildlp.com (Postfix) with ESMTP id A972D4056C; Thu, 21 May 2026 16:30:16 +0800 (CST) Received: from [10.204.63.22] (unknown [10.204.63.22]) by APP1 (Coremail) with SMTP id LxC2BwAXyaKOwg5q4q+OAQ--.43580S2; Thu, 21 May 2026 09:30:16 +0100 (CET) Message-ID: <760aa1c86b2c8fa3f4ceab5c65c42f4f1e1f59c1.camel@huaweicloud.com> Subject: Re: [PATCH v5 06/13] ima: Mediate open/release method of the measurements list From: Roberto Sassu To: Mimi Zohar , corbet@lwn.net, skhan@linuxfoundation.org, 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 Date: Thu, 21 May 2026 10:30:04 +0200 In-Reply-To: References: <20260429160319.4162918-1-roberto.sassu@huaweicloud.com> <20260429160319.4162918-7-roberto.sassu@huaweicloud.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.3-0ubuntu1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID:LxC2BwAXyaKOwg5q4q+OAQ--.43580S2 X-Coremail-Antispam: 1UD129KBjvJXoWxZw43tF15Wry3CryfAry8AFb_yoW5tr4rpa yDGayrCr1kJrW7CFnrGa47ArWFv3yrGa15urn3JFy3AF1rZr9I9r4Yyry7Crnrtry8tr1f tr4jqrZ8uas0yaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvjb4IE77IF4wAFF20E14v26ryj6rWUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWUJVW8JwA2z4x0Y4vEx4A2jsIEc7CjxV AFwI0_Gr0_Gr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40E x7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x 0Yz7v_Jr0_Gr1lF7xvr2IY64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1lc7CjxVAaw2AF wI0_Jw0_GFyl42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4 xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r4a6rW5 MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I 0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWU JVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUFk u4UUUUU X-CM-SenderInfo: purev21wro2thvvxqx5xdzvxpfor3voofrz/1tbiAgAABGoNmbQK-wABs4 On Wed, 2026-05-20 at 22:07 -0400, Mimi Zohar wrote: > On Wed, 2026-04-29 at 18:03 +0200, Roberto Sassu wrote: > > From: Roberto Sassu > >=20 > > Introduce the ima_measure_users counter, to implement a semaphore-like > > locking scheme where the binary and ASCII measurements list interfaces = can > > be concurrently open by multiple readers, or alternatively by a single > > writer. > >=20 > > A semaphore cannot be used because the kernel cannot return to user spa= ce > > with a lock held. > >=20 > > Introduce the ima_measure_lock() and ima_measure_unlock() primitives, t= o > > respectively lock/unlock the interfaces (safely with the ima_measure_us= ers > > counter, without holding a lock). > >=20 > > Finally, introduce _ima_measurements_open() to lock the interface befor= e > > seq_open(), and call it from ima_measurements_open() and > > ima_ascii_measurements_open(). And, introduce ima_measurements_release(= ), > > to unlock the interface. > >=20 > > Require CAP_SYS_ADMIN if the interface is opened for write (not possibl= e > > for the current measurements interfaces, since they only have read > > permission). > >=20 > > No functional changes: multiple readers are allowed as before. > >=20 > > Link: https://github.com/linux-integrity/linux/issues/1 > > Signed-off-by: Roberto Sassu > > --- > > security/integrity/ima/ima_fs.c | 71 +++++++++++++++++++++++++++++++-- > > 1 file changed, 67 insertions(+), 4 deletions(-) > >=20 > > diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/i= ma_fs.c > > index 9a8dba14d82a..68edea7139d5 100644 > > --- a/security/integrity/ima/ima_fs.c > > +++ b/security/integrity/ima/ima_fs.c > > @@ -25,6 +25,8 @@ > > #include "ima.h" > > =20 > > static DEFINE_MUTEX(ima_write_mutex); > > +static DEFINE_MUTEX(ima_measure_mutex); > > +static long ima_measure_users; >=20 > long? The limit pre process can be up to INT_MAX. Two processes could overflow the counter if it was int. Since privileged users can bypass the system wide max-file check (see alloc_empty_file()), I will add an overflow check to be sure. > > =20 > > bool ima_canonical_fmt; > > static int __init default_canonical_fmt_setup(char *str) > > @@ -209,16 +211,76 @@ static const struct seq_operations ima_measurment= s_seqops =3D { > > .show =3D ima_measurements_show > > }; > > =20 > > +static int ima_measure_lock(bool write) > > +{ > > + mutex_lock(&ima_measure_mutex); > > + if ((write && ima_measure_users !=3D 0) || > > + (!write && ima_measure_users < 0)) { > > + mutex_unlock(&ima_measure_mutex); > > + return -EBUSY; > > + } >=20 > Thanks, Roberto. The code is really clear and well written. However, it = could > use a comment indicating the different ima_measure_users values as a remi= nder. >=20 > ima_measure_users: > 0 open readers > ima_meaasure_users: =3D=3D -1 open writer Ok. > > + > > + if (write) > > + ima_measure_users--; > > + else > > + ima_measure_users++; > > + mutex_unlock(&ima_measure_mutex); > > + return 0; > > +} > > + > > +static void ima_measure_unlock(bool write) > > +{ > > + mutex_lock(&ima_measure_mutex); > > + if (write) > > + ima_measure_users++; >=20 > There should only be one writer at a time. ima_measure_users could be set= to > zero. Sure, but I find the code more clear this way. Roberto > > + else > > + ima_measure_users--; > > + mutex_unlock(&ima_measure_mutex); > > +} > > + >=20 > thanks, >=20 > Mimi