mirror of
				https://sourceware.org/git/glibc.git
				synced 2025-11-03 20:53:13 +03:00 
			
		
		
		
	Add generic code to handle PT_GNU_PROPERTY notes. Invalid content is ignored, _dl_process_pt_gnu_property is always called after PT_LOAD segments are mapped and it has no failure modes. Currently only one NT_GNU_PROPERTY_TYPE_0 note is handled, which contains target specific properties: the _dl_process_gnu_property hook is called for each property. The old _dl_process_pt_note and _rtld_process_pt_note differ in how the program header is read. The old _dl_process_pt_note is called before PT_LOAD segments are mapped and _rtld_process_pt_note is called after PT_LOAD segments are mapped. The old _rtld_process_pt_note is removed and _dl_process_pt_note is always called after PT_LOAD segments are mapped and now it has no failure modes. The program headers are scanned backwards so that PT_NOTE can be skipped if PT_GNU_PROPERTY exists. Co-Authored-By: H.J. Lu <hjl.tools@gmail.com> Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
		
			
				
	
	
		
			162 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			162 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/* Support for GNU properties.  x86 version.
 | 
						|
   Copyright (C) 2018-2020 Free Software Foundation, Inc.
 | 
						|
   This file is part of the GNU C Library.
 | 
						|
 | 
						|
   The GNU C Library is free software; you can redistribute it and/or
 | 
						|
   modify it under the terms of the GNU Lesser General Public
 | 
						|
   License as published by the Free Software Foundation; either
 | 
						|
   version 2.1 of the License, or (at your option) any later version.
 | 
						|
 | 
						|
   The GNU C Library is distributed in the hope that it will be useful,
 | 
						|
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
						|
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
						|
   Lesser General Public License for more details.
 | 
						|
 | 
						|
   You should have received a copy of the GNU Lesser General Public
 | 
						|
   License along with the GNU C Library; if not, see
 | 
						|
   <https://www.gnu.org/licenses/>.  */
 | 
						|
 | 
						|
#ifndef _DL_PROP_H
 | 
						|
#define _DL_PROP_H
 | 
						|
 | 
						|
extern void _dl_cet_check (struct link_map *, const char *)
 | 
						|
    attribute_hidden;
 | 
						|
extern void _dl_cet_open_check (struct link_map *)
 | 
						|
    attribute_hidden;
 | 
						|
 | 
						|
static inline void __attribute__ ((always_inline))
 | 
						|
_rtld_main_check (struct link_map *m, const char *program)
 | 
						|
{
 | 
						|
#if CET_ENABLED
 | 
						|
  _dl_cet_check (m, program);
 | 
						|
#endif
 | 
						|
}
 | 
						|
 | 
						|
static inline void __attribute__ ((always_inline))
 | 
						|
_dl_open_check (struct link_map *m)
 | 
						|
{
 | 
						|
#if CET_ENABLED
 | 
						|
  _dl_cet_open_check (m);
 | 
						|
#endif
 | 
						|
}
 | 
						|
 | 
						|
static inline void __attribute__ ((unused))
 | 
						|
_dl_process_cet_property_note (struct link_map *l,
 | 
						|
			      const ElfW(Nhdr) *note,
 | 
						|
			      const ElfW(Addr) size,
 | 
						|
			      const ElfW(Addr) align)
 | 
						|
{
 | 
						|
#if CET_ENABLED
 | 
						|
  /* Skip if we have seen a NT_GNU_PROPERTY_TYPE_0 note before.  */
 | 
						|
  if (l->l_cet != lc_unknown)
 | 
						|
    return;
 | 
						|
 | 
						|
  /* The NT_GNU_PROPERTY_TYPE_0 note must be aliged to 4 bytes in
 | 
						|
     32-bit objects and to 8 bytes in 64-bit objects.  Skip notes
 | 
						|
     with incorrect alignment.  */
 | 
						|
  if (align != (__ELF_NATIVE_CLASS / 8))
 | 
						|
    return;
 | 
						|
 | 
						|
  const ElfW(Addr) start = (ElfW(Addr)) note;
 | 
						|
 | 
						|
  unsigned int feature_1 = 0;
 | 
						|
  unsigned int last_type = 0;
 | 
						|
 | 
						|
  while ((ElfW(Addr)) (note + 1) - start < size)
 | 
						|
    {
 | 
						|
      /* Find the NT_GNU_PROPERTY_TYPE_0 note.  */
 | 
						|
      if (note->n_namesz == 4
 | 
						|
	  && note->n_type == NT_GNU_PROPERTY_TYPE_0
 | 
						|
	  && memcmp (note + 1, "GNU", 4) == 0)
 | 
						|
	{
 | 
						|
	  /* Stop if we see more than one GNU property note which may
 | 
						|
	     be generated by the older linker.  */
 | 
						|
	  if (l->l_cet != lc_unknown)
 | 
						|
	    return;
 | 
						|
 | 
						|
	  /* Check CET status now.  */
 | 
						|
	  l->l_cet = lc_none;
 | 
						|
 | 
						|
	  /* Check for invalid property.  */
 | 
						|
	  if (note->n_descsz < 8
 | 
						|
	      || (note->n_descsz % sizeof (ElfW(Addr))) != 0)
 | 
						|
	    return;
 | 
						|
 | 
						|
	  /* Start and end of property array.  */
 | 
						|
	  unsigned char *ptr = (unsigned char *) (note + 1) + 4;
 | 
						|
	  unsigned char *ptr_end = ptr + note->n_descsz;
 | 
						|
 | 
						|
	  do
 | 
						|
	    {
 | 
						|
	      unsigned int type = *(unsigned int *) ptr;
 | 
						|
	      unsigned int datasz = *(unsigned int *) (ptr + 4);
 | 
						|
 | 
						|
	      /* Property type must be in ascending order.  */
 | 
						|
	      if (type < last_type)
 | 
						|
		return;
 | 
						|
 | 
						|
	      ptr += 8;
 | 
						|
	      if ((ptr + datasz) > ptr_end)
 | 
						|
		return;
 | 
						|
 | 
						|
	      last_type = type;
 | 
						|
 | 
						|
	      if (type == GNU_PROPERTY_X86_FEATURE_1_AND)
 | 
						|
		{
 | 
						|
		  /* The size of GNU_PROPERTY_X86_FEATURE_1_AND is 4
 | 
						|
		     bytes.  When seeing GNU_PROPERTY_X86_FEATURE_1_AND,
 | 
						|
		     we stop the search regardless if its size is correct
 | 
						|
		     or not.  There is no point to continue if this note
 | 
						|
		     is ill-formed.  */
 | 
						|
		  if (datasz != 4)
 | 
						|
		    return;
 | 
						|
 | 
						|
		  feature_1 = *(unsigned int *) ptr;
 | 
						|
 | 
						|
		  /* Keep searching for the next GNU property note
 | 
						|
		     generated by the older linker.  */
 | 
						|
		  break;
 | 
						|
		}
 | 
						|
	      else if (type > GNU_PROPERTY_X86_FEATURE_1_AND)
 | 
						|
		{
 | 
						|
		  /* Stop since property type is in ascending order.  */
 | 
						|
		  return;
 | 
						|
		}
 | 
						|
 | 
						|
	      /* Check the next property item.  */
 | 
						|
	      ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr)));
 | 
						|
	    }
 | 
						|
	  while ((ptr_end - ptr) >= 8);
 | 
						|
	}
 | 
						|
 | 
						|
      /* NB: Note sections like .note.ABI-tag and .note.gnu.build-id are
 | 
						|
	 aligned to 4 bytes in 64-bit ELF objects.  */
 | 
						|
      note = ((const void *) note
 | 
						|
	      + ELF_NOTE_NEXT_OFFSET (note->n_namesz, note->n_descsz,
 | 
						|
				      align));
 | 
						|
    }
 | 
						|
 | 
						|
  /* We get here only if there is one or no GNU property note.  */
 | 
						|
  if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_IBT))
 | 
						|
    l->l_cet |= lc_ibt;
 | 
						|
  if ((feature_1 & GNU_PROPERTY_X86_FEATURE_1_SHSTK))
 | 
						|
    l->l_cet |= lc_shstk;
 | 
						|
#endif
 | 
						|
}
 | 
						|
 | 
						|
static inline void __attribute__ ((unused))
 | 
						|
_dl_process_pt_note (struct link_map *l, const ElfW(Phdr) *ph)
 | 
						|
{
 | 
						|
  const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr);
 | 
						|
  _dl_process_cet_property_note (l, note, ph->p_memsz, ph->p_align);
 | 
						|
}
 | 
						|
 | 
						|
static inline int __attribute__ ((always_inline))
 | 
						|
_dl_process_gnu_property (struct link_map *l, uint32_t type, uint32_t datasz,
 | 
						|
			  void *data)
 | 
						|
{
 | 
						|
  return 0;
 | 
						|
}
 | 
						|
 | 
						|
#endif /* _DL_PROP_H */
 |