public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Szabolcs Nagy <szabolcs.nagy@arm.com>
To: Florian Weimer <fw@deneb.enyo.de>, libc-alpha@sourceware.org
Cc: nd@arm.com
Subject: Re: [PATCH] libio: Disable vtable validation in case of interposition [BZ #23313]
Date: Tue, 19 Jun 2018 10:38:00 -0000	[thread overview]
Message-ID: <773dfddc-66ed-730f-d8b3-a0c9392cc5de@arm.com> (raw)
In-Reply-To: <E1fVDN1-0003cW-8d@mid.deneb.enyo.de>

On 19/06/18 10:56, Florian Weimer wrote:
> 2018-06-19  Florian Weimer  <fweimer@redhat.com>
> 
> 	[BZ #23313]
> 	* libio/stdfiles.c (STDFILE_SECTION): Define macro.
> 	(DEF_STDFILE): Use it.
> 	* libio/vtables.c [SHARED] (__libc_IO_stdfiles): Declare as symbol
> 	set.
> 	[SHARED] (interposed_stdfiles_variable): New function.
> 	(stdfiles_interposed): Likewise.
> 	(_IO_vtable_check): Call stdfiles_interposed.
> 
> diff --git a/libio/stdfiles.c b/libio/stdfiles.c
> index 18e1172ad0..2435f412f2 100644
> --- a/libio/stdfiles.c
> +++ b/libio/stdfiles.c
> @@ -33,11 +33,19 @@
>   
>   #include "libioP.h"
>   
> +#ifdef SHARED
> +/* Place the variables defined below in a separate section for the
> +   interposition check in vtables.c.  */
> +# define STDFILE_SECTION __attribute__ ((section ("__libc_IO_stdfiles")))
> +#else
> +# define STDFILE_SECTION
> +#endif
> +
>   #define DEF_STDFILE(NAME, FD, CHAIN, FLAGS) \
>     static _IO_lock_t _IO_stdfile_##FD##_lock = _IO_lock_initializer; \
>     static struct _IO_wide_data _IO_wide_data_##FD \
>       = { ._wide_vtable = &_IO_wfile_jumps }; \
> -  struct _IO_FILE_plus NAME \
> +  struct _IO_FILE_plus NAME STDFILE_SECTION \
>       = {FILEBUF_LITERAL(CHAIN, FLAGS, FD, &_IO_wide_data_##FD), \
>          &_IO_file_jumps};
>   
> diff --git a/libio/vtables.c b/libio/vtables.c
> index 9fd4ccf642..aba414ef2d 100644
> --- a/libio/vtables.c
> +++ b/libio/vtables.c
> @@ -29,11 +29,44 @@ void (*IO_accept_foreign_vtables) (void) attribute_hidden;
>   extern struct dl_open_hook *_dl_open_hook;
>   libc_hidden_proto (_dl_open_hook);
>   
> +/* Used to detect interposition of _IO_2_1_stdin_, _IO_2_1_stdout_,
> +   _IO_2_1_stderr_.  See stdfiles.c.  */
> +symbol_set_declare (__libc_IO_stdfiles)
> +
> +/* Check whether the standard file variable at *PTR has been
> +   interposed, by comparing its address against the start and end of
> +   the __libc_IO_stdfiles section.  */
> +static inline bool
> +interposed_stdfiles_variable (void *ptr)
> +{
> +  uintptr_t uptr = (uintptr_t) ptr; /* Avoid ptrdiff_t overflow.  */
> +  uintptr_t length = __stop___libc_IO_stdfiles - __start___libc_IO_stdfiles;
> +  uintptr_t start = uptr - (uintptr_t) __start___libc_IO_stdfiles;
> +  return start >= length;
> +}
> +
> +/* Return true if any of the standard variable definitions for stdin,
> +   stdout, stderr have been interposed.  */
> +static inline bool
> +stdfiles_interposed (void)
> +{
> +  return interposed_stdfiles_variable (&_IO_2_1_stdin_)
> +    || interposed_stdfiles_variable (&_IO_2_1_stdout_)
> +    || interposed_stdfiles_variable (&_IO_2_1_stderr_);
> +}

the check looks ok to me
i think hidden symbol alias would work too

return &_IO_2_1_stdin == &_IO_2_1_stdin_internal_alias || ...;

and it may be a bit nicer than checking the section.. i'm not sure

  reply	other threads:[~2018-06-19 10:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-19  9:56 Florian Weimer
2018-06-19 10:38 ` Szabolcs Nagy [this message]
2018-06-19 11:27   ` Florian Weimer
2018-06-19 19:13     ` Florian Weimer
2018-06-20  7:58       ` Florian Weimer
2018-06-21 21:20         ` Florian Weimer
2018-06-22 10:16           ` Szabolcs Nagy
2018-06-22 10:51             ` Florian Weimer
2018-06-26  9:33               ` Florian Weimer

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=773dfddc-66ed-730f-d8b3-a0c9392cc5de@arm.com \
    --to=szabolcs.nagy@arm.com \
    --cc=fw@deneb.enyo.de \
    --cc=libc-alpha@sourceware.org \
    --cc=nd@arm.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).