Re: [PATCH v10 2/9] fs: introduce kernel_pread_file* support

From: Mimi Zohar
Date: Tue Jul 07 2020 - 20:25:02 EST


On Tue, 2020-07-07 at 16:56 -0700, Kees Cook wrote:
> > @@ -951,21 +955,32 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂret = -EINVAL;
> >ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂgoto out;
> >ÂÂÂÂÂÂÂ}
> > -ÂÂÂÂÂif (i_size > SIZE_MAX || (max_size > 0 && i_size > max_size)) {
> > +
> > +ÂÂÂÂÂ/* Default read to end of file */
> > +ÂÂÂÂÂread_end = i_size;
> > +
> > +ÂÂÂÂÂ/* Allow reading partial portion of file */
> > +ÂÂÂÂÂif ((id == READING_FIRMWARE_PARTIAL_READ) &&
> > +ÂÂÂÂÂÂÂÂ (i_size > (pos + max_size)))
> > +ÂÂÂÂÂÂÂÂÂÂÂÂÂread_end = pos + max_size;
>
> There's no need to involve "id" here. There are other signals about
> what's happening (i.e. pos != 0, max_size != i_size, etc).

Both the pre and post security kernel_read_file hooks are called here,
but there isn't enough information being passed to the LSM/IMA to be
able to different which hook is applicable. ÂOne method of providing
that additional information is by enumeration. ÂThe other option would
be to pass some additional information.

For example, on the post kernel_read_file hook, the file is read once
into memory. ÂIMA calculates the firmware file hash based on the
buffer contents. ÂOn the pre kernel_read_file hook, IMA would need to
read the entire file, calculating the file hash. ÂBoth methods of
calculating the file hash work, but the post hook is more efficient.

Mimi