Skip to contentSkip to conversation

xen/arm: Support properly __ro_after_init on Arm

PATCH In review Julien Grall 5 participants 9 messages Aug 16, 2022 – Aug 31, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/arm: Support properly __ro_after_init on Arm

From: Julien Grall <[email protected]>

__ro_after_init was introduced recently to prevent modifying some variables after init.

At the moment, on Arm, the variables will still be accessible because the region permission is not updated.

Address that, but moving the sections .data.ro_after_init out of .data and then mark the region read-only once we finish to boot.

---

This patch is targeting Xen 4.17. There are quite a few arm specific variables that could be switch to use __ro_after_init.

This is not addressed by the commit. We could consider to switch some of them for Xen 4.17. So the benefits for now is any common variables using __ro_after_init.

---
 xen/arch/arm/include/asm/setup.h |  2 ++
 xen/arch/arm/setup.c             | 14 ++++++++++++++
 xen/arch/arm/xen.lds.S           |  7 +++++++
 3 files changed, 23 insertions(+)
xen/arch/arm/include/asm/setup.h +2 -0
index 2bb01ecfa88f..5815ccf8c5cc 100644
@@ -137,6 +137,8 @@ u32 device_tree_get_u32(const void *fdt, int node,
 int map_range_to_domain(const struct dt_device_node *dev,
                         u64 addr, u64 len, void *data);
 
+extern const char __ro_after_init_start[], __ro_after_init_end[];
+
 #endif
 /*
  * Local variables:
xen/arch/arm/setup.c +14 -0
index 500307edc08d..5bde321b9d07 100644
@@ -75,10 +75,24 @@ domid_t __read_mostly max_init_domid;
 
 static __used void init_done(void)
 {
+    int rc;
+
     /* Must be done past setting system_state. */
     unregister_init_virtual_region();
 
     free_init_memory();
+
+    /*
+     * We have finished to boot. Mark the section .data.ro_after_init
+     * read-only.
+     */
+    rc = modify_xen_mappings((unsigned long)&__ro_after_init_start,
+                             (unsigned long)&__ro_after_init_end,
+                             PAGE_HYPERVISOR_RO);
+    if ( rc )
+        panic("Unable to mark the .data.ro_after_init section read-only (rc = %d)\n",
+              rc);
+
     startup_cpu_idle_loop();
 }
 
xen/arch/arm/xen.lds.S +7 -0
index 1e986e211f68..92c298405259 100644
@@ -83,6 +83,13 @@ SECTIONS
   _erodata = .;                /* End of read-only data */
 
   . = ALIGN(PAGE_SIZE);
+  .data.ro_after_init : {
+      __ro_after_init_start = .;
+      *(.data.ro_after_init)
+      . = ALIGN(PAGE_SIZE);
+      __ro_after_init_end = .;
+  } : text
+
   .data.read_mostly : {
        /* Exception table */
        __start___ex_table = .;
Signature · 1 line
2.37.1
Tested-by on v1 1/1
#1 lore raw

Hi Julien,

Quoted 8 lines

-----Original Message-----
Subject: [PATCH for-4.17] xen/arm: Support properly __ro_after_init on Arm

From: Julien Grall <[email protected]>

__ro_after_init was introduced recently to prevent modifying some variables after init.

Quoted 8 lines

At the moment, on Arm, the variables will still be accessible because the region permission is not updated.

Address that, but moving the sections .data.ro_after_init out of .data and then mark the region read-only once we finish to boot.

Kind regards,
Henry

Reviewed-by on v1 1/1
#2 lore raw
In reply to Julien Grall

Hi Julien

Quoted 15 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Julien Grall
Sent: Wednesday, August 17, 2022 3:00 AM
To: [email protected]
Cc: [email protected]; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: [PATCH for-4.17] xen/arm: Support properly __ro_after_init on Arm

From: Julien Grall <[email protected]>

__ro_after_init was introduced recently to prevent modifying some variables after init.

Quoted 8 lines

At the moment, on Arm, the variables will still be accessible because the region permission is not updated.

Address that, but moving the sections .data.ro_after_init out of .data and then mark the region read-only once we finish to boot.

Quoted 37 lines

---

This patch is targeting Xen 4.17. There are quite a few arm specific variables that could be switch to use __ro_after_init.

This is not addressed by the commit. We could consider to switch some of them for Xen 4.17. So the benefits for now is any common variables using __ro_after_init.

---
 xen/arch/arm/include/asm/setup.h |  2 ++
 xen/arch/arm/setup.c             | 14 ++++++++++++++
 xen/arch/arm/xen.lds.S           |  7 +++++++
 3 files changed, 23 insertions(+)
xen/arch/arm/include/asm/setup.h +2 -0
index 2bb01ecfa88f..5815ccf8c5cc 100644
@@ -137,6 +137,8 @@ u32 device_tree_get_u32(const void *fdt, int node,
 int map_range_to_domain(const struct dt_device_node *dev,
                         u64 addr, u64 len, void *data);
 
+extern const char __ro_after_init_start[], __ro_after_init_end[];
+
 #endif
 /*
  * Local variables:
xen/arch/arm/setup.c index renamed from xen/arch/arm/setup.c +0 -0

500307edc08d..5bde321b9d07 100644

xen/arch/arm/setup.c +1 -0
@@ -75,10 +75,24 @@ domid_t __read_mostly max_init_domid;
 
 static __used void init_done(void)
 {
+    int rc;

xen/arch/arm/setup.c domid_t __read_mostly max_init_domid; · line 88

Quoted 10 lines
xen/arch/arm/setup.c +6 -0
+
     /* Must be done past setting system_state. */
     unregister_init_virtual_region();
 
     free_init_memory();
+
+    /*
+     * We have finished to boot. Mark the section .data.ro_after_init
+     * read-only.
+     */

Nit: Maybe it is finish + doing, could be wrong, feel free to change or not~~

Quoted 13 lines
xen/arch/arm/setup.c +7 -0
+    rc = modify_xen_mappings((unsigned long)&__ro_after_init_start,
+                             (unsigned long)&__ro_after_init_end,
+                             PAGE_HYPERVISOR_RO);
+    if ( rc )
+        panic("Unable to mark the .data.ro_after_init section read-only (rc
 = %d)\n",
+              rc);
+
     startup_cpu_idle_loop();
 }

2.37.1

#3 lore raw
Quoted 1 line · Penny Zheng

Hi Julien

Hi Penny,

Quoted 18 lines
Quoted 18 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Julien Grall
Sent: Wednesday, August 17, 2022 3:00 AM
To: [email protected]
Cc: [email protected]; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Bertrand Marquis <[email protected]>; Volodymyr Babchuk <[email protected]>
Subject: [PATCH for-4.17] xen/arm: Support properly __ro_after_init on Arm

From: Julien Grall <[email protected]>

__ro_after_init was introduced recently to prevent modifying some variables after init.

At the moment, on Arm, the variables will still be accessible because the region permission is not updated.

Quoted 7 lines
Quoted 5 lines

Address that, but moving the sections .data.ro_after_init out of .data and then mark the region read-only once we finish to boot.

Thanks!

Quoted 42 lines
Quoted 42 lines

---

This patch is targeting Xen 4.17. There are quite a few arm specific variables that could be switch to use __ro_after_init.

This is not addressed by the commit. We could consider to switch some of them for Xen 4.17. So the benefits for now is any common variables using __ro_after_init.

---
  xen/arch/arm/include/asm/setup.h |  2 ++
  xen/arch/arm/setup.c             | 14 ++++++++++++++
  xen/arch/arm/xen.lds.S           |  7 +++++++
  3 files changed, 23 insertions(+)
xen/arch/arm/include/asm/setup.h +2 -0
index 2bb01ecfa88f..5815ccf8c5cc 100644
@@ -137,6 +137,8 @@ u32 device_tree_get_u32(const void *fdt, int node,
 int map_range_to_domain(const struct dt_device_node *dev,
                          u64 addr, u64 len, void *data);
 
+extern const char __ro_after_init_start[], __ro_after_init_end[];
+
  #endif
  /*
   * Local variables:
xen/arch/arm/setup.c index renamed from xen/arch/arm/setup.c +0 -0

500307edc08d..5bde321b9d07 100644

xen/arch/arm/setup.c +2 -0
@@ -75,10 +75,24 @@ domid_t __read_mostly max_init_domid;
 
  static __used void init_done(void)
  {
+    int rc;
+
      /* Must be done past setting system_state. */
      unregister_init_virtual_region();
 
Quoted 8 lines

xen/arch/arm/setup.c domid_t __read_mostly max_init_domid; · line 88

Quoted 6 lines
xen/arch/arm/setup.c +5 -0
      free_init_memory();
+
+    /*
+     * We have finished to boot. Mark the section .data.ro_after_init
+     * read-only.
+     */

Nit: Maybe it is finish + doing, could be wrong, feel free to change or not~~

I will update.

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw
In reply to Julien Grall
Quoted 13 lines · Julien Grall
xen/arch/arm/setup.c +3 -0
@@ -75,10 +75,24 @@ domid_t __read_mostly max_init_domid;
 
 static __used void init_done(void)
 {
+    int rc;
+
     /* Must be done past setting system_state. */
     unregister_init_virtual_region();
 
     free_init_memory();
+

xen/arch/arm/setup.c domid_t __read_mostly max_init_domid; · line 94

Quoted 10 lines
xen/arch/arm/setup.c +10 -0
+    /*
+     * We have finished to boot. Mark the section .data.ro_after_init
+     * read-only.
+     */
+    rc = modify_xen_mappings((unsigned long)&__ro_after_init_start,
+                             (unsigned long)&__ro_after_init_end,
+                             PAGE_HYPERVISOR_RO);
+    if ( rc )
+        panic("Unable to mark the .data.ro_after_init section read-only (rc = %d)\n",
+              rc);

Just wondering - is this really worth panic()ing?

xen/arch/arm/xen.lds.S SECTIONS · line 91

Quoted 12 lines
xen/arch/arm/xen.lds.S +6 -0
@@ -83,6 +83,13 @@ SECTIONS
   _erodata = .;                /* End of read-only data */
 
   . = ALIGN(PAGE_SIZE);
+  .data.ro_after_init : {
+      __ro_after_init_start = .;
+      *(.data.ro_after_init)
+      . = ALIGN(PAGE_SIZE);
+      __ro_after_init_end = .;
+  } : text

Again just wondering: Wouldn't it be an option to avoid the initial page size alignment (and the resulting padding) here, simply making .data.ro_after_init part of .rodata and do the earlier write-protection only up to (but excluding) the page containing __ro_after_init_start?

Jan

#5 lore raw

Hi Jan,

Quoted 12 lines · Jan Beulich
Quoted 11 lines · Julien Grall
xen/arch/arm/setup.c +2 -0
@@ -75,10 +75,24 @@ domid_t __read_mostly max_init_domid;
 
  static __used void init_done(void)
  {
+    int rc;
+
      /* Must be done past setting system_state. */
      unregister_init_virtual_region();
 
Quoted 14 lines

xen/arch/arm/setup.c domid_t __read_mostly max_init_domid; · line 94

Quoted 12 lines
xen/arch/arm/setup.c +11 -0
      free_init_memory();
+
+    /*
+     * We have finished to boot. Mark the section .data.ro_after_init
+     * read-only.
+     */
+    rc = modify_xen_mappings((unsigned long)&__ro_after_init_start,
+                             (unsigned long)&__ro_after_init_end,
+                             PAGE_HYPERVISOR_RO);
+    if ( rc )
+        panic("Unable to mark the .data.ro_after_init section read-only (rc = %d)\n",
+              rc);

Just wondering - is this really worth panic()ing?

The function should never fails and it sounds wrong to me to continue in the unlikely case it will fail.

Quoted 6 lines
Quoted 6 lines
xen/arch/arm/xen.lds.S +0 -0
@@ -83,6 +83,13 @@ SECTIONS
    _erodata = .;                /* End of read-only data */
 
Quoted 12 lines

xen/arch/arm/xen.lds.S SECTIONS · line 91

Quoted 7 lines
xen/arch/arm/xen.lds.S +6 -0
    . = ALIGN(PAGE_SIZE);
+  .data.ro_after_init : {
+      __ro_after_init_start = .;
+      *(.data.ro_after_init)
+      . = ALIGN(PAGE_SIZE);
+      __ro_after_init_end = .;
+  } : text

Again just wondering: Wouldn't it be an option to avoid the initial page size alignment (and the resulting padding) here, simply making .data.ro_after_init part of .rodata and do the earlier write-protection only up to (but excluding) the page containing __ro_after_init_start?

So both this question and the previous one will impair the security of Xen on Arm (even though the later is only at boot time).

This is not something I will support just because we are going to save < PAGE_SIZE. If we are concern of the size wasted, then there are other way to mitigate it (i.e. moving more variables to __ro_after_init).

Cheers,

Signature · 1 line
Julien Grall
#6 lore raw

Hi,

Quoted 29 lines · Julien Grall

Hi Jan,

Quoted 15 lines · Jan Beulich
Quoted 14 lines · Julien Grall
xen/arch/arm/setup.c +6 -0
@@ -75,10 +75,24 @@ domid_t __read_mostly max_init_domid;
   static __used void init_done(void)
 {
+    int rc;
+
     /* Must be done past setting system_state. */
     unregister_init_virtual_region();
       free_init_memory();
+
+    /*
+     * We have finished to boot. Mark the section .data.ro_after_init
+     * read-only.
Quoted 8 lines

xen/arch/arm/setup.c domid_t __read_mostly max_init_domid; · line 94

Quoted 7 lines
xen/arch/arm/setup.c +7 -0
+     */
+    rc = modify_xen_mappings((unsigned long)&__ro_after_init_start,
+                             (unsigned long)&__ro_after_init_end,
+                             PAGE_HYPERVISOR_RO);
+    if ( rc )
+        panic("Unable to mark the .data.ro_after_init section read-only (rc = %d)\n",
+              rc);

Just wondering - is this really worth panic()ing?

Quoted 1 line

The function should never fails and it sounds wrong to me to continue in the unlikely case it will fail.

I agree, we should not ignore and error here.

Quoted 17 lines
Quoted 16 lines

xen/arch/arm/xen.lds.S SECTIONS · line 90

Quoted 12 lines
xen/arch/arm/xen.lds.S +6 -0
@@ -83,6 +83,13 @@ SECTIONS
   _erodata = .;                /* End of read-only data */
     . = ALIGN(PAGE_SIZE);
+  .data.ro_after_init : {
+      __ro_after_init_start = .;
+      *(.data.ro_after_init)
+      . = ALIGN(PAGE_SIZE);
+      __ro_after_init_end = .;
+  } : text

Again just wondering: Wouldn't it be an option to avoid the initial page size alignment (and the resulting padding) here, simply making .data.ro_after_init part of .rodata and do the earlier write-protection only up to (but excluding) the page containing __ro_after_init_start?

Quoted 3 lines

So both this question and the previous one will impair the security of Xen on Arm (even though the later is only at boot time).

This is not something I will support just because we are going to save < PAGE_SIZE. If we are concern of the size wasted, then there are other way to mitigate it (i.e. moving more variables to __ro_after_init).

Agree with Julien here.

Cheers
Bertrand

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
Reviewed-by on v1 1/1
#7 lore raw
In reply to Julien Grall

Hi Julien,

Quoted 11 lines · Julien Grall

From: Julien Grall <[email protected]>

__ro_after_init was introduced recently to prevent modifying some variables after init.

At the moment, on Arm, the variables will still be accessible because the region permission is not updated.

Address that, but moving the sections .data.ro_after_init

Typo here s/but/by/ and remove ,

Quoted 2 lines

out of .data and then mark the region read-only once we finish to boot.

I would s/mark/map/

Quoted 2 lines

(Commit message can be fixed on commit)

Cheers
Bertrand

Quoted 78 lines

---

This patch is targeting Xen 4.17. There are quite a few arm specific variables that could be switch to use __ro_after_init.

This is not addressed by the commit. We could consider to switch some of them for Xen 4.17. So the benefits for now is any common variables using __ro_after_init.
---

xen/arch/arm/include/asm/setup.h |  2 ++
xen/arch/arm/setup.c             | 14 ++++++++++++++
xen/arch/arm/xen.lds.S           |  7 +++++++
3 files changed, 23 insertions(+)
xen/arch/arm/include/asm/setup.h +2 -0
index 2bb01ecfa88f..5815ccf8c5cc 100644
@@ -137,6 +137,8 @@ u32 device_tree_get_u32(const void *fdt, int node,
 int map_range_to_domain(const struct dt_device_node *dev,
                        u64 addr, u64 len, void *data);
 
+extern const char __ro_after_init_start[], __ro_after_init_end[];
+
 #endif
 /*
 * Local variables:
xen/arch/arm/setup.c +14 -0
index 500307edc08d..5bde321b9d07 100644
@@ -75,10 +75,24 @@ domid_t __read_mostly max_init_domid;
 
 static __used void init_done(void)
 {
+    int rc;
+
    /* Must be done past setting system_state. */
    unregister_init_virtual_region();
 
    free_init_memory();
+
+    /*
+     * We have finished to boot. Mark the section .data.ro_after_init
+     * read-only.
+     */
+    rc = modify_xen_mappings((unsigned long)&__ro_after_init_start,
+                             (unsigned long)&__ro_after_init_end,
+                             PAGE_HYPERVISOR_RO);
+    if ( rc )
+        panic("Unable to mark the .data.ro_after_init section read-only (rc = %d)\n",
+              rc);
+
    startup_cpu_idle_loop();
 }
 
xen/arch/arm/xen.lds.S +7 -0
index 1e986e211f68..92c298405259 100644
@@ -83,6 +83,13 @@ SECTIONS
  _erodata = .;                /* End of read-only data */
 
  . = ALIGN(PAGE_SIZE);
+  .data.ro_after_init : {
+      __ro_after_init_start = .;
+      *(.data.ro_after_init)
+      . = ALIGN(PAGE_SIZE);
+      __ro_after_init_end = .;
+  } : text
+
  .data.read_mostly : {
       /* Exception table */
       __start___ex_table = .;
Signature · 1 line
2.37.1
#8 lore raw
Quoted 1 line · Bertrand Marquis

Hi Julien,

Hi Bertrand,

Quoted 8 lines
Quoted 8 lines · Julien Grall

From: Julien Grall <[email protected]>

__ro_after_init was introduced recently to prevent modifying some variables after init.

Quoted 6 lines
Quoted 4 lines

At the moment, on Arm, the variables will still be accessible because the region permission is not updated.

Address that, but moving the sections .data.ro_after_init

Typo here s/but/by/ and remove ,

I updated it.

Quoted 5 lines
Quoted 3 lines

out of .data and then mark the region read-only once we finish to boot.

I would s/mark/map/

Ok.

Quoted 4 lines
Quoted 3 lines

Thanks!

Quoted 2 lines

(Commit message can be fixed on commit)

I have fixed the commit message, addressed the typo from Penny and committed the patch.

Cheers,

Signature · 1 line
Julien Grall