From: Robin van der Gracht <robin@xxxxxxxxxxx> Introduce a mechanism to permanently lock OTP eFuses after programming by adding a new `writelock` parameter. When `writelock` is enabled, the driver: - Programs the OTP fuse using `BSEC_SMC_PROG_OTP`. - If successful, triggers `BSEC_SMC_WRLOCK_OTP` (OP-TEE: `STM32_SIP_SVC_BSEC_WRLOCK_OTP`) to permanently disable further modifications to the OTP word. Security Concern: Without this lock mechanism, an OTP word can still be altered by OR-ing additional bits onto the existing value, as STM32 BSEC OTP fuses only allow one-way bit transitions from 0 to 1. This is a potential security risk when dealing with keys or sensitive configuration values, as an attacker could modify certain OTP bits without fully replacing the original value. Warning! Write lock is enabled globally per BSEC device: - While `writelock=1`, all writes via the BSEC device will be permanently locked. - The user must avoid writing unintended values during this period, as they will become irrevocable. Example Use Case: To program and permanently lock an OTP word: bsec0.permanent_write_enable=1 bsec0.writelock=1 mw -l -d /dev/stm32-bsec 0x00000170+4 $some_data bsec0.permanent_write_enable=0 bsec0.writelock=0 After execution, the OTP at address `0x170` will be permanently write-locked. Signed-off-by: Robin van der Gracht <robin@xxxxxxxxxxx> Signed-off-by: Oleksij Rempel <o.rempel@xxxxxxxxxxxxxx> --- drivers/nvmem/bsec.c | 14 +++++++++++--- include/mach/stm32mp/bsec.h | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/drivers/nvmem/bsec.c b/drivers/nvmem/bsec.c index b92d925956ee..c0ca0a2ab6a4 100644 --- a/drivers/nvmem/bsec.c +++ b/drivers/nvmem/bsec.c @@ -27,6 +27,7 @@ struct bsec_priv { int permanent_write_enable; u8 lower; struct tee_context *ctx; + int writelock; }; struct stm32_bsec_data { @@ -67,11 +68,16 @@ static int stm32_bsec_read_shadow(void *ctx, unsigned reg, unsigned *val) static int stm32_bsec_reg_write(void *ctx, unsigned reg, unsigned val) { struct bsec_priv *priv = ctx; + int ret; - if (priv->permanent_write_enable) - return bsec_smc(BSEC_SMC_PROG_OTP, reg, val, NULL); - else + if (!priv->permanent_write_enable) return bsec_smc(BSEC_SMC_WRITE_SHADOW, reg, val, NULL); + + ret = bsec_smc(BSEC_SMC_PROG_OTP, reg, val, NULL); + if (!ret && priv->writelock) + ret = bsec_smc(BSEC_SMC_WRLOCK_OTP, reg, 0, NULL); + + return ret; } static struct regmap_bus stm32_bsec_regmap_bus = { @@ -245,6 +251,8 @@ static int stm32_bsec_probe(struct device *dev) if (IS_ENABLED(CONFIG_STM32_BSEC_WRITE)) { dev_add_param_bool(&priv->dev, "permanent_write_enable", NULL, NULL, &priv->permanent_write_enable, NULL); + dev_add_param_bool(&priv->dev, "writelock", + NULL, NULL, &priv->writelock, NULL); } nvmem = nvmem_regmap_register(map, "stm32-bsec"); diff --git a/include/mach/stm32mp/bsec.h b/include/mach/stm32mp/bsec.h index 45eb0a3f4523..be8cec536a40 100644 --- a/include/mach/stm32mp/bsec.h +++ b/include/mach/stm32mp/bsec.h @@ -26,6 +26,7 @@ enum bsec_op { BSEC_SMC_READ_OTP = 4, BSEC_SMC_READ_ALL = 5, BSEC_SMC_WRITE_ALL = 6, + BSEC_SMC_WRLOCK_OTP = 7, }; static inline enum bsec_smc bsec_read_field(unsigned field, unsigned *val) -- 2.39.5