mm/migrate: snapshot the source nid before the misplaced migration
Subsystems (2)
PATCH v2 1/1 mm/migrate: snapshot the source nid before the misplaced migration
A successful migration frees the source folio before the promotion counter update, so the folio_nid() read there operates on a freed folio — which may already have been reallocated. It happens to work today, as Zi explained:
: Strictly speaking, it can be seen as a benign UAF. It works, because : 1. folio->flags happens to be the same as page->flags,
: 2. folio/page nid is stored in the ->flags and never changed. :
: In memdesc world, after a folio is freed, I assume folio->flags would be : zeroed since struct folio is freed and calling folio_nid() on a freed : folio would give a bogus result (yeah, it can be mitigated by not : zeroing node field and each node has its own struct folio pool, but that : makes folio_nid() implementation dependent).
and Matthew explained further:
: once struct folio is separately allocated, it's a UAF. It could be : recycled to a different NID or maybe the slab containing the folio could : be freed (assuming we're not holding the RCU read lock at this point in : migration, and I don't see why we would be).
A separate concern is memory hot-removal. With memory-tiering mode enabled, successful migration can release the source before the promotion-accounting folio_nid() read. If the task is preempted there, an otherwise-free source block can be offlined and removed first. On the x86 path, removal can unmap the source's vmemmap, so the later read can fault. The snapshot avoids that read.
--- mm/migrate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
---
v1->v2: improve the commit log, thanks to Zi, Matthew and Andrew.
mm/migrate.c +2 -1
@@ -2757,6 +2757,7 @@ int migrate_misplaced_folio(struct folio *folio, int node)pg_data_t *pgdat =NODE_DATA (node);int nr_remaining;unsigned int nr_succeeded; +int src_node =folio_nid (folio);LIST_HEAD (migratepages);struct mem_cgroup *memcg =get_mem_cgroup_from_folio (folio);struct lruvec *lruvec =mem_cgroup_lruvec (memcg, pgdat);
@@ -2771,7 +2772,7 @@ int migrate_misplaced_folio(struct folio *folio, int node)count_vm_numa_events (NUMA_PAGE_MIGRATE , nr_succeeded);count_memcg_events (memcg,NUMA_PAGE_MIGRATE , nr_succeeded);if ((sysctl_numa_balancing_mode &NUMA_BALANCING_MEMORY_TIERING ) - && !node_is_toptier (folio_nid (folio)) + && !node_is_toptier (src_node) &&node_is_toptier (node))mod_lruvec_state (lruvec,PGPROMOTE_SUCCESS , nr_succeeded); }
Signature · 1 line
2.52.0
Quoted 35 lines · Yafang Shao
A successful migration frees the source folio before the promotion counter update, so the
folio_nid()read there operates on a freed folio — which may already have been reallocated. It happens to work today, as Zi explained:: Strictly speaking, it can be seen as a benign UAF. It works, because : 1.
folio->flagshappens to be the same aspage->flags,
: 2. folio/page nid is stored in the ->flags and never changed. :
: In memdesc world, after a folio is freed, I assumefolio->flagswould be : zeroed since struct folio is freed and callingfolio_nid()on a freed : folio would give a bogus result (yeah, it can be mitigated by not : zeroing node field and each node has its own struct folio pool, but that : makesfolio_nid()implementation dependent).and Matthew explained further:
: once struct folio is separately allocated, it's a UAF. It could be : recycled to a different NID or maybe the slab containing the folio could : be freed (assuming we're not holding the RCU read lock at this point in : migration, and I don't see why we would be).
A separate concern is memory hot-removal. With memory-tiering mode enabled, successful migration can release the source before the promotion-accounting
folio_nid()read. If the task is preempted there, an otherwise-free source block can be offlined and removed first. On the x86 path, removal can unmap the source's vmemmap, so the later read can fault. The snapshot avoids that read.Fixes e39bb6be9f2b ("NUMA Balancing: add page promotion counter")Suggested-by Zi Yan <[email protected]>Suggested-by Andrew Morton <[email protected]>Signed-off-by Yafang Shao <[email protected]>Cc Ying Huang <[email protected]>Cc Matthew Wilcox <[email protected]>
Quoted 8 lines
--- mm/migrate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)---
v1->v2: improve the commit log, thanks to Zi, Matthew and Andrew.
LGTM. Thanks.
Best Regards,
Yan, Zi
Quoted 48 lines · Yafang Shao
A successful migration frees the source folio before the promotion counter update, so the
folio_nid()read there operates on a freed folio — which may already have been reallocated. It happens to work today, as Zi explained:: Strictly speaking, it can be seen as a benign UAF. It works, because : 1.
folio->flagshappens to be the same aspage->flags,
: 2. folio/page nid is stored in the ->flags and never changed. :
: In memdesc world, after a folio is freed, I assumefolio->flagswould be : zeroed since struct folio is freed and callingfolio_nid()on a freed : folio would give a bogus result (yeah, it can be mitigated by not : zeroing node field and each node has its own struct folio pool, but that : makesfolio_nid()implementation dependent).and Matthew explained further:
: once struct folio is separately allocated, it's a UAF. It could be : recycled to a different NID or maybe the slab containing the folio could : be freed (assuming we're not holding the RCU read lock at this point in : migration, and I don't see why we would be).
A separate concern is memory hot-removal. With memory-tiering mode enabled, successful migration can release the source before the promotion-accounting
folio_nid()read. If the task is preempted there, an otherwise-free source block can be offlined and removed first. On the x86 path, removal can unmap the source's vmemmap, so the later read can fault. The snapshot avoids that read.Fixes e39bb6be9f2b ("NUMA Balancing: add page promotion counter")Suggested-by Zi Yan <[email protected]>Suggested-by Andrew Morton <[email protected]>Signed-off-by Yafang Shao <[email protected]>Cc Ying Huang <[email protected]>Cc Matthew Wilcox <[email protected]>--- mm/migrate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)---
v1->v2: improve the commit log, thanks to Zi, Matthew and Andrew.mm/migrate.c +0
-0
mm/migrate.c int migrate_misplaced_folio(struct folio *folio, int node) · line 2760
Quoted 4 lines
mm/migrate.c +1
-0@@ -2757,6 +2757,7 @@ int migrate_misplaced_folio(struct folio *folio, int node)pg_data_t *pgdat =NODE_DATA (node);int nr_remaining;unsigned int nr_succeeded; +int src_node =folio_nid (folio);
can be const.
Signature · 3 lines
Cheers, David