* [PATCH v2 1/4] PM: hibernate: add seed-wrapped encrypted snapshots
2026-08-04 10:14 [PATCH v2 0/4] PM: hibernate: encrypted snapshots under lockdown Sean Rhodes
@ 2026-08-04 10:14 ` Sean Rhodes
2026-08-04 10:14 ` [PATCH v2 2/4] PM: hibernate: permit encrypted snapshot device under lockdown Sean Rhodes
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Sean Rhodes @ 2026-08-04 10:14 UTC (permalink / raw)
To: linux-pm
Cc: Rafael J. Wysocki, Len Brown, Pavel Machek, Sean Rhodes,
Evan Green, linux-kernel, Xueqin Luo, Jens Axboe, Andrew Morton,
Chris Li, Kairui Song, Kemeng Shi, Nhat Pham, Baoquan He,
Barry Song, Youngjun Park, linux-block, linux-mm
From: Evan Green <evgreen@chromium.org>
Encrypt and authenticate uswsusp hibernation images in the kernel so
userspace cannot tamper with the image that will be restored as kernel
memory.
The image data is protected with gcm(aes) in 16-page chunks.
SNAPSHOT_ENABLE_ENCRYPTION enables the encrypted read/write paths and
returns an opaque wrapped image-key blob with the starting nonce on
suspend. On resume, userspace provides the same blob and nonce after
trusted early userspace has written the same 32-byte seed to
/sys/power/snapshot_seed.
The plaintext image key is generated and unwrapped inside the kernel. The
wrapping key is derived from the locked seed, leaving TPM PCR policy and
seed unsealing to early userspace rather than adding TPM or trusted-key
state to PM code.
SNAPSHOT_SET_USER_KEY lets userspace fold extra key material into the
data encryption key after the metadata area has been read or written.
Tested on StarFighter MTL with a TPM-sealed seed: encrypted uswsusp
suspend/resume completed and restored the image.
Signed-off-by: Evan Green <evgreen@chromium.org>
Co-developed-by: Sean Rhodes <sean@starlabs.systems>
Signed-off-by: Sean Rhodes <sean@starlabs.systems>
---
Documentation/power/userland-swsusp.rst | 16 +
include/uapi/linux/suspend_ioctls.h | 31 +-
kernel/power/Kconfig | 15 +
kernel/power/Makefile | 1 +
kernel/power/hibernate.c | 24 +
kernel/power/power.h | 1 +
kernel/power/snapenc.c | 878 ++++++++++++++++++++++++
kernel/power/snapshot.c | 5 +
kernel/power/user.c | 58 +-
kernel/power/user.h | 132 ++++
10 files changed, 1143 insertions(+), 18 deletions(-)
create mode 100644 kernel/power/snapenc.c
create mode 100644 kernel/power/user.h
diff --git a/Documentation/power/userland-swsusp.rst b/Documentation/power/userland-swsusp.rst
index 1cf62d80a9ca..282e01d2fe61 100644
--- a/Documentation/power/userland-swsusp.rst
+++ b/Documentation/power/userland-swsusp.rst
@@ -115,6 +115,22 @@ SNAPSHOT_S2RAM
to resume the system from RAM if there's enough battery power or restore
its state on the basis of the saved suspend image otherwise)
+SNAPSHOT_ENABLE_ENCRYPTION
+ Enables encryption of the hibernate image within the kernel. Upon suspend
+ (ie when the snapshot device was opened for reading), returns a blob
+ representing the random encryption key the kernel created to encrypt the
+ hibernate image with. Upon resume (ie when the snapshot device was opened
+ for writing), receives a blob from usermode containing the key material
+ previously returned during hibernate.
+
+SNAPSHOT_SET_USER_KEY
+ Mixes additional user key material into the data portion of an encrypted
+ hibernate image. The ioctl argument points to struct uswsusp_user_key.
+ key_len must be between 8 and USWSUSP_USER_KEY_SIZE bytes, and reserved
+ must be zero. The kernel writes meta_size with the encrypted metadata
+ size that userspace may transfer before providing the user key during
+ resume.
+
The device's read() operation can be used to transfer the snapshot image from
the kernel. It has the following limitations:
diff --git a/include/uapi/linux/suspend_ioctls.h b/include/uapi/linux/suspend_ioctls.h
index bcce04e21c0d..2615dbd03404 100644
--- a/include/uapi/linux/suspend_ioctls.h
+++ b/include/uapi/linux/suspend_ioctls.h
@@ -13,6 +13,31 @@ struct resume_swap_area {
__u32 dev;
} __attribute__((packed));
+#define USWSUSP_KEY_NONCE_SIZE 16
+#define USWSUSP_USER_KEY_SIZE 32
+
+/*
+ * This structure is used to pass the opaque wrapped hibernate image
+ * encryption key and starting nonce in either direction.
+ */
+struct uswsusp_key_blob {
+ __u32 blob_len;
+ __u8 blob[512];
+ __u8 nonce[USWSUSP_KEY_NONCE_SIZE] __kernel_nonstring;
+} __attribute__((packed));
+
+/*
+ * Allow user mode to fold in key material for the data portion of the hibernate
+ * image.
+ */
+struct uswsusp_user_key {
+ /* Kernel returns the metadata size. */
+ __u64 meta_size;
+ __u32 key_len;
+ __u32 reserved;
+ __u8 key[USWSUSP_USER_KEY_SIZE] __kernel_nonstring;
+} __attribute__((packed));
+
#define SNAPSHOT_IOC_MAGIC '3'
#define SNAPSHOT_FREEZE _IO(SNAPSHOT_IOC_MAGIC, 1)
#define SNAPSHOT_UNFREEZE _IO(SNAPSHOT_IOC_MAGIC, 2)
@@ -29,6 +54,10 @@ struct resume_swap_area {
#define SNAPSHOT_PREF_IMAGE_SIZE _IO(SNAPSHOT_IOC_MAGIC, 18)
#define SNAPSHOT_AVAIL_SWAP_SIZE _IOR(SNAPSHOT_IOC_MAGIC, 19, __kernel_loff_t)
#define SNAPSHOT_ALLOC_SWAP_PAGE _IOR(SNAPSHOT_IOC_MAGIC, 20, __kernel_loff_t)
-#define SNAPSHOT_IOC_MAXNR 20
+#define SNAPSHOT_ENABLE_ENCRYPTION _IOWR(SNAPSHOT_IOC_MAGIC, 21, \
+ struct uswsusp_key_blob)
+#define SNAPSHOT_SET_USER_KEY _IOWR(SNAPSHOT_IOC_MAGIC, 22, \
+ struct uswsusp_user_key)
+#define SNAPSHOT_IOC_MAXNR 22
#endif /* _LINUX_SUSPEND_IOCTLS_H */
diff --git a/kernel/power/Kconfig b/kernel/power/Kconfig
index 530c897311d4..a9bee71571cf 100644
--- a/kernel/power/Kconfig
+++ b/kernel/power/Kconfig
@@ -115,6 +115,21 @@ config HIBERNATION_DEF_COMP
help
Default compressor to be used for hibernation.
+config ENCRYPTED_HIBERNATION
+ bool "Encryption support for userspace snapshots"
+ depends on HIBERNATION_SNAPSHOT_DEV
+ select CRYPTO_AES
+ select CRYPTO_GCM
+ select CRYPTO_LIB_SHA256
+ help
+ Enable support for kernel-based encryption of hibernation snapshots
+ created by uswsusp tools. A trusted early userspace component must
+ provide the snapshot encryption seed before enabling encryption.
+
+ Say N if userspace handles the image encryption.
+
+ If in doubt, say N.
+
config PM_STD_PARTITION
string "Default resume partition"
depends on HIBERNATION
diff --git a/kernel/power/Makefile b/kernel/power/Makefile
index 773e2789412b..2fd31b2a250f 100644
--- a/kernel/power/Makefile
+++ b/kernel/power/Makefile
@@ -16,6 +16,7 @@ obj-$(CONFIG_SUSPEND) += suspend.o
obj-$(CONFIG_PM_TEST_SUSPEND) += suspend_test.o
obj-$(CONFIG_HIBERNATION) += hibernate.o snapshot.o swap.o
obj-$(CONFIG_HIBERNATION_SNAPSHOT_DEV) += user.o
+obj-$(CONFIG_ENCRYPTED_HIBERNATION) += snapenc.o
obj-$(CONFIG_PM_AUTOSLEEP) += autosleep.o
obj-$(CONFIG_PM_WAKELOCKS) += wakelock.o
diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
index d2479c69d71a..a95cb447a13a 100644
--- a/kernel/power/hibernate.c
+++ b/kernel/power/hibernate.c
@@ -36,6 +36,7 @@
#include <trace/events/power.h>
#include "power.h"
+#include "user.h"
static int nocompress;
@@ -1376,12 +1377,35 @@ static ssize_t reserved_size_store(struct kobject *kobj,
power_attr(reserved_size);
+#ifdef CONFIG_ENCRYPTED_HIBERNATION
+static ssize_t snapshot_seed_store(struct kobject *kobj,
+ struct kobj_attribute *attr,
+ const char *buf, size_t n)
+{
+ int ret;
+
+ ret = snapshot_store_encryption_seed(buf, n);
+ return ret ? ret : n;
+}
+
+static struct kobj_attribute snapshot_seed_attr = {
+ .attr = {
+ .name = "snapshot_seed",
+ .mode = 0200,
+ },
+ .store = snapshot_seed_store,
+};
+#endif
+
static struct attribute *g[] = {
&disk_attr.attr,
&resume_offset_attr.attr,
&resume_attr.attr,
&image_size_attr.attr,
&reserved_size_attr.attr,
+#ifdef CONFIG_ENCRYPTED_HIBERNATION
+ &snapshot_seed_attr.attr,
+#endif
NULL,
};
diff --git a/kernel/power/power.h b/kernel/power/power.h
index 75b63843886e..e080230f97fc 100644
--- a/kernel/power/power.h
+++ b/kernel/power/power.h
@@ -160,6 +160,7 @@ struct snapshot_handle {
extern unsigned int snapshot_additional_pages(struct zone *zone);
extern unsigned long snapshot_get_image_size(void);
+unsigned long snapshot_get_meta_page_count(void);
extern int snapshot_read_next(struct snapshot_handle *handle);
extern int snapshot_write_next(struct snapshot_handle *handle);
int snapshot_write_finalize(struct snapshot_handle *handle);
diff --git a/kernel/power/snapenc.c b/kernel/power/snapenc.c
new file mode 100644
index 000000000000..e6b2fcad9807
--- /dev/null
+++ b/kernel/power/snapenc.c
@@ -0,0 +1,878 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* This file provides encryption support for system snapshots. */
+
+#include <linux/crypto.h>
+#include <crypto/aead.h>
+#include <crypto/gcm.h>
+#include <crypto/sha2.h>
+#include <linux/hex.h>
+#include <linux/mutex.h>
+#include <linux/random.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/uaccess.h>
+
+#include "power.h"
+#include "user.h"
+
+#define SNAPSHOT_SEED_SIZE SHA256_DIGEST_SIZE
+#define SNAPSHOT_KEY_BLOB_VERSION 1
+
+static const u8 snapshot_key_blob_magic[8] = {
+ 'S', 'W', 'S', 'U', 'S', 'P', 'K', '1',
+};
+
+struct snapshot_wrapped_key {
+ u8 magic[sizeof(snapshot_key_blob_magic)];
+ __le32 version;
+ u8 wrap_nonce[GCM_AES_IV_SIZE] __nonstring;
+ u8 encrypted_key[SNAPSHOT_ENCRYPTION_KEY_SIZE] __nonstring;
+ u8 tag[SNAPSHOT_AUTH_TAG_SIZE] __nonstring;
+} __packed;
+
+static DEFINE_MUTEX(snapshot_seed_mutex);
+static u8 snapshot_seed[SNAPSHOT_SEED_SIZE] __nonstring;
+static bool snapshot_seed_valid;
+
+int snapshot_store_encryption_seed(const char *buf, size_t count)
+{
+ u8 seed[SNAPSHOT_SEED_SIZE] __nonstring;
+ size_t len = count;
+ int ret;
+
+ if (len && buf[len - 1] == '\n')
+ len--;
+ if (len != SNAPSHOT_SEED_SIZE * 2)
+ return -EINVAL;
+
+ ret = hex2bin(seed, buf, sizeof(seed));
+ if (ret)
+ return ret;
+
+ mutex_lock(&snapshot_seed_mutex);
+ if (snapshot_seed_valid) {
+ ret = memcmp(snapshot_seed, seed, sizeof(seed)) ? -EPERM : 0;
+ goto out;
+ }
+
+ memcpy(snapshot_seed, seed, sizeof(seed));
+ snapshot_seed_valid = true;
+ pr_info("PM: hibernate: snapshot encryption seed locked\n");
+
+out:
+ mutex_unlock(&snapshot_seed_mutex);
+ memzero_explicit(seed, sizeof(seed));
+ return ret;
+}
+
+static bool snapshot_copy_encryption_seed(u8 seed[SNAPSHOT_SEED_SIZE])
+{
+ bool valid;
+
+ mutex_lock(&snapshot_seed_mutex);
+ valid = snapshot_seed_valid;
+ if (valid)
+ memcpy(seed, snapshot_seed, SNAPSHOT_SEED_SIZE);
+ mutex_unlock(&snapshot_seed_mutex);
+
+ return valid;
+}
+
+static int snapshot_derive_wrapping_key(u8 key[SNAPSHOT_ENCRYPTION_KEY_SIZE])
+{
+ static const char label[] = "Linux hibernate snapshot key wrap v1";
+ u8 digest[SHA256_DIGEST_SIZE];
+ u8 seed[SNAPSHOT_SEED_SIZE] __nonstring;
+ struct sha256_ctx sha256_ctx;
+
+ if (!snapshot_copy_encryption_seed(seed))
+ return -ENOKEY;
+
+ sha256_init(&sha256_ctx);
+ sha256_update(&sha256_ctx, label, strlen(label));
+ sha256_update(&sha256_ctx, seed, sizeof(seed));
+ sha256_final(&sha256_ctx, digest);
+
+ memcpy(key, digest, SNAPSHOT_ENCRYPTION_KEY_SIZE);
+ memzero_explicit(seed, sizeof(seed));
+ memzero_explicit(digest, sizeof(digest));
+ return 0;
+}
+
+static int snapshot_crypt_wrapped_key(struct snapshot_wrapped_key *wrapped,
+ u8 image_key[SNAPSHOT_ENCRYPTION_KEY_SIZE],
+ bool encrypt)
+{
+ u8 wrap_key[SNAPSHOT_ENCRYPTION_KEY_SIZE] __nonstring;
+ u8 buf[SNAPSHOT_ENCRYPTION_KEY_SIZE + SNAPSHOT_AUTH_TAG_SIZE] __nonstring;
+ struct crypto_aead *tfm;
+ struct aead_request *req;
+ struct scatterlist sg;
+ DECLARE_CRYPTO_WAIT(wait);
+ int rc;
+
+ rc = snapshot_derive_wrapping_key(wrap_key);
+ if (rc)
+ return rc;
+
+ tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
+ if (IS_ERR(tfm)) {
+ rc = PTR_ERR(tfm);
+ goto out_key;
+ }
+
+ rc = crypto_aead_setkey(tfm, wrap_key, sizeof(wrap_key));
+ if (rc)
+ goto out_tfm;
+
+ rc = crypto_aead_setauthsize(tfm, SNAPSHOT_AUTH_TAG_SIZE);
+ if (rc)
+ goto out_tfm;
+
+ req = aead_request_alloc(tfm, GFP_KERNEL);
+ if (!req) {
+ rc = -ENOMEM;
+ goto out_tfm;
+ }
+
+ if (encrypt) {
+ get_random_bytes(wrapped->wrap_nonce, sizeof(wrapped->wrap_nonce));
+ memcpy(buf, image_key, SNAPSHOT_ENCRYPTION_KEY_SIZE);
+ } else {
+ memcpy(buf, wrapped->encrypted_key, SNAPSHOT_ENCRYPTION_KEY_SIZE);
+ memcpy(buf + SNAPSHOT_ENCRYPTION_KEY_SIZE, wrapped->tag,
+ SNAPSHOT_AUTH_TAG_SIZE);
+ }
+
+ sg_init_one(&sg, buf, sizeof(buf));
+ aead_request_set_callback(req, 0, crypto_req_done, &wait);
+ aead_request_set_ad(req, 0);
+ aead_request_set_crypt(req, &sg, &sg,
+ encrypt ? SNAPSHOT_ENCRYPTION_KEY_SIZE :
+ sizeof(buf),
+ wrapped->wrap_nonce);
+
+ rc = crypto_wait_req(encrypt ? crypto_aead_encrypt(req) :
+ crypto_aead_decrypt(req),
+ &wait);
+ if (rc)
+ goto out_req;
+
+ if (encrypt) {
+ memcpy(wrapped->encrypted_key, buf, SNAPSHOT_ENCRYPTION_KEY_SIZE);
+ memcpy(wrapped->tag, buf + SNAPSHOT_ENCRYPTION_KEY_SIZE,
+ SNAPSHOT_AUTH_TAG_SIZE);
+ } else {
+ memcpy(image_key, buf, SNAPSHOT_ENCRYPTION_KEY_SIZE);
+ }
+
+out_req:
+ aead_request_free(req);
+out_tfm:
+ crypto_free_aead(tfm);
+out_key:
+ memzero_explicit(buf, sizeof(buf));
+ memzero_explicit(wrap_key, sizeof(wrap_key));
+ return rc;
+}
+
+static int snapshot_wrap_image_key(const u8 image_key[SNAPSHOT_ENCRYPTION_KEY_SIZE],
+ struct snapshot_wrapped_key *wrapped)
+{
+ u8 key[SNAPSHOT_ENCRYPTION_KEY_SIZE] __nonstring;
+ int rc;
+
+ memset(wrapped, 0, sizeof(*wrapped));
+ memcpy(wrapped->magic, snapshot_key_blob_magic, sizeof(wrapped->magic));
+ wrapped->version = cpu_to_le32(SNAPSHOT_KEY_BLOB_VERSION);
+
+ memcpy(key, image_key, sizeof(key));
+ rc = snapshot_crypt_wrapped_key(wrapped, key, true);
+ memzero_explicit(key, sizeof(key));
+ return rc;
+}
+
+static int snapshot_unwrap_image_key(const struct snapshot_wrapped_key *wrapped,
+ u8 image_key[SNAPSHOT_ENCRYPTION_KEY_SIZE])
+{
+ struct snapshot_wrapped_key tmp;
+ int rc;
+
+ if (memcmp(wrapped->magic, snapshot_key_blob_magic,
+ sizeof(snapshot_key_blob_magic)))
+ return -EINVAL;
+ if (le32_to_cpu(wrapped->version) != SNAPSHOT_KEY_BLOB_VERSION)
+ return -EINVAL;
+
+ tmp = *wrapped;
+ rc = snapshot_crypt_wrapped_key(&tmp, image_key, false);
+ memzero_explicit(&tmp, sizeof(tmp));
+ return rc;
+}
+
+static int snapshot_install_image_key(struct snapshot_data *data,
+ const u8 image_key[SNAPSHOT_ENCRYPTION_KEY_SIZE])
+{
+ int rc;
+
+ memcpy(data->encryption_key, image_key, sizeof(data->encryption_key));
+ rc = crypto_aead_setkey(data->aead_tfm, data->encryption_key,
+ sizeof(data->encryption_key));
+ if (rc)
+ memzero_explicit(data->encryption_key,
+ sizeof(data->encryption_key));
+
+ return rc;
+}
+
+/* Derive a key from the kernel and user keys for data encryption. */
+static int snapshot_use_user_key(struct snapshot_data *data)
+{
+ u8 digest[SHA256_DIGEST_SIZE];
+ struct sha256_ctx sha256_ctx;
+ int rc;
+
+ /*
+ * Hash the kernel key and the user key together. This folds in the user
+ * key, but not in a way that gives the user mode predictable control
+ * over the key bits.
+ */
+ sha256_init(&sha256_ctx);
+
+ sha256_update(&sha256_ctx, data->encryption_key,
+ SNAPSHOT_ENCRYPTION_KEY_SIZE);
+ sha256_update(&sha256_ctx, data->user_key, sizeof(data->user_key));
+ sha256_final(&sha256_ctx, digest);
+
+ BUILD_BUG_ON(SNAPSHOT_ENCRYPTION_KEY_SIZE > SHA256_DIGEST_SIZE);
+
+ rc = crypto_aead_setkey(data->aead_tfm,
+ digest,
+ SNAPSHOT_ENCRYPTION_KEY_SIZE);
+ memzero_explicit(digest, sizeof(digest));
+
+ return rc;
+}
+
+/* Check to see if it's time to switch to the user key, and do it if so. */
+static int snapshot_check_user_key_switch(struct snapshot_data *data)
+{
+ if (data->user_key_valid && data->meta_size &&
+ data->crypt_total == data->meta_size) {
+ return snapshot_use_user_key(data);
+ }
+
+ return 0;
+}
+
+/* Encrypt more data from the snapshot into the staging area. */
+static int snapshot_encrypt_refill(struct snapshot_data *data)
+{
+ struct aead_request *req = data->aead_req;
+ u8 nonce[GCM_AES_IV_SIZE];
+ DECLARE_CRYPTO_WAIT(wait);
+ size_t total = 0;
+ int pg_idx;
+ int res;
+
+ if (data->crypt_total == 0) {
+ data->meta_size = snapshot_get_meta_page_count() << PAGE_SHIFT;
+ } else {
+ res = snapshot_check_user_key_switch(data);
+ if (res)
+ return res;
+ }
+
+ /*
+ * The first buffer is the associated data, set to the offset to prevent
+ * attacks that rearrange chunks.
+ */
+ sg_set_buf(&data->sg[0], &data->crypt_total, sizeof(data->crypt_total));
+
+ /* Load the crypt buffer with snapshot pages. */
+ for (pg_idx = 0; pg_idx < CHUNK_SIZE; pg_idx++) {
+ void *buf = data->crypt_pages[pg_idx];
+
+ /* Stop at the meta page boundary to potentially switch keys. */
+ if (total &&
+ ((data->crypt_total + total) == data->meta_size))
+ break;
+
+ res = snapshot_read_next(&data->handle);
+ if (res < 0)
+ return res;
+ if (res == 0)
+ break;
+
+ WARN_ON(res != PAGE_SIZE);
+
+ /*
+ * Copy the page into the staging area. A future optimization
+ * could potentially skip this copy for lowmem pages.
+ */
+ memcpy(buf, data_of(data->handle), PAGE_SIZE);
+ sg_set_buf(&data->sg[1 + pg_idx], buf, PAGE_SIZE);
+ total += PAGE_SIZE;
+ }
+
+ sg_set_buf(&data->sg[1 + pg_idx], &data->auth_tag, SNAPSHOT_AUTH_TAG_SIZE);
+ aead_request_set_callback(req, 0, crypto_req_done, &wait);
+ /*
+ * Use incrementing nonces for each chunk, since a 64 bit value won't
+ * roll into re-use for any given hibernate image.
+ */
+ memcpy(&nonce[0], &data->nonce_low, sizeof(data->nonce_low));
+ memcpy(&nonce[sizeof(data->nonce_low)],
+ &data->nonce_high,
+ sizeof(nonce) - sizeof(data->nonce_low));
+
+ data->nonce_low += 1;
+ /* Total does not include AAD or the auth tag. */
+ aead_request_set_crypt(req, data->sg, data->sg, total, nonce);
+ res = crypto_wait_req(crypto_aead_encrypt(req), &wait);
+ if (res)
+ return res;
+
+ data->crypt_size = total;
+ data->crypt_total += total;
+ return 0;
+}
+
+/* Decrypt data from the staging area and push it to the snapshot. */
+static int snapshot_decrypt_drain(struct snapshot_data *data)
+{
+ struct aead_request *req = data->aead_req;
+ u8 nonce[GCM_AES_IV_SIZE];
+ DECLARE_CRYPTO_WAIT(wait);
+ int page_count;
+ size_t total;
+ int pg_idx;
+ int res;
+
+ /* Set up the associated data. */
+ sg_set_buf(&data->sg[0], &data->crypt_total, sizeof(data->crypt_total));
+
+ /*
+ * Get the number of full pages, which could be short at the end. There
+ * should also be a tag at the end, so the offset won't be an even page.
+ */
+ page_count = data->crypt_offset >> PAGE_SHIFT;
+ total = page_count << PAGE_SHIFT;
+ if (total == 0 || total == data->crypt_offset)
+ return -EINVAL;
+
+ /*
+ * Load the sg list with the crypt buffer. Inline decrypt back into the
+ * staging buffer. A future optimization could decrypt directly into
+ * lowmem pages.
+ */
+ for (pg_idx = 0; pg_idx < page_count; pg_idx++)
+ sg_set_buf(&data->sg[1 + pg_idx], data->crypt_pages[pg_idx], PAGE_SIZE);
+
+ /*
+ * It's possible this is the final decrypt, or the final decrypt of the
+ * meta region, and there are fewer than CHUNK_SIZE pages. If this is
+ * the case we would have just written the auth tag into the first few
+ * bytes of a new page. Copy to the tag if so.
+ */
+ if (page_count < CHUNK_SIZE &&
+ (data->crypt_offset - total) == sizeof(data->auth_tag)) {
+ memcpy(data->auth_tag, data->crypt_pages[pg_idx],
+ sizeof(data->auth_tag));
+ } else if (data->crypt_offset !=
+ ((CHUNK_SIZE << PAGE_SHIFT) + SNAPSHOT_AUTH_TAG_SIZE)) {
+ return -EINVAL;
+ }
+
+ sg_set_buf(&data->sg[1 + pg_idx], &data->auth_tag, SNAPSHOT_AUTH_TAG_SIZE);
+ aead_request_set_callback(req, 0, crypto_req_done, &wait);
+ memcpy(&nonce[0], &data->nonce_low, sizeof(data->nonce_low));
+ memcpy(&nonce[sizeof(data->nonce_low)],
+ &data->nonce_high,
+ sizeof(nonce) - sizeof(data->nonce_low));
+
+ data->nonce_low += 1;
+ aead_request_set_crypt(req, data->sg, data->sg, total + SNAPSHOT_AUTH_TAG_SIZE, nonce);
+ res = crypto_wait_req(crypto_aead_decrypt(req), &wait);
+ if (res)
+ return res;
+
+ data->crypt_size = 0;
+ data->crypt_offset = 0;
+
+ /* Push the decrypted pages further down the stack. */
+ total = 0;
+ for (pg_idx = 0; pg_idx < page_count; pg_idx++) {
+ void *buf = data->crypt_pages[pg_idx];
+
+ res = snapshot_write_next(&data->handle);
+ if (res < 0)
+ return res;
+ if (res == 0)
+ break;
+
+ if (!data_of(data->handle))
+ return -EINVAL;
+
+ WARN_ON(res != PAGE_SIZE);
+
+ /* Copy the decrypted page into the snapshot image. */
+ memcpy(data_of(data->handle), buf, PAGE_SIZE);
+ total += PAGE_SIZE;
+ }
+
+ if (data->crypt_total == 0)
+ data->meta_size = snapshot_get_meta_page_count() << PAGE_SHIFT;
+
+ data->crypt_total += total;
+ res = snapshot_check_user_key_switch(data);
+ if (res)
+ return res;
+
+ return 0;
+}
+
+static ssize_t snapshot_read_next_encrypted(struct snapshot_data *data,
+ void **buf)
+{
+ size_t tag_off;
+
+ /* Refill the encrypted buffer if it's empty. */
+ if (data->crypt_size == 0 ||
+ (data->crypt_offset >=
+ (data->crypt_size + SNAPSHOT_AUTH_TAG_SIZE))) {
+ int rc;
+
+ data->crypt_size = 0;
+ data->crypt_offset = 0;
+ rc = snapshot_encrypt_refill(data);
+ if (rc < 0)
+ return rc;
+ }
+
+ /* Return data pages if the offset is in that region. */
+ if (data->crypt_offset < data->crypt_size) {
+ size_t pg_idx = data->crypt_offset >> PAGE_SHIFT;
+ size_t pg_off = data->crypt_offset & (PAGE_SIZE - 1);
+ *buf = data->crypt_pages[pg_idx] + pg_off;
+ return PAGE_SIZE - pg_off;
+ }
+
+ /* Use offsets just beyond the size to return the tag. */
+ tag_off = data->crypt_offset - data->crypt_size;
+ if (tag_off > SNAPSHOT_AUTH_TAG_SIZE)
+ tag_off = SNAPSHOT_AUTH_TAG_SIZE;
+
+ *buf = data->auth_tag + tag_off;
+ return SNAPSHOT_AUTH_TAG_SIZE - tag_off;
+}
+
+static ssize_t snapshot_write_next_encrypted(struct snapshot_data *data,
+ void **buf)
+{
+ size_t tag_off;
+
+ /* Return data pages if the offset is in that region. */
+ if (data->crypt_offset < (PAGE_SIZE * CHUNK_SIZE)) {
+ size_t pg_idx = data->crypt_offset >> PAGE_SHIFT;
+ size_t pg_off = data->crypt_offset & (PAGE_SIZE - 1);
+ size_t size_avail = PAGE_SIZE;
+ *buf = data->crypt_pages[pg_idx] + pg_off;
+
+ /*
+ * If this is the boundary where the meta pages end, then just
+ * return enough for the auth tag.
+ */
+ if (data->meta_size &&
+ data->crypt_total < data->meta_size) {
+ u64 total_done =
+ data->crypt_total + data->crypt_offset;
+
+ if (total_done >= data->meta_size &&
+ (total_done <
+ (data->meta_size + SNAPSHOT_AUTH_TAG_SIZE))) {
+ size_avail = SNAPSHOT_AUTH_TAG_SIZE;
+ }
+ }
+
+ return size_avail - pg_off;
+ }
+
+ /* Use offsets just beyond the size to return the tag. */
+ tag_off = data->crypt_offset - (PAGE_SIZE * CHUNK_SIZE);
+ if (tag_off > SNAPSHOT_AUTH_TAG_SIZE)
+ tag_off = SNAPSHOT_AUTH_TAG_SIZE;
+
+ *buf = data->auth_tag + tag_off;
+ return SNAPSHOT_AUTH_TAG_SIZE - tag_off;
+}
+
+ssize_t snapshot_read_encrypted(struct snapshot_data *data,
+ char __user *buf, size_t count, loff_t *offp)
+{
+ ssize_t total = 0;
+
+ /* Loop getting buffers of varying sizes and copying to userspace. */
+ while (count) {
+ size_t copy_size;
+ size_t not_done;
+ void *src;
+ ssize_t src_size = snapshot_read_next_encrypted(data, &src);
+
+ if (src_size <= 0) {
+ if (total == 0)
+ return src_size;
+
+ break;
+ }
+
+ copy_size = min(count, (size_t)src_size);
+ not_done = copy_to_user(buf + total, src, copy_size);
+ copy_size -= not_done;
+ total += copy_size;
+ count -= copy_size;
+ data->crypt_offset += copy_size;
+ if (copy_size == 0) {
+ if (total == 0)
+ return -EFAULT;
+
+ break;
+ }
+ }
+
+ *offp += total;
+ return total;
+}
+
+ssize_t snapshot_write_encrypted(struct snapshot_data *data,
+ const char __user *buf, size_t count,
+ loff_t *offp)
+{
+ ssize_t total = 0;
+
+ /* Loop getting buffers of varying sizes and copying from. */
+ while (count) {
+ size_t copy_size;
+ size_t not_done;
+ void *dst;
+ ssize_t dst_size = snapshot_write_next_encrypted(data, &dst);
+
+ if (dst_size <= 0) {
+ if (total == 0)
+ return dst_size;
+
+ break;
+ }
+
+ copy_size = min(count, (size_t)dst_size);
+ not_done = copy_from_user(dst, buf + total, copy_size);
+ copy_size -= not_done;
+ total += copy_size;
+ count -= copy_size;
+ data->crypt_offset += copy_size;
+ if (copy_size == 0) {
+ if (total == 0)
+ return -EFAULT;
+
+ break;
+ }
+
+ /*
+ * Drain the encrypted buffer if it's full, or if we hit the end
+ * of the meta pages and need a key change.
+ */
+ if (data->crypt_offset >=
+ (PAGE_SIZE * CHUNK_SIZE) + SNAPSHOT_AUTH_TAG_SIZE ||
+ (data->meta_size &&
+ data->crypt_total < data->meta_size &&
+ data->crypt_total + data->crypt_offset ==
+ data->meta_size + SNAPSHOT_AUTH_TAG_SIZE)) {
+ int rc;
+
+ rc = snapshot_decrypt_drain(data);
+ if (rc < 0)
+ return rc;
+ }
+ }
+
+ *offp += total;
+ return total;
+}
+
+void snapshot_teardown_encryption(struct snapshot_data *data)
+{
+ int i;
+
+ if (data->aead_req) {
+ aead_request_free(data->aead_req);
+ data->aead_req = NULL;
+ }
+
+ if (data->aead_tfm) {
+ crypto_free_aead(data->aead_tfm);
+ data->aead_tfm = NULL;
+ }
+
+ for (i = 0; i < CHUNK_SIZE; i++) {
+ if (data->crypt_pages[i]) {
+ free_page((unsigned long)data->crypt_pages[i]);
+ data->crypt_pages[i] = NULL;
+ }
+ }
+
+ memzero_explicit(data->encryption_key, sizeof(data->encryption_key));
+ memzero_explicit(data->user_key, sizeof(data->user_key));
+}
+
+static int snapshot_setup_encryption_common(struct snapshot_data *data)
+{
+ int i, rc;
+
+ data->crypt_total = 0;
+ data->crypt_offset = 0;
+ data->crypt_size = 0;
+ data->user_key_valid = false;
+ memset(data->crypt_pages, 0, sizeof(data->crypt_pages));
+ /* This only works once per hibernate. */
+ if (data->aead_tfm)
+ return -EINVAL;
+
+ /* Set up the encryption transform */
+ data->aead_tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
+ if (IS_ERR(data->aead_tfm)) {
+ rc = PTR_ERR(data->aead_tfm);
+ data->aead_tfm = NULL;
+ return rc;
+ }
+
+ rc = -ENOMEM;
+ data->aead_req = aead_request_alloc(data->aead_tfm, GFP_KERNEL);
+ if (!data->aead_req)
+ goto setup_fail;
+
+ /* Allocate the staging area */
+ for (i = 0; i < CHUNK_SIZE; i++) {
+ data->crypt_pages[i] = (void *)__get_free_page(GFP_ATOMIC);
+ if (!data->crypt_pages[i])
+ goto setup_fail;
+ }
+
+ sg_init_table(data->sg, CHUNK_SIZE + 2);
+
+ /*
+ * The associated data will be the offset so that blocks can't be
+ * rearranged.
+ */
+ aead_request_set_ad(data->aead_req, sizeof(data->crypt_total));
+ rc = crypto_aead_setauthsize(data->aead_tfm, SNAPSHOT_AUTH_TAG_SIZE);
+ if (rc)
+ goto setup_fail;
+
+ return 0;
+
+setup_fail:
+ snapshot_teardown_encryption(data);
+ return rc;
+}
+
+int snapshot_get_encryption_key(struct snapshot_data *data,
+ struct uswsusp_key_blob __user *key)
+{
+ struct snapshot_wrapped_key wrapped = {};
+ u8 image_key[SNAPSHOT_ENCRYPTION_KEY_SIZE] __nonstring = {};
+ u8 nonce[USWSUSP_KEY_NONCE_SIZE];
+ int rc;
+
+ /* Don't pull a random key from a world that can be reset. */
+ if (data->ready)
+ return -EPIPE;
+
+ rc = snapshot_setup_encryption_common(data);
+ if (rc)
+ return rc;
+
+ /* Build a random starting nonce. */
+ get_random_bytes(nonce, sizeof(nonce));
+ memcpy(&data->nonce_low, &nonce[0], sizeof(data->nonce_low));
+ memcpy(&data->nonce_high, &nonce[8], sizeof(data->nonce_high));
+
+ /* Build and install a random image encryption key. */
+ get_random_bytes(image_key, sizeof(image_key));
+ rc = snapshot_install_image_key(data, image_key);
+ if (rc)
+ goto fail;
+
+ rc = snapshot_wrap_image_key(image_key, &wrapped);
+ if (rc)
+ goto fail;
+
+ /* Hand the wrapped key and clear nonce back to user mode. */
+ rc = put_user(sizeof(wrapped), &key->blob_len);
+ if (rc)
+ goto fail;
+
+ BUILD_BUG_ON(sizeof(wrapped) >
+ sizeof(((struct uswsusp_key_blob *)0)->blob));
+ rc = copy_to_user(&key->blob, &wrapped, sizeof(wrapped));
+ if (rc)
+ goto fail;
+
+ rc = copy_to_user(&key->nonce, &nonce, sizeof(nonce));
+ if (rc)
+ goto fail;
+
+ memzero_explicit(image_key, sizeof(image_key));
+ memzero_explicit(&wrapped, sizeof(wrapped));
+ return 0;
+
+fail:
+ memzero_explicit(image_key, sizeof(image_key));
+ memzero_explicit(&wrapped, sizeof(wrapped));
+ snapshot_teardown_encryption(data);
+ return rc;
+}
+
+int snapshot_set_encryption_key(struct snapshot_data *data,
+ struct uswsusp_key_blob __user *key)
+{
+ struct uswsusp_key_blob *blob;
+ struct snapshot_wrapped_key wrapped = {};
+ u8 image_key[SNAPSHOT_ENCRYPTION_KEY_SIZE] __nonstring = {};
+ int rc;
+
+ /* It's too late if data's been pushed in. */
+ if (data->handle.cur)
+ return -EPIPE;
+
+ rc = snapshot_setup_encryption_common(data);
+ if (rc)
+ return rc;
+
+ /* Load the key from user mode. */
+ blob = memdup_user(key, sizeof(*blob));
+ if (IS_ERR(blob)) {
+ rc = PTR_ERR(blob);
+ goto crypto_setup_fail;
+ }
+
+ if (blob->blob_len != sizeof(wrapped)) {
+ rc = -EINVAL;
+ goto out_blob;
+ }
+
+ memcpy(&wrapped, blob->blob, sizeof(wrapped));
+ rc = snapshot_unwrap_image_key(&wrapped, image_key);
+ if (rc)
+ goto out_blob;
+
+ rc = snapshot_install_image_key(data, image_key);
+ if (rc)
+ goto out_blob;
+
+ /* Load the starting nonce. */
+ memcpy(&data->nonce_low, &blob->nonce[0], sizeof(data->nonce_low));
+ memcpy(&data->nonce_high, &blob->nonce[8], sizeof(data->nonce_high));
+
+out_blob:
+ kfree_sensitive(blob);
+ memzero_explicit(&wrapped, sizeof(wrapped));
+ memzero_explicit(image_key, sizeof(image_key));
+ if (rc)
+ goto crypto_setup_fail;
+
+ return 0;
+
+crypto_setup_fail:
+ memzero_explicit(&wrapped, sizeof(wrapped));
+ memzero_explicit(image_key, sizeof(image_key));
+ snapshot_teardown_encryption(data);
+ return rc;
+}
+
+static loff_t snapshot_encrypted_byte_count(loff_t plain_size)
+{
+ loff_t pages = plain_size >> PAGE_SHIFT;
+ loff_t chunks = (pages + (CHUNK_SIZE - 1)) / CHUNK_SIZE;
+ /*
+ * The encrypted size is the normal size, plus a stitched in
+ * authentication tag for every chunk of pages.
+ */
+ return plain_size + (chunks * SNAPSHOT_AUTH_TAG_SIZE);
+}
+
+static loff_t snapshot_get_meta_data_size(void)
+{
+ loff_t pages = snapshot_get_meta_page_count();
+
+ return snapshot_encrypted_byte_count(pages << PAGE_SHIFT);
+}
+
+int snapshot_set_user_key(struct snapshot_data *data,
+ struct uswsusp_user_key __user *key)
+{
+ struct uswsusp_user_key user_key;
+ unsigned int key_len;
+ u64 size;
+ int rc;
+
+ /*
+ * Return the metadata size, the number of bytes that can be fed in before
+ * the user data key is needed at resume time.
+ */
+ size = snapshot_get_meta_data_size();
+ rc = put_user(size, &key->meta_size);
+ if (rc)
+ return rc;
+
+ rc = copy_from_user(&user_key, key, sizeof(struct uswsusp_user_key));
+ if (rc)
+ return rc;
+
+ BUILD_BUG_ON(sizeof(data->user_key) < sizeof(user_key.key));
+ if (user_key.reserved)
+ return -EINVAL;
+ if (user_key.key_len > sizeof(data->user_key))
+ return -EINVAL;
+ if (user_key.key_len < 8)
+ return -EINVAL;
+
+ key_len = user_key.key_len;
+
+ /* Don't allow it if it's too late. */
+ if (data->crypt_total > data->meta_size)
+ return -EBUSY;
+
+ memset(data->user_key, 0, sizeof(data->user_key));
+ memcpy(data->user_key, user_key.key, key_len);
+ data->user_key_valid = true;
+ /* Install the key if the user is just under the wire. */
+ rc = snapshot_check_user_key_switch(data);
+ if (rc)
+ return rc;
+
+ return 0;
+}
+
+loff_t snapshot_get_encrypted_image_size(loff_t raw_size)
+{
+ loff_t pages = raw_size >> PAGE_SHIFT;
+ loff_t meta_size;
+
+ pages -= snapshot_get_meta_page_count();
+ meta_size = snapshot_get_meta_data_size();
+ return snapshot_encrypted_byte_count(pages << PAGE_SHIFT) + meta_size;
+}
+
+int snapshot_finalize_decrypted_image(struct snapshot_data *data)
+{
+ int rc;
+
+ if (data->crypt_offset != 0) {
+ rc = snapshot_decrypt_drain(data);
+ if (rc)
+ return rc;
+ }
+
+ return 0;
+}
diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c
index d933b5b2c05d..02400ac125c5 100644
--- a/kernel/power/snapshot.c
+++ b/kernel/power/snapshot.c
@@ -2177,6 +2177,11 @@ unsigned long snapshot_get_image_size(void)
return nr_copy_pages + nr_meta_pages + 1;
}
+unsigned long snapshot_get_meta_page_count(void)
+{
+ return nr_meta_pages + 1;
+}
+
static int init_header(struct swsusp_info *info)
{
memset(info, 0, sizeof(struct swsusp_info));
diff --git a/kernel/power/user.c b/kernel/power/user.c
index d0fcfba7ac23..10a61a9f5df0 100644
--- a/kernel/power/user.c
+++ b/kernel/power/user.c
@@ -25,19 +25,10 @@
#include <linux/uaccess.h>
#include "power.h"
+#include "user.h"
static bool need_wait;
-
-static struct snapshot_data {
- struct snapshot_handle handle;
- int swap;
- int mode;
- bool frozen;
- bool ready;
- bool platform_support;
- bool free_bitmaps;
- dev_t dev;
-} snapshot_state;
+struct snapshot_data snapshot_state;
int is_hibernate_resume_dev(dev_t dev)
{
@@ -57,13 +48,13 @@ static int snapshot_open(struct inode *inode, struct file *filp)
if (!hibernate_acquire()) {
error = -EBUSY;
- goto Unlock;
+ goto unlock;
}
if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
hibernate_release();
error = -ENOSYS;
- goto Unlock;
+ goto unlock;
}
nonseekable_open(inode, filp);
data = &snapshot_state;
@@ -100,7 +91,7 @@ static int snapshot_open(struct inode *inode, struct file *filp)
data->platform_support = false;
data->dev = 0;
- Unlock:
+ unlock:
unlock_system_sleep(sleep_flags);
return error;
@@ -125,6 +116,7 @@ static int snapshot_release(struct inode *inode, struct file *filp)
} else if (data->free_bitmaps) {
free_basic_memory_bitmaps();
}
+ snapshot_teardown_encryption(data);
pm_notifier_call_chain(data->mode == O_RDONLY ?
PM_POST_HIBERNATION : PM_POST_RESTORE);
hibernate_release();
@@ -147,12 +139,18 @@ static ssize_t snapshot_read(struct file *filp, char __user *buf,
data = filp->private_data;
if (!data->ready) {
res = -ENODATA;
- goto Unlock;
+ goto unlock;
+ }
+
+ if (snapshot_encryption_enabled(data)) {
+ res = snapshot_read_encrypted(data, buf, count, offp);
+ goto unlock;
}
+
if (!pg_offp) { /* on page boundary? */
res = snapshot_read_next(&data->handle);
if (res <= 0)
- goto Unlock;
+ goto unlock;
} else {
res = PAGE_SIZE - pg_offp;
}
@@ -162,7 +160,7 @@ static ssize_t snapshot_read(struct file *filp, char __user *buf,
if (res > 0)
*offp += res;
- Unlock:
+ unlock:
unlock_system_sleep(sleep_flags);
return res;
@@ -185,6 +183,11 @@ static ssize_t snapshot_write(struct file *filp, const char __user *buf,
data = filp->private_data;
+ if (snapshot_encryption_enabled(data)) {
+ res = snapshot_write_encrypted(data, buf, count, offp);
+ goto unlock;
+ }
+
if (!pg_offp) {
res = snapshot_write_next(&data->handle);
if (res <= 0)
@@ -328,6 +331,12 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
break;
case SNAPSHOT_ATOMIC_RESTORE:
+ if (snapshot_encryption_enabled(data)) {
+ error = snapshot_finalize_decrypted_image(data);
+ if (error)
+ break;
+ }
+
error = snapshot_write_finalize(&data->handle);
if (error)
break;
@@ -368,6 +377,8 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
}
size = snapshot_get_image_size();
size <<= PAGE_SHIFT;
+ if (snapshot_encryption_enabled(data))
+ size = snapshot_get_encrypted_image_size(size);
error = put_user(size, (loff_t __user *)arg);
break;
@@ -425,6 +436,17 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
error = snapshot_set_swap_area(data, (void __user *)arg);
break;
+ case SNAPSHOT_ENABLE_ENCRYPTION:
+ if (data->mode == O_RDONLY)
+ error = snapshot_get_encryption_key(data, (void __user *)arg);
+ else
+ error = snapshot_set_encryption_key(data, (void __user *)arg);
+ break;
+
+ case SNAPSHOT_SET_USER_KEY:
+ error = snapshot_set_user_key(data, (void __user *)arg);
+ break;
+
default:
error = -ENOTTY;
@@ -448,6 +470,8 @@ snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
case SNAPSHOT_ALLOC_SWAP_PAGE:
case SNAPSHOT_CREATE_IMAGE:
case SNAPSHOT_SET_SWAP_AREA:
+ case SNAPSHOT_ENABLE_ENCRYPTION:
+ case SNAPSHOT_SET_USER_KEY:
return snapshot_ioctl(file, cmd,
(unsigned long) compat_ptr(arg));
default:
diff --git a/kernel/power/user.h b/kernel/power/user.h
new file mode 100644
index 000000000000..e13a0ac439f3
--- /dev/null
+++ b/kernel/power/user.h
@@ -0,0 +1,132 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <linux/crypto.h>
+#include <linux/scatterlist.h>
+#include <linux/suspend_ioctls.h>
+#include <crypto/aead.h>
+#include <crypto/aes.h>
+
+#define SNAPSHOT_ENCRYPTION_KEY_SIZE AES_KEYSIZE_128
+#define SNAPSHOT_AUTH_TAG_SIZE 16
+
+/* Define the number of pages in a single AEAD encryption chunk. */
+#define CHUNK_SIZE 16
+
+struct snapshot_data {
+ struct snapshot_handle handle;
+ int swap;
+ int mode;
+ bool frozen;
+ bool ready;
+ bool platform_support;
+ bool free_bitmaps;
+ dev_t dev;
+
+#if defined(CONFIG_ENCRYPTED_HIBERNATION)
+ struct crypto_aead *aead_tfm;
+ struct aead_request *aead_req;
+ void *crypt_pages[CHUNK_SIZE];
+ u8 auth_tag[SNAPSHOT_AUTH_TAG_SIZE];
+ struct scatterlist sg[CHUNK_SIZE + 2]; /* Add room for AD and auth tag. */
+ size_t crypt_offset;
+ size_t crypt_size;
+ u64 crypt_total;
+ u64 nonce_low;
+ u64 nonce_high;
+ u8 encryption_key[SNAPSHOT_ENCRYPTION_KEY_SIZE] __nonstring;
+ u8 user_key[USWSUSP_USER_KEY_SIZE] __nonstring;
+ bool user_key_valid;
+ u64 meta_size;
+#endif
+
+};
+
+extern struct snapshot_data snapshot_state;
+
+/* kernel/power/snapenc.c routines */
+#if defined(CONFIG_ENCRYPTED_HIBERNATION)
+
+ssize_t snapshot_read_encrypted(struct snapshot_data *data,
+ char __user *buf, size_t count, loff_t *offp);
+
+ssize_t snapshot_write_encrypted(struct snapshot_data *data,
+ const char __user *buf, size_t count,
+ loff_t *offp);
+
+void snapshot_teardown_encryption(struct snapshot_data *data);
+int snapshot_get_encryption_key(struct snapshot_data *data,
+ struct uswsusp_key_blob __user *key);
+
+int snapshot_set_encryption_key(struct snapshot_data *data,
+ struct uswsusp_key_blob __user *key);
+
+int snapshot_set_user_key(struct snapshot_data *data,
+ struct uswsusp_user_key __user *key);
+
+int snapshot_store_encryption_seed(const char *buf, size_t count);
+
+loff_t snapshot_get_encrypted_image_size(loff_t raw_size);
+
+int snapshot_finalize_decrypted_image(struct snapshot_data *data);
+
+static inline bool snapshot_encryption_enabled(struct snapshot_data *data)
+{
+ return data->aead_tfm;
+}
+
+#else
+
+static inline ssize_t snapshot_read_encrypted(struct snapshot_data *data,
+ char __user *buf, size_t count,
+ loff_t *offp)
+{
+ return -ENOTTY;
+}
+
+static inline ssize_t snapshot_write_encrypted(struct snapshot_data *data,
+ const char __user *buf,
+ size_t count, loff_t *offp)
+{
+ return -ENOTTY;
+}
+
+static inline void snapshot_teardown_encryption(struct snapshot_data *data) {}
+static inline int snapshot_get_encryption_key(struct snapshot_data *data,
+ struct uswsusp_key_blob __user *key)
+{
+ return -ENOTTY;
+}
+
+static inline int snapshot_set_encryption_key(struct snapshot_data *data,
+ struct uswsusp_key_blob __user *key)
+{
+ return -ENOTTY;
+}
+
+static inline int snapshot_set_user_key(struct snapshot_data *data,
+ struct uswsusp_user_key __user *key)
+{
+ return -ENOTTY;
+}
+
+static inline int snapshot_store_encryption_seed(const char *buf, size_t count)
+{
+ return -ENOTTY;
+}
+
+static inline loff_t snapshot_get_encrypted_image_size(loff_t raw_size)
+{
+ return raw_size;
+}
+
+static inline int snapshot_finalize_decrypted_image(struct snapshot_data *data)
+{
+ return -ENOTTY;
+}
+
+static inline bool snapshot_encryption_enabled(struct snapshot_data *data)
+{
+ return false;
+}
+
+#endif
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v2 2/4] PM: hibernate: permit encrypted snapshot device under lockdown
2026-08-04 10:14 [PATCH v2 0/4] PM: hibernate: encrypted snapshots under lockdown Sean Rhodes
2026-08-04 10:14 ` [PATCH v2 1/4] PM: hibernate: add seed-wrapped encrypted snapshots Sean Rhodes
@ 2026-08-04 10:14 ` Sean Rhodes
2026-08-04 10:14 ` [PATCH v2 3/4] PM: hibernate: document encrypted snapshot seed ABI Sean Rhodes
2026-08-04 10:14 ` [PATCH v2 4/4] PM: hibernate: harden encrypted snapshot write path Sean Rhodes
3 siblings, 0 replies; 5+ messages in thread
From: Sean Rhodes @ 2026-08-04 10:14 UTC (permalink / raw)
To: linux-pm
Cc: Rafael J. Wysocki, Len Brown, Pavel Machek, Sean Rhodes,
Evan Green, linux-kernel, Xueqin Luo, Jens Axboe, Andrew Morton,
Chris Li, Kairui Song, Kemeng Shi, Nhat Pham, Baoquan He,
Barry Song, Youngjun Park, linux-block, linux-mm
Lockdown disables hibernation because restoring attacker-controlled image
data can modify kernel memory. Permit opening the userspace snapshot
device under lockdown when encrypted hibernation support is built, but
require encryption to be enabled before image data can be read or written
and before restore or power-off ioctls can proceed.
Tested on StarFighter MTL with lockdown enabled: encrypted uswsusp
hibernate and restore used the snapshot device path.
Signed-off-by: Sean Rhodes <sean@starlabs.systems>
---
kernel/power/hibernate.c | 8 ++++++++
kernel/power/power.h | 1 +
kernel/power/user.c | 36 ++++++++++++++++++++++++++++++++++--
kernel/power/user.h | 1 +
4 files changed, 44 insertions(+), 2 deletions(-)
diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
index a95cb447a13a..0b8626bdf81a 100644
--- a/kernel/power/hibernate.c
+++ b/kernel/power/hibernate.c
@@ -114,6 +114,14 @@ bool hibernation_available(void)
!secretmem_active() && !cxl_mem_active();
}
+bool hibernation_snapshot_dev_available(void)
+{
+ return nohibernate == 0 &&
+ (!security_locked_down(LOCKDOWN_HIBERNATION) ||
+ IS_ENABLED(CONFIG_ENCRYPTED_HIBERNATION)) &&
+ !secretmem_active() && !cxl_mem_active();
+}
+
/**
* hibernation_set_ops - Set the global hibernate operations.
* @ops: Hibernation operations to use in subsequent hibernation transitions.
diff --git a/kernel/power/power.h b/kernel/power/power.h
index e080230f97fc..68ed191b905c 100644
--- a/kernel/power/power.h
+++ b/kernel/power/power.h
@@ -168,6 +168,7 @@ extern int snapshot_image_loaded(struct snapshot_handle *handle);
extern bool hibernate_acquire(void);
extern void hibernate_release(void);
+bool hibernation_snapshot_dev_available(void);
extern sector_t alloc_swapdev_block(int swap);
extern void free_all_swap_pages(int swap);
diff --git a/kernel/power/user.c b/kernel/power/user.c
index 10a61a9f5df0..d30aed4bc35f 100644
--- a/kernel/power/user.c
+++ b/kernel/power/user.c
@@ -21,6 +21,7 @@
#include <linux/console.h>
#include <linux/cpu.h>
#include <linux/freezer.h>
+#include <linux/security.h>
#include <linux/uaccess.h>
@@ -32,7 +33,12 @@ struct snapshot_data snapshot_state;
int is_hibernate_resume_dev(dev_t dev)
{
- return hibernation_available() && snapshot_state.dev == dev;
+ return hibernation_snapshot_dev_available() && snapshot_state.dev == dev;
+}
+
+static bool snapshot_encryption_required(void)
+{
+ return security_locked_down(LOCKDOWN_HIBERNATION);
}
static int snapshot_open(struct inode *inode, struct file *filp)
@@ -41,7 +47,7 @@ static int snapshot_open(struct inode *inode, struct file *filp)
unsigned int sleep_flags;
int error;
- if (!hibernation_available())
+ if (!hibernation_snapshot_dev_available())
return -EPERM;
sleep_flags = lock_system_sleep();
@@ -90,6 +96,7 @@ static int snapshot_open(struct inode *inode, struct file *filp)
data->ready = false;
data->platform_support = false;
data->dev = 0;
+ data->encryption_required = snapshot_encryption_required();
unlock:
unlock_system_sleep(sleep_flags);
@@ -141,6 +148,10 @@ static ssize_t snapshot_read(struct file *filp, char __user *buf,
res = -ENODATA;
goto unlock;
}
+ if (data->encryption_required && !snapshot_encryption_enabled(data)) {
+ res = -EPERM;
+ goto unlock;
+ }
if (snapshot_encryption_enabled(data)) {
res = snapshot_read_encrypted(data, buf, count, offp);
@@ -183,6 +194,11 @@ static ssize_t snapshot_write(struct file *filp, const char __user *buf,
data = filp->private_data;
+ if (data->encryption_required && !snapshot_encryption_enabled(data)) {
+ res = -EPERM;
+ goto unlock;
+ }
+
if (snapshot_encryption_enabled(data)) {
res = snapshot_write_encrypted(data, buf, count, offp);
goto unlock;
@@ -321,6 +337,10 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
error = -EPERM;
break;
}
+ if (data->encryption_required && !snapshot_encryption_enabled(data)) {
+ error = -EPERM;
+ break;
+ }
pm_restore_gfp_mask();
error = hibernation_snapshot(data->platform_support);
if (!error) {
@@ -331,6 +351,10 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
break;
case SNAPSHOT_ATOMIC_RESTORE:
+ if (data->encryption_required && !snapshot_encryption_enabled(data)) {
+ error = -EPERM;
+ break;
+ }
if (snapshot_encryption_enabled(data)) {
error = snapshot_finalize_decrypted_image(data);
if (error)
@@ -415,6 +439,10 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
error = -EPERM;
break;
}
+ if (data->encryption_required && !snapshot_encryption_enabled(data)) {
+ error = -EPERM;
+ break;
+ }
/*
* Tasks are frozen and the notifiers have been called with
* PM_HIBERNATION_PREPARE
@@ -428,6 +456,10 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
break;
case SNAPSHOT_POWER_OFF:
+ if (data->encryption_required && !snapshot_encryption_enabled(data)) {
+ error = -EPERM;
+ break;
+ }
if (data->platform_support)
error = hibernation_platform_enter();
break;
diff --git a/kernel/power/user.h b/kernel/power/user.h
index e13a0ac439f3..3c3498bdb752 100644
--- a/kernel/power/user.h
+++ b/kernel/power/user.h
@@ -20,6 +20,7 @@ struct snapshot_data {
bool ready;
bool platform_support;
bool free_bitmaps;
+ bool encryption_required;
dev_t dev;
#if defined(CONFIG_ENCRYPTED_HIBERNATION)
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v2 3/4] PM: hibernate: document encrypted snapshot seed ABI
2026-08-04 10:14 [PATCH v2 0/4] PM: hibernate: encrypted snapshots under lockdown Sean Rhodes
2026-08-04 10:14 ` [PATCH v2 1/4] PM: hibernate: add seed-wrapped encrypted snapshots Sean Rhodes
2026-08-04 10:14 ` [PATCH v2 2/4] PM: hibernate: permit encrypted snapshot device under lockdown Sean Rhodes
@ 2026-08-04 10:14 ` Sean Rhodes
2026-08-04 10:14 ` [PATCH v2 4/4] PM: hibernate: harden encrypted snapshot write path Sean Rhodes
3 siblings, 0 replies; 5+ messages in thread
From: Sean Rhodes @ 2026-08-04 10:14 UTC (permalink / raw)
To: linux-pm
Cc: Rafael J. Wysocki, Len Brown, Pavel Machek, Sean Rhodes,
Evan Green, linux-kernel, Xueqin Luo, Jens Axboe, Andrew Morton,
Chris Li, Kairui Song, Kemeng Shi, Nhat Pham, Baoquan He,
Barry Song, Youngjun Park, linux-block, linux-mm
Document the write-only snapshot_seed sysfs file and update the userspace
snapshot interface text to describe the opaque wrapped-key flow used by
encrypted hibernation.
Signed-off-by: Sean Rhodes <sean@starlabs.systems>
---
Documentation/ABI/testing/sysfs-power | 14 ++++++++++++++
Documentation/power/userland-swsusp.rst | 15 +++++++++------
2 files changed, 23 insertions(+), 6 deletions(-)
diff --git a/Documentation/ABI/testing/sysfs-power b/Documentation/ABI/testing/sysfs-power
index d38da077905a..57f6063ef88c 100644
--- a/Documentation/ABI/testing/sysfs-power
+++ b/Documentation/ABI/testing/sysfs-power
@@ -470,3 +470,17 @@ Description:
Minimum value: 1
Default value: 3
+
+What: /sys/power/snapshot_seed
+Date: July 2026
+Contact: linux-pm@vger.kernel.org
+Description:
+ Write-only file present when CONFIG_ENCRYPTED_HIBERNATION=y.
+
+ Trusted early userspace writes a 32-byte seed, encoded as
+ 64 hexadecimal characters plus an optional newline, before
+ enabling encrypted userspace hibernation snapshots.
+
+ The first successful write locks the seed until the next boot.
+ Writing the same seed again succeeds. Writing a different seed
+ fails with EPERM.
diff --git a/Documentation/power/userland-swsusp.rst b/Documentation/power/userland-swsusp.rst
index 282e01d2fe61..777518ac591d 100644
--- a/Documentation/power/userland-swsusp.rst
+++ b/Documentation/power/userland-swsusp.rst
@@ -116,12 +116,15 @@ SNAPSHOT_S2RAM
its state on the basis of the saved suspend image otherwise)
SNAPSHOT_ENABLE_ENCRYPTION
- Enables encryption of the hibernate image within the kernel. Upon suspend
- (ie when the snapshot device was opened for reading), returns a blob
- representing the random encryption key the kernel created to encrypt the
- hibernate image with. Upon resume (ie when the snapshot device was opened
- for writing), receives a blob from usermode containing the key material
- previously returned during hibernate.
+ Enables encryption of the hibernate image within the kernel. Trusted
+ early userspace must write the 32-byte snapshot encryption seed to
+ /sys/power/snapshot_seed before calling this ioctl. Upon suspend
+ (ie when the snapshot device was opened for reading), this ioctl returns
+ an opaque wrapped key blob and the starting nonce. Upon resume (ie when
+ the snapshot device was opened for writing), this ioctl receives the
+ same blob and nonce from usermode after the same seed has been written
+ to /sys/power/snapshot_seed. The plaintext image key is generated and
+ unwrapped inside the kernel.
SNAPSHOT_SET_USER_KEY
Mixes additional user key material into the data portion of an encrypted
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v2 4/4] PM: hibernate: harden encrypted snapshot write path
2026-08-04 10:14 [PATCH v2 0/4] PM: hibernate: encrypted snapshots under lockdown Sean Rhodes
` (2 preceding siblings ...)
2026-08-04 10:14 ` [PATCH v2 3/4] PM: hibernate: document encrypted snapshot seed ABI Sean Rhodes
@ 2026-08-04 10:14 ` Sean Rhodes
3 siblings, 0 replies; 5+ messages in thread
From: Sean Rhodes @ 2026-08-04 10:14 UTC (permalink / raw)
To: linux-pm
Cc: Rafael J. Wysocki, Len Brown, Pavel Machek, Sean Rhodes,
Evan Green, linux-kernel, Xueqin Luo, Jens Axboe, Andrew Morton,
Chris Li, Kairui Song, Kemeng Shi, Nhat Pham, Baoquan He,
Barry Song, Youngjun Park, linux-block, linux-mm
Encrypted uswsusp permits the snapshot device under lockdown only after
encryption is enabled, but userspace still writes the image through the
normal block path. Track the snapshot owner, encrypted bytes read, swap
extents and swap header offset so lockdown only permits writes that match
the active encrypted image.
Tighten the encrypted stream bookkeeping while here: frame the metadata
boundary only when a user key switch is in use, report userspace copy
failures as -EFAULT, and reset encryption state when the snapshot is
freed.
Signed-off-by: Sean Rhodes <sean@starlabs.systems>
---
Documentation/power/userland-swsusp.rst | 7 +-
block/fops.c | 55 +++-
include/linux/suspend.h | 22 ++
include/linux/swap.h | 16 ++
include/uapi/linux/suspend_ioctls.h | 2 +-
kernel/power/power.h | 2 +
kernel/power/snapenc.c | 344 ++++++++++++++----------
kernel/power/swap.c | 98 +++++++
kernel/power/user.c | 177 +++++++++++-
kernel/power/user.h | 22 +-
mm/swapfile.c | 32 +++
11 files changed, 606 insertions(+), 171 deletions(-)
diff --git a/Documentation/power/userland-swsusp.rst b/Documentation/power/userland-swsusp.rst
index 777518ac591d..df8eca9e6e36 100644
--- a/Documentation/power/userland-swsusp.rst
+++ b/Documentation/power/userland-swsusp.rst
@@ -130,9 +130,10 @@ SNAPSHOT_SET_USER_KEY
Mixes additional user key material into the data portion of an encrypted
hibernate image. The ioctl argument points to struct uswsusp_user_key.
key_len must be between 8 and USWSUSP_USER_KEY_SIZE bytes, and reserved
- must be zero. The kernel writes meta_size with the encrypted metadata
- size that userspace may transfer before providing the user key during
- resume.
+ must be zero. During hibernation the kernel writes meta_size with the
+ encrypted metadata size. During resume, userspace passes the saved
+ meta_size back so the kernel can switch keys at the metadata boundary
+ before the snapshot header has been decrypted.
The device's read() operation can be used to transfer the snapshot image from
the kernel. It has the following limitations:
diff --git a/block/fops.c b/block/fops.c
index 15783a6180de..efeb485e91e6 100644
--- a/block/fops.c
+++ b/block/fops.c
@@ -723,43 +723,65 @@ static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
struct file *file = iocb->ki_filp;
struct inode *bd_inode = bdev_file_inode(file);
struct block_device *bdev = I_BDEV(bd_inode);
+ dev_t dev = bd_inode->i_rdev;
bool atomic = iocb->ki_flags & IOCB_ATOMIC;
loff_t size = bdev_nr_bytes(bdev);
+ enum hibernate_snapshot_write hibernate_write;
+ size_t hibernate_len = 0;
size_t shorted = 0;
+ ssize_t hibernate_written = 0;
ssize_t ret;
if (bdev_read_only(bdev))
return -EPERM;
- if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
- return -ETXTBSY;
+ if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(dev)) {
+ if (!is_sync_kiocb(iocb) || !iocb_is_dsync(iocb) ||
+ (iocb->ki_flags & IOCB_DIRECT))
+ return -ETXTBSY;
- if (!iov_iter_count(from))
- return 0;
+ hibernate_len = iov_iter_count(from);
+ hibernate_write = hibernate_snapshot_write_begin(dev, iocb->ki_pos, hibernate_len);
+ if (hibernate_write == HIBERNATE_SNAPSHOT_WRITE_NONE)
+ return -ETXTBSY;
+ } else {
+ hibernate_write = HIBERNATE_SNAPSHOT_WRITE_NONE;
+ }
- if (iocb->ki_pos >= size)
- return -ENOSPC;
+ if (!iov_iter_count(from)) {
+ ret = 0;
+ goto out_hibernate;
+ }
- if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
- return -EOPNOTSUPP;
+ if (iocb->ki_pos >= size) {
+ ret = -ENOSPC;
+ goto out_hibernate;
+ }
+
+ if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT) {
+ ret = -EOPNOTSUPP;
+ goto out_hibernate;
+ }
if (atomic) {
ret = generic_atomic_write_valid(iocb, from);
if (ret)
- return ret;
+ goto out_hibernate;
}
size -= iocb->ki_pos;
if (iov_iter_count(from) > size) {
- if (atomic)
- return -EINVAL;
+ if (atomic) {
+ ret = -EINVAL;
+ goto out_hibernate;
+ }
shorted = iov_iter_count(from) - size;
iov_iter_truncate(from, size);
}
ret = file_update_time(file);
if (ret)
- return ret;
+ goto out_hibernate;
if (iocb->ki_flags & IOCB_DIRECT) {
ret = blkdev_direct_write(iocb, from);
@@ -777,9 +799,16 @@ static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
inode_unlock_shared(bd_inode);
}
- if (ret > 0)
+ if (ret > 0) {
+ hibernate_written = ret;
ret = generic_write_sync(iocb, ret);
+ }
iov_iter_reexpand(from, iov_iter_count(from) + shorted);
+
+out_hibernate:
+ if (hibernate_len)
+ hibernate_snapshot_write_end(hibernate_write, hibernate_len,
+ hibernate_written);
return ret;
}
diff --git a/include/linux/suspend.h b/include/linux/suspend.h
index b02876f1ae38..8308a6583756 100644
--- a/include/linux/suspend.h
+++ b/include/linux/suspend.h
@@ -426,10 +426,32 @@ static inline bool pm_hibernation_mode_is_suspend(void) { return false; }
int arch_resume_nosmt(void);
+enum hibernate_snapshot_write {
+ HIBERNATE_SNAPSHOT_WRITE_NONE,
+ HIBERNATE_SNAPSHOT_WRITE_IMAGE,
+ HIBERNATE_SNAPSHOT_WRITE_HEADER,
+};
+
#ifdef CONFIG_HIBERNATION_SNAPSHOT_DEV
int is_hibernate_resume_dev(dev_t dev);
+enum hibernate_snapshot_write
+hibernate_snapshot_write_begin(dev_t dev, loff_t pos, size_t count);
+void hibernate_snapshot_write_end(enum hibernate_snapshot_write type,
+ size_t reserved, ssize_t written);
#else
static inline int is_hibernate_resume_dev(dev_t dev) { return 0; }
+
+static inline enum hibernate_snapshot_write
+hibernate_snapshot_write_begin(dev_t dev, loff_t pos, size_t count)
+{
+ return HIBERNATE_SNAPSHOT_WRITE_NONE;
+}
+
+static inline void hibernate_snapshot_write_end(enum hibernate_snapshot_write type,
+ size_t reserved,
+ ssize_t written)
+{
+}
#endif
/* Hibernation and suspend events */
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 8f0f68e245ba..b36685d1f314 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -439,6 +439,9 @@ extern int find_hibernation_swap_type(dev_t device, sector_t offset);
int find_first_swap(dev_t *device);
extern unsigned int count_swap_pages(int, int);
extern sector_t swapdev_block(int, pgoff_t);
+int swapdev_block_to_offset(int type, sector_t block, pgoff_t *offset);
+int swapdev_block_to_extent(int type, sector_t block, pgoff_t *offset,
+ pgoff_t *nr_pages);
extern int __swap_count(swp_entry_t entry);
extern bool swap_entry_swapped(struct swap_info_struct *si, swp_entry_t entry);
extern int swp_swapcount(swp_entry_t entry);
@@ -535,6 +538,19 @@ static inline int add_swap_extent(struct swap_info_struct *sis,
{
return -EINVAL;
}
+
+static inline int swapdev_block_to_offset(int type, sector_t block,
+ pgoff_t *offset)
+{
+ return -EINVAL;
+}
+
+static inline int swapdev_block_to_extent(int type, sector_t block,
+ pgoff_t *offset,
+ pgoff_t *nr_pages)
+{
+ return -EINVAL;
+}
#endif /* CONFIG_SWAP */
#ifdef CONFIG_MEMCG
static inline int mem_cgroup_swappiness(struct mem_cgroup *memcg)
diff --git a/include/uapi/linux/suspend_ioctls.h b/include/uapi/linux/suspend_ioctls.h
index 2615dbd03404..5e23bf936277 100644
--- a/include/uapi/linux/suspend_ioctls.h
+++ b/include/uapi/linux/suspend_ioctls.h
@@ -31,7 +31,7 @@ struct uswsusp_key_blob {
* image.
*/
struct uswsusp_user_key {
- /* Kernel returns the metadata size. */
+ /* Kernel returns the metadata size; resume passes the saved value in. */
__u64 meta_size;
__u32 key_len;
__u32 reserved;
diff --git a/kernel/power/power.h b/kernel/power/power.h
index 68ed191b905c..39ef04be283d 100644
--- a/kernel/power/power.h
+++ b/kernel/power/power.h
@@ -171,6 +171,8 @@ extern void hibernate_release(void);
bool hibernation_snapshot_dev_available(void);
extern sector_t alloc_swapdev_block(int swap);
+bool swsusp_swap_range_allocated(int swap, loff_t pos, size_t count);
+u64 swsusp_swap_map_bytes(u64 image_bytes);
extern void free_all_swap_pages(int swap);
extern int swsusp_swap_in_use(void);
diff --git a/kernel/power/snapenc.c b/kernel/power/snapenc.c
index e6b2fcad9807..cd29803ec6c5 100644
--- a/kernel/power/snapenc.c
+++ b/kernel/power/snapenc.c
@@ -5,6 +5,7 @@
#include <crypto/aead.h>
#include <crypto/gcm.h>
#include <crypto/sha2.h>
+#include <crypto/utils.h>
#include <linux/hex.h>
#include <linux/mutex.h>
#include <linux/random.h>
@@ -52,7 +53,7 @@ int snapshot_store_encryption_seed(const char *buf, size_t count)
mutex_lock(&snapshot_seed_mutex);
if (snapshot_seed_valid) {
- ret = memcmp(snapshot_seed, seed, sizeof(seed)) ? -EPERM : 0;
+ ret = crypto_memneq(snapshot_seed, seed, sizeof(seed)) ? -EPERM : 0;
goto out;
}
@@ -266,6 +267,22 @@ static int snapshot_check_user_key_switch(struct snapshot_data *data)
return 0;
}
+static loff_t snapshot_encrypted_byte_count(loff_t plain_size);
+
+static void snapshot_set_meta_size(struct snapshot_data *data, u64 meta_size)
+{
+ data->meta_size = meta_size;
+ data->crypt_meta_size = snapshot_encrypted_byte_count(meta_size);
+}
+
+static void snapshot_record_encrypted_read(struct snapshot_data *data,
+ size_t count)
+{
+ spin_lock(&data->crypt_lock);
+ data->crypt_bytes_read += count;
+ spin_unlock(&data->crypt_lock);
+}
+
/* Encrypt more data from the snapshot into the staging area. */
static int snapshot_encrypt_refill(struct snapshot_data *data)
{
@@ -277,7 +294,8 @@ static int snapshot_encrypt_refill(struct snapshot_data *data)
int res;
if (data->crypt_total == 0) {
- data->meta_size = snapshot_get_meta_page_count() << PAGE_SHIFT;
+ snapshot_set_meta_size(data,
+ snapshot_get_meta_page_count() << PAGE_SHIFT);
} else {
res = snapshot_check_user_key_switch(data);
if (res)
@@ -294,8 +312,8 @@ static int snapshot_encrypt_refill(struct snapshot_data *data)
for (pg_idx = 0; pg_idx < CHUNK_SIZE; pg_idx++) {
void *buf = data->crypt_pages[pg_idx];
- /* Stop at the meta page boundary to potentially switch keys. */
- if (total &&
+ /* Stop at the meta page boundary before switching keys. */
+ if (data->user_key_valid && total &&
((data->crypt_total + total) == data->meta_size))
break;
@@ -316,6 +334,9 @@ static int snapshot_encrypt_refill(struct snapshot_data *data)
total += PAGE_SIZE;
}
+ if (!total)
+ return 0;
+
sg_set_buf(&data->sg[1 + pg_idx], &data->auth_tag, SNAPSHOT_AUTH_TAG_SIZE);
aead_request_set_callback(req, 0, crypto_req_done, &wait);
/*
@@ -422,8 +443,12 @@ static int snapshot_decrypt_drain(struct snapshot_data *data)
total += PAGE_SIZE;
}
- if (data->crypt_total == 0)
+ if (data->crypt_total == 0) {
data->meta_size = snapshot_get_meta_page_count() << PAGE_SHIFT;
+ if (data->user_key_valid)
+ data->crypt_meta_size =
+ snapshot_encrypted_byte_count(data->meta_size);
+ }
data->crypt_total += total;
res = snapshot_check_user_key_switch(data);
@@ -449,6 +474,8 @@ static ssize_t snapshot_read_next_encrypted(struct snapshot_data *data,
rc = snapshot_encrypt_refill(data);
if (rc < 0)
return rc;
+ if (!data->crypt_size)
+ return 0;
}
/* Return data pages if the offset is in that region. */
@@ -471,133 +498,130 @@ static ssize_t snapshot_read_next_encrypted(struct snapshot_data *data,
static ssize_t snapshot_write_next_encrypted(struct snapshot_data *data,
void **buf)
{
+ size_t size_avail;
size_t tag_off;
/* Return data pages if the offset is in that region. */
if (data->crypt_offset < (PAGE_SIZE * CHUNK_SIZE)) {
size_t pg_idx = data->crypt_offset >> PAGE_SHIFT;
size_t pg_off = data->crypt_offset & (PAGE_SIZE - 1);
- size_t size_avail = PAGE_SIZE;
- *buf = data->crypt_pages[pg_idx] + pg_off;
- /*
- * If this is the boundary where the meta pages end, then just
- * return enough for the auth tag.
- */
- if (data->meta_size &&
- data->crypt_total < data->meta_size) {
- u64 total_done =
- data->crypt_total + data->crypt_offset;
-
- if (total_done >= data->meta_size &&
- (total_done <
- (data->meta_size + SNAPSHOT_AUTH_TAG_SIZE))) {
- size_avail = SNAPSHOT_AUTH_TAG_SIZE;
- }
- }
+ *buf = data->crypt_pages[pg_idx] + pg_off;
+ size_avail = PAGE_SIZE - pg_off;
+ } else {
+ /* Use offsets just beyond the size to return the tag. */
+ tag_off = data->crypt_offset - (PAGE_SIZE * CHUNK_SIZE);
+ if (tag_off > SNAPSHOT_AUTH_TAG_SIZE)
+ tag_off = SNAPSHOT_AUTH_TAG_SIZE;
- return size_avail - pg_off;
+ *buf = data->auth_tag + tag_off;
+ size_avail = SNAPSHOT_AUTH_TAG_SIZE - tag_off;
}
- /* Use offsets just beyond the size to return the tag. */
- tag_off = data->crypt_offset - (PAGE_SIZE * CHUNK_SIZE);
- if (tag_off > SNAPSHOT_AUTH_TAG_SIZE)
- tag_off = SNAPSHOT_AUTH_TAG_SIZE;
+ if (data->crypt_meta_size &&
+ data->crypt_stream_total < data->crypt_meta_size) {
+ u64 meta_avail = data->crypt_meta_size - data->crypt_stream_total;
- *buf = data->auth_tag + tag_off;
- return SNAPSHOT_AUTH_TAG_SIZE - tag_off;
+ size_avail = min_t(u64, size_avail, meta_avail);
+ }
+
+ return size_avail;
}
ssize_t snapshot_read_encrypted(struct snapshot_data *data,
char __user *buf, size_t count, loff_t *offp)
{
- ssize_t total = 0;
-
- /* Loop getting buffers of varying sizes and copying to userspace. */
- while (count) {
- size_t copy_size;
- size_t not_done;
- void *src;
- ssize_t src_size = snapshot_read_next_encrypted(data, &src);
-
- if (src_size <= 0) {
- if (total == 0)
- return src_size;
-
- break;
- }
-
- copy_size = min(count, (size_t)src_size);
- not_done = copy_to_user(buf + total, src, copy_size);
- copy_size -= not_done;
- total += copy_size;
- count -= copy_size;
- data->crypt_offset += copy_size;
- if (copy_size == 0) {
- if (total == 0)
- return -EFAULT;
-
- break;
- }
- }
-
- *offp += total;
- return total;
+ size_t copy_size;
+ size_t not_done;
+ size_t pg_off;
+ void *src;
+ ssize_t src_size;
+
+ if (!count)
+ return 0;
+
+ pg_off = *offp & (PAGE_SIZE - 1);
+ count = min_t(size_t, count, PAGE_SIZE - pg_off);
+
+ src_size = snapshot_read_next_encrypted(data, &src);
+ if (src_size <= 0)
+ return src_size;
+
+ copy_size = min_t(size_t, count, src_size);
+ not_done = copy_to_user(buf, src, copy_size);
+ copy_size -= not_done;
+ if (!copy_size)
+ return -EFAULT;
+
+ data->crypt_offset += copy_size;
+ *offp += copy_size;
+ snapshot_record_encrypted_read(data, copy_size);
+ return copy_size;
}
ssize_t snapshot_write_encrypted(struct snapshot_data *data,
const char __user *buf, size_t count,
loff_t *offp)
{
- ssize_t total = 0;
+ size_t copy_size;
+ size_t not_done;
+ size_t pg_off;
+ void *dst;
+ ssize_t dst_size;
+ int rc;
- /* Loop getting buffers of varying sizes and copying from. */
- while (count) {
- size_t copy_size;
- size_t not_done;
- void *dst;
- ssize_t dst_size = snapshot_write_next_encrypted(data, &dst);
+ if (!count)
+ return 0;
- if (dst_size <= 0) {
- if (total == 0)
- return dst_size;
+ pg_off = *offp & (PAGE_SIZE - 1);
+ count = min_t(size_t, count, PAGE_SIZE - pg_off);
- break;
- }
+ dst_size = snapshot_write_next_encrypted(data, &dst);
+ if (dst_size <= 0)
+ return dst_size;
- copy_size = min(count, (size_t)dst_size);
- not_done = copy_from_user(dst, buf + total, copy_size);
- copy_size -= not_done;
- total += copy_size;
- count -= copy_size;
- data->crypt_offset += copy_size;
- if (copy_size == 0) {
- if (total == 0)
- return -EFAULT;
-
- break;
- }
+ copy_size = min_t(size_t, count, dst_size);
+ not_done = copy_from_user(dst, buf, copy_size);
+ copy_size -= not_done;
+ if (!copy_size)
+ return -EFAULT;
- /*
- * Drain the encrypted buffer if it's full, or if we hit the end
- * of the meta pages and need a key change.
- */
- if (data->crypt_offset >=
- (PAGE_SIZE * CHUNK_SIZE) + SNAPSHOT_AUTH_TAG_SIZE ||
- (data->meta_size &&
- data->crypt_total < data->meta_size &&
- data->crypt_total + data->crypt_offset ==
- data->meta_size + SNAPSHOT_AUTH_TAG_SIZE)) {
- int rc;
-
- rc = snapshot_decrypt_drain(data);
- if (rc < 0)
- return rc;
- }
+ data->crypt_offset += copy_size;
+ data->crypt_stream_total += copy_size;
+ /*
+ * Drain the encrypted buffer if it's full, or if we hit the end of the
+ * encrypted metadata and need a key change before user data pages.
+ */
+ if (data->crypt_offset >=
+ (PAGE_SIZE * CHUNK_SIZE) + SNAPSHOT_AUTH_TAG_SIZE ||
+ (data->crypt_meta_size &&
+ data->crypt_stream_total == data->crypt_meta_size)) {
+ rc = snapshot_decrypt_drain(data);
+ if (rc < 0)
+ return rc;
}
- *offp += total;
- return total;
+ *offp += copy_size;
+ return copy_size;
+}
+
+static void snapshot_reset_encryption_state(struct snapshot_data *data)
+{
+ data->crypt_offset = 0;
+ data->crypt_size = 0;
+ data->crypt_total = 0;
+ data->crypt_stream_total = 0;
+ data->nonce_low = 0;
+ data->nonce_high = 0;
+ data->meta_size = 0;
+ data->crypt_meta_size = 0;
+ data->user_key_valid = false;
+ spin_lock(&data->crypt_lock);
+ data->crypt_bytes_read = 0;
+ data->crypt_swap_reserved = 0;
+ data->crypt_header_reserved = 0;
+ spin_unlock(&data->crypt_lock);
+ memset(data->auth_tag, 0, sizeof(data->auth_tag));
}
void snapshot_teardown_encryption(struct snapshot_data *data)
@@ -623,21 +647,20 @@ void snapshot_teardown_encryption(struct snapshot_data *data)
memzero_explicit(data->encryption_key, sizeof(data->encryption_key));
memzero_explicit(data->user_key, sizeof(data->user_key));
+ snapshot_reset_encryption_state(data);
}
static int snapshot_setup_encryption_common(struct snapshot_data *data)
{
int i, rc;
- data->crypt_total = 0;
- data->crypt_offset = 0;
- data->crypt_size = 0;
- data->user_key_valid = false;
- memset(data->crypt_pages, 0, sizeof(data->crypt_pages));
/* This only works once per hibernate. */
if (data->aead_tfm)
return -EINVAL;
+ snapshot_reset_encryption_state(data);
+ memset(data->crypt_pages, 0, sizeof(data->crypt_pages));
+
/* Set up the encryption transform */
data->aead_tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
if (IS_ERR(data->aead_tfm)) {
@@ -653,7 +676,7 @@ static int snapshot_setup_encryption_common(struct snapshot_data *data)
/* Allocate the staging area */
for (i = 0; i < CHUNK_SIZE; i++) {
- data->crypt_pages[i] = (void *)__get_free_page(GFP_ATOMIC);
+ data->crypt_pages[i] = (void *)__get_free_page(GFP_KERNEL);
if (!data->crypt_pages[i])
goto setup_fail;
}
@@ -714,13 +737,15 @@ int snapshot_get_encryption_key(struct snapshot_data *data,
BUILD_BUG_ON(sizeof(wrapped) >
sizeof(((struct uswsusp_key_blob *)0)->blob));
- rc = copy_to_user(&key->blob, &wrapped, sizeof(wrapped));
- if (rc)
+ if (copy_to_user(&key->blob, &wrapped, sizeof(wrapped))) {
+ rc = -EFAULT;
goto fail;
+ }
- rc = copy_to_user(&key->nonce, &nonce, sizeof(nonce));
- if (rc)
+ if (copy_to_user(&key->nonce, &nonce, sizeof(nonce))) {
+ rc = -EFAULT;
goto fail;
+ }
memzero_explicit(image_key, sizeof(image_key));
memzero_explicit(&wrapped, sizeof(wrapped));
@@ -801,47 +826,73 @@ static loff_t snapshot_encrypted_byte_count(loff_t plain_size)
return plain_size + (chunks * SNAPSHOT_AUTH_TAG_SIZE);
}
-static loff_t snapshot_get_meta_data_size(void)
+static loff_t snapshot_encrypted_split_byte_count(loff_t raw_size,
+ loff_t meta_plain_size)
{
- loff_t pages = snapshot_get_meta_page_count();
+ if (raw_size <= meta_plain_size)
+ return snapshot_encrypted_byte_count(raw_size);
- return snapshot_encrypted_byte_count(pages << PAGE_SHIFT);
+ return snapshot_encrypted_byte_count(meta_plain_size) +
+ snapshot_encrypted_byte_count(raw_size - meta_plain_size);
}
int snapshot_set_user_key(struct snapshot_data *data,
struct uswsusp_user_key __user *key)
{
- struct uswsusp_user_key user_key;
+ struct uswsusp_user_key user_key = {};
unsigned int key_len;
u64 size;
int rc;
- /*
- * Return the metadata size, the number of bytes that can be fed in before
- * the user data key is needed at resume time.
- */
- size = snapshot_get_meta_data_size();
- rc = put_user(size, &key->meta_size);
- if (rc)
- return rc;
+ if (!snapshot_encryption_enabled(data))
+ return -EINVAL;
- rc = copy_from_user(&user_key, key, sizeof(struct uswsusp_user_key));
- if (rc)
- return rc;
+ if (copy_from_user(&user_key, key, sizeof(struct uswsusp_user_key)))
+ return -EFAULT;
BUILD_BUG_ON(sizeof(data->user_key) < sizeof(user_key.key));
+ rc = -EINVAL;
if (user_key.reserved)
- return -EINVAL;
+ goto out;
if (user_key.key_len > sizeof(data->user_key))
- return -EINVAL;
+ goto out;
if (user_key.key_len < 8)
- return -EINVAL;
+ goto out;
+
+ if (data->mode == O_RDONLY && !data->ready) {
+ rc = -ENODATA;
+ goto out;
+ }
+
+ /*
+ * Return the metadata size, the number of bytes that can be fed in before
+ * the user data key is needed at resume time.
+ */
+ if (data->mode == O_WRONLY && !data->meta_size) {
+ if (user_key.meta_size < PAGE_SIZE + SNAPSHOT_AUTH_TAG_SIZE)
+ goto out;
+
+ data->crypt_meta_size = user_key.meta_size;
+ size = data->crypt_meta_size;
+ } else {
+ snapshot_set_meta_size(data,
+ snapshot_get_meta_page_count() << PAGE_SHIFT);
+ size = data->crypt_meta_size;
+ }
+
+ rc = put_user(size, &key->meta_size);
+ if (rc)
+ goto out;
key_len = user_key.key_len;
/* Don't allow it if it's too late. */
- if (data->crypt_total > data->meta_size)
- return -EBUSY;
+ if ((data->meta_size && data->crypt_total > data->meta_size) ||
+ (data->crypt_meta_size &&
+ data->crypt_stream_total > data->crypt_meta_size)) {
+ rc = -EBUSY;
+ goto out;
+ }
memset(data->user_key, 0, sizeof(data->user_key));
memcpy(data->user_key, user_key.key, key_len);
@@ -849,19 +900,30 @@ int snapshot_set_user_key(struct snapshot_data *data,
/* Install the key if the user is just under the wire. */
rc = snapshot_check_user_key_switch(data);
if (rc)
- return rc;
+ goto out;
- return 0;
+ rc = 0;
+
+out:
+ memzero_explicit(&user_key, sizeof(user_key));
+ return rc;
}
-loff_t snapshot_get_encrypted_image_size(loff_t raw_size)
+loff_t snapshot_get_encrypted_image_size(struct snapshot_data *data,
+ loff_t raw_size)
{
- loff_t pages = raw_size >> PAGE_SHIFT;
- loff_t meta_size;
+ loff_t meta_plain_size = data->meta_size;
+ loff_t split_size;
+
+ if (!meta_plain_size)
+ meta_plain_size = snapshot_get_meta_page_count() << PAGE_SHIFT;
+
+ split_size = snapshot_encrypted_split_byte_count(raw_size,
+ meta_plain_size);
+ if (!data->user_key_valid)
+ return max(snapshot_encrypted_byte_count(raw_size), split_size);
- pages -= snapshot_get_meta_page_count();
- meta_size = snapshot_get_meta_data_size();
- return snapshot_encrypted_byte_count(pages << PAGE_SHIFT) + meta_size;
+ return split_size;
}
int snapshot_finalize_decrypted_image(struct snapshot_data *data)
diff --git a/kernel/power/swap.c b/kernel/power/swap.c
index c626e9dc3c1c..3bdd58d5b62c 100644
--- a/kernel/power/swap.c
+++ b/kernel/power/swap.c
@@ -166,6 +166,104 @@ static int swsusp_extents_insert(unsigned long swap_offset)
return 0;
}
+static bool swsusp_extents_contain_range(unsigned long swap_offset,
+ unsigned long nr_pages)
+{
+ struct rb_node *node = swsusp_extents.rb_node;
+ struct swsusp_extent *ext;
+ unsigned long end;
+
+ if (!nr_pages)
+ return false;
+
+ end = swap_offset + nr_pages - 1;
+ if (end < swap_offset)
+ return false;
+
+ while (node) {
+ ext = rb_entry(node, struct swsusp_extent, node);
+ if (swap_offset < ext->start)
+ node = node->rb_left;
+ else if (swap_offset > ext->end)
+ node = node->rb_right;
+ else
+ goto found;
+ }
+
+ return false;
+
+found:
+ for (;;) {
+ if (swap_offset < ext->start)
+ return false;
+ if (end <= ext->end)
+ return true;
+ if (ext->end == (unsigned long)-1)
+ return false;
+
+ swap_offset = ext->end + 1;
+ node = rb_next(&ext->node);
+ if (!node)
+ return false;
+
+ ext = rb_entry(node, struct swsusp_extent, node);
+ }
+}
+
+bool swsusp_swap_range_allocated(int swap, loff_t pos, size_t count)
+{
+ u64 block;
+ u64 end;
+ u64 end_block;
+
+ if (swap < 0 || pos < 0 || !count)
+ return false;
+
+ end = (u64)pos + count - 1;
+ if (end < (u64)pos)
+ return false;
+
+ block = (u64)pos >> PAGE_SHIFT;
+ end_block = end >> PAGE_SHIFT;
+
+ for (;;) {
+ u64 remaining_pages = end_block - block + 1;
+ pgoff_t swap_offset;
+ pgoff_t mapped_pages;
+ sector_t page_block = block;
+ u64 pages;
+
+ if (!remaining_pages)
+ return false;
+ if ((u64)page_block != block)
+ return false;
+ if (swapdev_block_to_extent(swap, page_block, &swap_offset,
+ &mapped_pages))
+ return false;
+ if (!mapped_pages)
+ return false;
+ pages = min_t(u64, mapped_pages, remaining_pages);
+ if ((u64)(unsigned long)pages != pages)
+ return false;
+ if (!swsusp_extents_contain_range(swap_offset, pages))
+ return false;
+ if (pages == remaining_pages)
+ return true;
+ block += pages;
+ }
+}
+
+u64 swsusp_swap_map_bytes(u64 image_bytes)
+{
+ u64 image_pages = DIV_ROUND_UP_ULL(image_bytes, PAGE_SIZE);
+ u64 map_pages = image_pages / MAP_PAGE_ENTRIES + 1;
+
+ if (map_pages > U64_MAX >> PAGE_SHIFT)
+ return U64_MAX;
+
+ return map_pages << PAGE_SHIFT;
+}
+
sector_t alloc_swapdev_block(int swap)
{
unsigned long offset;
diff --git a/kernel/power/user.c b/kernel/power/user.c
index d30aed4bc35f..57b8ce83a626 100644
--- a/kernel/power/user.c
+++ b/kernel/power/user.c
@@ -21,6 +21,8 @@
#include <linux/console.h>
#include <linux/cpu.h>
#include <linux/freezer.h>
+#include <linux/pid.h>
+#include <linux/sched/signal.h>
#include <linux/security.h>
#include <linux/uaccess.h>
@@ -33,12 +35,136 @@ struct snapshot_data snapshot_state;
int is_hibernate_resume_dev(dev_t dev)
{
- return hibernation_snapshot_dev_available() && snapshot_state.dev == dev;
+ return hibernation_available() && snapshot_state.dev == dev;
}
-static bool snapshot_encryption_required(void)
+#if defined(CONFIG_ENCRYPTED_HIBERNATION)
+static bool snapshot_encrypted_output_active(struct snapshot_data *data, dev_t dev)
{
- return security_locked_down(LOCKDOWN_HIBERNATION);
+ return data->encryption_required && data->mode == O_RDONLY &&
+ data->ready && data->dev == dev &&
+ data->owner_tgid == task_tgid(current) &&
+ snapshot_encryption_enabled(data);
+}
+
+static bool snapshot_header_write_range(struct snapshot_data *data,
+ loff_t pos, size_t count)
+{
+ loff_t header_offset = data->swap_header_offset;
+ loff_t offset;
+
+ if (pos < header_offset)
+ return false;
+
+ offset = pos - header_offset;
+ return offset < PAGE_SIZE && count <= PAGE_SIZE - offset;
+}
+
+static u64 snapshot_swap_write_budget(struct snapshot_data *data)
+{
+ u64 image_bytes = round_up(data->crypt_bytes_read, PAGE_SIZE);
+ u64 map_bytes = swsusp_swap_map_bytes(data->crypt_bytes_read);
+ u64 budget = image_bytes + map_bytes;
+
+ if (image_bytes < data->crypt_bytes_read || budget < image_bytes)
+ return U64_MAX;
+
+ return budget;
+}
+
+static void snapshot_reset_swap_write_reservation(struct snapshot_data *data)
+{
+ spin_lock(&data->crypt_lock);
+ data->crypt_swap_reserved = 0;
+ spin_unlock(&data->crypt_lock);
+}
+#endif
+
+enum hibernate_snapshot_write
+hibernate_snapshot_write_begin(dev_t dev, loff_t pos, size_t count)
+{
+#if defined(CONFIG_ENCRYPTED_HIBERNATION)
+ struct snapshot_data *data = &snapshot_state;
+ enum hibernate_snapshot_write type = HIBERNATE_SNAPSHOT_WRITE_NONE;
+ bool image_range_allocated;
+ u64 swap_budget;
+
+ if (!count || !snapshot_encrypted_output_active(data, dev))
+ return HIBERNATE_SNAPSHOT_WRITE_NONE;
+
+ mutex_lock(&system_transition_mutex);
+
+ if (!snapshot_encrypted_output_active(data, dev))
+ goto unlock;
+
+ image_range_allocated = swsusp_swap_range_allocated(data->swap, pos, count);
+
+ spin_lock(&data->crypt_lock);
+ swap_budget = snapshot_swap_write_budget(data);
+ if (snapshot_header_write_range(data, pos, count) &&
+ data->crypt_header_reserved <= PAGE_SIZE &&
+ PAGE_SIZE - data->crypt_header_reserved >= count) {
+ data->crypt_header_reserved += count;
+ type = HIBERNATE_SNAPSHOT_WRITE_HEADER;
+ } else if (image_range_allocated &&
+ swap_budget >= data->crypt_swap_reserved &&
+ swap_budget - data->crypt_swap_reserved >= count) {
+ data->crypt_swap_reserved += count;
+ type = HIBERNATE_SNAPSHOT_WRITE_IMAGE;
+ }
+ spin_unlock(&data->crypt_lock);
+
+ if (type == HIBERNATE_SNAPSHOT_WRITE_NONE)
+ goto unlock;
+
+ return type;
+
+unlock:
+ mutex_unlock(&system_transition_mutex);
+ return HIBERNATE_SNAPSHOT_WRITE_NONE;
+#else
+ return HIBERNATE_SNAPSHOT_WRITE_NONE;
+#endif
+}
+
+void hibernate_snapshot_write_end(enum hibernate_snapshot_write type,
+ size_t reserved, ssize_t written)
+{
+#if defined(CONFIG_ENCRYPTED_HIBERNATION)
+ struct snapshot_data *data = &snapshot_state;
+ u64 *reserved_total;
+ size_t unused;
+
+ if (type == HIBERNATE_SNAPSHOT_WRITE_NONE)
+ return;
+ if (!reserved)
+ goto unlock;
+
+ if (type == HIBERNATE_SNAPSHOT_WRITE_HEADER)
+ unused = reserved;
+ else if (written <= 0)
+ unused = reserved;
+ else if (written < reserved)
+ unused = reserved - written;
+ else
+ goto unlock;
+
+ reserved_total = type == HIBERNATE_SNAPSHOT_WRITE_HEADER ?
+ &data->crypt_header_reserved : &data->crypt_swap_reserved;
+
+ spin_lock(&data->crypt_lock);
+ *reserved_total -= min_t(u64, *reserved_total, unused);
+ spin_unlock(&data->crypt_lock);
+
+unlock:
+ mutex_unlock(&system_transition_mutex);
+#endif
+}
+
+static bool snapshot_encryption_required(struct snapshot_data *data)
+{
+ data->encryption_required = security_locked_down(LOCKDOWN_HIBERNATION);
+ return data->encryption_required;
}
static int snapshot_open(struct inode *inode, struct file *filp)
@@ -66,6 +192,14 @@ static int snapshot_open(struct inode *inode, struct file *filp)
data = &snapshot_state;
filp->private_data = data;
memset(&data->handle, 0, sizeof(struct snapshot_handle));
+#if defined(CONFIG_ENCRYPTED_HIBERNATION)
+ spin_lock_init(&data->crypt_lock);
+ data->crypt_bytes_read = 0;
+ data->crypt_swap_reserved = 0;
+ data->crypt_header_reserved = 0;
+ data->swap_header_offset = 0;
+ data->owner_tgid = NULL;
+#endif
if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
/* Hibernating. The image device should be accessible. */
data->swap = pin_hibernation_swap_type(swsusp_resume_device, 0);
@@ -96,7 +230,11 @@ static int snapshot_open(struct inode *inode, struct file *filp)
data->ready = false;
data->platform_support = false;
data->dev = 0;
- data->encryption_required = snapshot_encryption_required();
+ data->encryption_required = snapshot_encryption_required(data);
+#if defined(CONFIG_ENCRYPTED_HIBERNATION)
+ if (!error)
+ data->owner_tgid = get_task_pid(current, PIDTYPE_TGID);
+#endif
unlock:
unlock_system_sleep(sleep_flags);
@@ -114,6 +252,10 @@ static int snapshot_release(struct inode *inode, struct file *filp)
swsusp_free();
data = filp->private_data;
data->dev = 0;
+#if defined(CONFIG_ENCRYPTED_HIBERNATION)
+ put_pid(data->owner_tgid);
+ data->owner_tgid = NULL;
+#endif
free_all_swap_pages(data->swap);
unpin_hibernation_swap_type(data->swap);
if (data->frozen) {
@@ -148,7 +290,8 @@ static ssize_t snapshot_read(struct file *filp, char __user *buf,
res = -ENODATA;
goto unlock;
}
- if (data->encryption_required && !snapshot_encryption_enabled(data)) {
+ if (snapshot_encryption_required(data) &&
+ !snapshot_encryption_enabled(data)) {
res = -EPERM;
goto unlock;
}
@@ -194,7 +337,8 @@ static ssize_t snapshot_write(struct file *filp, const char __user *buf,
data = filp->private_data;
- if (data->encryption_required && !snapshot_encryption_enabled(data)) {
+ if (snapshot_encryption_required(data) &&
+ !snapshot_encryption_enabled(data)) {
res = -EPERM;
goto unlock;
}
@@ -271,6 +415,9 @@ static int snapshot_set_swap_area(struct snapshot_data *data,
if (data->swap < 0)
return swdev ? -ENODEV : -EINVAL;
data->dev = swdev;
+#if defined(CONFIG_ENCRYPTED_HIBERNATION)
+ data->swap_header_offset = (loff_t)offset << PAGE_SHIFT;
+#endif
return 0;
}
@@ -337,7 +484,8 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
error = -EPERM;
break;
}
- if (data->encryption_required && !snapshot_encryption_enabled(data)) {
+ if (snapshot_encryption_required(data) &&
+ !snapshot_encryption_enabled(data)) {
error = -EPERM;
break;
}
@@ -351,7 +499,8 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
break;
case SNAPSHOT_ATOMIC_RESTORE:
- if (data->encryption_required && !snapshot_encryption_enabled(data)) {
+ if (snapshot_encryption_required(data) &&
+ !snapshot_encryption_enabled(data)) {
error = -EPERM;
break;
}
@@ -379,6 +528,7 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
swsusp_free();
memset(&data->handle, 0, sizeof(struct snapshot_handle));
data->ready = false;
+ snapshot_teardown_encryption(data);
/*
* It is necessary to thaw kernel threads here, because
* SNAPSHOT_CREATE_IMAGE may be invoked directly after
@@ -402,7 +552,7 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
size = snapshot_get_image_size();
size <<= PAGE_SHIFT;
if (snapshot_encryption_enabled(data))
- size = snapshot_get_encrypted_image_size(size);
+ size = snapshot_get_encrypted_image_size(data, size);
error = put_user(size, (loff_t __user *)arg);
break;
@@ -432,6 +582,9 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
break;
}
free_all_swap_pages(data->swap);
+#if defined(CONFIG_ENCRYPTED_HIBERNATION)
+ snapshot_reset_swap_write_reservation(data);
+#endif
break;
case SNAPSHOT_S2RAM:
@@ -439,7 +592,8 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
error = -EPERM;
break;
}
- if (data->encryption_required && !snapshot_encryption_enabled(data)) {
+ if (snapshot_encryption_required(data) &&
+ !snapshot_encryption_enabled(data)) {
error = -EPERM;
break;
}
@@ -456,7 +610,8 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
break;
case SNAPSHOT_POWER_OFF:
- if (data->encryption_required && !snapshot_encryption_enabled(data)) {
+ if (snapshot_encryption_required(data) &&
+ !snapshot_encryption_enabled(data)) {
error = -EPERM;
break;
}
diff --git a/kernel/power/user.h b/kernel/power/user.h
index 3c3498bdb752..4df2f9ae9ada 100644
--- a/kernel/power/user.h
+++ b/kernel/power/user.h
@@ -2,10 +2,13 @@
#include <linux/crypto.h>
#include <linux/scatterlist.h>
+#include <linux/spinlock.h>
#include <linux/suspend_ioctls.h>
#include <crypto/aead.h>
#include <crypto/aes.h>
+struct pid;
+
#define SNAPSHOT_ENCRYPTION_KEY_SIZE AES_KEYSIZE_128
#define SNAPSHOT_AUTH_TAG_SIZE 16
@@ -32,12 +35,25 @@ struct snapshot_data {
size_t crypt_offset;
size_t crypt_size;
u64 crypt_total;
+ u64 crypt_stream_total;
u64 nonce_low;
u64 nonce_high;
u8 encryption_key[SNAPSHOT_ENCRYPTION_KEY_SIZE] __nonstring;
u8 user_key[USWSUSP_USER_KEY_SIZE] __nonstring;
bool user_key_valid;
u64 meta_size;
+ u64 crypt_meta_size;
+ /*
+ * Protects crypt_bytes_read and the reservation counters below.
+ * crypt_swap_reserved covers encrypted snapshot bytes and userspace
+ * generated swap-map pages.
+ */
+ spinlock_t crypt_lock;
+ u64 crypt_bytes_read;
+ u64 crypt_swap_reserved;
+ u64 crypt_header_reserved;
+ loff_t swap_header_offset;
+ struct pid *owner_tgid;
#endif
};
@@ -66,7 +82,8 @@ int snapshot_set_user_key(struct snapshot_data *data,
int snapshot_store_encryption_seed(const char *buf, size_t count);
-loff_t snapshot_get_encrypted_image_size(loff_t raw_size);
+loff_t snapshot_get_encrypted_image_size(struct snapshot_data *data,
+ loff_t raw_size);
int snapshot_finalize_decrypted_image(struct snapshot_data *data);
@@ -115,7 +132,8 @@ static inline int snapshot_store_encryption_seed(const char *buf, size_t count)
return -ENOTTY;
}
-static inline loff_t snapshot_get_encrypted_image_size(loff_t raw_size)
+static inline loff_t snapshot_get_encrypted_image_size(struct snapshot_data *data,
+ loff_t raw_size)
{
return raw_size;
}
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 78b49b0658ad..11df0c556f4e 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2366,6 +2366,38 @@ sector_t swapdev_block(int type, pgoff_t offset)
return se->start_block + (offset - se->start_page);
}
+int swapdev_block_to_extent(int type, sector_t block, pgoff_t *offset,
+ pgoff_t *nr_pages)
+{
+ struct swap_info_struct *si = swap_type_to_info(type);
+ struct swap_extent *se;
+ struct rb_node *rb;
+
+ if (!si || !(si->flags & SWP_WRITEOK))
+ return -ENODEV;
+
+ for (rb = rb_first(&si->swap_extent_root); rb; rb = rb_next(rb)) {
+ se = rb_entry(rb, struct swap_extent, rb_node);
+ if (block >= se->start_block &&
+ block - se->start_block < se->nr_pages) {
+ pgoff_t page = block - se->start_block;
+
+ *offset = se->start_page + page;
+ *nr_pages = se->nr_pages - page;
+ return 0;
+ }
+ }
+
+ return -ENOENT;
+}
+
+int swapdev_block_to_offset(int type, sector_t block, pgoff_t *offset)
+{
+ pgoff_t nr_pages;
+
+ return swapdev_block_to_extent(type, block, offset, &nr_pages);
+}
+
/*
* Return either the total number of swap pages of given type, or the number
* of free pages of that type (depending on @free)
^ permalink raw reply related [flat|nested] 5+ messages in thread