[X,1/1] UBUNTU: SAUCE: PM / hibernate: memory_bm_find_bit -- tighten node optimisation
diff mbox series

Message ID 20191007163550.20548-2-andrea.righi@canonical.com
State New
Headers show
Series
  • PM / hibernate: fix potential memory corruption
Related show

Commit Message

Andrea Righi Oct. 7, 2019, 4:35 p.m. UTC
From: Andy Whitcroft <apw@canonical.com>

BugLink: https://bugs.launchpad.net/bugs/1847118

When looking for a bit by number we make use of the cached result from the
preceding lookup to speed up operation.  Firstly we check if the requested
pfn is within the cached zone and if not lookup the new zone.  We then
check if the offset for that pfn falls within the existing cached node.
This happens regardless of whether the node is within the zone we are
now scanning.  With certain memory layouts it is possible for this to
false trigger creating a temporary alias for the pfn to a different bit.
This leads the hibernation code to free memory which it was never allocated
with the expected fallout.

Ensure the zone we are scanning matches the cached zone before considering
the cached node.

Deep thanks go to Andrea for many, many, many hours of hacking and testing
that went into cornering this bug.

Reported-by: Andrea Righi <andrea.righi@canonical.com>
Tested-by: Andrea Righi <andrea.righi@canonical.com>
Signed-off-by: Andy Whitcroft <apw@canonical.com>
---
 kernel/power/snapshot.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Patch
diff mbox series

diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c
index f155c62..334191d 100644
--- a/kernel/power/snapshot.c
+++ b/kernel/power/snapshot.c
@@ -662,8 +662,14 @@  zone_found:
 	 * node for our pfn.
 	 */
 
+	/*
+	 * If the zone we wish to scan is the the current zone and the
+	 * pfn falls into the current node then we do not need to walk
+	 * the tree.
+	 */
 	node = bm->cur.node;
-	if (((pfn - zone->start_pfn) & ~BM_BLOCK_MASK) == bm->cur.node_pfn)
+	if (zone == bm->cur.zone &&
+	    ((pfn - zone->start_pfn) & ~BM_BLOCK_MASK) == bm->cur.node_pfn)
 		goto node_found;
 
 	node      = zone->rtree;