Blame sysdeps/x86/dl-prop.h

Packit 6c4009
/* Support for GNU properties.  x86 version.
Packit 6c4009
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef _DL_PROP_H
Packit 6c4009
#define _DL_PROP_H
Packit 6c4009
Packit 6c4009
#include <not-cancel.h>
Packit 6c4009
Packit 6c4009
extern void _dl_cet_check (struct link_map *, const char *)
Packit 6c4009
    attribute_hidden;
Packit 6c4009
extern void _dl_cet_open_check (struct link_map *)
Packit 6c4009
    attribute_hidden;
Packit 6c4009
Packit 6c4009
static inline void __attribute__ ((always_inline))
Packit 6c4009
_rtld_main_check (struct link_map *m, const char *program)
Packit 6c4009
{
Packit 6c4009
#if CET_ENABLED
Packit 6c4009
  _dl_cet_check (m, program);
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static inline void __attribute__ ((always_inline))
Packit 6c4009
_dl_open_check (struct link_map *m)
Packit 6c4009
{
Packit 6c4009
#if CET_ENABLED
Packit 6c4009
  _dl_cet_open_check (m);
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static inline void __attribute__ ((unused))
Packit 6c4009
_dl_process_cet_property_note (struct link_map *l,
Packit 6c4009
			      const ElfW(Nhdr) *note,
Packit 6c4009
			      const ElfW(Addr) size,
Packit 6c4009
			      const ElfW(Addr) align)
Packit 6c4009
{
Packit 6c4009
#if CET_ENABLED
Packit Service a142ba
  /* Skip if we have seen a NT_GNU_PROPERTY_TYPE_0 note before.  */
Packit Service a142ba
  if (l->l_cet != lc_unknown)
Packit Service a142ba
    return;
Packit Service a142ba
Packit 6c4009
  /* The NT_GNU_PROPERTY_TYPE_0 note must be aliged to 4 bytes in
Packit 6c4009
     32-bit objects and to 8 bytes in 64-bit objects.  Skip notes
Packit 6c4009
     with incorrect alignment.  */
Packit 6c4009
  if (align != (__ELF_NATIVE_CLASS / 8))
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  const ElfW(Addr) start = (ElfW(Addr)) note;
Packit 6c4009
Packit Service a142ba
  unsigned int feature_1 = 0;
Packit Service a142ba
  unsigned int last_type = 0;
Packit Service a142ba
Packit 6c4009
  while ((ElfW(Addr)) (note + 1) - start < size)
Packit 6c4009
    {
Packit 6c4009
      /* Find the NT_GNU_PROPERTY_TYPE_0 note.  */
Packit 6c4009
      if (note->n_namesz == 4
Packit 6c4009
	  && note->n_type == NT_GNU_PROPERTY_TYPE_0
Packit 6c4009
	  && memcmp (note + 1, "GNU", 4) == 0)
Packit 6c4009
	{
Packit Service a142ba
	  /* Stop if we see more than one GNU property note which may
Packit Service a142ba
	     be generated by the older linker.  */
Packit Service a142ba
	  if (l->l_cet != lc_unknown)
Packit Service a142ba
	    return;
Packit Service a142ba
Packit Service a142ba
	  /* Check CET status now.  */
Packit Service a142ba
	  l->l_cet = lc_none;
Packit Service a142ba
Packit 6c4009
	  /* Check for invalid property.  */
Packit 6c4009
	  if (note->n_descsz < 8
Packit 6c4009
	      || (note->n_descsz % sizeof (ElfW(Addr))) != 0)
Packit Service a142ba
	    return;
Packit 6c4009
Packit 6c4009
	  /* Start and end of property array.  */
Packit 6c4009
	  unsigned char *ptr = (unsigned char *) (note + 1) + 4;
Packit 6c4009
	  unsigned char *ptr_end = ptr + note->n_descsz;
Packit 6c4009
Packit 6c4009
	  do
Packit 6c4009
	    {
Packit 6c4009
	      unsigned int type = *(unsigned int *) ptr;
Packit 6c4009
	      unsigned int datasz = *(unsigned int *) (ptr + 4);
Packit 6c4009
Packit Service a142ba
	      /* Property type must be in ascending order.  */
Packit Service a142ba
	      if (type < last_type)
Packit Service a142ba
		return;
Packit Service a142ba
Packit 6c4009
	      ptr += 8;
Packit 6c4009
	      if ((ptr + datasz) > ptr_end)
Packit Service a142ba
		return;
Packit Service a142ba
Packit Service a142ba
	      last_type = type;
Packit 6c4009
Packit 6c4009
	      if (type == GNU_PROPERTY_X86_FEATURE_1_AND)
Packit 6c4009
		{
Packit 6c4009
		  /* The size of GNU_PROPERTY_X86_FEATURE_1_AND is 4
Packit 6c4009
		     bytes.  When seeing GNU_PROPERTY_X86_FEATURE_1_AND,
Packit 6c4009
		     we stop the search regardless if its size is correct
Packit 6c4009
		     or not.  There is no point to continue if this note
Packit 6c4009
		     is ill-formed.  */
Packit Service a142ba
		  if (datasz != 4)
Packit Service a142ba
		    return;
Packit Service a142ba
Packit Service a142ba
		  feature_1 = *(unsigned int *) ptr;
Packit Service a142ba
Packit Service a142ba
		  /* Keep searching for the next GNU property note
Packit Service a142ba
		     generated by the older linker.  */
Packit Service a142ba
		  break;
Packit Service a142ba
		}
Packit Service a142ba
	      else if (type > GNU_PROPERTY_X86_FEATURE_1_AND)
Packit Service a142ba
		{
Packit Service a142ba
		  /* Stop since property type is in ascending order.  */
Packit 6c4009
		  return;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      /* Check the next property item.  */
Packit 6c4009
	      ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr)));
Packit 6c4009
	    }
Packit 6c4009
	  while ((ptr_end - ptr) >= 8);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* NB: Note sections like .note.ABI-tag and .note.gnu.build-id are
Packit 6c4009
	 aligned to 4 bytes in 64-bit ELF objects.  */
Packit 6c4009
      note = ((const void *) note
Packit 6c4009
	      + ELF_NOTE_NEXT_OFFSET (note->n_namesz, note->n_descsz,
Packit 6c4009
				      align));
Packit 6c4009
    }
Packit Service a142ba
Packit Service a142ba
  /* We get here only if there is one or no GNU property note.  */
Packit Service a142ba
  if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_IBT))
Packit Service a142ba
    l->l_cet |= lc_ibt;
Packit Service a142ba
  if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_SHSTK))
Packit Service a142ba
    l->l_cet |= lc_shstk;
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#ifdef FILEBUF_SIZE
Packit 6c4009
static inline int __attribute__ ((unused))
Packit 6c4009
_dl_process_pt_note (struct link_map *l, const ElfW(Phdr) *ph,
Packit 6c4009
		     int fd, struct filebuf *fbp)
Packit 6c4009
{
Packit 6c4009
# if CET_ENABLED
Packit 6c4009
  const ElfW(Nhdr) *note;
Packit 6c4009
  ElfW(Nhdr) *note_malloced = NULL;
Packit 6c4009
  ElfW(Addr) size = ph->p_filesz;
Packit 6c4009
Packit 6c4009
  if (ph->p_offset + size <= (size_t) fbp->len)
Packit 6c4009
    note = (const void *) (fbp->buf + ph->p_offset);
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (size < __MAX_ALLOCA_CUTOFF)
Packit 6c4009
	note = alloca (size);
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  note_malloced = malloc (size);
Packit 6c4009
	  note = note_malloced;
Packit 6c4009
	}
Packit 6c4009
      __lseek (fd, ph->p_offset, SEEK_SET);
Packit 6c4009
      if (__read_nocancel (fd, (void *) note, size) != size)
Packit 6c4009
	{
Packit 6c4009
	  if (note_malloced)
Packit 6c4009
	    free (note_malloced);
Packit 6c4009
	  return -1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  _dl_process_cet_property_note (l, note, size, ph->p_align);
Packit 6c4009
  if (note_malloced)
Packit 6c4009
    free (note_malloced);
Packit 6c4009
# endif
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static inline int __attribute__ ((unused))
Packit 6c4009
_rtld_process_pt_note (struct link_map *l, const ElfW(Phdr) *ph)
Packit 6c4009
{
Packit 6c4009
  const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr);
Packit 6c4009
  _dl_process_cet_property_note (l, note, ph->p_memsz, ph->p_align);
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#endif /* _DL_PROP_H */