Skip to contentSkip to conversation

ima: limit the number of open-writers integrity violations

RFC PATCH In review Mimi Zohar 2 participants 3 messages Feb 5, 2025

Subsystems (4)
lore
Share & tools
lore

Mimi Zohar

author maintainer
#0 lore raw

RFC PATCH v2 1/2 ima: limit the number of open-writers integrity violations

Limit the number of open-writers integrity violation audit messages and records in the IMA measurement list emitted when re-opening a file for read.

---
 security/integrity/ima/ima.h      |  1 +
 security/integrity/ima/ima_main.c | 14 ++++++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)
security/integrity/ima/ima.h +1 -0
index a4f284bd846c..7f21568544dd 100644
@@ -182,6 +182,7 @@ struct ima_kexec_hdr {
 #define IMA_CHANGE_ATTR		2
 #define IMA_DIGSIG		3
 #define IMA_MUST_MEASURE	4
+#define IMA_LIMIT_VIOLATIONS	5
 
 /* IMA integrity metadata associated with an inode */
 struct ima_iint_cache {
security/integrity/ima/ima_main.c +12 -2
index 28b8b0db6f9b..5091ad931677 100644
@@ -137,8 +137,16 @@ static void ima_rdwr_violation_check(struct file *file,
 	} else {
 		if (must_measure)
 			set_bit(IMA_MUST_MEASURE, &iint->atomic_flags);
-		if (inode_is_open_for_write(inode) && must_measure)
-			send_writers = true;
+
+		if (inode_is_open_for_write(inode) && must_measure) {
+			if (!iint)
+				iint = ima_iint_find(inode);
+
+			/* Limit number of open_writers violations */
+			if (iint && !test_and_set_bit(IMA_LIMIT_VIOLATIONS,
+						      &iint->atomic_flags))
+				send_writers = true;
+		}
 	}
 
 	if (!send_tomtou && !send_writers)
@@ -167,6 +175,8 @@ static void ima_check_last_writer(struct ima_iint_cache *iint,
 	if (atomic_read(&inode->i_writecount) == 1) {
 		struct kstat stat;
 
+		clear_bit(IMA_LIMIT_VIOLATIONS, &iint->atomic_flags);
+
 		update = test_and_clear_bit(IMA_UPDATE_XATTR,
 					    &iint->atomic_flags);
 		if ((iint->flags & IMA_NEW_FILE) ||
Signature · 1 line
2.48.1
author maintainer
#1 lore raw

RFC PATCH v2 2/2 ima: limit the number of ToMToU integrity violations

Limit the number of Time-of-Measure-Time-of-Use (ToMToU) integrity violation audit messages and records in the IMA measurement list emitted when re-opening a file for write.

---
 security/integrity/ima/ima_main.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
security/integrity/ima/ima_main.c +3 -2
index 5091ad931677..b35afb844048 100644
@@ -129,9 +129,10 @@ static void ima_rdwr_violation_check(struct file *file,
 		if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) {
 			if (!iint)
 				iint = ima_iint_find(inode);
+
 			/* IMA_MEASURE is set from reader side */
-			if (iint && test_bit(IMA_MUST_MEASURE,
-						&iint->atomic_flags))
+			if (iint && test_and_clear_bit(IMA_MUST_MEASURE,
+						       &iint->atomic_flags))
 				send_tomtou = true;
 		}
 	} else {
Signature · 1 line
2.48.1
#2 lore raw
In reply to Mimi Zohar
Quoted 3 lines · Mimi Zohar

Limit the number of open-writers integrity violation audit messages and records in the IMA measurement list emitted when re-opening a file for read.

.. to only show one violation. (?)

Quoted 24 lines
---
  security/integrity/ima/ima.h      |  1 +
  security/integrity/ima/ima_main.c | 14 ++++++++++++--
  2 files changed, 13 insertions(+), 2 deletions(-)
security/integrity/ima/ima.h +1 -0
index a4f284bd846c..7f21568544dd 100644
@@ -182,6 +182,7 @@ struct ima_kexec_hdr {
  #define IMA_CHANGE_ATTR		2
  #define IMA_DIGSIG		3
  #define IMA_MUST_MEASURE	4
+#define IMA_LIMIT_VIOLATIONS	5
 
  /* IMA integrity metadata associated with an inode */
  struct ima_iint_cache {
security/integrity/ima/ima_main.c +0 -0
index 28b8b0db6f9b..5091ad931677 100644

security/integrity/ima/ima_main.c static void ima_rdwr_violation_check(struct file *file, · line 139

Quoted 3 lines
security/integrity/ima/ima_main.c +0 -0
@@ -137,8 +137,16 @@ static void ima_rdwr_violation_check(struct file *file,
  	} else {
  		if (must_measure)
  			set_bit(IMA_MUST_MEASURE, &iint->atomic_flags);

iint is assumed to be here under, which is correct when must_measure is set since how the single caller of this function calls it.

security/integrity/ima/ima_main.c static void ima_rdwr_violation_check(struct file *file, · line 143

Quoted 6 lines
security/integrity/ima/ima_main.c +4 -2
-		if (inode_is_open_for_write(inode) && must_measure)
-			send_writers = true;
+
+		if (inode_is_open_for_write(inode) && must_measure) {
+			if (!iint)
+				iint = ima_iint_find(inode);

When must_measure is set, the case of iint == NULL should never occur here.

security/integrity/ima/ima_main.c static void ima_rdwr_violation_check(struct file *file, · line 147

Quoted 4 lines
security/integrity/ima/ima_main.c +4 -0
+
+			/* Limit number of open_writers violations */
+			if (iint && !test_and_set_bit(IMA_LIMIT_VIOLATIONS,
+						      &iint->atomic_flags))

if (!test_and_set_bit(...))

Quoted 6 lines
security/integrity/ima/ima_main.c +2 -0
+				send_writers = true;
+		}
  	}
 
  	if (!send_tomtou && !send_writers)

security/integrity/ima/ima_main.c static void ima_check_last_writer(struct ima_iint_cache *iint, · line 182

Quoted 8 lines
security/integrity/ima/ima_main.c +2 -0
@@ -167,6 +175,8 @@ static void ima_check_last_writer(struct ima_iint_cache *iint,
  	if (atomic_read(&inode->i_writecount) == 1) {
  		struct kstat stat;
 
+		clear_bit(IMA_LIMIT_VIOLATIONS, &iint->atomic_flags);
+
  		update = test_and_clear_bit(IMA_UPDATE_XATTR,
  					    &iint->atomic_flags);
  		if ((iint->flags & IMA_NEW_FILE) ||

I tested the two patches. For the file /usr/bin/foobar that's being measured on FILE_CHECK I get two violations for this sequence here:

# exec 3>/usr/bin/foobar
# exec 4</usr/bin/foobar
# exec 3>/usr/bin/foobar

This here now only emits one violation now.

# exec 3>/usr/bin/foobar
# exec 4</usr/bin/foobar
# exec 4</usr/bin/foobar
# exec 5</usr/bin/foobar
# exec 6</usr/bin/foobar