From nobody Fri Oct 2 06:57:22 2026 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E6D5B44E657 for ; Tue, 4 Aug 2026 10:14:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785838487; cv=none; b=k9oV/jcHzAjUBDvRAZyInHKzj0KSDvEkxpJYBfx9IV5T9If7JFhyurmYEZ0RWSL8YvCln7dLBT6W1dw+4owpDN/cOl0+vKgJHdwwYK1lVCMFkD+mhGj3jQFv2iY6hv59CtUrdsH/ICj4RdAJ+563wuOIMbXdIBDAYrQktpZJCas= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785838487; c=relaxed/simple; bh=j9v+kPtnhAy1loPkUZkeYV3HfLS1SzVx7adZx9IUgGU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C0swleXl1sxyy/DRTunBaTHAyhSphfb511/dOZpkwoDZhg+tL5KSipftBS5kxypi1oTpZk0sA6G3tcmJP0Jyw3m7wago+0mbQDCZpu/Fkaz0Zih0HUE3+tAvyBPtfgJ3YcqomtzkpcjBnsPp1BBlL9im9UqAw6/Nomh7KGCjOmk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=starlabs.systems; spf=pass smtp.mailfrom=starlabs.systems; dkim=pass (2048-bit key) header.d=starlabs-systems.20251104.gappssmtp.com header.i=@starlabs-systems.20251104.gappssmtp.com header.b=reN3c3QT; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=starlabs.systems Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=starlabs.systems Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=starlabs-systems.20251104.gappssmtp.com header.i=@starlabs-systems.20251104.gappssmtp.com header.b="reN3c3QT" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-4954afac04bso35693845e9.0 for ; Tue, 04 Aug 2026 03:14:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=starlabs-systems.20251104.gappssmtp.com; s=20251104; t=1785838482; x=1786443282; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=XfRzMp3vXBu20BLR1Ps8zfE52+NCuZcvVo+z59S9I8o=; b=reN3c3QTjskabn9GghQp5FMbAraglbq2AxEhtdpmLRaYW+xDM7oTxvF0jxII8vD2Ku rvgfvKYIgdhi28XX7cENMvc3W60PyRXy/2odpSUW4cv1leRZtCn3ZWtMcQ4JiMkxOHao Jcvx/AV80KZE8c5g5flH4FeDDCx4Ma59qHaJqi04ByiuRxbeBgD3LjAC79APn5nAano9 3CW0pNdpzjUgc+9d5ebqvZq8T73UTRkSjaKH8OfvqxcBjcdZp954BIQts+rqA3QFUwxB bGNfkNHtYI8cLL1Nib941nrQuNjIw3Bt0DNMzRINvVKSFqLtmV4vNoHdWimFuVlqNNXZ casQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785838482; x=1786443282; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=XfRzMp3vXBu20BLR1Ps8zfE52+NCuZcvVo+z59S9I8o=; b=AUkJTGWV3pcuJ8RDmqCJimq01a++SBPE+/MktO5na2+FE2AhSRxRPBSS4JCu4UyCzD KKf3Q9VtX++zkT4CwUtOCrR8oN0txscglXRJ39UMyEMwNwKYsHmb2+/PeO2tZDPNBj8w F32KphURnYCfXQiOn+GuY/SI0Q6pjNlbt/nSBDL4OVGPjqK/cwpJBNHsT9mBvyrvOFBJ iQ1F5VWdVhtSxO/N/D/iBNZxqRAXO39Du+vXYukl+PL1ijTtoOFbo1bRACRWr3M2BqRj +YcUj85M4EQPwFvd/cjPM7BQKBa1URXXy9vDiuOYYNBDvuWe6tJqXKx6L4AbFLwOCedt tn9g== X-Forwarded-Encrypted: i=1; AHgh+RpCetuJntQAKzDAOQNSdM+UcsfSRJYKRJ3+1/Yel9l4NCAuCRVJ/JEcRwDPMknBxru43pjDI6DSDJe8TL0=@vger.kernel.org X-Gm-Message-State: AOJu0YyKgxSpks/TKf4iczg32RLvnxkGH/fFC5VupUhDiG6GJHu69Oam WlsNAJx+WXfwOYDhulzgE18i8a0oTMwuCVmUO76WhDfyUPBz2nJ52K+KbCBW5sMx4g== X-Gm-Gg: AR+sD11IZ7l1m44ZQGg/Ipgg2tN7TcdUIno7Z2v3jV43eEY+Qfd/vgArKFTFQ/Me7Kk 85K9TnG3peDxiCiGn48R4PHupwlJ4MtuSo8kYOa1girzKejyBKy7knf2ZJqa0RwFhiaPags/DNC qBBlDPiOtY06ji7GGHzwXK3Iny5SDqSya1lIf4SAcWJlMCtWjZ/frNJkNCGig2j3AVHc6Xeng7A QpNB7DIscoytD3o8k00mDX8f+Wr7uuJpdvQPYXjLoKud2gI2nTWx+QYllchfYbXRU35TTbuz5Id 23sINBphe67iY2LR6vpsNLmzkdTVvKK+2EwpMw3ZmswyDVWOkKn2O7DQNXHgeP/Is7FIB3dC1gj u0Af6TVB/PQUwuhoQ7tk2r6DpL+WcFWaHc/aphVzt0/jRHa7KPCNso3IV369ISL+TdfNkfXHGOD IBpIOwFULlfpEspLvrs+fkQM5SpxYMWfGz+sWVRpJbMm34Kcu9wfAjIjyUGA1aykuzwXaFTia5C qcSVD1NOIKN/fs++4v1jp70W7LPA+03 X-Received: by 2002:a05:600c:3f0d:b0:495:641a:bd3f with SMTP id 5b1f17b1804b1-4980c673891mr331648735e9.13.1785838481838; Tue, 04 Aug 2026 03:14:41 -0700 (PDT) Received: from horizon.localdomain ([216.128.28.240]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4994a0f7bf4sm73248805e9.11.2026.08.04.03.14.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Aug 2026 03:14:41 -0700 (PDT) From: Sean Rhodes To: linux-pm@vger.kernel.org Cc: "Rafael J. Wysocki" , Len Brown , Pavel Machek , Sean Rhodes , Evan Green , linux-kernel@vger.kernel.org, Xueqin Luo , Jens Axboe , Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , linux-block@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 1/4] PM: hibernate: add seed-wrapped encrypted snapshots Date: Tue, 4 Aug 2026 11:14:34 +0100 Message-ID: X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Evan Green 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 Co-developed-by: Sean Rhodes Signed-off-by: Sean Rhodes --- 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) =20 +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 f= rom the kernel. It has the following limitations: =20 diff --git a/include/uapi/linux/suspend_ioctls.h b/include/uapi/linux/suspe= nd_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)); =20 +#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 hib= ernate + * 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_lof= f_t) #define SNAPSHOT_ALLOC_SWAP_PAGE _IOR(SNAPSHOT_IOC_MAGIC, 20, __kernel_lof= f_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 =20 #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. =20 +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) +=3D suspend.o obj-$(CONFIG_PM_TEST_SUSPEND) +=3D suspend_test.o obj-$(CONFIG_HIBERNATION) +=3D hibernate.o snapshot.o swap.o obj-$(CONFIG_HIBERNATION_SNAPSHOT_DEV) +=3D user.o +obj-$(CONFIG_ENCRYPTED_HIBERNATION) +=3D snapenc.o obj-$(CONFIG_PM_AUTOSLEEP) +=3D autosleep.o obj-$(CONFIG_PM_WAKELOCKS) +=3D wakelock.o =20 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 =20 #include "power.h" +#include "user.h" =20 =20 static int nocompress; @@ -1376,12 +1377,35 @@ static ssize_t reserved_size_store(struct kobject *= kobj, =20 power_attr(reserved_size); =20 +#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 =3D snapshot_store_encryption_seed(buf, n); + return ret ? ret : n; +} + +static struct kobj_attribute snapshot_seed_attr =3D { + .attr =3D { + .name =3D "snapshot_seed", + .mode =3D 0200, + }, + .store =3D snapshot_seed_store, +}; +#endif + static struct attribute *g[] =3D { &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, }; =20 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 { =20 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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] =3D { + '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 =3D count; + int ret; + + if (len && buf[len - 1] =3D=3D '\n') + len--; + if (len !=3D SNAPSHOT_SEED_SIZE * 2) + return -EINVAL; + + ret =3D hex2bin(seed, buf, sizeof(seed)); + if (ret) + return ret; + + mutex_lock(&snapshot_seed_mutex); + if (snapshot_seed_valid) { + ret =3D memcmp(snapshot_seed, seed, sizeof(seed)) ? -EPERM : 0; + goto out; + } + + memcpy(snapshot_seed, seed, sizeof(seed)); + snapshot_seed_valid =3D 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 =3D 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_SIZ= E]) +{ + static const char label[] =3D "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 =3D snapshot_derive_wrapping_key(wrap_key); + if (rc) + return rc; + + tfm =3D crypto_alloc_aead("gcm(aes)", 0, 0); + if (IS_ERR(tfm)) { + rc =3D PTR_ERR(tfm); + goto out_key; + } + + rc =3D crypto_aead_setkey(tfm, wrap_key, sizeof(wrap_key)); + if (rc) + goto out_tfm; + + rc =3D crypto_aead_setauthsize(tfm, SNAPSHOT_AUTH_TAG_SIZE); + if (rc) + goto out_tfm; + + req =3D aead_request_alloc(tfm, GFP_KERNEL); + if (!req) { + rc =3D -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 =3D 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 =3D cpu_to_le32(SNAPSHOT_KEY_BLOB_VERSION); + + memcpy(key, image_key, sizeof(key)); + rc =3D 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 *wr= apped, + 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) !=3D SNAPSHOT_KEY_BLOB_VERSION) + return -EINVAL; + + tmp =3D *wrapped; + rc =3D 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 =3D 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 =3D 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 =3D=3D 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 =3D data->aead_req; + u8 nonce[GCM_AES_IV_SIZE]; + DECLARE_CRYPTO_WAIT(wait); + size_t total =3D 0; + int pg_idx; + int res; + + if (data->crypt_total =3D=3D 0) { + data->meta_size =3D snapshot_get_meta_page_count() << PAGE_SHIFT; + } else { + res =3D 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 =3D 0; pg_idx < CHUNK_SIZE; pg_idx++) { + void *buf =3D data->crypt_pages[pg_idx]; + + /* Stop at the meta page boundary to potentially switch keys. */ + if (total && + ((data->crypt_total + total) =3D=3D data->meta_size)) + break; + + res =3D snapshot_read_next(&data->handle); + if (res < 0) + return res; + if (res =3D=3D 0) + break; + + WARN_ON(res !=3D 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 +=3D 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 +=3D 1; + /* Total does not include AAD or the auth tag. */ + aead_request_set_crypt(req, data->sg, data->sg, total, nonce); + res =3D crypto_wait_req(crypto_aead_encrypt(req), &wait); + if (res) + return res; + + data->crypt_size =3D total; + data->crypt_total +=3D 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 =3D 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 =3D data->crypt_offset >> PAGE_SHIFT; + total =3D page_count << PAGE_SHIFT; + if (total =3D=3D 0 || total =3D=3D 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 =3D 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) =3D=3D sizeof(data->auth_tag)) { + memcpy(data->auth_tag, data->crypt_pages[pg_idx], + sizeof(data->auth_tag)); + } else if (data->crypt_offset !=3D + ((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 +=3D 1; + aead_request_set_crypt(req, data->sg, data->sg, total + SNAPSHOT_AUTH_TAG= _SIZE, nonce); + res =3D crypto_wait_req(crypto_aead_decrypt(req), &wait); + if (res) + return res; + + data->crypt_size =3D 0; + data->crypt_offset =3D 0; + + /* Push the decrypted pages further down the stack. */ + total =3D 0; + for (pg_idx =3D 0; pg_idx < page_count; pg_idx++) { + void *buf =3D data->crypt_pages[pg_idx]; + + res =3D snapshot_write_next(&data->handle); + if (res < 0) + return res; + if (res =3D=3D 0) + break; + + if (!data_of(data->handle)) + return -EINVAL; + + WARN_ON(res !=3D PAGE_SIZE); + + /* Copy the decrypted page into the snapshot image. */ + memcpy(data_of(data->handle), buf, PAGE_SIZE); + total +=3D PAGE_SIZE; + } + + if (data->crypt_total =3D=3D 0) + data->meta_size =3D snapshot_get_meta_page_count() << PAGE_SHIFT; + + data->crypt_total +=3D total; + res =3D 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 =3D=3D 0 || + (data->crypt_offset >=3D + (data->crypt_size + SNAPSHOT_AUTH_TAG_SIZE))) { + int rc; + + data->crypt_size =3D 0; + data->crypt_offset =3D 0; + rc =3D 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 =3D data->crypt_offset >> PAGE_SHIFT; + size_t pg_off =3D data->crypt_offset & (PAGE_SIZE - 1); + *buf =3D data->crypt_pages[pg_idx] + pg_off; + return PAGE_SIZE - pg_off; + } + + /* Use offsets just beyond the size to return the tag. */ + tag_off =3D data->crypt_offset - data->crypt_size; + if (tag_off > SNAPSHOT_AUTH_TAG_SIZE) + tag_off =3D SNAPSHOT_AUTH_TAG_SIZE; + + *buf =3D 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 =3D data->crypt_offset >> PAGE_SHIFT; + size_t pg_off =3D data->crypt_offset & (PAGE_SIZE - 1); + size_t size_avail =3D PAGE_SIZE; + *buf =3D 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 =3D + data->crypt_total + data->crypt_offset; + + if (total_done >=3D data->meta_size && + (total_done < + (data->meta_size + SNAPSHOT_AUTH_TAG_SIZE))) { + size_avail =3D SNAPSHOT_AUTH_TAG_SIZE; + } + } + + return size_avail - pg_off; + } + + /* Use offsets just beyond the size to return the tag. */ + tag_off =3D data->crypt_offset - (PAGE_SIZE * CHUNK_SIZE); + if (tag_off > SNAPSHOT_AUTH_TAG_SIZE) + tag_off =3D SNAPSHOT_AUTH_TAG_SIZE; + + *buf =3D 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 =3D 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 =3D snapshot_read_next_encrypted(data, &src); + + if (src_size <=3D 0) { + if (total =3D=3D 0) + return src_size; + + break; + } + + copy_size =3D min(count, (size_t)src_size); + not_done =3D copy_to_user(buf + total, src, copy_size); + copy_size -=3D not_done; + total +=3D copy_size; + count -=3D copy_size; + data->crypt_offset +=3D copy_size; + if (copy_size =3D=3D 0) { + if (total =3D=3D 0) + return -EFAULT; + + break; + } + } + + *offp +=3D 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 =3D 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 =3D snapshot_write_next_encrypted(data, &dst); + + if (dst_size <=3D 0) { + if (total =3D=3D 0) + return dst_size; + + break; + } + + copy_size =3D min(count, (size_t)dst_size); + not_done =3D copy_from_user(dst, buf + total, copy_size); + copy_size -=3D not_done; + total +=3D copy_size; + count -=3D copy_size; + data->crypt_offset +=3D copy_size; + if (copy_size =3D=3D 0) { + if (total =3D=3D 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 >=3D + (PAGE_SIZE * CHUNK_SIZE) + SNAPSHOT_AUTH_TAG_SIZE || + (data->meta_size && + data->crypt_total < data->meta_size && + data->crypt_total + data->crypt_offset =3D=3D + data->meta_size + SNAPSHOT_AUTH_TAG_SIZE)) { + int rc; + + rc =3D snapshot_decrypt_drain(data); + if (rc < 0) + return rc; + } + } + + *offp +=3D 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 =3D NULL; + } + + if (data->aead_tfm) { + crypto_free_aead(data->aead_tfm); + data->aead_tfm =3D NULL; + } + + for (i =3D 0; i < CHUNK_SIZE; i++) { + if (data->crypt_pages[i]) { + free_page((unsigned long)data->crypt_pages[i]); + data->crypt_pages[i] =3D 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 =3D 0; + data->crypt_offset =3D 0; + data->crypt_size =3D 0; + data->user_key_valid =3D 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 =3D crypto_alloc_aead("gcm(aes)", 0, 0); + if (IS_ERR(data->aead_tfm)) { + rc =3D PTR_ERR(data->aead_tfm); + data->aead_tfm =3D NULL; + return rc; + } + + rc =3D -ENOMEM; + data->aead_req =3D aead_request_alloc(data->aead_tfm, GFP_KERNEL); + if (!data->aead_req) + goto setup_fail; + + /* Allocate the staging area */ + for (i =3D 0; i < CHUNK_SIZE; i++) { + data->crypt_pages[i] =3D (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 =3D 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 =3D {}; + u8 image_key[SNAPSHOT_ENCRYPTION_KEY_SIZE] __nonstring =3D {}; + 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 =3D 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 =3D snapshot_install_image_key(data, image_key); + if (rc) + goto fail; + + rc =3D snapshot_wrap_image_key(image_key, &wrapped); + if (rc) + goto fail; + + /* Hand the wrapped key and clear nonce back to user mode. */ + rc =3D put_user(sizeof(wrapped), &key->blob_len); + if (rc) + goto fail; + + BUILD_BUG_ON(sizeof(wrapped) > + sizeof(((struct uswsusp_key_blob *)0)->blob)); + rc =3D copy_to_user(&key->blob, &wrapped, sizeof(wrapped)); + if (rc) + goto fail; + + rc =3D 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 =3D {}; + u8 image_key[SNAPSHOT_ENCRYPTION_KEY_SIZE] __nonstring =3D {}; + int rc; + + /* It's too late if data's been pushed in. */ + if (data->handle.cur) + return -EPIPE; + + rc =3D snapshot_setup_encryption_common(data); + if (rc) + return rc; + + /* Load the key from user mode. */ + blob =3D memdup_user(key, sizeof(*blob)); + if (IS_ERR(blob)) { + rc =3D PTR_ERR(blob); + goto crypto_setup_fail; + } + + if (blob->blob_len !=3D sizeof(wrapped)) { + rc =3D -EINVAL; + goto out_blob; + } + + memcpy(&wrapped, blob->blob, sizeof(wrapped)); + rc =3D snapshot_unwrap_image_key(&wrapped, image_key); + if (rc) + goto out_blob; + + rc =3D 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 =3D plain_size >> PAGE_SHIFT; + loff_t chunks =3D (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 =3D 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 =3D snapshot_get_meta_data_size(); + rc =3D put_user(size, &key->meta_size); + if (rc) + return rc; + + rc =3D 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 =3D 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 =3D true; + /* Install the key if the user is just under the wire. */ + rc =3D 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 =3D raw_size >> PAGE_SHIFT; + loff_t meta_size; + + pages -=3D snapshot_get_meta_page_count(); + meta_size =3D 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 !=3D 0) { + rc =3D 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; } =20 +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 =20 #include "power.h" +#include "user.h" =20 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; =20 int is_hibernate_resume_dev(dev_t dev) { @@ -57,13 +48,13 @@ static int snapshot_open(struct inode *inode, struct fi= le *filp) =20 if (!hibernate_acquire()) { error =3D -EBUSY; - goto Unlock; + goto unlock; } =20 if ((filp->f_flags & O_ACCMODE) =3D=3D O_RDWR) { hibernate_release(); error =3D -ENOSYS; - goto Unlock; + goto unlock; } nonseekable_open(inode, filp); data =3D &snapshot_state; @@ -100,7 +91,7 @@ static int snapshot_open(struct inode *inode, struct fil= e *filp) data->platform_support =3D false; data->dev =3D 0; =20 - Unlock: + unlock: unlock_system_sleep(sleep_flags); =20 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 =3D=3D 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 =3D filp->private_data; if (!data->ready) { res =3D -ENODATA; - goto Unlock; + goto unlock; + } + + if (snapshot_encryption_enabled(data)) { + res =3D snapshot_read_encrypted(data, buf, count, offp); + goto unlock; } + if (!pg_offp) { /* on page boundary? */ res =3D snapshot_read_next(&data->handle); if (res <=3D 0) - goto Unlock; + goto unlock; } else { res =3D PAGE_SIZE - pg_offp; } @@ -162,7 +160,7 @@ static ssize_t snapshot_read(struct file *filp, char __= user *buf, if (res > 0) *offp +=3D res; =20 - Unlock: + unlock: unlock_system_sleep(sleep_flags); =20 return res; @@ -185,6 +183,11 @@ static ssize_t snapshot_write(struct file *filp, const= char __user *buf, =20 data =3D filp->private_data; =20 + if (snapshot_encryption_enabled(data)) { + res =3D snapshot_write_encrypted(data, buf, count, offp); + goto unlock; + } + if (!pg_offp) { res =3D snapshot_write_next(&data->handle); if (res <=3D 0) @@ -328,6 +331,12 @@ static long snapshot_ioctl(struct file *filp, unsigned= int cmd, break; =20 case SNAPSHOT_ATOMIC_RESTORE: + if (snapshot_encryption_enabled(data)) { + error =3D snapshot_finalize_decrypted_image(data); + if (error) + break; + } + error =3D snapshot_write_finalize(&data->handle); if (error) break; @@ -368,6 +377,8 @@ static long snapshot_ioctl(struct file *filp, unsigned = int cmd, } size =3D snapshot_get_image_size(); size <<=3D PAGE_SHIFT; + if (snapshot_encryption_enabled(data)) + size =3D snapshot_get_encrypted_image_size(size); error =3D put_user(size, (loff_t __user *)arg); break; =20 @@ -425,6 +436,17 @@ static long snapshot_ioctl(struct file *filp, unsigned= int cmd, error =3D snapshot_set_swap_area(data, (void __user *)arg); break; =20 + case SNAPSHOT_ENABLE_ENCRYPTION: + if (data->mode =3D=3D O_RDONLY) + error =3D snapshot_get_encryption_key(data, (void __user *)arg); + else + error =3D snapshot_set_encryption_key(data, (void __user *)arg); + break; + + case SNAPSHOT_SET_USER_KEY: + error =3D snapshot_set_user_key(data, (void __user *)arg); + break; + default: error =3D -ENOTTY; =20 @@ -448,6 +470,8 @@ snapshot_compat_ioctl(struct file *file, unsigned int c= md, 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 +#include +#include +#include +#include + +#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 c= ount) +{ + 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 From nobody Fri Oct 2 06:57:22 2026 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0559C46AA98 for ; Tue, 4 Aug 2026 10:14:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785838488; cv=none; b=lPTBOIPMFxr7fuhVBpYAkRIiqXR262HUEaFkBToXjcG5TDLXTROyJLa0B65KVHHNQzLCCrg/RMGURHRULuBfcae7nKMUIAar5CkAn5ZNbLHPiye0eb0AOGxRoMrDsUWuITf6qbOyhGS464WddOL927TY5HWHxBl4LZn3BsJhSaQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785838488; c=relaxed/simple; bh=FlIqrnNujJ9mCUmJDCXq6O/g2BILPJv32q50WCBiCb4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=biks+xPXJj2kNquDDhPkT5/MzN5x8zUnPbAOxGFktYR3mekmYncH+LJUYYnf7i1RnLBgFwOwZDCf06LsCTczFkWFOYnsOx4OSbujTvoPBU+n/ZElWqC6Gi91XNWYLa2IemVaiMQuEsBTO6AiYTQiv1sRIB8N5IfD4U1wHl3kTtA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=starlabs.systems; spf=pass smtp.mailfrom=starlabs.systems; dkim=pass (2048-bit key) header.d=starlabs-systems.20251104.gappssmtp.com header.i=@starlabs-systems.20251104.gappssmtp.com header.b=RX7wxn9M; arc=none smtp.client-ip=209.85.128.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=starlabs.systems Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=starlabs.systems Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=starlabs-systems.20251104.gappssmtp.com header.i=@starlabs-systems.20251104.gappssmtp.com header.b="RX7wxn9M" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-49557167508so27339935e9.1 for ; Tue, 04 Aug 2026 03:14:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=starlabs-systems.20251104.gappssmtp.com; s=20251104; t=1785838484; x=1786443284; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=CJRtx4lAc4CjU0S7fi7raebVNgosyTdqYw/QYNsyThI=; b=RX7wxn9MQxAymG23WXmhYSvUEewn93taMoQ8EF1Z4gcxt0T5TQI0luyyLBaPfI/KR9 dmkCm/MRrvPHlKQMQQGDVkC1UcxPaKGnixfHA5VnWcYnObGfsHfu4ddeLHYO+koO+Q9C /RCA6xtEs8leaPzc2pf6zZtw1Llcnv3+WBvZUy7B38Skfu0UotodoNtkjHfZG000qk8Y YNamZ+fRLb/Kmf02iop23qZ7kAWfB5p9EimTQUSibFbHhZEtJP23SoXL6ZEPjS2eRWYF YCowJ2RQZA0RlbPLA9YefHkczxKWXk5m1+ONRlrABDbbdDlEVuZ665zF/zu1QpEtCV3l mqzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785838484; x=1786443284; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=CJRtx4lAc4CjU0S7fi7raebVNgosyTdqYw/QYNsyThI=; b=EBBkSg8V/cOtmT9o1m2SIvjzA9v4ALEcd8Cc1jN64ZeolmJZqSSBBuVfdivuXaqSBF JHdruOWILaRKWVfP1npTwFKUGIY3HFLABeRrHY8MNKPcmtFNP7GU2R28dhT/b3tZFDAj 3BmTM6+GTzKdtnyRxLAVmLYd+fa/P1S6ajRKQe4R72I9QqqxFLD0/KkiHoKpOr6KyChw SeYgwN8nmYr6/8rrTWtQGZBRQtRIlnDNXQX9Wd//Zma1fur80sgQQ6JHpqqe6BTT37J5 119mAcTn8/BDF4+2LKNcNt4vmuVnO+iP45G8husBGhoZ5C3RRFoSyS8j6xto7TPFDYF/ Iq3A== X-Forwarded-Encrypted: i=1; AHgh+Rr+ZZr2SV+8T7VUSXwuMBKToHQLd2+1EuNM454TzotYDRkKxV1ed07RmIr1zX83ccZFPWiqn0IVJXUo+6M=@vger.kernel.org X-Gm-Message-State: AOJu0YxiaBP8gT29yG2lm8ivbTCUq5JR/fTApuyLSE+H3sJTxqBONFwA q0VS3hSPTqbl4cq9UxBLtELw8Vf3IdsdSgijN9hiYLZLj64WCkfMKzSBhXVll6tb/Q== X-Gm-Gg: AR+sD13Eg81QKKQnBB5J5Kjli8t/wbaaV1GxdeAe7f+gAXbiQMBy6BVGMrd0IYgS1V2 tWaCrMU6fjprrwKQo/mUQVAZxnULSj4K2SKY8pRE88xbzUusPpMhf5n4zokq7cjNkUUArCUBqgf +eH+VPamr5NEGX7GBpllf0NQmziesEvl1J7dsgUeYczCGSTh8rT21eXF1g44sgmrmOvyNoL+0lQ aVrMGuBGqDupL234zWiVBHPsRpXnK6F+CS5NuxGeK5fjGqYb+x+GdpQLx8Zc6SWv+fWbbmTfZlz PF8Y9n1yact4/O9rRxtSy5+y6b5CQY1zjJ6d9R4ZvUBXKeGJRvB52GnOUMRH35YgBBm/367cWPq 0vXBUOaJHr5xI5HxboO2QIf/eTqIk6yDWvBoFW0ZontyCyfGoYUl7JW4u7DWeRyLqqOd0sTCarZ cqidxYKHNehmx19t/rHp9wtertFRnZ3QsdbD/bUPf4vOVISuWBGxrj9+nKqXvdOFTQvYLZPWXG9 v0D/eDxc1bLF5iTG/jI47W2VffOrl4gyvTdnu7MvVytx7F12MaDBVo= X-Received: by 2002:a05:600c:8711:b0:495:52a5:8829 with SMTP id 5b1f17b1804b1-4980c652515mr374637675e9.11.1785838484146; Tue, 04 Aug 2026 03:14:44 -0700 (PDT) Received: from horizon.localdomain ([216.128.28.240]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4994a0f7bf4sm73248805e9.11.2026.08.04.03.14.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Aug 2026 03:14:43 -0700 (PDT) From: Sean Rhodes To: linux-pm@vger.kernel.org Cc: "Rafael J. Wysocki" , Len Brown , Pavel Machek , Sean Rhodes , Evan Green , linux-kernel@vger.kernel.org, Xueqin Luo , Jens Axboe , Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , linux-block@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 2/4] PM: hibernate: permit encrypted snapshot device under lockdown Date: Tue, 4 Aug 2026 11:14:35 +0100 Message-ID: <517e8cbb3950f58b3a7a15e322b0c2d630874ce1.1785838445.git.sean@starlabs.systems> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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(); } =20 +bool hibernation_snapshot_dev_available(void) +{ + return nohibernate =3D=3D 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 transitio= ns. 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); =20 extern bool hibernate_acquire(void); extern void hibernate_release(void); +bool hibernation_snapshot_dev_available(void); =20 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 #include #include +#include =20 #include =20 @@ -32,7 +33,12 @@ struct snapshot_data snapshot_state; =20 int is_hibernate_resume_dev(dev_t dev) { - return hibernation_available() && snapshot_state.dev =3D=3D dev; + return hibernation_snapshot_dev_available() && snapshot_state.dev =3D=3D = dev; +} + +static bool snapshot_encryption_required(void) +{ + return security_locked_down(LOCKDOWN_HIBERNATION); } =20 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; =20 - if (!hibernation_available()) + if (!hibernation_snapshot_dev_available()) return -EPERM; =20 sleep_flags =3D lock_system_sleep(); @@ -90,6 +96,7 @@ static int snapshot_open(struct inode *inode, struct file= *filp) data->ready =3D false; data->platform_support =3D false; data->dev =3D 0; + data->encryption_required =3D snapshot_encryption_required(); =20 unlock: unlock_system_sleep(sleep_flags); @@ -141,6 +148,10 @@ static ssize_t snapshot_read(struct file *filp, char _= _user *buf, res =3D -ENODATA; goto unlock; } + if (data->encryption_required && !snapshot_encryption_enabled(data)) { + res =3D -EPERM; + goto unlock; + } =20 if (snapshot_encryption_enabled(data)) { res =3D snapshot_read_encrypted(data, buf, count, offp); @@ -183,6 +194,11 @@ static ssize_t snapshot_write(struct file *filp, const= char __user *buf, =20 data =3D filp->private_data; =20 + if (data->encryption_required && !snapshot_encryption_enabled(data)) { + res =3D -EPERM; + goto unlock; + } + if (snapshot_encryption_enabled(data)) { res =3D snapshot_write_encrypted(data, buf, count, offp); goto unlock; @@ -321,6 +337,10 @@ static long snapshot_ioctl(struct file *filp, unsigned= int cmd, error =3D -EPERM; break; } + if (data->encryption_required && !snapshot_encryption_enabled(data)) { + error =3D -EPERM; + break; + } pm_restore_gfp_mask(); error =3D hibernation_snapshot(data->platform_support); if (!error) { @@ -331,6 +351,10 @@ static long snapshot_ioctl(struct file *filp, unsigned= int cmd, break; =20 case SNAPSHOT_ATOMIC_RESTORE: + if (data->encryption_required && !snapshot_encryption_enabled(data)) { + error =3D -EPERM; + break; + } if (snapshot_encryption_enabled(data)) { error =3D snapshot_finalize_decrypted_image(data); if (error) @@ -415,6 +439,10 @@ static long snapshot_ioctl(struct file *filp, unsigned= int cmd, error =3D -EPERM; break; } + if (data->encryption_required && !snapshot_encryption_enabled(data)) { + error =3D -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; =20 case SNAPSHOT_POWER_OFF: + if (data->encryption_required && !snapshot_encryption_enabled(data)) { + error =3D -EPERM; + break; + } if (data->platform_support) error =3D 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; =20 #if defined(CONFIG_ENCRYPTED_HIBERNATION) From nobody Fri Oct 2 06:57:22 2026 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 44C6E46AA90 for ; Tue, 4 Aug 2026 10:14:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785838489; cv=none; b=hTV8sUtlwSvwG/ZguriMtf+e51oepwJa3M1qzr58mKP68hlpG2gBaRa6t07XyEnwPNPZbeitNg6B65SyIsmjEIU+u14qFYQxw8hi7avi2AT/sucLU4nY0h8JKLy0fEDoKGGbZJhTtbQWKXh1BztHDJKGBUOC55demKm4qQDu+7Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785838489; c=relaxed/simple; bh=YAY85gc1J7l4pem+Ynb5YEtvC9qyhSgj+8jFccC3U1Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YJ7KIsH+ezLN3UtPXmkOnIZpOzStoBq8pLr07pu0mPL0ZQsLV/gFS57a+6CMZXtaA1F/tko9ufUGpRpHPTUmRaS607op9BX1/fThTkrs0jBSQEpwltBfyw6j5sDEThTjqJSG4haOw4yzZeM0C20DL/T2ZOII9Kx+AqWxMTWxFIw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=starlabs.systems; spf=pass smtp.mailfrom=starlabs.systems; dkim=pass (2048-bit key) header.d=starlabs-systems.20251104.gappssmtp.com header.i=@starlabs-systems.20251104.gappssmtp.com header.b=TmhBfOkw; arc=none smtp.client-ip=209.85.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=starlabs.systems Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=starlabs.systems Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=starlabs-systems.20251104.gappssmtp.com header.i=@starlabs-systems.20251104.gappssmtp.com header.b="TmhBfOkw" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-495590dde14so30515935e9.0 for ; Tue, 04 Aug 2026 03:14:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=starlabs-systems.20251104.gappssmtp.com; s=20251104; t=1785838485; x=1786443285; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=xjThVuiPrln2+ib/P2lXqoKMuHizlqJXK8kVkecnjrE=; b=TmhBfOkw/s7grKESu166inWttGlniDjxJf2EATHZbsEEuh7pF0oUxim8gP5tMPWA0n UfMhy3NGtqvyRtZXFFOqruvKjzPB0Me3pC/rjrebzO4vwPGCEtdATlQv7hrzIEAdsWG5 Y5n498+moByVZLtxQi+mJ6TF3j+DpA4fdlGiXnE/Ws/TEHVxVqtp4FkJKnWkJhMtWvgN NZ+Yj3LD4RAI6cMEVvWkn5vjZvzP+hNLAD9AVbPESWHBFr2UNNEt2KHZzdJ6GZ5XH33e zjXPnJvriJS8I5xc26cRZyHOzBlVpxIZQEMwxi0KbYO7Fqrw0tgnTI5MNExAX0QfeVsj 3glA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785838485; x=1786443285; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=xjThVuiPrln2+ib/P2lXqoKMuHizlqJXK8kVkecnjrE=; b=BO/v5U8TykYcHdaEA2CPL/Ef+2YFEBGgWpSuCIvZ/Cpjk5W9JnTiISPe75tY/OqvAT MUr7TsmuwFes+hFFK/t5iIwOeeK3P+VwNYGPG/NjSPoFKLuCsFIaJVuTmTPVOawGyj+E oGnyTrcMjcN2aQ3GB1u4YteZr1u1vv8mlbl7WAYnJt2SSHiMRCvjBN5U0erZjB5OosK8 TjlNsO119t1O2dNS4kLh6F1/K4X/Veji10AOSzjspZ+PHIfmi/w13Qo6csgsJDJsFHjo o5xcPSXtd95GDTZIvyi/238Dc9QbzTB5QjXEUI2zE636Ljcbkfwb2GyklL29YgLhj9PF Cknw== X-Forwarded-Encrypted: i=1; AHgh+Rq8Ji5AZK6MG5jGYcJV8ZUF/LuhBlw3Ne6o6GiCpZz7PTttITt55ro32jzIffgnDXyRBVTBxhpuV4OPh/0=@vger.kernel.org X-Gm-Message-State: AOJu0YwDDRtRRPrD3600xHKTslEsyG/Ks6WbetQ7ccC/iNT3IIoSwgMk jzTad8UNFCxMMBVgrg7evS5M28KYTJlZ2fwGwkRlwtWazFeXrXsEjrA+OmLQGyugTA== X-Gm-Gg: AR+sD11moHzr1nOSg0g1wAm178g11e5JoUm/lHrujvx13/BKTGPbWLYvWHTk/s5Kt7v 3wOQdmFv9gBGx9o9rlG+4b/q4qSTqZ6JeewEZIFDwmK6FE+FKqevTV7EvbKvvwtiBgcTmlFzxzr kqdia6A/WQl3kPxzzSCRKSVJgZfaXcVvqFiZWz7ezy73oHIT7XTBMSQLE1zjsKeYaZoVEsujqZn JM9AyllV0zfmcIVaBGA23pDytVCYK2eI9NbIlNek5hvJaO0fs4ZuUK8/mi5RixBsFrIbHnv++VL 6jyza/hWqlBmFaOj5zKO5LNMYgjYncmfgKhHC2y8NBKAix1pwNaO0T8ewU0THDRsceut7aLHPPI BQovexs2++T3tBvi3+AZfqtTj05mlPTpqltifo+/uhjYxpV1mObeBt1n2PonbYa/+cAgw7YudYe FrsorIjPrzJyWpfyhmsYTKc5BFOhFOsZda8uO8nmGD2Q+qxBJvTB+3SJWIToJCqCqJbnSK1Zjcw aVjaZsjg/JsTupuOdt+shyHTlHeqhDD X-Received: by 2002:a05:600c:564b:b0:495:52dc:73a9 with SMTP id 5b1f17b1804b1-4980c675370mr239950485e9.11.1785838485451; Tue, 04 Aug 2026 03:14:45 -0700 (PDT) Received: from horizon.localdomain ([216.128.28.240]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4994a0f7bf4sm73248805e9.11.2026.08.04.03.14.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Aug 2026 03:14:45 -0700 (PDT) From: Sean Rhodes To: linux-pm@vger.kernel.org Cc: "Rafael J. Wysocki" , Len Brown , Pavel Machek , Sean Rhodes , Evan Green , linux-kernel@vger.kernel.org, Xueqin Luo , Jens Axboe , Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , linux-block@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 3/4] PM: hibernate: document encrypted snapshot seed ABI Date: Tue, 4 Aug 2026 11:14:36 +0100 Message-ID: <1f5b50e20b857dfa66f9c5d7c3df41738706140b.1785838445.git.sean@starlabs.systems> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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/test= ing/sysfs-power index d38da077905a..57f6063ef88c 100644 --- a/Documentation/ABI/testing/sysfs-power +++ b/Documentation/ABI/testing/sysfs-power @@ -470,3 +470,17 @@ Description: =20 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=3Dy. + + 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) =20 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. =20 SNAPSHOT_SET_USER_KEY Mixes additional user key material into the data portion of an encrypted From nobody Fri Oct 2 06:57:22 2026 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 32A9846EF6D for ; Tue, 4 Aug 2026 10:14:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785838494; cv=none; b=W7YQynu5Hwd48J+l2zUs4UTBIiE8xbYn8Cppurtpg1xpmcDoYbe0d7wXK3oAuz48DC+zSRFUQ27Y7qR+QEr726hhCr/ZYBsS+gngkQsPa2JI3uHIJkFf2qkGOrNY7KmiehGvdYHr94/kFB6zTU4fQZgSbxNt+KmSzMXg5Gj0DY0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785838494; c=relaxed/simple; bh=SEflLOzL0lfGY+k+oJM8VDufIcUJjBUR3+9oikCWla4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=N5P0DB5lf5d8rx6tFp//WMjXPKs4IV7zmurlxBJzTBBh2eZcR8D00/fumbQBGAAuNtq58JOp1gOQXzu6qdsYkJX20Et+JT/zQfxY9SeNnz1kMnb3NezjWmCG0Uh2R+TD9x5xzdrMiF40cejGt0547rp7U7doPDQQc5CHVDN1zvw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=starlabs.systems; spf=pass smtp.mailfrom=starlabs.systems; dkim=pass (2048-bit key) header.d=starlabs-systems.20251104.gappssmtp.com header.i=@starlabs-systems.20251104.gappssmtp.com header.b=K8n5m4dm; arc=none smtp.client-ip=209.85.128.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=starlabs.systems Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=starlabs.systems Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=starlabs-systems.20251104.gappssmtp.com header.i=@starlabs-systems.20251104.gappssmtp.com header.b="K8n5m4dm" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-490cf322ed0so17709055e9.1 for ; Tue, 04 Aug 2026 03:14:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=starlabs-systems.20251104.gappssmtp.com; s=20251104; t=1785838487; x=1786443287; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=zEbGOJa+VWUmYMgmNNATFxA3erx26Syz6kDMhAYS8wo=; b=K8n5m4dmm3SGreYwoXzDTWsPSOBuwPIk88zzxat9EcZjLoaIeVNRf3KFL4W5lD91aH zmuQnVzHAhFyDhQ/HSHHC/rOp4RrkxP0qqFt/iuoC0VMi6FbrjFRrrrIN15OEr+G0uw8 Zl/xkgglQoG8ZWeG29vB+rR60ThFXnuZPDYl3V601DUBNprPqPLyoPSFFWeQSR78AqGW RJkEsBeDI0MtFxgxshX4+DR7bo+ETpoX/q2XjDBc4asrZTJo80rxNCDrVfanf37ofWgV I+FnkrsxnjR4BA0zlZ50KuG/wTjMgAfNGr8BL+wM4/Bx5BjuszCncYsB5NvhxXT5Y4rN LEkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785838487; x=1786443287; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=zEbGOJa+VWUmYMgmNNATFxA3erx26Syz6kDMhAYS8wo=; b=AD70eEdwXmHG5hOBkqGP9Rvxux2W9cRePPszJrVTzlRZR9TGDsybC4IwkNCdqhMYAg jI6EqRTuSANSv/a6rAiXNZHy5TVIuSbBf9PHQ4lM81wUJmPH/MQA6nFIIN3vluC65X3E /m6CINUBbD3TuRY2e11GIuC39EAymlTSWIVBF667rMXb1mRHlYGQpdYu4FQzOV2H6ccy WYTG2JAlBKmp4RJ9fAtTkWujwiOuDZvn+J0wNMsZW/vkJbcC/AuKDfeN45w1WIQRKzMP Ztk2yAvXrNq7VJ3IYqGqmNGUgIABQJQ6dOjQk7R3BMIlEkPe3oBiSvVraVH1SzjiJQOo UPAg== X-Forwarded-Encrypted: i=1; AHgh+RrU/90g46DcY+i+3fjWno6r66BB54SWAMg04bwpIIbd49G5Z9k7aAyeHJ1/YhHlgPklvIPwjDciY7bADSE=@vger.kernel.org X-Gm-Message-State: AOJu0Yyf5Tp2QsO03z6rQ9tw6HGRLC1G6ih6q4CnRkx4bdjhbBomT3/9 ENaKVr2GohhhGpM69FPPOCf3pBtKUuwzpLfeJVGPNSv9CPM9IP4NL54QpVaRJpYMTA== X-Gm-Gg: AR+sD13h0ZOdZi/nqmZtqcBASibA7pruIjs/et7Aso51mAEmleayjnFkI/eoy9GdmvT RCfPzv+kCn/+F5UNN7v7V3cDJsWvbg3DTS6xKq2gVenh5BVPGF0Nr3H15e5FGchUbvgTzYj86oz oVPr/GS3oHG0GNSWuwsEm5Nw+gQnyqCHSvWcqccUFPTQB14XqJ28lRWCERue1ew73+veWh4+tqY UdhHanAqccDwU63pcIibjbWdUgNWv/DXr8pfWPqw8zT18AowHLjaWi3INV3BiRtC8JnQY4lXlHx MwpL/qtYJnfWdJSkEmU67EY5FhLqdlmGXZvhGNcWhupmgGNxZ2NytEeRVKJ+FJEU5fZvD1D6u38 9uwwJhqYPZS2YDssGoTe/MnRlBynRYNRZ72sxRgNl9qr5fDP9/avdUFLsdU2UsTF8VTXZEAvZC1 JhHamdMioYvdqBYCJN7qpeFLCGtGGtEC2Okb8CrWuwGM98I2kBJlSMGgn3ZHLI//9/63J71b1T6 opiWrvLUHRPFl/wP1JGBO3STYWkz8Vz X-Received: by 2002:a05:600c:34c6:b0:493:a966:d5b5 with SMTP id 5b1f17b1804b1-4980c66b50emr347664965e9.2.1785838487020; Tue, 04 Aug 2026 03:14:47 -0700 (PDT) Received: from horizon.localdomain ([216.128.28.240]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4994a0f7bf4sm73248805e9.11.2026.08.04.03.14.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Aug 2026 03:14:46 -0700 (PDT) From: Sean Rhodes To: linux-pm@vger.kernel.org Cc: "Rafael J. Wysocki" , Len Brown , Pavel Machek , Sean Rhodes , Evan Green , linux-kernel@vger.kernel.org, Xueqin Luo , Jens Axboe , Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , linux-block@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 4/4] PM: hibernate: harden encrypted snapshot write path Date: Tue, 4 Aug 2026 11:14:37 +0100 Message-ID: <996978852410335e8d08a43ee8c33f35cd19069c.1785838445.git.sean@starlabs.systems> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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. =20 The device's read() operation can be used to transfer the snapshot image f= rom 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 =3D iocb->ki_filp; struct inode *bd_inode =3D bdev_file_inode(file); struct block_device *bdev =3D I_BDEV(bd_inode); + dev_t dev =3D bd_inode->i_rdev; bool atomic =3D iocb->ki_flags & IOCB_ATOMIC; loff_t size =3D bdev_nr_bytes(bdev); + enum hibernate_snapshot_write hibernate_write; + size_t hibernate_len =3D 0; size_t shorted =3D 0; + ssize_t hibernate_written =3D 0; ssize_t ret; =20 if (bdev_read_only(bdev)) return -EPERM; =20 - 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; =20 - if (!iov_iter_count(from)) - return 0; + hibernate_len =3D iov_iter_count(from); + hibernate_write =3D hibernate_snapshot_write_begin(dev, iocb->ki_pos, hi= bernate_len); + if (hibernate_write =3D=3D HIBERNATE_SNAPSHOT_WRITE_NONE) + return -ETXTBSY; + } else { + hibernate_write =3D HIBERNATE_SNAPSHOT_WRITE_NONE; + } =20 - if (iocb->ki_pos >=3D size) - return -ENOSPC; + if (!iov_iter_count(from)) { + ret =3D 0; + goto out_hibernate; + } =20 - if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) =3D=3D IOCB_NOWAIT) - return -EOPNOTSUPP; + if (iocb->ki_pos >=3D size) { + ret =3D -ENOSPC; + goto out_hibernate; + } + + if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) =3D=3D IOCB_NOWAIT) { + ret =3D -EOPNOTSUPP; + goto out_hibernate; + } =20 if (atomic) { ret =3D generic_atomic_write_valid(iocb, from); if (ret) - return ret; + goto out_hibernate; } =20 size -=3D iocb->ki_pos; if (iov_iter_count(from) > size) { - if (atomic) - return -EINVAL; + if (atomic) { + ret =3D -EINVAL; + goto out_hibernate; + } shorted =3D iov_iter_count(from) - size; iov_iter_truncate(from, size); } =20 ret =3D file_update_time(file); if (ret) - return ret; + goto out_hibernate; =20 if (iocb->ki_flags & IOCB_DIRECT) { ret =3D blkdev_direct_write(iocb, from); @@ -777,9 +799,16 @@ static ssize_t blkdev_write_iter(struct kiocb *iocb, s= truct iov_iter *from) inode_unlock_shared(bd_inode); } =20 - if (ret > 0) + if (ret > 0) { + hibernate_written =3D ret; ret =3D 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; } =20 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(voi= d) { return false; } =20 int arch_resume_nosmt(void); =20 +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_wr= ite type, + size_t reserved, + ssize_t written) +{ +} #endif =20 /* 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, sec= tor_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 en= try); extern int swp_swapcount(swp_entry_t entry); @@ -535,6 +538,19 @@ static inline int add_swap_extent(struct swap_info_str= uct *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/suspe= nd_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); =20 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); =20 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 #include #include +#include #include #include #include @@ -52,7 +53,7 @@ int snapshot_store_encryption_seed(const char *buf, size_= t count) =20 mutex_lock(&snapshot_seed_mutex); if (snapshot_seed_valid) { - ret =3D memcmp(snapshot_seed, seed, sizeof(seed)) ? -EPERM : 0; + ret =3D crypto_memneq(snapshot_seed, seed, sizeof(seed)) ? -EPERM : 0; goto out; } =20 @@ -266,6 +267,22 @@ static int snapshot_check_user_key_switch(struct snaps= hot_data *data) return 0; } =20 +static loff_t snapshot_encrypted_byte_count(loff_t plain_size); + +static void snapshot_set_meta_size(struct snapshot_data *data, u64 meta_si= ze) +{ + data->meta_size =3D meta_size; + data->crypt_meta_size =3D 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 +=3D 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; =20 if (data->crypt_total =3D=3D 0) { - data->meta_size =3D snapshot_get_meta_page_count() << PAGE_SHIFT; + snapshot_set_meta_size(data, + snapshot_get_meta_page_count() << PAGE_SHIFT); } else { res =3D snapshot_check_user_key_switch(data); if (res) @@ -294,8 +312,8 @@ static int snapshot_encrypt_refill(struct snapshot_data= *data) for (pg_idx =3D 0; pg_idx < CHUNK_SIZE; pg_idx++) { void *buf =3D data->crypt_pages[pg_idx]; =20 - /* 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) =3D=3D data->meta_size)) break; =20 @@ -316,6 +334,9 @@ static int snapshot_encrypt_refill(struct snapshot_data= *data) total +=3D PAGE_SIZE; } =20 + 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 +=3D PAGE_SIZE; } =20 - if (data->crypt_total =3D=3D 0) + if (data->crypt_total =3D=3D 0) { data->meta_size =3D snapshot_get_meta_page_count() << PAGE_SHIFT; + if (data->user_key_valid) + data->crypt_meta_size =3D + snapshot_encrypted_byte_count(data->meta_size); + } =20 data->crypt_total +=3D total; res =3D snapshot_check_user_key_switch(data); @@ -449,6 +474,8 @@ static ssize_t snapshot_read_next_encrypted(struct snap= shot_data *data, rc =3D snapshot_encrypt_refill(data); if (rc < 0) return rc; + if (!data->crypt_size) + return 0; } =20 /* 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; =20 /* Return data pages if the offset is in that region. */ if (data->crypt_offset < (PAGE_SIZE * CHUNK_SIZE)) { size_t pg_idx =3D data->crypt_offset >> PAGE_SHIFT; size_t pg_off =3D data->crypt_offset & (PAGE_SIZE - 1); - size_t size_avail =3D PAGE_SIZE; - *buf =3D data->crypt_pages[pg_idx] + pg_off; =20 - /* - * 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 =3D - data->crypt_total + data->crypt_offset; - - if (total_done >=3D data->meta_size && - (total_done < - (data->meta_size + SNAPSHOT_AUTH_TAG_SIZE))) { - size_avail =3D SNAPSHOT_AUTH_TAG_SIZE; - } - } + *buf =3D data->crypt_pages[pg_idx] + pg_off; + size_avail =3D PAGE_SIZE - pg_off; + } else { + /* Use offsets just beyond the size to return the tag. */ + tag_off =3D data->crypt_offset - (PAGE_SIZE * CHUNK_SIZE); + if (tag_off > SNAPSHOT_AUTH_TAG_SIZE) + tag_off =3D SNAPSHOT_AUTH_TAG_SIZE; =20 - return size_avail - pg_off; + *buf =3D data->auth_tag + tag_off; + size_avail =3D SNAPSHOT_AUTH_TAG_SIZE - tag_off; } =20 - /* Use offsets just beyond the size to return the tag. */ - tag_off =3D data->crypt_offset - (PAGE_SIZE * CHUNK_SIZE); - if (tag_off > SNAPSHOT_AUTH_TAG_SIZE) - tag_off =3D SNAPSHOT_AUTH_TAG_SIZE; + if (data->crypt_meta_size && + data->crypt_stream_total < data->crypt_meta_size) { + u64 meta_avail =3D data->crypt_meta_size - data->crypt_stream_total; =20 - *buf =3D data->auth_tag + tag_off; - return SNAPSHOT_AUTH_TAG_SIZE - tag_off; + size_avail =3D min_t(u64, size_avail, meta_avail); + } + + return size_avail; } =20 ssize_t snapshot_read_encrypted(struct snapshot_data *data, char __user *buf, size_t count, loff_t *offp) { - ssize_t total =3D 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 =3D snapshot_read_next_encrypted(data, &src); - - if (src_size <=3D 0) { - if (total =3D=3D 0) - return src_size; - - break; - } - - copy_size =3D min(count, (size_t)src_size); - not_done =3D copy_to_user(buf + total, src, copy_size); - copy_size -=3D not_done; - total +=3D copy_size; - count -=3D copy_size; - data->crypt_offset +=3D copy_size; - if (copy_size =3D=3D 0) { - if (total =3D=3D 0) - return -EFAULT; - - break; - } - } - - *offp +=3D 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 =3D *offp & (PAGE_SIZE - 1); + count =3D min_t(size_t, count, PAGE_SIZE - pg_off); + + src_size =3D snapshot_read_next_encrypted(data, &src); + if (src_size <=3D 0) + return src_size; + + copy_size =3D min_t(size_t, count, src_size); + not_done =3D copy_to_user(buf, src, copy_size); + copy_size -=3D not_done; + if (!copy_size) + return -EFAULT; + + data->crypt_offset +=3D copy_size; + *offp +=3D copy_size; + snapshot_record_encrypted_read(data, copy_size); + return copy_size; } =20 ssize_t snapshot_write_encrypted(struct snapshot_data *data, const char __user *buf, size_t count, loff_t *offp) { - ssize_t total =3D 0; + size_t copy_size; + size_t not_done; + size_t pg_off; + void *dst; + ssize_t dst_size; + int rc; =20 - /* 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 =3D snapshot_write_next_encrypted(data, &dst); + if (!count) + return 0; =20 - if (dst_size <=3D 0) { - if (total =3D=3D 0) - return dst_size; + pg_off =3D *offp & (PAGE_SIZE - 1); + count =3D min_t(size_t, count, PAGE_SIZE - pg_off); =20 - break; - } + dst_size =3D snapshot_write_next_encrypted(data, &dst); + if (dst_size <=3D 0) + return dst_size; =20 - copy_size =3D min(count, (size_t)dst_size); - not_done =3D copy_from_user(dst, buf + total, copy_size); - copy_size -=3D not_done; - total +=3D copy_size; - count -=3D copy_size; - data->crypt_offset +=3D copy_size; - if (copy_size =3D=3D 0) { - if (total =3D=3D 0) - return -EFAULT; - - break; - } + copy_size =3D min_t(size_t, count, dst_size); + not_done =3D copy_from_user(dst, buf, copy_size); + copy_size -=3D not_done; + if (!copy_size) + return -EFAULT; =20 - /* - * 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 >=3D - (PAGE_SIZE * CHUNK_SIZE) + SNAPSHOT_AUTH_TAG_SIZE || - (data->meta_size && - data->crypt_total < data->meta_size && - data->crypt_total + data->crypt_offset =3D=3D - data->meta_size + SNAPSHOT_AUTH_TAG_SIZE)) { - int rc; - - rc =3D snapshot_decrypt_drain(data); - if (rc < 0) - return rc; - } + data->crypt_offset +=3D copy_size; + data->crypt_stream_total +=3D 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 >=3D + (PAGE_SIZE * CHUNK_SIZE) + SNAPSHOT_AUTH_TAG_SIZE || + (data->crypt_meta_size && + data->crypt_stream_total =3D=3D data->crypt_meta_size)) { + rc =3D snapshot_decrypt_drain(data); + if (rc < 0) + return rc; } =20 - *offp +=3D total; - return total; + *offp +=3D copy_size; + return copy_size; +} + +static void snapshot_reset_encryption_state(struct snapshot_data *data) +{ + data->crypt_offset =3D 0; + data->crypt_size =3D 0; + data->crypt_total =3D 0; + data->crypt_stream_total =3D 0; + data->nonce_low =3D 0; + data->nonce_high =3D 0; + data->meta_size =3D 0; + data->crypt_meta_size =3D 0; + data->user_key_valid =3D false; + spin_lock(&data->crypt_lock); + data->crypt_bytes_read =3D 0; + data->crypt_swap_reserved =3D 0; + data->crypt_header_reserved =3D 0; + spin_unlock(&data->crypt_lock); + memset(data->auth_tag, 0, sizeof(data->auth_tag)); } =20 void snapshot_teardown_encryption(struct snapshot_data *data) @@ -623,21 +647,20 @@ void snapshot_teardown_encryption(struct snapshot_dat= a *data) =20 memzero_explicit(data->encryption_key, sizeof(data->encryption_key)); memzero_explicit(data->user_key, sizeof(data->user_key)); + snapshot_reset_encryption_state(data); } =20 static int snapshot_setup_encryption_common(struct snapshot_data *data) { int i, rc; =20 - data->crypt_total =3D 0; - data->crypt_offset =3D 0; - data->crypt_size =3D 0; - data->user_key_valid =3D false; - memset(data->crypt_pages, 0, sizeof(data->crypt_pages)); /* This only works once per hibernate. */ if (data->aead_tfm) return -EINVAL; =20 + snapshot_reset_encryption_state(data); + memset(data->crypt_pages, 0, sizeof(data->crypt_pages)); + /* Set up the encryption transform */ data->aead_tfm =3D crypto_alloc_aead("gcm(aes)", 0, 0); if (IS_ERR(data->aead_tfm)) { @@ -653,7 +676,7 @@ static int snapshot_setup_encryption_common(struct snap= shot_data *data) =20 /* Allocate the staging area */ for (i =3D 0; i < CHUNK_SIZE; i++) { - data->crypt_pages[i] =3D (void *)__get_free_page(GFP_ATOMIC); + data->crypt_pages[i] =3D (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, =20 BUILD_BUG_ON(sizeof(wrapped) > sizeof(((struct uswsusp_key_blob *)0)->blob)); - rc =3D copy_to_user(&key->blob, &wrapped, sizeof(wrapped)); - if (rc) + if (copy_to_user(&key->blob, &wrapped, sizeof(wrapped))) { + rc =3D -EFAULT; goto fail; + } =20 - rc =3D copy_to_user(&key->nonce, &nonce, sizeof(nonce)); - if (rc) + if (copy_to_user(&key->nonce, &nonce, sizeof(nonce))) { + rc =3D -EFAULT; goto fail; + } =20 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 pl= ain_size) return plain_size + (chunks * SNAPSHOT_AUTH_TAG_SIZE); } =20 -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 =3D snapshot_get_meta_page_count(); + if (raw_size <=3D meta_plain_size) + return snapshot_encrypted_byte_count(raw_size); =20 - 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); } =20 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 =3D {}; unsigned int key_len; u64 size; int rc; =20 - /* - * Return the metadata size, the number of bytes that can be fed in before - * the user data key is needed at resume time. - */ - size =3D snapshot_get_meta_data_size(); - rc =3D put_user(size, &key->meta_size); - if (rc) - return rc; + if (!snapshot_encryption_enabled(data)) + return -EINVAL; =20 - rc =3D 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; =20 BUILD_BUG_ON(sizeof(data->user_key) < sizeof(user_key.key)); + rc =3D -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 =3D=3D O_RDONLY && !data->ready) { + rc =3D -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 =3D=3D O_WRONLY && !data->meta_size) { + if (user_key.meta_size < PAGE_SIZE + SNAPSHOT_AUTH_TAG_SIZE) + goto out; + + data->crypt_meta_size =3D user_key.meta_size; + size =3D data->crypt_meta_size; + } else { + snapshot_set_meta_size(data, + snapshot_get_meta_page_count() << PAGE_SHIFT); + size =3D data->crypt_meta_size; + } + + rc =3D put_user(size, &key->meta_size); + if (rc) + goto out; =20 key_len =3D user_key.key_len; =20 /* 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 =3D -EBUSY; + goto out; + } =20 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 =3D snapshot_check_user_key_switch(data); if (rc) - return rc; + goto out; =20 - return 0; + rc =3D 0; + +out: + memzero_explicit(&user_key, sizeof(user_key)); + return rc; } =20 -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 =3D raw_size >> PAGE_SHIFT; - loff_t meta_size; + loff_t meta_plain_size =3D data->meta_size; + loff_t split_size; + + if (!meta_plain_size) + meta_plain_size =3D snapshot_get_meta_page_count() << PAGE_SHIFT; + + split_size =3D 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); =20 - pages -=3D snapshot_get_meta_page_count(); - meta_size =3D snapshot_get_meta_data_size(); - return snapshot_encrypted_byte_count(pages << PAGE_SHIFT) + meta_size; + return split_size; } =20 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_o= ffset) return 0; } =20 +static bool swsusp_extents_contain_range(unsigned long swap_offset, + unsigned long nr_pages) +{ + struct rb_node *node =3D swsusp_extents.rb_node; + struct swsusp_extent *ext; + unsigned long end; + + if (!nr_pages) + return false; + + end =3D swap_offset + nr_pages - 1; + if (end < swap_offset) + return false; + + while (node) { + ext =3D rb_entry(node, struct swsusp_extent, node); + if (swap_offset < ext->start) + node =3D node->rb_left; + else if (swap_offset > ext->end) + node =3D node->rb_right; + else + goto found; + } + + return false; + +found: + for (;;) { + if (swap_offset < ext->start) + return false; + if (end <=3D ext->end) + return true; + if (ext->end =3D=3D (unsigned long)-1) + return false; + + swap_offset =3D ext->end + 1; + node =3D rb_next(&ext->node); + if (!node) + return false; + + ext =3D 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 =3D (u64)pos + count - 1; + if (end < (u64)pos) + return false; + + block =3D (u64)pos >> PAGE_SHIFT; + end_block =3D end >> PAGE_SHIFT; + + for (;;) { + u64 remaining_pages =3D end_block - block + 1; + pgoff_t swap_offset; + pgoff_t mapped_pages; + sector_t page_block =3D block; + u64 pages; + + if (!remaining_pages) + return false; + if ((u64)page_block !=3D block) + return false; + if (swapdev_block_to_extent(swap, page_block, &swap_offset, + &mapped_pages)) + return false; + if (!mapped_pages) + return false; + pages =3D min_t(u64, mapped_pages, remaining_pages); + if ((u64)(unsigned long)pages !=3D pages) + return false; + if (!swsusp_extents_contain_range(swap_offset, pages)) + return false; + if (pages =3D=3D remaining_pages) + return true; + block +=3D pages; + } +} + +u64 swsusp_swap_map_bytes(u64 image_bytes) +{ + u64 image_pages =3D DIV_ROUND_UP_ULL(image_bytes, PAGE_SIZE); + u64 map_pages =3D 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 #include #include +#include +#include #include =20 #include @@ -33,12 +35,136 @@ struct snapshot_data snapshot_state; =20 int is_hibernate_resume_dev(dev_t dev) { - return hibernation_snapshot_dev_available() && snapshot_state.dev =3D=3D = dev; + return hibernation_available() && snapshot_state.dev =3D=3D dev; } =20 -static bool snapshot_encryption_required(void) +#if defined(CONFIG_ENCRYPTED_HIBERNATION) +static bool snapshot_encrypted_output_active(struct snapshot_data *data, d= ev_t dev) { - return security_locked_down(LOCKDOWN_HIBERNATION); + return data->encryption_required && data->mode =3D=3D O_RDONLY && + data->ready && data->dev =3D=3D dev && + data->owner_tgid =3D=3D 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 =3D data->swap_header_offset; + loff_t offset; + + if (pos < header_offset) + return false; + + offset =3D pos - header_offset; + return offset < PAGE_SIZE && count <=3D PAGE_SIZE - offset; +} + +static u64 snapshot_swap_write_budget(struct snapshot_data *data) +{ + u64 image_bytes =3D round_up(data->crypt_bytes_read, PAGE_SIZE); + u64 map_bytes =3D swsusp_swap_map_bytes(data->crypt_bytes_read); + u64 budget =3D 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 *da= ta) +{ + spin_lock(&data->crypt_lock); + data->crypt_swap_reserved =3D 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 =3D &snapshot_state; + enum hibernate_snapshot_write type =3D 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 =3D swsusp_swap_range_allocated(data->swap, pos, co= unt); + + spin_lock(&data->crypt_lock); + swap_budget =3D snapshot_swap_write_budget(data); + if (snapshot_header_write_range(data, pos, count) && + data->crypt_header_reserved <=3D PAGE_SIZE && + PAGE_SIZE - data->crypt_header_reserved >=3D count) { + data->crypt_header_reserved +=3D count; + type =3D HIBERNATE_SNAPSHOT_WRITE_HEADER; + } else if (image_range_allocated && + swap_budget >=3D data->crypt_swap_reserved && + swap_budget - data->crypt_swap_reserved >=3D count) { + data->crypt_swap_reserved +=3D count; + type =3D HIBERNATE_SNAPSHOT_WRITE_IMAGE; + } + spin_unlock(&data->crypt_lock); + + if (type =3D=3D 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 =3D &snapshot_state; + u64 *reserved_total; + size_t unused; + + if (type =3D=3D HIBERNATE_SNAPSHOT_WRITE_NONE) + return; + if (!reserved) + goto unlock; + + if (type =3D=3D HIBERNATE_SNAPSHOT_WRITE_HEADER) + unused =3D reserved; + else if (written <=3D 0) + unused =3D reserved; + else if (written < reserved) + unused =3D reserved - written; + else + goto unlock; + + reserved_total =3D type =3D=3D HIBERNATE_SNAPSHOT_WRITE_HEADER ? + &data->crypt_header_reserved : &data->crypt_swap_reserved; + + spin_lock(&data->crypt_lock); + *reserved_total -=3D 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 =3D security_locked_down(LOCKDOWN_HIBERNATION); + return data->encryption_required; } =20 static int snapshot_open(struct inode *inode, struct file *filp) @@ -66,6 +192,14 @@ static int snapshot_open(struct inode *inode, struct fi= le *filp) data =3D &snapshot_state; filp->private_data =3D data; memset(&data->handle, 0, sizeof(struct snapshot_handle)); +#if defined(CONFIG_ENCRYPTED_HIBERNATION) + spin_lock_init(&data->crypt_lock); + data->crypt_bytes_read =3D 0; + data->crypt_swap_reserved =3D 0; + data->crypt_header_reserved =3D 0; + data->swap_header_offset =3D 0; + data->owner_tgid =3D NULL; +#endif if ((filp->f_flags & O_ACCMODE) =3D=3D O_RDONLY) { /* Hibernating. The image device should be accessible. */ data->swap =3D pin_hibernation_swap_type(swsusp_resume_device, 0); @@ -96,7 +230,11 @@ static int snapshot_open(struct inode *inode, struct fi= le *filp) data->ready =3D false; data->platform_support =3D false; data->dev =3D 0; - data->encryption_required =3D snapshot_encryption_required(); + data->encryption_required =3D snapshot_encryption_required(data); +#if defined(CONFIG_ENCRYPTED_HIBERNATION) + if (!error) + data->owner_tgid =3D get_task_pid(current, PIDTYPE_TGID); +#endif =20 unlock: unlock_system_sleep(sleep_flags); @@ -114,6 +252,10 @@ static int snapshot_release(struct inode *inode, struc= t file *filp) swsusp_free(); data =3D filp->private_data; data->dev =3D 0; +#if defined(CONFIG_ENCRYPTED_HIBERNATION) + put_pid(data->owner_tgid); + data->owner_tgid =3D 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 =3D -ENODATA; goto unlock; } - if (data->encryption_required && !snapshot_encryption_enabled(data)) { + if (snapshot_encryption_required(data) && + !snapshot_encryption_enabled(data)) { res =3D -EPERM; goto unlock; } @@ -194,7 +337,8 @@ static ssize_t snapshot_write(struct file *filp, const = char __user *buf, =20 data =3D filp->private_data; =20 - if (data->encryption_required && !snapshot_encryption_enabled(data)) { + if (snapshot_encryption_required(data) && + !snapshot_encryption_enabled(data)) { res =3D -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 =3D swdev; +#if defined(CONFIG_ENCRYPTED_HIBERNATION) + data->swap_header_offset =3D (loff_t)offset << PAGE_SHIFT; +#endif return 0; } =20 @@ -337,7 +484,8 @@ static long snapshot_ioctl(struct file *filp, unsigned = int cmd, error =3D -EPERM; break; } - if (data->encryption_required && !snapshot_encryption_enabled(data)) { + if (snapshot_encryption_required(data) && + !snapshot_encryption_enabled(data)) { error =3D -EPERM; break; } @@ -351,7 +499,8 @@ static long snapshot_ioctl(struct file *filp, unsigned = int cmd, break; =20 case SNAPSHOT_ATOMIC_RESTORE: - if (data->encryption_required && !snapshot_encryption_enabled(data)) { + if (snapshot_encryption_required(data) && + !snapshot_encryption_enabled(data)) { error =3D -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 =3D 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 =3D snapshot_get_image_size(); size <<=3D PAGE_SHIFT; if (snapshot_encryption_enabled(data)) - size =3D snapshot_get_encrypted_image_size(size); + size =3D snapshot_get_encrypted_image_size(data, size); error =3D put_user(size, (loff_t __user *)arg); break; =20 @@ -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; =20 case SNAPSHOT_S2RAM: @@ -439,7 +592,8 @@ static long snapshot_ioctl(struct file *filp, unsigned = int cmd, error =3D -EPERM; break; } - if (data->encryption_required && !snapshot_encryption_enabled(data)) { + if (snapshot_encryption_required(data) && + !snapshot_encryption_enabled(data)) { error =3D -EPERM; break; } @@ -456,7 +610,8 @@ static long snapshot_ioctl(struct file *filp, unsigned = int cmd, break; =20 case SNAPSHOT_POWER_OFF: - if (data->encryption_required && !snapshot_encryption_enabled(data)) { + if (snapshot_encryption_required(data) && + !snapshot_encryption_enabled(data)) { error =3D -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 @@ =20 #include #include +#include #include #include #include =20 +struct pid; + #define SNAPSHOT_ENCRYPTION_KEY_SIZE AES_KEYSIZE_128 #define SNAPSHOT_AUTH_TAG_SIZE 16 =20 @@ -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 =20 }; @@ -66,7 +82,8 @@ int snapshot_set_user_key(struct snapshot_data *data, =20 int snapshot_store_encryption_seed(const char *buf, size_t count); =20 -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); =20 int snapshot_finalize_decrypted_image(struct snapshot_data *data); =20 @@ -115,7 +132,8 @@ static inline int snapshot_store_encryption_seed(const = char *buf, size_t count) return -ENOTTY; } =20 -static inline loff_t snapshot_get_encrypted_image_size(loff_t raw_size) +static inline loff_t snapshot_get_encrypted_image_size(struct snapshot_dat= a *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); } =20 +int swapdev_block_to_extent(int type, sector_t block, pgoff_t *offset, + pgoff_t *nr_pages) +{ + struct swap_info_struct *si =3D swap_type_to_info(type); + struct swap_extent *se; + struct rb_node *rb; + + if (!si || !(si->flags & SWP_WRITEOK)) + return -ENODEV; + + for (rb =3D rb_first(&si->swap_extent_root); rb; rb =3D rb_next(rb)) { + se =3D rb_entry(rb, struct swap_extent, rb_node); + if (block >=3D se->start_block && + block - se->start_block < se->nr_pages) { + pgoff_t page =3D block - se->start_block; + + *offset =3D se->start_page + page; + *nr_pages =3D 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 numb= er * of free pages of that type (depending on @free)