mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mimi Zohar <zohar@linux.ibm.com>
To: Roberto Sassu <roberto.sassu@huaweicloud.com>,
	dmitry.kasatkin@gmail.com, eric.snowberg@oracle.com,
	paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com,
	fabrice.derepas@canonical.com
Cc: linux-integrity@vger.kernel.org,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Roberto Sassu <roberto.sassu@huawei.com>
Subject: Re: [PATCH] ima: Fix out-of-bounds read in ima_read_policy()
Date: Mon, 23 Nov 2026 15:33:11 -0500	[thread overview]
Message-ID: <d97e7b9c48b38c59023b8a8b18779b5c3318af2f.camel@linux.ibm.com> (raw)
In-Reply-To: <20261112143029.4159022-1-roberto.sassu@huaweicloud.com>

On Thu, 2026-11-12 at 15:30 +0100, Roberto Sassu wrote:
> From: Roberto Sassu <roberto.sassu@huawei.com>
> 
> ima_read_policy() reads the policy file with kernel_read_file_from_path()
> and splits it into rules with strsep(). However, the buffer allocated by
> kernel_read_file_from_path() is exactly the size of the file and is not
> NUL-terminated. If the policy file does not end with a newline, strsep()
> scans past the end of the buffer while looking for the next separator. If
> the file size is a multiple of PAGE_SIZE, the scan reaches the vmalloc
> guard page and the kernel oopses.
> 
> If the last byte of the policy is not a newline, parse a NUL-terminated
> copy of the buffer obtained with kmemdup_nul() instead, and account for
> the NUL terminator in the remaining size, since ima_parse_add_rule()
> counts it in the length of the last rule.
> 
> As a side effect, a policy whose last rule is not followed by a newline
> is now loaded, instead of being rejected with -EINVAL due to the
> terminator being counted in the length of the last rule but not in the
> file size.
> 
> Fixes: 7429b092811f ("ima: load policy using path")
> Reported-by: Fabrice Derepas <fabrice.derepas@canonical.com>
> Closes: https://lore.kernel.org/linux-integrity/20260814085443.1211989-1-fabrice.derepas@canonical.com/
> Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> ---
>  security/integrity/ima/ima_fs.c | 20 +++++++++++++++++++-
>  1 file changed, 19 insertions(+), 1 deletion(-)
> 
> diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c
> index 2a0bca554316..6d5e061f88c2 100644
> --- a/security/integrity/ima/ima_fs.c
> +++ b/security/integrity/ima/ima_fs.c
> @@ -528,7 +528,7 @@ static const struct file_operations ima_ascii_measurements_staged_ops = {
>  static ssize_t ima_read_policy(char *path)
>  {
>  	void *data = NULL;
> -	char *datap;
> +	char *datap, *buf = NULL;
>  	size_t size;
>  	int rc, pathlen = strlen(path);
>  
> @@ -548,6 +548,23 @@ static ssize_t ima_read_policy(char *path)
>  	rc = 0;
>  
>  	datap = data;
> +
> +	/*
> +	 * kernel_read_file_from_path() does not NUL-terminate the buffer.
> +	 * If the policy does not end with a newline, parse a NUL-terminated
> +	 * copy, so that strsep() does not scan past the end of the buffer.
> +	 */
> +	if (size && datap[size - 1] != '\n') {

Hi Roberto, 

Why allocate a copy rather than reject the file when the last byte is neither
'\n' nor '\0'?

if (size && datap[size - 1] != '\n' && datap[size - 1] != '\0') {
	vfree(data);
	return -EINVAL;
}

That avoids the out-of-bounds read with no allocation, and it keeps the current
behavior, since a policy with an unterminated last rule is already rejected with
-EINVAL. Is accepting a policy with no trailing newline an intended change, or
just a side effect of the fix?

Thanks,

Mimi

> +		buf = kmemdup_nul(data, size, GFP_KERNEL);
> +		if (!buf) {
> +			vfree(data);
> +			return -ENOMEM;
> +		}
> +		datap = buf;
> +		/* Count the NUL terminator, included in ima_parse_add_rule(). */
> +		size++;
> +	}
> +
>  	while (size > 0 && (p = strsep(&datap, "\n"))) {
>  		pr_debug("rule: %s\n", p);
>  		rc = ima_parse_add_rule(p);
> @@ -557,6 +574,7 @@ static ssize_t ima_read_policy(char *path)
>  	}
>  
>  	vfree(data);
> +	kfree(buf);
>  	if (rc < 0)
>  		return rc;
>  	else if (size)

      reply	other threads:[~2026-11-23 21:10 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-14  8:54 [PATCH] ima: bound line scan in ima_read_policy() to fix OOB read Fabrice Derepas
2026-08-26  9:51 ` Roberto Sassu
2026-10-09 13:21   ` Roberto Sassu
2026-11-12 14:30     ` [PATCH] ima: Fix out-of-bounds read in ima_read_policy() Roberto Sassu
2026-11-23 20:33       ` Mimi Zohar [this message]

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=d97e7b9c48b38c59023b8a8b18779b5c3318af2f.camel@linux.ibm.com \
    --to=zohar@linux.ibm.com \
    --cc=dmitry.kasatkin@gmail.com \
    --cc=eric.snowberg@oracle.com \
    --cc=fabrice.derepas@canonical.com \
    --cc=jmorris@namei.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=paul@paul-moore.com \
    --cc=roberto.sassu@huawei.com \
    --cc=roberto.sassu@huaweicloud.com \
    --cc=serge@hallyn.com \
    /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

all inboxes | Powered by JetHome®