Re: [PATCH 09/13] platform/x86: lenovo: tlmi: Add authentication sysfs entries

From: Rong Zhang

Date: Fri Dec 04 2026 - 08:58:34 EST


Hi Mark,

On Thu, 2026-12-03 at 21:20 -0500, Mark Pearson wrote:
> Add interface to authentication settings using it's associated GUID.
> tlmi-auth uses the data read in by tlmi-authdata.
>
> Enables ability to configure admin, system, power-on, hdd and nvme
> passwords.
> Enables certificate support.
>
> Signed-off-by: Mark Pearson <mpearson-lenovo@xxxxxxxxx>
> ---
> drivers/platform/x86/lenovo/Makefile | 2 +-
> drivers/platform/x86/lenovo/think-lmi.h | 6 +
> drivers/platform/x86/lenovo/tlmi-auth.c | 958 ++++++++++++++++++++++
> drivers/platform/x86/lenovo/tlmi-config.c | 50 +-
> 4 files changed, 1009 insertions(+), 7 deletions(-)
> create mode 100644 drivers/platform/x86/lenovo/tlmi-auth.c
>
> diff --git a/drivers/platform/x86/lenovo/Makefile b/drivers/platform/x86/lenovo/Makefile
> index f1600b562273..6434e77f4ed3 100644
> --- a/drivers/platform/x86/lenovo/Makefile
> +++ b/drivers/platform/x86/lenovo/Makefile
> @@ -5,7 +5,7 @@
> #
> obj-$(CONFIG_IDEAPAD_LAPTOP) += ideapad-laptop.o
> obj-$(CONFIG_THINKPAD_LMI) += think-lmi.o
> -obj-$(CONFIG_THINKPAD_LMI) += tlmi-config.o tlmi-attr.o tlmi-authdata.o
> +obj-$(CONFIG_THINKPAD_LMI) += tlmi-config.o tlmi-attr.o tlmi-authdata.o tlmi-auth.o
> obj-$(CONFIG_THINKPAD_ACPI) += thinkpad_acpi.o
>
> lenovo-target-$(CONFIG_LENOVO_WMI_HOTKEY_UTILITIES) += wmi-hotkey-utilities.o
> diff --git a/drivers/platform/x86/lenovo/think-lmi.h b/drivers/platform/x86/lenovo/think-lmi.h
> index 75b21dcc2f42..6ab9a7e6fabf 100644
> --- a/drivers/platform/x86/lenovo/think-lmi.h
> +++ b/drivers/platform/x86/lenovo/think-lmi.h
> @@ -13,6 +13,7 @@
> #define TLMI_INDEX_MAX 32
>
> #define TLMI_ATTRIBUTE_GUID "4B79FBA6-FFD4-41B6-8EA8-E81CDA0F5FC3"
> +#define TLMI_AUTHENTICATION_GUID "5cb5f556-30ce-40d3-a135-7ca0648a5a52"
> #define TLMI_AUTHENTICATION_DATA_GUID "1178acfd-78a1-46d8-a8eb-5f7cb74a3b4e"
>
> /* Possible error values */
> @@ -95,6 +96,7 @@ struct tlmi_pwdcfg {
> /* password setting details */
> struct tlmi_pwd_setting {
> struct kobject kobj;
> + struct wmi_device *wdev;
> bool pwd_enabled;
> char password[TLMI_PWD_BUFSIZE];
> const char *pwd_type;
> @@ -146,6 +148,7 @@ struct think_lmi {
> struct kset *attribute_kset;
> struct kset *authentication_kset;
> struct wmi_device *config_wdev;
> + struct wmi_device *auth_wdev;
>
> struct tlmi_pwdcfg pwdcfg;
> struct tlmi_pwd_setting *pwd_admin;
> @@ -153,6 +156,7 @@ struct think_lmi {
> struct tlmi_pwd_setting *pwd_system;
> struct tlmi_pwd_setting *pwd_hdd;
> struct tlmi_pwd_setting *pwd_nvme;
> + struct mutex tlmi_auth_lock; /* shared between auth and config */
>
> const struct tlmi_cert_guids *cert_guid;
> };
> @@ -162,5 +166,7 @@ struct component_match;
> void tlmi_attr_match_add(struct device *master, struct component_match **matchptr);
> void tlmi_attr_release_setting(struct tlmi_attr_setting *setting);
> void tlmi_authdata_match_add(struct device *master, struct component_match **matchptr);
> +void tlmi_auth_match_add(struct device *master, struct component_match **matchptr);
> +int tlmi_auth_authenticate_user(struct think_lmi *td);
>
> #endif /* !_THINK_LMI_H_ */
> diff --git a/drivers/platform/x86/lenovo/tlmi-auth.c b/drivers/platform/x86/lenovo/tlmi-auth.c
> new file mode 100644
> index 000000000000..d18cf6390663
> --- /dev/null
> +++ b/drivers/platform/x86/lenovo/tlmi-auth.c
> @@ -0,0 +1,958 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Lenovo ThinkLMI Authentication WMI interface driver.
> + *
> + * Manage the WMI authentication settings and their properties.
> + *
> + * This driver supports the "Common WMI" approach implemented on
> + * Thinkpad, ThinkStation & ThinkCenter from 2027 onwards.
> + */
> +
> +#include <linux/component.h>
> +#include <linux/device.h>
> +#include <linux/dmi.h>
> +#include <linux/kdev_t.h>
> +#include <linux/kobject.h>
> +#include <linux/limits.h>
> +#include <linux/module.h>
> +#include <linux/string_helpers.h>
> +#include <linux/types.h>
> +#include <linux/wmi.h>
> +#include "think-lmi.h"
> +#include "wmi-helpers.h"
> +
> +#define MAX_HDD_INDEX 4
> +#define MAX_NVME_INDEX 8
> +
> +enum pwd_type_v2 {
> + SVP = 0,
> + SMP = 1,
> + POP = 2,
> + HDD = 3, /* 3 to 6 */
> + NVME = HDD + MAX_HDD_INDEX, /* 7 to 14 */
> + MASTERHDD = NVME + MAX_NVME_INDEX, /* 15 to 18 */
> + MASTERNVME = MASTERHDD + MAX_HDD_INDEX, /* 19 to 26 */
> +};
> +
> +/* Method IDS for Access GUID */
> +#define SET_PASSWORD 1
> +#define AUTHENTICATE 2
> +#define SET_CERT 3
> +#define UPDATE_CERT 4
> +#define CLEAR_CERT 5
> +#define CERT_TO_PWD 6
> +#define CERT_THUMBPRINT 7
> +
> +#define TLMI_POP_PWD BIT(0) /* Power-on */
> +#define TLMI_SVP_PWD BIT(1) /* Supervisor */
> +#define TLMI_SMP_PWD BIT(2) /* System Management */
> +#define TLMI_SVC_PWD BIT(7) /* Supervisor Certificate */
> +#define TLMI_SMC_PWD BIT(8) /* System Certificate */
> +
> +static const char * const level_options[] = {
> + [TLMI_LEVEL_USER] = "user",
> + [TLMI_LEVEL_MASTER] = "master",
> +};
> +
> +int tlmi_auth_authenticate_user(struct think_lmi *td)
> +{
> + struct wmi_buffer wmibuf;
> + char *password;
> + int ret;
> +
> + lockdep_assert_held(&td->tlmi_auth_lock);
> +
> + /* If certificate used, we use signatures not passwords*/
> + if (td->pwd_admin->cert_installed ||
> + td->pwd_system->cert_installed)
> + return 0;
> +
> + /* If admin password configured, use that. If not try system */
> + if (strlen(td->pwd_admin->password) > 0)
> + password = td->pwd_admin->password;
> + else if (strlen(td->pwd_system->password) > 0)
> + password = td->pwd_system->password;
> + else
> + /* No password configured, so no need to run authentication */
> + return 0;
> +
> + wmibuf.length = struct_size_t(struct wmi_string, chars, strlen(password) + 1);
> + char *wmidata __free(kfree_sensitive) = kzalloc(wmibuf.length, GFP_KERNEL);
> + if (!wmidata)
> + return -ENOMEM;
> + wmibuf.data = wmidata;
> +
> + ret = tlmi_populate_string_buffer(wmibuf.data, wmibuf.length, password);
> + if (ret < 0)
> + return ret;
> +
> + return tlmi_request(td->auth_wdev, 0, AUTHENTICATE, &wmibuf);
> +}
> +EXPORT_SYMBOL_NS_GPL(tlmi_auth_authenticate_user, "TLMI-AUTH");
> +
> +/* ---- Authentication sysfs ---- */
> +static inline struct tlmi_pwd_setting *to_tlmi_pwd_setting(struct kobject *kobj)
> +{
> + return container_of(kobj, struct tlmi_pwd_setting, kobj);
> +}
> +
> +static ssize_t is_enabled_show(struct kobject *kobj, struct kobj_attribute *attr,
> + char *buf)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> +
> + return sysfs_emit(buf, "%d\n", setting->pwd_enabled || setting->cert_installed);
> +}
> +
> +static struct kobj_attribute auth_is_pass_set = __ATTR_RO(is_enabled);
> +
> +static ssize_t current_password_store(struct kobject *kobj,
> + struct kobj_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> + struct think_lmi *td = dev_get_drvdata(&setting->wdev->dev);
> + size_t pwdlen;
> +
> + guard(mutex)(&td->tlmi_auth_lock);
> +
> + /* Strip newline; setting password won't work if one is present. */
> + pwdlen = strchrnul(buf, '\n') - buf;

Only the last LF can be stripped. If you'd really want to handle LF in
between, return -EINVAL.

> + /* pwdlen == 0 is allowed to clear the password */
> + if (pwdlen && (pwdlen < setting->minlen || pwdlen > setting->maxlen))
> + return -EINVAL;

Could you factor out a helper function for the sanity checks of password
string? You are replicating mostly the same code below.

> +
> + strscpy(setting->password, buf, pwdlen + 1);
> + return count;
> +}
> +
> +static struct kobj_attribute auth_current_password = __ATTR_WO(current_password);
> +
> +static ssize_t new_password_store(struct kobject *kobj,
> + struct kobj_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> + struct think_lmi *td = dev_get_drvdata(&setting->wdev->dev);
> + enum pwd_type_v2 pwd_type;
> + size_t pwdlen, cur_pwdlen;
> + struct wmi_buffer wmibuf;
> + char *start;
> + int ix = 0;
> + int ret;
> +
> + if (!capable(CAP_SYS_ADMIN))
> + return -EPERM;
> +
> + guard(mutex)(&td->tlmi_auth_lock);
> +
> + /* Authorisation needs to be done first */
> + ret = tlmi_auth_authenticate_user(td);
> + /* Ignore most errors. We might be in deployment mode */

Could you add a dev_warn/info/dbg() when something is ignored?

> + if (ret == -ENOMEM)
> + return ret;
> +
> + /* Strip out CR if one is present, setting password won't work if it is present */
> + char *new_pwd __free(kfree_sensitive) = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);

Only the last LF can be stripped, too.

I believe v2 will have a considerable amount of changes, so my review
will pause here. I will wait until your v2 to restart reviewing.

Thanks,
Rong

> + if (!new_pwd)
> + return -ENOMEM;
> +
> + pwdlen = strlen(new_pwd);
> + /* pwdlen == 0 is allowed to clear the password */
> + if (pwdlen && (pwdlen < setting->minlen || pwdlen > setting->maxlen))
> + return -EINVAL;
> +
> + cur_pwdlen = struct_size_t(struct wmi_string, chars, strlen(setting->password) + 1);
> + pwdlen = struct_size_t(struct wmi_string, chars, strlen(new_pwd) + 1);
> + wmibuf.length = 2 + cur_pwdlen + pwdlen;
> + char *wmidata __free(kfree_sensitive) = kzalloc(wmibuf.length, GFP_KERNEL);
> + if (!wmidata)
> + return -ENOMEM;
> + wmibuf.data = wmidata;
> +
> + if (setting == td->pwd_admin) {
> + pwd_type = SVP;
> + } else if (setting == td->pwd_power) {
> + pwd_type = POP;
> + } else if (setting == td->pwd_system) {
> + pwd_type = SMP;
> + } else if (setting == td->pwd_hdd) {
> + if (setting->level == TLMI_LEVEL_MASTER)
> + pwd_type = MASTERHDD;
> + else
> + pwd_type = HDD;
> + pwd_type += setting->index - 1;
> + } else if (setting == td->pwd_nvme) {
> + if (setting->level == TLMI_LEVEL_MASTER)
> + pwd_type = MASTERNVME;
> + else
> + pwd_type = NVME;
> + pwd_type += setting->index - 1;
> + } else {
> + return -EOPNOTSUPP;
> + }
> +
> + start = wmibuf.data;
> + start[0] = pwd_type;
> + start += 2;
> +
> + ix = tlmi_populate_string_buffer(start, cur_pwdlen, setting->password);
> + if (ix < 0)
> + return ix;
> + start += ix;
> + ix = tlmi_populate_string_buffer(start, pwdlen, new_pwd);
> + if (ix < 0)
> + return ix;
> +
> + ret = tlmi_request(td->auth_wdev, 0, SET_PASSWORD, &wmibuf);
> + return ret ?: count;
> +}
> +
> +static struct kobj_attribute auth_new_password = __ATTR_WO(new_password);
> +
> +static ssize_t min_password_length_show(struct kobject *kobj, struct kobj_attribute *attr,
> + char *buf)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> +
> + return sysfs_emit(buf, "%d\n", setting->minlen);
> +}
> +
> +static struct kobj_attribute auth_min_pass_length = __ATTR_RO(min_password_length);
> +
> +static ssize_t max_password_length_show(struct kobject *kobj, struct kobj_attribute *attr,
> + char *buf)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> +
> + return sysfs_emit(buf, "%d\n", setting->maxlen);
> +}
> +
> +static struct kobj_attribute auth_max_pass_length = __ATTR_RO(max_password_length);
> +
> +static ssize_t mechanism_show(struct kobject *kobj, struct kobj_attribute *attr,
> + char *buf)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> +
> + if (setting->cert_installed)
> + return sysfs_emit(buf, "certificate\n");
> + return sysfs_emit(buf, "password\n");
> +}
> +
> +static struct kobj_attribute auth_mechanism = __ATTR_RO(mechanism);
> +
> +static ssize_t role_show(struct kobject *kobj, struct kobj_attribute *attr,
> + char *buf)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> +
> + return sysfs_emit(buf, "%s\n", setting->role);
> +}
> +
> +static struct kobj_attribute auth_role = __ATTR_RO(role);
> +
> +static ssize_t index_show(struct kobject *kobj, struct kobj_attribute *attr,
> + char *buf)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> +
> + return sysfs_emit(buf, "%d\n", setting->index);
> +}
> +
> +static ssize_t index_store(struct kobject *kobj,
> + struct kobj_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> + struct think_lmi *td = dev_get_drvdata(&setting->wdev->dev);
> + int err, val, max;
> +
> + err = kstrtoint(buf, 10, &val);
> + if (err < 0)
> + return err;
> +
> + if (setting == td->pwd_hdd)
> + max = MAX_HDD_INDEX;
> + else if (setting == td->pwd_nvme)
> + max = MAX_NVME_INDEX;
> + else
> + return -EOPNOTSUPP;
> +
> + if (val <= 0 || val > max)
> + return -EINVAL;
> +
> + guard(mutex)(&td->tlmi_auth_lock);
> + setting->index = val;
> + return count;
> +}
> +
> +static struct kobj_attribute auth_index = __ATTR_RW(index);
> +
> +static ssize_t level_show(struct kobject *kobj, struct kobj_attribute *attr,
> + char *buf)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> +
> + return sysfs_emit(buf, "%s\n", level_options[setting->level]);
> +}
> +
> +static ssize_t level_store(struct kobject *kobj,
> + struct kobj_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> + struct think_lmi *td = dev_get_drvdata(&setting->wdev->dev);
> + int i;
> +
> + /* Scan for a matching profile */
> + i = sysfs_match_string(level_options, buf);
> + if (i < 0)
> + return -EINVAL;
> +
> + guard(mutex)(&td->tlmi_auth_lock);
> + setting->level = i;
> + return count;
> +}
> +
> +static struct kobj_attribute auth_level = __ATTR_RW(level);
> +
> +static char *thumbtypes[] = {"SHA1", "SHA256", "SHA384", "SHA512", "MD5"};
> +#define MAX_SHA_LEN 513
> +
> +static ssize_t cert_thumbprint(char *buf, char *type, unsigned char alg, int *count,
> + struct think_lmi *td)
> +{
> + struct wmi_buffer wmibuf, wmibuf_thumb;
> + int ix, ret;
> + char *start;
> +
> + if (!type)
> + return -EINVAL;
> +
> + wmibuf.length = struct_size_t(struct wmi_string, chars, strlen(type) + 2);
> + char *wmidata __free(kfree) = kzalloc(wmibuf.length, GFP_KERNEL);
> + if (!wmidata)
> + return -ENOMEM;
> + wmibuf.data = wmidata;
> +
> + char *thumbstr __free(kfree) = kzalloc(MAX_SHA_LEN, GFP_KERNEL);
> + if (!thumbstr)
> + return -ENOMEM;
> +
> + start = wmibuf.data;
> + ix = tlmi_populate_string_buffer(start, wmibuf.length, type);
> + if (ix < 0)
> + return ix;
> + start += ix;
> + *start = alg;
> +
> + ret = wmidev_invoke_method(td->auth_wdev, 0, CERT_THUMBPRINT, &wmibuf, &wmibuf_thumb,
> + sizeof(struct wmi_string));
> + if (ret < 0)
> + return ret;
> +
> + if (wmibuf_thumb.data) {
> + /* Check for valid string */
> + if (wmibuf_thumb.length < 2 || wmibuf_thumb.length > MAX_SHA_LEN * 2)
> + ret = -EINVAL;
> + else
> + /* Extract thumbprint */
> + ret = wmi_string_to_utf8s((struct wmi_string *)wmibuf_thumb.data,
> + thumbstr, MAX_SHA_LEN);
> + kfree(wmibuf_thumb.data);
> + if (ret > 0)
> + *count += sysfs_emit_at(buf, *count, "%s : %s\n",
> + thumbtypes[alg], thumbstr);
> + }
> +
> + return ret;
> +}
> +
> +static ssize_t certificate_thumbprint_show(struct kobject *kobj, struct kobj_attribute *attr,
> + char *buf)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> + struct think_lmi *td = dev_get_drvdata(&setting->wdev->dev);
> + unsigned int i;
> + int ret, count = 0;
> +
> + if (!setting->cert_installed)
> + return -EOPNOTSUPP;
> +
> + guard(mutex)(&td->tlmi_auth_lock);
> +
> + for (i = 0; i < ARRAY_SIZE(thumbtypes); i++) {
> + ret = cert_thumbprint(buf, setting == td->pwd_admin ? "SVC" : "SMC",
> + i, &count, td);
> + if (ret < 0)
> + return ret;
> + }
> +
> + return count;
> +}
> +
> +static struct kobj_attribute auth_cert_thumb = __ATTR_RO(certificate_thumbprint);
> +
> +static ssize_t cert_to_password_store(struct kobject *kobj,
> + struct kobj_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> + struct think_lmi *td = dev_get_drvdata(&setting->wdev->dev);
> + size_t typelen, pwdlen, siglen;
> + char *start, *cert_type;
> + struct wmi_buffer wmibuf;
> + int ret = -EINVAL;
> + int ix;
> +
> + if (!capable(CAP_SYS_ADMIN))
> + return -EPERM;
> +
> + if (!setting->cert_installed)
> + return -EINVAL;
> +
> + guard(mutex)(&td->tlmi_auth_lock);
> +
> + if (!setting->signature || !setting->signature[0])
> + return -EACCES;
> +
> + /* Strip out CR if one is present */
> + char *passwd __free(kfree_sensitive) = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> + if (!passwd)
> + return -ENOMEM;
> +
> + cert_type = (setting == td->pwd_admin) ? "SVC" : "SMC";
> + typelen = struct_size_t(struct wmi_string, chars, strlen(cert_type) + 1);
> + pwdlen = struct_size_t(struct wmi_string, chars, strlen(passwd) + 1);
> + siglen = struct_size_t(struct wmi_string, chars, strlen(setting->signature) + 1);
> + wmibuf.length = typelen + pwdlen + siglen;
> +
> + char *wmidata __free(kfree_sensitive) = kzalloc(wmibuf.length, GFP_KERNEL);
> + if (!wmidata)
> + return -ENOMEM;
> + wmibuf.data = wmidata;
> +
> + start = wmibuf.data;
> + ix = tlmi_populate_string_buffer(start, typelen, cert_type);
> + if (ix < 0)
> + return ix;
> +
> + start += ix;
> + ix = tlmi_populate_string_buffer(start, pwdlen, passwd);
> + if (ix < 0)
> + return ix;
> +
> + start += ix;
> + ix = tlmi_populate_string_buffer(start, siglen, setting->signature);
> + if (ix < 0)
> + return ix;
> +
> + ret = tlmi_request(td->auth_wdev, 0, CERT_TO_PWD, &wmibuf);
> +
> + return ret ?: count;
> +}
> +
> +static struct kobj_attribute auth_cert_to_password = __ATTR_WO(cert_to_password);
> +
> +static ssize_t certificate_store(struct kobject *kobj,
> + struct kobj_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> + struct think_lmi *td = dev_get_drvdata(&setting->wdev->dev);
> + struct tlmi_pwd_setting *pwd_admin = td->pwd_admin;
> + size_t typelen, certlen, pwdlen, siglen, seriallen;
> + struct wmi_buffer wmibuf;
> + char *start;
> + char *signature, *cert_type;
> + int install_mode = SET_CERT;
> + const char *serial;
> + int ix = 0;
> + int ret;
> +
> + char *new_cert __free(kfree_sensitive) = NULL;
> +
> + if (!capable(CAP_SYS_ADMIN))
> + return -EPERM;
> +
> + guard(mutex)(&td->tlmi_auth_lock);
> +
> + cert_type = (setting == pwd_admin) ? "SVC" : "SMC";
> + typelen = struct_size_t(struct wmi_string, chars, strlen(cert_type) + 1);
> +
> + if ((buf[0] == '\0') || (buf[0] == '\n')) {
> + /*
> + * Clear installed certificate
> + * Format: 'Type, serial#, signature'
> + */
> +
> + /* Check that signature is set */
> + if (!setting->signature || !setting->signature[0])
> + return -EACCES;
> +
> + install_mode = CLEAR_CERT;
> + serial = dmi_get_system_info(DMI_PRODUCT_SERIAL);
> + if (!serial)
> + return -ENODEV;
> + seriallen = struct_size_t(struct wmi_string, chars, strlen(serial) + 1);
> + signature = setting->signature;
> + siglen = struct_size_t(struct wmi_string, chars, strlen(signature) + 1);
> + wmibuf.length = typelen + seriallen + siglen;
> + } else {
> + /* New install, or update */
> + /* Strip out CR from certificate if one is present */
> + new_cert = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> + if (!new_cert)
> + return -ENOMEM;
> + certlen = struct_size_t(struct wmi_string, chars, strlen(new_cert) + 1);
> +
> + if (setting->cert_installed) {
> + /*
> + * Update installed certificate
> + * Format: 'Type, Certificate, signature'
> + */
> + install_mode = UPDATE_CERT;
> + /* If admin account enabled - need to use its signature */
> + if (pwd_admin && pwd_admin->pwd_enabled)
> + signature = pwd_admin->signature;
> + else
> + signature = setting->signature;
> + if (!signature)
> + return -EINVAL;
> +
> + siglen = struct_size_t(struct wmi_string, chars, strlen(signature) + 1);
> + wmibuf.length = typelen + certlen + siglen;
> + } else {
> + /*
> + * Install a new certificate
> + * Format: 'Type, Certificate, Password'
> + */
> + install_mode = SET_CERT;
> + /* To set admin cert, a password must be enabled */
> + if (setting == pwd_admin &&
> + (!setting->pwd_enabled || !setting->password[0]))
> + return -EACCES;
> +
> + pwdlen = struct_size_t(struct wmi_string, chars,
> + strlen(setting->password) + 1);
> + wmibuf.length = typelen + certlen + pwdlen;
> + }
> + }
> +
> + /* Set up WMI buffer */
> + char *wmidata __free(kfree_sensitive) = kzalloc(wmibuf.length, GFP_KERNEL);
> + if (!wmidata)
> + return -ENOMEM;
> + wmibuf.data = wmidata;
> +
> + start = wmibuf.data;
> + ix = tlmi_populate_string_buffer(start, typelen, cert_type);
> + if (ix < 0)
> + return ix;
> +
> + start += ix;
> + if (install_mode == CLEAR_CERT)
> + ix = tlmi_populate_string_buffer(start, seriallen, serial);
> + else
> + ix = tlmi_populate_string_buffer(start, certlen, new_cert);
> + if (ix < 0)
> + return ix;
> +
> + start += ix;
> + if (install_mode == SET_CERT)
> + ix = tlmi_populate_string_buffer(start, pwdlen, setting->password);
> + else
> + ix = tlmi_populate_string_buffer(start, siglen, signature);
> + if (ix < 0)
> + return ix;
> +
> + ret = tlmi_request(td->auth_wdev, 0, install_mode, &wmibuf);
> +
> + return ret ?: count;
> +}
> +
> +static struct kobj_attribute auth_certificate = __ATTR_WO(certificate);
> +
> +static ssize_t signature_store(struct kobject *kobj,
> + struct kobj_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> + struct think_lmi *td = dev_get_drvdata(&setting->wdev->dev);
> + char *new_signature;
> +
> + if (!capable(CAP_SYS_ADMIN))
> + return -EPERM;
> +
> + guard(mutex)(&td->tlmi_auth_lock);
> +
> + /* Strip out CR if one is present */
> + new_signature = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> + if (!new_signature)
> + return -ENOMEM;
> +
> + /* Free any previous signature */
> + kfree_sensitive(setting->signature);
> + setting->signature = new_signature;
> +
> + return count;
> +}
> +
> +static struct kobj_attribute auth_signature = __ATTR_WO(signature);
> +
> +static ssize_t save_signature_store(struct kobject *kobj,
> + struct kobj_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> + struct think_lmi *td = dev_get_drvdata(&setting->wdev->dev);
> + char *new_signature;
> +
> + if (!capable(CAP_SYS_ADMIN))
> + return -EPERM;
> +
> + guard(mutex)(&td->tlmi_auth_lock);
> +
> + /* Strip out CR if one is present */
> + new_signature = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> + if (!new_signature)
> + return -ENOMEM;
> +
> + /* Free any previous signature */
> + kfree_sensitive(setting->save_signature);
> + setting->save_signature = new_signature;
> +
> + return count;
> +}
> +
> +static struct kobj_attribute auth_save_signature = __ATTR_WO(save_signature);
> +
> +static umode_t auth_attr_is_visible(struct kobject *kobj,
> + struct attribute *attr, int n)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> + struct think_lmi *td = dev_get_drvdata(&setting->wdev->dev);
> +
> + /* We only want to display level and index settings on HDD/NVMe */
> + if (attr == &auth_index.attr || attr == &auth_level.attr) {
> + if (setting == td->pwd_hdd || setting == td->pwd_nvme)
> + return attr->mode;
> + return 0;
> + }
> +
> + return attr->mode;
> +}
> +
> +static struct attribute *auth_attrs[] = {
> + &auth_is_pass_set.attr,
> + &auth_min_pass_length.attr,
> + &auth_max_pass_length.attr,
> + &auth_current_password.attr,
> + &auth_new_password.attr,
> + &auth_role.attr,
> + &auth_mechanism.attr,
> + &auth_index.attr,
> + &auth_level.attr,
> + &auth_certificate.attr,
> + &auth_signature.attr,
> + &auth_save_signature.attr,
> + &auth_cert_thumb.attr,
> + &auth_cert_to_password.attr,
> + NULL
> +};
> +
> +static const struct attribute_group auth_attr_group = {
> + .is_visible = auth_attr_is_visible,
> + .attrs = auth_attrs,
> +};
> +__ATTRIBUTE_GROUPS(auth_attr);
> +
> +static void tlmi_pwd_setting_release(struct kobject *kobj)
> +{
> + struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
> +
> + kfree_sensitive(setting->save_signature);
> + kfree_sensitive(setting->signature);
> + kfree_sensitive(setting);
> +}
> +
> +static const struct kobj_type tlmi_pwd_setting_ktype = {
> + .release = &tlmi_pwd_setting_release,
> + .sysfs_ops = &kobj_sysfs_ops,
> + .default_groups = auth_attr_groups,
> +};
> +
> +static void tlmi_auth_sysfs_release(struct think_lmi *td)
> +{
> + struct kobject *pos, *n;
> +
> + if (!td || !td->authentication_kset)
> + return;
> +
> + /* Authentication structures */
> + list_for_each_entry_safe(pos, n, &td->authentication_kset->list, entry)
> + kobject_put(pos);
> +
> + kset_unregister(td->authentication_kset);
> +
> + /* Prevent dangling pointers */
> + td->authentication_kset = NULL;
> + td->pwd_admin = NULL;
> + td->pwd_system = NULL;
> + td->pwd_power = NULL;
> + td->pwd_hdd = NULL;
> + td->pwd_nvme = NULL;
> +}
> +
> +static int tlmi_auth_sysfs_init(struct think_lmi *td)
> +{
> + int ret;
> +
> + if (!td)
> + return -EINVAL;
> +
> + /* Create authentication entries */
> + td->authentication_kset = kset_create_and_add("authentication", NULL,
> + &td->class_dev->kobj);
> + if (!td->authentication_kset) {
> + ret = -ENOMEM;
> + goto out_admin_fail;
> + }
> +
> + td->pwd_admin->kobj.kset = td->authentication_kset;
> + ret = kobject_init_and_add(&td->pwd_admin->kobj, &tlmi_pwd_setting_ktype,
> + NULL, "%s", "Admin");
> + if (ret)
> + goto out_admin_fail;
> +
> + td->pwd_power->kobj.kset = td->authentication_kset;
> + ret = kobject_init_and_add(&td->pwd_power->kobj, &tlmi_pwd_setting_ktype,
> + NULL, "%s", "Power-on");
> + if (ret)
> + goto out_power_fail;
> +
> + td->pwd_system->kobj.kset = td->authentication_kset;
> + ret = kobject_init_and_add(&td->pwd_system->kobj, &tlmi_pwd_setting_ktype,
> + NULL, "%s", "System");
> + if (ret)
> + goto out_system_fail;
> +
> + td->pwd_hdd->kobj.kset = td->authentication_kset;
> + ret = kobject_init_and_add(&td->pwd_hdd->kobj, &tlmi_pwd_setting_ktype,
> + NULL, "%s", "HDD");
> + if (ret)
> + goto out_hdd_fail;
> +
> + td->pwd_nvme->kobj.kset = td->authentication_kset;
> + ret = kobject_init_and_add(&td->pwd_nvme->kobj, &tlmi_pwd_setting_ktype,
> + NULL, "%s", "NVMe");
> + if (ret)
> + goto out_nvme_fail;
> +
> + return 0;
> +
> +out_admin_fail:
> + kobject_put(&td->pwd_admin->kobj);
> + td->pwd_admin = NULL;
> +out_power_fail:
> + kobject_put(&td->pwd_power->kobj);
> + td->pwd_power = NULL;
> +out_system_fail:
> + kobject_put(&td->pwd_system->kobj);
> + td->pwd_system = NULL;
> +out_hdd_fail:
> + kobject_put(&td->pwd_hdd->kobj);
> + td->pwd_hdd = NULL;
> +out_nvme_fail:
> + kobject_put(&td->pwd_nvme->kobj);
> + td->pwd_nvme = NULL;
> + return ret;
> +}
> +
> +static int tlmi_create_auth(struct think_lmi *td,
> + const char *pwd_role,
> + struct tlmi_pwd_setting **setting)
> +{
> + struct tlmi_pwd_setting *new_pwd;
> +
> + new_pwd = kzalloc_obj(*new_pwd);
> + if (!new_pwd)
> + return -ENOMEM;
> +
> + new_pwd->role = pwd_role;
> + new_pwd->minlen = td->pwdcfg.core.min_length;
> + new_pwd->maxlen = td->pwdcfg.core.max_length;
> + new_pwd->index = 1;
> + new_pwd->level = TLMI_LEVEL_MASTER;
> + new_pwd->wdev = td->auth_wdev;
> +
> + *setting = new_pwd;
> + return 0;
> +}
> +
> +static int tlmi_auth_setup(struct think_lmi *td)
> +{
> + struct tlmi_pwd_setting *admin __free(kfree) = NULL;
> + struct tlmi_pwd_setting *power __free(kfree) = NULL;
> + struct tlmi_pwd_setting *system __free(kfree) = NULL;
> + struct tlmi_pwd_setting *hdd __free(kfree) = NULL;
> + struct tlmi_pwd_setting *nvme __free(kfree) = NULL;
> + unsigned int index;
> + int ret;
> +
> + if (!td)
> + return -EINVAL;
> +
> + ret = tlmi_create_auth(td, "bios-admin", &admin);
> + if (ret)
> + return ret;
> +
> + ret = tlmi_create_auth(td, "power-on", &power);
> + if (ret)
> + return ret;
> +
> + ret = tlmi_create_auth(td, "system", &system);
> + if (ret)
> + return ret;
> +
> + ret = tlmi_create_auth(td, "hdd", &hdd);
> + if (ret)
> + return ret;
> +
> + ret = tlmi_create_auth(td, "nvme", &nvme);
> + if (ret)
> + return ret;
> +
> + td->pwd_admin = no_free_ptr(admin);
> + td->pwd_power = no_free_ptr(power);
> + td->pwd_system = no_free_ptr(system);
> + td->pwd_hdd = no_free_ptr(hdd);
> + td->pwd_nvme = no_free_ptr(nvme);
> +
> + if (td->pwdcfg.core.password_state & TLMI_SVP_PWD)
> + td->pwd_admin->pwd_enabled = true;
> +
> + if (td->pwdcfg.core.password_state & TLMI_POP_PWD)
> + td->pwd_power->pwd_enabled = true;
> +
> + if (td->pwdcfg.core.password_state & TLMI_SMP_PWD)
> + td->pwd_system->pwd_enabled = true;
> +
> + if (td->pwdcfg.core.password_state & TLMI_SVC_PWD)
> + td->pwd_admin->cert_installed = true;
> + if (td->pwdcfg.core.password_state & TLMI_SMC_PWD)
> + td->pwd_system->cert_installed = true;
> +
> + if (td->pwdcfg.ext.hdd_user_password ||
> + td->pwdcfg.ext.hdd_master_password) {
> + td->pwd_hdd->pwd_enabled = true;
> + if (td->pwdcfg.ext.hdd_master_password)
> + index = ffs(td->pwdcfg.ext.hdd_master_password);
> + else
> + index = ffs(td->pwdcfg.ext.hdd_user_password);
> + /* Sanitize result */
> + if (index > MAX_HDD_INDEX || !index) {
> + pr_info("tlmi-auth: Invalid HDD password index %u. Setting to %d\n",
> + index, MAX_HDD_INDEX);
> + index = MAX_HDD_INDEX;
> + }
> + td->pwd_hdd->index = index;
> + }
> +
> + if (td->pwdcfg.ext.nvme_user_password ||
> + td->pwdcfg.ext.nvme_master_password) {
> + td->pwd_nvme->pwd_enabled = true;
> + if (td->pwdcfg.ext.nvme_master_password)
> + index = ffs(td->pwdcfg.ext.nvme_master_password);
> + else
> + index = ffs(td->pwdcfg.ext.nvme_user_password);
> + /* Sanitize result */
> + if (index > MAX_NVME_INDEX || !index) {
> + pr_info("tlmi-auth: Invalid NVME password index %d. Setting to %d\n",
> + index, MAX_NVME_INDEX);
> + index = MAX_NVME_INDEX;
> + }
> + td->pwd_nvme->index = index;
> + }
> +
> + mutex_init(&td->tlmi_auth_lock);
> +
> + return ret;
> +}
> +
> +static int tlmi_auth_component_bind(struct device *auth_dev, struct device *config_dev,
> + void *d)
> +{
> + struct think_lmi *td = d;
> + int ret;
> +
> + td->auth_wdev = to_wmi_device(auth_dev);
> + dev_set_drvdata(&td->auth_wdev->dev, td);
> +
> + ret = tlmi_auth_setup(td);
> + if (ret)
> + return ret;
> +
> + ret = tlmi_auth_sysfs_init(td);
> + if (ret)
> + tlmi_auth_sysfs_release(td);
> + return ret;
> +}
> +
> +static void tlmi_auth_component_unbind(struct device *auth_dev, struct device *cfg_dev, void *d)
> +{
> + struct think_lmi *td = d;
> +
> + tlmi_auth_sysfs_release(td);
> +}
> +
> +static const struct component_ops tlmi_auth_component_ops = {
> + .bind = tlmi_auth_component_bind,
> + .unbind = tlmi_auth_component_unbind,
> +};
> +
> +static int tlmi_auth_probe(struct wmi_device *wdev, const void *context)
> +{
> + return component_add(&wdev->dev, &tlmi_auth_component_ops);
> +}
> +
> +static void tlmi_auth_remove(struct wmi_device *wdev)
> +{
> + component_del(&wdev->dev, &tlmi_auth_component_ops);
> +}
> +
> +static const struct wmi_device_id tlmi_auth_id_table[] = {
> + { TLMI_AUTHENTICATION_GUID, NULL },
> + {}
> +};
> +
> +static struct wmi_driver tlmi_auth_driver = {
> + .driver = {
> + .name = "tlmi-auth",
> + .probe_type = PROBE_PREFER_ASYNCHRONOUS,
> + },
> + .id_table = tlmi_auth_id_table,
> + .probe = tlmi_auth_probe,
> + .remove = tlmi_auth_remove,
> + .no_singleton = true,
> +};
> +
> +static int tlmi_auth_match(struct device *dev, void *type)
> +{
> + return (dev->driver == &tlmi_auth_driver.driver);
> +}
> +
> +void tlmi_auth_match_add(struct device *master, struct component_match **matchptr)
> +{
> + component_match_add(master, matchptr, tlmi_auth_match, NULL);
> +}
> +EXPORT_SYMBOL_NS_GPL(tlmi_auth_match_add, "TLMI-AUTH");
> +
> +module_wmi_driver(tlmi_auth_driver);
> +
> +MODULE_IMPORT_NS("LENOVO_WMI_HELPERS");
> +MODULE_DEVICE_TABLE(wmi, tlmi_auth_id_table);
> +MODULE_AUTHOR("Mark Pearson <mpearson-lenovo@xxxxxxxxx>");
> +MODULE_DESCRIPTION("ThinkLMI Authentication WMI Driver");
> +MODULE_LICENSE("GPL");
> diff --git a/drivers/platform/x86/lenovo/tlmi-config.c b/drivers/platform/x86/lenovo/tlmi-config.c
> index bb9c5f720c24..702e6754fcb4 100644
> --- a/drivers/platform/x86/lenovo/tlmi-config.c
> +++ b/drivers/platform/x86/lenovo/tlmi-config.c
> @@ -50,7 +50,7 @@ static ssize_t common_current_value_store(char *name, const char *buf, int wmi_i
> {
> char *new_setting __free(kfree) = NULL;
> char *wmidata __free(kfree) = NULL;
> - size_t namelen, valuelen;
> + size_t namelen, valuelen, siglen;
> struct wmi_buffer wmibuf;
> char *start;
> int ix = 0;
> @@ -64,10 +64,17 @@ static ssize_t common_current_value_store(char *name, const char *buf, int wmi_i
> if (!new_setting)
> return -ENOMEM;
>
> + guard(mutex)(&td->tlmi_auth_lock);
> +
> /* Calculate and alloc buffer for command string */
> namelen = struct_size_t(struct wmi_string, chars, strlen(name) + 1);
> valuelen = struct_size_t(struct wmi_string, chars, strlen(new_setting) + 1);
> - wmibuf.length = namelen + valuelen;
> + if (td->pwd_admin && td->pwd_admin->signature)
> + siglen = struct_size_t(struct wmi_string, chars,
> + strlen(td->pwd_admin->signature) + 1);
> + else
> + siglen = struct_size_t(struct wmi_string, chars, 1 + 1);
> + wmibuf.length = namelen + valuelen + siglen;
> wmidata = kzalloc(wmibuf.length, GFP_KERNEL);
> wmibuf.data = wmidata;
> if (!wmibuf.data)
> @@ -83,6 +90,14 @@ static ssize_t common_current_value_store(char *name, const char *buf, int wmi_i
> ix = tlmi_populate_string_buffer(start, valuelen, new_setting);
> if (ix < 0)
> return ix;
> + start += ix;
> + if (td->pwd_admin && td->pwd_admin->signature)
> + ix = tlmi_populate_string_buffer(start, siglen,
> + td->pwd_admin->signature);
> + else
> + ix = tlmi_populate_string_buffer(start, siglen, "\0");
> + if (ix < 0)
> + return ix;
>
> return tlmi_request(td->config_wdev, 0, wmi_id, &wmibuf);
> }
> @@ -209,8 +224,10 @@ static ssize_t save_settings_store(struct kobject *kobj, struct kobj_attribute *
> const char *buf, size_t count)
> {
> struct think_lmi *td = dev_get_drvdata(kobj_to_dev(kobj->parent));
> + struct tlmi_pwd_setting *pwd_admin = td->pwd_admin;
> char *wmidata __free(kfree) = NULL;
> struct wmi_buffer wmibuf;
> + size_t siglen;
> int ret = 0;
>
> /* save command triggered by writing 'save' */
> @@ -224,13 +241,30 @@ static ssize_t save_settings_store(struct kobject *kobj, struct kobj_attribute *
> if (!td->save_required)
> return -ENOENT;
>
> - wmibuf.length = struct_size_t(struct wmi_string, chars, 1 + 1);
> + guard(mutex)(&td->tlmi_auth_lock);
> +
> + /* Authorisation (if needed) is done just before save */
> + ret = tlmi_auth_authenticate_user(td);
> + /* Ignore most errors. We might be in deployment mode */
> + if (ret == -ENOMEM)
> + return ret;
> +
> + if (td->pwd_admin && td->pwd_admin->save_signature)
> + siglen = struct_size_t(struct wmi_string, chars,
> + strlen(td->pwd_admin->save_signature) + 1);
> + else
> + siglen = struct_size_t(struct wmi_string, chars, 1 + 1);
> + wmibuf.length = siglen;
> wmidata = kzalloc(wmibuf.length, GFP_KERNEL);
> wmibuf.data = wmidata;
> if (!wmibuf.data)
> return -ENOMEM;
>
> - ret = tlmi_populate_string_buffer(wmibuf.data, 1, "\0");
> + if (pwd_admin && pwd_admin->save_signature)
> + ret = tlmi_populate_string_buffer(wmibuf.data, wmibuf.length,
> + pwd_admin->save_signature);
> + else
> + ret = tlmi_populate_string_buffer(wmibuf.data, siglen, "\0");
> if (ret < 0)
> return ret;
>
> @@ -359,16 +393,18 @@ static const struct component_master_ops aggregate_ops = {
> static int tlmi_config_probe(struct wmi_device *wdev, const void *context)
> {
> struct component_match *match = NULL;
> - guid_t tlmi_attr_guid, tlmi_authdata_guid;
> + guid_t tlmi_attr_guid, tlmi_authdata_guid, tlmi_auth_guid;
> struct think_lmi *td;
> int ret;
>
> guid_parse(TLMI_ATTRIBUTE_GUID, &tlmi_attr_guid);
> guid_parse(TLMI_AUTHENTICATION_DATA_GUID, &tlmi_authdata_guid);
> + guid_parse(TLMI_AUTHENTICATION_GUID, &tlmi_auth_guid);
>
> /* At minimum need to support BIOS and authentication settings */
> if (!wmidev_exists(&tlmi_attr_guid) ||
> - !wmidev_exists(&tlmi_authdata_guid))
> + !wmidev_exists(&tlmi_authdata_guid) ||
> + !wmidev_exists(&tlmi_auth_guid))
> return -EOPNOTSUPP;
>
> td = devm_kzalloc(&wdev->dev, sizeof(struct think_lmi), GFP_KERNEL);
> @@ -387,6 +423,7 @@ static int tlmi_config_probe(struct wmi_device *wdev, const void *context)
> /* Register the other components */
> tlmi_attr_match_add(&wdev->dev, &match);
> tlmi_authdata_match_add(&wdev->dev, &match);
> + tlmi_auth_match_add(&wdev->dev, &match);
>
> ret = component_master_add_with_match(&wdev->dev, &aggregate_ops, match);
> if (ret)
> @@ -424,6 +461,7 @@ module_wmi_driver(tlmi_config_driver);
> MODULE_IMPORT_NS("LENOVO_WMI_HELPERS");
> MODULE_IMPORT_NS("TLMI-ATTR");
> MODULE_IMPORT_NS("TLMI-AUTHDATA");
> +MODULE_IMPORT_NS("TLMI-AUTH");
> MODULE_DEVICE_TABLE(wmi, tlmi_config_id_table);
> MODULE_AUTHOR("Mark Pearson <mpearson-lenovo@xxxxxxxxx>");
> MODULE_DESCRIPTION("TLMI Config WMI Driver");