Refactor and harden PLPKS code
Subsystems (3)
cover Refactor and harden PLPKS code
This series contains fixes, hardening changes, and general cleanup for the PLPKS code. It addresses error handling and input validation issues, improves the cleanup of sensitive data, and prevents the wrapping_features sysfs attribute from being exposed when the key-wrapping interface is not supported.
Srish Srinivasan (12):
pseries/plpks: fix error handling in plpks_read_var() pseries/plpks: fix error code for wrapping key reads pseries/plpks: validate input to plpks_signed_update_var() pseries/plpks: clear sensitive PLPKS buffers pseries/plpks: clear sensitive buffers in wrapping operations pseries/plpks: clear signed update authentication buffer pseries/plpks: clear SED key data after use pseries/plpks: fix self-reference in plpks_var initializer pseries/plpks: hide wrapping_features when unsupported pseries/plpks: make narrowing conversions explicit pseries/plpks: rename the default wrapping key macro pseries/plpks: update PKS documentation and MAINTAINERS entry
Documentation/arch/powerpc/papr_hcalls.rst | 16 +++--- MAINTAINERS | 2 +- arch/powerpc/platforms/pseries/plpks-sysfs.c | 20 ++++++-- arch/powerpc/platforms/pseries/plpks.c | 51 ++++++++++--------- .../powerpc/platforms/pseries/plpks_sed_ops.c | 12 +++-- 5 files changed, 61 insertions(+), 40 deletions(-)
Signature · 1 line
2.52.0
PATCH 1/12 pseries/plpks: fix error handling in plpks_read_var()
When a PLPKS variable is initialized without a policy and used to read an object with the PLPKS_WRAPPINGKEY policy set, the hypervisor returns H_AUTHORITY along with the object's policy. However, plpks_read_var() currently treats this the same as any other H_AUTHORITY failure and returns an error without propagating the policy information to the caller.
Distinguish this case from other H_AUTHORITY failures and propagate the returned policy to the caller while preserving the authorization error. Remove the explicit assignment of zero to rc on H_SUCCESS, as it is redundant.
Also validate the PLPKS variable pointer before dereferencing it.
--- arch/powerpc/platforms/pseries/plpks.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
arch/powerpc/platforms/pseries/plpks.c +8 -6
@@ -822,6 +822,9 @@ static int plpks_read_var(u8 consumer, struct plpks_var *var)u8 *output;int rc; +if (!var) +return -EINVAL ; +if (var->namelen >PLPKS_MAX_NAME_SIZE )return -EINVAL ;
@@ -856,22 +859,21 @@ static int plpks_read_var(u8 consumer, struct plpks_var *var)virt_to_phys (var->name), var->namelen,virt_to_phys (output), maxobjsize); -if (rc !=H_SUCCESS ) { rc =pseries_status_to_err (rc); - goto out_free_output; +if (rc != -EPERM || !retbuf[1 ]) + goto out_free_output; + goto out_copy_policy; }if (!var->data || var->datalen > retbuf[0 ]) var->datalen = retbuf[0 ]; - var->policy = retbuf[1 ]; -if (var->data)memcpy (var->data, output, var->datalen); - rc =0 ; - +out_copy_policy: + var->policy = retbuf[1 ]; out_free_output:kfree (output); out_free_label:
Signature · 1 line
2.52.0
PATCH 2/12 pseries/plpks: fix error code for wrapping key reads
In reply to Srish SrinivasanPLPKS variables with the PLPKS_WRAPPINGKEY policy bit set are not readable. Return -EPERM instead of -EINVAL in this case to better reflect the access restriction being enforced.
--- arch/powerpc/platforms/pseries/plpks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/platforms/pseries/plpks.c +1 -1
@@ -829,7 +829,7 @@ static int plpks_read_var(u8 consumer, struct plpks_var *var)return -EINVAL ;if (var->policy &PLPKS_WRAPPINGKEY ) -return -EINVAL ; +return -EPERM ; auth =construct_auth (consumer);if (IS_ERR (auth))
Signature · 1 line
2.52.0
PATCH 3/12 pseries/plpks: validate input to plpks_signed_update_var()
In reply to Srish Srinivasanplpks_signed_update_var() dereferences the supplied PLPKS variable pointer without validating it. A NULL argument could therefore result in a NULL pointer dereference.
Reject a NULL pointer argument with -EINVAL.
--- arch/powerpc/platforms/pseries/plpks.c | 3 +++ 1 file changed, 3 insertions(+)
arch/powerpc/platforms/pseries/plpks.c +3 -0
@@ -637,6 +637,9 @@ int plpks_signed_update_var(struct plpks_var *var, u64 flags)u64 continuetoken =0 ;u64 timeout =0 ; +if (!var) +return -EINVAL ; +if (!var->data || var->datalen <=0 || var->namelen >PLPKS_MAX_NAME_SIZE )return -EINVAL ;
Signature · 1 line
2.52.0
PATCH 4/12 pseries/plpks: clear sensitive PLPKS buffers
In reply to Srish SrinivasanPLPKS read, remove, and write operations use authentication buffers that contain passwords. The read output buffer may also contain sensitive data. Freeing these buffers with kfree(), without wiping the contents, can leave sensitive data in memory.
Use kfree_sensitive() to wipe the buffers when freeing them.
--- arch/powerpc/platforms/pseries/plpks.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/powerpc/platforms/pseries/plpks.c +4 -4
@@ -754,7 +754,7 @@ int plpks_write_var(struct plpks_var var) rc =pseries_status_to_err (rc);kfree (label); out: -kfree (auth); +kfree_sensitive (auth);return rc; }
@@ -812,7 +812,7 @@ int plpks_remove_var(char *component, u8 varos, struct plpks_var_name vname) rc =pseries_status_to_err (rc);kfree (label); out: -kfree (auth); +kfree_sensitive (auth);return rc; }
@@ -878,11 +878,11 @@ static int plpks_read_var(u8 consumer, struct plpks_var *var) out_copy_policy: var->policy = retbuf[1 ]; out_free_output: -kfree (output); +kfree_sensitive (output); out_free_label:kfree (label); out_free_auth: -kfree (auth); +kfree_sensitive (auth);return rc; }
Signature · 1 line
2.52.0
PATCH 5/12 pseries/plpks: clear sensitive buffers in wrapping operations
In reply to Srish SrinivasanPLPKS wrapping key generation, object wrapping, and object unwrapping use authentication buffers that contain passwords. An object unwrapping failure also frees an output buffer that may contain sensitive data. Freeing these buffers with kfree(), without wiping the contents, can leave sensitive data in memory.
Use kfree_sensitive() to wipe the buffers when freeing them.
--- arch/powerpc/platforms/pseries/plpks.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/powerpc/platforms/pseries/plpks.c +4 -4
@@ -975,7 +975,7 @@ int plpks_gen_wrapping_key(void)kfree (label); out: -kfree (auth); +kfree_sensitive (auth);return rc; }EXPORT_SYMBOL_GPL (plpks_gen_wrapping_key);
@@ -1100,7 +1100,7 @@ int plpks_wrap_object(u8 **input_buf, u32 input_len, u16 wrap_flags, out_free_label:kfree (label); out: -kfree (auth); +kfree_sensitive (auth);return rc; }EXPORT_SYMBOL_GPL (plpks_wrap_object);
@@ -1177,14 +1177,14 @@ int plpks_unwrap_object(u8 **input_buf, u32 input_len, u8 **output_buf,if (rc) {pr_err ("H_PKS_UNWRAP_OBJECT failed. pseries_status=%d, rc=%d" , pseries_status, rc); -kfree (*output_buf); +kfree_sensitive (*output_buf); *output_buf =NULL ; }else { *output_len = retbuf[1 ]; } out: -kfree (auth); +kfree_sensitive (auth);return rc; }EXPORT_SYMBOL_GPL (plpks_unwrap_object);
Signature · 1 line
2.52.0
PATCH 6/12 pseries/plpks: clear signed update authentication buffer
In reply to Srish SrinivasanThe PLPKS signed update operation uses an authentication buffer that contains a password. Freeing this buffer with kfree(), without wiping the contents, can leave sensitive data in memory.
Use kfree_sensitive() to wipe the buffer when freeing it.
--- arch/powerpc/platforms/pseries/plpks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/platforms/pseries/plpks.c +1 -1
@@ -685,7 +685,7 @@ int plpks_signed_update_var(struct plpks_var *var, u64 flags)kfree (label); out: -kfree (auth); +kfree_sensitive (auth);return rc; }
Signature · 1 line
2.52.0
PATCH 7/12 pseries/plpks: clear SED key data after use
In reply to Srish SrinivasanExplicitly clear the temporary buffer containing SED key data after read and write operations.
--- arch/powerpc/platforms/pseries/plpks_sed_ops.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
arch/powerpc/platforms/pseries/plpks_sed_ops.c +9 -3
@@ -84,14 +84,16 @@ int sed_read_key(char *keyname, char *key, u_int *keylen) ret =plpks_read_os_var (&var);if (ret !=0 ) -return ret; + goto out; len =min_t (u16,be32_to_cpu (data.key_len), var.datalen);memcpy (key, data.key, len); key[len] ='\0' ; *keylen = len; -return 0 ; +out: +memzero_explicit (&data,sizeof (data)); +return ret; }/*
@@ -102,6 +104,7 @@ int sed_write_key(char *keyname, char *key, u_int keylen)struct plpks_var var;struct plpks_sed_object_data data;struct plpks_var_name vname; +int ret;plpks_init_var (&var, keyname);
@@ -127,5 +130,8 @@ int sed_write_key(char *keyname, char *key, u_int keylen) vname.name = var.name;plpks_remove_var (var.component, var.os, vname); -return plpks_write_var (var); + ret =plpks_write_var (var); +memzero_explicit (&data,sizeof (data)); + +return ret; }
Signature · 1 line
2.52.0
PATCH 8/12 pseries/plpks: fix self-reference in plpks_var initializer
In reply to Srish SrinivasanIn plpks_gen_wrapping_key() and plpks_wrap_object(), strlen(var.name) is used to initialize var.namelen within the same struct initializer. This references a member of var before initialization of var is complete.
Use the compile-time length of PLPKS_WRAPKEY_NAME instead.
--- arch/powerpc/platforms/pseries/plpks.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/platforms/pseries/plpks.c +2 -2
@@ -939,7 +939,7 @@ int plpks_gen_wrapping_key(void)int rc =0 , pseries_status =0 ;struct plpks_var var = { .name =PLPKS_WRAPKEY_NAME , - .namelen =strlen (var.name), + .namelen =sizeof (PLPKS_WRAPKEY_NAME ) -1 , .policy =PLPKS_WRAPPINGKEY , .os =PLPKS_VAR_LINUX , .component =PLPKS_WRAPKEY_COMPONENT
@@ -1034,7 +1034,7 @@ int plpks_wrap_object(u8 **input_buf, u32 input_len, u16 wrap_flags,bool sb_enforce_bit = wrap_flags &BIT (1 );struct plpks_var var = { .name =PLPKS_WRAPKEY_NAME , - .namelen =strlen (var.name), + .namelen =sizeof (PLPKS_WRAPKEY_NAME ) -1 , .os =PLPKS_VAR_LINUX , .component =PLPKS_WRAPKEY_COMPONENT };
Signature · 1 line
2.52.0
PATCH 9/12 pseries/plpks: hide wrapping_features when unsupported
In reply to Srish SrinivasanThe PLPKS wrapping features config is only valid on systems that support the "Key Wrapping" feature. Currently the config is always exposed, even when the feature is unavailable.
Add an is_visible() callback to suppress the wrapping_features sysfs attribute when the "Key Wrapping" feature is not supported. Declare the attribute group static const, as it is not modified after initialization.
--- arch/powerpc/platforms/pseries/plpks-sysfs.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
arch/powerpc/platforms/pseries/plpks-sysfs.c +15 -5
@@ -45,6 +45,16 @@ static const struct attribute *config_attrs[] = {static struct kobject *plpks_kobj, *plpks_config_kobj; +static umode_t plpks_config_attr_is_visible (struct kobject *kobj, +struct attribute *attr,int n) +{ +if (attr == &attr_wrapping_features.attr && + !plpks_wrapping_is_supported ()) +return 0 ; + +return attr->mode; +} +int plpks_config_create_softlink (struct kobject *from) {if (!plpks_config_kobj)
@@ -52,13 +62,13 @@ int plpks_config_create_softlink(struct kobject *from)return sysfs_create_link (from, plpks_config_kobj,"config" ); } +static const struct attribute_group config_group = { + .attrs = (struct attribute **)config_attrs, + .is_visible = plpks_config_attr_is_visible, +}; +static __init int plpks_sysfs_config (struct kobject *kobj) { -struct attribute_group config_group = { - .name =NULL , - .attrs = (struct attribute **)config_attrs, - }; -return sysfs_create_group (kobj, &config_group); }
Signature · 1 line
2.52.0
PATCH 10/12 pseries/plpks: make narrowing conversions explicit
In reply to Srish SrinivasanAdd explicit casts when assigning values from the hcall return buffers to narrower fields to make the intended conversions clear.
--- arch/powerpc/platforms/pseries/plpks.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/powerpc/platforms/pseries/plpks.c +3 -3
@@ -576,7 +576,7 @@ static int plpks_confirm_object_flushed(struct label *label,virt_to_phys (auth),virt_to_phys (label), label->size); - status = retbuf[0 ]; + status = (u8 )retbuf[0 ];if (rc) { timed_out = false;if (rc ==H_NOT_FOUND && status ==1 )
@@ -870,13 +870,13 @@ static int plpks_read_var(u8 consumer, struct plpks_var *var) }if (!var->data || var->datalen > retbuf[0 ]) - var->datalen = retbuf[0 ]; + var->datalen = (u16 )retbuf[0 ];if (var->data)memcpy (var->data, output, var->datalen); out_copy_policy: - var->policy = retbuf[1 ]; + var->policy = (u32 )retbuf[1 ]; out_free_output:kfree_sensitive (output); out_free_label:
Signature · 1 line
2.52.0
PATCH 11/12 pseries/plpks: rename the default wrapping key macro
In reply to Srish SrinivasanRename PLPKS_WRAPKEY_NAME to PLPKS_DEFAULT_WRAPKEY_LABEL to clarify that the macro represents the default wrapping key label used by PKWM.
--- arch/powerpc/platforms/pseries/plpks.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
arch/powerpc/platforms/pseries/plpks.c +5 -5
@@ -10,7 +10,7 @@#define pr_fmt (fmt) "plpks: " fmt#define PLPKS_WRAPKEY_COMPONENT "PLPKSWR" -#define PLPKS_WRAPKEY_NAME "default-wrapping-key" +#define PLPKS_DEFAULT_WRAPKEY_LABEL "default-wrapping-key"/* * To 4K align the {input, output} buffers to the {UN}WRAP H_CALLs
@@ -938,8 +938,8 @@ int plpks_gen_wrapping_key(void)struct label *label;int rc =0 , pseries_status =0 ;struct plpks_var var = { - .name =PLPKS_WRAPKEY_NAME , - .namelen =sizeof (PLPKS_WRAPKEY_NAME ) -1 , + .name =PLPKS_DEFAULT_WRAPKEY_LABEL , + .namelen =sizeof (PLPKS_DEFAULT_WRAPKEY_LABEL ) -1 , .policy =PLPKS_WRAPPINGKEY , .os =PLPKS_VAR_LINUX , .component =PLPKS_WRAPKEY_COMPONENT
@@ -1033,8 +1033,8 @@ int plpks_wrap_object(u8 **input_buf, u32 input_len, u16 wrap_flags,bool sb_audit_or_enforce_bit = wrap_flags &BIT (0 );bool sb_enforce_bit = wrap_flags &BIT (1 );struct plpks_var var = { - .name =PLPKS_WRAPKEY_NAME , - .namelen =sizeof (PLPKS_WRAPKEY_NAME ) -1 , + .name =PLPKS_DEFAULT_WRAPKEY_LABEL , + .namelen =sizeof (PLPKS_DEFAULT_WRAPKEY_LABEL ) -1 , .os =PLPKS_VAR_LINUX , .component =PLPKS_WRAPKEY_COMPONENT };
Signature · 1 line
2.52.0
PATCH 12/12 pseries/plpks: update PKS documentation and MAINTAINERS entry
In reply to Srish SrinivasanWrap long PKS hcall return-value lines consistently and fix a typo in the H_PKS_UNWRAP_OBJECT description.
Also update the MAINTAINERS entry from KEYS-TRUSTED-PLPKS to KEYS-TRUSTED-PKWM to reflect the PowerVM Key Wrapping Module naming.
--- Documentation/arch/powerpc/papr_hcalls.rst | 16 ++++++++-------- MAINTAINERS | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-)
Documentation/arch/powerpc/papr_hcalls.rst +8 -8
@@ -305,8 +305,8 @@ like core instruction, core LLAT and nest. | Input: authorization, objectlabel, objectlabellen, policy, out, outlen | Out: *Hypervisor Generated Key, or None when the wrapping key policy is set* | Return Value: *H_SUCCESS, H_Function, H_State, H_R_State, H_Parameter, H_P2, - H_P3, H_P4, H_P5, H_P6, H_Authority, H_Nomem, H_Busy, H_Resource, - H_Aborted* + H_P3, H_P4, H_P5, H_P6, H_Authority, H_Nomem, H_Busy, + H_Resource, H_Aborted* H_PKS_GEN_KEY is used to have the hypervisor generate a new random key. This key is stored as an object in the Power LPAR Platform KeyStore with
@@ -321,8 +321,8 @@ the user. Generation of wrapping keys is supported only for a key size of | inlen, out, outlen, continue-token | Out: *continue-token, byte size of wrapped object, wrapped object* | Return Value: *H_SUCCESS, H_Function, H_State, H_R_State, H_Parameter, H_P2, - H_P3, H_P4, H_P5, H_P6, H_P7, H_P8, H_P9, H_Authority, H_Invalid_Key, - H_NOT_FOUND, H_Busy, H_LongBusy, H_Aborted* + H_P3, H_P4, H_P5, H_P6, H_P7, H_P8, H_P9, H_Authority, + H_Invalid_Key, H_NOT_FOUND, H_Busy, H_LongBusy, H_Aborted* H_PKS_WRAP_OBJECT is used to wrap an object using a wrapping key stored in the Power LPAR Platform KeyStore and return the wrapped object to the caller. The
@@ -331,16 +331,16 @@ which must have been previously created with H_PKS_GEN_KEY. The provided object is then encrypted with the wrapping key and additional metadata and the result is returned to the caller. - **H_PKS_UNWRAP_OBJECT** | Input: authorization, objectwrapflags, in, inlen, out, outlen, continue-token | Out: *continue-token, byte size of unwrapped object, unwrapped object* | Return Value: *H_SUCCESS, H_Function, H_State, H_R_State, H_Parameter, H_P2, - H_P3, H_P4, H_P5, H_P6, H_P7, H_Authority, H_Unsupported, H_Bad_Data, - H_NOT_FOUND, H_Invalid_Key, H_Busy, H_LongBusy, H_Aborted* + H_P3, H_P4, H_P5, H_P6, H_P7, H_Authority, H_Unsupported, + H_Bad_Data, H_NOT_FOUND, H_Invalid_Key, H_Busy, H_LongBusy, + H_Aborted* -H_PKS_UNWRAP_OBJECT is used to unwrap an object that was previously warapped with +H_PKS_UNWRAP_OBJECT is used to unwrap an object that was previously wrapped with H_PKS_WRAP_OBJECT. References
MAINTAINERS +1 -1
@@ -14485,7 +14485,7 @@ S: Supported F: include/keys/trusted_dcp.h F: security/keys/trusted-keys/trusted_dcp.c -KEYS-TRUSTED-PLPKS +KEYS-TRUSTED-PKWM M: Srish Srinivasan <[email protected]> M: Nayna Jain <[email protected]> L: [email protected]
Signature · 1 line
2.52.0
Quoted 20 lines · Srish Srinivasan
This series contains fixes, hardening changes, and general cleanup for the PLPKS code. It addresses error handling and input validation issues, improves the cleanup of sensitive data, and prevents the
wrapping_featuressysfs attribute from being exposed when the key-wrapping interface is not supported.Srish Srinivasan (12):
pseries/plpks: fix error handling inplpks_read_var()pseries/plpks: fix error code for wrapping key reads pseries/plpks: validate input toplpks_signed_update_var()pseries/plpks: clear sensitive PLPKS buffers pseries/plpks: clear sensitive buffers in wrapping operations pseries/plpks: clear signed update authentication buffer pseries/plpks: clear SED key data after use pseries/plpks: fix self-reference inplpks_varinitializer pseries/plpks: hidewrapping_featureswhen unsupported pseries/plpks: make narrowing conversions explicit pseries/plpks: rename the default wrapping key macro pseries/plpks: update PKS documentation andMAINTAINERSentry
Quoted 1 line
[...]
Applied to powerpc/next.
[01/12] pseries/plpks: fix error handling in plpks_read_var()
https://git.kernel.org/powerpc/c/d52a1659cb1718f93166ab8dd8621237e564a1c4 [02/12] pseries/plpks: fix error code for wrapping key reads https://git.kernel.org/powerpc/c/0d0eafedcaf4939819d38ede6ad355ea37acd978 [03/12] pseries/plpks: validate input to plpks_signed_update_var() https://git.kernel.org/powerpc/c/b59ff2d50b817eeec1259f357aae821c7834007f [04/12] pseries/plpks: clear sensitive PLPKS buffers https://git.kernel.org/powerpc/c/fe18716ff314eb32299048fd64242d31b4e852cf [05/12] pseries/plpks: clear sensitive buffers in wrapping operations https://git.kernel.org/powerpc/c/203695c109e37d8fe3531844218f75b98eef8e1f [06/12] pseries/plpks: clear signed update authentication buffer https://git.kernel.org/powerpc/c/63de55024e7c85975f4410380323edb05b186d7c [07/12] pseries/plpks: clear SED key data after use https://git.kernel.org/powerpc/c/217ba01e5b9096091acc2c1a3776cb3eb88c00ff [08/12] pseries/plpks: fix self-reference in plpks_var initializer https://git.kernel.org/powerpc/c/abcccecf4aa4b681f50eb7d7de93fc9a0a61c181 [09/12] pseries/plpks: hide wrapping_features when unsupported https://git.kernel.org/powerpc/c/46b8411b2ee948b1786bc4af76e7feede0c2ddff [10/12] pseries/plpks: make narrowing conversions explicit https://git.kernel.org/powerpc/c/e0645be900a856a8ec462b1c47a4977ca19808e7 [11/12] pseries/plpks: rename the default wrapping key macro https://git.kernel.org/powerpc/c/a2df535d9dd9029987fd9339e5fe668d53a459fa [12/12] pseries/plpks: update PKS documentation and MAINTAINERS entry https://git.kernel.org/powerpc/c/0499ab96527ac4c55be381d7417ff542d4ea24f6
cheers