* [PATCH 1/2] Initial support for GNU_PROPERTY_1_NEEDED
@ 2021-08-03 21:59 H.J. Lu via Libc-alpha
2021-08-03 21:59 ` [PATCH 2/2] Add run-time chesk for indirect external access H.J. Lu via Libc-alpha
2021-10-07 14:53 ` [PATCH 1/2] Initial support for GNU_PROPERTY_1_NEEDED Adhemerval Zanella via Libc-alpha
0 siblings, 2 replies; 10+ messages in thread
From: H.J. Lu via Libc-alpha @ 2021-08-03 21:59 UTC (permalink / raw)
To: libc-alpha
1. Add GNU_PROPERTY_1_NEEDED:
#define GNU_PROPERTY_1_NEEDED GNU_PROPERTY_UINT32_OR_LO
to indicate the needed properties by the object file.
2. Add GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
#define GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS (1U << 0)
to indicate that the object file requires canonical function pointers and
cannot be used with copy relocation.
3. Scan GNU_PROPERTY_1_NEEDED property and store it in l_1_needed.
---
elf/elf.h | 17 +++++++++++++++++
sysdeps/generic/dl-prop.h | 9 ++++++++-
sysdeps/generic/link_map.h | 3 ++-
sysdeps/x86/dl-prop.h | 19 ++++++++++++++-----
sysdeps/x86/link_map.h | 2 ++
5 files changed, 43 insertions(+), 7 deletions(-)
diff --git a/elf/elf.h b/elf/elf.h
index 4738dfa28f..50f87baceb 100644
--- a/elf/elf.h
+++ b/elf/elf.h
@@ -1312,6 +1312,23 @@ typedef struct
/* No copy relocation on protected data symbol. */
#define GNU_PROPERTY_NO_COPY_ON_PROTECTED 2
+/* A 4-byte unsigned integer property: A bit is set if it is set in all
+ relocatable inputs. */
+#define GNU_PROPERTY_UINT32_AND_LO 0xb0000000
+#define GNU_PROPERTY_UINT32_AND_HI 0xb0007fff
+
+/* A 4-byte unsigned integer property: A bit is set if it is set in any
+ relocatable inputs. */
+#define GNU_PROPERTY_UINT32_OR_LO 0xb0008000
+#define GNU_PROPERTY_UINT32_OR_HI 0xb000ffff
+
+/* The needed properties by the object file. */
+#define GNU_PROPERTY_1_NEEDED GNU_PROPERTY_UINT32_OR_LO
+
+/* Set if the object file requires canonical function pointers and
+ cannot be used with copy relocation. */
+#define GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS (1U << 0)
+
/* Processor-specific semantics, lo */
#define GNU_PROPERTY_LOPROC 0xc0000000
/* Processor-specific semantics, hi */
diff --git a/sysdeps/generic/dl-prop.h b/sysdeps/generic/dl-prop.h
index eaee8052b6..207aadb35d 100644
--- a/sysdeps/generic/dl-prop.h
+++ b/sysdeps/generic/dl-prop.h
@@ -47,7 +47,14 @@ static inline int __attribute__ ((always_inline))
_dl_process_gnu_property (struct link_map *l, int fd, uint32_t type,
uint32_t datasz, void *data)
{
- return 0;
+ /* Continue until GNU_PROPERTY_1_NEEDED is found. */
+ if (type == GNU_PROPERTY_1_NEEDED)
+ {
+ if (datasz == 4)
+ l->l_1_needed = *(unsigned int *) data;
+ return 0;
+ }
+ return 1;
}
#endif /* _DL_PROP_H */
diff --git a/sysdeps/generic/link_map.h b/sysdeps/generic/link_map.h
index a056184690..9f482b8c20 100644
--- a/sysdeps/generic/link_map.h
+++ b/sysdeps/generic/link_map.h
@@ -1 +1,2 @@
-/* No architecture specific definitions. */
+/* GNU_PROPERTY_1_NEEDED of this object. */
+unsigned int l_1_needed;
diff --git a/sysdeps/x86/dl-prop.h b/sysdeps/x86/dl-prop.h
index 56bd020b3c..385548fad3 100644
--- a/sysdeps/x86/dl-prop.h
+++ b/sysdeps/x86/dl-prop.h
@@ -97,6 +97,7 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note,
const ElfW(Addr) start = (ElfW(Addr)) note;
+ unsigned int needed_1 = 0;
unsigned int feature_1_and = 0;
unsigned int isa_1_needed = 0;
unsigned int last_type = 0;
@@ -141,7 +142,8 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note,
last_type = type;
if (type == GNU_PROPERTY_X86_FEATURE_1_AND
- || type == GNU_PROPERTY_X86_ISA_1_NEEDED)
+ || type == GNU_PROPERTY_X86_ISA_1_NEEDED
+ || type == GNU_PROPERTY_1_NEEDED)
{
/* The sizes of types which we are searching for are
4 bytes. There is no point to continue if this
@@ -151,12 +153,18 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note,
/* NB: Stop the scan only after seeing all types which
we are searching for. */
- _Static_assert ((GNU_PROPERTY_X86_ISA_1_NEEDED >
- GNU_PROPERTY_X86_FEATURE_1_AND),
+ _Static_assert (((GNU_PROPERTY_X86_ISA_1_NEEDED
+ > GNU_PROPERTY_X86_FEATURE_1_AND)
+ && (GNU_PROPERTY_X86_FEATURE_1_AND
+ > GNU_PROPERTY_1_NEEDED)),
"GNU_PROPERTY_X86_ISA_1_NEEDED > "
- "GNU_PROPERTY_X86_FEATURE_1_AND");
+ "GNU_PROPERTY_X86_FEATURE_1_AND && "
+ "GNU_PROPERTY_X86_FEATURE_1_AND > "
+ "GNU_PROPERTY_1_NEEDED");
if (type == GNU_PROPERTY_X86_FEATURE_1_AND)
feature_1_and = *(unsigned int *) ptr;
+ else if (type == GNU_PROPERTY_1_NEEDED)
+ needed_1 = *(unsigned int *) ptr;
else
{
isa_1_needed = *(unsigned int *) ptr;
@@ -187,9 +195,10 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note,
}
/* We get here only if there is one or no GNU property note. */
- if (isa_1_needed != 0 || feature_1_and != 0)
+ if (needed_1 != 0 || isa_1_needed != 0 || feature_1_and != 0)
{
l->l_property = lc_property_valid;
+ l->l_1_needed = needed_1;
l->l_x86_isa_1_needed = isa_1_needed;
l->l_x86_feature_1_and = feature_1_and;
}
diff --git a/sysdeps/x86/link_map.h b/sysdeps/x86/link_map.h
index 4c46a25f83..0c7e25dc96 100644
--- a/sysdeps/x86/link_map.h
+++ b/sysdeps/x86/link_map.h
@@ -29,3 +29,5 @@ unsigned int l_x86_feature_1_and;
/* GNU_PROPERTY_X86_ISA_1_NEEDED of this object. */
unsigned int l_x86_isa_1_needed;
+
+#include <sysdeps/generic/link_map.h>
--
2.31.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 2/2] Add run-time chesk for indirect external access
2021-08-03 21:59 [PATCH 1/2] Initial support for GNU_PROPERTY_1_NEEDED H.J. Lu via Libc-alpha
@ 2021-08-03 21:59 ` H.J. Lu via Libc-alpha
2021-09-27 2:10 ` PING^1 " H.J. Lu via Libc-alpha
2021-10-07 16:58 ` Adhemerval Zanella via Libc-alpha
2021-10-07 14:53 ` [PATCH 1/2] Initial support for GNU_PROPERTY_1_NEEDED Adhemerval Zanella via Libc-alpha
1 sibling, 2 replies; 10+ messages in thread
From: H.J. Lu via Libc-alpha @ 2021-08-03 21:59 UTC (permalink / raw)
To: libc-alpha
When performing symbol lookup for references in executable without
indirect external access:
1. Disallow copy relocations in executable against protected data symbols
in a shared object with indirect external access.
2. Disallow non-zero symbol values of undefined function symbols in
executable, which are used as the function pointer, against protected
function symbols in a shared object with indirect external access.
---
elf/dl-lookup.c | 5 ++++
sysdeps/generic/dl-protected.h | 54 ++++++++++++++++++++++++++++++++++
2 files changed, 59 insertions(+)
create mode 100644 sysdeps/generic/dl-protected.h
diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c
index eea217eb28..430359af39 100644
--- a/elf/dl-lookup.c
+++ b/elf/dl-lookup.c
@@ -24,6 +24,7 @@
#include <ldsodefs.h>
#include <dl-hash.h>
#include <dl-machine.h>
+#include <dl-protected.h>
#include <sysdep-cancel.h>
#include <libc-lock.h>
#include <tls.h>
@@ -527,6 +528,10 @@ do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
if (__glibc_unlikely (dl_symbol_visibility_binds_local_p (sym)))
goto skip;
+ if (ELFW(ST_VISIBILITY) (sym->st_other) == STV_PROTECTED)
+ _dl_check_protected_symbol (undef_name, undef_map, ref, map,
+ type_class);
+
switch (ELFW(ST_BIND) (sym->st_info))
{
case STB_WEAK:
diff --git a/sysdeps/generic/dl-protected.h b/sysdeps/generic/dl-protected.h
new file mode 100644
index 0000000000..244d020dc4
--- /dev/null
+++ b/sysdeps/generic/dl-protected.h
@@ -0,0 +1,54 @@
+/* Support for STV_PROTECTED visibility. Generic version.
+ Copyright (C) 2021 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_PROTECTED_H
+#define _DL_PROTECTED_H
+
+static inline void __attribute__ ((always_inline))
+_dl_check_protected_symbol (const char *undef_name,
+ const struct link_map *undef_map,
+ const ElfW(Sym) *ref,
+ const struct link_map *map,
+ int type_class)
+{
+ if (undef_map != NULL
+ && undef_map->l_type == lt_executable
+ && !(undef_map->l_1_needed
+ & GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS)
+ && (map->l_1_needed
+ & GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS))
+ {
+ if ((type_class & ELF_RTYPE_CLASS_COPY))
+ /* Disallow copy relocations in executable against protected
+ data symbols in a shared object which needs indirect external
+ access. */
+ _dl_signal_error (0, map->l_name, undef_name,
+ N_("copy relocation against non-copyable protected symbol"));
+ else if (ref->st_value != 0
+ && ref->st_shndx == SHN_UNDEF
+ && (type_class & ELF_RTYPE_CLASS_PLT))
+ /* Disallow non-zero symbol values of undefined symbols in
+ executable, which are used as the function pointer, against
+ protected function symbols in a shared object with indirect
+ external access. */
+ _dl_signal_error (0, map->l_name, undef_name,
+ N_("non-canonical reference to canonical protected function"));
+ }
+}
+
+#endif /* _DL_PROTECTED_H */
--
2.31.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* PING^1 [PATCH 2/2] Add run-time chesk for indirect external access
2021-08-03 21:59 ` [PATCH 2/2] Add run-time chesk for indirect external access H.J. Lu via Libc-alpha
@ 2021-09-27 2:10 ` H.J. Lu via Libc-alpha
2021-10-07 16:58 ` Adhemerval Zanella via Libc-alpha
1 sibling, 0 replies; 10+ messages in thread
From: H.J. Lu via Libc-alpha @ 2021-09-27 2:10 UTC (permalink / raw)
To: GNU C Library
On Tue, Aug 3, 2021 at 2:59 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> When performing symbol lookup for references in executable without
> indirect external access:
>
> 1. Disallow copy relocations in executable against protected data symbols
> in a shared object with indirect external access.
> 2. Disallow non-zero symbol values of undefined function symbols in
> executable, which are used as the function pointer, against protected
> function symbols in a shared object with indirect external access.
PING:
https://sourceware.org/pipermail/libc-alpha/2021-August/129798.html
https://sourceware.org/pipermail/libc-alpha/2021-August/129799.html
--
H.J.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 1/2] Initial support for GNU_PROPERTY_1_NEEDED
2021-08-03 21:59 [PATCH 1/2] Initial support for GNU_PROPERTY_1_NEEDED H.J. Lu via Libc-alpha
2021-08-03 21:59 ` [PATCH 2/2] Add run-time chesk for indirect external access H.J. Lu via Libc-alpha
@ 2021-10-07 14:53 ` Adhemerval Zanella via Libc-alpha
2021-10-07 15:11 ` H.J. Lu via Libc-alpha
1 sibling, 1 reply; 10+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2021-10-07 14:53 UTC (permalink / raw)
To: H.J. Lu, libc-alpha
On 03/08/2021 18:59, H.J. Lu via Libc-alpha wrote:
> 1. Add GNU_PROPERTY_1_NEEDED:
>
> #define GNU_PROPERTY_1_NEEDED GNU_PROPERTY_UINT32_OR_LO
>
> to indicate the needed properties by the object file.
> 2. Add GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
>
> #define GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS (1U << 0)
>
> to indicate that the object file requires canonical function pointers and
> cannot be used with copy relocation.
> 3. Scan GNU_PROPERTY_1_NEEDED property and store it in l_1_needed.
It was added by binutils with commit 6320fd00dc374f74658, maybe add on
the commit the version expected to have this support.
LGTM, with a small suggestion below.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
> ---
> elf/elf.h | 17 +++++++++++++++++
> sysdeps/generic/dl-prop.h | 9 ++++++++-
> sysdeps/generic/link_map.h | 3 ++-
> sysdeps/x86/dl-prop.h | 19 ++++++++++++++-----
> sysdeps/x86/link_map.h | 2 ++
> 5 files changed, 43 insertions(+), 7 deletions(-)
>
> diff --git a/elf/elf.h b/elf/elf.h
> index 4738dfa28f..50f87baceb 100644
> --- a/elf/elf.h
> +++ b/elf/elf.h
> @@ -1312,6 +1312,23 @@ typedef struct
> /* No copy relocation on protected data symbol. */
> #define GNU_PROPERTY_NO_COPY_ON_PROTECTED 2
>
> +/* A 4-byte unsigned integer property: A bit is set if it is set in all
> + relocatable inputs. */
> +#define GNU_PROPERTY_UINT32_AND_LO 0xb0000000
> +#define GNU_PROPERTY_UINT32_AND_HI 0xb0007fff
> +
> +/* A 4-byte unsigned integer property: A bit is set if it is set in any
> + relocatable inputs. */
> +#define GNU_PROPERTY_UINT32_OR_LO 0xb0008000
> +#define GNU_PROPERTY_UINT32_OR_HI 0xb000ffff
> +
> +/* The needed properties by the object file. */
> +#define GNU_PROPERTY_1_NEEDED GNU_PROPERTY_UINT32_OR_LO
> +
> +/* Set if the object file requires canonical function pointers and
> + cannot be used with copy relocation. */
> +#define GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS (1U << 0)
> +
> /* Processor-specific semantics, lo */
> #define GNU_PROPERTY_LOPROC 0xc0000000
> /* Processor-specific semantics, hi */
Ok.
> diff --git a/sysdeps/generic/dl-prop.h b/sysdeps/generic/dl-prop.h
> index eaee8052b6..207aadb35d 100644
> --- a/sysdeps/generic/dl-prop.h
> +++ b/sysdeps/generic/dl-prop.h
> @@ -47,7 +47,14 @@ static inline int __attribute__ ((always_inline))
> _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type,
> uint32_t datasz, void *data)
> {
> - return 0;
> + /* Continue until GNU_PROPERTY_1_NEEDED is found. */
> + if (type == GNU_PROPERTY_1_NEEDED)
> + {
> + if (datasz == 4)
> + l->l_1_needed = *(unsigned int *) data;
Although the pointer seems to be aligned, I think it would be better to
use memcpy here:
unsigned int t;
memcpy (&t, data, sizeof (t));
l->l_1_needed = t;
(the x86 header might use a more relax since the architecture always
support unaligned access)
> + return 0;
> + }
> + return 1;
> }
>
> #endif /* _DL_PROP_H */
> diff --git a/sysdeps/generic/link_map.h b/sysdeps/generic/link_map.h
> index a056184690..9f482b8c20 100644
> --- a/sysdeps/generic/link_map.h
> +++ b/sysdeps/generic/link_map.h
> @@ -1 +1,2 @@
> -/* No architecture specific definitions. */
> +/* GNU_PROPERTY_1_NEEDED of this object. */
> +unsigned int l_1_needed;
> diff --git a/sysdeps/x86/dl-prop.h b/sysdeps/x86/dl-prop.h
> index 56bd020b3c..385548fad3 100644
> --- a/sysdeps/x86/dl-prop.h
> +++ b/sysdeps/x86/dl-prop.h
> @@ -97,6 +97,7 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note,
>
> const ElfW(Addr) start = (ElfW(Addr)) note;
>
> + unsigned int needed_1 = 0;
> unsigned int feature_1_and = 0;
> unsigned int isa_1_needed = 0;
> unsigned int last_type = 0;
> @@ -141,7 +142,8 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note,
> last_type = type;
>
> if (type == GNU_PROPERTY_X86_FEATURE_1_AND
> - || type == GNU_PROPERTY_X86_ISA_1_NEEDED)
> + || type == GNU_PROPERTY_X86_ISA_1_NEEDED
> + || type == GNU_PROPERTY_1_NEEDED)
> {
> /* The sizes of types which we are searching for are
> 4 bytes. There is no point to continue if this
Ok.
> @@ -151,12 +153,18 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note,
>
> /* NB: Stop the scan only after seeing all types which
> we are searching for. */
> - _Static_assert ((GNU_PROPERTY_X86_ISA_1_NEEDED >
> - GNU_PROPERTY_X86_FEATURE_1_AND),
> + _Static_assert (((GNU_PROPERTY_X86_ISA_1_NEEDED
> + > GNU_PROPERTY_X86_FEATURE_1_AND)
> + && (GNU_PROPERTY_X86_FEATURE_1_AND
> + > GNU_PROPERTY_1_NEEDED)),
> "GNU_PROPERTY_X86_ISA_1_NEEDED > "
> - "GNU_PROPERTY_X86_FEATURE_1_AND");
> + "GNU_PROPERTY_X86_FEATURE_1_AND && "
> + "GNU_PROPERTY_X86_FEATURE_1_AND > "
> + "GNU_PROPERTY_1_NEEDED");
> if (type == GNU_PROPERTY_X86_FEATURE_1_AND)
> feature_1_and = *(unsigned int *) ptr;
> + else if (type == GNU_PROPERTY_1_NEEDED)
> + needed_1 = *(unsigned int *) ptr;
> else
> {
> isa_1_needed = *(unsigned int *) ptr;
Ok.
> @@ -187,9 +195,10 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note,
> }
>
> /* We get here only if there is one or no GNU property note. */
> - if (isa_1_needed != 0 || feature_1_and != 0)
> + if (needed_1 != 0 || isa_1_needed != 0 || feature_1_and != 0)
> {
> l->l_property = lc_property_valid;
> + l->l_1_needed = needed_1;
> l->l_x86_isa_1_needed = isa_1_needed;
> l->l_x86_feature_1_and = feature_1_and;
> }
Ok.
> diff --git a/sysdeps/x86/link_map.h b/sysdeps/x86/link_map.h
> index 4c46a25f83..0c7e25dc96 100644
> --- a/sysdeps/x86/link_map.h
> +++ b/sysdeps/x86/link_map.h
> @@ -29,3 +29,5 @@ unsigned int l_x86_feature_1_and;
>
> /* GNU_PROPERTY_X86_ISA_1_NEEDED of this object. */
> unsigned int l_x86_isa_1_needed;
> +
> +#include <sysdeps/generic/link_map.h>
>
Ok.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 1/2] Initial support for GNU_PROPERTY_1_NEEDED
2021-10-07 14:53 ` [PATCH 1/2] Initial support for GNU_PROPERTY_1_NEEDED Adhemerval Zanella via Libc-alpha
@ 2021-10-07 15:11 ` H.J. Lu via Libc-alpha
2021-10-07 16:53 ` Adhemerval Zanella via Libc-alpha
0 siblings, 1 reply; 10+ messages in thread
From: H.J. Lu via Libc-alpha @ 2021-10-07 15:11 UTC (permalink / raw)
To: Adhemerval Zanella; +Cc: GNU C Library
On Thu, Oct 7, 2021 at 7:53 AM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
>
>
> On 03/08/2021 18:59, H.J. Lu via Libc-alpha wrote:
> > 1. Add GNU_PROPERTY_1_NEEDED:
> >
> > #define GNU_PROPERTY_1_NEEDED GNU_PROPERTY_UINT32_OR_LO
> >
> > to indicate the needed properties by the object file.
> > 2. Add GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
> >
> > #define GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS (1U << 0)
> >
> > to indicate that the object file requires canonical function pointers and
> > cannot be used with copy relocation.
> > 3. Scan GNU_PROPERTY_1_NEEDED property and store it in l_1_needed.
>
> It was added by binutils with commit 6320fd00dc374f74658, maybe add on
> the commit the version expected to have this support.
>
> LGTM, with a small suggestion below.
>
> Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
>
> > ---
> > elf/elf.h | 17 +++++++++++++++++
> > sysdeps/generic/dl-prop.h | 9 ++++++++-
> > sysdeps/generic/link_map.h | 3 ++-
> > sysdeps/x86/dl-prop.h | 19 ++++++++++++++-----
> > sysdeps/x86/link_map.h | 2 ++
> > 5 files changed, 43 insertions(+), 7 deletions(-)
> >
> > diff --git a/elf/elf.h b/elf/elf.h
> > index 4738dfa28f..50f87baceb 100644
> > --- a/elf/elf.h
> > +++ b/elf/elf.h
> > @@ -1312,6 +1312,23 @@ typedef struct
> > /* No copy relocation on protected data symbol. */
> > #define GNU_PROPERTY_NO_COPY_ON_PROTECTED 2
> >
> > +/* A 4-byte unsigned integer property: A bit is set if it is set in all
> > + relocatable inputs. */
> > +#define GNU_PROPERTY_UINT32_AND_LO 0xb0000000
> > +#define GNU_PROPERTY_UINT32_AND_HI 0xb0007fff
> > +
> > +/* A 4-byte unsigned integer property: A bit is set if it is set in any
> > + relocatable inputs. */
> > +#define GNU_PROPERTY_UINT32_OR_LO 0xb0008000
> > +#define GNU_PROPERTY_UINT32_OR_HI 0xb000ffff
> > +
> > +/* The needed properties by the object file. */
> > +#define GNU_PROPERTY_1_NEEDED GNU_PROPERTY_UINT32_OR_LO
> > +
> > +/* Set if the object file requires canonical function pointers and
> > + cannot be used with copy relocation. */
> > +#define GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS (1U << 0)
> > +
> > /* Processor-specific semantics, lo */
> > #define GNU_PROPERTY_LOPROC 0xc0000000
> > /* Processor-specific semantics, hi */
>
> Ok.
>
> > diff --git a/sysdeps/generic/dl-prop.h b/sysdeps/generic/dl-prop.h
> > index eaee8052b6..207aadb35d 100644
> > --- a/sysdeps/generic/dl-prop.h
> > +++ b/sysdeps/generic/dl-prop.h
> > @@ -47,7 +47,14 @@ static inline int __attribute__ ((always_inline))
> > _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type,
> > uint32_t datasz, void *data)
> > {
> > - return 0;
> > + /* Continue until GNU_PROPERTY_1_NEEDED is found. */
> > + if (type == GNU_PROPERTY_1_NEEDED)
> > + {
> > + if (datasz == 4)
> > + l->l_1_needed = *(unsigned int *) data;
>
> Although the pointer seems to be aligned, I think it would be better to
> use memcpy here:
>
> unsigned int t;
> memcpy (&t, data, sizeof (t));
> l->l_1_needed = t;
>
> (the x86 header might use a more relax since the architecture always
> support unaligned access)
The GNU property is guaranteed to be properly aligned:
void
_dl_process_pt_gnu_property (struct link_map *l, int fd, const ElfW(Phdr) *ph)
{
const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr);
const ElfW(Addr) size = ph->p_memsz;
const ElfW(Addr) align = ph->p_align;
/* The NT_GNU_PROPERTY_TYPE_0 note must be aligned 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;
data passed to _dl_process_gnu_property should be properly aligned.
> > + return 0;
> > + }
> > + return 1;
> > }
> >
> > #endif /* _DL_PROP_H */
> > diff --git a/sysdeps/generic/link_map.h b/sysdeps/generic/link_map.h
> > index a056184690..9f482b8c20 100644
> > --- a/sysdeps/generic/link_map.h
> > +++ b/sysdeps/generic/link_map.h
> > @@ -1 +1,2 @@
> > -/* No architecture specific definitions. */
> > +/* GNU_PROPERTY_1_NEEDED of this object. */
> > +unsigned int l_1_needed;
> > diff --git a/sysdeps/x86/dl-prop.h b/sysdeps/x86/dl-prop.h
> > index 56bd020b3c..385548fad3 100644
> > --- a/sysdeps/x86/dl-prop.h
> > +++ b/sysdeps/x86/dl-prop.h
> > @@ -97,6 +97,7 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note,
> >
> > const ElfW(Addr) start = (ElfW(Addr)) note;
> >
> > + unsigned int needed_1 = 0;
> > unsigned int feature_1_and = 0;
> > unsigned int isa_1_needed = 0;
> > unsigned int last_type = 0;
> > @@ -141,7 +142,8 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note,
> > last_type = type;
> >
> > if (type == GNU_PROPERTY_X86_FEATURE_1_AND
> > - || type == GNU_PROPERTY_X86_ISA_1_NEEDED)
> > + || type == GNU_PROPERTY_X86_ISA_1_NEEDED
> > + || type == GNU_PROPERTY_1_NEEDED)
> > {
> > /* The sizes of types which we are searching for are
> > 4 bytes. There is no point to continue if this
>
> Ok.
>
> > @@ -151,12 +153,18 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note,
> >
> > /* NB: Stop the scan only after seeing all types which
> > we are searching for. */
> > - _Static_assert ((GNU_PROPERTY_X86_ISA_1_NEEDED >
> > - GNU_PROPERTY_X86_FEATURE_1_AND),
> > + _Static_assert (((GNU_PROPERTY_X86_ISA_1_NEEDED
> > + > GNU_PROPERTY_X86_FEATURE_1_AND)
> > + && (GNU_PROPERTY_X86_FEATURE_1_AND
> > + > GNU_PROPERTY_1_NEEDED)),
> > "GNU_PROPERTY_X86_ISA_1_NEEDED > "
> > - "GNU_PROPERTY_X86_FEATURE_1_AND");
> > + "GNU_PROPERTY_X86_FEATURE_1_AND && "
> > + "GNU_PROPERTY_X86_FEATURE_1_AND > "
> > + "GNU_PROPERTY_1_NEEDED");
> > if (type == GNU_PROPERTY_X86_FEATURE_1_AND)
> > feature_1_and = *(unsigned int *) ptr;
> > + else if (type == GNU_PROPERTY_1_NEEDED)
> > + needed_1 = *(unsigned int *) ptr;
> > else
> > {
> > isa_1_needed = *(unsigned int *) ptr;
>
> Ok.
>
> > @@ -187,9 +195,10 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note,
> > }
> >
> > /* We get here only if there is one or no GNU property note. */
> > - if (isa_1_needed != 0 || feature_1_and != 0)
> > + if (needed_1 != 0 || isa_1_needed != 0 || feature_1_and != 0)
> > {
> > l->l_property = lc_property_valid;
> > + l->l_1_needed = needed_1;
> > l->l_x86_isa_1_needed = isa_1_needed;
> > l->l_x86_feature_1_and = feature_1_and;
> > }
>
> Ok.
>
> > diff --git a/sysdeps/x86/link_map.h b/sysdeps/x86/link_map.h
> > index 4c46a25f83..0c7e25dc96 100644
> > --- a/sysdeps/x86/link_map.h
> > +++ b/sysdeps/x86/link_map.h
> > @@ -29,3 +29,5 @@ unsigned int l_x86_feature_1_and;
> >
> > /* GNU_PROPERTY_X86_ISA_1_NEEDED of this object. */
> > unsigned int l_x86_isa_1_needed;
> > +
> > +#include <sysdeps/generic/link_map.h>
> >
>
> Ok.
--
H.J.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 1/2] Initial support for GNU_PROPERTY_1_NEEDED
2021-10-07 15:11 ` H.J. Lu via Libc-alpha
@ 2021-10-07 16:53 ` Adhemerval Zanella via Libc-alpha
2021-10-07 17:04 ` H.J. Lu via Libc-alpha
0 siblings, 1 reply; 10+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2021-10-07 16:53 UTC (permalink / raw)
To: H.J. Lu; +Cc: GNU C Library
On 07/10/2021 12:11, H.J. Lu wrote:
> On Thu, Oct 7, 2021 at 7:53 AM Adhemerval Zanella
> <adhemerval.zanella@linaro.org> wrote:
>>
>>
>>
>> On 03/08/2021 18:59, H.J. Lu via Libc-alpha wrote:
>>> 1. Add GNU_PROPERTY_1_NEEDED:
>>>
>>> #define GNU_PROPERTY_1_NEEDED GNU_PROPERTY_UINT32_OR_LO
>>>
>>> to indicate the needed properties by the object file.
>>> 2. Add GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
>>>
>>> #define GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS (1U << 0)
>>>
>>> to indicate that the object file requires canonical function pointers and
>>> cannot be used with copy relocation.
>>> 3. Scan GNU_PROPERTY_1_NEEDED property and store it in l_1_needed.
>>
>> It was added by binutils with commit 6320fd00dc374f74658, maybe add on
>> the commit the version expected to have this support.
>>
>> LGTM, with a small suggestion below.
>>
>> Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
>>
>>> ---
>>> elf/elf.h | 17 +++++++++++++++++
>>> sysdeps/generic/dl-prop.h | 9 ++++++++-
>>> sysdeps/generic/link_map.h | 3 ++-
>>> sysdeps/x86/dl-prop.h | 19 ++++++++++++++-----
>>> sysdeps/x86/link_map.h | 2 ++
>>> 5 files changed, 43 insertions(+), 7 deletions(-)
>>>
>>> diff --git a/elf/elf.h b/elf/elf.h
>>> index 4738dfa28f..50f87baceb 100644
>>> --- a/elf/elf.h
>>> +++ b/elf/elf.h
>>> @@ -1312,6 +1312,23 @@ typedef struct
>>> /* No copy relocation on protected data symbol. */
>>> #define GNU_PROPERTY_NO_COPY_ON_PROTECTED 2
>>>
>>> +/* A 4-byte unsigned integer property: A bit is set if it is set in all
>>> + relocatable inputs. */
>>> +#define GNU_PROPERTY_UINT32_AND_LO 0xb0000000
>>> +#define GNU_PROPERTY_UINT32_AND_HI 0xb0007fff
>>> +
>>> +/* A 4-byte unsigned integer property: A bit is set if it is set in any
>>> + relocatable inputs. */
>>> +#define GNU_PROPERTY_UINT32_OR_LO 0xb0008000
>>> +#define GNU_PROPERTY_UINT32_OR_HI 0xb000ffff
>>> +
>>> +/* The needed properties by the object file. */
>>> +#define GNU_PROPERTY_1_NEEDED GNU_PROPERTY_UINT32_OR_LO
>>> +
>>> +/* Set if the object file requires canonical function pointers and
>>> + cannot be used with copy relocation. */
>>> +#define GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS (1U << 0)
>>> +
>>> /* Processor-specific semantics, lo */
>>> #define GNU_PROPERTY_LOPROC 0xc0000000
>>> /* Processor-specific semantics, hi */
>>
>> Ok.
>>
>>> diff --git a/sysdeps/generic/dl-prop.h b/sysdeps/generic/dl-prop.h
>>> index eaee8052b6..207aadb35d 100644
>>> --- a/sysdeps/generic/dl-prop.h
>>> +++ b/sysdeps/generic/dl-prop.h
>>> @@ -47,7 +47,14 @@ static inline int __attribute__ ((always_inline))
>>> _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type,
>>> uint32_t datasz, void *data)
>>> {
>>> - return 0;
>>> + /* Continue until GNU_PROPERTY_1_NEEDED is found. */
>>> + if (type == GNU_PROPERTY_1_NEEDED)
>>> + {
>>> + if (datasz == 4)
>>> + l->l_1_needed = *(unsigned int *) data;
>>
>> Although the pointer seems to be aligned, I think it would be better to
>> use memcpy here:
>>
>> unsigned int t;
>> memcpy (&t, data, sizeof (t));
>> l->l_1_needed = t;
>>
>> (the x86 header might use a more relax since the architecture always
>> support unaligned access)
>
> The GNU property is guaranteed to be properly aligned:
>
> void
> _dl_process_pt_gnu_property (struct link_map *l, int fd, const ElfW(Phdr) *ph)
> {
> const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr);
> const ElfW(Addr) size = ph->p_memsz;
> const ElfW(Addr) align = ph->p_align;
>
> /* The NT_GNU_PROPERTY_TYPE_0 note must be aligned 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;
>
> data passed to _dl_process_gnu_property should be properly aligned.
Alright, the memcpy seems unnecessary then.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 2/2] Add run-time chesk for indirect external access
2021-08-03 21:59 ` [PATCH 2/2] Add run-time chesk for indirect external access H.J. Lu via Libc-alpha
2021-09-27 2:10 ` PING^1 " H.J. Lu via Libc-alpha
@ 2021-10-07 16:58 ` Adhemerval Zanella via Libc-alpha
2021-10-07 17:10 ` H.J. Lu via Libc-alpha
1 sibling, 1 reply; 10+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2021-10-07 16:58 UTC (permalink / raw)
To: H.J. Lu, libc-alpha
On 03/08/2021 18:59, H.J. Lu via Libc-alpha wrote:
> When performing symbol lookup for references in executable without
> indirect external access:
>
> 1. Disallow copy relocations in executable against protected data symbols
> in a shared object with indirect external access.
> 2. Disallow non-zero symbol values of undefined function symbols in
> executable, which are used as the function pointer, against protected
> function symbols in a shared object with indirect external access.
How hard would to add some testcases for both cases? To simplify we may
want to build it iff binutins supports noindirect-extern-access.
The rest LGTM, just a nit below due an ununsed variable.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
> ---
> elf/dl-lookup.c | 5 ++++
> sysdeps/generic/dl-protected.h | 54 ++++++++++++++++++++++++++++++++++
> 2 files changed, 59 insertions(+)
> create mode 100644 sysdeps/generic/dl-protected.h
>
> diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c
> index eea217eb28..430359af39 100644
> --- a/elf/dl-lookup.c
> +++ b/elf/dl-lookup.c
> @@ -24,6 +24,7 @@
> #include <ldsodefs.h>
> #include <dl-hash.h>
> #include <dl-machine.h>
> +#include <dl-protected.h>
> #include <sysdep-cancel.h>
> #include <libc-lock.h>
> #include <tls.h>
> @@ -527,6 +528,10 @@ do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
> if (__glibc_unlikely (dl_symbol_visibility_binds_local_p (sym)))
> goto skip;
>
> + if (ELFW(ST_VISIBILITY) (sym->st_other) == STV_PROTECTED)
> + _dl_check_protected_symbol (undef_name, undef_map, ref, map,
> + type_class);
> +
> switch (ELFW(ST_BIND) (sym->st_info))
> {
> case STB_WEAK:
Ok.
> diff --git a/sysdeps/generic/dl-protected.h b/sysdeps/generic/dl-protected.h
> new file mode 100644
> index 0000000000..244d020dc4
> --- /dev/null
> +++ b/sysdeps/generic/dl-protected.h
> @@ -0,0 +1,54 @@
> +/* Support for STV_PROTECTED visibility. Generic version.
> + Copyright (C) 2021 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_PROTECTED_H
> +#define _DL_PROTECTED_H
> +
> +static inline void __attribute__ ((always_inline))
> +_dl_check_protected_symbol (const char *undef_name,
This argument seems unused.
> + const struct link_map *undef_map,
> + const ElfW(Sym) *ref,
> + const struct link_map *map,
> + int type_class)
> +{
> + if (undef_map != NULL
> + && undef_map->l_type == lt_executable
> + && !(undef_map->l_1_needed
> + & GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS)
> + && (map->l_1_needed
> + & GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS))
> + {
> + if ((type_class & ELF_RTYPE_CLASS_COPY))
> + /* Disallow copy relocations in executable against protected
> + data symbols in a shared object which needs indirect external
> + access. */
> + _dl_signal_error (0, map->l_name, undef_name,
> + N_("copy relocation against non-copyable protected symbol"));
> + else if (ref->st_value != 0
> + && ref->st_shndx == SHN_UNDEF
> + && (type_class & ELF_RTYPE_CLASS_PLT))
> + /* Disallow non-zero symbol values of undefined symbols in
> + executable, which are used as the function pointer, against
> + protected function symbols in a shared object with indirect
> + external access. */
> + _dl_signal_error (0, map->l_name, undef_name,
> + N_("non-canonical reference to canonical protected function"));
> + }
> +}
> +
> +#endif /* _DL_PROTECTED_H */
>
Ok.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 1/2] Initial support for GNU_PROPERTY_1_NEEDED
2021-10-07 16:53 ` Adhemerval Zanella via Libc-alpha
@ 2021-10-07 17:04 ` H.J. Lu via Libc-alpha
0 siblings, 0 replies; 10+ messages in thread
From: H.J. Lu via Libc-alpha @ 2021-10-07 17:04 UTC (permalink / raw)
To: Adhemerval Zanella; +Cc: GNU C Library
On Thu, Oct 7, 2021 at 9:53 AM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
>
>
> On 07/10/2021 12:11, H.J. Lu wrote:
> > On Thu, Oct 7, 2021 at 7:53 AM Adhemerval Zanella
> > <adhemerval.zanella@linaro.org> wrote:
> >>
> >>
> >>
> >> On 03/08/2021 18:59, H.J. Lu via Libc-alpha wrote:
> >>> 1. Add GNU_PROPERTY_1_NEEDED:
> >>>
> >>> #define GNU_PROPERTY_1_NEEDED GNU_PROPERTY_UINT32_OR_LO
> >>>
> >>> to indicate the needed properties by the object file.
> >>> 2. Add GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
> >>>
> >>> #define GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS (1U << 0)
> >>>
> >>> to indicate that the object file requires canonical function pointers and
> >>> cannot be used with copy relocation.
> >>> 3. Scan GNU_PROPERTY_1_NEEDED property and store it in l_1_needed.
> >>
> >> It was added by binutils with commit 6320fd00dc374f74658, maybe add on
> >> the commit the version expected to have this support.
> >>
> >> LGTM, with a small suggestion below.
> >>
> >> Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
> >>
> >>> ---
> >>> elf/elf.h | 17 +++++++++++++++++
> >>> sysdeps/generic/dl-prop.h | 9 ++++++++-
> >>> sysdeps/generic/link_map.h | 3 ++-
> >>> sysdeps/x86/dl-prop.h | 19 ++++++++++++++-----
> >>> sysdeps/x86/link_map.h | 2 ++
> >>> 5 files changed, 43 insertions(+), 7 deletions(-)
> >>>
> >>> diff --git a/elf/elf.h b/elf/elf.h
> >>> index 4738dfa28f..50f87baceb 100644
> >>> --- a/elf/elf.h
> >>> +++ b/elf/elf.h
> >>> @@ -1312,6 +1312,23 @@ typedef struct
> >>> /* No copy relocation on protected data symbol. */
> >>> #define GNU_PROPERTY_NO_COPY_ON_PROTECTED 2
> >>>
> >>> +/* A 4-byte unsigned integer property: A bit is set if it is set in all
> >>> + relocatable inputs. */
> >>> +#define GNU_PROPERTY_UINT32_AND_LO 0xb0000000
> >>> +#define GNU_PROPERTY_UINT32_AND_HI 0xb0007fff
> >>> +
> >>> +/* A 4-byte unsigned integer property: A bit is set if it is set in any
> >>> + relocatable inputs. */
> >>> +#define GNU_PROPERTY_UINT32_OR_LO 0xb0008000
> >>> +#define GNU_PROPERTY_UINT32_OR_HI 0xb000ffff
> >>> +
> >>> +/* The needed properties by the object file. */
> >>> +#define GNU_PROPERTY_1_NEEDED GNU_PROPERTY_UINT32_OR_LO
> >>> +
> >>> +/* Set if the object file requires canonical function pointers and
> >>> + cannot be used with copy relocation. */
> >>> +#define GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS (1U << 0)
> >>> +
> >>> /* Processor-specific semantics, lo */
> >>> #define GNU_PROPERTY_LOPROC 0xc0000000
> >>> /* Processor-specific semantics, hi */
> >>
> >> Ok.
> >>
> >>> diff --git a/sysdeps/generic/dl-prop.h b/sysdeps/generic/dl-prop.h
> >>> index eaee8052b6..207aadb35d 100644
> >>> --- a/sysdeps/generic/dl-prop.h
> >>> +++ b/sysdeps/generic/dl-prop.h
> >>> @@ -47,7 +47,14 @@ static inline int __attribute__ ((always_inline))
> >>> _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type,
> >>> uint32_t datasz, void *data)
> >>> {
> >>> - return 0;
> >>> + /* Continue until GNU_PROPERTY_1_NEEDED is found. */
> >>> + if (type == GNU_PROPERTY_1_NEEDED)
> >>> + {
> >>> + if (datasz == 4)
> >>> + l->l_1_needed = *(unsigned int *) data;
> >>
> >> Although the pointer seems to be aligned, I think it would be better to
> >> use memcpy here:
> >>
> >> unsigned int t;
> >> memcpy (&t, data, sizeof (t));
> >> l->l_1_needed = t;
> >>
> >> (the x86 header might use a more relax since the architecture always
> >> support unaligned access)
> >
> > The GNU property is guaranteed to be properly aligned:
> >
> > void
> > _dl_process_pt_gnu_property (struct link_map *l, int fd, const ElfW(Phdr) *ph)
> > {
> > const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr);
> > const ElfW(Addr) size = ph->p_memsz;
> > const ElfW(Addr) align = ph->p_align;
> >
> > /* The NT_GNU_PROPERTY_TYPE_0 note must be aligned 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;
> >
> > data passed to _dl_process_gnu_property should be properly aligned.
>
> Alright, the memcpy seems unnecessary then.
I will check in my patch ASIS.
Thanks.
--
H.J.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 2/2] Add run-time chesk for indirect external access
2021-10-07 16:58 ` Adhemerval Zanella via Libc-alpha
@ 2021-10-07 17:10 ` H.J. Lu via Libc-alpha
2021-10-07 17:45 ` Adhemerval Zanella via Libc-alpha
0 siblings, 1 reply; 10+ messages in thread
From: H.J. Lu via Libc-alpha @ 2021-10-07 17:10 UTC (permalink / raw)
To: Adhemerval Zanella; +Cc: GNU C Library
On Thu, Oct 7, 2021 at 9:58 AM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
>
>
> On 03/08/2021 18:59, H.J. Lu via Libc-alpha wrote:
> > When performing symbol lookup for references in executable without
> > indirect external access:
> >
> > 1. Disallow copy relocations in executable against protected data symbols
> > in a shared object with indirect external access.
> > 2. Disallow non-zero symbol values of undefined function symbols in
> > executable, which are used as the function pointer, against protected
> > function symbols in a shared object with indirect external access.
>
> How hard would to add some testcases for both cases? To simplify we may
> want to build it iff binutins supports noindirect-extern-access.
I will submit followup patches with testcases from
users/hjl/indirect/master branch:
https://gitlab.com/x86-glibc/glibc/-/tree/users/hjl/indirect/master
including adding LD_DEBUG=protected to check copy relocations against
protected data and non-canonical reference to protected function.
> The rest LGTM, just a nit below due an ununsed variable.
>
> Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
>
> > ---
> > elf/dl-lookup.c | 5 ++++
> > sysdeps/generic/dl-protected.h | 54 ++++++++++++++++++++++++++++++++++
> > 2 files changed, 59 insertions(+)
> > create mode 100644 sysdeps/generic/dl-protected.h
> >
> > diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c
> > index eea217eb28..430359af39 100644
> > --- a/elf/dl-lookup.c
> > +++ b/elf/dl-lookup.c
> > @@ -24,6 +24,7 @@
> > #include <ldsodefs.h>
> > #include <dl-hash.h>
> > #include <dl-machine.h>
> > +#include <dl-protected.h>
> > #include <sysdep-cancel.h>
> > #include <libc-lock.h>
> > #include <tls.h>
> > @@ -527,6 +528,10 @@ do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
> > if (__glibc_unlikely (dl_symbol_visibility_binds_local_p (sym)))
> > goto skip;
> >
> > + if (ELFW(ST_VISIBILITY) (sym->st_other) == STV_PROTECTED)
> > + _dl_check_protected_symbol (undef_name, undef_map, ref, map,
> > + type_class);
> > +
> > switch (ELFW(ST_BIND) (sym->st_info))
> > {
> > case STB_WEAK:
>
> Ok.
>
> > diff --git a/sysdeps/generic/dl-protected.h b/sysdeps/generic/dl-protected.h
> > new file mode 100644
> > index 0000000000..244d020dc4
> > --- /dev/null
> > +++ b/sysdeps/generic/dl-protected.h
> > @@ -0,0 +1,54 @@
> > +/* Support for STV_PROTECTED visibility. Generic version.
> > + Copyright (C) 2021 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_PROTECTED_H
> > +#define _DL_PROTECTED_H
> > +
> > +static inline void __attribute__ ((always_inline))
> > +_dl_check_protected_symbol (const char *undef_name,
>
> This argument seems unused.
It is used in
_dl_signal_error (0, map->l_name, undef_name,
N_("non-canonical reference to canonical
protected function"));
> > + const struct link_map *undef_map,
> > + const ElfW(Sym) *ref,
> > + const struct link_map *map,
> > + int type_class)
> > +{
> > + if (undef_map != NULL
> > + && undef_map->l_type == lt_executable
> > + && !(undef_map->l_1_needed
> > + & GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS)
> > + && (map->l_1_needed
> > + & GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS))
> > + {
> > + if ((type_class & ELF_RTYPE_CLASS_COPY))
> > + /* Disallow copy relocations in executable against protected
> > + data symbols in a shared object which needs indirect external
> > + access. */
> > + _dl_signal_error (0, map->l_name, undef_name,
> > + N_("copy relocation against non-copyable protected symbol"));
> > + else if (ref->st_value != 0
> > + && ref->st_shndx == SHN_UNDEF
> > + && (type_class & ELF_RTYPE_CLASS_PLT))
> > + /* Disallow non-zero symbol values of undefined symbols in
> > + executable, which are used as the function pointer, against
> > + protected function symbols in a shared object with indirect
> > + external access. */
> > + _dl_signal_error (0, map->l_name, undef_name,
> > + N_("non-canonical reference to canonical protected function"));
> > + }
> > +}
> > +
> > +#endif /* _DL_PROTECTED_H */
> >
>
> Ok.
I will check it in ASIS.
Thanks.
--
H.J.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 2/2] Add run-time chesk for indirect external access
2021-10-07 17:10 ` H.J. Lu via Libc-alpha
@ 2021-10-07 17:45 ` Adhemerval Zanella via Libc-alpha
0 siblings, 0 replies; 10+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2021-10-07 17:45 UTC (permalink / raw)
To: H.J. Lu; +Cc: GNU C Library
On 07/10/2021 14:10, H.J. Lu wrote:
> On Thu, Oct 7, 2021 at 9:58 AM Adhemerval Zanella
> <adhemerval.zanella@linaro.org> wrote:
>>
>>
>>
>> On 03/08/2021 18:59, H.J. Lu via Libc-alpha wrote:
>>> When performing symbol lookup for references in executable without
>>> indirect external access:
>>>
>>> 1. Disallow copy relocations in executable against protected data symbols
>>> in a shared object with indirect external access.
>>> 2. Disallow non-zero symbol values of undefined function symbols in
>>> executable, which are used as the function pointer, against protected
>>> function symbols in a shared object with indirect external access.
>>
>> How hard would to add some testcases for both cases? To simplify we may
>> want to build it iff binutins supports noindirect-extern-access.
>
> I will submit followup patches with testcases from
> users/hjl/indirect/master branch:
>
> https://gitlab.com/x86-glibc/glibc/-/tree/users/hjl/indirect/master
>
> including adding LD_DEBUG=protected to check copy relocations against
> protected data and non-canonical reference to protected function.
>
>> The rest LGTM, just a nit below due an ununsed variable.
>>
>> Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
>>
>>> ---
>>> elf/dl-lookup.c | 5 ++++
>>> sysdeps/generic/dl-protected.h | 54 ++++++++++++++++++++++++++++++++++
>>> 2 files changed, 59 insertions(+)
>>> create mode 100644 sysdeps/generic/dl-protected.h
>>>
>>> diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c
>>> index eea217eb28..430359af39 100644
>>> --- a/elf/dl-lookup.c
>>> +++ b/elf/dl-lookup.c
>>> @@ -24,6 +24,7 @@
>>> #include <ldsodefs.h>
>>> #include <dl-hash.h>
>>> #include <dl-machine.h>
>>> +#include <dl-protected.h>
>>> #include <sysdep-cancel.h>
>>> #include <libc-lock.h>
>>> #include <tls.h>
>>> @@ -527,6 +528,10 @@ do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
>>> if (__glibc_unlikely (dl_symbol_visibility_binds_local_p (sym)))
>>> goto skip;
>>>
>>> + if (ELFW(ST_VISIBILITY) (sym->st_other) == STV_PROTECTED)
>>> + _dl_check_protected_symbol (undef_name, undef_map, ref, map,
>>> + type_class);
>>> +
>>> switch (ELFW(ST_BIND) (sym->st_info))
>>> {
>>> case STB_WEAK:
>>
>> Ok.
>>
>>> diff --git a/sysdeps/generic/dl-protected.h b/sysdeps/generic/dl-protected.h
>>> new file mode 100644
>>> index 0000000000..244d020dc4
>>> --- /dev/null
>>> +++ b/sysdeps/generic/dl-protected.h
>>> @@ -0,0 +1,54 @@
>>> +/* Support for STV_PROTECTED visibility. Generic version.
>>> + Copyright (C) 2021 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_PROTECTED_H
>>> +#define _DL_PROTECTED_H
>>> +
>>> +static inline void __attribute__ ((always_inline))
>>> +_dl_check_protected_symbol (const char *undef_name,
>>
>> This argument seems unused.
>
> It is used in
>
> _dl_signal_error (0, map->l_name, undef_name,
> N_("non-canonical reference to canonical
> protected function"));
Indeed, you are right.
>
>>> + const struct link_map *undef_map,
>>> + const ElfW(Sym) *ref,
>>> + const struct link_map *map,
>>> + int type_class)
>>> +{
>>> + if (undef_map != NULL
>>> + && undef_map->l_type == lt_executable
>>> + && !(undef_map->l_1_needed
>>> + & GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS)
>>> + && (map->l_1_needed
>>> + & GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS))
>>> + {
>>> + if ((type_class & ELF_RTYPE_CLASS_COPY))
>>> + /* Disallow copy relocations in executable against protected
>>> + data symbols in a shared object which needs indirect external
>>> + access. */
>>> + _dl_signal_error (0, map->l_name, undef_name,
>>> + N_("copy relocation against non-copyable protected symbol"));
>>> + else if (ref->st_value != 0
>>> + && ref->st_shndx == SHN_UNDEF
>>> + && (type_class & ELF_RTYPE_CLASS_PLT))
>>> + /* Disallow non-zero symbol values of undefined symbols in
>>> + executable, which are used as the function pointer, against
>>> + protected function symbols in a shared object with indirect
>>> + external access. */
>>> + _dl_signal_error (0, map->l_name, undef_name,
>>> + N_("non-canonical reference to canonical protected function"));
>>> + }
>>> +}
>>> +
>>> +#endif /* _DL_PROTECTED_H */
>>>
>>
>> Ok.
>
> I will check it in ASIS.
>
> Thanks.
>
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2021-10-07 17:45 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-03 21:59 [PATCH 1/2] Initial support for GNU_PROPERTY_1_NEEDED H.J. Lu via Libc-alpha
2021-08-03 21:59 ` [PATCH 2/2] Add run-time chesk for indirect external access H.J. Lu via Libc-alpha
2021-09-27 2:10 ` PING^1 " H.J. Lu via Libc-alpha
2021-10-07 16:58 ` Adhemerval Zanella via Libc-alpha
2021-10-07 17:10 ` H.J. Lu via Libc-alpha
2021-10-07 17:45 ` Adhemerval Zanella via Libc-alpha
2021-10-07 14:53 ` [PATCH 1/2] Initial support for GNU_PROPERTY_1_NEEDED Adhemerval Zanella via Libc-alpha
2021-10-07 15:11 ` H.J. Lu via Libc-alpha
2021-10-07 16:53 ` Adhemerval Zanella via Libc-alpha
2021-10-07 17:04 ` H.J. Lu via Libc-alpha
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).