IMA: use i_writecount rather than a private counter
authorEric Paris <eparis@redhat.com>
Mon, 25 Oct 2010 18:41:52 +0000 (14:41 -0400)
committerLinus Torvalds <torvalds@linux-foundation.org>
Tue, 26 Oct 2010 18:37:18 +0000 (11:37 -0700)
IMA tracks the number of struct files which are holding a given inode
readonly and the number which are holding the inode write or r/w.  It
needs this information so when a new reader or writer comes in it can
tell if this new file will be able to invalidate results it already made
about existing files.

aka if a task is holding a struct file open RO, IMA measured the file
and recorded those measurements and then a task opens the file RW IMA
needs to note in the logs that the old measurement may not be correct.
It's called a "Time of Measure Time of Use" (ToMToU) issue.  The same is
true is a RO file is opened to an inode which has an open writer.  We
cannot, with any validity, measure the file in question since it could
be changing.

This patch attempts to use the i_writecount field to track writers.  The
i_writecount field actually embeds more information in it's value than
IMA needs but it should work for our purposes and allow us to shrink the
struct inode even more.

Signed-off-by: Eric Paris <eparis@redhat.com>
Acked-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
security/integrity/ima/ima.h
security/integrity/ima/ima_iint.c
security/integrity/ima/ima_main.c

index 80aca3d2cb7106192752a49b554e2347acb4e047..b546b90f5fa874a6be1bbfad9ef47f029735c053 100644 (file)
@@ -108,7 +108,6 @@ struct ima_iint_cache {
        struct mutex mutex;     /* protects: version, flags, digest */
        /* protected by inode->i_lock */
        unsigned int readcount; /* measured files readcount */
-       unsigned int writecount;/* measured files writecount */
        struct kref refcount;   /* ima_iint_cache reference count */
 };
 
index db71a13f27fe1bb21a023041726e56c09599b4ae..e68891f8d55afe1aadca81885e499c57643b469c 100644 (file)
@@ -129,11 +129,6 @@ void iint_free(struct kref *kref)
                       iint->readcount);
                iint->readcount = 0;
        }
-       if (iint->writecount != 0) {
-               printk(KERN_INFO "%s: writecount: %u\n", __func__,
-                      iint->writecount);
-               iint->writecount = 0;
-       }
        kref_init(&iint->refcount);
        kmem_cache_free(iint_cache, iint);
 }
@@ -166,7 +161,6 @@ static void init_once(void *foo)
        iint->flags = 0UL;
        mutex_init(&iint->mutex);
        iint->readcount = 0;
-       iint->writecount = 0;
        kref_init(&iint->refcount);
 }
 
index 2f9b5d50424ec237ac0410836990de98ca40308d..24660bf3f82ab27a22de12a551961e84772738bc 100644 (file)
@@ -94,8 +94,6 @@ static void ima_inc_counts(struct ima_iint_cache *iint, fmode_t mode)
 
        if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
                iint->readcount++;
-       if (mode & FMODE_WRITE)
-               iint->writecount++;
 }
 
 /*
@@ -173,18 +171,16 @@ static void ima_dec_counts(struct ima_iint_cache *iint, struct inode *inode,
                iint->readcount--;
        }
        if (mode & FMODE_WRITE) {
-               if (unlikely(iint->writecount == 0))
+               if (atomic_read(&inode->i_writecount) <= 0)
                        dump = true;
-               iint->writecount--;
-               if (iint->writecount == 0) {
-                       if (iint->version != inode->i_version)
-                               iint->flags &= ~IMA_MEASURED;
-               }
+               if (atomic_read(&inode->i_writecount) == 1 &&
+                   iint->version != inode->i_version)
+                       iint->flags &= ~IMA_MEASURED;
        }
 
        if (dump && !ima_limit_imbalance(file)) {
-               printk(KERN_INFO "%s: open/free imbalance (r:%u w:%u)\n",
-                      __func__, iint->readcount, iint->writecount);
+               printk(KERN_INFO "%s: open/free imbalance (r:%u)\n",
+                      __func__, iint->readcount);
                dump_stack();
        }
 }