public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Szabolcs Nagy <szabolcs.nagy@arm.com>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH v2] string: Fix OOB read on generic strncmp
Date: Wed, 22 Feb 2023 17:21:26 +0000	[thread overview]
Message-ID: <Y/ZPFmLgwSNgxphJ@arm.com> (raw)
In-Reply-To: <20230222163159.3446687-1-adhemerval.zanella@linaro.org>

The 02/22/2023 13:31, Adhemerval Zanella wrote:
> For unaligned case, reading ahead can only be done if parting reads
> matches the aligned input.
> 
> Also extend the stratcliff tests to check such cases.
> 
> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
> and powerpc-linux-gnu by removing the arch-specific assembly
> implementation and disabling multi-arch (it covers both LE and BE
> for 64 and 32 bits).

thanks this looks good.

Reviewed-by: Szabolcs Nagy <szabolcs.nagy@arm.com>


> ---
>  string/stratcliff.c | 17 ++++++++++++++++-
>  string/strncmp.c    | 13 ++++++++++++-
>  2 files changed, 28 insertions(+), 2 deletions(-)
> 
> diff --git a/string/stratcliff.c b/string/stratcliff.c
> index 74d64cc03d..88ac787088 100644
> --- a/string/stratcliff.c
> +++ b/string/stratcliff.c
> @@ -401,12 +401,27 @@ do_test (void)
>  		result = 1;
>  	      }
>  
> -	    if (STRNCMP (dest + nchars - outer, adr + middle, outer) <= 0)
> +	    /* Also check for size larger than the string.  */
> +	    if (STRNCMP (adr + middle, dest + nchars - outer, outer + 99) >= 0)
>  	      {
>  		printf ("%s 2 flunked for outer = %zu, middle = %zu, full\n",
> +			STRINGIFY (STRNCMP), outer + 99, middle);
> +		result = 1;
> +	      }
> +
> +	    if (STRNCMP (dest + nchars - outer, adr + middle, outer) <= 0)
> +	      {
> +		printf ("%s 3 flunked for outer = %zu, middle = %zu, full\n",
>  			STRINGIFY (STRNCMP), outer, middle);
>  		result = 1;
>  	      }
> +
> +	    if (STRNCMP (dest + nchars - outer, adr + middle, outer + 99) <= 0)
> +	      {
> +		printf ("%s 4 flunked for outer = %zu, middle = %zu, full\n",
> +			STRINGIFY (STRNCMP), outer + 99, middle);
> +		result = 1;
> +	      }
>  	  }
>  
>        /* strncpy/wcsncpy tests */
> diff --git a/string/strncmp.c b/string/strncmp.c
> index 4c8bf36bb9..751bf53d55 100644
> --- a/string/strncmp.c
> +++ b/string/strncmp.c
> @@ -73,7 +73,11 @@ strncmp_unaligned_loop (const op_t *x1, const op_t *x2, op_t w1, uintptr_t ofs,
>    uintptr_t sh_2 = sizeof(op_t) * CHAR_BIT - sh_1;
>  
>    op_t w2 = MERGE (w2a, sh_1, (op_t)-1, sh_2);
> -  if (!has_zero (w2) && n > (sizeof (op_t) - ofs))
> +
> +  /* Reading ahead is wrong if w1 and w2 already differs.  */
> +  op_t w1a = MERGE (w1, 0, (op_t)-1, sh_2);
> +
> +  if (!has_zero (w2) && w2 == w1a && n >= (sizeof (op_t) - ofs))
>      {
>        op_t w2b;
>  
> @@ -90,6 +94,13 @@ strncmp_unaligned_loop (const op_t *x1, const op_t *x2, op_t w1, uintptr_t ofs,
>  	  if (has_zero (w2b) || n <= (sizeof (op_t) - ofs))
>  	    break;
>  	  w1 = *x1++;
> +
> +	  /* Reading ahead is wrong if w1 and w2 already differs.  */
> +	  w2 = MERGE (w2b, sh_1, (op_t)-1, sh_2);
> +	  w1a = MERGE (w1, 0, (op_t)-1, sh_2);
> +	  if (w2 != w1a)
> +	    return final_cmp (w1a, w2, n);
> +
>  	  w2a = w2b;
>  	}
>  
> -- 
> 2.34.1
> 

  reply	other threads:[~2023-02-22 17:21 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-22 16:31 Adhemerval Zanella
2023-02-22 17:21 ` Szabolcs Nagy [this message]
2023-02-23 18:15   ` Adhemerval Zanella Netto
2023-02-24 10:19     ` Szabolcs Nagy
2023-02-24 10:58       ` Florian Weimer
2023-02-23 19:10 Wilco Dijkstra
2023-02-24 12:24 Wilco Dijkstra
2023-02-24 12:34 ` Adhemerval Zanella Netto
2023-02-24 12:43 ` Szabolcs Nagy
2023-02-24 12:57   ` Adhemerval Zanella Netto

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=Y/ZPFmLgwSNgxphJ@arm.com \
    --to=szabolcs.nagy@arm.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.org \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).