public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] x86: Properly check invalid relocation against protected symbol
@ 2022-07-18 20:00 H.J. Lu
  2022-07-19  1:53 ` [PATCH] ld: Pass -nostdlib to compiler with -r H.J. Lu
  2022-07-19 16:00 ` [PATCH] x86: Properly check invalid relocation against protected symbol H.J. Lu
  0 siblings, 2 replies; 3+ messages in thread
From: H.J. Lu @ 2022-07-18 20:00 UTC (permalink / raw)
  To: binutils

Only check invalid relocation against protected symbol defined in shared
object.

bfd/

	PR ld/29377
	* elf32-i386.c (elf_i386_scan_relocs): Only check invalid
	relocation against protected symbol defined in shared object.
	* elf64-x86-64.c (elf_x86_64_scan_relocs): Likewise.

ld/

	PR ld/29377
	* testsuite/ld-elf/linux-x86.exp: Run PR ld/29377 tests.
	* testsuite/ld-elf/pr29377a.c: New file.
	* testsuite/ld-elf/pr29377b.c: Likewise.
---
 bfd/elf32-i386.c                  |  2 ++
 bfd/elf64-x86-64.c                |  2 ++
 ld/testsuite/ld-elf/linux-x86.exp | 16 ++++++++++++++++
 ld/testsuite/ld-elf/pr29377a.c    |  5 +++++
 ld/testsuite/ld-elf/pr29377b.c    | 10 ++++++++++
 5 files changed, 35 insertions(+)
 create mode 100644 ld/testsuite/ld-elf/pr29377a.c
 create mode 100644 ld/testsuite/ld-elf/pr29377b.c

diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c
index e4106d9fd3b..3d39dc07fe0 100644
--- a/bfd/elf32-i386.c
+++ b/bfd/elf32-i386.c
@@ -1811,6 +1811,8 @@ elf_i386_scan_relocs (bfd *abfd,
 		  if (h->pointer_equality_needed
 		      && h->type == STT_FUNC
 		      && eh->def_protected
+		      && !SYMBOL_DEFINED_NON_SHARED_P (h)
+		      && h->def_dynamic
 		      && elf_has_indirect_extern_access (h->root.u.def.section->owner))
 		    {
 		      /* Disallow non-canonical reference to canonical
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
index 6154a70bdd7..48ca6309d1b 100644
--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -2254,6 +2254,8 @@ elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info,
 		  if (h->pointer_equality_needed
 		      && h->type == STT_FUNC
 		      && eh->def_protected
+		      && !SYMBOL_DEFINED_NON_SHARED_P (h)
+		      && h->def_dynamic
 		      && elf_has_indirect_extern_access (h->root.u.def.section->owner))
 		    {
 		      /* Disallow non-canonical reference to canonical
diff --git a/ld/testsuite/ld-elf/linux-x86.exp b/ld/testsuite/ld-elf/linux-x86.exp
index 6035ecd6829..0f0a6f01ec0 100644
--- a/ld/testsuite/ld-elf/linux-x86.exp
+++ b/ld/testsuite/ld-elf/linux-x86.exp
@@ -134,6 +134,22 @@ run_cc_link_tests [list \
 	{{readelf {--dyn-syms -W} pr28158.rd} {nm -D pr28158.nd}} \
 	"pr28158" \
     ] \
+    [list \
+	"Build pr29377.o" \
+	"-r -Wl,-z,noexecstack" \
+	"" \
+	{pr29377a.c indirect-extern-access.S} \
+	{} \
+	"pr29377.o" \
+    ] \
+    [list \
+	"Build pr29377" \
+	"$NOPIE_LDFLAGS tmpdir/pr29377.o" \
+	"-fPIC" \
+	{pr29377b.c} \
+	{} \
+	"pr29377" \
+    ] \
 ]
 
 run_ld_link_exec_tests [list \
diff --git a/ld/testsuite/ld-elf/pr29377a.c b/ld/testsuite/ld-elf/pr29377a.c
new file mode 100644
index 00000000000..41d9545e3bf
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr29377a.c
@@ -0,0 +1,5 @@
+__attribute__ ((visibility ("protected")))
+void
+foo (void)
+{
+}
diff --git a/ld/testsuite/ld-elf/pr29377b.c b/ld/testsuite/ld-elf/pr29377b.c
new file mode 100644
index 00000000000..3cf163b25b4
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr29377b.c
@@ -0,0 +1,10 @@
+extern void foo (void);
+
+void (*foo_p) (void);
+
+int
+main ()
+{
+  foo_p = foo;
+  return 0;
+}
-- 
2.36.1


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

* [PATCH] ld: Pass -nostdlib to compiler with -r
  2022-07-18 20:00 [PATCH] x86: Properly check invalid relocation against protected symbol H.J. Lu
@ 2022-07-19  1:53 ` H.J. Lu
  2022-07-19 16:00 ` [PATCH] x86: Properly check invalid relocation against protected symbol H.J. Lu
  1 sibling, 0 replies; 3+ messages in thread
From: H.J. Lu @ 2022-07-19  1:53 UTC (permalink / raw)
  To: Binutils

[-- Attachment #1: Type: text/plain, Size: 3757 bytes --]

On Mon, Jul 18, 2022 at 1:00 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> Only check invalid relocation against protected symbol defined in shared
> object.
>
> bfd/
>
>         PR ld/29377
>         * elf32-i386.c (elf_i386_scan_relocs): Only check invalid
>         relocation against protected symbol defined in shared object.
>         * elf64-x86-64.c (elf_x86_64_scan_relocs): Likewise.
>
> ld/
>
>         PR ld/29377
>         * testsuite/ld-elf/linux-x86.exp: Run PR ld/29377 tests.
>         * testsuite/ld-elf/pr29377a.c: New file.
>         * testsuite/ld-elf/pr29377b.c: Likewise.
> ---
>  bfd/elf32-i386.c                  |  2 ++
>  bfd/elf64-x86-64.c                |  2 ++
>  ld/testsuite/ld-elf/linux-x86.exp | 16 ++++++++++++++++
>  ld/testsuite/ld-elf/pr29377a.c    |  5 +++++
>  ld/testsuite/ld-elf/pr29377b.c    | 10 ++++++++++
>  5 files changed, 35 insertions(+)
>  create mode 100644 ld/testsuite/ld-elf/pr29377a.c
>  create mode 100644 ld/testsuite/ld-elf/pr29377b.c
>
> diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c
> index e4106d9fd3b..3d39dc07fe0 100644
> --- a/bfd/elf32-i386.c
> +++ b/bfd/elf32-i386.c
> @@ -1811,6 +1811,8 @@ elf_i386_scan_relocs (bfd *abfd,
>                   if (h->pointer_equality_needed
>                       && h->type == STT_FUNC
>                       && eh->def_protected
> +                     && !SYMBOL_DEFINED_NON_SHARED_P (h)
> +                     && h->def_dynamic
>                       && elf_has_indirect_extern_access (h->root.u.def.section->owner))
>                     {
>                       /* Disallow non-canonical reference to canonical
> diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
> index 6154a70bdd7..48ca6309d1b 100644
> --- a/bfd/elf64-x86-64.c
> +++ b/bfd/elf64-x86-64.c
> @@ -2254,6 +2254,8 @@ elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info,
>                   if (h->pointer_equality_needed
>                       && h->type == STT_FUNC
>                       && eh->def_protected
> +                     && !SYMBOL_DEFINED_NON_SHARED_P (h)
> +                     && h->def_dynamic
>                       && elf_has_indirect_extern_access (h->root.u.def.section->owner))
>                     {
>                       /* Disallow non-canonical reference to canonical
> diff --git a/ld/testsuite/ld-elf/linux-x86.exp b/ld/testsuite/ld-elf/linux-x86.exp
> index 6035ecd6829..0f0a6f01ec0 100644
> --- a/ld/testsuite/ld-elf/linux-x86.exp
> +++ b/ld/testsuite/ld-elf/linux-x86.exp
> @@ -134,6 +134,22 @@ run_cc_link_tests [list \
>         {{readelf {--dyn-syms -W} pr28158.rd} {nm -D pr28158.nd}} \
>         "pr28158" \
>      ] \
> +    [list \
> +       "Build pr29377.o" \
> +       "-r -Wl,-z,noexecstack" \

I am checking in this to add -nostdlib.

> +       "" \
> +       {pr29377a.c indirect-extern-access.S} \
> +       {} \
> +       "pr29377.o" \
> +    ] \
> +    [list \
> +       "Build pr29377" \
> +       "$NOPIE_LDFLAGS tmpdir/pr29377.o" \
> +       "-fPIC" \
> +       {pr29377b.c} \
> +       {} \
> +       "pr29377" \
> +    ] \
>  ]
>
>  run_ld_link_exec_tests [list \
> diff --git a/ld/testsuite/ld-elf/pr29377a.c b/ld/testsuite/ld-elf/pr29377a.c
> new file mode 100644
> index 00000000000..41d9545e3bf
> --- /dev/null
> +++ b/ld/testsuite/ld-elf/pr29377a.c
> @@ -0,0 +1,5 @@
> +__attribute__ ((visibility ("protected")))
> +void
> +foo (void)
> +{
> +}
> diff --git a/ld/testsuite/ld-elf/pr29377b.c b/ld/testsuite/ld-elf/pr29377b.c
> new file mode 100644
> index 00000000000..3cf163b25b4
> --- /dev/null
> +++ b/ld/testsuite/ld-elf/pr29377b.c
> @@ -0,0 +1,10 @@
> +extern void foo (void);
> +
> +void (*foo_p) (void);
> +
> +int
> +main ()
> +{
> +  foo_p = foo;
> +  return 0;
> +}
> --
> 2.36.1
>


-- 
H.J.

[-- Attachment #2: 0001-ld-Pass-nostdlib-to-compiler-with-r.patch --]
[-- Type: text/x-patch, Size: 862 bytes --]

From 0ac83a32aac4a3f57ee20536b16eb957a009fdc9 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Mon, 18 Jul 2022 18:49:27 -0700
Subject: [PATCH] ld: Pass -nostdlib to compiler with -r

Pass -nostdlib to compiler with -r to avoid unnecessary .o file and
libraries.

	* testsuite/ld-elf/linux-x86.exp: Pass -nostdlib with -r.
---
 ld/testsuite/ld-elf/linux-x86.exp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ld/testsuite/ld-elf/linux-x86.exp b/ld/testsuite/ld-elf/linux-x86.exp
index 0f0a6f01ec0..06379057ff2 100644
--- a/ld/testsuite/ld-elf/linux-x86.exp
+++ b/ld/testsuite/ld-elf/linux-x86.exp
@@ -136,7 +136,7 @@ run_cc_link_tests [list \
     ] \
     [list \
 	"Build pr29377.o" \
-	"-r -Wl,-z,noexecstack" \
+	"-r -nostdlib -Wl,-z,noexecstack" \
 	"" \
 	{pr29377a.c indirect-extern-access.S} \
 	{} \
-- 
2.36.1


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

* Re: [PATCH] x86: Properly check invalid relocation against protected symbol
  2022-07-18 20:00 [PATCH] x86: Properly check invalid relocation against protected symbol H.J. Lu
  2022-07-19  1:53 ` [PATCH] ld: Pass -nostdlib to compiler with -r H.J. Lu
@ 2022-07-19 16:00 ` H.J. Lu
  1 sibling, 0 replies; 3+ messages in thread
From: H.J. Lu @ 2022-07-19 16:00 UTC (permalink / raw)
  To: Binutils

On Mon, Jul 18, 2022 at 1:00 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> Only check invalid relocation against protected symbol defined in shared
> object.
>
> bfd/
>
>         PR ld/29377
>         * elf32-i386.c (elf_i386_scan_relocs): Only check invalid
>         relocation against protected symbol defined in shared object.
>         * elf64-x86-64.c (elf_x86_64_scan_relocs): Likewise.
>
> ld/
>
>         PR ld/29377
>         * testsuite/ld-elf/linux-x86.exp: Run PR ld/29377 tests.
>         * testsuite/ld-elf/pr29377a.c: New file.
>         * testsuite/ld-elf/pr29377b.c: Likewise.
> ---
>  bfd/elf32-i386.c                  |  2 ++
>  bfd/elf64-x86-64.c                |  2 ++
>  ld/testsuite/ld-elf/linux-x86.exp | 16 ++++++++++++++++
>  ld/testsuite/ld-elf/pr29377a.c    |  5 +++++
>  ld/testsuite/ld-elf/pr29377b.c    | 10 ++++++++++
>  5 files changed, 35 insertions(+)
>  create mode 100644 ld/testsuite/ld-elf/pr29377a.c
>  create mode 100644 ld/testsuite/ld-elf/pr29377b.c
>
> diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c
> index e4106d9fd3b..3d39dc07fe0 100644
> --- a/bfd/elf32-i386.c
> +++ b/bfd/elf32-i386.c
> @@ -1811,6 +1811,8 @@ elf_i386_scan_relocs (bfd *abfd,
>                   if (h->pointer_equality_needed
>                       && h->type == STT_FUNC
>                       && eh->def_protected
> +                     && !SYMBOL_DEFINED_NON_SHARED_P (h)
> +                     && h->def_dynamic
>                       && elf_has_indirect_extern_access (h->root.u.def.section->owner))
>                     {
>                       /* Disallow non-canonical reference to canonical
> diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
> index 6154a70bdd7..48ca6309d1b 100644
> --- a/bfd/elf64-x86-64.c
> +++ b/bfd/elf64-x86-64.c
> @@ -2254,6 +2254,8 @@ elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info,
>                   if (h->pointer_equality_needed
>                       && h->type == STT_FUNC
>                       && eh->def_protected
> +                     && !SYMBOL_DEFINED_NON_SHARED_P (h)
> +                     && h->def_dynamic
>                       && elf_has_indirect_extern_access (h->root.u.def.section->owner))
>                     {
>                       /* Disallow non-canonical reference to canonical
> diff --git a/ld/testsuite/ld-elf/linux-x86.exp b/ld/testsuite/ld-elf/linux-x86.exp
> index 6035ecd6829..0f0a6f01ec0 100644
> --- a/ld/testsuite/ld-elf/linux-x86.exp
> +++ b/ld/testsuite/ld-elf/linux-x86.exp
> @@ -134,6 +134,22 @@ run_cc_link_tests [list \
>         {{readelf {--dyn-syms -W} pr28158.rd} {nm -D pr28158.nd}} \
>         "pr28158" \
>      ] \
> +    [list \
> +       "Build pr29377.o" \
> +       "-r -Wl,-z,noexecstack" \
> +       "" \
> +       {pr29377a.c indirect-extern-access.S} \
> +       {} \
> +       "pr29377.o" \
> +    ] \
> +    [list \
> +       "Build pr29377" \
> +       "$NOPIE_LDFLAGS tmpdir/pr29377.o" \
> +       "-fPIC" \
> +       {pr29377b.c} \
> +       {} \
> +       "pr29377" \
> +    ] \
>  ]
>
>  run_ld_link_exec_tests [list \
> diff --git a/ld/testsuite/ld-elf/pr29377a.c b/ld/testsuite/ld-elf/pr29377a.c
> new file mode 100644
> index 00000000000..41d9545e3bf
> --- /dev/null
> +++ b/ld/testsuite/ld-elf/pr29377a.c
> @@ -0,0 +1,5 @@
> +__attribute__ ((visibility ("protected")))
> +void
> +foo (void)
> +{
> +}
> diff --git a/ld/testsuite/ld-elf/pr29377b.c b/ld/testsuite/ld-elf/pr29377b.c
> new file mode 100644
> index 00000000000..3cf163b25b4
> --- /dev/null
> +++ b/ld/testsuite/ld-elf/pr29377b.c
> @@ -0,0 +1,10 @@
> +extern void foo (void);
> +
> +void (*foo_p) (void);
> +
> +int
> +main ()
> +{
> +  foo_p = foo;
> +  return 0;
> +}
> --
> 2.36.1
>

I am backporting this to 2.38 and 2.39 branches.

-- 
H.J.

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

end of thread, other threads:[~2022-07-19 16:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-18 20:00 [PATCH] x86: Properly check invalid relocation against protected symbol H.J. Lu
2022-07-19  1:53 ` [PATCH] ld: Pass -nostdlib to compiler with -r H.J. Lu
2022-07-19 16:00 ` [PATCH] x86: Properly check invalid relocation against protected symbol H.J. Lu

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