public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Navin P <navinp0304@gmail.com>
To: Sam James <sam@gentoo.org>
Cc: gcc-help@gcc.gnu.org
Subject: Re: sanitizer not detecting buffer overrun
Date: Sun, 12 Nov 2023 10:44:58 +0530	[thread overview]
Message-ID: <CALO2TqKtxvhShV6XQ4HjxY7uwVEjSUvtGD=bTSGOQGnG9duOmg@mail.gmail.com> (raw)
In-Reply-To: <877cmn66kl.fsf@gentoo.org>

Hi,

On Sun, Nov 12, 2023 at 8:06 AM Sam James <sam@gentoo.org> wrote:
>
>
> Navin P via Gcc-help <gcc-help@gcc.gnu.org> writes:
>
> > Hi,
> >
> > Why doesn't sanitizer catch this ? The value ptr is a valid address but it
> > did a buffer overflow into another object a3 and then it is a valid
> > address. This is from production code where a ptr whose base was different
> > array address overflows into another array and becomes a valid address.
> > This is not caught by address sanitizer.
> >
> >    - How do you detect this and fix this ? Are there any alternative
> >    datastructures in C or C++ that prevent these kind of overruns
> >    Please don't increase the cookie or red zone size between arrays. Again
> >    sizes more than the cookie or redzone between arrays or objects can be
> >    overrun
>
> You appear to have answered your own question unless I'm
> misunderstanding you?
>
> ASAN does not claim to capture every possible overflow. It has to strike
> a balance, for one, between performance and catching errors (it has some
> other trade-offs too).
>
> Are you interested in a broad technical discussion about alternatives
> to redzones and other mitigations like SSP (which is unrelated here...)
> or are you wondering specifically just about how ASAN works and why it
> missed something?
>
I was asking if there exists any way to catch such errors ?
Assuming right now there doesn't exist any such implementation i was
thinking of an approach as to how it should be.
All pointers are null by default. Every pointer has a [start,end) . pointer
arithmetic even though it is not dereferenced cannot go beyond the end.
The start,end can be stored in a hash table for each pointer and it should
always stay within the bounds. Assignment of pointers copies the [start,end]
range to the lvalue from the rvalue.

ptr=ptr+x if x is greater than array size +1 is undefined
ptr=ptr+x-y where x=array size+10 and y=11 is defined

Do you think this is right and covers all cases ? Are there better ways ?
What does it take to implement this ? Maybe I can try or is it too complicated ?

> To me, the intent of your email seems mixed.
>
> >
> >
> >
> > navin@Navin-acer-5740:~/cpp$ gcc -fsanitize=address sanitizer.c
> > navin@Navin-acer-5740:~/cpp$ ./a.out
> > a1=(0x614000000040-0x6140000001d0) a2=(0x614000000240-0x6140000003d0)
> > a3=(0x614000000440-0x6140000005d0)
> > value=0, ptr=0x614000000498
> > ptr lies in the array a3
> > navin@Navin-acer-5740:~/cpp$ cat sanitizer.c
> > #include<stdlib.h>
> > #include<stdio.h>
> > int main(){
> > int *a1=calloc(100,sizeof(int));
> > int *a2=calloc(100,sizeof(int));
> > int *a3=calloc(100,sizeof(int));
> >
> > printf("a1=(%p-%p) a2=(%p-%p) a3=(%p-%p)\n",a1,a1+100,a2,a2+100,a3,a3+100);
> > int *ptr=a2;
> > ptr+=150;
> > printf("value=%d, ptr=%p\n",*ptr,ptr);
> > if(a3<=ptr && ptr<=a3+100) printf("ptr lies in the array a3\n");
> >
> > free(a1);
> > free(a2);
> > free(a3);
> > }
> > navin@Navin-acer-5740:~/cpp$
> >
> >
> > Regards,
> > Navin
>

  reply	other threads:[~2023-11-12  5:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-12  0:01 Navin P
2023-11-12  2:34 ` Sam James
2023-11-12  5:14   ` Navin P [this message]
2023-11-12 10:02     ` Xi Ruoyao
2023-11-12  9:37 ` Jonathan Wakely

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='CALO2TqKtxvhShV6XQ4HjxY7uwVEjSUvtGD=bTSGOQGnG9duOmg@mail.gmail.com' \
    --to=navinp0304@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=sam@gentoo.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).