Path: blob/master/arch/powerpc/platforms/pseries/plpks.c
51484 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* POWER LPAR Platform KeyStore(PLPKS)3* Copyright (C) 2022 IBM Corporation4* Author: Nayna Jain <[email protected]>5*6* Provides access to variables stored in Power LPAR Platform KeyStore(PLPKS).7*/89#define pr_fmt(fmt) "plpks: " fmt1011#define PLPKS_WRAPKEY_COMPONENT "PLPKSWR"12#define PLPKS_WRAPKEY_NAME "default-wrapping-key"1314/*15* To 4K align the {input, output} buffers to the {UN}WRAP H_CALLs16*/17#define PLPKS_WRAPPING_BUF_ALIGN 40961819/*20* To ensure the output buffer's length is at least 1024 bytes greater21* than the input buffer's length during the WRAP H_CALL22*/23#define PLPKS_WRAPPING_BUF_DIFF 10242425#define PLPKS_WRAP_INTERFACE_BIT 326#define PLPKS_WRAPPING_KEY_LENGTH 322728#define WRAPFLAG_BE_BIT_SET(be_bit) \29BIT_ULL(63 - (be_bit))3031#define WRAPFLAG_BE_GENMASK(be_bit_hi, be_bit_lo) \32GENMASK_ULL(63 - (be_bit_hi), 63 - (be_bit_lo))3334#define WRAPFLAG_BE_FIELD_PREP(be_bit_hi, be_bit_lo, val) \35FIELD_PREP(WRAPFLAG_BE_GENMASK(be_bit_hi, be_bit_lo), (val))3637#include <linux/delay.h>38#include <linux/errno.h>39#include <linux/io.h>40#include <linux/printk.h>41#include <linux/slab.h>42#include <linux/string.h>43#include <linux/types.h>44#include <linux/of_fdt.h>45#include <linux/libfdt.h>46#include <linux/memblock.h>47#include <linux/bitfield.h>48#include <asm/hvcall.h>49#include <asm/machdep.h>50#include <asm/plpks.h>51#include <asm/firmware.h>5253static u8 *ospassword;54static u16 ospasswordlength;5556// Retrieved with H_PKS_GET_CONFIG57static u8 version;58static u16 objoverhead;59static u16 maxpwsize;60static u16 maxobjsize;61static s16 maxobjlabelsize;62static u32 totalsize;63static u32 usedspace;64static u32 supportedpolicies;65static u32 maxlargeobjectsize;66static u64 signedupdatealgorithms;67static u64 wrappingfeatures;68static bool wrapsupport;6970struct plpks_auth {71u8 version;72u8 consumer;73__be64 rsvd0;74__be32 rsvd1;75__be16 passwordlength;76u8 password[];77} __packed __aligned(16);7879struct label_attr {80u8 prefix[8];81u8 version;82u8 os;83u8 length;84u8 reserved[5];85};8687struct label {88struct label_attr attr;89u8 name[PLPKS_MAX_NAME_SIZE];90size_t size;91};9293static int pseries_status_to_err(int rc)94{95int err;9697switch (rc) {98case H_SUCCESS:99err = 0;100break;101case H_FUNCTION:102err = -ENXIO;103break;104case H_PARAMETER:105case H_P2:106case H_P3:107case H_P4:108case H_P5:109case H_P6:110err = -EINVAL;111break;112case H_NOT_FOUND:113err = -ENOENT;114break;115case H_BUSY:116case H_LONG_BUSY_ORDER_1_MSEC:117case H_LONG_BUSY_ORDER_10_MSEC:118case H_LONG_BUSY_ORDER_100_MSEC:119case H_LONG_BUSY_ORDER_1_SEC:120case H_LONG_BUSY_ORDER_10_SEC:121case H_LONG_BUSY_ORDER_100_SEC:122err = -EBUSY;123break;124case H_AUTHORITY:125err = -EPERM;126break;127case H_NO_MEM:128err = -ENOMEM;129break;130case H_RESOURCE:131err = -EEXIST;132break;133case H_TOO_BIG:134err = -EFBIG;135break;136case H_STATE:137err = -EIO;138break;139case H_R_STATE:140err = -EIO;141break;142case H_IN_USE:143err = -EEXIST;144break;145case H_ABORTED:146err = -EIO;147break;148default:149err = -EINVAL;150}151152pr_debug("Converted hypervisor code %d to Linux %d\n", rc, err);153154return err;155}156157static int plpks_gen_password(void)158{159unsigned long retbuf[PLPAR_HCALL_BUFSIZE] = { 0 };160u8 *password, consumer = PLPKS_OS_OWNER;161int rc;162163// If we booted from kexec, we could be reusing an existing password already164if (ospassword) {165pr_debug("Password of length %u already in use\n", ospasswordlength);166return 0;167}168169// The password must not cross a page boundary, so we align to the next power of 2170password = kzalloc(roundup_pow_of_two(maxpwsize), GFP_KERNEL);171if (!password)172return -ENOMEM;173174rc = plpar_hcall(H_PKS_GEN_PASSWORD, retbuf, consumer, 0,175virt_to_phys(password), maxpwsize);176177if (!rc) {178ospasswordlength = maxpwsize;179ospassword = kzalloc(maxpwsize, GFP_KERNEL);180if (!ospassword) {181kfree_sensitive(password);182return -ENOMEM;183}184memcpy(ospassword, password, ospasswordlength);185} else {186if (rc == H_IN_USE) {187pr_warn("Password already set - authenticated operations will fail\n");188rc = 0;189} else {190goto out;191}192}193out:194kfree_sensitive(password);195196return pseries_status_to_err(rc);197}198199static struct plpks_auth *construct_auth(u8 consumer)200{201struct plpks_auth *auth;202203if (consumer > PLPKS_OS_OWNER)204return ERR_PTR(-EINVAL);205206// The auth structure must not cross a page boundary and must be207// 16 byte aligned. We align to the next largest power of 2208auth = kzalloc(roundup_pow_of_two(struct_size(auth, password, maxpwsize)), GFP_KERNEL);209if (!auth)210return ERR_PTR(-ENOMEM);211212auth->version = 1;213auth->consumer = consumer;214215if (consumer == PLPKS_FW_OWNER || consumer == PLPKS_BOOTLOADER_OWNER)216return auth;217218memcpy(auth->password, ospassword, ospasswordlength);219220auth->passwordlength = cpu_to_be16(ospasswordlength);221222return auth;223}224225/*226* Label is combination of label attributes + name.227* Label attributes are used internally by kernel and not exposed to the user.228*/229static struct label *construct_label(char *component, u8 varos, u8 *name,230u16 namelen)231{232struct label *label;233size_t slen = 0;234235if (!name || namelen > PLPKS_MAX_NAME_SIZE)236return ERR_PTR(-EINVAL);237238// Support NULL component for signed updates239if (component) {240slen = strlen(component);241if (slen > sizeof(label->attr.prefix))242return ERR_PTR(-EINVAL);243}244245// The label structure must not cross a page boundary, so we align to the next power of 2246label = kzalloc(roundup_pow_of_two(sizeof(*label)), GFP_KERNEL);247if (!label)248return ERR_PTR(-ENOMEM);249250if (component)251memcpy(&label->attr.prefix, component, slen);252253label->attr.version = PLPKS_LABEL_VERSION;254label->attr.os = varos;255label->attr.length = PLPKS_MAX_LABEL_ATTR_SIZE;256memcpy(&label->name, name, namelen);257258label->size = sizeof(struct label_attr) + namelen;259260return label;261}262263static int _plpks_get_config(void)264{265unsigned long retbuf[PLPAR_HCALL_BUFSIZE] = { 0 };266struct config {267u8 version;268u8 flags;269__be16 rsvd0;270__be16 objoverhead;271__be16 maxpwsize;272__be16 maxobjlabelsize;273__be16 maxobjsize;274__be32 totalsize;275__be32 usedspace;276__be32 supportedpolicies;277__be32 maxlargeobjectsize;278__be64 signedupdatealgorithms;279__be64 wrappingfeatures;280u8 rsvd1[476];281} __packed * config;282size_t size;283int rc = 0;284285size = sizeof(*config);286287// Config struct must not cross a page boundary. So long as the struct288// size is a power of 2, this should be fine as alignment is guaranteed289config = kzalloc(size, GFP_KERNEL);290if (!config) {291rc = -ENOMEM;292goto err;293}294295rc = plpar_hcall(H_PKS_GET_CONFIG, retbuf, virt_to_phys(config), size);296297if (rc != H_SUCCESS) {298rc = pseries_status_to_err(rc);299goto err;300}301302version = config->version;303objoverhead = be16_to_cpu(config->objoverhead);304maxpwsize = be16_to_cpu(config->maxpwsize);305maxobjsize = be16_to_cpu(config->maxobjsize);306maxobjlabelsize = be16_to_cpu(config->maxobjlabelsize);307totalsize = be32_to_cpu(config->totalsize);308usedspace = be32_to_cpu(config->usedspace);309supportedpolicies = be32_to_cpu(config->supportedpolicies);310maxlargeobjectsize = be32_to_cpu(config->maxlargeobjectsize);311signedupdatealgorithms = be64_to_cpu(config->signedupdatealgorithms);312wrappingfeatures = be64_to_cpu(config->wrappingfeatures);313wrapsupport = config->flags & PPC_BIT8(PLPKS_WRAP_INTERFACE_BIT);314315// Validate that the numbers we get back match the requirements of the spec316if (maxpwsize < 32) {317pr_err("Invalid Max Password Size received from hypervisor (%d < 32)\n", maxpwsize);318rc = -EIO;319goto err;320}321322if (maxobjlabelsize < 255) {323pr_err("Invalid Max Object Label Size received from hypervisor (%d < 255)\n",324maxobjlabelsize);325rc = -EIO;326goto err;327}328329if (totalsize < 4096) {330pr_err("Invalid Total Size received from hypervisor (%d < 4096)\n", totalsize);331rc = -EIO;332goto err;333}334335if (version >= 3 && maxlargeobjectsize >= 65536 && maxobjsize != 0xFFFF) {336pr_err("Invalid Max Object Size (0x%x != 0xFFFF)\n", maxobjsize);337rc = -EIO;338goto err;339}340341err:342kfree(config);343return rc;344}345346/**347* plpks_get_version() - Get the version of the PLPKS config structure.348*349* Successful execution of the H_PKS_GET_CONFIG HCALL during initialization350* reads the PLPKS config structure version and saves it in a file local static351* version variable.352*353* Returns: On success the saved PLPKS config structure version is returned, 0354* if not.355*/356u8 plpks_get_version(void)357{358return version;359}360361/**362* plpks_get_objoverhead() - Get the hypervisor storage overhead per object.363*364* Successful execution of the H_PKS_GET_CONFIG HCALL during initialization365* reads the per object hypervisor storage overhead in bytes into the local366* static objoverhead variable, excluding the size of the object or the label.367* This value can be treated as valid only when the PLPKS config structure368* version >= 2.369*370* Returns: If PLPKS config structure version >= 2 then the storage overhead is371* returned, 0 otherwise.372*/373u16 plpks_get_objoverhead(void)374{375return objoverhead;376}377378/**379* plpks_get_maxpwsize() - Get the maximum password size.380*381* Successful execution of the H_PKS_GET_CONFIG HCALL during initialization382* reads the maximum password size and checks if it is 32 bytes at the least383* before storing it in the local static maxpwsize variable.384*385* Returns: On success the maximum password size is returned, 0 if not.386*/387u16 plpks_get_maxpwsize(void)388{389return maxpwsize;390}391392/**393* plpks_get_maxobjectsize() - Get the maximum object size supported by the394* PLPKS.395*396* Successful execution of the H_PKS_GET_CONFIG HCALL during initialization397* reads the maximum object size into the file local static maxobjsize variable.398*399* Returns: On success the maximum object size is returned, 0 if not.400*/401u16 plpks_get_maxobjectsize(void)402{403return maxobjsize;404}405406/**407* plpks_get_maxobjectlabelsize() - Get the maximum object label size supported408* by the PLPKS.409*410* Successful execution of the H_PKS_GET_CONFIG HCALL during initialization411* reads the maximum object label size into the local static maxobjlabelsize412* variable.413*414* Returns: On success the maximum object label size is returned, 0 if not.415*/416u16 plpks_get_maxobjectlabelsize(void)417{418return maxobjlabelsize;419}420421/**422* plpks_get_totalsize() - Get the total size of the PLPKS that is configured.423*424* Successful execution of the H_PKS_GET_CONFIG HCALL during initialization425* reads the total size of the PLPKS that is configured for the LPAR into the426* file local static totalsize variable.427*428* Returns: On success the total size of the PLPKS configured is returned, 0 if429* not.430*/431u32 plpks_get_totalsize(void)432{433return totalsize;434}435436/**437* plpks_get_usedspace() - Get the used space from the total size of the PLPKS.438*439* Invoke the H_PKS_GET_CONFIG HCALL to refresh the latest value for the used440* space as this keeps changing with the creation and removal of objects in the441* PLPKS.442*443* Returns: On success the used space is returned, 0 if not.444*/445u32 plpks_get_usedspace(void)446{447int rc = _plpks_get_config();448if (rc) {449pr_err("Couldn't get config, rc: %d\n", rc);450return 0;451}452return usedspace;453}454455/**456* plpks_get_supportedpolicies() - Get a bitmask of the policies supported by457* the hypervisor.458*459* Successful execution of the H_PKS_GET_CONFIG HCALL during initialization460* reads a bitmask of the policies supported by the hypervisor into the file461* local static supportedpolicies variable.462*463* Returns: On success the bitmask of the policies supported by the hypervisor464* are returned, 0 if not.465*/466u32 plpks_get_supportedpolicies(void)467{468return supportedpolicies;469}470471/**472* plpks_get_maxlargeobjectsize() - Get the maximum object size supported for473* PLPKS config structure version >= 3474*475* Successful execution of the H_PKS_GET_CONFIG HCALL during initialization476* reads the maximum object size into the local static maxlargeobjectsize477* variable for PLPKS config structure version >= 3. This was introduced478* starting with PLPKS config structure version 3 to allow for objects of479* size >= 64K.480*481* Returns: If PLPKS config structure version >= 3 then the new maximum object482* size is returned, 0 if not.483*/484u32 plpks_get_maxlargeobjectsize(void)485{486return maxlargeobjectsize;487}488489/**490* plpks_get_signedupdatealgorithms() - Get a bitmask of the signature491* algorithms supported for signed updates.492*493* Successful execution of the H_PKS_GET_CONFIG HCALL during initialization494* reads a bitmask of the signature algorithms supported for signed updates into495* the file local static signedupdatealgorithms variable. This is valid only496* when the PLPKS config structure version >= 3.497*498* Returns: On success the bitmask of the signature algorithms supported for499* signed updates is returned, 0 if not.500*/501u64 plpks_get_signedupdatealgorithms(void)502{503return signedupdatealgorithms;504}505506/**507* plpks_get_wrappingfeatures() - Returns a bitmask of the wrapping features508* supported by the hypervisor.509*510* Successful execution of the H_PKS_GET_CONFIG HCALL during initialization511* reads a bitmask of the wrapping features supported by the hypervisor into the512* file local static wrappingfeatures variable. This is valid only when the513* PLPKS config structure version >= 3.514*515* Return:516* bitmask of the wrapping features supported by the hypervisor517*/518u64 plpks_get_wrappingfeatures(void)519{520return wrappingfeatures;521}522523/**524* plpks_get_passwordlen() - Get the length of the PLPKS password in bytes.525*526* The H_PKS_GEN_PASSWORD HCALL makes the hypervisor generate a random password527* for the specified consumer, apply that password to the PLPKS and return it to528* the caller. In this process, the password length for the OS consumer is529* stored in the local static ospasswordlength variable.530*531* Returns: On success the password length for the OS consumer in bytes is532* returned, 0 if not.533*/534u16 plpks_get_passwordlen(void)535{536return ospasswordlength;537}538539/**540* plpks_is_available() - Get the PLPKS availability status for the LPAR.541*542* The availability of PLPKS is inferred based upon the successful execution of543* the H_PKS_GET_CONFIG HCALL provided the firmware supports this feature. The544* H_PKS_GET_CONFIG HCALL reads the configuration and status information related545* to the PLPKS. The configuration structure provides a version number to inform546* the caller of the supported features.547*548* Returns: true is returned if PLPKS is available, false if not.549*/550bool plpks_is_available(void)551{552int rc;553554if (!firmware_has_feature(FW_FEATURE_PLPKS))555return false;556557rc = _plpks_get_config();558if (rc)559return false;560561return true;562}563564static int plpks_confirm_object_flushed(struct label *label,565struct plpks_auth *auth)566{567unsigned long retbuf[PLPAR_HCALL_BUFSIZE] = { 0 };568bool timed_out = true;569u64 timeout = 0;570u8 status;571int rc;572573do {574rc = plpar_hcall(H_PKS_CONFIRM_OBJECT_FLUSHED, retbuf,575virt_to_phys(auth), virt_to_phys(label),576label->size);577578status = retbuf[0];579if (rc) {580timed_out = false;581if (rc == H_NOT_FOUND && status == 1)582rc = 0;583break;584}585586if (!rc && status == 1) {587timed_out = false;588break;589}590591fsleep(PLPKS_FLUSH_SLEEP);592timeout = timeout + PLPKS_FLUSH_SLEEP;593} while (timeout < PLPKS_MAX_TIMEOUT);594595if (timed_out)596return -ETIMEDOUT;597598return pseries_status_to_err(rc);599}600601/**602* plpks_signed_update_var() - Update the specified authenticated variable.603* @var: authenticated variable to be updated604* @flags: signed update request operation flags605*606* The H_PKS_SIGNED_UPDATE HCALL performs a signed update to an object in the607* PLPKS. The object must have the signed update policy flag set.608*609* Possible reasons for the returned errno values:610*611* -ENXIO if PLPKS is not supported612* -EIO if PLPKS access is blocked due to the LPAR's state613* if PLPKS modification is blocked due to the LPAR's state614* if an error occurred while processing the request615* -EINVAL if invalid authorization parameter616* if invalid object label parameter617* if invalid object label len parameter618* if invalid or unsupported policy declaration619* if invalid signed update flags620* if invalid input data parameter621* if invalid input data len parameter622* if invalid continue token parameter623* -EPERM if access is denied624* -ENOMEM if there is inadequate memory to perform the operation625* -EBUSY if unable to handle the request or long running operation626* initiated, retry later627*628* Returns: On success 0 is returned, a negative errno if not.629*/630int plpks_signed_update_var(struct plpks_var *var, u64 flags)631{632unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};633int rc;634struct label *label;635struct plpks_auth *auth;636u64 continuetoken = 0;637u64 timeout = 0;638639if (!var->data || var->datalen <= 0 || var->namelen > PLPKS_MAX_NAME_SIZE)640return -EINVAL;641642if (!(var->policy & PLPKS_SIGNEDUPDATE))643return -EINVAL;644645if (var->policy & PLPKS_WRAPPINGKEY)646return -EINVAL;647648// Signed updates need the component to be NULL.649if (var->component)650return -EINVAL;651652auth = construct_auth(PLPKS_OS_OWNER);653if (IS_ERR(auth))654return PTR_ERR(auth);655656label = construct_label(var->component, var->os, var->name, var->namelen);657if (IS_ERR(label)) {658rc = PTR_ERR(label);659goto out;660}661662do {663rc = plpar_hcall9(H_PKS_SIGNED_UPDATE, retbuf,664virt_to_phys(auth), virt_to_phys(label),665label->size, var->policy, flags,666virt_to_phys(var->data), var->datalen,667continuetoken);668669continuetoken = retbuf[0];670if (pseries_status_to_err(rc) == -EBUSY) {671int delay_us = get_longbusy_msecs(rc) * 1000;672673fsleep(delay_us);674timeout += delay_us;675}676rc = pseries_status_to_err(rc);677} while (rc == -EBUSY && timeout < PLPKS_MAX_TIMEOUT);678679if (!rc)680rc = plpks_confirm_object_flushed(label, auth);681682kfree(label);683out:684kfree(auth);685686return rc;687}688689/**690* plpks_write_var() - Write the specified variable and its data to PLPKS.691* @var: variable to be written into the PLPKS692*693* The H_PKS_WRITE_OBJECT HCALL writes an object into the PLPKS. The caller must694* provide a valid component type for the variable, and the signed update policy695* flag must not be set.696*697* Possible reasons for the returned errno values:698*699* -ENXIO if PLPKS is not supported700* -EIO if PLPKS access is blocked due to the LPAR's state701* if PLPKS modification is blocked due to the LPAR's state702* if an error occurred while processing the request703* -EINVAL if invalid authorization parameter704* if invalid object label parameter705* if invalid object label len parameter706* if invalid or unsupported policy declaration707* if invalid input data parameter708* if invalid input data len parameter709* -EPERM if access is denied710* -ENOMEM if unable to store the requested object in the space available711* -EBUSY if unable to handle the request712* -EEXIST if the object label already exists713*714* Returns: On success 0 is returned, a negative errno if not.715*/716int plpks_write_var(struct plpks_var var)717{718unsigned long retbuf[PLPAR_HCALL_BUFSIZE] = { 0 };719struct plpks_auth *auth;720struct label *label;721int rc;722723if (!var.component || !var.data || var.datalen <= 0 ||724var.namelen > PLPKS_MAX_NAME_SIZE || var.datalen > PLPKS_MAX_DATA_SIZE)725return -EINVAL;726727if (var.policy & PLPKS_SIGNEDUPDATE)728return -EINVAL;729730if (var.policy & PLPKS_WRAPPINGKEY)731return -EINVAL;732733auth = construct_auth(PLPKS_OS_OWNER);734if (IS_ERR(auth))735return PTR_ERR(auth);736737label = construct_label(var.component, var.os, var.name, var.namelen);738if (IS_ERR(label)) {739rc = PTR_ERR(label);740goto out;741}742743rc = plpar_hcall(H_PKS_WRITE_OBJECT, retbuf, virt_to_phys(auth),744virt_to_phys(label), label->size, var.policy,745virt_to_phys(var.data), var.datalen);746747if (!rc)748rc = plpks_confirm_object_flushed(label, auth);749750rc = pseries_status_to_err(rc);751kfree(label);752out:753kfree(auth);754755return rc;756}757758/**759* plpks_remove_var() - Remove the specified variable and its data from PLPKS.760* @component: metadata prefix in the object label metadata structure761* @varos: metadata OS flags in the object label metadata structure762* @vname: object label for the object that needs to be removed763*764* The H_PKS_REMOVE_OBJECT HCALL removes an object from the PLPKS. The removal765* is independent of the policy bits that are set.766*767* Possible reasons for the returned errno values:768*769* -ENXIO if PLPKS is not supported770* -EIO if PLPKS access is blocked due to the LPAR's state771* if PLPKS modification is blocked due to the LPAR's state772* if an error occurred while processing the request773* -EINVAL if invalid authorization parameter774* if invalid object label parameter775* if invalid object label len parameter776* -EPERM if access is denied777* -ENOENT if the requested object was not found778* -EBUSY if unable to handle the request779*780* Returns: On success 0 is returned, a negative errno if not.781*/782int plpks_remove_var(char *component, u8 varos, struct plpks_var_name vname)783{784unsigned long retbuf[PLPAR_HCALL_BUFSIZE] = { 0 };785struct plpks_auth *auth;786struct label *label;787int rc;788789if (vname.namelen > PLPKS_MAX_NAME_SIZE)790return -EINVAL;791792auth = construct_auth(PLPKS_OS_OWNER);793if (IS_ERR(auth))794return PTR_ERR(auth);795796label = construct_label(component, varos, vname.name, vname.namelen);797if (IS_ERR(label)) {798rc = PTR_ERR(label);799goto out;800}801802rc = plpar_hcall(H_PKS_REMOVE_OBJECT, retbuf, virt_to_phys(auth),803virt_to_phys(label), label->size);804805if (!rc)806rc = plpks_confirm_object_flushed(label, auth);807808rc = pseries_status_to_err(rc);809kfree(label);810out:811kfree(auth);812813return rc;814}815816static int plpks_read_var(u8 consumer, struct plpks_var *var)817{818unsigned long retbuf[PLPAR_HCALL_BUFSIZE] = { 0 };819struct plpks_auth *auth;820struct label *label = NULL;821u8 *output;822int rc;823824if (var->namelen > PLPKS_MAX_NAME_SIZE)825return -EINVAL;826827if (var->policy & PLPKS_WRAPPINGKEY)828return -EINVAL;829830auth = construct_auth(consumer);831if (IS_ERR(auth))832return PTR_ERR(auth);833834if (consumer == PLPKS_OS_OWNER) {835label = construct_label(var->component, var->os, var->name,836var->namelen);837if (IS_ERR(label)) {838rc = PTR_ERR(label);839goto out_free_auth;840}841}842843output = kzalloc(maxobjsize, GFP_KERNEL);844if (!output) {845rc = -ENOMEM;846goto out_free_label;847}848849if (consumer == PLPKS_OS_OWNER)850rc = plpar_hcall(H_PKS_READ_OBJECT, retbuf, virt_to_phys(auth),851virt_to_phys(label), label->size, virt_to_phys(output),852maxobjsize);853else854rc = plpar_hcall(H_PKS_READ_OBJECT, retbuf, virt_to_phys(auth),855virt_to_phys(var->name), var->namelen, virt_to_phys(output),856maxobjsize);857858859if (rc != H_SUCCESS) {860rc = pseries_status_to_err(rc);861goto out_free_output;862}863864if (!var->data || var->datalen > retbuf[0])865var->datalen = retbuf[0];866867var->policy = retbuf[1];868869if (var->data)870memcpy(var->data, output, var->datalen);871872rc = 0;873874out_free_output:875kfree(output);876out_free_label:877kfree(label);878out_free_auth:879kfree(auth);880881return rc;882}883884/**885* plpks_wrapping_is_supported() - Get the H_PKS_WRAP_OBJECT interface886* availability status for the LPAR.887*888* Successful execution of the H_PKS_GET_CONFIG HCALL during initialization889* sets bit 3 of the flags variable in the PLPKS config structure if the890* H_PKS_WRAP_OBJECT interface is supported.891*892* Returns: true if the H_PKS_WRAP_OBJECT interface is supported, false if not.893*/894bool plpks_wrapping_is_supported(void)895{896return wrapsupport;897}898EXPORT_SYMBOL_GPL(plpks_wrapping_is_supported);899900/**901* plpks_gen_wrapping_key() - Generate a new random key with the 'wrapping key'902* policy set.903*904* The H_PKS_GEN_KEY HCALL makes the hypervisor generate a new random key and905* store the key in a PLPKS object with the provided object label. With the906* 'wrapping key' policy set, only the label to the newly generated random key907* would be visible to the user.908*909* Possible reasons for the returned errno values:910*911* -ENXIO if PLPKS is not supported912* -EIO if PLPKS access is blocked due to the LPAR's state913* if PLPKS modification is blocked due to the LPAR's state914* if an error occurred while processing the request915* -EINVAL if invalid authorization parameter916* if invalid object label parameter917* if invalid object label len parameter918* if invalid or unsupported policy declaration919* if invalid output buffer parameter920* if invalid output buffer length parameter921* -EPERM if access is denied922* -ENOMEM if there is inadequate memory to perform this operation923* -EBUSY if unable to handle the request924* -EEXIST if the object label already exists925*926* Returns: On success 0 is returned, a negative errno if not.927*/928int plpks_gen_wrapping_key(void)929{930unsigned long retbuf[PLPAR_HCALL_BUFSIZE] = { 0 };931struct plpks_auth *auth;932struct label *label;933int rc = 0, pseries_status = 0;934struct plpks_var var = {935.name = PLPKS_WRAPKEY_NAME,936.namelen = strlen(var.name),937.policy = PLPKS_WRAPPINGKEY,938.os = PLPKS_VAR_LINUX,939.component = PLPKS_WRAPKEY_COMPONENT940};941942auth = construct_auth(PLPKS_OS_OWNER);943if (IS_ERR(auth))944return PTR_ERR(auth);945946label = construct_label(var.component, var.os, var.name, var.namelen);947if (IS_ERR(label)) {948rc = PTR_ERR(label);949goto out;950}951952rc = plpar_hcall(H_PKS_GEN_KEY, retbuf,953virt_to_phys(auth), virt_to_phys(label),954label->size, var.policy,955NULL, PLPKS_WRAPPING_KEY_LENGTH);956957if (!rc)958rc = plpks_confirm_object_flushed(label, auth);959960pseries_status = rc;961rc = pseries_status_to_err(rc);962963if (rc && rc != -EEXIST) {964pr_err("H_PKS_GEN_KEY failed. pseries_status=%d, rc=%d",965pseries_status, rc);966} else {967rc = 0;968}969970kfree(label);971out:972kfree(auth);973return rc;974}975EXPORT_SYMBOL_GPL(plpks_gen_wrapping_key);976977/**978* plpks_wrap_object() - Wrap an object using the default wrapping key stored in979* the PLPKS.980* @input_buf: buffer containing the data to be wrapped981* @input_len: length of the input buffer982* @wrap_flags: object wrapping flags983* @output_buf: buffer to store the wrapped data984* @output_len: length of the output buffer985*986* The H_PKS_WRAP_OBJECT HCALL wraps an object using a wrapping key stored in987* the PLPKS and returns the wrapped object to the caller. The caller provides a988* label to the wrapping key with the 'wrapping key' policy set that must have989* been previously created with the H_PKS_GEN_KEY HCALL. The provided object is990* then encrypted with the wrapping key and additional metadata and the result991* is returned to the user. The metadata includes the wrapping algorithm and the992* wrapping key name so those parameters are not required during unwrap.993*994* Possible reasons for the returned errno values:995*996* -ENXIO if PLPKS is not supported997* -EIO if PLPKS access is blocked due to the LPAR's state998* if PLPKS modification is blocked due to the LPAR's state999* if an error occurred while processing the request1000* -EINVAL if invalid authorization parameter1001* if invalid wrapping key label parameter1002* if invalid wrapping key label length parameter1003* if invalid or unsupported object wrapping flags1004* if invalid input buffer parameter1005* if invalid input buffer length parameter1006* if invalid output buffer parameter1007* if invalid output buffer length parameter1008* if invalid continue token parameter1009* if the wrapping key is not compatible with the wrapping1010* algorithm1011* -EPERM if access is denied1012* -ENOENT if the requested wrapping key was not found1013* -EBUSY if unable to handle the request or long running operation1014* initiated, retry later.1015*1016* Returns: On success 0 is returned, a negative errno if not.1017*/1018int plpks_wrap_object(u8 **input_buf, u32 input_len, u16 wrap_flags,1019u8 **output_buf, u32 *output_len)1020{1021unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = { 0 };1022struct plpks_auth *auth;1023struct label *label;1024u64 continuetoken = 0;1025u64 objwrapflags = 0;1026int rc = 0, pseries_status = 0;1027bool sb_audit_or_enforce_bit = wrap_flags & BIT(0);1028bool sb_enforce_bit = wrap_flags & BIT(1);1029struct plpks_var var = {1030.name = PLPKS_WRAPKEY_NAME,1031.namelen = strlen(var.name),1032.os = PLPKS_VAR_LINUX,1033.component = PLPKS_WRAPKEY_COMPONENT1034};10351036auth = construct_auth(PLPKS_OS_OWNER);1037if (IS_ERR(auth))1038return PTR_ERR(auth);10391040label = construct_label(var.component, var.os, var.name, var.namelen);1041if (IS_ERR(label)) {1042rc = PTR_ERR(label);1043goto out;1044}10451046/* Set the consumer password requirement bit. A must have. */1047objwrapflags |= WRAPFLAG_BE_BIT_SET(3);10481049/* Set the wrapping algorithm bit. Just one algorithm option for now */1050objwrapflags |= WRAPFLAG_BE_FIELD_PREP(60, 63, 0x1);10511052if (sb_audit_or_enforce_bit & sb_enforce_bit) {1053pr_err("Cannot set both audit/enforce and enforce bits.");1054rc = -EINVAL;1055goto out_free_label;1056} else if (sb_audit_or_enforce_bit) {1057objwrapflags |= WRAPFLAG_BE_BIT_SET(1);1058} else if (sb_enforce_bit) {1059objwrapflags |= WRAPFLAG_BE_BIT_SET(2);1060}10611062*output_len = input_len + PLPKS_WRAPPING_BUF_DIFF;10631064*output_buf = kzalloc(ALIGN(*output_len, PLPKS_WRAPPING_BUF_ALIGN),1065GFP_KERNEL);1066if (!(*output_buf)) {1067pr_err("Output buffer allocation failed. Returning -ENOMEM.");1068rc = -ENOMEM;1069goto out_free_label;1070}10711072do {1073rc = plpar_hcall9(H_PKS_WRAP_OBJECT, retbuf,1074virt_to_phys(auth), virt_to_phys(label),1075label->size, objwrapflags,1076virt_to_phys(*input_buf), input_len,1077virt_to_phys(*output_buf), *output_len,1078continuetoken);10791080continuetoken = retbuf[0];1081pseries_status = rc;1082rc = pseries_status_to_err(rc);1083} while (rc == -EBUSY);10841085if (rc) {1086pr_err("H_PKS_WRAP_OBJECT failed. pseries_status=%d, rc=%d",1087pseries_status, rc);1088kfree(*output_buf);1089*output_buf = NULL;1090} else {1091*output_len = retbuf[1];1092}10931094out_free_label:1095kfree(label);1096out:1097kfree(auth);1098return rc;1099}1100EXPORT_SYMBOL_GPL(plpks_wrap_object);11011102/**1103* plpks_unwrap_object() - Unwrap an object using the default wrapping key1104* stored in the PLPKS.1105* @input_buf: buffer containing the data to be unwrapped1106* @input_len: length of the input buffer1107* @output_buf: buffer to store the unwrapped data1108* @output_len: length of the output buffer1109*1110* The H_PKS_UNWRAP_OBJECT HCALL unwraps an object that was previously wrapped1111* using the H_PKS_WRAP_OBJECT HCALL.1112*1113* Possible reasons for the returned errno values:1114*1115* -ENXIO if PLPKS is not supported1116* -EIO if PLPKS access is blocked due to the LPAR's state1117* if PLPKS modification is blocked due to the LPAR's state1118* if an error occurred while processing the request1119* -EINVAL if invalid authorization parameter1120* if invalid or unsupported object unwrapping flags1121* if invalid input buffer parameter1122* if invalid input buffer length parameter1123* if invalid output buffer parameter1124* if invalid output buffer length parameter1125* if invalid continue token parameter1126* if the wrapping key is not compatible with the wrapping1127* algorithm1128* if the wrapped object's format is not supported1129* if the wrapped object is invalid1130* -EPERM if access is denied1131* -ENOENT if the wrapping key for the provided object was not found1132* -EBUSY if unable to handle the request or long running operation1133* initiated, retry later.1134*1135* Returns: On success 0 is returned, a negative errno if not.1136*/1137int plpks_unwrap_object(u8 **input_buf, u32 input_len, u8 **output_buf,1138u32 *output_len)1139{1140unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = { 0 };1141struct plpks_auth *auth;1142u64 continuetoken = 0;1143u64 objwrapflags = 0;1144int rc = 0, pseries_status = 0;11451146auth = construct_auth(PLPKS_OS_OWNER);1147if (IS_ERR(auth))1148return PTR_ERR(auth);11491150*output_len = input_len - PLPKS_WRAPPING_BUF_DIFF;1151*output_buf = kzalloc(ALIGN(*output_len, PLPKS_WRAPPING_BUF_ALIGN),1152GFP_KERNEL);1153if (!(*output_buf)) {1154pr_err("Output buffer allocation failed. Returning -ENOMEM.");1155rc = -ENOMEM;1156goto out;1157}11581159do {1160rc = plpar_hcall9(H_PKS_UNWRAP_OBJECT, retbuf,1161virt_to_phys(auth), objwrapflags,1162virt_to_phys(*input_buf), input_len,1163virt_to_phys(*output_buf), *output_len,1164continuetoken);11651166continuetoken = retbuf[0];1167pseries_status = rc;1168rc = pseries_status_to_err(rc);1169} while (rc == -EBUSY);11701171if (rc) {1172pr_err("H_PKS_UNWRAP_OBJECT failed. pseries_status=%d, rc=%d",1173pseries_status, rc);1174kfree(*output_buf);1175*output_buf = NULL;1176} else {1177*output_len = retbuf[1];1178}11791180out:1181kfree(auth);1182return rc;1183}1184EXPORT_SYMBOL_GPL(plpks_unwrap_object);11851186/**1187* plpks_read_os_var() - Fetch the data for the specified variable that is owned1188* by the OS consumer.1189* @var: variable to be read from the PLPKS1190*1191* The consumer or the owner of the object is the os kernel. The1192* H_PKS_READ_OBJECT HCALL reads an object from the PLPKS. The caller must1193* allocate the buffer var->data and specify the length for this buffer in1194* var->datalen. If no buffer is provided, var->datalen will be populated with1195* the requested object's size.1196*1197* Possible reasons for the returned errno values:1198*1199* -ENXIO if PLPKS is not supported1200* -EIO if PLPKS access is blocked due to the LPAR's state1201* if an error occurred while processing the request1202* -EINVAL if invalid authorization parameter1203* if invalid object label parameter1204* if invalid object label len parameter1205* if invalid output data parameter1206* if invalid output data len parameter1207* -EPERM if access is denied1208* -ENOENT if the requested object was not found1209* -EFBIG if the requested object couldn't be1210* stored in the buffer provided1211* -EBUSY if unable to handle the request1212*1213* Returns: On success 0 is returned, a negative errno if not.1214*/1215int plpks_read_os_var(struct plpks_var *var)1216{1217return plpks_read_var(PLPKS_OS_OWNER, var);1218}12191220/**1221* plpks_read_fw_var() - Fetch the data for the specified variable that is1222* owned by the firmware consumer.1223* @var: variable to be read from the PLPKS1224*1225* The consumer or the owner of the object is the firmware. The1226* H_PKS_READ_OBJECT HCALL reads an object from the PLPKS. The caller must1227* allocate the buffer var->data and specify the length for this buffer in1228* var->datalen. If no buffer is provided, var->datalen will be populated with1229* the requested object's size.1230*1231* Possible reasons for the returned errno values:1232*1233* -ENXIO if PLPKS is not supported1234* -EIO if PLPKS access is blocked due to the LPAR's state1235* if an error occurred while processing the request1236* -EINVAL if invalid authorization parameter1237* if invalid object label parameter1238* if invalid object label len parameter1239* if invalid output data parameter1240* if invalid output data len parameter1241* -EPERM if access is denied1242* -ENOENT if the requested object was not found1243* -EFBIG if the requested object couldn't be1244* stored in the buffer provided1245* -EBUSY if unable to handle the request1246*1247* Returns: On success 0 is returned, a negative errno if not.1248*/1249int plpks_read_fw_var(struct plpks_var *var)1250{1251return plpks_read_var(PLPKS_FW_OWNER, var);1252}12531254/**1255* plpks_read_bootloader_var() - Fetch the data for the specified variable1256* owned by the bootloader consumer.1257* @var: variable to be read from the PLPKS1258*1259* The consumer or the owner of the object is the bootloader. The1260* H_PKS_READ_OBJECT HCALL reads an object from the PLPKS. The caller must1261* allocate the buffer var->data and specify the length for this buffer in1262* var->datalen. If no buffer is provided, var->datalen will be populated with1263* the requested object's size.1264*1265* Possible reasons for the returned errno values:1266*1267* -ENXIO if PLPKS is not supported1268* -EIO if PLPKS access is blocked due to the LPAR's state1269* if an error occurred while processing the request1270* -EINVAL if invalid authorization parameter1271* if invalid object label parameter1272* if invalid object label len parameter1273* if invalid output data parameter1274* if invalid output data len parameter1275* -EPERM if access is denied1276* -ENOENT if the requested object was not found1277* -EFBIG if the requested object couldn't be1278* stored in the buffer provided1279* -EBUSY if unable to handle the request1280*1281* Returns: On success 0 is returned, a negative errno if not.1282*/1283int plpks_read_bootloader_var(struct plpks_var *var)1284{1285return plpks_read_var(PLPKS_BOOTLOADER_OWNER, var);1286}12871288/**1289* plpks_populate_fdt(): Populates the FDT with the PLPKS password to prepare1290* for kexec.1291* @fdt: pointer to the device tree blob1292*1293* Upon confirming the existence of the chosen node, invoke fdt_setprop to1294* populate the device tree with the PLPKS password in order to prepare for1295* kexec.1296*1297* Returns: On success 0 is returned, a negative value if not.1298*/1299int plpks_populate_fdt(void *fdt)1300{1301int chosen_offset = fdt_path_offset(fdt, "/chosen");13021303if (chosen_offset < 0) {1304pr_err("Can't find chosen node: %s\n",1305fdt_strerror(chosen_offset));1306return chosen_offset;1307}13081309return fdt_setprop(fdt, chosen_offset, "ibm,plpks-pw", ospassword, ospasswordlength);1310}13111312/**1313* plpks_early_init_devtree() - Retrieves and clears the PLPKS password from the1314* DT in early init.1315*1316* Once a password is registered with the hypervisor it cannot be cleared1317* without rebooting the LPAR, so to keep using the PLPKS across kexec boots we1318* need to recover the previous password from the FDT.1319*1320* There are a few challenges here. We don't want the password to be visible to1321* users, so we need to clear it from the FDT. This has to be done in early1322* boot. Clearing it from the FDT would make the FDT's checksum invalid, so we1323* have to manually cause the checksum to be recalculated.1324*/1325void __init plpks_early_init_devtree(void)1326{1327void *fdt = initial_boot_params;1328int chosen_node = fdt_path_offset(fdt, "/chosen");1329const u8 *password;1330int len;13311332if (chosen_node < 0)1333return;13341335password = fdt_getprop(fdt, chosen_node, "ibm,plpks-pw", &len);1336if (len <= 0) {1337pr_debug("Couldn't find ibm,plpks-pw node.\n");1338return;1339}13401341ospassword = memblock_alloc_raw(len, SMP_CACHE_BYTES);1342if (!ospassword) {1343pr_err("Error allocating memory for password.\n");1344goto out;1345}13461347memcpy(ospassword, password, len);1348ospasswordlength = (u16)len;13491350out:1351fdt_nop_property(fdt, chosen_node, "ibm,plpks-pw");1352// Since we've cleared the password, we must update the FDT checksum1353early_init_dt_verify(fdt, __pa(fdt));1354}13551356static __init int pseries_plpks_init(void)1357{1358int rc;13591360if (!firmware_has_feature(FW_FEATURE_PLPKS))1361return -ENODEV;13621363rc = _plpks_get_config();13641365if (rc) {1366pr_err("POWER LPAR Platform KeyStore is not supported or enabled\n");1367return rc;1368}13691370rc = plpks_gen_password();1371if (rc)1372pr_err("Failed setting POWER LPAR Platform KeyStore Password\n");1373else1374pr_info("POWER LPAR Platform KeyStore initialized successfully\n");13751376return rc;1377}1378machine_arch_initcall(pseries, pseries_plpks_init);137913801381