mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Paul Moore <paul@paul-moore.com>
To: Ricardo Robaina <rrobaina@redhat.com>,
	audit@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: eparis@redhat.com, Ricardo Robaina <rrobaina@redhat.com>
Subject: Re: [PATCH] audit: fix potential integer overflow in audit_log_n_hex()
Date: Tue, 26 May 2026 17:22:58 -0400	[thread overview]
Message-ID: <c94ce54e232d50dcfc7cef9ada654ed7@paul-moore.com> (raw)
In-Reply-To: <20260513130843.790380-1-rrobaina@redhat.com>

On May 13, 2026 Ricardo Robaina <rrobaina@redhat.com> wrote:
> 
> The function audit_log_n_hex() calculates new_len as len << 1 to
> account for hex encoding, where each byte becomes two characters.
> However, when len is passed as size_t but new_len was declared as
> int, this could lead to integer overflow for large values of len.
> 
> Additionally, the bit shift operation itself could overflow if len
> exceeds SIZE_MAX / 2, leading to incorrect buffer size calculations
> and potential memory corruption.
> 
> Fix this by changing new_len and loop counter i from int to size_t
> to match the len parameter type, preventing signed/unsigned mismatches,
> and by adding an overflow check before the bit shift operation that
> calculates len << 1.
> 
> Fixes: 168b7173959f ("AUDIT: Clean up logging of untrusted strings")
> Signed-off-by: Ricardo Robaina <rrobaina@redhat.com>
> ---
>  kernel/audit.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/audit.c b/kernel/audit.c
> index e1d489bc2dff..32ac50996451 100644
> --- a/kernel/audit.c
> +++ b/kernel/audit.c
> @@ -2076,7 +2076,8 @@ void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
>  void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
>  		size_t len)
>  {
> -	int i, avail, new_len;
> +	int avail;
> +	size_t i, new_len;
>  	unsigned char *ptr;
>  	struct sk_buff *skb;
>  
> @@ -2084,9 +2085,14 @@ void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf,
>  		return;
>  
>  	BUG_ON(!ab->skb);
> +
> +	/* prevent new_len overflow */
> +	if (len > SIZE_MAX / 2)
> +		return;
> +
>  	skb = ab->skb;
>  	avail = skb_tailroom(skb);
> -	new_len = len<<1;
> +	new_len = len << 1;
>  	if (new_len >= avail) {
>  		/* Round the buffer request up to the next multiple */
>  		new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);

I might suggest using check_shl_overflow() instead, mostly because it
helps document a shift that could potentially overflow.

--
paul-moore.com

  reply	other threads:[~2026-05-26 21:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-13 13:08 Ricardo Robaina
2026-05-26 21:22 ` Paul Moore [this message]
2026-05-27 19:57   ` Ricardo Robaina

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=c94ce54e232d50dcfc7cef9ada654ed7@paul-moore.com \
    --to=paul@paul-moore.com \
    --cc=audit@vger.kernel.org \
    --cc=eparis@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rrobaina@redhat.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

Powered by JetHome