Skip to contentSkip to conversation

integrity: add ima_counts_put

PATCH In review Mimi Zohar 3 participants 11 messages Jun 26, 2009 – Jul 29, 2009

Subsystems (4)
lore
Share & tools
lore

Mimi Zohar

author
#0 lore raw

PATCH 1/1 integrity: add ima_counts_put

This patch fixes an imbalance message as reported by J.R. Okajima. The IMA file counters are incremented in ima_path_check. If the actual open fails, such as ETXTBSY, decrement the counters to prevent unnecessary imbalance messages.

---
 fs/namei.c                        |    7 +++++++
 include/linux/ima.h               |    6 ++++++
 security/integrity/ima/ima_main.c |   29 ++++++++++++++++++++++++++++-
 3 files changed, 41 insertions(+), 1 deletions(-)
fs/namei.c +7 -0
index 527119a..5abba79 100644
@@ -1758,6 +1758,10 @@ do_last:
 			goto exit;
 		}
 		filp = nameidata_to_filp(&nd, open_flag);
+		if (IS_ERR(filp))
+			ima_counts_put(&nd.path,
+				       acc_mode & (MAY_READ | MAY_WRITE |
+						   MAY_EXEC));
 		mnt_drop_write(nd.path.mnt);
 		return filp;
 	}
@@ -1812,6 +1816,9 @@ ok:
 		goto exit;
 	}
 	filp = nameidata_to_filp(&nd, open_flag);
+	if (IS_ERR(filp))
+		ima_counts_put(&nd.path,
+			       acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC));
 	/*
 	 * It is now safe to drop the mnt write
 	 * because the filp has had a write taken
include/linux/ima.h +6 -0
index b1b827d..57ab5fd 100644
@@ -24,6 +24,7 @@ extern int ima_path_check(struct path *path, int mask, int update_counts);
 extern void ima_file_free(struct file *file);
 extern int ima_file_mmap(struct file *file, unsigned long prot);
 extern void ima_counts_get(struct file *file);
+extern void ima_counts_put(struct path *path, int mask);
 
 #else
 static inline int ima_bprm_check(struct linux_binprm *bprm)
@@ -60,5 +61,10 @@ static inline void ima_counts_get(struct file *file)
 {
 	return;
 }
+
+static inline void ima_counts_put(struct path *path, int mask);)
+{
+	return;
+}
 #endif /* CONFIG_IMA_H */
 #endif /* _LINUX_IMA_H */
security/integrity/ima/ima_main.c +28 -1
index 6f61187..101c512 100644
@@ -238,7 +238,34 @@ out:
 }
 
 /*
- * ima_opens_get - increment file counts
+ * ima_counts_put - decrement file counts
+ *
+ * File counts are incremented in ima_path_check. On file open
+ * error, such as ETXTBSY, decrement the counts to prevent
+ * unnecessary imbalance messages.
+ */
+void ima_counts_put(struct path *path, int mask)
+{
+	struct inode *inode = path->dentry->d_inode;
+	struct ima_iint_cache *iint;
+
+	if (!ima_initialized || !S_ISREG(inode->i_mode))
+		return;
+	iint = ima_iint_find_insert_get(inode);
+	if (!iint)
+		return;
+
+	mutex_lock(&iint->mutex);
+	iint->opencount--;
+	if ((mask & MAY_WRITE) || (mask == 0))
+		iint->writecount--;
+	else if (mask & (MAY_READ | MAY_EXEC))
+		iint->readcount--;
+	mutex_unlock(&iint->mutex);
+}
+
+/*
+ * ima_counts_get - increment file counts
  *
  * - for IPC shm and shmat file.
  * - for nfsd exported files.
Signature · 1 line
1.6.0.6
author
#1 lore raw

Re: [PATCH] integrity: add ima_counts_put (updated)

This patch fixes an imbalance message as reported by J.R. Okajima. The IMA file counters are incremented in ima_path_check. If the actual open fails, such as ETXTBSY, decrement the counters to prevent unnecessary imbalance messages.

---
 fs/namei.c                        |    7 +++++++
 include/linux/ima.h               |    6 ++++++
 security/integrity/ima/ima_main.c |   29 ++++++++++++++++++++++++++++-
 3 files changed, 41 insertions(+), 1 deletions(-)
fs/namei.c +7 -0
index 527119a..5abba79 100644
@@ -1758,6 +1758,10 @@ do_last:
 			goto exit;
 		}
 		filp = nameidata_to_filp(&nd, open_flag);
+		if (IS_ERR(filp))
+			ima_counts_put(&nd.path,
+				       acc_mode & (MAY_READ | MAY_WRITE |
+						   MAY_EXEC));
 		mnt_drop_write(nd.path.mnt);
 		return filp;
 	}
@@ -1812,6 +1816,9 @@ ok:
 		goto exit;
 	}
 	filp = nameidata_to_filp(&nd, open_flag);
+	if (IS_ERR(filp))
+		ima_counts_put(&nd.path,
+			       acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC));
 	/*
 	 * It is now safe to drop the mnt write
 	 * because the filp has had a write taken
include/linux/ima.h +6 -0
index b1b827d..0e3f2a4 100644
@@ -24,6 +24,7 @@ extern int ima_path_check(struct path *path, int mask, int update_counts);
 extern void ima_file_free(struct file *file);
 extern int ima_file_mmap(struct file *file, unsigned long prot);
 extern void ima_counts_get(struct file *file);
+extern void ima_counts_put(struct path *path, int mask);
 
 #else
 static inline int ima_bprm_check(struct linux_binprm *bprm)
@@ -60,5 +61,10 @@ static inline void ima_counts_get(struct file *file)
 {
 	return;
 }
+
+static inline void ima_counts_put(struct path *path, int mask)
+{
+	return;
+}
 #endif /* CONFIG_IMA_H */
 #endif /* _LINUX_IMA_H */
security/integrity/ima/ima_main.c +28 -1
index 6f61187..101c512 100644
@@ -238,7 +238,34 @@ out:
 }
 
 /*
- * ima_opens_get - increment file counts
+ * ima_counts_put - decrement file counts
+ *
+ * File counts are incremented in ima_path_check. On file open
+ * error, such as ETXTBSY, decrement the counts to prevent
+ * unnecessary imbalance messages.
+ */
+void ima_counts_put(struct path *path, int mask)
+{
+	struct inode *inode = path->dentry->d_inode;
+	struct ima_iint_cache *iint;
+
+	if (!ima_initialized || !S_ISREG(inode->i_mode))
+		return;
+	iint = ima_iint_find_insert_get(inode);
+	if (!iint)
+		return;
+
+	mutex_lock(&iint->mutex);
+	iint->opencount--;
+	if ((mask & MAY_WRITE) || (mask == 0))
+		iint->writecount--;
+	else if (mask & (MAY_READ | MAY_EXEC))
+		iint->readcount--;
+	mutex_unlock(&iint->mutex);
+}
+
+/*
+ * ima_counts_get - increment file counts
  *
  * - for IPC shm and shmat file.
  * - for nfsd exported files.
Signature · 1 line
1.6.0.6
maintainer
#2 lore raw

Re: [PATCH] integrity: add ima_counts_put (updated)

Quoted 6 lines · Mimi Zohar

This patch fixes an imbalance message as reported by J.R. Okajima. The IMA file counters are incremented in ima_path_check. If the actual open fails, such as ETXTBSY, decrement the counters to prevent unnecessary imbalance messages.

Don't forget to use "Reported-by:" lines for the bug reporter.

Have you tested this using the original test case?

Signature · 2 lines
James Morris
<[email protected]>
author
#3 lore raw

Re: [PATCH] integrity: add ima_counts_put (updated)

Quoted 10 lines · James Morris
Quoted 6 lines · Mimi Zohar

This patch fixes an imbalance message as reported by J.R. Okajima. The IMA file counters are incremented in ima_path_check. If the actual open fails, such as ETXTBSY, decrement the counters to prevent unnecessary imbalance messages.

Don't forget to use "Reported-by:" lines for the bug reporter.

Ok

Quoted 1 line

Have you tested this using the original test case?

Yes, the patch resolves the iint_free() message.

Mimi

#4 lore raw

Re: [PATCH] integrity: add ima_counts_put (updated)

In reply to Mimi Zohar

Mimi Zohar:

Quoted 4 lines

This patch fixes an imbalance message as reported by J.R. Okajima. The IMA file counters are incremented in ima_path_check. If the actual open fails, such as ETXTBSY, decrement the counters to prevent unnecessary imbalance messages.

Although I have no objection for this fix, I'd like to suggest you to stop incrementing the counters in ima_path_check().
A while ago IMA_COUNT_LEAVE and ima_counts_get() were introduced, and now ima_counts_put() appears.
Isn't it easier something like this,
- stop incrementing in ima_path_check().
- call ima_counts_get() in dentry_open() (or similar).
- delete IMA_COUNT_LEAVE/UPDATE and ima_counts_put().

How do you think?

J. R. Okajima

author
#5 lore raw

Re: [PATCH] integrity: add ima_counts_put (updated)

Quoted 15 lines · On Mon, 2009-06-29 at 23:12 +0900, [email protected] wrote:

Mimi Zohar:

Quoted 4 lines

This patch fixes an imbalance message as reported by J.R. Okajima. The IMA file counters are incremented in ima_path_check. If the actual open fails, such as ETXTBSY, decrement the counters to prevent unnecessary imbalance messages.

Although I have no objection for this fix, I'd like to suggest you to stop incrementing the counters in ima_path_check().
A while ago IMA_COUNT_LEAVE and ima_counts_get() were introduced, and now ima_counts_put() appears.
Isn't it easier something like this,
- stop incrementing in ima_path_check().
- call ima_counts_get() in dentry_open() (or similar).
- delete IMA_COUNT_LEAVE/UPDATE and ima_counts_put().

Quoted 4 lines

How do you think?

J. R. Okajima

This suggestion has been mentioned before; and yes would definitely resolve the annoying imbalance and iint_free() messages. But incrementing/decrementing the pointers automatically each time a file is opened/closed would defeat their purpose - alerting us that a file was possibly not measured before being read/executed.

Mimi

#6 lore raw

Re: [PATCH] integrity: add ima_counts_put (updated)

Mimi Zohar:

Quoted 5 lines

This suggestion has been mentioned before; and yes would definitely resolve the annoying imbalance and iint_free() messages. But incrementing/decrementing the pointers automatically each time a file is opened/closed would defeat their purpose - alerting us that a file was possibly not measured before being read/executed.

I may be wrong since I don't fully understand IMA's purpose, but why did you create ima_counts_get() and make it call after dentry_open() in nfsd_open()? Isn't it same thing essentially?

J. R. Okajima

author
#7 lore raw

Re: [PATCH] integrity: add ima_counts_put (updated)

Quoted 7 lines · On Tue, 2009-06-30 at 05:36 +0900, [email protected] wrote:

Mimi Zohar:

Quoted 5 lines

This suggestion has been mentioned before; and yes would definitely resolve the annoying imbalance and iint_free() messages. But incrementing/decrementing the pointers automatically each time a file is opened/closed would defeat their purpose - alerting us that a file was possibly not measured before being read/executed.

Quoted 6 lines

I may be wrong since I don't fully understand IMA's purpose, but why did you create ima_counts_get() and make it call after dentry_open() in nfsd_open()? Isn't it same thing essentially?

J. R. Okajima

NFSv3 is an interesting example. Permission checking is done once, followed by multiple open/read/close calls. Incrementing the counters in nfsd_permission() once and decrementing the counters in close, multiple times, resulted in imbalance messages. True, the solution in this case was to increment in open and decrement in close, but that was only part of the solution. The other part of the solution, the important part, was to add a call to ima_path_check() to measure the file.

The imbalance message did what it was suppose to do - highlight the fact that a file was read/executed without first being measured.

Mimi

#8 lore raw

Re: [PATCH] integrity: add ima_counts_put (updated)

Mimi Zohar:

Quoted 7 lines

NFSv3 is an interesting example. Permission checking is done once, followed by multiple open/read/close calls. Incrementing the counters in nfsd_permission() once and decrementing the counters in close, multiple times, resulted in imbalance messages. True, the solution in this case was to increment in open and decrement in close, but that was only part of the solution. The other part of the solution, the important part, was to add a call to ima_path_check() to measure the file.

Let me make sure.
Does "that was only part of the solution" mean IMA does not work for NFSD fully? To make IMA work fully, is incrementing before open absolutely necessary?

J. R. Okajima

author
#9 lore raw

Re: [PATCH] integrity: add ima_counts_put (updated)

Quoted 9 lines · On Fri, 2009-07-03 at 13:02 +0900, [email protected] wrote:

Mimi Zohar:

Quoted 7 lines

NFSv3 is an interesting example. Permission checking is done once, followed by multiple open/read/close calls. Incrementing the counters in nfsd_permission() once and decrementing the counters in close, multiple times, resulted in imbalance messages. True, the solution in this case was to increment in open and decrement in close, but that was only part of the solution. The other part of the solution, the important part, was to add a call to ima_path_check() to measure the file.

Quoted 6 lines

Let me make sure.
Does "that was only part of the solution" mean IMA does not work for NFSD fully? To make IMA work fully, is incrementing before open absolutely necessary?

J. R. Okajima

The patch is fine. It adds a call to ima_path_check() in nfsd_permission(), but delays incrementing the counters to nfsd_open() and decrementing the counters to nfsd_close() in order for the counters to be balanced.

Mimi

#10 lore raw

Re: [PATCH] integrity: add ima_counts_put (updated)

In reply to Mimi Zohar

Mimi Zohar:

Quoted 4 lines

This patch fixes an imbalance message as reported by J.R. Okajima. The IMA file counters are incremented in ima_path_check. If the actual open fails, such as ETXTBSY, decrement the counters to prevent unnecessary imbalance messages.

Unfortunately IMA seems to be still buggy.
may_open() calls ima_path_check() with IMA_COUNT_UPDATE, but may_open() may fail later with several reasons such like, open-flag mismatch with inode-flag, capability, the file was executing and get_write_access() failed, etc.
In these cases, IMA has to maintain its counters too by calling ima_counts_put() or something, does it?

J. R. Okajima