public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Siddhesh Poyarekar <siddhesh@gotplt.org>
To: Mark Wielaard <mark@klomp.org>, elfutils-devel@sourceware.org
Subject: Re: [PATCH] libdwfl: Rewrite reading of ar_size in elf_begin_rand
Date: Fri, 29 Jul 2022 12:31:10 -0400	[thread overview]
Message-ID: <64196909-f1ae-3c54-5485-8e89f1dba9c8@gotplt.org> (raw)
In-Reply-To: <fe190da2-4341-efac-10f4-9de893b19b1b@gotplt.org>

On 2022-07-28 13:27, Siddhesh Poyarekar wrote:
> Interesting, I'll take a closer look at this from the gcc context.  I 
> obviously don't have any strong opinions about the elfutils patch :)

I reduced this to the below program and I see it warns with 
`-D_FORTIFY_SOURCE=3 -O2 -fsanitize=undefined` as well as 
`-D_FORTIFY_SOURCE=2 -O2 -fsanitize=undefined`.  This is definitely a 
false positive on unreachable code; __pread_alias will never be called 
when nbytes is greater than object size.

So I can confirm that this is harmless.

Sid

~~~

typedef long ssize_t;
typedef unsigned long size_t;
typedef long off_t;


struct ar_hdr
{
   char ar_mode;
   char ar_size[10];
};

extern ssize_t __pread_chk (int fd, void *buf, size_t nbytes, off_t offset,
			    size_t bufsize)
   __attribute__((__access__ (__write_only__, 2, 3)));
extern ssize_t __pread_alias (int fd, void *buf, size_t nbytes, off_t 
offset)
   __attribute__((__access__ (__write_only__, 2, 3)));
extern ssize_t __pread_chk_warn (int fd, void *buf, size_t nbytes,
				 off_t offset, size_t bufsize);

extern __inline __attribute__((__always_inline__))
   __attribute__((__gnu_inline__)) ssize_t
pread (int fd, void *buf, size_t nbytes, off_t offset)
{
   size_t osz = __builtin_dynamic_object_size (buf, 0);
   if (__builtin_constant_p (osz) && osz == (size_t) -1)
     return __pread_alias (fd, buf, nbytes, offset);
   return (((__typeof (nbytes)) 0 < (__typeof (nbytes)) - 1
	   || (__builtin_constant_p (nbytes) && (nbytes) > 0))
	  && __builtin_constant_p (nbytes <= osz / 1)
	  && nbytes <= osz / 1)
     ? __pread_alias (fd, buf, nbytes, offset)
     : __pread_chk (fd, buf, nbytes, offset, osz);
}

ssize_t
pread_retry (int fd, off_t start_offset, off_t offset)
{
   ssize_t recvd = 0;
   struct ar_hdr h = {.ar_size = {0} };
   void *buf = h.ar_size;
   size_t len = sizeof (h.ar_size);
   off_t off =
     start_offset + offset + __builtin_offsetof (struct ar_hdr, ar_size);

   do
     {
       long int res;
       do
	{
           res = pread (fd, ((char *) buf) + recvd, len - recvd, off + 
recvd);
	}
       while (res == -1L);
       recvd += res;
     }
   while ((size_t) recvd < len);

   return recvd;
}

  reply	other threads:[~2022-07-29 16:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-28 13:48 Mark Wielaard
2022-07-28 13:54 ` Mark Wielaard
2022-07-28 17:27 ` Siddhesh Poyarekar
2022-07-29 16:31   ` Siddhesh Poyarekar [this message]
2022-07-29 18:34 ` Mark Wielaard

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=64196909-f1ae-3c54-5485-8e89f1dba9c8@gotplt.org \
    --to=siddhesh@gotplt.org \
    --cc=elfutils-devel@sourceware.org \
    --cc=mark@klomp.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).