Encryption and authentication for hibernate snapshot image
Subsystems (4)
cover Encryption and authentication for hibernate snapshot image
Hi,
This patchset is the implementation of encryption and authentication for hibernate snapshot image. The image will be encrypted by AES and authenticated by HMAC.
The hibernate function can be used to snapshot memory pages to an image, then kernel restores the image to memory space in a appropriate time. There have secrets in snapshot image and cracker may modifies it for hacking system. Encryption and authentication of snapshot image can protect the system.
Hibernate function requests the master key through key retention service. The snapshot master key can be a trusted key or a user defined key. The name of snapshot master key is fixed to "swsusp-kmk". User should loads swsusp-kmk to kernel by keyctl tool before the hibernation resume. e.g. The swsusp-kmk must be loaded before systemd-hibernate-resume
The TPM trusted key type is preferred to be the master key. But user defined key can also be used for testing or when the platform doesn't have TPM. User must be aware that the security of user key relies on user space. If the root account be compromised, then the user key will easy to be grabbed.
Lee, Chun-Yi (5):
PM / hibernate: Create snapshot keys handler
PM / hibernate: Generate and verify signature for snapshot image PM / hibernate: Encrypt snapshot image
PM / hibernate: Erase the snapshot master key in snapshot pages PM / hibernate: An option to request that snapshot image must be
authenticated
Documentation/admin-guide/kernel-parameters.txt | 6 + include/linux/kernel.h | 3 +- kernel/panic.c | 1 + kernel/power/Kconfig | 25 + kernel/power/Makefile | 1 + kernel/power/hibernate.c | 62 ++- kernel/power/power.h | 59 +++ kernel/power/snapshot.c | 576 +++++++++++++++++++++++- kernel/power/snapshot_key.c | 303 +++++++++++++ kernel/power/swap.c | 6 + kernel/power/user.c | 12 + 11 files changed, 1036 insertions(+), 18 deletions(-) create mode 100644 kernel/power/snapshot_key.c
Signature · 1 line
2.13.6
PATCH 1/5 PM / hibernate: Create snapshot keys handler
This patch adds a snapshot keys handler for using the key retention service api to create keys for snapshot image encryption and authentication.
This handler uses TPM trusted key as the snapshot master key, and the encryption key and authentication key are derived from the snapshot key. The user defined key can also be used as the snapshot master key , but user must be aware that the security of user key relies on user space.
The name of snapshot key is fixed to "swsusp-kmk". User should use the keyctl tool to load the key blob to root's user keyring. e.g.
# /bin/keyctl add trusted swsusp-kmk "load `cat swsusp-kmk.blob`" @u
or create a new user key. e.g.
# /bin/keyctl add user swsusp-kmk password @u
Then the disk_kmk sysfs file can be used to trigger the initialization of snapshot key:
# echo 1 > /sys/power/disk_kmk
After the initialization be triggered, the secret in the payload of swsusp-key will be copied by hibernation and be erased. Then user can use keyctl to remove swsusp-kmk key from root's keyring.
If user does not trigger the initialization by disk_kmk file after swsusp-kmk be loaded to kernel. Then the snapshot key will be initialled when hibernation be triggered.
--- kernel/power/Kconfig | 14 +++ kernel/power/Makefile | 1 + kernel/power/hibernate.c | 36 +++++++ kernel/power/power.h | 16 +++ kernel/power/snapshot_key.c | 237 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 304 insertions(+) create mode 100644 kernel/power/snapshot_key.c
kernel/power/Kconfig +14 -0
@@ -76,6 +76,20 @@ config HIBERNATION For more information take a look at <file:Documentation/power/swsusp.txt>. +config HIBERNATION_ENC_AUTH + bool "Hibernation encryption and authentication" + depends on HIBERNATION + depends on TRUSTED_KEYS + select CRYPTO_AES + select CRYPTO_HMAC + select CRYPTO_SHA512 + help + This option will encrypt and authenticate the memory snapshot image + of hibernation. It prevents that the snapshot image be arbitrary + modified. User can use TPMs trusted key or user defined key as the + master key of hibernation. The TPM trusted key depends on TPM. The + security of user defined key relies on user space. + config ARCH_SAVE_PAGE_KEYS bool
kernel/power/Makefile +1 -0
@@ -11,6 +11,7 @@ obj-$(CONFIG_FREEZER) += process.o obj-$(CONFIG_SUSPEND) += suspend.o obj-$(CONFIG_PM_TEST_SUSPEND) += suspend_test.o obj-$(CONFIG_HIBERNATION) += hibernate.o snapshot.o swap.o user.o +obj-$(CONFIG_HIBERNATION_ENC_AUTH) += snapshot_key.o obj-$(CONFIG_PM_AUTOSLEEP) += autosleep.o obj-$(CONFIG_PM_WAKELOCKS) += wakelock.o
kernel/power/hibernate.c +36 -0
@@ -1034,6 +1034,39 @@ static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,power_attr (disk); +#ifdef CONFIG_HIBERNATION_ENC_AUTH +static ssize_t disk_kmk_show (struct kobject *kobj,struct kobj_attribute *attr, +char *buf) +{ +if (snapshot_key_initialized ()) +return sprintf (buf,"initialized\n" ); +else +return sprintf (buf,"uninitialized\n" ); +} + +static ssize_t disk_kmk_store (struct kobject *kobj,struct kobj_attribute *attr, +const char *buf,size_t n) +{ +int error =0 ; +char *p; +int len; + +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ; + + p =memchr (buf,'\n' , n); + len = p ? p - buf : n; +if (strncmp (buf,"1" , len)) +return -EINVAL ; + + error =snapshot_key_init (); + +return error ? error : n; +} + +power_attr (disk_kmk); +#endif /* !CONFIG_HIBERNATION_ENC_AUTH */ +static ssize_t resume_show (struct kobject *kobj,struct kobj_attribute *attr,char *buf) {
@@ -1138,6 +1171,9 @@ power_attr(reserved_size);static struct attribute * g[] = { &disk_attr.attr, +#ifdef CONFIG_HIBERNATION_ENC_AUTH + &disk_kmk_attr.attr, +#endif &resume_offset_attr.attr, &resume_attr.attr, &image_size_attr.attr,
kernel/power/power.h +16 -0
@@ -4,6 +4,12 @@#include <linux/utsname.h> #include <linux/freezer.h> #include <linux/compiler.h> +#include <crypto/sha.h> + +/* The max size of encrypted key blob */ +#define KEY_BLOB_BUFF_LEN 512 +#define SNAPSHOT_KEY_SIZE SHA512_DIGEST_SIZE +#define DERIVED_KEY_SIZE SHA512_DIGEST_SIZEstruct swsusp_info {struct new_utsname uts;
@@ -20,6 +26,16 @@ struct swsusp_info {extern void __init hibernate_reserved_size_init (void );extern void __init hibernate_image_size_init (void ); +#ifdef CONFIG_HIBERNATION_ENC_AUTH +/* kernel/power/snapshot_key.c */ +extern int snapshot_key_init (void ); +extern bool snapshot_key_initialized (void ); +extern int snapshot_get_auth_key (u8 *auth_key,bool may_sleep); +extern int snapshot_get_enc_key (u8 *enc_key,bool may_sleep); +#else +static inline int snapshot_key_init (void ) {return 0 ; } +#endif /* !CONFIG_HIBERNATION_ENC_AUTH */ +#ifdef CONFIG_ARCH_HIBERNATION_HEADER /* Maximum size of architecture specific data in a hibernation header */ #define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4)
kernel/power/snapshot_key.c new file +237 -0
237 lines not on this page · raw message
Signature · 1 line
2.13.6
PATCH 2/5 PM / hibernate: Generate and verify signature for snapshot image
In reply to Lee, Chun-YiWhen producing memory snapshot image, hibernation uses HMAC-SHA512 with snapshot key (from TPM trusted key) to calculate the hash of all data pages in snapshot image. The hash result will be kept in the snapshot header as the image signature. Before hibernation restores image, kernel executes HMAC-SHA512 again and compares the result with the signature in the header to verify the integrity of snapshot image.
If the verification failed, the resume process will be stopped. Then the snapshot image will be discarded and system will boot as normal.
On the other hand, a trampoline page be created in snapshot image when hibernation. This trampoline page be used to forward the state of snapshot key and the result of snapshot image verification from boot kernel to image kernel when resuming. The trampoline page will also be used to forward the snapshot key in the later patch.
--- kernel/power/hibernate.c | 18 ++- kernel/power/power.h | 26 ++++ kernel/power/snapshot.c | 387 +++++++++++++++++++++++++++++++++++++++++++++-- kernel/power/swap.c | 6 + kernel/power/user.c | 12 ++ 5 files changed, 432 insertions(+), 17 deletions(-)
kernel/power/hibernate.c +17 -1
@@ -271,10 +271,14 @@ static int create_image(int platform_mode) {int error; + error =snapshot_prepare_hash (false); +if (error) +return error; + error =dpm_suspend_end (PMSG_FREEZE );if (error) {pr_err ("Some devices failed to power down, aborting hibernation\n" ); -return error; + goto finish_hash; } error =platform_pre_snapshot (platform_mode);
@@ -331,6 +335,9 @@ static int create_image(int platform_mode)dpm_resume_start (in_suspend ? (error ?PMSG_RECOVER :PMSG_THAW ) :PMSG_RESTORE ); + finish_hash: +snapshot_finish_hash (); +return error; }
@@ -694,6 +701,14 @@ int hibernate(void)return -EPERM ; } + error =snapshot_key_init (); +if (error) +return error; + + error =snapshot_create_trampoline (); +if (error) +return error; +lock_system_sleep ();/* The snapshot device should not be opened while we're running */ if (!atomic_add_unless (&snapshot_device_available,-1 ,0 )) {
@@ -750,6 +765,7 @@ int hibernate(void)pm_restore_gfp_mask (); }else {pm_pr_dbg ("Image restored successfully.\n" ); +snapshot_restore_trampoline (); } Free_bitmaps:
kernel/power/power.h +26 -0
@@ -11,6 +11,10 @@#define SNAPSHOT_KEY_SIZE SHA512_DIGEST_SIZE#define DERIVED_KEY_SIZE SHA512_DIGEST_SIZE +/* HMAC algorithm for hibernate snapshot signature */ +#define SNAPSHOT_HMAC "hmac(sha512)" +#define SNAPSHOT_DIGEST_SIZE SHA512_DIGEST_SIZE +struct swsusp_info {struct new_utsname uts;u32 version_code;
@@ -19,6 +23,17 @@ struct swsusp_info {unsigned long image_pages;unsigned long pages;unsigned long size; +unsigned long trampoline_pfn; +u8 signature[SNAPSHOT_DIGEST_SIZE ]; +}__aligned(PAGE_SIZE) ; + +/* +* The trampoline page is used to forward information +* from boot kernel to image kernel in restore stage. +*/ +struct trampoline { +bool snapshot_key_valid; +int sig_verify_ret; }__aligned(PAGE_SIZE) ;#ifdef CONFIG_HIBERNATION
@@ -27,12 +42,19 @@ extern void __init hibernate_reserved_size_init(void);extern void __init hibernate_image_size_init (void );#ifdef CONFIG_HIBERNATION_ENC_AUTH +/* kernel/power/snapshot.c */ +extern int snapshot_image_verify_decrypt (void ); +extern int snapshot_prepare_hash (bool may_sleep); +extern void snapshot_finish_hash (void );/* kernel/power/snapshot_key.c */ extern int snapshot_key_init (void );extern bool snapshot_key_initialized (void );extern int snapshot_get_auth_key (u8 *auth_key,bool may_sleep);extern int snapshot_get_enc_key (u8 *enc_key,bool may_sleep);#else +static inline int snapshot_image_verify_decrypt (void ) {return 0 ; } +static inline int snapshot_prepare_hash (bool may_sleep) {return 0 ; } +static inline void snapshot_finish_hash (void ) {}static inline int snapshot_key_init (void ) {return 0 ; }#endif /* !CONFIG_HIBERNATION_ENC_AUTH */
@@ -171,6 +193,10 @@ extern int snapshot_read_next(struct snapshot_handle *handle);extern int snapshot_write_next (struct snapshot_handle *handle);extern void snapshot_write_finalize (struct snapshot_handle *handle);extern int snapshot_image_loaded (struct snapshot_handle *handle); +extern int snapshot_create_trampoline (void ); +extern void snapshot_init_trampoline (void ); +extern void snapshot_restore_trampoline (void ); +extern void snapshot_free_trampoline (void );/* If unset, the snapshot device cannot be open. */ extern atomic_t snapshot_device_available;
kernel/power/snapshot.c +371 -16
490 lines not on this page · raw message
kernel/power/swap.c +6 -0
@@ -1095,6 +1095,9 @@ static int load_image(struct swap_map_handle *handle,snapshot_write_finalize (snapshot);if (!snapshot_image_loaded (snapshot)) ret = -ENODATA ; +if (!ret) + ret =snapshot_image_verify_decrypt (); +snapshot_init_trampoline (); }swsusp_show_speed (start, stop, nr_to_read,"Read" );return ret;
@@ -1447,6 +1450,9 @@ static int load_image_lzo(struct swap_map_handle *handle, } } } +if (!ret) + ret =snapshot_image_verify_decrypt (); +snapshot_init_trampoline (); }swsusp_show_speed (start, stop, nr_to_read,"Read" ); out_clean:
kernel/power/user.c +12 -0
@@ -248,6 +248,7 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,if (!data->frozen || data->ready)break ;pm_restore_gfp_mask (); +snapshot_restore_trampoline ();free_basic_memory_bitmaps (); data->free_bitmaps = false;thaw_processes ();
@@ -259,6 +260,12 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd, error = -EPERM ;break ; } + error =snapshot_key_init (); +if (error) +return error; + error =snapshot_create_trampoline (); +if (error) +return error;pm_restore_gfp_mask (); error =hibernation_snapshot (data->platform_support);if (!error) {
@@ -275,6 +282,11 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd, error = -EPERM ;break ; } +if (snapshot_image_verify_decrypt ()) { + error = -EPERM ; +break ; + } +snapshot_init_trampoline (); error =hibernation_restore (data->platform_support);break ;
Signature · 1 line
2.13.6
PATCH 3/5 PM / hibernate: Encrypt snapshot image
In reply to Lee, Chun-YiTo protect the secret in memory snapshot image, this patch adds the logic to encrypt snapshot pages by AES-CTR. Using AES-CTR is because it's simple, fast and parallelizable. But this patch didn't implement parallel encryption.
The encrypt key is derived from the snapshot key. And the initialization vector will be kept in snapshot header for resuming.
--- kernel/power/hibernate.c | 8 ++- kernel/power/power.h | 6 ++ kernel/power/snapshot.c | 154 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 164 insertions(+), 4 deletions(-)
kernel/power/hibernate.c +7 -1
@@ -275,10 +275,14 @@ static int create_image(int platform_mode)if (error)return error; + error =snapshot_prepare_crypto (false, true); +if (error) + goto finish_hash; + error =dpm_suspend_end (PMSG_FREEZE );if (error) {pr_err ("Some devices failed to power down, aborting hibernation\n" ); - goto finish_hash; + goto finish_crypto; } error =platform_pre_snapshot (platform_mode);
@@ -335,6 +339,8 @@ static int create_image(int platform_mode)dpm_resume_start (in_suspend ? (error ?PMSG_RECOVER :PMSG_THAW ) :PMSG_RESTORE ); + finish_crypto: +snapshot_finish_crypto (); finish_hash:snapshot_finish_hash ();
kernel/power/power.h +6 -0
@@ -5,6 +5,7 @@#include <linux/freezer.h> #include <linux/compiler.h> #include <crypto/sha.h> +#include <crypto/aes.h> /* The max size of encrypted key blob */ #define KEY_BLOB_BUFF_LEN 512
@@ -24,6 +25,7 @@ struct swsusp_info {unsigned long pages;unsigned long size;unsigned long trampoline_pfn; +u8 iv[AES_BLOCK_SIZE ];u8 signature[SNAPSHOT_DIGEST_SIZE ]; }__aligned(PAGE_SIZE) ;
@@ -44,6 +46,8 @@ extern void __init hibernate_image_size_init(void);#ifdef CONFIG_HIBERNATION_ENC_AUTH /* kernel/power/snapshot.c */ extern int snapshot_image_verify_decrypt (void ); +extern int snapshot_prepare_crypto (bool may_sleep,bool create_iv); +extern void snapshot_finish_crypto (void );extern int snapshot_prepare_hash (bool may_sleep);extern void snapshot_finish_hash (void );/* kernel/power/snapshot_key.c */
@@ -53,6 +57,8 @@ extern int snapshot_get_auth_key(u8 *auth_key, bool may_sleep);extern int snapshot_get_enc_key (u8 *enc_key,bool may_sleep);#else static inline int snapshot_image_verify_decrypt (void ) {return 0 ; } +static inline int snapshot_prepare_crypto (bool may_sleep,bool create_iv) {return 0 ; } +static inline void snapshot_finish_crypto (void ) {}static inline int snapshot_prepare_hash (bool may_sleep) {return 0 ; }static inline void snapshot_finish_hash (void ) {}static inline int snapshot_key_init (void ) {return 0 ; }
kernel/power/snapshot.c +151 -3
210 lines not on this page · raw message
Signature · 1 line
2.13.6
PATCH 4/5 PM / hibernate: Erase the snapshot master key in snapshot pages
In reply to Lee, Chun-YiIf the encryption key be guessed then the snapshot master key can also be grabbed from snapshot image. Which means that the authentication key can also be calculated. So kernel erases master key in snapshot pages.
Because the master key in image kernel be erased, kernel uses the trampoline page to forward snapshot master key to image kernel.
--- kernel/power/power.h | 6 +++++ kernel/power/snapshot.c | 5 ++++ kernel/power/snapshot_key.c | 66 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+)
kernel/power/power.h +6 -0
@@ -36,6 +36,7 @@ struct swsusp_info {struct trampoline {bool snapshot_key_valid;int sig_verify_ret; +u8 snapshot_key[SNAPSHOT_KEY_SIZE ]; }__aligned(PAGE_SIZE) ;#ifdef CONFIG_HIBERNATION
@@ -55,6 +56,9 @@ extern int snapshot_key_init(void);extern bool snapshot_key_initialized (void );extern int snapshot_get_auth_key (u8 *auth_key,bool may_sleep);extern int snapshot_get_enc_key (u8 *enc_key,bool may_sleep); +extern void snapshot_key_page_erase (unsigned long pfn,void *buff_addr); +extern void snapshot_key_trampoline_backup (struct trampoline *t); +extern void snapshot_key_trampoline_restore (struct trampoline *t);#else static inline int snapshot_image_verify_decrypt (void ) {return 0 ; }static inline int snapshot_prepare_crypto (bool may_sleep,bool create_iv) {return 0 ; }
@@ -62,6 +66,8 @@ static inline void snapshot_finish_crypto(void) {}static inline int snapshot_prepare_hash (bool may_sleep) {return 0 ; }static inline void snapshot_finish_hash (void ) {}static inline int snapshot_key_init (void ) {return 0 ; } +static inline void snapshot_key_trampoline_backup (struct trampoline *t) {} +static inline void snapshot_key_trampoline_restore (struct trampoline *t) {}#endif /* !CONFIG_HIBERNATION_ENC_AUTH */ #ifdef CONFIG_ARCH_HIBERNATION_HEADER
kernel/power/snapshot.c +5 -0
@@ -1696,6 +1696,9 @@ __copy_data_pages(struct memory_bitmap *copy_bm, struct memory_bitmap *orig_bm) crypto_buffer =page_address (d_page); } +/* Erase key data in snapshot */ +snapshot_key_page_erase (pfn, crypto_buffer); +/* Encrypt hashed page */ encrypt_data_page (crypto_buffer);
@@ -2481,6 +2484,7 @@ void snapshot_init_trampoline(void) t = (struct trampoline *)trampoline_buff;init_sig_verify (t); +snapshot_key_trampoline_backup (t);pr_info ("Hibernation trampoline page prepared\n" ); }
@@ -2504,6 +2508,7 @@ void snapshot_restore_trampoline(void) t = (struct trampoline *)trampoline_virt;handle_sig_verify (t); +snapshot_key_trampoline_restore (t);snapshot_free_trampoline (); }
kernel/power/snapshot_key.c +66 -0
92 lines not on this page · raw message
Signature · 1 line
2.13.6
PATCH 5/5 PM / hibernate: An option to request that snapshot image must be authenticated
In reply to Lee, Chun-YiThis kernel option is similar to the option for kernel module signature verification. When this option is unselected, kernel will be tainted by restored from a snapshot image without (valid) signature.
When the option is selected, kernel will refuse the system to be restored from a unauthenticated image. The hibernation resume process will be stopped , the snapshot image will be discarded and system just boots as normal.
The hibernation can be triggered without snapshot master key when this option is unselected. But kernel will be tainted after hibernation resume.
--- Documentation/admin-guide/kernel-parameters.txt | 6 ++++ include/linux/kernel.h | 3 +- kernel/panic.c | 1 + kernel/power/Kconfig | 11 +++++++ kernel/power/hibernate.c | 8 +++-- kernel/power/power.h | 5 ++++ kernel/power/snapshot.c | 40 +++++++++++++++++++++++-- kernel/power/user.c | 2 +- 8 files changed, 69 insertions(+), 7 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +6 -0
@@ -3941,6 +3941,12 @@ protect_image Turn on image protection during restoration (that will set all pages holding image data during restoration read-only). + enforce_auth When HIBERNATION_ENC_AUTH is set, this means + that snapshot image without (valid) signature + will fail to restore. + Note that if HIBERNATION_ENC_AUTH_FORCE is + set, that is always true, so this option does + nothing. retain_initrd [RAM] Keep initrd memory after extraction
include/linux/kernel.h +2 -1
@@ -598,7 +598,8 @@ extern enum system_states {#define TAINT_LIVEPATCH 15#define TAINT_AUX 16#define TAINT_RANDSTRUCT 17 -#define TAINT_FLAGS_COUNT 18 +#define TAINT_UNSAFE_HIBERNATE 18 +#define TAINT_FLAGS_COUNT 19struct taint_flag {char c_true;/* character printed when tainted */
kernel/panic.c +1 -0
@@ -327,6 +327,7 @@ const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = { [ TAINT_LIVEPATCH ] = { 'K', ' ', true }, [ TAINT_AUX ] = { 'X', ' ', true }, [ TAINT_RANDSTRUCT ] = { 'T', ' ', true }, + [ TAINT_UNSAFE_HIBERNATE ] = { 'H', ' ', true }, };/**
kernel/power/Kconfig +11 -0
@@ -90,6 +90,17 @@ config HIBERNATION_ENC_AUTH master key of hibernation. The TPM trusted key depends on TPM. The security of user defined key relies on user space. +config HIBERNATION_ENC_AUTH_FORCE + bool "Require hibernate snapshot image to be validly signed" + depends on HIBERNATION_ENC_AUTH + help + This option will prevent that a snapshot image without (valid) + signature be restored. Without this option, a unauthenticated + snapshot image can be restored but the restored kernel will be + tainted. Which also means that the hibernation can be triggered + without snapshot key but kernel will be tainted without this + option. + config ARCH_SAVE_PAGE_KEYS bool
kernel/power/hibernate.c +5 -3
@@ -272,11 +272,11 @@ static int create_image(int platform_mode)int error; error =snapshot_prepare_hash (false); -if (error) +if (error &&snapshot_is_enforce_auth ())return error; error =snapshot_prepare_crypto (false, true); -if (error) +if (error &&snapshot_is_enforce_auth ()) goto finish_hash; error =dpm_suspend_end (PMSG_FREEZE );
@@ -708,7 +708,7 @@ int hibernate(void) } error =snapshot_key_init (); -if (error) +if (error &&snapshot_is_enforce_auth ())return error; error =snapshot_create_trampoline ();
@@ -1251,6 +1251,8 @@ static int __init hibernate_setup(char *str) }else if (IS_ENABLED (CONFIG_STRICT_KERNEL_RWX ) && !strncmp (str,"protect_image" ,13 )) {enable_restore_image_protection (); + }else if (!strncmp (str,"enforce_auth" ,10 )) { +snapshot_set_enforce_auth (); }return 1 ; }
kernel/power/power.h +5 -0
@@ -36,6 +36,7 @@ struct swsusp_info {struct trampoline {bool snapshot_key_valid;int sig_verify_ret; +bool enforce_auth;u8 snapshot_key[SNAPSHOT_KEY_SIZE ]; }__aligned(PAGE_SIZE) ;
@@ -51,6 +52,8 @@ extern int snapshot_prepare_crypto(bool may_sleep, bool create_iv);extern void snapshot_finish_crypto (void );extern int snapshot_prepare_hash (bool may_sleep);extern void snapshot_finish_hash (void ); +extern void snapshot_set_enforce_auth (void ); +extern int snapshot_is_enforce_auth (void );/* kernel/power/snapshot_key.c */ extern int snapshot_key_init (void );extern bool snapshot_key_initialized (void );
@@ -65,6 +68,8 @@ static inline int snapshot_prepare_crypto(bool may_sleep, bool create_iv) { retustatic inline void snapshot_finish_crypto (void ) {}static inline int snapshot_prepare_hash (bool may_sleep) {return 0 ; }static inline void snapshot_finish_hash (void ) {} +static inline void snapshot_set_enforce_auth (void ) {} +static inline int snapshot_is_enforce_auth (void ) {return 0 ; }static inline int snapshot_key_init (void ) {return 0 ; }static inline void snapshot_key_trampoline_backup (struct trampoline *t) {}static inline void snapshot_key_trampoline_restore (struct trampoline *t) {}
kernel/power/snapshot.c +38 -2
@@ -1537,6 +1537,23 @@ static int decrypt_data_page(void *encrypted_page)return ret; } +/* enforce the snapshot to be signed */ +#ifdef CONFIG_HIBERNATION_ENC_AUTH_FORCE +static bool enforce_auth = true; +#else +static bool enforce_auth; +#endif + +void snapshot_set_enforce_auth (void ) +{ + enforce_auth = true; +} + +int snapshot_is_enforce_auth (void ) +{ +return enforce_auth; +} +/* * Signature of snapshot image */
@@ -1603,6 +1620,8 @@ int snapshot_prepare_hash(bool may_sleep)crypto_free_shash (tfm); s4_verify_digest =NULL ; s4_verify_desc =NULL ; +if (!enforce_auth) + ret =0 ;return ret; }
@@ -1664,6 +1683,8 @@ int snapshot_image_verify_decrypt(void)pr_warn ("Signature verification failed: %d\n" , ret); error: sig_verify_ret = ret; +if (!enforce_auth) + ret =0 ;return ret; }
@@ -1751,6 +1772,7 @@ static void load_signature(struct swsusp_info *info)static void init_sig_verify (struct trampoline *t) { + t->enforce_auth = enforce_auth; t->sig_verify_ret = sig_verify_ret; t->snapshot_key_valid = snapshot_key_valid; sig_verify_ret =0 ;
@@ -1759,11 +1781,25 @@ static void init_sig_verify(struct trampoline *t)static void handle_sig_verify (struct trampoline *t) { -if (t->sig_verify_ret) + enforce_auth = t->enforce_auth; +if (enforce_auth) +pr_info ("Enforce the snapshot to be validly signed\n" ); + +if (t->sig_verify_ret) {pr_warn ("Signature verification failed: %d\n" , t->sig_verify_ret); -else if (t->snapshot_key_valid) +if (t->snapshot_key_valid) +pr_warn ("Did not find valid snapshot key.\n" ); +/* taint kernel */ +if (!enforce_auth) { +pr_warn ("System resumed from unsafe snapshot - " +"tainting kernel\n" ); +add_taint (TAINT_UNSAFE_HIBERNATE ,LOCKDEP_STILL_OK ); +pr_info ("%s\n" ,print_tainted ()); + } + }else if (t->snapshot_key_valid) {pr_info ("Signature verification passed.\n" ); + } }#else static int
kernel/power/user.c +1 -1
@@ -261,7 +261,7 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,break ; } error =snapshot_key_init (); -if (error) +if (error &&snapshot_is_enforce_auth ())return error; error =snapshot_create_trampoline ();if (error)
Signature · 1 line
2.13.6
Randy Dunlap
reviewerRe: [PATCH 5/5] PM / hibernate: An option to request that snapshot image must be authenticated
Hi,
Quoted 5 lines · Chun-Yi
kernel/power/Kconfig +0
-0
kernel/power/Kconfig config HIBERNATION_ENC_AUTH · line 98
Quoted 9 lines
kernel/power/Kconfig +6
-0@@ -90,6 +90,17 @@ config HIBERNATION_ENC_AUTH master key of hibernation. The TPM trusted key depends on TPM. The security of user defined key relies on user space. +config HIBERNATION_ENC_AUTH_FORCE + bool "Require hibernate snapshot image to be validly signed" + depends on HIBERNATION_ENC_AUTH + help + This option will prevent that a snapshot image without (valid) + signature be restored. Without this option, a unauthenticated
an
kernel/power/Kconfig config HIBERNATION_ENC_AUTH · line 100
Quoted 2 lines
kernel/power/Kconfig +2
-0+ snapshot image can be restored but the restored kernel will be + tainted. Which also means that the hibernation can be triggered
s/Which/This/
or like this:
tainted, which also
Quoted 3 lines
kernel/power/Kconfig +3
-0+ without snapshot key but kernel will be tainted without this + option. +
Signature · 1 line
~Randy
Randy Dunlap
reviewerRe: [PATCH 1/5] PM / hibernate: Create snapshot keys handler
In reply to Lee, Chun-YiHi,
Quoted 8 lines · Chun-Yi
kernel/power/Kconfig +0
-0@@ -76,6 +76,20 @@ config HIBERNATION For more information take a look at <file:Documentation/power/swsusp.txt>.
kernel/power/Kconfig config HIBERNATION · line 88
Quoted 10 lines
kernel/power/Kconfig +10
-0+config HIBERNATION_ENC_AUTH + bool "Hibernation encryption and authentication" + depends on HIBERNATION + depends on TRUSTED_KEYS + select CRYPTO_AES + select CRYPTO_HMAC + select CRYPTO_SHA512 + help + This option will encrypt and authenticate the memory snapshot image + of hibernation. It prevents that the snapshot image be arbitrary
arbitrarily
kernel/power/Kconfig config HIBERNATION · line 89
Quoted 1 line
kernel/power/Kconfig +1
-0+ modified. User can use TPMs trusted key or user defined key as the
The user
or A user can use the TPM's trusted key
Quoted 3 lines
kernel/power/Kconfig +3
-0+ master key of hibernation. The TPM trusted key depends on TPM. The + security of user defined key relies on user space. +
Signature · 1 line
~Randy
Re: [PATCH 5/5] PM / hibernate: An option to request that snapshot image must be authenticated
In reply to Randy DunlapHi Randy,
Quoted 20 lines · Randy Dunlap
Hi,
Quoted 5 lines · Chun-Yi
kernel/power/Kconfig +0
-0kernel/power/Kconfig config HIBERNATION_ENC_AUTH · line 98
Quoted 9 lines
kernel/power/Kconfig +6
-0@@ -90,6 +90,17 @@ config HIBERNATION_ENC_AUTH master key of hibernation. The TPM trusted key depends on TPM. The security of user defined key relies on user space. +config HIBERNATION_ENC_AUTH_FORCE + bool "Require hibernate snapshot image to be validly signed" + depends on HIBERNATION_ENC_AUTH + help + This option will prevent that a snapshot image without (valid) + signature be restored. Without this option, a unauthenticatedan
Quoted 8 lines
kernel/power/Kconfig config HIBERNATION_ENC_AUTH · line 100
Quoted 2 lines
kernel/power/Kconfig +2
-0+ snapshot image can be restored but the restored kernel will be + tainted. Which also means that the hibernation can be triggereds/Which/This/
or like this:
tainted, which also
Thanks for your review and suggestion! I will update the description in next version.
Regards
Joey Lee
Re: [PATCH 1/5] PM / hibernate: Create snapshot keys handler
In reply to Randy DunlapHi Randy,
Quoted 24 lines · Randy Dunlap
Hi,
Quoted 8 lines · Chun-Yi
kernel/power/Kconfig +0
-0@@ -76,6 +76,20 @@ config HIBERNATION For more information take a look at <file:Documentation/power/swsusp.txt>.kernel/power/Kconfig config HIBERNATION · line 88
Quoted 10 lines
kernel/power/Kconfig +10
-0+config HIBERNATION_ENC_AUTH + bool "Hibernation encryption and authentication" + depends on HIBERNATION + depends on TRUSTED_KEYS + select CRYPTO_AES + select CRYPTO_HMAC + select CRYPTO_SHA512 + help + This option will encrypt and authenticate the memory snapshot image + of hibernation. It prevents that the snapshot image be arbitraryarbitrarily
Quoted 5 lines
kernel/power/Kconfig config HIBERNATION · line 89
Quoted 1 line
kernel/power/Kconfig +1
-0+ modified. User can use TPMs trusted key or user defined key as theThe user
or A user can use the TPM's trusted key
Thanks for your review! I will update it in next version.
Joey Lee
Re: [PATCH 1/5] PM / hibernate: Create snapshot keys handler
In reply to Lee, Chun-YiQuoted 10 lines · Chun-Yi
This patch adds a snapshot keys handler for using the key retention service api to create keys for snapshot image encryption and authentication.
This handler uses TPM trusted key as the snapshot master key, and the encryption key and authentication key are derived from the snapshot key. The user defined key can also be used as the snapshot master key , but user must be aware that the security of user key relies on user space.
In case the kernel provides mechanism to generate key from passphase, the master key could also be generated in kernel space other than TPM. It seems than snapshot_key_init() is easy to add the interface for that, right?
Quoted 87 lines
The name of snapshot key is fixed to "swsusp-kmk". User should use the keyctl tool to load the key blob to root's user keyring. e.g.
# /bin/keyctl add trusted swsusp-kmk "load `cat swsusp-kmk.blob`" @uor create a new user key. e.g.
# /bin/keyctl add user swsusp-kmk password @uThen the
disk_kmksysfs file can be used to trigger the initialization of snapshot key:# echo 1 > /sys/power/disk_kmkAfter the initialization be triggered, the secret in the payload of swsusp-key will be copied by hibernation and be erased. Then user can use keyctl to remove swsusp-kmk key from root's keyring.
If user does not trigger the initialization by
disk_kmkfile after swsusp-kmk be loaded to kernel. Then the snapshot key will be initialled when hibernation be triggered.Cc "Rafael J. Wysocki" <[email protected]>Cc Pavel Machek <[email protected]>Cc Chen Yu <[email protected]>Cc Oliver Neukum <[email protected]>Cc Ryan Chen <[email protected]>Cc David Howells <[email protected]>Cc Giovanni Gherdovich <[email protected]>Signed-off-by "Lee, Chun-Yi" <[email protected]>--- kernel/power/Kconfig | 14 +++ kernel/power/Makefile | 1 + kernel/power/hibernate.c | 36 +++++++ kernel/power/power.h | 16 +++ kernel/power/snapshot_key.c | 237 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 304 insertions(+) create mode 100644 kernel/power/snapshot_key.ckernel/power/Kconfig +14
-0@@ -76,6 +76,20 @@ config HIBERNATION For more information take a look at <file:Documentation/power/swsusp.txt>. +config HIBERNATION_ENC_AUTH + bool "Hibernation encryption and authentication" + depends on HIBERNATION + depends on TRUSTED_KEYS + select CRYPTO_AES + select CRYPTO_HMAC + select CRYPTO_SHA512 + help + This option will encrypt and authenticate the memory snapshot image + of hibernation. It prevents that the snapshot image be arbitrary + modified. User can use TPMs trusted key or user defined key as the + master key of hibernation. The TPM trusted key depends on TPM. The + security of user defined key relies on user space. + config ARCH_SAVE_PAGE_KEYS boolkernel/power/Makefile +1
-0@@ -11,6 +11,7 @@ obj-$(CONFIG_FREEZER) += process.o obj-$(CONFIG_SUSPEND) += suspend.o obj-$(CONFIG_PM_TEST_SUSPEND) += suspend_test.o obj-$(CONFIG_HIBERNATION) += hibernate.o snapshot.o swap.o user.o +obj-$(CONFIG_HIBERNATION_ENC_AUTH) += snapshot_key.o obj-$(CONFIG_PM_AUTOSLEEP) += autosleep.o obj-$(CONFIG_PM_WAKELOCKS) += wakelock.okernel/power/hibernate.c +3
-0@@ -1034,6 +1034,39 @@ static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,power_attr (disk); +#ifdef CONFIG_HIBERNATION_ENC_AUTH +static ssize_t disk_kmk_show (struct kobject *kobj,struct kobj_attribute *attr, +char *buf)
kernel/power/hibernate.c static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, · line 1049
Quoted 10 lines
kernel/power/hibernate.c +10
-0+{ +if (snapshot_key_initialized ()) +return sprintf (buf,"initialized\n" ); +else +return sprintf (buf,"uninitialized\n" ); +} + +static ssize_t disk_kmk_store (struct kobject *kobj,struct kobj_attribute *attr, +const char *buf,size_t n) +{
Does kmk mean kernel master key? It might looks unclear from first glance, how about disk_genkey_store()?
kernel/power/hibernate.c static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, · line 1060
Quoted 11 lines
kernel/power/hibernate.c +11
-0+int error =0 ; +char *p; +int len; + +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ; + + p =memchr (buf,'\n' , n); + len = p ? p - buf : n; +if (strncmp (buf,"1" , len)) +return -EINVAL ;
Why user is not allowed to disable(remove) it by
echo 0 ?
Quoted 90 lines
kernel/power/hibernate.c +12
-0+ + error =snapshot_key_init (); + + return error ? error : n; +} + +power_attr (disk_kmk); +#endif /* !CONFIG_HIBERNATION_ENC_AUTH */ +static ssize_t resume_show (struct kobject *kobj,struct kobj_attribute *attr,char *buf) {@@ -1138,6 +1171,9 @@ power_attr(reserved_size);static struct attribute * g[] = { &disk_attr.attr, +#ifdef CONFIG_HIBERNATION_ENC_AUTH + &disk_kmk_attr.attr, +#endif &resume_offset_attr.attr, &resume_attr.attr, &image_size_attr.attr,kernel/power/power.h +16
-0@@ -4,6 +4,12 @@#include <linux/utsname.h> #include <linux/freezer.h> #include <linux/compiler.h> +#include <crypto/sha.h> + +/* The max size of encrypted key blob */ +#define KEY_BLOB_BUFF_LEN 512 +#define SNAPSHOT_KEY_SIZE SHA512_DIGEST_SIZE +#define DERIVED_KEY_SIZE SHA512_DIGEST_SIZEstruct swsusp_info {struct new_utsname uts;@@ -20,6 +26,16 @@ struct swsusp_info {extern void __init hibernate_reserved_size_init (void );extern void __init hibernate_image_size_init (void ); +#ifdef CONFIG_HIBERNATION_ENC_AUTH +/* kernel/power/snapshot_key.c */ +extern int snapshot_key_init (void ); +extern bool snapshot_key_initialized (void ); +extern int snapshot_get_auth_key (u8 *auth_key,bool may_sleep); +extern int snapshot_get_enc_key (u8 *enc_key,bool may_sleep); +#else +static inline int snapshot_key_init (void ) {return 0 ; } +#endif /* !CONFIG_HIBERNATION_ENC_AUTH */ +#ifdef CONFIG_ARCH_HIBERNATION_HEADER /* Maximum size of architecture specific data in a hibernation header */ #define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4)kernel/power/snapshot_key.c new file +28
-0@@ -0,0 +1,237 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* snapshot keys handler +* +* Copyright (C) 2018 Lee, Chun-Yi <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public Licence +* as published by the Free Software Foundation; either version +* 2 of the Licence, or (at your option) any later version. +*/ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/cred.h> +#include <linux/key-type.h> +#include <linux/slab.h> +#include <crypto/hash.h> +#include <crypto/sha.h> +#include <keys/trusted-type.h> +#include <keys/user-type.h> + +#include "power.h" + +static const char hash_alg[] ="sha512" ; +static struct crypto_shash *hash_tfm; + +/* The master key of snapshot */ +static struct snapshot_key {
kernel/power/snapshot_key.c line 38
Quoted 10 lines
kernel/power/snapshot_key.c new file +10
-0+const char *key_name; +bool initialized; +unsigned int key_len; +u8 key[SNAPSHOT_KEY_SIZE ]; +} skey = { + .key_name = "swsusp-kmk", +}; + +static int calc_hash (u8 *digest,const u8 *buf,unsigned int buflen, +bool may_sleep)
calc_hash() is used for both signature and encryption, could it be integrated in snapshot_key_init() thus the code could be re-used?
kernel/power/snapshot_key.c line 40
Quoted 2 lines
kernel/power/snapshot_key.c new file +2
-0+{ +SHASH_DESC_ON_STACK (desc, hash_tfm);
Per commit c2cd0b08e1efd9ee58d09049a6c77e5efa0ef627
SHASH_DESC_ON_STACK() should not be used.
kernel/power/snapshot_key.c line 46
Quoted 6 lines
kernel/power/snapshot_key.c new file +6
-0+int err; + + desc->tfm = hash_tfm; + desc->flags = may_sleep ?CRYPTO_TFM_REQ_MAY_SLEEP :0 ; + + err =crypto_shash_digest (desc, buf, buflen, digest);
Check the err?
Quoted 194 lines
kernel/power/snapshot_key.c new file +191
-0+shash_desc_zero (desc); +return err; +} + +static int calc_key_hash (u8 *key,unsigned int key_len,const char *salt, +u8 *hash,bool may_sleep) +{ +unsigned int salted_buf_len; +u8 *salted_buf; +int ret; + +if (!key || !hash_tfm || !hash) +return -EINVAL ; + + salted_buf_len =strlen (salt) +1 +SNAPSHOT_KEY_SIZE ; + salted_buf =kzalloc (salted_buf_len, + may_sleep ?GFP_KERNEL :GFP_ATOMIC ); +if (!salted_buf) +return -ENOMEM ; + +strcpy (salted_buf, salt); +memcpy (salted_buf +strlen (salted_buf) +1 , key, key_len); + + ret =calc_hash (hash, salted_buf, salted_buf_len, may_sleep); +memzero_explicit (salted_buf, salted_buf_len); +kzfree (salted_buf); + +return ret; +} + +/* Derive authentication/encryption key */ +static int get_derived_key (u8 *derived_key,const char *derived_type_str, +bool may_sleep) +{ +int ret; + +if (!skey.initialized || !hash_tfm) +return -EINVAL ; + + ret =calc_key_hash (skey.key, skey.key_len, derived_type_str, + derived_key, may_sleep); + +return ret; +} + +int snapshot_get_auth_key (u8 *auth_key,bool may_sleep) +{ +return get_derived_key (auth_key,"AUTH_KEY" , may_sleep); +} + +int snapshot_get_enc_key (u8 *enc_key,bool may_sleep) +{ +return get_derived_key (enc_key,"ENC_KEY" , may_sleep); +} + +bool snapshot_key_initialized (void ) +{ +return skey.initialized; +} + +static bool invalid_key (u8 *key,unsigned int key_len) +{ +int i; + +if (!key || !key_len) +return true; + +if (key_len >SNAPSHOT_KEY_SIZE ) { +pr_warn ("Size of swsusp key more than: %d.\n" , +SNAPSHOT_KEY_SIZE ); +return true; + } + +/* zero keyblob is invalid key */ +for (i =0 ; i < key_len; i++) { +if (key[i] !=0 ) +return false; + } +pr_warn ("The swsusp key should not be zero.\n" ); + +return true; +} + +static int trusted_key_init (void ) +{ +struct trusted_key_payload *tkp; +struct key *key; +int err; + +pr_debug ("%s\n" , __func__); + +/* find out swsusp-key */ + key =request_key (&key_type_trusted, skey.key_name,NULL ); +if (IS_ERR (key)) { +pr_err ("Request key error: %ld\n" ,PTR_ERR (key)); + err =PTR_ERR (key); +return err; + } + +down_write (&key->sem); + tkp = key->payload.data[0 ]; +if (invalid_key (tkp->key, tkp->key_len)) { + err = -EINVAL ; + goto key_invalid; + } + skey.key_len = tkp->key_len; +memcpy (skey.key, tkp->key, tkp->key_len); +/* burn the original key contents */ +memzero_explicit (tkp->key, tkp->key_len); + +key_invalid: +up_write (&key->sem); +key_put (key); + +return err; +} + +static int user_key_init (void ) +{ +struct user_key_payload *ukp; +struct key *key; +int err =0 ; + +pr_debug ("%s\n" , __func__); + +/* find out swsusp-key */ + key =request_key (&key_type_user, skey.key_name,NULL ); +if (IS_ERR (key)) { +pr_err ("Request key error: %ld\n" ,PTR_ERR (key)); + err =PTR_ERR (key); +return err; + } + +down_write (&key->sem); + ukp =user_key_payload_locked (key); +if (!ukp) { +/* key was revoked before we acquired its semaphore */ + err = -EKEYREVOKED ; + goto key_invalid; + } +if (invalid_key (ukp->data, ukp->datalen)) { + err = -EINVAL ; + goto key_invalid; + } + skey.key_len = ukp->datalen; +memcpy (skey.key, ukp->data, ukp->datalen); +/* burn the original key contents */ +memzero_explicit (ukp->data, ukp->datalen); + +key_invalid: +up_write (&key->sem); +key_put (key); + +return err; +} + +/* this function may sleeps */ +int snapshot_key_init (void ) +{ +int err; + +pr_debug ("%s\n" , __func__); + +if (skey.initialized) +return 0 ; + + hash_tfm =crypto_alloc_shash (hash_alg,0 ,CRYPTO_ALG_ASYNC ); +if (IS_ERR (hash_tfm)) { +pr_err ("Can't allocate %s transform: %ld\n" , + hash_alg,PTR_ERR (hash_tfm)); +return PTR_ERR (hash_tfm); + } + + err =trusted_key_init (); +if (err) + err =user_key_init (); +if (err) + goto key_fail; + + skey.initialized = true; + +pr_info ("Snapshot key is initialled.\n" ); + +return 0 ; + +key_fail: +crypto_free_shash (hash_tfm); + hash_tfm =NULL ; + +return err; +}Signature · 1 line
2.13.6
Re: [PATCH 1/5] PM / hibernate: Create snapshot keys handler
In reply to Lee, Chun-Yi+cc keyrings list
Quoted 31 lines · Chun-Yi
This patch adds a snapshot keys handler for using the key retention service api to create keys for snapshot image encryption and authentication.
This handler uses TPM trusted key as the snapshot master key, and the encryption key and authentication key are derived from the snapshot key. The user defined key can also be used as the snapshot master key , but user must be aware that the security of user key relies on user space.
The name of snapshot key is fixed to "swsusp-kmk". User should use the keyctl tool to load the key blob to root's user keyring. e.g.
# /bin/keyctl add trusted swsusp-kmk "load `cat swsusp-kmk.blob`" @uor create a new user key. e.g.
# /bin/keyctl add user swsusp-kmk password @uThen the
disk_kmksysfs file can be used to trigger the initialization of snapshot key:# echo 1 > /sys/power/disk_kmkAfter the initialization be triggered, the secret in the payload of swsusp-key will be copied by hibernation and be erased. Then user can use keyctl to remove swsusp-kmk key from root's keyring.
If user does not trigger the initialization by
disk_kmkfile after swsusp-kmk be loaded to kernel. Then the snapshot key will be initialled when hibernation be triggered.
[...]
kernel/power/hibernate.c static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, · line 1055
Quoted 9 lines
kernel/power/hibernate.c +9
-0+static ssize_t disk_kmk_store (struct kobject *kobj,struct kobj_attribute *attr, +const char *buf,size_t n) +{ +int error =0 ; +char *p; +int len; + +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ;
This is wrong, you can't use capable() in a write handler. You'd have to use file_ns_capable(), and I think sysfs currently doesn't give you a pointer to the struct file.
If you want to do this in a write handler, you'll have to either get rid of this check or plumb through the cred struct pointer. Alternatively, you could use some interface that doesn't go through a write handler.
Quoted 10 lines
kernel/power/hibernate.c +10
-0+ p =memchr (buf,'\n' , n); + len = p ? p - buf : n; +if (strncmp (buf,"1" , len)) +return -EINVAL ; + + error =snapshot_key_init (); + +return error ? error : n; +} +
[...]
kernel/power/snapshot_key.c line 173
Quoted 11 lines
kernel/power/snapshot_key.c new file +11
-0+ +static int user_key_init (void ) +{ +struct user_key_payload *ukp; +struct key *key; +int err =0 ; + +pr_debug ("%s\n" , __func__); + +/* find out swsusp-key */ + key =request_key (&key_type_user, skey.key_name,NULL );
request_key() looks at current's keyring. That shouldn't happen in a write handler.
Quoted 11 lines
kernel/power/snapshot_key.c new file +11
-0+if (IS_ERR (key)) { +pr_err ("Request key error: %ld\n" ,PTR_ERR (key)); + err =PTR_ERR (key); +return err; + } + +down_write (&key->sem); + ukp =user_key_payload_locked (key); +if (!ukp) { +/* key was revoked before we acquired its semaphore */ + err = -EKEYREVOKED ;
kernel/power/snapshot_key.c line 194
Quoted 10 lines
kernel/power/snapshot_key.c new file +10
-0+ goto key_invalid; + } +if (invalid_key (ukp->data, ukp->datalen)) { + err = -EINVAL ; + goto key_invalid; + } + skey.key_len = ukp->datalen; +memcpy (skey.key, ukp->data, ukp->datalen); +/* burn the original key contents */ +memzero_explicit (ukp->data, ukp->datalen);
You just zero out the contents of the supplied key? That seems very unidiomatic for the keys subsystem, and makes me wonder why you're using the keys subsystem for this in the first place. It doesn't look like normal use of the keys subsystem.
Quoted 21 lines
kernel/power/snapshot_key.c new file +21
-0+key_invalid: +up_write (&key->sem); +key_put (key); + +return err; +} + +/* this function may sleeps */ +int snapshot_key_init (void ) +{ +int err; + +pr_debug ("%s\n" , __func__); + +if (skey.initialized) +return 0 ; + + hash_tfm =crypto_alloc_shash (hash_alg,0 ,CRYPTO_ALG_ASYNC ); +if (IS_ERR (hash_tfm)) { +pr_err ("Can't allocate %s transform: %ld\n" , + hash_alg,PTR_ERR (hash_tfm));
kernel/power/snapshot_key.c line 226
Quoted 10 lines
kernel/power/snapshot_key.c new file +10
-0+return PTR_ERR (hash_tfm); + } + + err =trusted_key_init (); +if (err) + err =user_key_init (); +if (err) + goto key_fail; + + skey.initialized = true;
Does this need a memory barrier to prevent reordering of the "skey.initialized = true" assignment before the key is fully initialized?
Quoted 15 lines
kernel/power/snapshot_key.c new file +11
-1+ +pr_info ("Snapshot key is initialled.\n" ); + +return 0 ; + +key_fail: +crypto_free_shash (hash_tfm); + hash_tfm =NULL ; + +return err; +} --2.13.6
kbuild test robot
build botRe: [PATCH 1/5] PM / hibernate: Create snapshot keys handler
In reply to Lee, Chun-YiI love your patch! Yet something to improve:
Hi Chun-Yi,
I love your patch! Yet something to improve:
[auto build test ERROR on linus/master]
[also build test ERROR on v4.19-rc3 next-20180913]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Lee-Chun-Yi/Encryption-and-authentication-for-hibernate-snapshot-image/20180914-031757
config: powerpc64-allmodconfig (attached as .config)
compiler: powerpc64-linux-gnu-gcc (Debian 7.2.0-11) 7.2.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.2.0 make.cross ARCH=powerpc64
All errors (new ones prefixed by >>):
powerpc64-linux-gnu-ld: warning: orphan section .gnu.hash' from linker stubs' being placed in section `.gnu.hash'.
>> kernel/power/snapshot_key.o:(.toc+0x0): undefined reference to `key_type_trusted'
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Re: [PATCH 1/5] PM / hibernate: Create snapshot keys handler
In reply to Yu ChenHi Yu Chen,
Thanks for your review and very sorry for my delay!
Quoted 11 lines · Yu Chen
Quoted 10 lines · Chun-Yi
This patch adds a snapshot keys handler for using the key retention service api to create keys for snapshot image encryption and authentication.
This handler uses TPM trusted key as the snapshot master key, and the encryption key and authentication key are derived from the snapshot key. The user defined key can also be used as the snapshot master key , but user must be aware that the security of user key relies on user space.
Quoted 4 lines
In case the kernel provides mechanism to generate key from passphase, the master key could also be generated in kernel space other than TPM. It seems than
snapshot_key_init()is easy to add the interface for that, right?
The user defined key can be used to carry the blob from user space. User sapce can use keyctl tool to enroll the blob. We can design a structure of blob that it carries the hash of passphase, salt... so on. Then kernel parses the blob to generate the key for encryption/authentication.
Quoted 8 lines
Quoted 8 lines
The name of snapshot key is fixed to "swsusp-kmk". User should use the keyctl tool to load the key blob to root's user keyring. e.g.
# /bin/keyctl add trusted swsusp-kmk "load `cat swsusp-kmk.blob`" @uor create a new user key. e.g.
# /bin/keyctl add user swsusp-kmk password @u
The above password can be a blob. The user defined key can carries the blob to kernel. We can design the blob with userland tool later.
Quoted 23 lines
Quoted 23 lines
Then the
disk_kmksysfs file can be used to trigger the initialization of snapshot key:# echo 1 > /sys/power/disk_kmkAfter the initialization be triggered, the secret in the payload of swsusp-key will be copied by hibernation and be erased. Then user can use keyctl to remove swsusp-kmk key from root's keyring.
If user does not trigger the initialization by
disk_kmkfile after swsusp-kmk be loaded to kernel. Then the snapshot key will be initialled when hibernation be triggered.Cc "Rafael J. Wysocki" <[email protected]>Cc Pavel Machek <[email protected]>Cc Chen Yu <[email protected]>Cc Oliver Neukum <[email protected]>Cc Ryan Chen <[email protected]>Cc David Howells <[email protected]>Cc Giovanni Gherdovich <[email protected]>Signed-off-by "Lee, Chun-Yi" <[email protected]>---
[...snip]
Quoted 8 lines
Quoted 8 lines
kernel/power/hibernate.c +0
-0@@ -1034,6 +1034,39 @@ static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,power_attr (disk);
Quoted 15 lines
kernel/power/hibernate.c static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, · line 1049
Quoted 13 lines
kernel/power/hibernate.c +13
-0+#ifdef CONFIG_HIBERNATION_ENC_AUTH +static ssize_t disk_kmk_show (struct kobject *kobj,struct kobj_attribute *attr, +char *buf) +{ +if (snapshot_key_initialized ()) +return sprintf (buf,"initialized\n" ); +else +return sprintf (buf,"uninitialized\n" ); +} + +static ssize_t disk_kmk_store (struct kobject *kobj,struct kobj_attribute *attr, +const char *buf,size_t n) +{Does kmk mean kernel master key? It might looks unclear from first glance, how about
disk_genkey_store()?
Yes, it's the kernel maskter key for disk (hibernate).
The sysfs interfaces is used to load the master key from keyring, then kernel parses the encrypted key or user defined key to grab the plaintext key. So sysfs triggered the initial process of the master key.
Even user space didn't trigger the process through sysfs, kernel will still runs the initial process when hibernation be triggered. Then kernel uses the master key to derive encrypte key and authenticate key.
I prefer to keep the name of sysfs and the function name.
Quoted 5 lines
Quoted 5 lines
kernel/power/hibernate.c +5
-0+int error =0 ; +char *p; +int len; + +if (!capable (CAP_SYS_ADMIN ))
Quoted 8 lines
kernel/power/hibernate.c static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, · line 1060
Quoted 6 lines
kernel/power/hibernate.c +6
-0+return -EPERM ; + + p =memchr (buf,'\n' , n); + len = p ? p - buf : n; +if (strncmp (buf,"1" , len)) +return -EINVAL ;Why user is not allowed to disable(remove) it by
echo 0 ?
Similar with evm, this sysfs interface gives user space a chance to ask kernel to initial master key. Once the master key be initialled and loaded, kernel doesn't want the key to be removed because the key is unsealed by TPM. The PCRs may already capped then there have no other master key can be unsealed and enrolled.
So, I prefer that do not give user space the function to remove an enrolled master key. Once the master key be loaded, kernel will use the key to encrypt snapshot image.
Quoted 8 lines
Quoted 8 lines
kernel/power/hibernate.c +8
-0+ + error =snapshot_key_init (); + + return error ? error : n; +} + +power_attr (disk_kmk); +#endif /* !CONFIG_HIBERNATION_ENC_AUTH */
[...snip]
Quoted 5 lines
Quoted 5 lines
kernel/power/snapshot_key.c new file +0
-0
[...snip]
Quoted 6 lines
kernel/power/snapshot_key.c line 38
Quoted 3 lines
kernel/power/snapshot_key.c new file +3
-0+ +static int calc_hash (u8 *digest,const u8 *buf,unsigned int buflen, +bool may_sleep)
calc_hash()is used for both signature and encryption, could it be integrated insnapshot_key_init()thus the code could be re-used?
The snapshot_key_init() is used to parse the blob of master key for grabbing the plaintext of key. It doesn't relate to encryption key and authentication key.
The snapshot_key_init() reuses calc_hash() to calculate the fingerprint of master key in 0004 patch. The get_key_fingerprint() is a wrapper of calc_hash().
Quoted 4 lines
kernel/power/snapshot_key.c line 40
Quoted 2 lines
kernel/power/snapshot_key.c new file +2
-0+{ +SHASH_DESC_ON_STACK (desc, hash_tfm);Per commit
c2cd0b08e1efd9ee58d09049a6c77e5efa0ef627
SHASH_DESC_ON_STACK()should not be used.
Thank you for point out! I will avoid to use VLA in next version.
Quoted 7 lines
kernel/power/snapshot_key.c line 46
Quoted 6 lines
kernel/power/snapshot_key.c new file +6
-0+int err; + + desc->tfm = hash_tfm; + desc->flags = may_sleep ?CRYPTO_TFM_REQ_MAY_SLEEP :0 ; + + err =crypto_shash_digest (desc, buf, buflen, digest);Check the err?
I will check the err, thanks!
Quoted 4 lines
kernel/power/snapshot_key.c line 50
Quoted 4 lines
kernel/power/snapshot_key.c new file +4
-0+shash_desc_zero (desc); +return err; +} +
[...snip]
Thanks a lot!
Joey Lee
Re: [PATCH 1/5] PM / hibernate: Create snapshot keys handler
In reply to Jann HornHi Jann,
Thanks for your review and very sorry for my delay!
Quoted 6 lines · Jann Horn
+cc keyrings list
Quoted 3 lines · Chun-Yi
This patch adds a snapshot keys handler for using the key retention service api to create keys for snapshot image encryption and authentication.
[...snip]
Quoted 10 lines
kernel/power/hibernate.c static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, · line 1055
Quoted 9 lines
kernel/power/hibernate.c +9
-0+static ssize_t disk_kmk_store (struct kobject *kobj,struct kobj_attribute *attr, +const char *buf,size_t n) +{ +int error =0 ; +char *p; +int len; + +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ;
Quoted 8 lines
This is wrong, you can't use
capable()in a write handler. You'd have to usefile_ns_capable(), and I think sysfs currently doesn't give you a pointer to the struct file.
If you want to do this in a write handler, you'll have to either get rid of this check or plumb through the cred struct pointer. Alternatively, you could use some interface that doesn't go through a write handler.
Thank you for point out this problem.
Actually the evm_write_key() is the example for my code. The difference is that evm creates interface file on securityfs, but my implementation is on sysfs:
security/integrity/evm/evm_secfs.c
static ssize_t evm_write_key(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
int i, ret;
if (!capable(CAP_SYS_ADMIN) || (evm_initialized & EVM_SETUP))
return -EPERM;...
On the other hand, the writing handler of /sys/power/wake_lock also uses capable() to check the CAP_BLOCK_SUSPEND capability:
kernel/power/main.c
static ssize_t wake_lock_store(struct kobject *kobj,
struct kobj_attribute *attr, const char *buf, size_t n)
{
int error = pm_wake_lock(buf);
return error ? error : n;
}power_attr(wake_lock);
kernel/power/wakelock.c
int pm_wake_lock(const char *buf)
{
...
if (!capable(CAP_BLOCK_SUSPEND)) return -EPERM;
...
So I confused for when can capable() be used in sysfs interface? Is capable() only allowed in reading handler? Why the writing handler of securityfs can use capable()?
Quoted 6 lines
Quoted 6 lines
kernel/power/snapshot_key.c new file +6
-0+ +static int user_key_init (void ) +{ +struct user_key_payload *ukp; +struct key *key; +int err =0 ;
Quoted 9 lines
kernel/power/snapshot_key.c line 173
Quoted 5 lines
kernel/power/snapshot_key.c new file +5
-0+ +pr_debug ("%s\n" , __func__); + +/* find out swsusp-key */ + key =request_key (&key_type_user, skey.key_name,NULL );
request_key()looks at current's keyring. That shouldn't happen in a write handler.
The evm_write_key() also uses request_key() but it's on securityfs. Should I move my sysfs interface to securityfs?
Quoted 22 lines
Quoted 11 lines
kernel/power/snapshot_key.c new file +11
-0+if (IS_ERR (key)) { +pr_err ("Request key error: %ld\n" ,PTR_ERR (key)); + err =PTR_ERR (key); +return err; + } + +down_write (&key->sem); + ukp =user_key_payload_locked (key); +if (!ukp) { +/* key was revoked before we acquired its semaphore */ + err = -EKEYREVOKED ;kernel/power/snapshot_key.c line 194
Quoted 10 lines
kernel/power/snapshot_key.c new file +10
-0+ goto key_invalid; + } +if (invalid_key (ukp->data, ukp->datalen)) { + err = -EINVAL ; + goto key_invalid; + } + skey.key_len = ukp->datalen; +memcpy (skey.key, ukp->data, ukp->datalen); +/* burn the original key contents */ +memzero_explicit (ukp->data, ukp->datalen);
Quoted 5 lines
You just zero out the contents of the supplied key? That seems very unidiomatic for the keys subsystem, and makes me wonder why you're using the keys subsystem for this in the first place. It doesn't look like normal use of the keys subsystem.
Because I want that only one decrypted key in kernel memory. Then hibernation can handle the key more easy. In evm_init_key(), it also burned the key contents after evm key be initialled:
security/integrity/evm/evm_crypto.c
int evm_init_key(void)
{
[...snip]
/* burn the original key contents */ memset(ekp->decrypted_data, 0, ekp->decrypted_datalen); up_read(&evm_key->sem); key_put(evm_key); return rc; }
The keys subsystem already handles the interactive with userland and TPM. That's the reason for using keys subsystem in hibernation.
Quoted 27 lines
Quoted 27 lines
kernel/power/snapshot_key.c new file +27
-0+key_invalid: +up_write (&key->sem); +key_put (key); + +return err; +} + +/* this function may sleeps */ +int snapshot_key_init (void ) +{ +int err; + +pr_debug ("%s\n" , __func__); + +if (skey.initialized) +return 0 ; + + hash_tfm =crypto_alloc_shash (hash_alg,0 ,CRYPTO_ALG_ASYNC ); +if (IS_ERR (hash_tfm)) { +pr_err ("Can't allocate %s transform: %ld\n" , + hash_alg,PTR_ERR (hash_tfm)); +return PTR_ERR (hash_tfm); + } + + err =trusted_key_init (); +if (err) + err =user_key_init ();
Quoted 9 lines
kernel/power/snapshot_key.c line 226
Quoted 4 lines
kernel/power/snapshot_key.c new file +4
-0+if (err) + goto key_fail; + + skey.initialized = true;Does this need a memory barrier to prevent reordering of the "skey.initialized = true" assignment before the key is fully initialized?
Thanks for your reminding. I will add memory barrier here.
Thank a lot!
Joey Lee
Re: [PATCH 1/5] PM / hibernate: Create snapshot keys handler
+Andy for opinions on things in write handlers
+Mimi Zohar as EVM maintainer
Quoted 59 lines · joeyli
Quoted 4 lines · Jann Horn
Quoted 3 lines · Chun-Yi
This patch adds a snapshot keys handler for using the key retention service api to create keys for snapshot image encryption and authentication.
[...snip]
Quoted 10 lines
kernel/power/hibernate.c static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, · line 1055
Quoted 9 lines
kernel/power/hibernate.c +9
-0+static ssize_t disk_kmk_store (struct kobject *kobj,struct kobj_attribute *attr, +const char *buf,size_t n) +{ +int error =0 ; +char *p; +int len; + +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ;Quoted 8 lines
This is wrong, you can't use
capable()in a write handler. You'd have to usefile_ns_capable(), and I think sysfs currently doesn't give you a pointer to the struct file.
If you want to do this in a write handler, you'll have to either get rid of this check or plumb through the cred struct pointer. Alternatively, you could use some interface that doesn't go through a write handler.Thank you for point out this problem.
Actually the
evm_write_key()is the example for my code. The difference is that evm creates interface file on securityfs, but my implementation is on sysfs:
security/integrity/evm/evm_secfs.cstatic
ssize_tevm_write_key(struct file *file, const char__user*buf,
size_tcount,loff_t*ppos){ int i, ret; if (!capable(CAP_SYS_ADMIN) || (evm_initialized & EVM_SETUP)) return -EPERM;...
On the other hand, the writing handler of
/sys/power/wake_lockalso usescapable()to check theCAP_BLOCK_SUSPENDcapability:
kernel/power/main.c
staticssize_twake_lock_store(struct kobject *kobj,
structkobj_attribute*attr, const char *buf,size_tn){ int error = pm_wake_lock(buf); return error ? error : n; }
power_attr(wake_lock);
kernel/power/wakelock.c
intpm_wake_lock(const char *buf)
{
...
Quoted 8 lines
if (!capable(CAP_BLOCK_SUSPEND)) return -EPERM;...
So I confused for when can
capable()be used in sysfs interface? Iscapable()only allowed in reading handler? Why the writing handler of securityfs can usecapable()?
They can't, they're all wrong. :P All of these capable() checks in write handlers have to be assumed to be ineffective. But it's not a big deal because you still need UID 0 to access these files.
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
kernel/power/snapshot_key.c new file +5
-0+static int user_key_init (void ) +{ +struct user_key_payload *ukp; +struct key *key; +int err =0 ;Quoted 9 lines
kernel/power/snapshot_key.c line 173
Quoted 5 lines
kernel/power/snapshot_key.c new file +5
-0+ +pr_debug ("%s\n" , __func__); + +/* find out swsusp-key */ + key =request_key (&key_type_user, skey.key_name,NULL );
request_key()looks at current's keyring. That shouldn't happen in a write handler.
Quoted 2 lines
The
evm_write_key()also usesrequest_key()but it's on securityfs. Should I move my sysfs interface to securityfs?
I don't think you should be doing this in the context of any filesystem. If EVM does that, EVM is doing it wrong.
Quoted 43 lines
Quoted 22 lines
Quoted 11 lines
kernel/power/snapshot_key.c new file +11
-0+if (IS_ERR (key)) { +pr_err ("Request key error: %ld\n" ,PTR_ERR (key)); + err =PTR_ERR (key); +return err; + } + +down_write (&key->sem); + ukp =user_key_payload_locked (key); +if (!ukp) { +/* key was revoked before we acquired its semaphore */ + err = -EKEYREVOKED ;kernel/power/snapshot_key.c line 194
Quoted 10 lines
kernel/power/snapshot_key.c new file +10
-0+ goto key_invalid; + } +if (invalid_key (ukp->data, ukp->datalen)) { + err = -EINVAL ; + goto key_invalid; + } + skey.key_len = ukp->datalen; +memcpy (skey.key, ukp->data, ukp->datalen); +/* burn the original key contents */ +memzero_explicit (ukp->data, ukp->datalen);Quoted 5 lines
You just zero out the contents of the supplied key? That seems very unidiomatic for the keys subsystem, and makes me wonder why you're using the keys subsystem for this in the first place. It doesn't look like normal use of the keys subsystem.
Because I want that only one decrypted key in kernel memory. Then hibernation can handle the key more easy. In
evm_init_key(), it also burned the key contents after evm key be initialled:
security/integrity/evm/evm_crypto.c
intevm_init_key(void)
{
[...snip]/* burn the original key contents */ memset(ekp->decrypted_data, 0, ekp->decrypted_datalen); up_read(&evm_key->sem); key_put(evm_key); return rc; }
Quoted 2 lines
The keys subsystem already handles the interactive with userland and TPM. That's the reason for using keys subsystem in hibernation.
How do you guarantee that the user is allowed to overwrite that key? I don't understand the keys subsystem very well - could this be a key on the trusted keyring, or something like that?
Re: [PATCH 1/5] PM / hibernate: Create snapshot keys handler
Quoted 38 lines · Jann Horn
(quoted diff) +2
-0+Andy for opinions on things in write handlers +Mimi Zohar as EVM maintainerQuoted 33 lines · joeyli
Quoted 4 lines · Jann Horn
Quoted 3 lines · Chun-Yi
This patch adds a snapshot keys handler for using the key retention service api to create keys for snapshot image encryption and authentication.
[...snip]
Quoted 10 lines
kernel/power/hibernate.c static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, · line 1055
Quoted 9 lines
kernel/power/hibernate.c +9
-0+static ssize_t disk_kmk_store (struct kobject *kobj,struct kobj_attribute *attr, +const char *buf,size_t n) +{ +int error =0 ; +char *p; +int len; + +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ;Quoted 8 lines
This is wrong, you can't use
capable()in a write handler. You'd have to usefile_ns_capable(), and I think sysfs currently doesn't give you a pointer to the struct file.
If you want to do this in a write handler, you'll have to either get rid of this check or plumb through the cred struct pointer. Alternatively, you could use some interface that doesn't go through a write handler.Thank you for point out this problem.
Actually the
evm_write_key()is the example for my code. The difference is that evm creates interface file on securityfs, but my implementation is on sysfs:
security/integrity/evm/evm_secfs.c
Quoted 7 lines
Quoted 7 lines
static
ssize_tevm_write_key(struct file *file, const char__user*buf,
size_tcount,loff_t*ppos){ int i, ret; if (!capable(CAP_SYS_ADMIN) || (evm_initialized & EVM_SETUP)) return -EPERM;
Yeah, that's a bug.
Quoted 15 lines
Quoted 15 lines
...
On the other hand, the writing handler of
/sys/power/wake_lockalso usescapable()to check theCAP_BLOCK_SUSPENDcapability:
kernel/power/main.c
staticssize_twake_lock_store(struct kobject *kobj,
structkobj_attribute*attr, const char *buf,size_tn){ int error = pm_wake_lock(buf); return error ? error : n; }
power_attr(wake_lock);
Quoted 7 lines
Quoted 7 lines
kernel/power/wakelock.c
intpm_wake_lock(const char *buf)
{
...if (!capable(CAP_BLOCK_SUSPEND)) return -EPERM;...
Also a bug.
Quoted 9 lines
Quoted 5 lines
So I confused for when can
capable()be used in sysfs interface? Iscapable()only allowed in reading handler? Why the writing handler of securityfs can usecapable()?They can't, they're all wrong. :P All of these
capable()checks in write handlers have to be assumed to be ineffective. But it's not a big deal because you still need UID 0 to access these files.
Why are there capability checks at all?
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
kernel/power/snapshot_key.c line 173
Quoted 11 lines
kernel/power/snapshot_key.c new file +10
-0+static int user_key_init (void ) +{ +struct user_key_payload *ukp; +struct key *key; +int err =0 ; + +pr_debug ("%s\n" , __func__); + +/* find out swsusp-key */ + key =request_key (&key_type_user, skey.key_name,NULL );
request_key()looks at current's keyring. That shouldn't happen in a write handler.
Quoted 5 lines
Quoted 2 lines
The
evm_write_key()also usesrequest_key()but it's on securityfs. Should I move my sysfs interface to securityfs?I don't think you should be doing this in the context of any filesystem. If EVM does that, EVM is doing it wrong.
EVM sounds buggy.
In general if you look at current *at all* in an implementation of write() *in any filesystem*, you are doing it wrong.
Re: [PATCH 1/5] PM / hibernate: Create snapshot keys handler
In reply to Jann HornQuoted 68 lines · Jann Horn
(quoted diff) +2
-0+Andy for opinions on things in write handlers +Mimi Zohar as EVM maintainerQuoted 64 lines · joeyli
Quoted 4 lines · Jann Horn
Quoted 3 lines · Chun-Yi
This patch adds a snapshot keys handler for using the key retention service api to create keys for snapshot image encryption and authentication.
[...snip]
Quoted 10 lines
kernel/power/hibernate.c static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, · line 1055
Quoted 9 lines
kernel/power/hibernate.c +9
-0+static ssize_t disk_kmk_store (struct kobject *kobj,struct kobj_attribute *attr, +const char *buf,size_t n) +{ +int error =0 ; +char *p; +int len; + +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ;Quoted 8 lines
This is wrong, you can't use
capable()in a write handler. You'd have to usefile_ns_capable(), and I think sysfs currently doesn't give you a pointer to the struct file.
If you want to do this in a write handler, you'll have to either get rid of this check or plumb through the cred struct pointer. Alternatively, you could use some interface that doesn't go through a write handler.Thank you for point out this problem.
Actually the
evm_write_key()is the example for my code. The difference is that evm creates interface file on securityfs, but my implementation is on sysfs:
security/integrity/evm/evm_secfs.cstatic
ssize_tevm_write_key(struct file *file, const char__user*buf,
size_tcount,loff_t*ppos){ int i, ret; if (!capable(CAP_SYS_ADMIN) || (evm_initialized & EVM_SETUP)) return -EPERM;...
On the other hand, the writing handler of
/sys/power/wake_lockalso usescapable()to check theCAP_BLOCK_SUSPENDcapability:
kernel/power/main.c
staticssize_twake_lock_store(struct kobject *kobj,
structkobj_attribute*attr, const char *buf,size_tn){ int error = pm_wake_lock(buf); return error ? error : n; }
power_attr(wake_lock);
kernel/power/wakelock.c
intpm_wake_lock(const char *buf)
{
...if (!capable(CAP_BLOCK_SUSPEND)) return -EPERM;...
Quoted 7 lines
Quoted 3 lines
So I confused for when can
capable()be used in sysfs interface? Iscapable()only allowed in reading handler? Why the writing handler of securityfs can usecapable()?They can't, they're all wrong. :P All of these
capable()checks in write handlers have to be assumed to be ineffective. But it's not a big deal because you still need UID 0 to access these files.
Thanks, Jann. At least EVM should be updated to replace the existing "capable" check with "if (file_ns_capable(file, &init_user_ns, CAP_SYS_ADMIN))".
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
kernel/power/snapshot_key.c line 173
Quoted 11 lines
kernel/power/snapshot_key.c new file +10
-0+static int user_key_init (void ) +{ +struct user_key_payload *ukp; +struct key *key; +int err =0 ; + +pr_debug ("%s\n" , __func__); + +/* find out swsusp-key */ + key =request_key (&key_type_user, skey.key_name,NULL );
request_key()looks at current's keyring. That shouldn't happen in a write handler.
Quoted 5 lines
Quoted 2 lines
The
evm_write_key()also usesrequest_key()but it's on securityfs. Should I move my sysfs interface to securityfs?I don't think you should be doing this in the context of any filesystem. If EVM does that, EVM is doing it wrong.
This is being executed in the initramfs before pivoting root.
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
kernel/power/snapshot_key.c new file +21
-0+if (IS_ERR (key)) { +pr_err ("Request key error: %ld\n" ,PTR_ERR (key)); + err =PTR_ERR (key); +return err; + } + +down_write (&key->sem); + ukp =user_key_payload_locked (key); +if (!ukp) { +/* key was revoked before we acquired its semaphore */ + err = -EKEYREVOKED ; + goto key_invalid; + } +if (invalid_key (ukp->data, ukp->datalen)) { + err = -EINVAL ; + goto key_invalid; + } + skey.key_len = ukp->datalen; +memcpy (skey.key, ukp->data, ukp->datalen); +/* burn the original key contents */ +memzero_explicit (ukp->data, ukp->datalen);
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
You just zero out the contents of the supplied key? That seems very unidiomatic for the keys subsystem, and makes me wonder why you're using the keys subsystem for this in the first place. It doesn't look like normal use of the keys subsystem.
Right, evm_write_key() is normally called from the initramfs to signal the kernel that the EVM encrypted key has been loaded onto root's keyring. The decrypted EVM key is then copied to kernel memory. Before returning, the decrypted EVM key on root's keyring is cleared. The initramfs then removes the EVM key from the keyring.
For more details about trusted/encrypted keys refer to Documentation/security/keys/trusted-encrypted.rst.
Mimi
Quoted 22 lines
Quoted 15 lines
Because I want that only one decrypted key in kernel memory. Then hibernation can handle the key more easy. In
evm_init_key(), it also burned the key contents after evm key be initialled:
security/integrity/evm/evm_crypto.c
intevm_init_key(void)
{
[...snip]/* burn the original key contents */ memset(ekp->decrypted_data, 0, ekp->decrypted_datalen); up_read(&evm_key->sem); key_put(evm_key); return rc; }Quoted 2 lines
The keys subsystem already handles the interactive with userland and TPM. That's the reason for using keys subsystem in hibernation.
How do you guarantee that the user is allowed to overwrite that key? I don't understand the keys subsystem very well - could this be a key on the trusted keyring, or something like that?
Re: [PATCH 1/5] PM / hibernate: Create snapshot keys handler
In reply to Andy LutomirskiHi Any, Jann,
Quoted 105 lines · Andy Lutomirski
Quoted 38 lines · Jann Horn
(quoted diff) +2
-0+Andy for opinions on things in write handlers +Mimi Zohar as EVM maintainerQuoted 33 lines · joeyli
Quoted 4 lines · Jann Horn
Quoted 3 lines · Chun-Yi
This patch adds a snapshot keys handler for using the key retention service api to create keys for snapshot image encryption and authentication.
[...snip]
Quoted 10 lines
kernel/power/hibernate.c static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, · line 1055
Quoted 9 lines
kernel/power/hibernate.c +9
-0+static ssize_t disk_kmk_store (struct kobject *kobj,struct kobj_attribute *attr, +const char *buf,size_t n) +{ +int error =0 ; +char *p; +int len; + +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ;Quoted 8 lines
This is wrong, you can't use
capable()in a write handler. You'd have to usefile_ns_capable(), and I think sysfs currently doesn't give you a pointer to the struct file.
If you want to do this in a write handler, you'll have to either get rid of this check or plumb through the cred struct pointer. Alternatively, you could use some interface that doesn't go through a write handler.Thank you for point out this problem.
Actually the
evm_write_key()is the example for my code. The difference is that evm creates interface file on securityfs, but my implementation is on sysfs:
security/integrity/evm/evm_secfs.cQuoted 7 lines
Quoted 7 lines
static
ssize_tevm_write_key(struct file *file, const char__user*buf,
size_tcount,loff_t*ppos){ int i, ret; if (!capable(CAP_SYS_ADMIN) || (evm_initialized & EVM_SETUP)) return -EPERM;Yeah, that's a bug.
Quoted 15 lines
Quoted 15 lines
...
On the other hand, the writing handler of
/sys/power/wake_lockalso usescapable()to check theCAP_BLOCK_SUSPENDcapability:
kernel/power/main.c
staticssize_twake_lock_store(struct kobject *kobj,
structkobj_attribute*attr, const char *buf,size_tn){ int error = pm_wake_lock(buf); return error ? error : n; }
power_attr(wake_lock);Quoted 7 lines
Quoted 7 lines
kernel/power/wakelock.c
intpm_wake_lock(const char *buf)
{
...if (!capable(CAP_BLOCK_SUSPEND)) return -EPERM;...
Also a bug.
Quoted 9 lines
Quoted 5 lines
So I confused for when can
capable()be used in sysfs interface? Iscapable()only allowed in reading handler? Why the writing handler of securityfs can usecapable()?They can't, they're all wrong. :P All of these
capable()checks in write handlers have to be assumed to be ineffective. But it's not a big deal because you still need UID 0 to access these files.Why are there capability checks at all?
Quoted 19 lines
Quoted 18 lines
Quoted 15 lines
kernel/power/snapshot_key.c line 173
Quoted 11 lines
kernel/power/snapshot_key.c new file +10
-0+static int user_key_init (void ) +{ +struct user_key_payload *ukp; +struct key *key; +int err =0 ; + +pr_debug ("%s\n" , __func__); + +/* find out swsusp-key */ + key =request_key (&key_type_user, skey.key_name,NULL );
request_key()looks at current's keyring. That shouldn't happen in a write handler.The
evm_write_key()also usesrequest_key()but it's on securityfs. Should I move my sysfs interface to securityfs?
Quoted 7 lines
Quoted 2 lines
I don't think you should be doing this in the context of any filesystem. If EVM does that, EVM is doing it wrong.
EVM sounds buggy.
In general if you look at current *at all* in an implementation of
write()*in any filesystem*, you are doing it wrong.
I have read CVE-2013-1959... Thanks for Jann and Andy's review.
I will create the sysfs interface through other way, then using file_ns_capable() for capability checking in next version.
Thanks a lot!
Joey Lee