[RFC v2 14/43] PKRAM: prevent inadvertent use of a stale superblock

From: Anthony Yznaga
Date: Tue Mar 30 2021 - 17:29:00 EST


When pages have been saved to be preserved by the current boot, set
a magic number on the super block to be validated by the next kernel.

Signed-off-by: Anthony Yznaga <anthony.yznaga@xxxxxxxxxx>
---
mm/pkram.c | 9 +++++++++
1 file changed, 9 insertions(+)

diff --git a/mm/pkram.c b/mm/pkram.c
index dab6657080bf..8670d1633a9d 100644
--- a/mm/pkram.c
+++ b/mm/pkram.c
@@ -22,6 +22,7 @@

#include "internal.h"

+#define PKRAM_MAGIC 0x706B726D

/*
* Represents a reference to a data page saved to PKRAM.
@@ -112,6 +113,8 @@ struct pkram_region_list {
* The structure occupies a memory page.
*/
struct pkram_super_block {
+ __u32 magic;
+
__u64 node_pfn; /* first element of the node list */
__u64 region_list_pfn;
__u64 nr_regions;
@@ -180,6 +183,11 @@ void __init pkram_reserve(void)
err = PTR_ERR(pkram_sb);
goto out;
}
+ if (pkram_sb->magic != PKRAM_MAGIC) {
+ pr_err("PKRAM: invalid super block\n");
+ err = -EINVAL;
+ goto out;
+ }
/* An empty pkram_sb is not an error */
if (!pkram_sb->node_pfn) {
pkram_sb = NULL;
@@ -993,6 +1001,7 @@ static void __pkram_reboot(void)
*/
memset(pkram_sb, 0, PAGE_SIZE);
if (!err && node_pfn) {
+ pkram_sb->magic = PKRAM_MAGIC;
pkram_sb->node_pfn = node_pfn;
pkram_sb->region_list_pfn = rl_pfn;
pkram_sb->nr_regions = nr_regions;
--
1.8.3.1