Skip to content

Commit b9593d3

Browse files
eparistorvalds
authored andcommitted
IMA: use i_writecount rather than a private counter
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>
1 parent ad16ad0 commit b9593d3

3 files changed

Lines changed: 6 additions & 17 deletions

File tree

‎security/integrity/ima/ima.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ struct ima_iint_cache {
108108
struct mutex mutex; /* protects: version, flags, digest */
109109
/* protected by inode->i_lock */
110110
unsigned int readcount; /* measured files readcount */
111-
unsigned int writecount;/* measured files writecount */
112111
struct kref refcount; /* ima_iint_cache reference count */
113112
};
114113

‎security/integrity/ima/ima_iint.c‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,6 @@ void iint_free(struct kref *kref)
129129
iint->readcount);
130130
iint->readcount = 0;
131131
}
132-
if (iint->writecount != 0) {
133-
printk(KERN_INFO "%s: writecount: %u\n", __func__,
134-
iint->writecount);
135-
iint->writecount = 0;
136-
}
137132
kref_init(&iint->refcount);
138133
kmem_cache_free(iint_cache, iint);
139134
}
@@ -166,7 +161,6 @@ static void init_once(void *foo)
166161
iint->flags = 0UL;
167162
mutex_init(&iint->mutex);
168163
iint->readcount = 0;
169-
iint->writecount = 0;
170164
kref_init(&iint->refcount);
171165
}
172166

‎security/integrity/ima/ima_main.c‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ static void ima_inc_counts(struct ima_iint_cache *iint, fmode_t mode)
9494

9595
if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
9696
iint->readcount++;
97-
if (mode & FMODE_WRITE)
98-
iint->writecount++;
9997
}
10098

10199
/*
@@ -173,18 +171,16 @@ static void ima_dec_counts(struct ima_iint_cache *iint, struct inode *inode,
173171
iint->readcount--;
174172
}
175173
if (mode & FMODE_WRITE) {
176-
if (unlikely(iint->writecount == 0))
174+
if (atomic_read(&inode->i_writecount) <= 0)
177175
dump = true;
178-
iint->writecount--;
179-
if (iint->writecount == 0) {
180-
if (iint->version != inode->i_version)
181-
iint->flags &= ~IMA_MEASURED;
182-
}
176+
if (atomic_read(&inode->i_writecount) == 1 &&
177+
iint->version != inode->i_version)
178+
iint->flags &= ~IMA_MEASURED;
183179
}
184180

185181
if (dump && !ima_limit_imbalance(file)) {
186-
printk(KERN_INFO "%s: open/free imbalance (r:%u w:%u)\n",
187-
__func__, iint->readcount, iint->writecount);
182+
printk(KERN_INFO "%s: open/free imbalance (r:%u)\n",
183+
__func__, iint->readcount);
188184
dump_stack();
189185
}
190186
}

0 commit comments

Comments
 (0)