Skip to content

Commit bade72d

Browse files
eparistorvalds
authored andcommitted
IMA: fix the ToMToU logic
Current logic looks like this: rc = ima_must_measure(NULL, inode, MAY_READ, FILE_CHECK); if (rc < 0) goto out; if (mode & FMODE_WRITE) { if (inode->i_readcount) send_tomtou = true; goto out; } if (atomic_read(&inode->i_writecount) > 0) send_writers = true; Lets assume we have a policy which states that all files opened for read by root must be measured. Lets assume the file has permissions 777. Lets assume that root has the given file open for read. Lets assume that a non-root process opens the file write. The non-root process will get to ima_counts_get() and will check the ima_must_measure(). Since it is not supposed to measure it will goto out. We should check the i_readcount no matter what since we might be causing a ToMToU voilation! This is close to correct, but still not quite perfect. The situation could have been that root, which was interested in the mesurement opened and closed the file and another process which is not interested in the measurement is the one holding the i_readcount ATM. This is just overly strict on ToMToU violations, which is better than not strict enough... 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 196f518 commit bade72d

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

security/integrity/ima/ima_main.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,23 @@ void ima_counts_get(struct file *file)
112112
if (!ima_initialized)
113113
goto out;
114114

115-
rc = ima_must_measure(NULL, inode, MAY_READ, FILE_CHECK);
116-
if (rc < 0)
117-
goto out;
118-
119115
if (mode & FMODE_WRITE) {
120-
if (inode->i_readcount)
116+
if (inode->i_readcount && IS_IMA(inode))
121117
send_tomtou = true;
122118
goto out;
123119
}
124120

121+
rc = ima_must_measure(NULL, inode, MAY_READ, FILE_CHECK);
122+
if (rc < 0)
123+
goto out;
124+
125125
if (atomic_read(&inode->i_writecount) > 0)
126126
send_writers = true;
127127
out:
128128
/* remember the vfs deals with i_writecount */
129129
if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
130130
inode->i_readcount++;
131+
131132
spin_unlock(&inode->i_lock);
132133

133134
if (send_tomtou)

0 commit comments

Comments
 (0)