Skip to content

Commit b75cd21

Browse files
Oleksandr Tyshchenkojgross1
authored andcommitted
xen/arm: Fix race in RB-tree based P2M accounting
During the PV driver life cycle the mappings are added to the RB-tree by set_foreign_p2m_mapping(), which is called from gnttab_map_refs() and are removed by clear_foreign_p2m_mapping() which is called from gnttab_unmap_refs(). As both functions end up calling __set_phys_to_machine_multi() which updates the RB-tree, this function can be called concurrently. There is already a "p2m_lock" to protect against concurrent accesses, but the problem is that the first read of "phys_to_mach.rb_node" in __set_phys_to_machine_multi() is not covered by it, so this might lead to the incorrect mappings update (removing in our case) in RB-tree. In my environment the related issue happens rarely and only when PV net backend is running, the xen_add_phys_to_mach_entry() claims that it cannot add new pfn <-> mfn mapping to the tree since it is already exists which results in a failure when mapping foreign pages. But there might be other bad consequences related to the non-protected root reads such use-after-free, etc. While at it, also fix the similar usage in __pfn_to_mfn(), so initialize "struct rb_node *n" with the "p2m_lock" held in both functions to avoid possible bad consequences. This is CVE-2022-33744 / XSA-406. Signed-off-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com> Reviewed-by: Stefano Stabellini <sstabellini@kernel.org> Signed-off-by: Juergen Gross <jgross@suse.com>
1 parent f63c2c2 commit b75cd21

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

arch/arm/xen/p2m.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@ static int xen_add_phys_to_mach_entry(struct xen_p2m_entry *new)
6363

6464
unsigned long __pfn_to_mfn(unsigned long pfn)
6565
{
66-
struct rb_node *n = phys_to_mach.rb_node;
66+
struct rb_node *n;
6767
struct xen_p2m_entry *entry;
6868
unsigned long irqflags;
6969

7070
read_lock_irqsave(&p2m_lock, irqflags);
71+
n = phys_to_mach.rb_node;
7172
while (n) {
7273
entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys);
7374
if (entry->pfn <= pfn &&
@@ -152,10 +153,11 @@ bool __set_phys_to_machine_multi(unsigned long pfn,
152153
int rc;
153154
unsigned long irqflags;
154155
struct xen_p2m_entry *p2m_entry;
155-
struct rb_node *n = phys_to_mach.rb_node;
156+
struct rb_node *n;
156157

157158
if (mfn == INVALID_P2M_ENTRY) {
158159
write_lock_irqsave(&p2m_lock, irqflags);
160+
n = phys_to_mach.rb_node;
159161
while (n) {
160162
p2m_entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys);
161163
if (p2m_entry->pfn <= pfn &&

0 commit comments

Comments
 (0)