All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] livepatch: make symbol resolution more robust
@ 2024-04-19 10:02 Roger Pau Monne
  2024-04-19 10:02 ` [PATCH v2 1/2] xen: introduce header file with section related symbols Roger Pau Monne
  2024-04-19 10:02 ` [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections Roger Pau Monne
  0 siblings, 2 replies; 20+ messages in thread
From: Roger Pau Monne @ 2024-04-19 10:02 UTC (permalink / raw)
  To: xen-devel
  Cc: Roger Pau Monne, Stefano Stabellini, Julien Grall,
	Bertrand Marquis, Michal Orzel, Volodymyr Babchuk, Andrew Cooper,
	George Dunlap, Jan Beulich, Ross Lagerwall

Hello,

Previously a single patch, now with a pre-patch to place the init
section markers into a common file.

Thanks, Roger.

Roger Pau Monne (2):
  xen: introduce header file with section related symbols
  livepatch: refuse to resolve symbols that belong to init sections

 xen/arch/arm/mmu/setup.c   |  3 +--
 xen/arch/x86/setup.c       |  3 +--
 xen/common/livepatch_elf.c | 16 ++++++++++++++++
 xen/include/xen/sections.h | 17 +++++++++++++++++
 4 files changed, 35 insertions(+), 4 deletions(-)
 create mode 100644 xen/include/xen/sections.h

-- 
2.44.0



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

* [PATCH v2 1/2] xen: introduce header file with section related symbols
  2024-04-19 10:02 [PATCH v2 0/2] livepatch: make symbol resolution more robust Roger Pau Monne
@ 2024-04-19 10:02 ` Roger Pau Monne
  2024-04-19 10:08   ` Andrew Cooper
  2024-04-23 12:25   ` Jan Beulich
  2024-04-19 10:02 ` [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections Roger Pau Monne
  1 sibling, 2 replies; 20+ messages in thread
From: Roger Pau Monne @ 2024-04-19 10:02 UTC (permalink / raw)
  To: xen-devel
  Cc: Roger Pau Monne, Stefano Stabellini, Julien Grall,
	Bertrand Marquis, Michal Orzel, Volodymyr Babchuk, Andrew Cooper,
	George Dunlap, Jan Beulich

Start by declaring the beginning and end of the init section.

No functional change intended.

Requested-by: Andrew Cooper <andrew.cooper3@citrix.com>
Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
---
 xen/arch/arm/mmu/setup.c   |  3 +--
 xen/arch/x86/setup.c       |  3 +--
 xen/include/xen/sections.h | 17 +++++++++++++++++
 3 files changed, 19 insertions(+), 4 deletions(-)
 create mode 100644 xen/include/xen/sections.h

diff --git a/xen/arch/arm/mmu/setup.c b/xen/arch/arm/mmu/setup.c
index c0cb17ca2ecf..f4bb424c3c91 100644
--- a/xen/arch/arm/mmu/setup.c
+++ b/xen/arch/arm/mmu/setup.c
@@ -7,6 +7,7 @@
 
 #include <xen/init.h>
 #include <xen/libfdt/libfdt.h>
+#include <xen/sections.h>
 #include <xen/sizes.h>
 #include <xen/vmap.h>
 
@@ -62,8 +63,6 @@ vaddr_t directmap_virt_start __read_mostly;
 unsigned long directmap_base_pdx __read_mostly;
 #endif
 
-extern char __init_begin[], __init_end[];
-
 /* Checking VA memory layout alignment. */
 static void __init __maybe_unused build_assertions(void)
 {
diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c
index 86cd8b999774..dd4d1b2887ee 100644
--- a/xen/arch/x86/setup.c
+++ b/xen/arch/x86/setup.c
@@ -5,6 +5,7 @@
 #include <xen/param.h>
 #include <xen/sched.h>
 #include <xen/domain.h>
+#include <xen/sections.h>
 #include <xen/serial.h>
 #include <xen/softirq.h>
 #include <xen/acpi.h>
@@ -309,8 +310,6 @@ void __init discard_initial_images(void)
     initial_images = NULL;
 }
 
-extern unsigned char __init_begin[], __init_end[];
-
 static void __init init_idle_domain(void)
 {
     scheduler_init();
diff --git a/xen/include/xen/sections.h b/xen/include/xen/sections.h
new file mode 100644
index 000000000000..b6cb5604c285
--- /dev/null
+++ b/xen/include/xen/sections.h
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __XEN_SECTIONS_H__
+#define __XEN_SECTIONS_H__
+
+/* SAF-0-safe */
+extern char __init_begin[], __init_end[];
+
+#endif /* !__XEN_SECTIONS_H__ */
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
-- 
2.44.0



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

* [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections
  2024-04-19 10:02 [PATCH v2 0/2] livepatch: make symbol resolution more robust Roger Pau Monne
  2024-04-19 10:02 ` [PATCH v2 1/2] xen: introduce header file with section related symbols Roger Pau Monne
@ 2024-04-19 10:02 ` Roger Pau Monne
  2024-04-19 10:10   ` Andrew Cooper
  2024-04-19 10:15   ` Jan Beulich
  1 sibling, 2 replies; 20+ messages in thread
From: Roger Pau Monne @ 2024-04-19 10:02 UTC (permalink / raw)
  To: xen-devel; +Cc: Roger Pau Monne, Ross Lagerwall

Livepatch payloads containing symbols that belong to init sections can only
lead to page faults later on, as by the time the livepatch is loaded init
sections have already been freed.

Refuse to resolve such symbols and return an error instead.

Note such resolutions are only relevant for symbols that point to undefined
sections (SHN_UNDEF), as that implies the symbol is not in the current payload
and hence must either be a Xen or a different livepatch payload symbol.

Do not allow to resolve symbols that point to __init_begin, as that address is
also unmapped.  On the other hand, __init_end is not unmapped, and hence allow
resolutions against it.

Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
---
Changes since v1:
 - Fix off-by-one in range checking.
---
 xen/common/livepatch_elf.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/xen/common/livepatch_elf.c b/xen/common/livepatch_elf.c
index 45d73912a3cd..a67101eadc02 100644
--- a/xen/common/livepatch_elf.c
+++ b/xen/common/livepatch_elf.c
@@ -4,6 +4,7 @@
 
 #include <xen/errno.h>
 #include <xen/lib.h>
+#include <xen/sections.h>
 #include <xen/symbols.h>
 #include <xen/livepatch_elf.h>
 #include <xen/livepatch.h>
@@ -310,6 +311,21 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
                     break;
                 }
             }
+
+            /*
+             * Ensure not an init symbol.  Only applicable to Xen symbols, as
+             * livepatch payloads don't have init sections or equivalent.
+             */
+            else if ( st_value >= (uintptr_t)&__init_begin &&
+                      st_value < (uintptr_t)&__init_end )
+            {
+                printk(XENLOG_ERR LIVEPATCH
+                       "%s: symbol %s is in init section, not resolving\n",
+                       elf->name, elf->sym[i].name);
+                rc = -ENXIO;
+                break;
+            }
+
             dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Undefined symbol resolved: %s => %#"PRIxElfAddr"\n",
                     elf->name, elf->sym[i].name, st_value);
             break;
-- 
2.44.0



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

* Re: [PATCH v2 1/2] xen: introduce header file with section related symbols
  2024-04-19 10:02 ` [PATCH v2 1/2] xen: introduce header file with section related symbols Roger Pau Monne
@ 2024-04-19 10:08   ` Andrew Cooper
  2024-04-19 10:12     ` Jan Beulich
  2024-04-23 12:25   ` Jan Beulich
  1 sibling, 1 reply; 20+ messages in thread
From: Andrew Cooper @ 2024-04-19 10:08 UTC (permalink / raw)
  To: Roger Pau Monne, xen-devel
  Cc: Stefano Stabellini, Julien Grall, Bertrand Marquis, Michal Orzel,
	Volodymyr Babchuk, George Dunlap, Jan Beulich

On 19/04/2024 11:02 am, Roger Pau Monne wrote:
> Start by declaring the beginning and end of the init section.
>
> No functional change intended.
>
> Requested-by: Andrew Cooper <andrew.cooper3@citrix.com>
> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>

TYVM for doing this.  There's a lot of cleanup which can follow on for it.

Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>

although if anyone has a better name than sections.h then speak now.


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

* Re: [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections
  2024-04-19 10:02 ` [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections Roger Pau Monne
@ 2024-04-19 10:10   ` Andrew Cooper
  2024-04-19 10:48     ` Roger Pau Monné
  2024-04-19 10:15   ` Jan Beulich
  1 sibling, 1 reply; 20+ messages in thread
From: Andrew Cooper @ 2024-04-19 10:10 UTC (permalink / raw)
  To: Roger Pau Monne, xen-devel; +Cc: Ross Lagerwall

On 19/04/2024 11:02 am, Roger Pau Monne wrote:
> Livepatch payloads containing symbols that belong to init sections can only
> lead to page faults later on, as by the time the livepatch is loaded init
> sections have already been freed.
>
> Refuse to resolve such symbols and return an error instead.
>
> Note such resolutions are only relevant for symbols that point to undefined
> sections (SHN_UNDEF), as that implies the symbol is not in the current payload
> and hence must either be a Xen or a different livepatch payload symbol.
>
> Do not allow to resolve symbols that point to __init_begin, as that address is
> also unmapped.  On the other hand, __init_end is not unmapped, and hence allow
> resolutions against it.
>
> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>

Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>, although ...

> ---
> Changes since v1:
>  - Fix off-by-one in range checking.
> ---
>  xen/common/livepatch_elf.c | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
>
> diff --git a/xen/common/livepatch_elf.c b/xen/common/livepatch_elf.c
> index 45d73912a3cd..a67101eadc02 100644
> --- a/xen/common/livepatch_elf.c
> +++ b/xen/common/livepatch_elf.c
> @@ -4,6 +4,7 @@
>  
>  #include <xen/errno.h>
>  #include <xen/lib.h>
> +#include <xen/sections.h>
>  #include <xen/symbols.h>
>  #include <xen/livepatch_elf.h>
>  #include <xen/livepatch.h>
> @@ -310,6 +311,21 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
>                      break;
>                  }
>              }
> +
> +            /*
> +             * Ensure not an init symbol.  Only applicable to Xen symbols, as
> +             * livepatch payloads don't have init sections or equivalent.
> +             */
> +            else if ( st_value >= (uintptr_t)&__init_begin &&
> +                      st_value < (uintptr_t)&__init_end )

... I normally vertically the (casts) in cases like this for improved
legibility.  Happy to fold on commit.

~Andrew


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

* Re: [PATCH v2 1/2] xen: introduce header file with section related symbols
  2024-04-19 10:08   ` Andrew Cooper
@ 2024-04-19 10:12     ` Jan Beulich
  2024-04-19 10:16       ` Andrew Cooper
  0 siblings, 1 reply; 20+ messages in thread
From: Jan Beulich @ 2024-04-19 10:12 UTC (permalink / raw)
  To: Andrew Cooper
  Cc: Stefano Stabellini, Julien Grall, Bertrand Marquis, Michal Orzel,
	Volodymyr Babchuk, George Dunlap, Roger Pau Monne, xen-devel

On 19.04.2024 12:08, Andrew Cooper wrote:
> On 19/04/2024 11:02 am, Roger Pau Monne wrote:
>> Start by declaring the beginning and end of the init section.
>>
>> No functional change intended.
>>
>> Requested-by: Andrew Cooper <andrew.cooper3@citrix.com>
>> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> 
> TYVM for doing this.  There's a lot of cleanup which can follow on for it.
> 
> Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
> 
> although if anyone has a better name than sections.h then speak now.

For what is put there now, and for any other section bounds markers the
name is fine with me. I'm not presently convinced though we want to put
__read_mostly and friends there.

Jan


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

* Re: [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections
  2024-04-19 10:02 ` [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections Roger Pau Monne
  2024-04-19 10:10   ` Andrew Cooper
@ 2024-04-19 10:15   ` Jan Beulich
  2024-04-19 10:50     ` Roger Pau Monné
  1 sibling, 1 reply; 20+ messages in thread
From: Jan Beulich @ 2024-04-19 10:15 UTC (permalink / raw)
  To: Roger Pau Monne; +Cc: Ross Lagerwall, xen-devel

On 19.04.2024 12:02, Roger Pau Monne wrote:
> Livepatch payloads containing symbols that belong to init sections can only
> lead to page faults later on, as by the time the livepatch is loaded init
> sections have already been freed.
> 
> Refuse to resolve such symbols and return an error instead.
> 
> Note such resolutions are only relevant for symbols that point to undefined
> sections (SHN_UNDEF), as that implies the symbol is not in the current payload
> and hence must either be a Xen or a different livepatch payload symbol.
> 
> Do not allow to resolve symbols that point to __init_begin, as that address is
> also unmapped.  On the other hand, __init_end is not unmapped, and hence allow
> resolutions against it.
> 
> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> ---
> Changes since v1:
>  - Fix off-by-one in range checking.

Which means you addressed Andrew's comment while at the same time extending
the scope of ...

> @@ -310,6 +311,21 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
>                      break;
>                  }
>              }
> +
> +            /*
> +             * Ensure not an init symbol.  Only applicable to Xen symbols, as
> +             * livepatch payloads don't have init sections or equivalent.
> +             */
> +            else if ( st_value >= (uintptr_t)&__init_begin &&
> +                      st_value < (uintptr_t)&__init_end )
> +            {
> +                printk(XENLOG_ERR LIVEPATCH
> +                       "%s: symbol %s is in init section, not resolving\n",
> +                       elf->name, elf->sym[i].name);

... what I raised concern about (and I had not seen any verbal reply to that,
I don't think).

Jan


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

* Re: [PATCH v2 1/2] xen: introduce header file with section related symbols
  2024-04-19 10:12     ` Jan Beulich
@ 2024-04-19 10:16       ` Andrew Cooper
  2024-04-19 14:32         ` Jan Beulich
  0 siblings, 1 reply; 20+ messages in thread
From: Andrew Cooper @ 2024-04-19 10:16 UTC (permalink / raw)
  To: Jan Beulich
  Cc: Stefano Stabellini, Julien Grall, Bertrand Marquis, Michal Orzel,
	Volodymyr Babchuk, George Dunlap, Roger Pau Monne, xen-devel

On 19/04/2024 11:12 am, Jan Beulich wrote:
> On 19.04.2024 12:08, Andrew Cooper wrote:
>> On 19/04/2024 11:02 am, Roger Pau Monne wrote:
>>> Start by declaring the beginning and end of the init section.
>>>
>>> No functional change intended.
>>>
>>> Requested-by: Andrew Cooper <andrew.cooper3@citrix.com>
>>> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
>> TYVM for doing this.  There's a lot of cleanup which can follow on for it.
>>
>> Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
>>
>> although if anyone has a better name than sections.h then speak now.
> For what is put there now, and for any other section bounds markers the
> name is fine with me. I'm not presently convinced though we want to put
> __read_mostly and friends there.

Well that's exactly what I intend to clean up into it, because it's far
better in sections.h than (duplicated per arch) cache.h

(Also I intend to strip down kernel.h for the other major sections too.)

~Andrew


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

* Re: [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections
  2024-04-19 10:10   ` Andrew Cooper
@ 2024-04-19 10:48     ` Roger Pau Monné
  0 siblings, 0 replies; 20+ messages in thread
From: Roger Pau Monné @ 2024-04-19 10:48 UTC (permalink / raw)
  To: Andrew Cooper; +Cc: xen-devel, Ross Lagerwall

On Fri, Apr 19, 2024 at 11:10:33AM +0100, Andrew Cooper wrote:
> On 19/04/2024 11:02 am, Roger Pau Monne wrote:
> > Livepatch payloads containing symbols that belong to init sections can only
> > lead to page faults later on, as by the time the livepatch is loaded init
> > sections have already been freed.
> >
> > Refuse to resolve such symbols and return an error instead.
> >
> > Note such resolutions are only relevant for symbols that point to undefined
> > sections (SHN_UNDEF), as that implies the symbol is not in the current payload
> > and hence must either be a Xen or a different livepatch payload symbol.
> >
> > Do not allow to resolve symbols that point to __init_begin, as that address is
> > also unmapped.  On the other hand, __init_end is not unmapped, and hence allow
> > resolutions against it.
> >
> > Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> 
> Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>, although ...
> 
> > ---
> > Changes since v1:
> >  - Fix off-by-one in range checking.
> > ---
> >  xen/common/livepatch_elf.c | 16 ++++++++++++++++
> >  1 file changed, 16 insertions(+)
> >
> > diff --git a/xen/common/livepatch_elf.c b/xen/common/livepatch_elf.c
> > index 45d73912a3cd..a67101eadc02 100644
> > --- a/xen/common/livepatch_elf.c
> > +++ b/xen/common/livepatch_elf.c
> > @@ -4,6 +4,7 @@
> >  
> >  #include <xen/errno.h>
> >  #include <xen/lib.h>
> > +#include <xen/sections.h>
> >  #include <xen/symbols.h>
> >  #include <xen/livepatch_elf.h>
> >  #include <xen/livepatch.h>
> > @@ -310,6 +311,21 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
> >                      break;
> >                  }
> >              }
> > +
> > +            /*
> > +             * Ensure not an init symbol.  Only applicable to Xen symbols, as
> > +             * livepatch payloads don't have init sections or equivalent.
> > +             */
> > +            else if ( st_value >= (uintptr_t)&__init_begin &&
> > +                      st_value < (uintptr_t)&__init_end )
> 
> ... I normally vertically the (casts) in cases like this for improved
> legibility.  Happy to fold on commit.

Did this, but reverted afterwards because I wasn't sure whether it
would go against the coding style.  Aligning would be my preference
also.

Thanks, Roger.


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

* Re: [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections
  2024-04-19 10:15   ` Jan Beulich
@ 2024-04-19 10:50     ` Roger Pau Monné
  2024-04-19 14:34       ` Jan Beulich
  0 siblings, 1 reply; 20+ messages in thread
From: Roger Pau Monné @ 2024-04-19 10:50 UTC (permalink / raw)
  To: Jan Beulich; +Cc: Ross Lagerwall, xen-devel

On Fri, Apr 19, 2024 at 12:15:19PM +0200, Jan Beulich wrote:
> On 19.04.2024 12:02, Roger Pau Monne wrote:
> > Livepatch payloads containing symbols that belong to init sections can only
> > lead to page faults later on, as by the time the livepatch is loaded init
> > sections have already been freed.
> > 
> > Refuse to resolve such symbols and return an error instead.
> > 
> > Note such resolutions are only relevant for symbols that point to undefined
> > sections (SHN_UNDEF), as that implies the symbol is not in the current payload
> > and hence must either be a Xen or a different livepatch payload symbol.
> > 
> > Do not allow to resolve symbols that point to __init_begin, as that address is
> > also unmapped.  On the other hand, __init_end is not unmapped, and hence allow
> > resolutions against it.
> > 
> > Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> > ---
> > Changes since v1:
> >  - Fix off-by-one in range checking.
> 
> Which means you addressed Andrew's comment while at the same time extending
> the scope of ...
> 
> > @@ -310,6 +311,21 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
> >                      break;
> >                  }
> >              }
> > +
> > +            /*
> > +             * Ensure not an init symbol.  Only applicable to Xen symbols, as
> > +             * livepatch payloads don't have init sections or equivalent.
> > +             */
> > +            else if ( st_value >= (uintptr_t)&__init_begin &&
> > +                      st_value < (uintptr_t)&__init_end )
> > +            {
> > +                printk(XENLOG_ERR LIVEPATCH
> > +                       "%s: symbol %s is in init section, not resolving\n",
> > +                       elf->name, elf->sym[i].name);
> 
> ... what I raised concern about (and I had not seen any verbal reply to that,
> I don't think).

I've extended the commit message to explicitly mention the handling of
bounds for __init_{begin,end} checks.  Let me know if you are not fine
with it (or maybe you expected something else?).

Thanks, Roger.


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

* Re: [PATCH v2 1/2] xen: introduce header file with section related symbols
  2024-04-19 10:16       ` Andrew Cooper
@ 2024-04-19 14:32         ` Jan Beulich
  0 siblings, 0 replies; 20+ messages in thread
From: Jan Beulich @ 2024-04-19 14:32 UTC (permalink / raw)
  To: Andrew Cooper
  Cc: Stefano Stabellini, Julien Grall, Bertrand Marquis, Michal Orzel,
	Volodymyr Babchuk, George Dunlap, Roger Pau Monne, xen-devel

On 19.04.2024 12:16, Andrew Cooper wrote:
> On 19/04/2024 11:12 am, Jan Beulich wrote:
>> On 19.04.2024 12:08, Andrew Cooper wrote:
>>> On 19/04/2024 11:02 am, Roger Pau Monne wrote:
>>>> Start by declaring the beginning and end of the init section.
>>>>
>>>> No functional change intended.
>>>>
>>>> Requested-by: Andrew Cooper <andrew.cooper3@citrix.com>
>>>> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
>>> TYVM for doing this.  There's a lot of cleanup which can follow on for it.
>>>
>>> Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
>>>
>>> although if anyone has a better name than sections.h then speak now.
>> For what is put there now, and for any other section bounds markers the
>> name is fine with me. I'm not presently convinced though we want to put
>> __read_mostly and friends there.
> 
> Well that's exactly what I intend to clean up into it, because it's far
> better in sections.h than (duplicated per arch) cache.h

The duplication per arch has had a patch pending for a long time, which
you've been blocking. What you're suggesting is not only a very different
sections-related use of the header (which is probably okay), but also
requires touching a fair part of the code base (each and every .c file
using __read_mostly). That's what I'd like to avoid. Yet what I could
live with if it's not me needing to perform this tedious work (hence why
I didn't want to adjust my patch along these lines).

> (Also I intend to strip down kernel.h for the other major sections too.)

I certainly have no issue with this; I was in fact already expecting that
to happen as a follow-on step.

Jan


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

* Re: [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections
  2024-04-19 10:50     ` Roger Pau Monné
@ 2024-04-19 14:34       ` Jan Beulich
  2024-04-22  7:54         ` Roger Pau Monné
  0 siblings, 1 reply; 20+ messages in thread
From: Jan Beulich @ 2024-04-19 14:34 UTC (permalink / raw)
  To: Roger Pau Monné; +Cc: Ross Lagerwall, xen-devel

On 19.04.2024 12:50, Roger Pau Monné wrote:
> On Fri, Apr 19, 2024 at 12:15:19PM +0200, Jan Beulich wrote:
>> On 19.04.2024 12:02, Roger Pau Monne wrote:
>>> Livepatch payloads containing symbols that belong to init sections can only
>>> lead to page faults later on, as by the time the livepatch is loaded init
>>> sections have already been freed.
>>>
>>> Refuse to resolve such symbols and return an error instead.
>>>
>>> Note such resolutions are only relevant for symbols that point to undefined
>>> sections (SHN_UNDEF), as that implies the symbol is not in the current payload
>>> and hence must either be a Xen or a different livepatch payload symbol.
>>>
>>> Do not allow to resolve symbols that point to __init_begin, as that address is
>>> also unmapped.  On the other hand, __init_end is not unmapped, and hence allow
>>> resolutions against it.
>>>
>>> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
>>> ---
>>> Changes since v1:
>>>  - Fix off-by-one in range checking.
>>
>> Which means you addressed Andrew's comment while at the same time extending
>> the scope of ...
>>
>>> @@ -310,6 +311,21 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
>>>                      break;
>>>                  }
>>>              }
>>> +
>>> +            /*
>>> +             * Ensure not an init symbol.  Only applicable to Xen symbols, as
>>> +             * livepatch payloads don't have init sections or equivalent.
>>> +             */
>>> +            else if ( st_value >= (uintptr_t)&__init_begin &&
>>> +                      st_value < (uintptr_t)&__init_end )
>>> +            {
>>> +                printk(XENLOG_ERR LIVEPATCH
>>> +                       "%s: symbol %s is in init section, not resolving\n",
>>> +                       elf->name, elf->sym[i].name);
>>
>> ... what I raised concern about (and I had not seen any verbal reply to that,
>> I don't think).
> 
> I've extended the commit message to explicitly mention the handling of
> bounds for __init_{begin,end} checks.  Let me know if you are not fine
> with it (or maybe you expected something else?).

Well, you mention the two symbols you care about, but you don't mention
at all that these two may alias other symbols which might be legal to
reference from a livepatch.

Jan


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

* Re: [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections
  2024-04-19 14:34       ` Jan Beulich
@ 2024-04-22  7:54         ` Roger Pau Monné
  2024-04-22  8:25           ` Jan Beulich
  0 siblings, 1 reply; 20+ messages in thread
From: Roger Pau Monné @ 2024-04-22  7:54 UTC (permalink / raw)
  To: Jan Beulich; +Cc: Ross Lagerwall, xen-devel

On Fri, Apr 19, 2024 at 04:34:41PM +0200, Jan Beulich wrote:
> On 19.04.2024 12:50, Roger Pau Monné wrote:
> > On Fri, Apr 19, 2024 at 12:15:19PM +0200, Jan Beulich wrote:
> >> On 19.04.2024 12:02, Roger Pau Monne wrote:
> >>> Livepatch payloads containing symbols that belong to init sections can only
> >>> lead to page faults later on, as by the time the livepatch is loaded init
> >>> sections have already been freed.
> >>>
> >>> Refuse to resolve such symbols and return an error instead.
> >>>
> >>> Note such resolutions are only relevant for symbols that point to undefined
> >>> sections (SHN_UNDEF), as that implies the symbol is not in the current payload
> >>> and hence must either be a Xen or a different livepatch payload symbol.
> >>>
> >>> Do not allow to resolve symbols that point to __init_begin, as that address is
> >>> also unmapped.  On the other hand, __init_end is not unmapped, and hence allow
> >>> resolutions against it.
> >>>
> >>> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> >>> ---
> >>> Changes since v1:
> >>>  - Fix off-by-one in range checking.
> >>
> >> Which means you addressed Andrew's comment while at the same time extending
> >> the scope of ...
> >>
> >>> @@ -310,6 +311,21 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
> >>>                      break;
> >>>                  }
> >>>              }
> >>> +
> >>> +            /*
> >>> +             * Ensure not an init symbol.  Only applicable to Xen symbols, as
> >>> +             * livepatch payloads don't have init sections or equivalent.
> >>> +             */
> >>> +            else if ( st_value >= (uintptr_t)&__init_begin &&
> >>> +                      st_value < (uintptr_t)&__init_end )
> >>> +            {
> >>> +                printk(XENLOG_ERR LIVEPATCH
> >>> +                       "%s: symbol %s is in init section, not resolving\n",
> >>> +                       elf->name, elf->sym[i].name);
> >>
> >> ... what I raised concern about (and I had not seen any verbal reply to that,
> >> I don't think).
> > 
> > I've extended the commit message to explicitly mention the handling of
> > bounds for __init_{begin,end} checks.  Let me know if you are not fine
> > with it (or maybe you expected something else?).
> 
> Well, you mention the two symbols you care about, but you don't mention
> at all that these two may alias other symbols which might be legal to
> reference from a livepatch.

I'm having a hard time understanding why a livepatch would want to
reference those, or any symbol in the .init sections for that matter.
__init_{begin,end} are exclusively used to unmap the init region after
boot.  What's the point in livepatch referencing data that's no
longer there?  The only application I would think of is to calculate
some kind of offsets, but that would better be done using other
symbols instead.

Please bear with me, it would be easier for me to understand if you
could provide a concrete example.

Thanks, Roger.


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

* Re: [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections
  2024-04-22  7:54         ` Roger Pau Monné
@ 2024-04-22  8:25           ` Jan Beulich
  2024-04-22 10:49             ` Roger Pau Monné
  0 siblings, 1 reply; 20+ messages in thread
From: Jan Beulich @ 2024-04-22  8:25 UTC (permalink / raw)
  To: Roger Pau Monné; +Cc: Ross Lagerwall, xen-devel

On 22.04.2024 09:54, Roger Pau Monné wrote:
> On Fri, Apr 19, 2024 at 04:34:41PM +0200, Jan Beulich wrote:
>> On 19.04.2024 12:50, Roger Pau Monné wrote:
>>> On Fri, Apr 19, 2024 at 12:15:19PM +0200, Jan Beulich wrote:
>>>> On 19.04.2024 12:02, Roger Pau Monne wrote:
>>>>> Livepatch payloads containing symbols that belong to init sections can only
>>>>> lead to page faults later on, as by the time the livepatch is loaded init
>>>>> sections have already been freed.
>>>>>
>>>>> Refuse to resolve such symbols and return an error instead.
>>>>>
>>>>> Note such resolutions are only relevant for symbols that point to undefined
>>>>> sections (SHN_UNDEF), as that implies the symbol is not in the current payload
>>>>> and hence must either be a Xen or a different livepatch payload symbol.
>>>>>
>>>>> Do not allow to resolve symbols that point to __init_begin, as that address is
>>>>> also unmapped.  On the other hand, __init_end is not unmapped, and hence allow
>>>>> resolutions against it.
>>>>>
>>>>> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
>>>>> ---
>>>>> Changes since v1:
>>>>>  - Fix off-by-one in range checking.
>>>>
>>>> Which means you addressed Andrew's comment while at the same time extending
>>>> the scope of ...
>>>>
>>>>> @@ -310,6 +311,21 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
>>>>>                      break;
>>>>>                  }
>>>>>              }
>>>>> +
>>>>> +            /*
>>>>> +             * Ensure not an init symbol.  Only applicable to Xen symbols, as
>>>>> +             * livepatch payloads don't have init sections or equivalent.
>>>>> +             */
>>>>> +            else if ( st_value >= (uintptr_t)&__init_begin &&
>>>>> +                      st_value < (uintptr_t)&__init_end )
>>>>> +            {
>>>>> +                printk(XENLOG_ERR LIVEPATCH
>>>>> +                       "%s: symbol %s is in init section, not resolving\n",
>>>>> +                       elf->name, elf->sym[i].name);
>>>>
>>>> ... what I raised concern about (and I had not seen any verbal reply to that,
>>>> I don't think).
>>>
>>> I've extended the commit message to explicitly mention the handling of
>>> bounds for __init_{begin,end} checks.  Let me know if you are not fine
>>> with it (or maybe you expected something else?).
>>
>> Well, you mention the two symbols you care about, but you don't mention
>> at all that these two may alias other symbols which might be legal to
>> reference from a livepatch.
> 
> I'm having a hard time understanding why a livepatch would want to
> reference those, or any symbol in the .init sections for that matter.
> __init_{begin,end} are exclusively used to unmap the init region after
> boot.  What's the point in livepatch referencing data that's no
> longer there?  The only application I would think of is to calculate
> some kind of offsets, but that would better be done using other
> symbols instead.
> 
> Please bear with me, it would be easier for me to understand if you
> could provide a concrete example.

The issue is that you do comparison by address, not by name. Let's assume
that .rodata ends exactly where .init.* starts. Then &__init_begin ==
&_erodata == &__2M_rodata_end. Access to the latter two symbols wants to
be permitted; only __init_begin and whatever ends up being the very first
symbol in .init.* ought to not be referenced.

Worse (but contrived) would be cases of "constructed" symbols derived from
ones ahead of __init_begin, with an offset large enough to actually point
into .init.*. Such are _still_ valid to be used in calculations, as long
as no deref occurs for anything at or past __init_begin.

Jan


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

* Re: [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections
  2024-04-22  8:25           ` Jan Beulich
@ 2024-04-22 10:49             ` Roger Pau Monné
  2024-04-22 10:57               ` Jan Beulich
  0 siblings, 1 reply; 20+ messages in thread
From: Roger Pau Monné @ 2024-04-22 10:49 UTC (permalink / raw)
  To: Jan Beulich; +Cc: Ross Lagerwall, xen-devel

On Mon, Apr 22, 2024 at 10:25:40AM +0200, Jan Beulich wrote:
> On 22.04.2024 09:54, Roger Pau Monné wrote:
> > On Fri, Apr 19, 2024 at 04:34:41PM +0200, Jan Beulich wrote:
> >> On 19.04.2024 12:50, Roger Pau Monné wrote:
> >>> On Fri, Apr 19, 2024 at 12:15:19PM +0200, Jan Beulich wrote:
> >>>> On 19.04.2024 12:02, Roger Pau Monne wrote:
> >>>>> Livepatch payloads containing symbols that belong to init sections can only
> >>>>> lead to page faults later on, as by the time the livepatch is loaded init
> >>>>> sections have already been freed.
> >>>>>
> >>>>> Refuse to resolve such symbols and return an error instead.
> >>>>>
> >>>>> Note such resolutions are only relevant for symbols that point to undefined
> >>>>> sections (SHN_UNDEF), as that implies the symbol is not in the current payload
> >>>>> and hence must either be a Xen or a different livepatch payload symbol.
> >>>>>
> >>>>> Do not allow to resolve symbols that point to __init_begin, as that address is
> >>>>> also unmapped.  On the other hand, __init_end is not unmapped, and hence allow
> >>>>> resolutions against it.
> >>>>>
> >>>>> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> >>>>> ---
> >>>>> Changes since v1:
> >>>>>  - Fix off-by-one in range checking.
> >>>>
> >>>> Which means you addressed Andrew's comment while at the same time extending
> >>>> the scope of ...
> >>>>
> >>>>> @@ -310,6 +311,21 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
> >>>>>                      break;
> >>>>>                  }
> >>>>>              }
> >>>>> +
> >>>>> +            /*
> >>>>> +             * Ensure not an init symbol.  Only applicable to Xen symbols, as
> >>>>> +             * livepatch payloads don't have init sections or equivalent.
> >>>>> +             */
> >>>>> +            else if ( st_value >= (uintptr_t)&__init_begin &&
> >>>>> +                      st_value < (uintptr_t)&__init_end )
> >>>>> +            {
> >>>>> +                printk(XENLOG_ERR LIVEPATCH
> >>>>> +                       "%s: symbol %s is in init section, not resolving\n",
> >>>>> +                       elf->name, elf->sym[i].name);
> >>>>
> >>>> ... what I raised concern about (and I had not seen any verbal reply to that,
> >>>> I don't think).
> >>>
> >>> I've extended the commit message to explicitly mention the handling of
> >>> bounds for __init_{begin,end} checks.  Let me know if you are not fine
> >>> with it (or maybe you expected something else?).
> >>
> >> Well, you mention the two symbols you care about, but you don't mention
> >> at all that these two may alias other symbols which might be legal to
> >> reference from a livepatch.
> > 
> > I'm having a hard time understanding why a livepatch would want to
> > reference those, or any symbol in the .init sections for that matter.
> > __init_{begin,end} are exclusively used to unmap the init region after
> > boot.  What's the point in livepatch referencing data that's no
> > longer there?  The only application I would think of is to calculate
> > some kind of offsets, but that would better be done using other
> > symbols instead.
> > 
> > Please bear with me, it would be easier for me to understand if you
> > could provide a concrete example.
> 
> The issue is that you do comparison by address, not by name. Let's assume
> that .rodata ends exactly where .init.* starts. Then &__init_begin ==
> &_erodata == &__2M_rodata_end. Access to the latter two symbols wants to
> be permitted; only __init_begin and whatever ends up being the very first
> symbol in .init.* ought to not be referenced.

Hm, I guess I could add comparison by name additionally, but it looks
fragile to me.

So when st_value == __init_begin check if the name matches
"__init_begin" or "__2M_init_start" or "_sinittext", and fail
resolution, otherwise allow it?

> Worse (but contrived) would be cases of "constructed" symbols derived from
> ones ahead of __init_begin, with an offset large enough to actually point
> into .init.*. Such are _still_ valid to be used in calculations, as long
> as no deref occurs for anything at or past __init_begin.

But one would have to craft such a symbol specifically, at which point
I consider this out of the scope of what this patch is attempting to
protect against.  The aim is not to prevent malicious livepatches, and
there are far easier ways to trigger a page-fault from a livepatch.

Thanks, Roger.


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

* Re: [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections
  2024-04-22 10:49             ` Roger Pau Monné
@ 2024-04-22 10:57               ` Jan Beulich
  2024-04-22 11:34                 ` Roger Pau Monné
  0 siblings, 1 reply; 20+ messages in thread
From: Jan Beulich @ 2024-04-22 10:57 UTC (permalink / raw)
  To: Roger Pau Monné; +Cc: Ross Lagerwall, xen-devel

On 22.04.2024 12:49, Roger Pau Monné wrote:
> On Mon, Apr 22, 2024 at 10:25:40AM +0200, Jan Beulich wrote:
>> On 22.04.2024 09:54, Roger Pau Monné wrote:
>>> On Fri, Apr 19, 2024 at 04:34:41PM +0200, Jan Beulich wrote:
>>>> On 19.04.2024 12:50, Roger Pau Monné wrote:
>>>>> On Fri, Apr 19, 2024 at 12:15:19PM +0200, Jan Beulich wrote:
>>>>>> On 19.04.2024 12:02, Roger Pau Monne wrote:
>>>>>>> Livepatch payloads containing symbols that belong to init sections can only
>>>>>>> lead to page faults later on, as by the time the livepatch is loaded init
>>>>>>> sections have already been freed.
>>>>>>>
>>>>>>> Refuse to resolve such symbols and return an error instead.
>>>>>>>
>>>>>>> Note such resolutions are only relevant for symbols that point to undefined
>>>>>>> sections (SHN_UNDEF), as that implies the symbol is not in the current payload
>>>>>>> and hence must either be a Xen or a different livepatch payload symbol.
>>>>>>>
>>>>>>> Do not allow to resolve symbols that point to __init_begin, as that address is
>>>>>>> also unmapped.  On the other hand, __init_end is not unmapped, and hence allow
>>>>>>> resolutions against it.
>>>>>>>
>>>>>>> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
>>>>>>> ---
>>>>>>> Changes since v1:
>>>>>>>  - Fix off-by-one in range checking.
>>>>>>
>>>>>> Which means you addressed Andrew's comment while at the same time extending
>>>>>> the scope of ...
>>>>>>
>>>>>>> @@ -310,6 +311,21 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
>>>>>>>                      break;
>>>>>>>                  }
>>>>>>>              }
>>>>>>> +
>>>>>>> +            /*
>>>>>>> +             * Ensure not an init symbol.  Only applicable to Xen symbols, as
>>>>>>> +             * livepatch payloads don't have init sections or equivalent.
>>>>>>> +             */
>>>>>>> +            else if ( st_value >= (uintptr_t)&__init_begin &&
>>>>>>> +                      st_value < (uintptr_t)&__init_end )
>>>>>>> +            {
>>>>>>> +                printk(XENLOG_ERR LIVEPATCH
>>>>>>> +                       "%s: symbol %s is in init section, not resolving\n",
>>>>>>> +                       elf->name, elf->sym[i].name);
>>>>>>
>>>>>> ... what I raised concern about (and I had not seen any verbal reply to that,
>>>>>> I don't think).
>>>>>
>>>>> I've extended the commit message to explicitly mention the handling of
>>>>> bounds for __init_{begin,end} checks.  Let me know if you are not fine
>>>>> with it (or maybe you expected something else?).
>>>>
>>>> Well, you mention the two symbols you care about, but you don't mention
>>>> at all that these two may alias other symbols which might be legal to
>>>> reference from a livepatch.
>>>
>>> I'm having a hard time understanding why a livepatch would want to
>>> reference those, or any symbol in the .init sections for that matter.
>>> __init_{begin,end} are exclusively used to unmap the init region after
>>> boot.  What's the point in livepatch referencing data that's no
>>> longer there?  The only application I would think of is to calculate
>>> some kind of offsets, but that would better be done using other
>>> symbols instead.
>>>
>>> Please bear with me, it would be easier for me to understand if you
>>> could provide a concrete example.
>>
>> The issue is that you do comparison by address, not by name. Let's assume
>> that .rodata ends exactly where .init.* starts. Then &__init_begin ==
>> &_erodata == &__2M_rodata_end. Access to the latter two symbols wants to
>> be permitted; only __init_begin and whatever ends up being the very first
>> symbol in .init.* ought to not be referenced.
> 
> Hm, I guess I could add comparison by name additionally, but it looks
> fragile to me.

It looks fragile, yes. Thing is that you're trying to deal with this when
crucial information was lost already. Or wait - isn't the section number
still available in ->st_shndx?

> So when st_value == __init_begin check if the name matches
> "__init_begin" or "__2M_init_start" or "_sinittext", and fail
> resolution, otherwise allow it?

Kind of, but that's not enough. For one, as said, the first symbol in
the first .init.* section would also have this same address, and would
also want rejecting. And then the same would be needed for __init_end.

>> Worse (but contrived) would be cases of "constructed" symbols derived from
>> ones ahead of __init_begin, with an offset large enough to actually point
>> into .init.*. Such are _still_ valid to be used in calculations, as long
>> as no deref occurs for anything at or past __init_begin.
> 
> But one would have to craft such a symbol specifically, at which point
> I consider this out of the scope of what this patch is attempting to
> protect against.  The aim is not to prevent malicious livepatches, and
> there are far easier ways to trigger a page-fault from a livepatch.

I understand the latter is the case, but I'm afraid I then don't see what
the goal of this change is.

Jan


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

* Re: [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections
  2024-04-22 10:57               ` Jan Beulich
@ 2024-04-22 11:34                 ` Roger Pau Monné
  2024-04-22 11:51                   ` Jan Beulich
  0 siblings, 1 reply; 20+ messages in thread
From: Roger Pau Monné @ 2024-04-22 11:34 UTC (permalink / raw)
  To: Jan Beulich; +Cc: Ross Lagerwall, xen-devel

On Mon, Apr 22, 2024 at 12:57:55PM +0200, Jan Beulich wrote:
> On 22.04.2024 12:49, Roger Pau Monné wrote:
> > On Mon, Apr 22, 2024 at 10:25:40AM +0200, Jan Beulich wrote:
> >> On 22.04.2024 09:54, Roger Pau Monné wrote:
> >>> On Fri, Apr 19, 2024 at 04:34:41PM +0200, Jan Beulich wrote:
> >>>> On 19.04.2024 12:50, Roger Pau Monné wrote:
> >>>>> On Fri, Apr 19, 2024 at 12:15:19PM +0200, Jan Beulich wrote:
> >>>>>> On 19.04.2024 12:02, Roger Pau Monne wrote:
> >>>>>>> Livepatch payloads containing symbols that belong to init sections can only
> >>>>>>> lead to page faults later on, as by the time the livepatch is loaded init
> >>>>>>> sections have already been freed.
> >>>>>>>
> >>>>>>> Refuse to resolve such symbols and return an error instead.
> >>>>>>>
> >>>>>>> Note such resolutions are only relevant for symbols that point to undefined
> >>>>>>> sections (SHN_UNDEF), as that implies the symbol is not in the current payload
> >>>>>>> and hence must either be a Xen or a different livepatch payload symbol.
> >>>>>>>
> >>>>>>> Do not allow to resolve symbols that point to __init_begin, as that address is
> >>>>>>> also unmapped.  On the other hand, __init_end is not unmapped, and hence allow
> >>>>>>> resolutions against it.
> >>>>>>>
> >>>>>>> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> >>>>>>> ---
> >>>>>>> Changes since v1:
> >>>>>>>  - Fix off-by-one in range checking.
> >>>>>>
> >>>>>> Which means you addressed Andrew's comment while at the same time extending
> >>>>>> the scope of ...
> >>>>>>
> >>>>>>> @@ -310,6 +311,21 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
> >>>>>>>                      break;
> >>>>>>>                  }
> >>>>>>>              }
> >>>>>>> +
> >>>>>>> +            /*
> >>>>>>> +             * Ensure not an init symbol.  Only applicable to Xen symbols, as
> >>>>>>> +             * livepatch payloads don't have init sections or equivalent.
> >>>>>>> +             */
> >>>>>>> +            else if ( st_value >= (uintptr_t)&__init_begin &&
> >>>>>>> +                      st_value < (uintptr_t)&__init_end )
> >>>>>>> +            {
> >>>>>>> +                printk(XENLOG_ERR LIVEPATCH
> >>>>>>> +                       "%s: symbol %s is in init section, not resolving\n",
> >>>>>>> +                       elf->name, elf->sym[i].name);
> >>>>>>
> >>>>>> ... what I raised concern about (and I had not seen any verbal reply to that,
> >>>>>> I don't think).
> >>>>>
> >>>>> I've extended the commit message to explicitly mention the handling of
> >>>>> bounds for __init_{begin,end} checks.  Let me know if you are not fine
> >>>>> with it (or maybe you expected something else?).
> >>>>
> >>>> Well, you mention the two symbols you care about, but you don't mention
> >>>> at all that these two may alias other symbols which might be legal to
> >>>> reference from a livepatch.
> >>>
> >>> I'm having a hard time understanding why a livepatch would want to
> >>> reference those, or any symbol in the .init sections for that matter.
> >>> __init_{begin,end} are exclusively used to unmap the init region after
> >>> boot.  What's the point in livepatch referencing data that's no
> >>> longer there?  The only application I would think of is to calculate
> >>> some kind of offsets, but that would better be done using other
> >>> symbols instead.
> >>>
> >>> Please bear with me, it would be easier for me to understand if you
> >>> could provide a concrete example.
> >>
> >> The issue is that you do comparison by address, not by name. Let's assume
> >> that .rodata ends exactly where .init.* starts. Then &__init_begin ==
> >> &_erodata == &__2M_rodata_end. Access to the latter two symbols wants to
> >> be permitted; only __init_begin and whatever ends up being the very first
> >> symbol in .init.* ought to not be referenced.
> > 
> > Hm, I guess I could add comparison by name additionally, but it looks
> > fragile to me.
> 
> It looks fragile, yes. Thing is that you're trying to deal with this when
> crucial information was lost already. Or wait - isn't the section number
> still available in ->st_shndx?

But that's the section number of the to be resolved symbol?  In the
livepatch payload it would for example appear as:

0000000000000000       F *UND*	0000000000000000 .hidden setup_boot_APIC_clock

With undefined section.

It's possible I'm not following, is there a way to get the section
number of the current Xen symbols, and then correlate it with the
.init section?

Overall, I think it's unlikely for a livepatch to care about the
section start/end markers, albeit it would be good if we could make
this less ambiguous.

> 
> > So when st_value == __init_begin check if the name matches
> > "__init_begin" or "__2M_init_start" or "_sinittext", and fail
> > resolution, otherwise allow it?
> 
> Kind of, but that's not enough. For one, as said, the first symbol in
> the first .init.* section would also have this same address, and would
> also want rejecting. And then the same would be needed for __init_end.
> 
> >> Worse (but contrived) would be cases of "constructed" symbols derived from
> >> ones ahead of __init_begin, with an offset large enough to actually point
> >> into .init.*. Such are _still_ valid to be used in calculations, as long
> >> as no deref occurs for anything at or past __init_begin.
> > 
> > But one would have to craft such a symbol specifically, at which point
> > I consider this out of the scope of what this patch is attempting to
> > protect against.  The aim is not to prevent malicious livepatches, and
> > there are far easier ways to trigger a page-fault from a livepatch.
> 
> I understand the latter is the case, but I'm afraid I then don't see what
> the goal of this change is.

The goal would be for something like what caused:

https://xenbits.xen.org/gitweb/?p=xen.git;a=commit;h=af4cd0a6a61cdb03bc1afca9478b05b0c9703599

To be rejected at load time instead of causing a page-fault during
alternative patching, because xsm_ops was (wrongly) an .init symbol.

Thanks, Roger.


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

* Re: [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections
  2024-04-22 11:34                 ` Roger Pau Monné
@ 2024-04-22 11:51                   ` Jan Beulich
  0 siblings, 0 replies; 20+ messages in thread
From: Jan Beulich @ 2024-04-22 11:51 UTC (permalink / raw)
  To: Roger Pau Monné; +Cc: Ross Lagerwall, xen-devel

On 22.04.2024 13:34, Roger Pau Monné wrote:
> On Mon, Apr 22, 2024 at 12:57:55PM +0200, Jan Beulich wrote:
>> On 22.04.2024 12:49, Roger Pau Monné wrote:
>>> On Mon, Apr 22, 2024 at 10:25:40AM +0200, Jan Beulich wrote:
>>>> On 22.04.2024 09:54, Roger Pau Monné wrote:
>>>>> On Fri, Apr 19, 2024 at 04:34:41PM +0200, Jan Beulich wrote:
>>>>>> On 19.04.2024 12:50, Roger Pau Monné wrote:
>>>>>>> On Fri, Apr 19, 2024 at 12:15:19PM +0200, Jan Beulich wrote:
>>>>>>>> On 19.04.2024 12:02, Roger Pau Monne wrote:
>>>>>>>>> Livepatch payloads containing symbols that belong to init sections can only
>>>>>>>>> lead to page faults later on, as by the time the livepatch is loaded init
>>>>>>>>> sections have already been freed.
>>>>>>>>>
>>>>>>>>> Refuse to resolve such symbols and return an error instead.
>>>>>>>>>
>>>>>>>>> Note such resolutions are only relevant for symbols that point to undefined
>>>>>>>>> sections (SHN_UNDEF), as that implies the symbol is not in the current payload
>>>>>>>>> and hence must either be a Xen or a different livepatch payload symbol.
>>>>>>>>>
>>>>>>>>> Do not allow to resolve symbols that point to __init_begin, as that address is
>>>>>>>>> also unmapped.  On the other hand, __init_end is not unmapped, and hence allow
>>>>>>>>> resolutions against it.
>>>>>>>>>
>>>>>>>>> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
>>>>>>>>> ---
>>>>>>>>> Changes since v1:
>>>>>>>>>  - Fix off-by-one in range checking.
>>>>>>>>
>>>>>>>> Which means you addressed Andrew's comment while at the same time extending
>>>>>>>> the scope of ...
>>>>>>>>
>>>>>>>>> @@ -310,6 +311,21 @@ int livepatch_elf_resolve_symbols(struct livepatch_elf *elf)
>>>>>>>>>                      break;
>>>>>>>>>                  }
>>>>>>>>>              }
>>>>>>>>> +
>>>>>>>>> +            /*
>>>>>>>>> +             * Ensure not an init symbol.  Only applicable to Xen symbols, as
>>>>>>>>> +             * livepatch payloads don't have init sections or equivalent.
>>>>>>>>> +             */
>>>>>>>>> +            else if ( st_value >= (uintptr_t)&__init_begin &&
>>>>>>>>> +                      st_value < (uintptr_t)&__init_end )
>>>>>>>>> +            {
>>>>>>>>> +                printk(XENLOG_ERR LIVEPATCH
>>>>>>>>> +                       "%s: symbol %s is in init section, not resolving\n",
>>>>>>>>> +                       elf->name, elf->sym[i].name);
>>>>>>>>
>>>>>>>> ... what I raised concern about (and I had not seen any verbal reply to that,
>>>>>>>> I don't think).
>>>>>>>
>>>>>>> I've extended the commit message to explicitly mention the handling of
>>>>>>> bounds for __init_{begin,end} checks.  Let me know if you are not fine
>>>>>>> with it (or maybe you expected something else?).
>>>>>>
>>>>>> Well, you mention the two symbols you care about, but you don't mention
>>>>>> at all that these two may alias other symbols which might be legal to
>>>>>> reference from a livepatch.
>>>>>
>>>>> I'm having a hard time understanding why a livepatch would want to
>>>>> reference those, or any symbol in the .init sections for that matter.
>>>>> __init_{begin,end} are exclusively used to unmap the init region after
>>>>> boot.  What's the point in livepatch referencing data that's no
>>>>> longer there?  The only application I would think of is to calculate
>>>>> some kind of offsets, but that would better be done using other
>>>>> symbols instead.
>>>>>
>>>>> Please bear with me, it would be easier for me to understand if you
>>>>> could provide a concrete example.
>>>>
>>>> The issue is that you do comparison by address, not by name. Let's assume
>>>> that .rodata ends exactly where .init.* starts. Then &__init_begin ==
>>>> &_erodata == &__2M_rodata_end. Access to the latter two symbols wants to
>>>> be permitted; only __init_begin and whatever ends up being the very first
>>>> symbol in .init.* ought to not be referenced.
>>>
>>> Hm, I guess I could add comparison by name additionally, but it looks
>>> fragile to me.
>>
>> It looks fragile, yes. Thing is that you're trying to deal with this when
>> crucial information was lost already. Or wait - isn't the section number
>> still available in ->st_shndx?
> 
> But that's the section number of the to be resolved symbol?  In the
> livepatch payload it would for example appear as:
> 
> 0000000000000000       F *UND*	0000000000000000 .hidden setup_boot_APIC_clock
> 
> With undefined section.
> 
> It's possible I'm not following, is there a way to get the section
> number of the current Xen symbols, and then correlate it with the
> .init section?

Hmm, yes, looks like I was forgetting that livepatch resolves symbols
using kallsyms data, not Xen's ELF symbol table.

Jan


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

* Re: [PATCH v2 1/2] xen: introduce header file with section related symbols
  2024-04-19 10:02 ` [PATCH v2 1/2] xen: introduce header file with section related symbols Roger Pau Monne
  2024-04-19 10:08   ` Andrew Cooper
@ 2024-04-23 12:25   ` Jan Beulich
  2024-04-23 13:17     ` Roger Pau Monné
  1 sibling, 1 reply; 20+ messages in thread
From: Jan Beulich @ 2024-04-23 12:25 UTC (permalink / raw)
  To: Roger Pau Monne
  Cc: Stefano Stabellini, Julien Grall, Bertrand Marquis, Michal Orzel,
	Volodymyr Babchuk, Andrew Cooper, George Dunlap, xen-devel

On 19.04.2024 12:02, Roger Pau Monne wrote:
> Start by declaring the beginning and end of the init section.
> 
> No functional change intended.
> 
> Requested-by: Andrew Cooper <andrew.cooper3@citrix.com>
> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> ---
>  xen/arch/arm/mmu/setup.c   |  3 +--
>  xen/arch/x86/setup.c       |  3 +--
>  xen/include/xen/sections.h | 17 +++++++++++++++++
>  3 files changed, 19 insertions(+), 4 deletions(-)
>  create mode 100644 xen/include/xen/sections.h

Noticing a few things only after committing / pushing: For one, I should have
waited for an Arm ack. I hope that's not a big issue.

> --- a/xen/arch/arm/mmu/setup.c
> +++ b/xen/arch/arm/mmu/setup.c
> @@ -7,6 +7,7 @@
>  
>  #include <xen/init.h>
>  #include <xen/libfdt/libfdt.h>
> +#include <xen/sections.h>
>  #include <xen/sizes.h>
>  #include <xen/vmap.h>
>  
> @@ -62,8 +63,6 @@ vaddr_t directmap_virt_start __read_mostly;
>  unsigned long directmap_base_pdx __read_mostly;
>  #endif
>  
> -extern char __init_begin[], __init_end[];

Then I wonder why it was this rather than ...

> --- a/xen/arch/x86/setup.c
> +++ b/xen/arch/x86/setup.c
> @@ -5,6 +5,7 @@
>  #include <xen/param.h>
>  #include <xen/sched.h>
>  #include <xen/domain.h>
> +#include <xen/sections.h>
>  #include <xen/serial.h>
>  #include <xen/softirq.h>
>  #include <xen/acpi.h>
> @@ -309,8 +310,6 @@ void __init discard_initial_images(void)
>      initial_images = NULL;
>  }
>  
> -extern unsigned char __init_begin[], __init_end[];

... this ...

> --- /dev/null
> +++ b/xen/include/xen/sections.h
> @@ -0,0 +1,17 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +
> +#ifndef __XEN_SECTIONS_H__
> +#define __XEN_SECTIONS_H__
> +
> +/* SAF-0-safe */
> +extern char __init_begin[], __init_end[];

... that was moved. "unsigned char" is generally preferred over
declaring binary stuff "strings". I further wonder why the opportunity
wasn't taken to make both array-of-const. And finally I'm slightly
puzzled by the SAF comment appearing with no mention at all in the
description; of course I don't mind its addition.

Jan


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

* Re: [PATCH v2 1/2] xen: introduce header file with section related symbols
  2024-04-23 12:25   ` Jan Beulich
@ 2024-04-23 13:17     ` Roger Pau Monné
  0 siblings, 0 replies; 20+ messages in thread
From: Roger Pau Monné @ 2024-04-23 13:17 UTC (permalink / raw)
  To: Jan Beulich
  Cc: Stefano Stabellini, Julien Grall, Bertrand Marquis, Michal Orzel,
	Volodymyr Babchuk, Andrew Cooper, George Dunlap, xen-devel

On Tue, Apr 23, 2024 at 02:25:16PM +0200, Jan Beulich wrote:
> On 19.04.2024 12:02, Roger Pau Monne wrote:
> Then I wonder why it was this rather than ...
> 
> > --- a/xen/arch/x86/setup.c
> > +++ b/xen/arch/x86/setup.c
> > @@ -5,6 +5,7 @@
> >  #include <xen/param.h>
> >  #include <xen/sched.h>
> >  #include <xen/domain.h>
> > +#include <xen/sections.h>
> >  #include <xen/serial.h>
> >  #include <xen/softirq.h>
> >  #include <xen/acpi.h>
> > @@ -309,8 +310,6 @@ void __init discard_initial_images(void)
> >      initial_images = NULL;
> >  }
> >  
> > -extern unsigned char __init_begin[], __init_end[];
> 
> ... this ...
> 
> > --- /dev/null
> > +++ b/xen/include/xen/sections.h
> > @@ -0,0 +1,17 @@
> > +/* SPDX-License-Identifier: GPL-2.0-only */
> > +
> > +#ifndef __XEN_SECTIONS_H__
> > +#define __XEN_SECTIONS_H__
> > +
> > +/* SAF-0-safe */
> > +extern char __init_begin[], __init_end[];
> 
> ... that was moved. "unsigned char" is generally preferred over
> declaring binary stuff "strings".

OK, noted.

> I further wonder why the opportunity
> wasn't taken to make both array-of-const.

Hm, yes.  The sections are mapped RWX IIRC, but there's no reason to
not make the markers const.

> And finally I'm slightly
> puzzled by the SAF comment appearing with no mention at all in the
> description; of course I don't mind its addition.

I could have noted it in the commit message indeed, sorry.

Thanks, Roger.


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

end of thread, other threads:[~2024-04-23 13:18 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-19 10:02 [PATCH v2 0/2] livepatch: make symbol resolution more robust Roger Pau Monne
2024-04-19 10:02 ` [PATCH v2 1/2] xen: introduce header file with section related symbols Roger Pau Monne
2024-04-19 10:08   ` Andrew Cooper
2024-04-19 10:12     ` Jan Beulich
2024-04-19 10:16       ` Andrew Cooper
2024-04-19 14:32         ` Jan Beulich
2024-04-23 12:25   ` Jan Beulich
2024-04-23 13:17     ` Roger Pau Monné
2024-04-19 10:02 ` [PATCH v2 2/2] livepatch: refuse to resolve symbols that belong to init sections Roger Pau Monne
2024-04-19 10:10   ` Andrew Cooper
2024-04-19 10:48     ` Roger Pau Monné
2024-04-19 10:15   ` Jan Beulich
2024-04-19 10:50     ` Roger Pau Monné
2024-04-19 14:34       ` Jan Beulich
2024-04-22  7:54         ` Roger Pau Monné
2024-04-22  8:25           ` Jan Beulich
2024-04-22 10:49             ` Roger Pau Monné
2024-04-22 10:57               ` Jan Beulich
2024-04-22 11:34                 ` Roger Pau Monné
2024-04-22 11:51                   ` Jan Beulich

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.