diff mbox series

[v4,3/4] linux-user: Parse NT_GNU_PROPERTY_TYPE_0 notes

Message ID 20190330005900.17282-4-richard.henderson@linaro.org
State New
Headers show
Series target/arm: Implement ARMv8.5-BTI for linux-user | expand

Commit Message

Richard Henderson March 30, 2019, 12:58 a.m. UTC
For aarch64, this includes the GNU_PROPERTY_AARCH64_FEATURE_1_BTI bit,
which indicates that the image should be mapped with guarded pages.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>

---
 linux-user/elfload.c | 79 ++++++++++++++++++++++++++++++++++++++------
 1 file changed, 68 insertions(+), 11 deletions(-)

-- 
2.17.1

Comments

Peter Maydell April 29, 2019, 4:17 p.m. UTC | #1
On Sat, 30 Mar 2019 at 00:59, Richard Henderson
<richard.henderson@linaro.org> wrote:
>

> For aarch64, this includes the GNU_PROPERTY_AARCH64_FEATURE_1_BTI bit,

> which indicates that the image should be mapped with guarded pages.

>

> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>

> ---

>  linux-user/elfload.c | 79 ++++++++++++++++++++++++++++++++++++++------

>  1 file changed, 68 insertions(+), 11 deletions(-)

>

> diff --git a/linux-user/elfload.c b/linux-user/elfload.c

> index c1a26021f8..12ee96e5d4 100644

> --- a/linux-user/elfload.c

> +++ b/linux-user/elfload.c

> @@ -116,6 +116,10 @@ typedef abi_uint        target_gid_t;

>  #endif

>  typedef abi_int         target_pid_t;

>

> +

> +#define TARGET_NT_GNU_PROPERTY_TYPE_0  5

> +

> +

>  #ifdef TARGET_I386

>

>  #define ELF_PLATFORM get_elf_platform()

> @@ -543,6 +547,10 @@ static const char *get_elf_platform(void)

>  # define ELF_PLATFORM    "aarch64"

>  #endif

>

> +#define TARGET_GNU_PROPERTY_AARCH64_FEATURE_1_AND  0xc0000000

> +#define TARGET_GNU_PROPERTY_AARCH64_FEATURE_1_BTI  (1u << 0)

> +#define TARGET_GNU_PROPERTY_AARCH64_FEATURE_1_PAC  (1u << 1)

> +

>  static inline void init_thread(struct target_pt_regs *regs,

>                                 struct image_info *infop)

>  {

> @@ -2252,7 +2260,7 @@ static void load_elf_image(const char *image_name, int image_fd,

>      struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;

>      struct elf_phdr *phdr;

>      abi_ulong load_addr, load_bias, loaddr, hiaddr, error;

> -    int i, retval;

> +    int i, retval, prot_exec = PROT_EXEC;

>      const char *errmsg;

>

>      /* First of all, some simple consistency checks */

> @@ -2287,17 +2295,66 @@ static void load_elf_image(const char *image_name, int image_fd,

>      loaddr = -1, hiaddr = 0;

>      info->alignment = 0;

>      for (i = 0; i < ehdr->e_phnum; ++i) {

> -        if (phdr[i].p_type == PT_LOAD) {

> -            abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset;

> -            if (a < loaddr) {

> -                loaddr = a;

> +        struct elf_phdr *eppnt = phdr + i;

> +

> +        switch (eppnt->p_type) {

> +        case PT_LOAD:

> +            {


I think you have an extra layer of indent here that we usually
don't do for switch statement cases.


> +#ifdef BSWAP_NEEDED

> +                for (i = 0; i < ARRAY_SIZE(note); ++i) {

> +                    bswap32s(note + i);

> +                }

> +#endif

> +#ifdef HOST_WORDS_BIGENDIAN

> +                gnu0 = 'G' << 24 | 'N' << 16 | 'U' << 8;

> +#else

> +                gnu0 = 'G' | 'N' << 8 | 'U' << 16;

> +#endif


#define GNU0_MAGIC const_le32('G' | 'N' << 8 | 'U' << 16)

and then you can avoid the #ifdef HOST_WORDS_BIGENDIAN?

> +

> +                if (note[0] != 4 ||     /* namesz */

> +                    note[1] < 12 ||     /* descsz -- may include padding */

> +                    note[2] != TARGET_NT_GNU_PROPERTY_TYPE_0 ||  /* type */

> +                    note[3] != gnu0) {  /* name */

> +                    break;

> +                }

> +#ifdef TARGET_AARCH64

> +                if (note[4] == TARGET_GNU_PROPERTY_AARCH64_FEATURE_1_AND &&

> +                    note[5] == 4 &&

> +                    (note[6] & TARGET_GNU_PROPERTY_AARCH64_FEATURE_1_BTI)) {

> +                    prot_exec |= TARGET_PROT_BTI;

> +                }

> +#endif

>              }

> -            ++info->nsegs;

> -            info->alignment |= phdr[i].p_align;

> +            break;

>          }

>      }

>

> @@ -2359,7 +2416,7 @@ static void load_elf_image(const char *image_name, int image_fd,

>

>              if (eppnt->p_flags & PF_R) elf_prot =  PROT_READ;

>              if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;

> -            if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;

> +            if (eppnt->p_flags & PF_X) elf_prot |= prot_exec;

>

>              vaddr = load_bias + eppnt->p_vaddr;

>              vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);

> --

> 2.17.1


Otherwise
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>


thanks
-- PMM
Richard Henderson April 29, 2019, 8:18 p.m. UTC | #2
On 4/29/19 9:17 AM, Peter Maydell wrote:
>> +        struct elf_phdr *eppnt = phdr + i;

>> +

>> +        switch (eppnt->p_type) {

>> +        case PT_LOAD:

>> +            {

> 

> I think you have an extra layer of indent here that we usually

> don't do for switch statement cases.


No, that indent is exactly right for a compound statement
not associated with an if/while/whatnot.

> #define GNU0_MAGIC const_le32('G' | 'N' << 8 | 'U' << 16)

> 

> and then you can avoid the #ifdef HOST_WORDS_BIGENDIAN?


Sure.


r~
diff mbox series

Patch

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index c1a26021f8..12ee96e5d4 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -116,6 +116,10 @@  typedef abi_uint        target_gid_t;
 #endif
 typedef abi_int         target_pid_t;
 
+
+#define TARGET_NT_GNU_PROPERTY_TYPE_0  5
+
+
 #ifdef TARGET_I386
 
 #define ELF_PLATFORM get_elf_platform()
@@ -543,6 +547,10 @@  static const char *get_elf_platform(void)
 # define ELF_PLATFORM    "aarch64"
 #endif
 
+#define TARGET_GNU_PROPERTY_AARCH64_FEATURE_1_AND  0xc0000000
+#define TARGET_GNU_PROPERTY_AARCH64_FEATURE_1_BTI  (1u << 0)
+#define TARGET_GNU_PROPERTY_AARCH64_FEATURE_1_PAC  (1u << 1)
+
 static inline void init_thread(struct target_pt_regs *regs,
                                struct image_info *infop)
 {
@@ -2252,7 +2260,7 @@  static void load_elf_image(const char *image_name, int image_fd,
     struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
     struct elf_phdr *phdr;
     abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
-    int i, retval;
+    int i, retval, prot_exec = PROT_EXEC;
     const char *errmsg;
 
     /* First of all, some simple consistency checks */
@@ -2287,17 +2295,66 @@  static void load_elf_image(const char *image_name, int image_fd,
     loaddr = -1, hiaddr = 0;
     info->alignment = 0;
     for (i = 0; i < ehdr->e_phnum; ++i) {
-        if (phdr[i].p_type == PT_LOAD) {
-            abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset;
-            if (a < loaddr) {
-                loaddr = a;
+        struct elf_phdr *eppnt = phdr + i;
+
+        switch (eppnt->p_type) {
+        case PT_LOAD:
+            {
+                abi_ulong a = eppnt->p_vaddr - eppnt->p_offset;
+                if (a < loaddr) {
+                    loaddr = a;
+                }
+                a = eppnt->p_vaddr + eppnt->p_memsz;
+                if (a > hiaddr) {
+                    hiaddr = a;
+                }
+                ++info->nsegs;
+                info->alignment |= eppnt->p_align;
             }
-            a = phdr[i].p_vaddr + phdr[i].p_memsz;
-            if (a > hiaddr) {
-                hiaddr = a;
+            break;
+        case PT_NOTE:
+            {
+                uint32_t note[7];
+                uint32_t gnu0;
+
+                if (eppnt->p_filesz < sizeof(note)) {
+                    break;
+                }
+                if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
+                    memcpy(note, bprm_buf + eppnt->p_offset, sizeof(note));
+                } else {
+                    retval = pread(image_fd, note, sizeof(note),
+                                   eppnt->p_offset);
+                    if (retval != sizeof(note)) {
+                        goto exit_perror;
+                    }
+                }
+#ifdef BSWAP_NEEDED
+                for (i = 0; i < ARRAY_SIZE(note); ++i) {
+                    bswap32s(note + i);
+                }
+#endif
+#ifdef HOST_WORDS_BIGENDIAN
+                gnu0 = 'G' << 24 | 'N' << 16 | 'U' << 8;
+#else
+                gnu0 = 'G' | 'N' << 8 | 'U' << 16;
+#endif
+
+                if (note[0] != 4 ||     /* namesz */
+                    note[1] < 12 ||     /* descsz -- may include padding */
+                    note[2] != TARGET_NT_GNU_PROPERTY_TYPE_0 ||  /* type */
+                    note[3] != gnu0) {  /* name */
+                    break;
+                }
+#ifdef TARGET_AARCH64
+                if (note[4] == TARGET_GNU_PROPERTY_AARCH64_FEATURE_1_AND &&
+                    note[5] == 4 &&
+                    (note[6] & TARGET_GNU_PROPERTY_AARCH64_FEATURE_1_BTI)) {
+                    prot_exec |= TARGET_PROT_BTI;
+                }
+#endif
             }
-            ++info->nsegs;
-            info->alignment |= phdr[i].p_align;
+            break;
         }
     }
 
@@ -2359,7 +2416,7 @@  static void load_elf_image(const char *image_name, int image_fd,
 
             if (eppnt->p_flags & PF_R) elf_prot =  PROT_READ;
             if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
-            if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
+            if (eppnt->p_flags & PF_X) elf_prot |= prot_exec;
 
             vaddr = load_bias + eppnt->p_vaddr;
             vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);