Skip to contentSkip to conversation

dt-overlay: some fixes and improvements

PATCH In review Michal Orzel 6 participants 26 messages Apr 15 – Apr 21

lore
Share & tools
lore
#0 lore raw

cover dt-overlay: some fixes and improvements

I don't like the state this feature is in right now. While the feature, even in Linux is quite fragile, I investigated quite a lot of corner cases and many issues are present due to the fact that we support a weird combination of ADD/REMOVE + ATTACH without DETACH. Focusing on fixing issues and implementing DETACH is not an easy task now. REMOVE handles things that should really be done by DETACH. I think that the best move forward would be to strip the REMOVE operation from the dt-overlay subsystem (I would preserve ABI). This would eliminate the buggy, incomplete teardown code and leave a clean ADD+ATTACH surface to later build DETACH and a proper REMOVE on top of. Thoughts? The removal of REMOVE could be done in this release, not to carry on the buggy code.

I tested this series with the AMD SW/HW stack.

Michal Orzel (6):
xen/dt-overlay: fix double-free of rangesets on attach failure xen/dt-overlay: fix rangeset leak and dead code in domctl path xen/dt-overlay: check overlay size before memcmp in tracker lookup xen/dt-overlay: fix silent success in dt_overlay_remove_node xen/dt-overlay: support phandle-based targeting in
overlay_get_nodes_info
xen/dt-overlay: attach resources for child nodes in overlay

 xen/common/device-tree/dt-overlay.c | 120 ++++++++++++++++++++++++----
 1 file changed, 105 insertions(+), 15 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/6 xen/dt-overlay: fix double-free of rangesets on attach failure

handle_attach_overlay_nodes() destroys the IRQ and IOMEM rangesets on failure but leaves the pointers dangling in the tracker entry. A subsequent handle_remove_overlay_nodes() for the same overlay will call rangeset_consume_ranges() on freed memory followed by a second rangeset_destroy(), resulting in use-after-free and double-free.

NULL the pointers after rangeset_destroy() so that remove_nodes() and handle_remove_overlay_nodes() skip the stale entries.

Fixes 4c733873b5c2 ("xen/arm: Add XEN_DOMCTL_dt_overlay and device attachment to domains")
---
 xen/common/device-tree/dt-overlay.c | 2 ++
 1 file changed, 2 insertions(+)
xen/common/device-tree/dt-overlay.c +2 -0
index d184186c015e..6fa07dbf42a5 100644
@@ -910,7 +910,9 @@ static long handle_attach_overlay_nodes(struct domain *d,
     if ( entry )
     {
         rangeset_destroy(entry->irq_ranges);
+        entry->irq_ranges = NULL;
         rangeset_destroy(entry->iomem_ranges);
+        entry->iomem_ranges = NULL;
     }
 
     return rc;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/6] xen/dt-overlay: fix double-free of rangesets on attach failure

Thanks for your efforts.

I’ve seen some advisories that received cve here.
https://xenbits.xen.org/xsa/

Can I receive a cve for this report?

Thanks.

2026년 4월 15일 (수) 20:37, Michal Orzel <[email protected]>님이 작성:

Quoted 37 lines

handle_attach_overlay_nodes() destroys the IRQ and IOMEM rangesets on failure but leaves the pointers dangling in the tracker entry. A subsequent handle_remove_overlay_nodes() for the same overlay will call rangeset_consume_ranges() on freed memory followed by a second rangeset_destroy(), resulting in use-after-free and double-free.

NULL the pointers after rangeset_destroy() so that remove_nodes() and handle_remove_overlay_nodes() skip the stale entries.

Fixes 4c733873b5c2 ("xen/arm: Add XEN_DOMCTL_dt_overlay and device

attachment to domains")

---
 xen/common/device-tree/dt-overlay.c | 2 ++
 1 file changed, 2 insertions(+)
xen/common/device-tree/dt-overlay.c +2 -0
index d184186c015e..6fa07dbf42a5 100644
@@ -910,7 +910,9 @@ static long handle_attach_overlay_nodes(struct domain
 *d,
     if ( entry )
     {
         rangeset_destroy(entry->irq_ranges);
+        entry->irq_ranges = NULL;
         rangeset_destroy(entry->iomem_ranges);
+        entry->iomem_ranges = NULL;
     }
 
     return rc;

--
2.43.0

#3 lore raw

Re: [PATCH 1/6] xen/dt-overlay: fix double-free of rangesets on attach failure

Le 15/04/2026 à 13:51, Gyujeong Jin a écrit :

Quoted 9 lines

Thanks for your efforts.

I’ve seen some advisories that received cve here.
https://xenbits.xen.org/xsa/ <https://xenbits.xen.org/xsa/>

Can I receive a cve for this report?

Thanks.

Device-tree overlay is marked as "Experimental", hence it's not Security supported so no XSA would be issued, even if it it has security implications for the feature.

I don't know regarding individual CVEs though.

Quoted 32 lines

2026년 4월 15일 (수) 20:37, Michal Orzel <[email protected] <mailto:[email protected]>>님이 작성:

    handle_attach_overlay_nodes() destroys the IRQ and IOMEM rangesets on
    failure but leaves the pointers dangling in the tracker entry. A
    subsequent handle_remove_overlay_nodes() for the same overlay will call
    rangeset_consume_ranges() on freed memory followed by a second
    rangeset_destroy(), resulting in use-after-free and double-free.

    NULL the pointers after rangeset_destroy() so that remove_nodes() and
    handle_remove_overlay_nodes() skip the stale entries.

    Fixes: 4c733873b5c2 ("xen/arm: Add XEN_DOMCTL_dt_overlay and device
    attachment to domains")
    Reported-by: Gyujeong Jin <[email protected]
    <mailto:[email protected]>>
    Signed-off-by: Michal Orzel <[email protected]
    <mailto:[email protected]>>
    ---
      xen/common/device-tree/dt-overlay.c | 2 ++
      1 file changed, 2 insertions(+)

    diff --git a/xen/common/device-tree/dt-overlay.c b/xen/common/
    device-tree/dt-overlay.c
    index d184186c015e..6fa07dbf42a5 100644
    --- a/xen/common/device-tree/dt-overlay.c
    +++ b/xen/common/device-tree/dt-overlay.c
    @@ -910,7 +910,9 @@ static long handle_attach_overlay_nodes(struct
    domain *d,
          if ( entry )
          {
              rangeset_destroy(entry->irq_ranges);
Quoted 9 lines
    +        entry->irq_ranges = NULL;
              rangeset_destroy(entry->iomem_ranges);
    +        entry->iomem_ranges = NULL;
          }

          return rc;
    --
    2.43.0

Teddy

--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#4 lore raw

Re: [PATCH 1/6] xen/dt-overlay: fix double-free of rangesets on attach failure

In reply to Gyujeong Jin

Hi Gyujeong,

Quoted 6 lines · Gyujeong Jin

Thanks for your efforts.

I’ve seen some advisories that received cve here.
https://xenbits.xen.org/xsa/ <https://xenbits.xen.org/xsa/>

Can I receive a cve for this report?

Thank you for the original report. The security team doesn't issue any CVE when there is no associated XSA.

Best regards,

Signature · 1 line
Julien Grall
Reviewed-by on v1 1/6
#5 lore raw

Re: [PATCH 1/6] xen/dt-overlay: fix double-free of rangesets on attach failure

In reply to Michal Orzel

Hi Michal,

Quoted 8 lines · Michal Orzel

handle_attach_overlay_nodes() destroys the IRQ and IOMEM rangesets on failure but leaves the pointers dangling in the tracker entry. A subsequent handle_remove_overlay_nodes() for the same overlay will call rangeset_consume_ranges() on freed memory followed by a second rangeset_destroy(), resulting in use-after-free and double-free.

Quoted 7 lines

NULL the pointers after rangeset_destroy() so that remove_nodes() and handle_remove_overlay_nodes() skip the stale entries.

Fixes 4c733873b5c2 ("xen/arm: Add XEN_DOMCTL_dt_overlay and device attachment to domains")

---

Looks ok to me

Cheers,
Luca

#6 lore raw

Re: [PATCH 1/6] xen/dt-overlay: fix double-free of rangesets on attach failure

Quoted 13 lines · Luca Fancellu

Hi Michal,

Quoted 11 lines · Michal Orzel

handle_attach_overlay_nodes() destroys the IRQ and IOMEM rangesets on failure but leaves the pointers dangling in the tracker entry. A subsequent handle_remove_overlay_nodes() for the same overlay will call rangeset_consume_ranges() on freed memory followed by a second rangeset_destroy(), resulting in use-after-free and double-free.

NULL the pointers after rangeset_destroy() so that remove_nodes() and handle_remove_overlay_nodes() skip the stale entries.

Quoted 8 lines
Quoted 4 lines
Fixes 4c733873b5c2 ("xen/arm: Add XEN_DOMCTL_dt_overlay and device attachment to domains")

---

Looks ok to me

#7 lore raw

PATCH 2/6 xen/dt-overlay: fix rangeset leak and dead code in domctl path

In reply to Michal Orzel

handle_attach_overlay_nodes() unconditionally creates new rangesets without checking whether they already exist from a prior attach. A repeated attach for the same overlay leaks the original pair. Reject the operation with -EEXIST if rangesets are already present.

Also, dt_overlay_domctl() early-returns -EOPNOTSUPP for any operation other than ATTACH, making the if/else at the bottom unreachable. Remove the dead branch.

Fixes 4c733873b5c2 ("xen/arm: Add XEN_DOMCTL_dt_overlay and device attachment to domains")
---
 xen/common/device-tree/dt-overlay.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
xen/common/device-tree/dt-overlay.c +8 -4
index 6fa07dbf42a5..3853e6e347fe 100644
@@ -861,6 +861,13 @@ static long handle_attach_overlay_nodes(struct domain *d,
         goto out;
     }
 
+    if ( entry->irq_ranges || entry->iomem_ranges )
+    {
+        printk(XENLOG_ERR "Overlay is already attached\n");
+        spin_unlock(&overlay_lock);
+        return -EEXIST;
+    }
+
     entry->irq_ranges = rangeset_new(d, "Overlays: Interrupts", 0);
     if (entry->irq_ranges == NULL)
     {
@@ -991,10 +998,7 @@ long dt_overlay_domctl(struct domain *d, struct xen_domctl_dt_overlay *op)
         return -EFAULT;
     }
 
-    if ( op->overlay_op == XEN_DOMCTL_DT_OVERLAY_ATTACH )
-        ret = handle_attach_overlay_nodes(d, overlay_fdt, op->overlay_fdt_size);
-    else
-        ret = -EOPNOTSUPP;
+    ret = handle_attach_overlay_nodes(d, overlay_fdt, op->overlay_fdt_size);
 
     xfree(overlay_fdt);
 
Signature · 1 line
2.43.0
Reviewed-by on v1 2/6
#8 lore raw

Re: [PATCH 2/6] xen/dt-overlay: fix rangeset leak and dead code in domctl path

Hi Michal,

Quoted 7 lines · Michal Orzel

handle_attach_overlay_nodes() unconditionally creates new rangesets without checking whether they already exist from a prior attach. A repeated attach for the same overlay leaks the original pair. Reject the operation with -EEXIST if rangesets are already present.

Quoted 8 lines

Also, dt_overlay_domctl() early-returns -EOPNOTSUPP for any operation other than ATTACH, making the if/else at the bottom unreachable. Remove the dead branch.

Fixes 4c733873b5c2 ("xen/arm: Add XEN_DOMCTL_dt_overlay and device attachment to domains")

---

Cheers,
Luca

#9 lore raw

Re: [PATCH 2/6] xen/dt-overlay: fix rangeset leak and dead code in domctl path

Quoted 13 lines · Luca Fancellu

Hi Michal,

Quoted 11 lines · Michal Orzel

handle_attach_overlay_nodes() unconditionally creates new rangesets without checking whether they already exist from a prior attach. A repeated attach for the same overlay leaks the original pair. Reject the operation with -EEXIST if rangesets are already present.

Also, dt_overlay_domctl() early-returns -EOPNOTSUPP for any operation other than ATTACH, making the if/else at the bottom unreachable. Remove the dead branch.

Quoted 6 lines
Quoted 4 lines
Fixes 4c733873b5c2 ("xen/arm: Add XEN_DOMCTL_dt_overlay and device attachment to domains")

---

#10 lore raw

PATCH 3/6 xen/dt-overlay: check overlay size before memcmp in tracker lookup

In reply to Michal Orzel

find_track_entry_from_tracker() compares overlay_fdt_size bytes of the stored overlay against the input without verifying that the stored overlay is at least that large. If the input is larger, memcmp reads past the stored allocation. If smaller, a prefix match could falsely succeed.

Compare fdt_totalsize() of the stored overlay against overlay_fdt_size first. Both values are validated by check_overlay_fdt() at their respective entry points, so no additional field in overlay_track is needed.

Fixes 7e5c4a8b86f1 ("xen/arm: Implement device tree node removal functionalities")
---
 xen/common/device-tree/dt-overlay.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
xen/common/device-tree/dt-overlay.c +2 -1
index 3853e6e347fe..0eed1532a10d 100644
@@ -379,7 +379,8 @@ find_track_entry_from_tracker(const void *overlay_fdt,
      */
     list_for_each_entry_safe( entry, temp, &overlay_tracker, entry )
     {
-        if ( memcmp(entry->overlay_fdt, overlay_fdt, overlay_fdt_size) == 0 )
+        if ( (fdt_totalsize(entry->overlay_fdt) == overlay_fdt_size) &&
+             !memcmp(entry->overlay_fdt, overlay_fdt, overlay_fdt_size) )
         {
             found_entry = true;
             break;
Signature · 1 line
2.43.0
Reviewed-by on v1 3/6
#11 lore raw

Re: [PATCH 3/6] xen/dt-overlay: check overlay size before memcmp in tracker lookup

Hi Michal,

Quoted 8 lines · Michal Orzel

find_track_entry_from_tracker() compares overlay_fdt_size bytes of the stored overlay against the input without verifying that the stored overlay is at least that large. If the input is larger, memcmp reads past the stored allocation. If smaller, a prefix match could falsely succeed.

Quoted 8 lines

Compare fdt_totalsize() of the stored overlay against overlay_fdt_size first. Both values are validated by check_overlay_fdt() at their respective entry points, so no additional field in overlay_track is needed.

Fixes 7e5c4a8b86f1 ("xen/arm: Implement device tree node removal functionalities")

---

Cheers,
Luca

#12 lore raw

Re: [PATCH 3/6] xen/dt-overlay: check overlay size before memcmp in tracker lookup

Quoted 15 lines · Luca Fancellu

Hi Michal,

Quoted 13 lines · Michal Orzel

find_track_entry_from_tracker() compares overlay_fdt_size bytes of the stored overlay against the input without verifying that the stored overlay is at least that large. If the input is larger, memcmp reads past the stored allocation. If smaller, a prefix match could falsely succeed.

Compare fdt_totalsize() of the stored overlay against overlay_fdt_size first. Both values are validated by check_overlay_fdt() at their respective entry points, so no additional field in overlay_track is needed.

Quoted 5 lines
Quoted 3 lines
Fixes 7e5c4a8b86f1 ("xen/arm: Implement device tree node removal functionalities")

---

#13 lore raw

PATCH 4/6 xen/dt-overlay: fix silent success in dt_overlay_remove_node

In reply to Michal Orzel

dt_overlay_remove_node() silently returns 0 when the target node is not found in the parent's sibling list. The loop simply exits without matching and falls through to "return 0", making the caller believe the node was successfully removed.

Return -ENODEV after the loop when no match is found, and change the found-path from break to an explicit return 0 so the two outcomes are distinct.

Fixes 7e5c4a8b86f1 ("xen/arm: Implement device tree node removal functionalities")
---
 xen/common/device-tree/dt-overlay.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/common/device-tree/dt-overlay.c +2 -2
index 0eed1532a10d..d3d4669718ac 100644
@@ -125,11 +125,11 @@ static int dt_overlay_remove_node(struct dt_device_node *device_node)
             else
                 np->allnext = np->allnext->allnext;
 
-            break;
+            return 0;
         }
     }
 
-    return 0;
+    return -ENODEV;
 }
 
 static int dt_overlay_add_node(struct dt_device_node *device_node,
Signature · 1 line
2.43.0
Reviewed-by on v1 4/6
#14 lore raw

Re: [PATCH 4/6] xen/dt-overlay: fix silent success in dt_overlay_remove_node

Hi Michal,

Quoted 7 lines · Michal Orzel

dt_overlay_remove_node() silently returns 0 when the target node is not found in the parent's sibling list. The loop simply exits without matching and falls through to "return 0", making the caller believe the node was successfully removed.

Quoted 7 lines

Return -ENODEV after the loop when no match is found, and change the found-path from break to an explicit return 0 so the two outcomes are distinct.

Fixes 7e5c4a8b86f1 ("xen/arm: Implement device tree node removal functionalities")

---

Looks ok to me

Cheers,
Luca

#15 lore raw

Re: [PATCH 4/6] xen/dt-overlay: fix silent success in dt_overlay_remove_node

Quoted 13 lines · Luca Fancellu

Hi Michal,

Quoted 11 lines · Michal Orzel

dt_overlay_remove_node() silently returns 0 when the target node is not found in the parent's sibling list. The loop simply exits without matching and falls through to "return 0", making the caller believe the node was successfully removed.

Return -ENODEV after the loop when no match is found, and change the found-path from break to an explicit return 0 so the two outcomes are distinct.

Quoted 7 lines
Quoted 3 lines
Fixes 7e5c4a8b86f1 ("xen/arm: Implement device tree node removal functionalities")

---

Looks ok to me

#16 lore raw

PATCH 5/6 xen/dt-overlay: support phandle-based targeting in overlay_get_nodes_info

In reply to Michal Orzel

overlay_get_nodes_info() is called before fdt_overlay_apply() to extract target paths from the overlay. This fails for overlays using phandle-based targeting (target = <&label>) because DTC compiles these as unresolved fixups (target = <0xffffffff>), causing fdt_overlay_target_offset() to return -FDT_ERR_BADPHANDLE. Prior to this change users were forced to manually modify the dtbo (even for hwdom) to switch from target to target-phandle by manually inspecting also the host DTB.

Introduce overlay_get_target_path() which directly handles the two targeting cases that occur before fixup resolution:
- target-path: the string property is returned directly.
- target = <&label>: the label is found in the overlay's __fixups__ node, then resolved to a path via the base DTB's __symbols__ node.

---
 xen/common/device-tree/dt-overlay.c | 65 ++++++++++++++++++++++++++---
 1 file changed, 59 insertions(+), 6 deletions(-)
xen/common/device-tree/dt-overlay.c +59 -6
index d3d4669718ac..a0dee7edb7e5 100644
@@ -286,6 +286,63 @@ static unsigned int overlay_node_count(const void *overlay_fdt)
     return num_overlay_nodes;
 }
 
+/*
+ * Resolve the target path for an overlay fragment.
+ *
+ * This is called before fdt_overlay_apply(), so phandle-based targets
+ * (target = <&label>) are still unresolved (compiled as 0xffffffff by DTC).
+ * Handle the two cases that actually occur:
+ *  - target-path property: the path string is used directly,
+ *  - target = <&label>: the label is looked up in the overlay's __fixups__
+ *    node, then resolved to a path via the base DTB's __symbols__ node.
+ *
+ * Returns a pointer into the FDT on success, NULL on failure.
+ */
+static const char *overlay_get_target_path(const void *fdt, const void *fdto,
+                                           int fragment)
+{
+    const char *path, *fragment_name;
+    int fixups_off, symbols_off, property;
+    int fragment_name_len;
+
+    /* Try target-path first (string-based targeting) */
+    path = fdt_getprop(fdto, fragment, "target-path", NULL);
+    if ( path )
+        return path;
+
+    /* Phandle-based target: resolve via __fixups__ and __symbols__ */
+    fixups_off = fdt_path_offset(fdto, "/__fixups__");
+    if ( fixups_off < 0 )
+        return NULL;
+
+    symbols_off = fdt_path_offset(fdt, "/__symbols__");
+    if ( symbols_off < 0 )
+        return NULL;
+
+    fragment_name = fdt_get_name(fdto, fragment, &fragment_name_len);
+    if ( !fragment_name )
+        return NULL;
+
+    fdt_for_each_property_offset(property, fdto, fixups_off)
+    {
+        const char *val, *label, *p;
+        int val_len;
+
+        val = fdt_getprop_by_offset(fdto, property, &label, &val_len);
+        if ( !val )
+            continue;
+
+        /* Match entries of the form "/<fragment_name>:target:0" */
+        for ( p = val; p < (val + val_len); p += (strlen(p) + 1) )
+            if ( p[0] == '/' &&
+                 !strncmp(p + 1, fragment_name, fragment_name_len) &&
+                 !strcmp(p + 1 + fragment_name_len, ":target:0") )
+                return fdt_getprop(fdt, symbols_off, label, NULL);
+    }
+
+    return NULL;
+}
+
 /*
  * overlay_get_nodes_info gets full name with path for all the nodes which
  * are in one level of __overlay__ tag. This is useful when checking node for
@@ -298,7 +355,6 @@ static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path)
 
     fdt_for_each_subnode(fragment, fdto, 0)
     {
-        int target;
         int overlay;
         int subnode;
         const char *target_path;
@@ -307,11 +363,8 @@ static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path)
         if ( overlay < 0 )
             continue;
 
-        target = fdt_overlay_target_offset(device_tree_flattened, fdto,
-                                           fragment, &target_path);
-        if ( target < 0 )
-            return target;
-
+        target_path = overlay_get_target_path(device_tree_flattened, fdto,
+                                              fragment);
         if ( target_path == NULL )
             return -EINVAL;
 
Signature · 1 line
2.43.0
#17 lore raw

Re: [PATCH 5/6] xen/dt-overlay: support phandle-based targeting in overlay_get_nodes_info

Hi Michal,

Quoted 67 lines · Michal Orzel

overlay_get_nodes_info() is called before fdt_overlay_apply() to extract target paths from the overlay. This fails for overlays using phandle-based targeting (target = <&label>) because DTC compiles these as unresolved fixups (target = <0xffffffff>), causing fdt_overlay_target_offset() to return -FDT_ERR_BADPHANDLE. Prior to this change users were forced to manually modify the dtbo (even for hwdom) to switch from target to target-phandle by manually inspecting also the host DTB.

Introduce overlay_get_target_path() which directly handles the two targeting cases that occur before fixup resolution:
- target-path: the string property is returned directly.
- target = <&label>: the label is found in the overlay's __fixups__ node, then resolved to a path via the base DTB's __symbols__ node.

---
xen/common/device-tree/dt-overlay.c | 65 ++++++++++++++++++++++++++---

1 file changed, 59 insertions(+), 6 deletions(-)
xen/common/device-tree/dt-overlay.c +38 -0
index d3d4669718ac..a0dee7edb7e5 100644
@@ -286,6 +286,63 @@ static unsigned int overlay_node_count(const void *overlay_fdt)
    return num_overlay_nodes;
 }
 
+/*
+ * Resolve the target path for an overlay fragment.
+ *
+ * This is called before fdt_overlay_apply(), so phandle-based targets
+ * (target = <&label>) are still unresolved (compiled as 0xffffffff by DTC).
+ * Handle the two cases that actually occur:
+ *  - target-path property: the path string is used directly,
+ *  - target = <&label>: the label is looked up in the overlay's __fixups__
+ *    node, then resolved to a path via the base DTB's __symbols__ node.
+ *
+ * Returns a pointer into the FDT on success, NULL on failure.
+ */
+static const char *overlay_get_target_path(const void *fdt, const void *fdto,
+                                           int fragment)
+{
+    const char *path, *fragment_name;
+    int fixups_off, symbols_off, property;
+    int fragment_name_len;
+
+    /* Try target-path first (string-based targeting) */
+    path = fdt_getprop(fdto, fragment, "target-path", NULL);
+    if ( path )
+        return path;
+
+    /* Phandle-based target: resolve via __fixups__ and __symbols__ */
+    fixups_off = fdt_path_offset(fdto, "/__fixups__");
+    if ( fixups_off < 0 )
+        return NULL;
+
+    symbols_off = fdt_path_offset(fdt, "/__symbols__");
+    if ( symbols_off < 0 )
+        return NULL;
+
+    fragment_name = fdt_get_name(fdto, fragment, &fragment_name_len);
+    if ( !fragment_name )
+        return NULL;
+
+    fdt_for_each_property_offset(property, fdto, fixups_off)

xen/common/device-tree/dt-overlay.c static unsigned int overlay_node_count(const void *overlay_fdt) · line 336

Quoted 10 lines
xen/common/device-tree/dt-overlay.c +10 -0
+    {
+        const char *val, *label, *p;
+        int val_len;
+
+        val = fdt_getprop_by_offset(fdto, property, &label, &val_len);
+        if ( !val )
+            continue;
+
+        /* Match entries of the form "/<fragment_name>:target:0" */
+        for ( p = val; p < (val + val_len); p += (strlen(p) + 1) )

what guarantees us that p will be null terminated, if a malformed overlay is passed this strlen can read past the string, we can use strnlen having as upper bound a counter=val_len? decreasing counter each iteration.

Or do you think it can never happen?

Quoted 38 lines
xen/common/device-tree/dt-overlay.c +11 -6
+            if ( p[0] == '/' &&
+                 !strncmp(p + 1, fragment_name, fragment_name_len) &&
+                 !strcmp(p + 1 + fragment_name_len, ":target:0") )
+                return fdt_getprop(fdt, symbols_off, label, NULL);
+    }
+
+    return NULL;
+}
+
 /*
 * overlay_get_nodes_info gets full name with path for all the nodes which
 * are in one level of __overlay__ tag. This is useful when checking node for
@@ -298,7 +355,6 @@ static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path)
 
    fdt_for_each_subnode(fragment, fdto, 0)
    {
-        int target;
        int overlay;
        int subnode;
        const char *target_path;
@@ -307,11 +363,8 @@ static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path)
        if ( overlay < 0 )
            continue;
 
-        target = fdt_overlay_target_offset(device_tree_flattened, fdto,
-                                           fragment, &target_path);
-        if ( target < 0 )
-            return target;
-
+        target_path = overlay_get_target_path(device_tree_flattened, fdto,
+                                              fragment);
        if ( target_path == NULL )
            return -EINVAL;
 
Signature · 1 line
2.43.0

Cheers,
Luca

#18 lore raw

Re: [PATCH 5/6] xen/dt-overlay: support phandle-based targeting in overlay_get_nodes_info

Quoted 84 lines · Luca Fancellu

Hi Michal,

Quoted 67 lines · Michal Orzel

overlay_get_nodes_info() is called before fdt_overlay_apply() to extract target paths from the overlay. This fails for overlays using phandle-based targeting (target = <&label>) because DTC compiles these as unresolved fixups (target = <0xffffffff>), causing fdt_overlay_target_offset() to return -FDT_ERR_BADPHANDLE. Prior to this change users were forced to manually modify the dtbo (even for hwdom) to switch from target to target-phandle by manually inspecting also the host DTB.

Introduce overlay_get_target_path() which directly handles the two targeting cases that occur before fixup resolution:
- target-path: the string property is returned directly.
- target = <&label>: the label is found in the overlay's __fixups__ node, then resolved to a path via the base DTB's __symbols__ node.

---
xen/common/device-tree/dt-overlay.c | 65 ++++++++++++++++++++++++++---

1 file changed, 59 insertions(+), 6 deletions(-)
xen/common/device-tree/dt-overlay.c +38 -0
index d3d4669718ac..a0dee7edb7e5 100644
@@ -286,6 +286,63 @@ static unsigned int overlay_node_count(const void *overlay_fdt)
    return num_overlay_nodes;
 }
 
+/*
+ * Resolve the target path for an overlay fragment.
+ *
+ * This is called before fdt_overlay_apply(), so phandle-based targets
+ * (target = <&label>) are still unresolved (compiled as 0xffffffff by DTC).
+ * Handle the two cases that actually occur:
+ *  - target-path property: the path string is used directly,
+ *  - target = <&label>: the label is looked up in the overlay's __fixups__
+ *    node, then resolved to a path via the base DTB's __symbols__ node.
+ *
+ * Returns a pointer into the FDT on success, NULL on failure.
+ */
+static const char *overlay_get_target_path(const void *fdt, const void *fdto,
+                                           int fragment)
+{
+    const char *path, *fragment_name;
+    int fixups_off, symbols_off, property;
+    int fragment_name_len;
+
+    /* Try target-path first (string-based targeting) */
+    path = fdt_getprop(fdto, fragment, "target-path", NULL);
+    if ( path )
+        return path;
+
+    /* Phandle-based target: resolve via __fixups__ and __symbols__ */
+    fixups_off = fdt_path_offset(fdto, "/__fixups__");
+    if ( fixups_off < 0 )
+        return NULL;
+
+    symbols_off = fdt_path_offset(fdt, "/__symbols__");
+    if ( symbols_off < 0 )
+        return NULL;
+
+    fragment_name = fdt_get_name(fdto, fragment, &fragment_name_len);
+    if ( !fragment_name )
+        return NULL;
+
+    fdt_for_each_property_offset(property, fdto, fixups_off)

xen/common/device-tree/dt-overlay.c static unsigned int overlay_node_count(const void *overlay_fdt) · line 336

Quoted 10 lines
xen/common/device-tree/dt-overlay.c +10 -0
+    {
+        const char *val, *label, *p;
+        int val_len;
+
+        val = fdt_getprop_by_offset(fdto, property, &label, &val_len);
+        if ( !val )
+            continue;
+
+        /* Match entries of the form "/<fragment_name>:target:0" */
+        for ( p = val; p < (val + val_len); p += (strlen(p) + 1) )

what guarantees us that p will be null terminated, if a malformed overlay is passed this strlen can read past the string, we can use strnlen having as upper bound a counter=val_len? decreasing counter each iteration.

Quoted 1 line

Or do you think it can never happen?

In theory it can happen, in practice this is something not usually taken into account. But we can surely stay on the defensive side and do a single check right after fdt_getprop_by_offset to catch not-NUL terminated stringlist:
if ( !val || !val_len || val[val_len - 1] != '\0' )

In case of no other remarks, I'd do that on commit.

~Michal

#19 lore raw

Re: [PATCH 5/6] xen/dt-overlay: support phandle-based targeting in overlay_get_nodes_info

Quoted 62 lines
Quoted 62 lines
Quoted 47 lines
xen/common/device-tree/dt-overlay.c +38 -0
index d3d4669718ac..a0dee7edb7e5 100644
@@ -286,6 +286,63 @@ static unsigned int overlay_node_count(const void *overlay_fdt)
   return num_overlay_nodes;
 }
 
+/*
+ * Resolve the target path for an overlay fragment.
+ *
+ * This is called before fdt_overlay_apply(), so phandle-based targets
+ * (target = <&label>) are still unresolved (compiled as 0xffffffff by DTC).
+ * Handle the two cases that actually occur:
+ *  - target-path property: the path string is used directly,
+ *  - target = <&label>: the label is looked up in the overlay's __fixups__
+ *    node, then resolved to a path via the base DTB's __symbols__ node.
+ *
+ * Returns a pointer into the FDT on success, NULL on failure.
+ */
+static const char *overlay_get_target_path(const void *fdt, const void *fdto,
+                                           int fragment)
+{
+    const char *path, *fragment_name;
+    int fixups_off, symbols_off, property;
+    int fragment_name_len;
+
+    /* Try target-path first (string-based targeting) */
+    path = fdt_getprop(fdto, fragment, "target-path", NULL);
+    if ( path )
+        return path;
+
+    /* Phandle-based target: resolve via __fixups__ and __symbols__ */
+    fixups_off = fdt_path_offset(fdto, "/__fixups__");
+    if ( fixups_off < 0 )
+        return NULL;
+
+    symbols_off = fdt_path_offset(fdt, "/__symbols__");
+    if ( symbols_off < 0 )
+        return NULL;
+
+    fragment_name = fdt_get_name(fdto, fragment, &fragment_name_len);
+    if ( !fragment_name )
+        return NULL;
+
+    fdt_for_each_property_offset(property, fdto, fixups_off)

xen/common/device-tree/dt-overlay.c static unsigned int overlay_node_count(const void *overlay_fdt) · line 336

Quoted 10 lines
xen/common/device-tree/dt-overlay.c +10 -0
+    {
+        const char *val, *label, *p;
+        int val_len;
+
+        val = fdt_getprop_by_offset(fdto, property, &label, &val_len);
+        if ( !val )
+            continue;
+
+        /* Match entries of the form "/<fragment_name>:target:0" */
+        for ( p = val; p < (val + val_len); p += (strlen(p) + 1) )

what guarantees us that p will be null terminated, if a malformed overlay is passed this strlen can read past the string, we can use strnlen having as upper bound a counter=val_len? decreasing counter each iteration.

Quoted 5 lines
Quoted 1 line

Or do you think it can never happen?

In theory it can happen, in practice this is something not usually taken into account. But we can surely stay on the defensive side and do a single check right after fdt_getprop_by_offset to catch not-NUL terminated stringlist:
if ( !val || !val_len || val[val_len - 1] != '\0' )

Ok, do we also want to print a warning in case of malformed properties instead of ignoring and continue?

Quoted 5 lines

In case of no other remarks, I'd do that on commit.

~Michal

Cheers,
Luca

#20 lore raw

Re: [PATCH 5/6] xen/dt-overlay: support phandle-based targeting in overlay_get_nodes_info

Quoted 62 lines · Luca Fancellu
Quoted 62 lines
Quoted 62 lines
Quoted 47 lines
xen/common/device-tree/dt-overlay.c +38 -0
index d3d4669718ac..a0dee7edb7e5 100644
@@ -286,6 +286,63 @@ static unsigned int overlay_node_count(const void *overlay_fdt)
   return num_overlay_nodes;
 }
 
+/*
+ * Resolve the target path for an overlay fragment.
+ *
+ * This is called before fdt_overlay_apply(), so phandle-based targets
+ * (target = <&label>) are still unresolved (compiled as 0xffffffff by DTC).
+ * Handle the two cases that actually occur:
+ *  - target-path property: the path string is used directly,
+ *  - target = <&label>: the label is looked up in the overlay's __fixups__
+ *    node, then resolved to a path via the base DTB's __symbols__ node.
+ *
+ * Returns a pointer into the FDT on success, NULL on failure.
+ */
+static const char *overlay_get_target_path(const void *fdt, const void *fdto,
+                                           int fragment)
+{
+    const char *path, *fragment_name;
+    int fixups_off, symbols_off, property;
+    int fragment_name_len;
+
+    /* Try target-path first (string-based targeting) */
+    path = fdt_getprop(fdto, fragment, "target-path", NULL);
+    if ( path )
+        return path;
+
+    /* Phandle-based target: resolve via __fixups__ and __symbols__ */
+    fixups_off = fdt_path_offset(fdto, "/__fixups__");
+    if ( fixups_off < 0 )
+        return NULL;
+
+    symbols_off = fdt_path_offset(fdt, "/__symbols__");
+    if ( symbols_off < 0 )
+        return NULL;
+
+    fragment_name = fdt_get_name(fdto, fragment, &fragment_name_len);
+    if ( !fragment_name )
+        return NULL;
+
+    fdt_for_each_property_offset(property, fdto, fixups_off)

xen/common/device-tree/dt-overlay.c static unsigned int overlay_node_count(const void *overlay_fdt) · line 336

Quoted 10 lines
xen/common/device-tree/dt-overlay.c +10 -0
+    {
+        const char *val, *label, *p;
+        int val_len;
+
+        val = fdt_getprop_by_offset(fdto, property, &label, &val_len);
+        if ( !val )
+            continue;
+
+        /* Match entries of the form "/<fragment_name>:target:0" */
+        for ( p = val; p < (val + val_len); p += (strlen(p) + 1) )

what guarantees us that p will be null terminated, if a malformed overlay is passed this strlen can read past the string, we can use strnlen having as upper bound a counter=val_len? decreasing counter each iteration.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Or do you think it can never happen?

In theory it can happen, in practice this is something not usually taken into account. But we can surely stay on the defensive side and do a single check right after fdt_getprop_by_offset to catch not-NUL terminated stringlist:
if ( !val || !val_len || val[val_len - 1] != '\0' )

Ok, do we also want to print a warning in case of malformed properties instead of ignoring and continue?

That's really a matter of taste. I would not do it unless you really want to.

~Michal

Reviewed-by
#21 lore raw

Re: [PATCH 5/6] xen/dt-overlay: support phandle-based targeting in overlay_get_nodes_info

Quoted 70 lines · Michal
Quoted 63 lines · Luca Fancellu
Quoted 63 lines
Quoted 63 lines
Quoted 47 lines
xen/common/device-tree/dt-overlay.c +38 -0
index d3d4669718ac..a0dee7edb7e5 100644
@@ -286,6 +286,63 @@ static unsigned int overlay_node_count(const void *overlay_fdt)
  return num_overlay_nodes;
 }
 
+/*
+ * Resolve the target path for an overlay fragment.
+ *
+ * This is called before fdt_overlay_apply(), so phandle-based targets
+ * (target = <&label>) are still unresolved (compiled as 0xffffffff by DTC).
+ * Handle the two cases that actually occur:
+ *  - target-path property: the path string is used directly,
+ *  - target = <&label>: the label is looked up in the overlay's __fixups__
+ *    node, then resolved to a path via the base DTB's __symbols__ node.
+ *
+ * Returns a pointer into the FDT on success, NULL on failure.
+ */
+static const char *overlay_get_target_path(const void *fdt, const void *fdto,
+                                           int fragment)
+{
+    const char *path, *fragment_name;
+    int fixups_off, symbols_off, property;
+    int fragment_name_len;
+
+    /* Try target-path first (string-based targeting) */
+    path = fdt_getprop(fdto, fragment, "target-path", NULL);
+    if ( path )
+        return path;
+
+    /* Phandle-based target: resolve via __fixups__ and __symbols__ */
+    fixups_off = fdt_path_offset(fdto, "/__fixups__");
+    if ( fixups_off < 0 )
+        return NULL;
+
+    symbols_off = fdt_path_offset(fdt, "/__symbols__");
+    if ( symbols_off < 0 )
+        return NULL;
+
+    fragment_name = fdt_get_name(fdto, fragment, &fragment_name_len);
+    if ( !fragment_name )
+        return NULL;
+
+    fdt_for_each_property_offset(property, fdto, fixups_off)

xen/common/device-tree/dt-overlay.c static unsigned int overlay_node_count(const void *overlay_fdt) · line 336

Quoted 10 lines
xen/common/device-tree/dt-overlay.c +10 -0
+    {
+        const char *val, *label, *p;
+        int val_len;
+
+        val = fdt_getprop_by_offset(fdto, property, &label, &val_len);
+        if ( !val )
+            continue;
+
+        /* Match entries of the form "/<fragment_name>:target:0" */
+        for ( p = val; p < (val + val_len); p += (strlen(p) + 1) )

what guarantees us that p will be null terminated, if a malformed overlay is passed this strlen can read past the string, we can use strnlen having as upper bound a counter=val_len? decreasing counter each iteration.

Or do you think it can never happen?

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines

In theory it can happen, in practice this is something not usually taken into account. But we can surely stay on the defensive side and do a single check right after fdt_getprop_by_offset to catch not-NUL terminated stringlist:
if ( !val || !val_len || val[val_len - 1] != '\0' )

Ok, do we also want to print a warning in case of malformed properties instead of ignoring and continue?

That's really a matter of taste. I would not do it unless you really want to.

I’ll leave this to the other maintainer, with the fix above:

Cheers,
Luca

#22 lore raw

Re: [PATCH 5/6] xen/dt-overlay: support phandle-based targeting in overlay_get_nodes_info

Quoted 7 lines · Luca Fancellu
Quoted 3 lines
Quoted 2 lines

Ok, do we also want to print a warning in case of malformed properties instead of ignoring and continue?

That's really a matter of taste. I would not do it unless you really want to.

I’ll leave this to the other maintainer, with the fix above:

#23 lore raw

PATCH 6/6 xen/dt-overlay: attach resources for child nodes in overlay

In reply to Michal Orzel

handle_attach_overlay_nodes() only calls handle_device() on the top-level overlay nodes tracked in entry->nodes_address[]. Child nodes that have their own reg, interrupts, or iommus properties are never processed, so their IRQs are not routed, MMIO regions are not mapped, and IOMMU setup is skipped.

Introduce handle_device_and_children() which recursively walks the subtree rooted at each overlay node and calls handle_device() on every descendant, ensuring all resources in the overlay are properly attached.

Note that the attach error path has a pre-existing bug: on partial failure, the tracking rangesets are destroyed without first revoking the IRQ/MMIO permissions and IOMMU assignments that were already granted by the successful handle_device() calls. Add a TODO comment to flag this.

---
 xen/common/device-tree/dt-overlay.c | 34 +++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
xen/common/device-tree/dt-overlay.c +32 -2
index a0dee7edb7e5..b837f1112188 100644
@@ -894,6 +894,30 @@ static long handle_add_overlay_nodes(void *overlay_fdt,
     return rc;
 }
 
+static int handle_device_and_children(struct domain *d,
+                                      struct dt_device_node *dev,
+                                      p2m_type_t p2mt,
+                                      struct rangeset *iomem_ranges,
+                                      struct rangeset *irq_ranges)
+{
+    int rc;
+    struct dt_device_node *child;
+
+    rc = handle_device(d, dev, p2mt, iomem_ranges, irq_ranges);
+    if ( rc )
+        return rc;
+
+    dt_for_each_child_node(dev, child)
+    {
+        rc = handle_device_and_children(d, child, p2mt,
+                                        iomem_ranges, irq_ranges);
+        if ( rc )
+            return rc;
+    }
+
+    return 0;
+}
+
 static long handle_attach_overlay_nodes(struct domain *d,
                                         const void *overlay_fdt,
                                         uint32_t overlay_fdt_size)
@@ -951,8 +975,9 @@ static long handle_attach_overlay_nodes(struct domain *d,
         }
 
         write_lock(&dt_host_lock);
-        rc = handle_device(d, overlay_node, p2m_mmio_direct_c,
-                           entry->iomem_ranges, entry->irq_ranges);
+        rc = handle_device_and_children(d, overlay_node, p2m_mmio_direct_c,
+                                        entry->iomem_ranges,
+                                        entry->irq_ranges);
         write_unlock(&dt_host_lock);
         if ( rc )
         {
@@ -968,6 +993,11 @@ static long handle_attach_overlay_nodes(struct domain *d,
  out:
     spin_unlock(&overlay_lock);
 
+    /*
+     * TODO: IRQ/MMIO permissions and IOMMU assignments granted by
+     * handle_device() before the failure are not revoked here.  We only
+     * destroy the tracking rangesets, leaking the actual grants.
+     */
     if ( entry )
     {
         rangeset_destroy(entry->irq_ranges);
Signature · 1 line
2.43.0
Reviewed-by on v1 6/6
#24 lore raw

Re: [PATCH 6/6] xen/dt-overlay: attach resources for child nodes in overlay

Hi Michal,

Quoted 12 lines · Michal Orzel

handle_attach_overlay_nodes() only calls handle_device() on the top-level overlay nodes tracked in entry->nodes_address[]. Child nodes that have their own reg, interrupts, or iommus properties are never processed, so their IRQs are not routed, MMIO regions are not mapped, and IOMMU setup is skipped.

Introduce handle_device_and_children() which recursively walks the subtree rooted at each overlay node and calls handle_device() on every descendant, ensuring all resources in the overlay are properly attached.

Quoted 8 lines

Note that the attach error path has a pre-existing bug: on partial failure, the tracking rangesets are destroyed without first revoking the IRQ/MMIO permissions and IOMMU assignments that were already granted by the successful handle_device() calls. Add a TODO comment to flag this.

---

Looks ok to me

Cheers,
Luca

#25 lore raw

Re: [PATCH 6/6] xen/dt-overlay: attach resources for child nodes in overlay

Quoted 19 lines · Luca Fancellu

Hi Michal,

Quoted 17 lines · Michal Orzel

handle_attach_overlay_nodes() only calls handle_device() on the top-level overlay nodes tracked in entry->nodes_address[]. Child nodes that have their own reg, interrupts, or iommus properties are never processed, so their IRQs are not routed, MMIO regions are not mapped, and IOMMU setup is skipped.

Introduce handle_device_and_children() which recursively walks the subtree rooted at each overlay node and calls handle_device() on every descendant, ensuring all resources in the overlay are properly attached.

Note that the attach error path has a pre-existing bug: on partial failure, the tracking rangesets are destroyed without first revoking the IRQ/MMIO permissions and IOMMU assignments that were already granted by the successful handle_device() calls. Add a TODO comment to flag this.

Quoted 7 lines
Quoted 3 lines

---

Looks ok to me