Skip to content

Commit 226af18

Browse files
KP Singhgregkh
authored andcommitted
ima: Fix NULL pointer dereference in ima_file_hash
[ Upstream commit aa662fc ] ima_file_hash can be called when there is no iint->ima_hash available even though the inode exists in the integrity cache. It is fairly common for a file to not have a hash. (e.g. an mknodat, prior to the file being closed). Another example where this can happen (suggested by Jann Horn): Process A does: while(1) { unlink("/tmp/imafoo"); fd = open("/tmp/imafoo", O_RDWR|O_CREAT|O_TRUNC, 0700); if (fd == -1) { perror("open"); continue; } write(fd, "A", 1); close(fd); } and Process B does: while (1) { int fd = open("/tmp/imafoo", O_RDONLY); if (fd == -1) continue; char *mapping = mmap(NULL, 0x1000, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0); if (mapping != MAP_FAILED) munmap(mapping, 0x1000); close(fd); } Due to the race to get the iint->mutex between ima_file_hash and process_measurement iint->ima_hash could still be NULL. Fixes: 6beea7a ("ima: add the ability to query the cached hash of a given file") Signed-off-by: KP Singh <kpsingh@google.com> Reviewed-by: Florent Revest <revest@chromium.org> Signed-off-by: Mimi Zohar <zohar@linux.ibm.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 11a647d commit 226af18

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

‎security/integrity/ima/ima_main.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,16 @@ int ima_file_hash(struct file *file, char *buf, size_t buf_size)
531531
return -EOPNOTSUPP;
532532

533533
mutex_lock(&iint->mutex);
534+
535+
/*
536+
* ima_file_hash can be called when ima_collect_measurement has still
537+
* not been called, we might not always have a hash.
538+
*/
539+
if (!iint->ima_hash) {
540+
mutex_unlock(&iint->mutex);
541+
return -EOPNOTSUPP;
542+
}
543+
534544
if (buf) {
535545
size_t copied_size;
536546

0 commit comments

Comments
 (0)