IMA: use unsigned int instead of long for counters
authorEric Paris <eparis@redhat.com>
Mon, 25 Oct 2010 18:41:32 +0000 (14:41 -0400)
committerLinus Torvalds <torvalds@linux-foundation.org>
Tue, 26 Oct 2010 18:37:18 +0000 (11:37 -0700)
Currently IMA uses 2 longs in struct inode.  To save space (and as it
seems impossible to overflow 32 bits) we switch these to unsigned int.
The switch to unsigned does require slightly different checks for
underflow, but it isn't complex.

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 3d70108..000d13a 100644 (file)
@@ -106,8 +106,8 @@ struct ima_iint_cache {
        unsigned long flags;
        u8 digest[IMA_DIGEST_SIZE];
        struct mutex mutex;     /* protects: version, flags, digest */
-       long readcount;         /* measured files readcount */
-       long writecount;        /* measured files writecount */
+       unsigned int readcount; /* measured files readcount */
+       unsigned int writecount;/* measured files writecount */
        struct kref refcount;   /* ima_iint_cache reference count */
 };
 
index 8e64313..db71a13 100644 (file)
@@ -125,12 +125,12 @@ void iint_free(struct kref *kref)
        iint->version = 0;
        iint->flags = 0UL;
        if (iint->readcount != 0) {
-               printk(KERN_INFO "%s: readcount: %ld\n", __func__,
+               printk(KERN_INFO "%s: readcount: %u\n", __func__,
                       iint->readcount);
                iint->readcount = 0;
        }
        if (iint->writecount != 0) {
-               printk(KERN_INFO "%s: writecount: %ld\n", __func__,
+               printk(KERN_INFO "%s: writecount: %u\n", __func__,
                       iint->writecount);
                iint->writecount = 0;
        }
index 995bd1b..5a1bf3d 100644 (file)
@@ -178,11 +178,18 @@ static void ima_dec_counts(struct ima_iint_cache *iint, struct inode *inode,
                           struct file *file)
 {
        mode_t mode = file->f_mode;
+       bool dump = false;
+
        BUG_ON(!mutex_is_locked(&iint->mutex));
 
-       if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
+       if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
+               if (unlikely(iint->readcount == 0))
+                       dump = true;
                iint->readcount--;
+       }
        if (mode & FMODE_WRITE) {
+               if (unlikely(iint->writecount == 0))
+                       dump = true;
                iint->writecount--;
                if (iint->writecount == 0) {
                        if (iint->version != inode->i_version)
@@ -190,10 +197,8 @@ static void ima_dec_counts(struct ima_iint_cache *iint, struct inode *inode,
                }
        }
 
-       if (((iint->readcount < 0) ||
-            (iint->writecount < 0)) &&
-           !ima_limit_imbalance(file)) {
-               printk(KERN_INFO "%s: open/free imbalance (r:%ld w:%ld)\n",
+       if (dump && !ima_limit_imbalance(file)) {
+               printk(KERN_INFO "%s: open/free imbalance (r:%u w:%u)\n",
                       __func__, iint->readcount, iint->writecount);
                dump_stack();
        }