public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* RFC: Add a linker warning when creating segments with RWX permissions
@ 2022-04-26 11:31 Nick Clifton
  2022-04-26 13:56 ` Jan Beulich
  2022-04-26 15:06 ` binutils as policy checker (was: RFC: Add a linker warning when creating segments with RWX permissions) Michael Matz
  0 siblings, 2 replies; 12+ messages in thread
From: Nick Clifton @ 2022-04-26 11:31 UTC (permalink / raw)
  To: binutils

Hi Guys,

  Following on from the patch to add warnings when the linker creates an
  executable stack, here is another proposal for a patch to add a
  warning when the linker creates a memory resident segment with RWX
  permissions.

  Whilst testing this patch I found that a lot of linker tests were
  failing because they use simple linker scripts that only allocate one
  loadable program header, or combine data and code sections together in
  a single page.  In order to avoid prevent the linker from issuing
  too many warnings therefore, the code only complains if there is more
  than one loadable segment.  Possibly this is too lenient...

  There are still a fair number of linker failures, even with the
  current version of the patch, so I am considering adding an extra
  regexp to the linker warning pruning code in binutils-common.exp.  But
  in the meantime I thought that people might like to see the current
  code and comment on whether or not they thought that this was a good
  idea.

Cheers
  Nick

diff --git a/bfd/elfcode.h b/bfd/elfcode.h
index 4d4cb68164a..385cff1dc13 100644
--- a/bfd/elfcode.h
+++ b/bfd/elfcode.h
@@ -388,6 +388,13 @@ elf_swap_phdr_in (bfd *abfd,
   dst->p_align = H_GET_WORD (abfd, src->p_align);
 }
 
+static inline bool
+is_memory_resident (const Elf_Internal_Phdr *src)
+{
+  /* FIXME: Should we return true for PT_TLS segments ?  */
+  return src->p_type == PT_LOAD;
+}
+
 void
 elf_swap_phdr_out (bfd *abfd,
 		   const Elf_Internal_Phdr *src,
@@ -399,6 +406,27 @@ elf_swap_phdr_out (bfd *abfd,
   bed = get_elf_backend_data (abfd);
   p_paddr = bed->want_p_paddr_set_to_zero ? 0 : src->p_paddr;
 
+  /* Memory resident segments with non-zero size and RWX permissions are a
+     security risk, so we generate a warning here if we are creating any.
+
+     We suppress the warning if there is only one memory resident segment
+     however.  This is an an assist for simple programs that do not separate
+     code and data segments or linker scrips that only define one program
+     header.  Whilst this is not ideal - even those simple programs will be
+     vulnerable - the most likely sceanario is that these programs are test
+     code and not real apps.  */
+  if (src->p_memsz > 0 && is_memory_resident (src))
+    {
+      static unsigned int seen = 0;
+
+      if (++ seen > 1)
+	{
+	  if ((src->p_flags & (PF_R | PF_W | PF_X)) == (PF_R | PF_W | PF_X))
+	    _bfd_error_handler (_("warning: %pB has a segment with RWX permissions"),
+				abfd);
+	}
+    }
+
   /* note that all elements of dst are *arrays of unsigned char* already...  */
   H_PUT_32 (abfd, src->p_type, dst->p_type);
   H_PUT_WORD (abfd, src->p_offset, dst->p_offset);


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-05-03 20:30 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-26 11:31 RFC: Add a linker warning when creating segments with RWX permissions Nick Clifton
2022-04-26 13:56 ` Jan Beulich
2022-04-26 16:39   ` Nick Clifton
2022-04-26 15:06 ` binutils as policy checker (was: RFC: Add a linker warning when creating segments with RWX permissions) Michael Matz
2022-04-26 15:20   ` Joel Sherrill
2022-04-28  9:46     ` Nick Clifton
2022-04-29  6:29       ` Sam James
2022-05-03 14:54       ` Michael Matz
2022-05-03 19:35       ` Matthias Klose
2022-05-03 19:57         ` H.J. Lu
2022-05-03 20:29           ` Matthias Klose
2022-04-26 16:47   ` Nick Clifton

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).