Skip to contentSkip to conversation

xen/livepatch: Fix secure_payload() in non-debug builds

PATCH In review Andrew Cooper 3 participants 3 messages Apr 17, 2023 – Apr 18, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/livepatch: Fix secure_payload() in non-debug builds

The ro_pages + rw_pages + text_pages != payload->pages check is not something which is reasonable to skip at runtime. Rewrite it to not be an ASSERT().

As the code is being shuffled anyway, rework the logic calling arch_livepatch_secure() to reduce its verbosity.

---
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Ross Lagerwall <[email protected]>

---
 xen/common/livepatch.c | 37 ++++++++++++++++---------------------
 1 file changed, 16 insertions(+), 21 deletions(-)
xen/common/livepatch.c +16 -21
index d385f882c65c..c10ab1f374e0 100644
@@ -405,32 +405,27 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf)
 
 static int secure_payload(struct payload *payload, struct livepatch_elf *elf)
 {
-    int rc = 0;
-    unsigned int text_pages, rw_pages, ro_pages;
+    unsigned int text_pages = PFN_UP(payload->text_size);
+    unsigned int rw_pages   = PFN_UP(payload->rw_size);
+    unsigned int ro_pages   = PFN_UP(payload->ro_size);
+    int rc;
 
-    text_pages = PFN_UP(payload->text_size);
+    if ( ro_pages + rw_pages + text_pages != payload->pages )
+        return -EINVAL;
 
-    if ( text_pages )
-    {
-        rc = arch_livepatch_secure(payload->text_addr, text_pages, LIVEPATCH_VA_RX);
-        if ( rc )
-            return rc;
-    }
-    rw_pages = PFN_UP(payload->rw_size);
-    if ( rw_pages )
-    {
-        rc = arch_livepatch_secure(payload->rw_addr, rw_pages, LIVEPATCH_VA_RW);
-        if ( rc )
-            return rc;
-    }
+    if ( text_pages &&
+         (rc = arch_livepatch_secure(payload->text_addr, text_pages, LIVEPATCH_VA_RX)) )
+        return rc;
 
-    ro_pages = PFN_UP(payload->ro_size);
-    if ( ro_pages )
-        rc = arch_livepatch_secure(payload->ro_addr, ro_pages, LIVEPATCH_VA_RO);
+    if ( rw_pages &&
+         (rc = arch_livepatch_secure(payload->rw_addr, rw_pages, LIVEPATCH_VA_RW)) )
+        return rc;
 
-    ASSERT(ro_pages + rw_pages + text_pages == payload->pages);
+    if ( ro_pages &&
+         (rc = arch_livepatch_secure(payload->ro_addr, ro_pages, LIVEPATCH_VA_RO)) )
+        return rc;
 
-    return rc;
+    return 0;
 }
 
 static bool section_ok(const struct livepatch_elf *elf,
Signature · 1 line
2.30.2
#1 lore raw
Quoted 2 lines · Andrew Cooper

The ro_pages + rw_pages + text_pages != payload->pages check is not something which is reasonable to skip at runtime. Rewrite it to not be an ASSERT().

Isn't this merely a sanity check? IOW isn't returning -EINVAL in this case misleading, as to calling "invalid input" what really is an internal error in Xen? But anyway, I guess I'll leave this to the maintainers.

Quoted 2 lines

As the code is being shuffled anyway, rework the logic calling arch_livepatch_secure() to reduce its verbosity.

By "verbosity", dym lines of code?

Jan

#2 lore raw
Quoted 7 lines

From: Jan Beulich <[email protected]>
Sent: Monday, April 17, 2023 11:41 AM
To: Andrew Cooper <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>; Ross Lagerwall <[email protected]>; Xen-devel <[email protected]>
Subject: Re: [PATCH] xen/livepatch: Fix secure_payload() in non-debug builds

On 17.04.2023 11:58, Andrew Cooper wrote:

Quoted 7 lines
Quoted 2 lines

The ro_pages + rw_pages + text_pages != payload->pages check is not something which is reasonable to skip at runtime.  Rewrite it to not be an ASSERT().

Isn't this merely a sanity check? IOW isn't returning -EINVAL in this case misleading, as to calling "invalid input" what really is an internal error in Xen? But anyway, I guess I'll leave this to the maintainers.

Yes, it looks like it is just a sanity check of the payload->pages calculation in move_payload(). Since it is not dependent on the payload, I think ASSERT() is correct.

Ross