Path: blob/master/arch/powerpc/platforms/pseries/plpks.c
26481 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#include <linux/delay.h>12#include <linux/errno.h>13#include <linux/io.h>14#include <linux/printk.h>15#include <linux/slab.h>16#include <linux/string.h>17#include <linux/types.h>18#include <linux/of_fdt.h>19#include <linux/libfdt.h>20#include <linux/memblock.h>21#include <asm/hvcall.h>22#include <asm/machdep.h>23#include <asm/plpks.h>24#include <asm/firmware.h>2526static u8 *ospassword;27static u16 ospasswordlength;2829// Retrieved with H_PKS_GET_CONFIG30static u8 version;31static u16 objoverhead;32static u16 maxpwsize;33static u16 maxobjsize;34static s16 maxobjlabelsize;35static u32 totalsize;36static u32 usedspace;37static u32 supportedpolicies;38static u32 maxlargeobjectsize;39static u64 signedupdatealgorithms;4041struct plpks_auth {42u8 version;43u8 consumer;44__be64 rsvd0;45__be32 rsvd1;46__be16 passwordlength;47u8 password[];48} __packed __aligned(16);4950struct label_attr {51u8 prefix[8];52u8 version;53u8 os;54u8 length;55u8 reserved[5];56};5758struct label {59struct label_attr attr;60u8 name[PLPKS_MAX_NAME_SIZE];61size_t size;62};6364static int pseries_status_to_err(int rc)65{66int err;6768switch (rc) {69case H_SUCCESS:70err = 0;71break;72case H_FUNCTION:73err = -ENXIO;74break;75case H_PARAMETER:76case H_P2:77case H_P3:78case H_P4:79case H_P5:80case H_P6:81err = -EINVAL;82break;83case H_NOT_FOUND:84err = -ENOENT;85break;86case H_BUSY:87case H_LONG_BUSY_ORDER_1_MSEC:88case H_LONG_BUSY_ORDER_10_MSEC:89case H_LONG_BUSY_ORDER_100_MSEC:90case H_LONG_BUSY_ORDER_1_SEC:91case H_LONG_BUSY_ORDER_10_SEC:92case H_LONG_BUSY_ORDER_100_SEC:93err = -EBUSY;94break;95case H_AUTHORITY:96err = -EPERM;97break;98case H_NO_MEM:99err = -ENOMEM;100break;101case H_RESOURCE:102err = -EEXIST;103break;104case H_TOO_BIG:105err = -EFBIG;106break;107case H_STATE:108err = -EIO;109break;110case H_R_STATE:111err = -EIO;112break;113case H_IN_USE:114err = -EEXIST;115break;116case H_ABORTED:117err = -EIO;118break;119default:120err = -EINVAL;121}122123pr_debug("Converted hypervisor code %d to Linux %d\n", rc, err);124125return err;126}127128static int plpks_gen_password(void)129{130unsigned long retbuf[PLPAR_HCALL_BUFSIZE] = { 0 };131u8 *password, consumer = PLPKS_OS_OWNER;132int rc;133134// If we booted from kexec, we could be reusing an existing password already135if (ospassword) {136pr_debug("Password of length %u already in use\n", ospasswordlength);137return 0;138}139140// The password must not cross a page boundary, so we align to the next power of 2141password = kzalloc(roundup_pow_of_two(maxpwsize), GFP_KERNEL);142if (!password)143return -ENOMEM;144145rc = plpar_hcall(H_PKS_GEN_PASSWORD, retbuf, consumer, 0,146virt_to_phys(password), maxpwsize);147148if (!rc) {149ospasswordlength = maxpwsize;150ospassword = kzalloc(maxpwsize, GFP_KERNEL);151if (!ospassword) {152kfree_sensitive(password);153return -ENOMEM;154}155memcpy(ospassword, password, ospasswordlength);156} else {157if (rc == H_IN_USE) {158pr_warn("Password already set - authenticated operations will fail\n");159rc = 0;160} else {161goto out;162}163}164out:165kfree_sensitive(password);166167return pseries_status_to_err(rc);168}169170static struct plpks_auth *construct_auth(u8 consumer)171{172struct plpks_auth *auth;173174if (consumer > PLPKS_OS_OWNER)175return ERR_PTR(-EINVAL);176177// The auth structure must not cross a page boundary and must be178// 16 byte aligned. We align to the next largest power of 2179auth = kzalloc(roundup_pow_of_two(struct_size(auth, password, maxpwsize)), GFP_KERNEL);180if (!auth)181return ERR_PTR(-ENOMEM);182183auth->version = 1;184auth->consumer = consumer;185186if (consumer == PLPKS_FW_OWNER || consumer == PLPKS_BOOTLOADER_OWNER)187return auth;188189memcpy(auth->password, ospassword, ospasswordlength);190191auth->passwordlength = cpu_to_be16(ospasswordlength);192193return auth;194}195196/*197* Label is combination of label attributes + name.198* Label attributes are used internally by kernel and not exposed to the user.199*/200static struct label *construct_label(char *component, u8 varos, u8 *name,201u16 namelen)202{203struct label *label;204size_t slen = 0;205206if (!name || namelen > PLPKS_MAX_NAME_SIZE)207return ERR_PTR(-EINVAL);208209// Support NULL component for signed updates210if (component) {211slen = strlen(component);212if (slen > sizeof(label->attr.prefix))213return ERR_PTR(-EINVAL);214}215216// The label structure must not cross a page boundary, so we align to the next power of 2217label = kzalloc(roundup_pow_of_two(sizeof(*label)), GFP_KERNEL);218if (!label)219return ERR_PTR(-ENOMEM);220221if (component)222memcpy(&label->attr.prefix, component, slen);223224label->attr.version = PLPKS_LABEL_VERSION;225label->attr.os = varos;226label->attr.length = PLPKS_MAX_LABEL_ATTR_SIZE;227memcpy(&label->name, name, namelen);228229label->size = sizeof(struct label_attr) + namelen;230231return label;232}233234static int _plpks_get_config(void)235{236unsigned long retbuf[PLPAR_HCALL_BUFSIZE] = { 0 };237struct config {238u8 version;239u8 flags;240__be16 rsvd0;241__be16 objoverhead;242__be16 maxpwsize;243__be16 maxobjlabelsize;244__be16 maxobjsize;245__be32 totalsize;246__be32 usedspace;247__be32 supportedpolicies;248__be32 maxlargeobjectsize;249__be64 signedupdatealgorithms;250u8 rsvd1[476];251} __packed * config;252size_t size;253int rc = 0;254255size = sizeof(*config);256257// Config struct must not cross a page boundary. So long as the struct258// size is a power of 2, this should be fine as alignment is guaranteed259config = kzalloc(size, GFP_KERNEL);260if (!config) {261rc = -ENOMEM;262goto err;263}264265rc = plpar_hcall(H_PKS_GET_CONFIG, retbuf, virt_to_phys(config), size);266267if (rc != H_SUCCESS) {268rc = pseries_status_to_err(rc);269goto err;270}271272version = config->version;273objoverhead = be16_to_cpu(config->objoverhead);274maxpwsize = be16_to_cpu(config->maxpwsize);275maxobjsize = be16_to_cpu(config->maxobjsize);276maxobjlabelsize = be16_to_cpu(config->maxobjlabelsize);277totalsize = be32_to_cpu(config->totalsize);278usedspace = be32_to_cpu(config->usedspace);279supportedpolicies = be32_to_cpu(config->supportedpolicies);280maxlargeobjectsize = be32_to_cpu(config->maxlargeobjectsize);281signedupdatealgorithms = be64_to_cpu(config->signedupdatealgorithms);282283// Validate that the numbers we get back match the requirements of the spec284if (maxpwsize < 32) {285pr_err("Invalid Max Password Size received from hypervisor (%d < 32)\n", maxpwsize);286rc = -EIO;287goto err;288}289290if (maxobjlabelsize < 255) {291pr_err("Invalid Max Object Label Size received from hypervisor (%d < 255)\n",292maxobjlabelsize);293rc = -EIO;294goto err;295}296297if (totalsize < 4096) {298pr_err("Invalid Total Size received from hypervisor (%d < 4096)\n", totalsize);299rc = -EIO;300goto err;301}302303if (version >= 3 && maxlargeobjectsize >= 65536 && maxobjsize != 0xFFFF) {304pr_err("Invalid Max Object Size (0x%x != 0xFFFF)\n", maxobjsize);305rc = -EIO;306goto err;307}308309err:310kfree(config);311return rc;312}313314u8 plpks_get_version(void)315{316return version;317}318319u16 plpks_get_objoverhead(void)320{321return objoverhead;322}323324u16 plpks_get_maxpwsize(void)325{326return maxpwsize;327}328329u16 plpks_get_maxobjectsize(void)330{331return maxobjsize;332}333334u16 plpks_get_maxobjectlabelsize(void)335{336return maxobjlabelsize;337}338339u32 plpks_get_totalsize(void)340{341return totalsize;342}343344u32 plpks_get_usedspace(void)345{346// Unlike other config values, usedspace regularly changes as objects347// are updated, so we need to refresh.348int rc = _plpks_get_config();349if (rc) {350pr_err("Couldn't get config, rc: %d\n", rc);351return 0;352}353return usedspace;354}355356u32 plpks_get_supportedpolicies(void)357{358return supportedpolicies;359}360361u32 plpks_get_maxlargeobjectsize(void)362{363return maxlargeobjectsize;364}365366u64 plpks_get_signedupdatealgorithms(void)367{368return signedupdatealgorithms;369}370371u16 plpks_get_passwordlen(void)372{373return ospasswordlength;374}375376bool plpks_is_available(void)377{378int rc;379380if (!firmware_has_feature(FW_FEATURE_PLPKS))381return false;382383rc = _plpks_get_config();384if (rc)385return false;386387return true;388}389390static int plpks_confirm_object_flushed(struct label *label,391struct plpks_auth *auth)392{393unsigned long retbuf[PLPAR_HCALL_BUFSIZE] = { 0 };394bool timed_out = true;395u64 timeout = 0;396u8 status;397int rc;398399do {400rc = plpar_hcall(H_PKS_CONFIRM_OBJECT_FLUSHED, retbuf,401virt_to_phys(auth), virt_to_phys(label),402label->size);403404status = retbuf[0];405if (rc) {406timed_out = false;407if (rc == H_NOT_FOUND && status == 1)408rc = 0;409break;410}411412if (!rc && status == 1) {413timed_out = false;414break;415}416417fsleep(PLPKS_FLUSH_SLEEP);418timeout = timeout + PLPKS_FLUSH_SLEEP;419} while (timeout < PLPKS_MAX_TIMEOUT);420421if (timed_out)422return -ETIMEDOUT;423424return pseries_status_to_err(rc);425}426427int plpks_signed_update_var(struct plpks_var *var, u64 flags)428{429unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};430int rc;431struct label *label;432struct plpks_auth *auth;433u64 continuetoken = 0;434u64 timeout = 0;435436if (!var->data || var->datalen <= 0 || var->namelen > PLPKS_MAX_NAME_SIZE)437return -EINVAL;438439if (!(var->policy & PLPKS_SIGNEDUPDATE))440return -EINVAL;441442// Signed updates need the component to be NULL.443if (var->component)444return -EINVAL;445446auth = construct_auth(PLPKS_OS_OWNER);447if (IS_ERR(auth))448return PTR_ERR(auth);449450label = construct_label(var->component, var->os, var->name, var->namelen);451if (IS_ERR(label)) {452rc = PTR_ERR(label);453goto out;454}455456do {457rc = plpar_hcall9(H_PKS_SIGNED_UPDATE, retbuf,458virt_to_phys(auth), virt_to_phys(label),459label->size, var->policy, flags,460virt_to_phys(var->data), var->datalen,461continuetoken);462463continuetoken = retbuf[0];464if (pseries_status_to_err(rc) == -EBUSY) {465int delay_us = get_longbusy_msecs(rc) * 1000;466467fsleep(delay_us);468timeout += delay_us;469}470rc = pseries_status_to_err(rc);471} while (rc == -EBUSY && timeout < PLPKS_MAX_TIMEOUT);472473if (!rc)474rc = plpks_confirm_object_flushed(label, auth);475476kfree(label);477out:478kfree(auth);479480return rc;481}482483int plpks_write_var(struct plpks_var var)484{485unsigned long retbuf[PLPAR_HCALL_BUFSIZE] = { 0 };486struct plpks_auth *auth;487struct label *label;488int rc;489490if (!var.component || !var.data || var.datalen <= 0 ||491var.namelen > PLPKS_MAX_NAME_SIZE || var.datalen > PLPKS_MAX_DATA_SIZE)492return -EINVAL;493494if (var.policy & PLPKS_SIGNEDUPDATE)495return -EINVAL;496497auth = construct_auth(PLPKS_OS_OWNER);498if (IS_ERR(auth))499return PTR_ERR(auth);500501label = construct_label(var.component, var.os, var.name, var.namelen);502if (IS_ERR(label)) {503rc = PTR_ERR(label);504goto out;505}506507rc = plpar_hcall(H_PKS_WRITE_OBJECT, retbuf, virt_to_phys(auth),508virt_to_phys(label), label->size, var.policy,509virt_to_phys(var.data), var.datalen);510511if (!rc)512rc = plpks_confirm_object_flushed(label, auth);513514rc = pseries_status_to_err(rc);515kfree(label);516out:517kfree(auth);518519return rc;520}521522int plpks_remove_var(char *component, u8 varos, struct plpks_var_name vname)523{524unsigned long retbuf[PLPAR_HCALL_BUFSIZE] = { 0 };525struct plpks_auth *auth;526struct label *label;527int rc;528529if (vname.namelen > PLPKS_MAX_NAME_SIZE)530return -EINVAL;531532auth = construct_auth(PLPKS_OS_OWNER);533if (IS_ERR(auth))534return PTR_ERR(auth);535536label = construct_label(component, varos, vname.name, vname.namelen);537if (IS_ERR(label)) {538rc = PTR_ERR(label);539goto out;540}541542rc = plpar_hcall(H_PKS_REMOVE_OBJECT, retbuf, virt_to_phys(auth),543virt_to_phys(label), label->size);544545if (!rc)546rc = plpks_confirm_object_flushed(label, auth);547548rc = pseries_status_to_err(rc);549kfree(label);550out:551kfree(auth);552553return rc;554}555556static int plpks_read_var(u8 consumer, struct plpks_var *var)557{558unsigned long retbuf[PLPAR_HCALL_BUFSIZE] = { 0 };559struct plpks_auth *auth;560struct label *label = NULL;561u8 *output;562int rc;563564if (var->namelen > PLPKS_MAX_NAME_SIZE)565return -EINVAL;566567auth = construct_auth(consumer);568if (IS_ERR(auth))569return PTR_ERR(auth);570571if (consumer == PLPKS_OS_OWNER) {572label = construct_label(var->component, var->os, var->name,573var->namelen);574if (IS_ERR(label)) {575rc = PTR_ERR(label);576goto out_free_auth;577}578}579580output = kzalloc(maxobjsize, GFP_KERNEL);581if (!output) {582rc = -ENOMEM;583goto out_free_label;584}585586if (consumer == PLPKS_OS_OWNER)587rc = plpar_hcall(H_PKS_READ_OBJECT, retbuf, virt_to_phys(auth),588virt_to_phys(label), label->size, virt_to_phys(output),589maxobjsize);590else591rc = plpar_hcall(H_PKS_READ_OBJECT, retbuf, virt_to_phys(auth),592virt_to_phys(var->name), var->namelen, virt_to_phys(output),593maxobjsize);594595596if (rc != H_SUCCESS) {597rc = pseries_status_to_err(rc);598goto out_free_output;599}600601if (!var->data || var->datalen > retbuf[0])602var->datalen = retbuf[0];603604var->policy = retbuf[1];605606if (var->data)607memcpy(var->data, output, var->datalen);608609rc = 0;610611out_free_output:612kfree(output);613out_free_label:614kfree(label);615out_free_auth:616kfree(auth);617618return rc;619}620621int plpks_read_os_var(struct plpks_var *var)622{623return plpks_read_var(PLPKS_OS_OWNER, var);624}625626int plpks_read_fw_var(struct plpks_var *var)627{628return plpks_read_var(PLPKS_FW_OWNER, var);629}630631int plpks_read_bootloader_var(struct plpks_var *var)632{633return plpks_read_var(PLPKS_BOOTLOADER_OWNER, var);634}635636int plpks_populate_fdt(void *fdt)637{638int chosen_offset = fdt_path_offset(fdt, "/chosen");639640if (chosen_offset < 0) {641pr_err("Can't find chosen node: %s\n",642fdt_strerror(chosen_offset));643return chosen_offset;644}645646return fdt_setprop(fdt, chosen_offset, "ibm,plpks-pw", ospassword, ospasswordlength);647}648649// Once a password is registered with the hypervisor it cannot be cleared without650// rebooting the LPAR, so to keep using the PLPKS across kexec boots we need to651// recover the previous password from the FDT.652//653// There are a few challenges here. We don't want the password to be visible to654// users, so we need to clear it from the FDT. This has to be done in early boot.655// Clearing it from the FDT would make the FDT's checksum invalid, so we have to656// manually cause the checksum to be recalculated.657void __init plpks_early_init_devtree(void)658{659void *fdt = initial_boot_params;660int chosen_node = fdt_path_offset(fdt, "/chosen");661const u8 *password;662int len;663664if (chosen_node < 0)665return;666667password = fdt_getprop(fdt, chosen_node, "ibm,plpks-pw", &len);668if (len <= 0) {669pr_debug("Couldn't find ibm,plpks-pw node.\n");670return;671}672673ospassword = memblock_alloc_raw(len, SMP_CACHE_BYTES);674if (!ospassword) {675pr_err("Error allocating memory for password.\n");676goto out;677}678679memcpy(ospassword, password, len);680ospasswordlength = (u16)len;681682out:683fdt_nop_property(fdt, chosen_node, "ibm,plpks-pw");684// Since we've cleared the password, we must update the FDT checksum685early_init_dt_verify(fdt, __pa(fdt));686}687688static __init int pseries_plpks_init(void)689{690int rc;691692if (!firmware_has_feature(FW_FEATURE_PLPKS))693return -ENODEV;694695rc = _plpks_get_config();696697if (rc) {698pr_err("POWER LPAR Platform KeyStore is not supported or enabled\n");699return rc;700}701702rc = plpks_gen_password();703if (rc)704pr_err("Failed setting POWER LPAR Platform KeyStore Password\n");705else706pr_info("POWER LPAR Platform KeyStore initialized successfully\n");707708return rc;709}710machine_arch_initcall(pseries, pseries_plpks_init);711712713