From: Mimi Zohar <zohar@linux.ibm.com>
To: Goldwyn Rodrigues <rgoldwyn@suse.de>
Cc: linux-integrity@vger.kernel.org, linux-unionfs@vger.kernel.org,
iforster@suse.de, fvogt@suse.de, miklos@szeredi.hu
Subject: Re: [PATCH] Open a new file instance if no read permissions on files
Date: Sat, 06 Oct 2018 21:01:01 -0400 [thread overview]
Message-ID: <1538874061.4914.16.camel@linux.ibm.com> (raw)
In-Reply-To: <20181005214213.ickkfgu5a7tzzenk@merlin>
On Fri, 2018-10-05 at 16:42 -0500, Goldwyn Rodrigues wrote:
> Open a new file instance as opposed to changing file->f_mode when
> the file is not readable.
>
> This is done to accomodate overlayfs stacked file operations change. The
> real struct file is hidden behind the overlays struct file. So, any
> file->f_mode manipulations are not reflected on the real struct file.
> Open the file again, read andcalculate the hash.
>
> Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
>
> diff --git a/security/integrity/ima/ima_crypto.c b/security/integrity/ima/ima_crypto.c
> index 7e7e7e7c250a..3848cf208792 100644
> --- a/security/integrity/ima/ima_crypto.c
> +++ b/security/integrity/ima/ima_crypto.c
> @@ -210,7 +210,7 @@ static int ima_calc_file_hash_atfm(struct file *file,
> {
> loff_t i_size, offset;
> char *rbuf[2] = { NULL, };
> - int rc, read = 0, rbuf_len, active = 0, ahash_rc = 0;
> + int rc, rbuf_len, active = 0, ahash_rc = 0;
> struct ahash_request *req;
> struct scatterlist sg[1];
> struct crypto_wait wait;
> @@ -257,11 +257,6 @@ static int ima_calc_file_hash_atfm(struct file *file,
> &rbuf_size[1], 0);
> }
>
> - if (!(file->f_mode & FMODE_READ)) {
> - file->f_mode |= FMODE_READ;
> - read = 1;
> - }
> -
> for (offset = 0; offset < i_size; offset += rbuf_len) {
> if (!rbuf[1] && offset) {
> /* Not using two buffers, and it is not the first
> @@ -300,8 +295,6 @@ static int ima_calc_file_hash_atfm(struct file *file,
> /* wait for the last update request to complete */
> rc = ahash_wait(ahash_rc, &wait);
> out3:
> - if (read)
> - file->f_mode &= ~FMODE_READ;
> ima_free_pages(rbuf[0], rbuf_size[0]);
> ima_free_pages(rbuf[1], rbuf_size[1]);
> out2:
> @@ -336,7 +329,7 @@ static int ima_calc_file_hash_tfm(struct file *file,
> {
> loff_t i_size, offset = 0;
> char *rbuf;
> - int rc, read = 0;
> + int rc;
> SHASH_DESC_ON_STACK(shash, tfm);
>
> shash->tfm = tfm;
> @@ -357,11 +350,6 @@ static int ima_calc_file_hash_tfm(struct file *file,
> if (!rbuf)
> return -ENOMEM;
>
> - if (!(file->f_mode & FMODE_READ)) {
> - file->f_mode |= FMODE_READ;
> - read = 1;
> - }
> -
> while (offset < i_size) {
> int rbuf_len;
>
> @@ -378,8 +366,6 @@ static int ima_calc_file_hash_tfm(struct file *file,
> if (rc)
> break;
> }
> - if (read)
> - file->f_mode &= ~FMODE_READ;
> kfree(rbuf);
> out:
> if (!rc)
> @@ -419,7 +405,7 @@ static int ima_calc_file_shash(struct file *file, struct ima_digest_data *hash)
> int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
> {
> loff_t i_size;
> - int rc;
> + int read = 0, rc;
>
> /*
> * For consistency, fail file's opened with the O_DIRECT flag on
> @@ -431,15 +417,29 @@ int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
> return -EINVAL;
> }
>
> + if (!(file->f_mode & FMODE_READ)) {
> + struct file *f;
I would define "struct file *f = file" above, at the beginning of
function, and "free(f)" below, without modifying "file".
> + int flags = file->f_flags & ~(O_WRONLY | O_APPEND | O_TRUNC | O_CREAT | O_NOCTTY | O_EXCL);
Doesn't O_RDONLY need to be added?
Please fix the line length.
> + f = dentry_open(&file->f_path, flags, file->f_cred);
> + if (IS_ERR(f))
> + return PTR_ERR(f);
> + read = 1;
> + file = f;
With the above change, no need to modify "file".
Mimi
> + }
> +
> i_size = i_size_read(file_inode(file));
>
> if (ima_ahash_minsize && i_size >= ima_ahash_minsize) {
> rc = ima_calc_file_ahash(file, hash);
> if (!rc)
> - return 0;
> + goto out;
> }
>
> - return ima_calc_file_shash(file, hash);
> + rc = ima_calc_file_shash(file, hash);
> +out:
> + if (read)
> + fput(file);
> + return rc;
> }
>
> /*
>
next prev parent reply other threads:[~2018-10-07 1:01 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-10-05 21:42 [PATCH] Open a new file instance if no read permissions on files Goldwyn Rodrigues
2018-10-07 1:01 ` Mimi Zohar [this message]
2018-10-08 12:14 ` Goldwyn Rodrigues
2018-10-08 13:27 ` Mimi Zohar
2018-10-08 15:30 ` Goldwyn Rodrigues
2018-10-08 21:18 ` Mimi Zohar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1538874061.4914.16.camel@linux.ibm.com \
--to=zohar@linux.ibm.com \
--cc=fvogt@suse.de \
--cc=iforster@suse.de \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-unionfs@vger.kernel.org \
--cc=miklos@szeredi.hu \
--cc=rgoldwyn@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).