* [PATCH v2] ima: Fix NULL pointer dereference in ima_file_hash
@ 2020-09-16 12:49 KP Singh
2020-09-16 13:36 ` KP Singh
2020-09-16 16:00 ` Mimi Zohar
0 siblings, 2 replies; 4+ messages in thread
From: KP Singh @ 2020-09-16 12:49 UTC (permalink / raw)
To: linux-kernel, linux-security-module
Cc: Florent Revest, Mimi Zohar, James Morris, Kees Cook,
Lakshmi Ramasubramanian, Jann Horn
From: KP Singh <kpsingh@google.com>
ima_file_hash can be called when there is no iint->ima_hash available
even though the inode exists in the integrity cache.
An 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: 6beea7afcc72 ("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>
---
security/integrity/ima/ima_main.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c
index 8a91711ca79b..4c86cd4eece0 100644
--- a/security/integrity/ima/ima_main.c
+++ b/security/integrity/ima/ima_main.c
@@ -531,6 +531,16 @@ int ima_file_hash(struct file *file, char *buf, size_t buf_size)
return -EOPNOTSUPP;
mutex_lock(&iint->mutex);
+
+ /*
+ * ima_file_hash can be called when ima_collect_measurement has still
+ * not been called, we might not always have a hash.
+ */
+ if (!iint->ima_hash) {
+ mutex_unlock(&iint->mutex);
+ return -EOPNOTSUPP;
+ }
+
if (buf) {
size_t copied_size;
--
2.28.0.526.ge36021eeef-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] ima: Fix NULL pointer dereference in ima_file_hash
2020-09-16 12:49 [PATCH v2] ima: Fix NULL pointer dereference in ima_file_hash KP Singh
@ 2020-09-16 13:36 ` KP Singh
2020-09-16 16:00 ` Mimi Zohar
1 sibling, 0 replies; 4+ messages in thread
From: KP Singh @ 2020-09-16 13:36 UTC (permalink / raw)
To: Linux Security Module list
On Wed, Sep 16, 2020 at 2:49 PM KP Singh <kpsingh@chromium.org> wrote:
>
> From: KP Singh <kpsingh@google.com>
[...]
Another attempt to get this on the lists.
> diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c
> index 8a91711ca79b..4c86cd4eece0 100644
> --- a/security/integrity/ima/ima_main.c
> +++ b/security/integrity/ima/ima_main.c
> @@ -531,6 +531,16 @@ int ima_file_hash(struct file *file, char *buf, size_t buf_size)
> return -EOPNOTSUPP;
>
> mutex_lock(&iint->mutex);
> +
> + /*
> + * ima_file_hash can be called when ima_collect_measurement has still
> + * not been called, we might not always have a hash.
> + */
> + if (!iint->ima_hash) {
> + mutex_unlock(&iint->mutex);
> + return -EOPNOTSUPP;
> + }
> +
> if (buf) {
> size_t copied_size;
>
> --
> 2.28.0.526.ge36021eeef-goog
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] ima: Fix NULL pointer dereference in ima_file_hash
2020-09-16 12:49 [PATCH v2] ima: Fix NULL pointer dereference in ima_file_hash KP Singh
2020-09-16 13:36 ` KP Singh
@ 2020-09-16 16:00 ` Mimi Zohar
2020-09-16 16:59 ` KP Singh
1 sibling, 1 reply; 4+ messages in thread
From: Mimi Zohar @ 2020-09-16 16:00 UTC (permalink / raw)
To: KP Singh, linux-kernel, linux-security-module
Cc: Florent Revest, James Morris, Kees Cook, Lakshmi Ramasubramanian,
Jann Horn
On Wed, 2020-09-16 at 14:49 +0200, KP Singh wrote:
> From: KP Singh <kpsingh@google.com>
>
> ima_file_hash can be called when there is no iint->ima_hash available
> even though the inode exists in the integrity cache.
>
> An 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: 6beea7afcc72 ("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>
> ---
> security/integrity/ima/ima_main.c | 10 ++++++++++
> 1 file changed, 10 insertions(+)
>
> diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c
> index 8a91711ca79b..4c86cd4eece0 100644
> --- a/security/integrity/ima/ima_main.c
> +++ b/security/integrity/ima/ima_main.c
> @@ -531,6 +531,16 @@ int ima_file_hash(struct file *file, char *buf, size_t buf_size)
> return -EOPNOTSUPP;
>
> mutex_lock(&iint->mutex);
> +
> + /*
> + * ima_file_hash can be called when ima_collect_measurement has still
> + * not been called, we might not always have a hash.
> + */
> + if (!iint->ima_hash) {
> + mutex_unlock(&iint->mutex);
> + return -EOPNOTSUPP;
> + }
> +
Not having a file hash is rather common (e.g. mknodat, prior to the
file being closed). Before appraising the integrity of a file, it
checks whether it is a new file (eg. IMA_NEW_FILE), but, unfortunately,
the flag is only set for those files in the appraise policy.
The patch looks fine, but you might want to reflect not having a file
hash is common in the patch description.
Mimi
> if (buf) {
> size_t copied_size;
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] ima: Fix NULL pointer dereference in ima_file_hash
2020-09-16 16:00 ` Mimi Zohar
@ 2020-09-16 16:59 ` KP Singh
0 siblings, 0 replies; 4+ messages in thread
From: KP Singh @ 2020-09-16 16:59 UTC (permalink / raw)
To: Mimi Zohar
Cc: open list, Linux Security Module list, Florent Revest,
James Morris, Kees Cook, Lakshmi Ramasubramanian, Jann Horn
On Wed, Sep 16, 2020 at 6:00 PM Mimi Zohar <zohar@linux.ibm.com> wrote:
>
> On Wed, 2020-09-16 at 14:49 +0200, KP Singh wrote:
> > From: KP Singh <kpsingh@google.com>
> >
> > ima_file_hash can be called when there is no iint->ima_hash available
> > even though the inode exists in the integrity cache.
> >
> > An 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: 6beea7afcc72 ("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>
> > ---
> > security/integrity/ima/ima_main.c | 10 ++++++++++
> > 1 file changed, 10 insertions(+)
> >
> > diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c
> > index 8a91711ca79b..4c86cd4eece0 100644
> > --- a/security/integrity/ima/ima_main.c
> > +++ b/security/integrity/ima/ima_main.c
> > @@ -531,6 +531,16 @@ int ima_file_hash(struct file *file, char *buf, size_t buf_size)
> > return -EOPNOTSUPP;
> >
> > mutex_lock(&iint->mutex);
> > +
> > + /*
> > + * ima_file_hash can be called when ima_collect_measurement has still
> > + * not been called, we might not always have a hash.
> > + */
> > + if (!iint->ima_hash) {
> > + mutex_unlock(&iint->mutex);
> > + return -EOPNOTSUPP;
> > + }
> > +
>
> Not having a file hash is rather common (e.g. mknodat, prior to the
> file being closed). Before appraising the integrity of a file, it
> checks whether it is a new file (eg. IMA_NEW_FILE), but, unfortunately,
> the flag is only set for those files in the appraise policy.
Makes sense.
>
> The patch looks fine, but you might want to reflect not having a file
> hash is common in the patch description.
>
Thanks! Will send another revision with an updated description.
- KP
> Mimi
>
> > if (buf) {
> > size_t copied_size;
> >
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2020-09-16 20:57 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-16 12:49 [PATCH v2] ima: Fix NULL pointer dereference in ima_file_hash KP Singh
2020-09-16 13:36 ` KP Singh
2020-09-16 16:00 ` Mimi Zohar
2020-09-16 16:59 ` KP Singh
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).