From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.182.112 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796392712; b=YZqALQkJpnA+GgG2SKg0vkdKvKu+joeQQDPXMu9Zz6GyKfEylz+B5kwb4NYDLID926Y3 kNSb4bXPV5cQ0ZyfeFB7ETmRaBKZeEvjv+MLQyrY4vE2y2sbQAhzvTbctKW3gx8uGDI2w WXhnI9DE5HAjNO+Qt9uBvpe5hzodX1AqI2LHW2WAPrIOSxublaH0IwHmI85mcu6W6aEgh f/06NBqijILCiiHHEIl37drNXf6nXe5PcyD78TmdNwfaySUQZxCmASYZZh0i7AM8Yiy3K nQ5dnSsXu1XDevioSEqCjxVgPXCjm8c1TbkQUolVvqaPnKzTHxL/bhVb7ylxaJY7Ytg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796392712; h=DMARC-Filter:DKIM-Signature:Received:Message-ID:Subject:From:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date: MIME-Version:User-Agent:X-ZohoMailClient; bh=WbmSnWIUHO+ukelXu5g2J42jS2/KcBoRqHqj0NTCaPE=; b=Lw3Mryuc0kFfNucodDvTmNcAWps3FO4qirIxgNeHjJOmVzgeBOtHas29jdwrS218gcXa v4f/z4dmA8xxMPkw0GSqqocdLX0TQgwPLWStiI5D0V2Bf/eDCQ9JjFnGGBuBh8Yek3ef9 71/qvHo0poUyA8T3WnSyCjkFJvZBG4z/fHGjM+ZfYXi+CnuOOYTpAwVDSpvJ9IC2SBSwf mhl0WWNNOebsVaQsvgAqEVwrlrb1c3XTLnuaiKoGEUCoFVXW58ydTeK8m5gxFsf17f1bW fkEyEMQzNAgsQNqGOeOe5ACwdkhhDi5b7RyqpuAC71DP5q0TgudhNuCci+cxgfNgs1g== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass header.d=rong.moe header.i=@rong.moe header.a=rsa-sha256 header.s=zmail2048 header.b=V0ikuYBA; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.182.112 Received: from sender5-pp-f112.zoho.com (sender5-pp-f112.zoho.com [165.173.182.112]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB67D606A322; Fri, 4 Dec 2026 13:58:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EB67D606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rong.moe header.i=@rong.moe header.a=rsa-sha256 header.s=zmail2048 header.b=V0ikuYBA ARC-Seal: i=1; a=rsa-sha256; t=1796392692; cv=none; d=zohomail.com; s=zohoarc; b=C63H/UQjV3k2so4CRSsBkdM+alOBTWLk8uMXhtuwFrxjv9a+BWhoXFDqYpKuqycKX9pUxwIXtwiD+JbtuOXPu/eQSUyYa3wvdMLh7mjOc6oYFuqM0kf+/20BaEtx1k0tYRR7Nf5XkCK6ub1RKw9fMBXx5XmWof/rphhYJVHuZww= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1796392692; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=WbmSnWIUHO+ukelXu5g2J42jS2/KcBoRqHqj0NTCaPE=; b=TtsPP6t3+jDDZwlt+05LlS21BNipwVvEptUBLnWOxV3dzTB5kEKGKoJEDimK2rT+HvAWW8Cl4S+h/gN9OwFAIU1i3OMqPMjyL/Z09ZXOeAGjwzjNldgNP4CTeBOlhq/wLwdZe7N/rrdZrADHZ54KldF0s1hGi3MsepcUfpJP2ew= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: a=rsa-sha256; b=V0ikuYBA7za8DXxDPbC+WUeoTiwNZ5IPkKLhKpvDgUXnDLlXDGPaHhvYDDQlbwdlX1aZA+cbvnHtRoUtUGzCPdi6oXEJtjQ9H8TxRFHAR64r3iyCyXyycDjP6KAYwZ4TgMAX8h1vgu55Oye8W+7xbluypoAYYye0aEvnehnGmsdv2fh8GmnlddVyP0srblIUdvGJvWytKXNcf+S9C9UNl00xjpRNxv/Gc9NJScH/sdS3OdrAR03YelBaNucN89tU4F9kiU1pha2Xtc1m7nRviw6rRVX6RZGQTgQeuw3G8nQg7iSxzqebVraHkgetYMiqPqh4HYjP1ydqu/S5d5NigQ==; c=relaxed/relaxed; s=zmail2048; d=rong.moe; v=1; bh=WbmSnWIUHO+ukelXu5g2J42jS2/KcBoRqHqj0NTCaPE=; h=Content-Description:Content-ID:Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Resent-Sender:Resent-Cc:Resent-Date:Resent-To:Reply-To:Resent-Message-ID:Resent-From:Sender:Subject:To:Date:From:Message-Id:Reply-To:Subject:To:Cc; Received: by smtp.zohomail.com with SMTPS id 1796392691294926.1582350100296; Fri, 4 Dec 2026 05:58:11 -0800 (PST) Message-ID: Subject: Re: [PATCH 09/13] platform/x86: lenovo: tlmi: Add authentication sysfs entries From: Rong Zhang To: Mark Pearson Cc: derekjohn.clark@gmail.com, hansg@kernel.org, ilpo.jarvinen@linux.intel.com, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20261204022032.3074618-10-mpearson-lenovo@squebb.ca> References: <20261204022032.3074618-1-mpearson-lenovo@squebb.ca> <20261204022032.3074618-10-mpearson-lenovo@squebb.ca> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 21:52:13 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Evolution 3.62.2-1 X-ZohoMailClient: External 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. >=20 > Enables ability to configure admin, system, power-on, hdd and nvme > passwords. > Enables certificate support. >=20 > Signed-off-by: Mark Pearson > --- > 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 >=20 > 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)=09+=3D ideapad-laptop.o > obj-$(CONFIG_THINKPAD_LMI)=09+=3D think-lmi.o > -obj-$(CONFIG_THINKPAD_LMI)=09+=3D tlmi-config.o tlmi-attr.o tlmi-authdat= a.o > +obj-$(CONFIG_THINKPAD_LMI)=09+=3D tlmi-config.o tlmi-attr.o tlmi-authdat= a.o tlmi-auth.o > obj-$(CONFIG_THINKPAD_ACPI)=09+=3D thinkpad_acpi.o > =20 > lenovo-target-$(CONFIG_LENOVO_WMI_HOTKEY_UTILITIES)=09+=3D wmi-hotkey-ut= ilities.o > diff --git a/drivers/platform/x86/lenovo/think-lmi.h b/drivers/platform/x= 86/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 > =20 > #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-5f7cb74a3= b4e" > =20 > /* Possible error values */ > @@ -95,6 +96,7 @@ struct tlmi_pwdcfg { > /* password setting details */ > struct tlmi_pwd_setting { > =09struct kobject kobj; > +=09struct wmi_device *wdev; > =09bool pwd_enabled; > =09char password[TLMI_PWD_BUFSIZE]; > =09const char *pwd_type; > @@ -146,6 +148,7 @@ struct think_lmi { > =09struct kset *attribute_kset; > =09struct kset *authentication_kset; > =09struct wmi_device *config_wdev; > +=09struct wmi_device *auth_wdev; > =20 > =09struct tlmi_pwdcfg pwdcfg; > =09struct tlmi_pwd_setting *pwd_admin; > @@ -153,6 +156,7 @@ struct think_lmi { > =09struct tlmi_pwd_setting *pwd_system; > =09struct tlmi_pwd_setting *pwd_hdd; > =09struct tlmi_pwd_setting *pwd_nvme; > +=09struct mutex tlmi_auth_lock; /* shared between auth and config */ > =20 > =09const 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_mat= ch **matchptr); > +void tlmi_auth_match_add(struct device *master, struct component_match *= *matchptr); > +int tlmi_auth_authenticate_user(struct think_lmi *td); > =20 > #endif /* !_THINK_LMI_H_ */ > diff --git a/drivers/platform/x86/lenovo/tlmi-auth.c b/drivers/platform/x= 86/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 > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include "think-lmi.h" > +#include "wmi-helpers.h" > + > +#define MAX_HDD_INDEX 4 > +#define MAX_NVME_INDEX 8 > + > +enum pwd_type_v2 { > +=09SVP =3D 0, > +=09SMP =3D 1, > +=09POP =3D 2, > +=09HDD =3D 3, /* 3 to 6 */ > +=09NVME =3D HDD + MAX_HDD_INDEX, /* 7 to 14 */ > +=09MASTERHDD =3D NVME + MAX_NVME_INDEX, /* 15 to 18 */ > +=09MASTERNVME =3D 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[] =3D { > +=09[TLMI_LEVEL_USER] =3D "user", > +=09[TLMI_LEVEL_MASTER] =3D "master", > +}; > + > +int tlmi_auth_authenticate_user(struct think_lmi *td) > +{ > +=09struct wmi_buffer wmibuf; > +=09char *password; > +=09int ret; > + > +=09lockdep_assert_held(&td->tlmi_auth_lock); > + > +=09/* If certificate used, we use signatures not passwords*/ > +=09if (td->pwd_admin->cert_installed || > +=09 td->pwd_system->cert_installed) > +=09=09return 0; > + > +=09/* If admin password configured, use that. If not try system */ > +=09if (strlen(td->pwd_admin->password) > 0) > +=09=09password =3D td->pwd_admin->password; > +=09else if (strlen(td->pwd_system->password) > 0) > +=09=09password =3D td->pwd_system->password; > +=09else > +=09=09/* No password configured, so no need to run authentication */ > +=09=09return 0; > + > +=09wmibuf.length =3D struct_size_t(struct wmi_string, chars, strlen(pass= word) + 1); > +=09char *wmidata __free(kfree_sensitive) =3D kzalloc(wmibuf.length, GFP_= KERNEL); > +=09if (!wmidata) > +=09=09return -ENOMEM; > +=09wmibuf.data =3D wmidata; > + > +=09ret =3D tlmi_populate_string_buffer(wmibuf.data, wmibuf.length, passw= ord); > +=09if (ret < 0) > +=09=09return ret; > + > +=09return 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 kobjec= t *kobj) > +{ > +=09return container_of(kobj, struct tlmi_pwd_setting, kobj); > +} > + > +static ssize_t is_enabled_show(struct kobject *kobj, struct kobj_attribu= te *attr, > +=09=09=09 char *buf) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > + > +=09return sysfs_emit(buf, "%d\n", setting->pwd_enabled || setting->cert_= installed); > +} > + > +static struct kobj_attribute auth_is_pass_set =3D __ATTR_RO(is_enabled); > + > +static ssize_t current_password_store(struct kobject *kobj, > +=09=09=09=09 struct kobj_attribute *attr, > +=09=09=09=09 const char *buf, size_t count) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > +=09struct think_lmi *td =3D dev_get_drvdata(&setting->wdev->dev); > +=09size_t pwdlen; > + > +=09guard(mutex)(&td->tlmi_auth_lock); > + > +=09/* Strip newline; setting password won't work if one is present. */ > +=09pwdlen =3D strchrnul(buf, '\n') - buf; Only the last LF can be stripped. If you'd really want to handle LF in between, return -EINVAL. > +=09/* pwdlen =3D=3D 0 is allowed to clear the password */ > +=09if (pwdlen && (pwdlen < setting->minlen || pwdlen > setting->maxlen)) > +=09=09return -EINVAL; Could you factor out a helper function for the sanity checks of password string? You are replicating mostly the same code below. > + > +=09strscpy(setting->password, buf, pwdlen + 1); > +=09return count; > +} > + > +static struct kobj_attribute auth_current_password =3D __ATTR_WO(current= _password); > + > +static ssize_t new_password_store(struct kobject *kobj, > +=09=09=09=09 struct kobj_attribute *attr, > +=09=09=09=09 const char *buf, size_t count) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > +=09struct think_lmi *td =3D dev_get_drvdata(&setting->wdev->dev); > +=09enum pwd_type_v2 pwd_type; > +=09size_t pwdlen, cur_pwdlen; > +=09struct wmi_buffer wmibuf; > +=09char *start; > +=09int ix =3D 0; > +=09int ret; > + > +=09if (!capable(CAP_SYS_ADMIN)) > +=09=09return -EPERM; > + > +=09guard(mutex)(&td->tlmi_auth_lock); > + > +=09/* Authorisation needs to be done first */ > +=09ret =3D tlmi_auth_authenticate_user(td); > +=09/* Ignore most errors. We might be in deployment mode */ Could you add a dev_warn/info/dbg() when something is ignored? > +=09if (ret =3D=3D -ENOMEM) > +=09=09return ret; > + > +=09/* Strip out CR if one is present, setting password won't work if it = is present */ > +=09char *new_pwd __free(kfree_sensitive) =3D 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 > +=09if (!new_pwd) > +=09=09return -ENOMEM; > + > +=09pwdlen =3D strlen(new_pwd); > +=09/* pwdlen =3D=3D 0 is allowed to clear the password */ > +=09if (pwdlen && (pwdlen < setting->minlen || pwdlen > setting->maxlen)) > +=09=09return -EINVAL; > + > +=09cur_pwdlen =3D struct_size_t(struct wmi_string, chars, strlen(setting= ->password) + 1); > +=09pwdlen =3D struct_size_t(struct wmi_string, chars, strlen(new_pwd) + = 1); > +=09wmibuf.length =3D 2 + cur_pwdlen + pwdlen; > +=09char *wmidata __free(kfree_sensitive) =3D kzalloc(wmibuf.length, GFP_= KERNEL); > +=09if (!wmidata) > +=09=09return -ENOMEM; > +=09wmibuf.data =3D wmidata; > + > +=09if (setting =3D=3D td->pwd_admin) { > +=09=09pwd_type =3D SVP; > +=09} else if (setting =3D=3D td->pwd_power) { > +=09=09pwd_type =3D POP; > +=09} else if (setting =3D=3D td->pwd_system) { > +=09=09pwd_type =3D SMP; > +=09} else if (setting =3D=3D td->pwd_hdd) { > +=09=09if (setting->level =3D=3D TLMI_LEVEL_MASTER) > +=09=09=09pwd_type =3D MASTERHDD; > +=09=09else > +=09=09=09pwd_type =3D HDD; > +=09=09pwd_type +=3D setting->index - 1; > +=09} else if (setting =3D=3D td->pwd_nvme) { > +=09=09if (setting->level =3D=3D TLMI_LEVEL_MASTER) > +=09=09=09pwd_type =3D MASTERNVME; > +=09=09else > +=09=09=09pwd_type =3D NVME; > +=09=09pwd_type +=3D setting->index - 1; > +=09} else { > +=09=09return -EOPNOTSUPP; > +=09} > + > +=09start =3D wmibuf.data; > +=09start[0] =3D pwd_type; > +=09start +=3D 2; > + > +=09ix =3D tlmi_populate_string_buffer(start, cur_pwdlen, setting->passwo= rd); > +=09if (ix < 0) > +=09=09return ix; > +=09start +=3D ix; > +=09ix =3D tlmi_populate_string_buffer(start, pwdlen, new_pwd); > +=09if (ix < 0) > +=09=09return ix; > + > +=09ret =3D tlmi_request(td->auth_wdev, 0, SET_PASSWORD, &wmibuf); > +=09return ret ?: count; > +} > + > +static struct kobj_attribute auth_new_password =3D __ATTR_WO(new_passwor= d); > + > +static ssize_t min_password_length_show(struct kobject *kobj, struct kob= j_attribute *attr, > +=09=09=09=09=09char *buf) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > + > +=09return sysfs_emit(buf, "%d\n", setting->minlen); > +} > + > +static struct kobj_attribute auth_min_pass_length =3D __ATTR_RO(min_pass= word_length); > + > +static ssize_t max_password_length_show(struct kobject *kobj, struct kob= j_attribute *attr, > +=09=09=09=09=09char *buf) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > + > +=09return sysfs_emit(buf, "%d\n", setting->maxlen); > +} > + > +static struct kobj_attribute auth_max_pass_length =3D __ATTR_RO(max_pass= word_length); > + > +static ssize_t mechanism_show(struct kobject *kobj, struct kobj_attribut= e *attr, > +=09=09=09 char *buf) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > + > +=09if (setting->cert_installed) > +=09=09return sysfs_emit(buf, "certificate\n"); > +=09return sysfs_emit(buf, "password\n"); > +} > + > +static struct kobj_attribute auth_mechanism =3D __ATTR_RO(mechanism); > + > +static ssize_t role_show(struct kobject *kobj, struct kobj_attribute *at= tr, > +=09=09=09 char *buf) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > + > +=09return sysfs_emit(buf, "%s\n", setting->role); > +} > + > +static struct kobj_attribute auth_role =3D __ATTR_RO(role); > + > +static ssize_t index_show(struct kobject *kobj, struct kobj_attribute *a= ttr, > +=09=09=09 char *buf) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > + > +=09return sysfs_emit(buf, "%d\n", setting->index); > +} > + > +static ssize_t index_store(struct kobject *kobj, > +=09=09=09 struct kobj_attribute *attr, > +=09=09=09 const char *buf, size_t count) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > +=09struct think_lmi *td =3D dev_get_drvdata(&setting->wdev->dev); > +=09int err, val, max; > + > +=09err =3D kstrtoint(buf, 10, &val); > +=09if (err < 0) > +=09=09return err; > + > +=09if (setting =3D=3D td->pwd_hdd) > +=09=09max =3D MAX_HDD_INDEX; > +=09else if (setting =3D=3D td->pwd_nvme) > +=09=09max =3D MAX_NVME_INDEX; > +=09else > +=09=09return -EOPNOTSUPP; > + > +=09if (val <=3D 0 || val > max) > +=09=09return -EINVAL; > + > +=09guard(mutex)(&td->tlmi_auth_lock); > +=09setting->index =3D val; > +=09return count; > +} > + > +static struct kobj_attribute auth_index =3D __ATTR_RW(index); > + > +static ssize_t level_show(struct kobject *kobj, struct kobj_attribute *a= ttr, > +=09=09=09 char *buf) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > + > +=09return sysfs_emit(buf, "%s\n", level_options[setting->level]); > +} > + > +static ssize_t level_store(struct kobject *kobj, > +=09=09=09 struct kobj_attribute *attr, > +=09=09=09 const char *buf, size_t count) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > +=09struct think_lmi *td =3D dev_get_drvdata(&setting->wdev->dev); > +=09int i; > + > +=09/* Scan for a matching profile */ > +=09i =3D sysfs_match_string(level_options, buf); > +=09if (i < 0) > +=09=09return -EINVAL; > + > +=09guard(mutex)(&td->tlmi_auth_lock); > +=09setting->level =3D i; > +=09return count; > +} > + > +static struct kobj_attribute auth_level =3D __ATTR_RW(level); > + > +static char *thumbtypes[] =3D {"SHA1", "SHA256", "SHA384", "SHA512", "MD= 5"}; > +#define MAX_SHA_LEN 513 > + > +static ssize_t cert_thumbprint(char *buf, char *type, unsigned char alg,= int *count, > +=09=09=09 struct think_lmi *td) > +{ > +=09struct wmi_buffer wmibuf, wmibuf_thumb; > +=09int ix, ret; > +=09char *start; > + > +=09if (!type) > +=09=09return -EINVAL; > + > +=09wmibuf.length =3D struct_size_t(struct wmi_string, chars, strlen(type= ) + 2); > +=09char *wmidata __free(kfree) =3D kzalloc(wmibuf.length, GFP_KERNEL); > +=09if (!wmidata) > +=09=09return -ENOMEM; > +=09wmibuf.data =3D wmidata; > + > +=09char *thumbstr __free(kfree) =3D kzalloc(MAX_SHA_LEN, GFP_KERNEL); > +=09if (!thumbstr) > +=09=09return -ENOMEM; > + > +=09start =3D wmibuf.data; > +=09ix =3D tlmi_populate_string_buffer(start, wmibuf.length, type); > +=09if (ix < 0) > +=09=09return ix; > +=09start +=3D ix; > +=09*start =3D alg; > + > +=09ret =3D wmidev_invoke_method(td->auth_wdev, 0, CERT_THUMBPRINT, &wmib= uf, &wmibuf_thumb, > +=09=09=09=09 sizeof(struct wmi_string)); > +=09if (ret < 0) > +=09=09return ret; > + > +=09if (wmibuf_thumb.data) { > +=09=09/* Check for valid string */ > +=09=09if (wmibuf_thumb.length < 2 || wmibuf_thumb.length > MAX_SHA_LEN *= 2) > +=09=09=09ret =3D -EINVAL; > +=09=09else > +=09=09=09/* Extract thumbprint */ > +=09=09=09ret =3D wmi_string_to_utf8s((struct wmi_string *)wmibuf_thumb.d= ata, > +=09=09=09=09=09=09 thumbstr, MAX_SHA_LEN); > +=09=09kfree(wmibuf_thumb.data); > +=09=09if (ret > 0) > +=09=09=09*count +=3D sysfs_emit_at(buf, *count, "%s : %s\n", > +=09=09=09=09=09=09thumbtypes[alg], thumbstr); > +=09} > + > +=09return ret; > +} > + > +static ssize_t certificate_thumbprint_show(struct kobject *kobj, struct = kobj_attribute *attr, > +=09=09=09=09=09 char *buf) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > +=09struct think_lmi *td =3D dev_get_drvdata(&setting->wdev->dev); > +=09unsigned int i; > +=09int ret, count =3D 0; > + > +=09if (!setting->cert_installed) > +=09=09return -EOPNOTSUPP; > + > +=09guard(mutex)(&td->tlmi_auth_lock); > + > +=09for (i =3D 0; i < ARRAY_SIZE(thumbtypes); i++) { > +=09=09ret =3D cert_thumbprint(buf, setting =3D=3D td->pwd_admin ? "SVC" = : "SMC", > +=09=09=09=09 i, &count, td); > +=09=09if (ret < 0) > +=09=09=09return ret; > +=09} > + > +=09return count; > +} > + > +static struct kobj_attribute auth_cert_thumb =3D __ATTR_RO(certificate_t= humbprint); > + > +static ssize_t cert_to_password_store(struct kobject *kobj, > +=09=09=09=09 struct kobj_attribute *attr, > +=09=09=09=09 const char *buf, size_t count) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > +=09struct think_lmi *td =3D dev_get_drvdata(&setting->wdev->dev); > +=09size_t typelen, pwdlen, siglen; > +=09char *start, *cert_type; > +=09struct wmi_buffer wmibuf; > +=09int ret =3D -EINVAL; > +=09int ix; > + > +=09if (!capable(CAP_SYS_ADMIN)) > +=09=09return -EPERM; > + > +=09if (!setting->cert_installed) > +=09=09return -EINVAL; > + > +=09guard(mutex)(&td->tlmi_auth_lock); > + > +=09if (!setting->signature || !setting->signature[0]) > +=09=09return -EACCES; > + > +=09/* Strip out CR if one is present */ > +=09char *passwd __free(kfree_sensitive) =3D kstrdup_and_replace(buf, '\n= ', '\0', GFP_KERNEL); > +=09if (!passwd) > +=09=09return -ENOMEM; > + > +=09cert_type =3D (setting =3D=3D td->pwd_admin) ? "SVC" : "SMC"; > +=09typelen =3D struct_size_t(struct wmi_string, chars, strlen(cert_type)= + 1); > +=09pwdlen =3D struct_size_t(struct wmi_string, chars, strlen(passwd) + 1= ); > +=09siglen =3D struct_size_t(struct wmi_string, chars, strlen(setting->si= gnature) + 1); > +=09wmibuf.length =3D typelen + pwdlen + siglen; > + > +=09char *wmidata __free(kfree_sensitive) =3D kzalloc(wmibuf.length, GFP_= KERNEL); > +=09if (!wmidata) > +=09=09return -ENOMEM; > +=09wmibuf.data =3D wmidata; > + > +=09start =3D wmibuf.data; > +=09ix =3D tlmi_populate_string_buffer(start, typelen, cert_type); > +=09if (ix < 0) > +=09=09return ix; > + > +=09start +=3D ix; > +=09ix =3D tlmi_populate_string_buffer(start, pwdlen, passwd); > +=09if (ix < 0) > +=09=09return ix; > + > +=09start +=3D ix; > +=09ix =3D tlmi_populate_string_buffer(start, siglen, setting->signature)= ; > +=09if (ix < 0) > +=09=09return ix; > + > +=09ret =3D tlmi_request(td->auth_wdev, 0, CERT_TO_PWD, &wmibuf); > + > +=09return ret ?: count; > +} > + > +static struct kobj_attribute auth_cert_to_password =3D __ATTR_WO(cert_to= _password); > + > +static ssize_t certificate_store(struct kobject *kobj, > +=09=09=09=09 struct kobj_attribute *attr, > +=09=09=09=09 const char *buf, size_t count) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > +=09struct think_lmi *td =3D dev_get_drvdata(&setting->wdev->dev); > +=09struct tlmi_pwd_setting *pwd_admin =3D td->pwd_admin; > +=09size_t typelen, certlen, pwdlen, siglen, seriallen; > +=09struct wmi_buffer wmibuf; > +=09char *start; > +=09char *signature, *cert_type; > +=09int install_mode =3D SET_CERT; > +=09const char *serial; > +=09int ix =3D 0; > +=09int ret; > + > +=09char *new_cert __free(kfree_sensitive) =3D NULL; > + > +=09if (!capable(CAP_SYS_ADMIN)) > +=09=09return -EPERM; > + > +=09guard(mutex)(&td->tlmi_auth_lock); > + > +=09cert_type =3D (setting =3D=3D pwd_admin) ? "SVC" : "SMC"; > +=09typelen =3D struct_size_t(struct wmi_string, chars, strlen(cert_type)= + 1); > + > +=09if ((buf[0] =3D=3D '\0') || (buf[0] =3D=3D '\n')) { > +=09=09/* > +=09=09 * Clear installed certificate > +=09=09 * Format: 'Type, serial#, signature' > +=09=09 */ > + > +=09=09/* Check that signature is set */ > +=09=09if (!setting->signature || !setting->signature[0]) > +=09=09=09return -EACCES; > + > +=09=09install_mode =3D CLEAR_CERT; > +=09=09serial =3D dmi_get_system_info(DMI_PRODUCT_SERIAL); > +=09=09if (!serial) > +=09=09=09return -ENODEV; > +=09=09seriallen =3D struct_size_t(struct wmi_string, chars, strlen(seria= l) + 1); > +=09=09signature =3D setting->signature; > +=09=09siglen =3D struct_size_t(struct wmi_string, chars, strlen(signatur= e) + 1); > +=09=09wmibuf.length =3D typelen + seriallen + siglen; > +=09} else { > +=09=09/* New install, or update */ > +=09=09/* Strip out CR from certificate if one is present */ > +=09=09new_cert =3D kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL); > +=09=09if (!new_cert) > +=09=09=09return -ENOMEM; > +=09=09certlen =3D struct_size_t(struct wmi_string, chars, strlen(new_cer= t) + 1); > + > +=09=09if (setting->cert_installed) { > +=09=09=09/* > +=09=09=09 * Update installed certificate > +=09=09=09 * Format: 'Type, Certificate, signature' > +=09=09=09 */ > +=09=09=09install_mode =3D UPDATE_CERT; > +=09=09=09/* If admin account enabled - need to use its signature */ > +=09=09=09if (pwd_admin && pwd_admin->pwd_enabled) > +=09=09=09=09signature =3D pwd_admin->signature; > +=09=09=09else > +=09=09=09=09signature =3D setting->signature; > +=09=09=09if (!signature) > +=09=09=09=09return -EINVAL; > + > +=09=09=09siglen =3D struct_size_t(struct wmi_string, chars, strlen(signa= ture) + 1); > +=09=09=09wmibuf.length =3D typelen + certlen + siglen; > +=09=09} else { > +=09=09=09/* > +=09=09=09 * Install a new certificate > +=09=09=09 * Format: 'Type, Certificate, Password' > +=09=09=09 */ > +=09=09=09install_mode =3D SET_CERT; > +=09=09=09/* To set admin cert, a password must be enabled */ > +=09=09=09if (setting =3D=3D pwd_admin && > +=09=09=09 (!setting->pwd_enabled || !setting->password[0])) > +=09=09=09=09return -EACCES; > + > +=09=09=09pwdlen =3D struct_size_t(struct wmi_string, chars, > +=09=09=09=09=09 strlen(setting->password) + 1); > +=09=09=09wmibuf.length =3D typelen + certlen + pwdlen; > +=09=09} > +=09} > + > +=09/* Set up WMI buffer */ > +=09char *wmidata __free(kfree_sensitive) =3D kzalloc(wmibuf.length, GFP_= KERNEL); > +=09if (!wmidata) > +=09=09return -ENOMEM; > +=09wmibuf.data =3D wmidata; > + > +=09start =3D wmibuf.data; > +=09ix =3D tlmi_populate_string_buffer(start, typelen, cert_type); > +=09if (ix < 0) > +=09=09return ix; > + > +=09start +=3D ix; > +=09if (install_mode =3D=3D CLEAR_CERT) > +=09=09ix =3D tlmi_populate_string_buffer(start, seriallen, serial); > +=09else > +=09=09ix =3D tlmi_populate_string_buffer(start, certlen, new_cert); > +=09if (ix < 0) > +=09=09return ix; > + > +=09start +=3D ix; > +=09if (install_mode =3D=3D SET_CERT) > +=09=09ix =3D tlmi_populate_string_buffer(start, pwdlen, setting->passwor= d); > +=09else > +=09=09ix =3D tlmi_populate_string_buffer(start, siglen, signature); > +=09if (ix < 0) > +=09=09return ix; > + > +=09ret =3D tlmi_request(td->auth_wdev, 0, install_mode, &wmibuf); > + > +=09return ret ?: count; > +} > + > +static struct kobj_attribute auth_certificate =3D __ATTR_WO(certificate)= ; > + > +static ssize_t signature_store(struct kobject *kobj, > +=09=09=09 struct kobj_attribute *attr, > +=09=09=09 const char *buf, size_t count) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > +=09struct think_lmi *td =3D dev_get_drvdata(&setting->wdev->dev); > +=09char *new_signature; > + > +=09if (!capable(CAP_SYS_ADMIN)) > +=09=09return -EPERM; > + > +=09guard(mutex)(&td->tlmi_auth_lock); > + > +=09/* Strip out CR if one is present */ > +=09new_signature =3D kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL); > +=09if (!new_signature) > +=09=09return -ENOMEM; > + > +=09/* Free any previous signature */ > +=09kfree_sensitive(setting->signature); > +=09setting->signature =3D new_signature; > + > +=09return count; > +} > + > +static struct kobj_attribute auth_signature =3D __ATTR_WO(signature); > + > +static ssize_t save_signature_store(struct kobject *kobj, > +=09=09=09=09 struct kobj_attribute *attr, > +=09=09=09=09 const char *buf, size_t count) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > +=09struct think_lmi *td =3D dev_get_drvdata(&setting->wdev->dev); > +=09char *new_signature; > + > +=09if (!capable(CAP_SYS_ADMIN)) > +=09=09return -EPERM; > + > +=09guard(mutex)(&td->tlmi_auth_lock); > + > +=09/* Strip out CR if one is present */ > +=09new_signature =3D kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL); > +=09if (!new_signature) > +=09=09return -ENOMEM; > + > +=09/* Free any previous signature */ > +=09kfree_sensitive(setting->save_signature); > +=09setting->save_signature =3D new_signature; > + > +=09return count; > +} > + > +static struct kobj_attribute auth_save_signature =3D __ATTR_WO(save_sign= ature); > + > +static umode_t auth_attr_is_visible(struct kobject *kobj, > +=09=09=09=09 struct attribute *attr, int n) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > +=09struct think_lmi *td =3D dev_get_drvdata(&setting->wdev->dev); > + > +=09/* We only want to display level and index settings on HDD/NVMe */ > +=09if (attr =3D=3D &auth_index.attr || attr =3D=3D &auth_level.attr) { > +=09=09if (setting =3D=3D td->pwd_hdd || setting =3D=3D td->pwd_nvme) > +=09=09=09return attr->mode; > +=09=09return 0; > +=09} > + > +=09return attr->mode; > +} > + > +static struct attribute *auth_attrs[] =3D { > +=09&auth_is_pass_set.attr, > +=09&auth_min_pass_length.attr, > +=09&auth_max_pass_length.attr, > +=09&auth_current_password.attr, > +=09&auth_new_password.attr, > +=09&auth_role.attr, > +=09&auth_mechanism.attr, > +=09&auth_index.attr, > +=09&auth_level.attr, > +=09&auth_certificate.attr, > +=09&auth_signature.attr, > +=09&auth_save_signature.attr, > +=09&auth_cert_thumb.attr, > +=09&auth_cert_to_password.attr, > +=09NULL > +}; > + > +static const struct attribute_group auth_attr_group =3D { > +=09.is_visible =3D auth_attr_is_visible, > +=09.attrs =3D auth_attrs, > +}; > +__ATTRIBUTE_GROUPS(auth_attr); > + > +static void tlmi_pwd_setting_release(struct kobject *kobj) > +{ > +=09struct tlmi_pwd_setting *setting =3D to_tlmi_pwd_setting(kobj); > + > +=09kfree_sensitive(setting->save_signature); > +=09kfree_sensitive(setting->signature); > +=09kfree_sensitive(setting); > +} > + > +static const struct kobj_type tlmi_pwd_setting_ktype =3D { > +=09.release =3D &tlmi_pwd_setting_release, > +=09.sysfs_ops=09=3D &kobj_sysfs_ops, > +=09.default_groups =3D auth_attr_groups, > +}; > + > +static void tlmi_auth_sysfs_release(struct think_lmi *td) > +{ > +=09struct kobject *pos, *n; > + > +=09if (!td || !td->authentication_kset) > +=09=09return; > + > +=09/* Authentication structures */ > +=09list_for_each_entry_safe(pos, n, &td->authentication_kset->list, entr= y) > +=09=09kobject_put(pos); > + > +=09kset_unregister(td->authentication_kset); > + > +=09/* Prevent dangling pointers */ > +=09td->authentication_kset =3D NULL; > +=09td->pwd_admin =3D NULL; > +=09td->pwd_system =3D NULL; > +=09td->pwd_power =3D NULL; > +=09td->pwd_hdd =3D NULL; > +=09td->pwd_nvme =3D NULL; > +} > + > +static int tlmi_auth_sysfs_init(struct think_lmi *td) > +{ > +=09int ret; > + > +=09if (!td) > +=09=09return -EINVAL; > + > +=09/* Create authentication entries */ > +=09td->authentication_kset =3D kset_create_and_add("authentication", NUL= L, > +=09=09=09=09=09=09 &td->class_dev->kobj); > +=09if (!td->authentication_kset) { > +=09=09ret =3D -ENOMEM; > +=09=09goto out_admin_fail; > +=09} > + > +=09td->pwd_admin->kobj.kset =3D td->authentication_kset; > +=09ret =3D kobject_init_and_add(&td->pwd_admin->kobj, &tlmi_pwd_setting_= ktype, > +=09=09=09=09 NULL, "%s", "Admin"); > +=09if (ret) > +=09=09goto out_admin_fail; > + > +=09td->pwd_power->kobj.kset =3D td->authentication_kset; > +=09ret =3D kobject_init_and_add(&td->pwd_power->kobj, &tlmi_pwd_setting_= ktype, > +=09=09=09=09 NULL, "%s", "Power-on"); > +=09if (ret) > +=09=09goto out_power_fail; > + > +=09td->pwd_system->kobj.kset =3D td->authentication_kset; > +=09ret =3D kobject_init_and_add(&td->pwd_system->kobj, &tlmi_pwd_setting= _ktype, > +=09=09=09=09 NULL, "%s", "System"); > +=09if (ret) > +=09=09goto out_system_fail; > + > +=09td->pwd_hdd->kobj.kset =3D td->authentication_kset; > +=09ret =3D kobject_init_and_add(&td->pwd_hdd->kobj, &tlmi_pwd_setting_kt= ype, > +=09=09=09=09 NULL, "%s", "HDD"); > +=09if (ret) > +=09=09goto out_hdd_fail; > + > +=09td->pwd_nvme->kobj.kset =3D td->authentication_kset; > +=09ret =3D kobject_init_and_add(&td->pwd_nvme->kobj, &tlmi_pwd_setting_k= type, > +=09=09=09=09 NULL, "%s", "NVMe"); > +=09if (ret) > +=09=09goto out_nvme_fail; > + > +=09return 0; > + > +out_admin_fail: > +=09kobject_put(&td->pwd_admin->kobj); > +=09td->pwd_admin =3D NULL; > +out_power_fail: > +=09kobject_put(&td->pwd_power->kobj); > +=09td->pwd_power =3D NULL; > +out_system_fail: > +=09kobject_put(&td->pwd_system->kobj); > +=09td->pwd_system =3D NULL; > +out_hdd_fail: > +=09kobject_put(&td->pwd_hdd->kobj); > +=09td->pwd_hdd =3D NULL; > +out_nvme_fail: > +=09kobject_put(&td->pwd_nvme->kobj); > +=09td->pwd_nvme =3D NULL; > +=09return ret; > +} > + > +static int tlmi_create_auth(struct think_lmi *td, > +=09=09=09 const char *pwd_role, > +=09=09=09 struct tlmi_pwd_setting **setting) > +{ > +=09struct tlmi_pwd_setting *new_pwd; > + > +=09new_pwd =3D kzalloc_obj(*new_pwd); > +=09if (!new_pwd) > +=09=09return -ENOMEM; > + > +=09new_pwd->role =3D pwd_role; > +=09new_pwd->minlen =3D td->pwdcfg.core.min_length; > +=09new_pwd->maxlen =3D td->pwdcfg.core.max_length; > +=09new_pwd->index =3D 1; > +=09new_pwd->level =3D TLMI_LEVEL_MASTER; > +=09new_pwd->wdev =3D td->auth_wdev; > + > +=09*setting =3D new_pwd; > +=09return 0; > +} > + > +static int tlmi_auth_setup(struct think_lmi *td) > +{ > +=09struct tlmi_pwd_setting *admin __free(kfree) =3D NULL; > +=09struct tlmi_pwd_setting *power __free(kfree) =3D NULL; > +=09struct tlmi_pwd_setting *system __free(kfree) =3D NULL; > +=09struct tlmi_pwd_setting *hdd __free(kfree) =3D NULL; > +=09struct tlmi_pwd_setting *nvme __free(kfree) =3D NULL; > +=09unsigned int index; > +=09int ret; > + > +=09if (!td) > +=09=09return -EINVAL; > + > +=09ret =3D tlmi_create_auth(td, "bios-admin", &admin); > +=09if (ret) > +=09=09return ret; > + > +=09ret =3D tlmi_create_auth(td, "power-on", &power); > +=09if (ret) > +=09=09return ret; > + > +=09ret =3D tlmi_create_auth(td, "system", &system); > +=09if (ret) > +=09=09return ret; > + > +=09ret =3D tlmi_create_auth(td, "hdd", &hdd); > +=09if (ret) > +=09=09return ret; > + > +=09ret =3D tlmi_create_auth(td, "nvme", &nvme); > +=09if (ret) > +=09=09return ret; > + > +=09td->pwd_admin =3D no_free_ptr(admin); > +=09td->pwd_power =3D no_free_ptr(power); > +=09td->pwd_system =3D no_free_ptr(system); > +=09td->pwd_hdd =3D no_free_ptr(hdd); > +=09td->pwd_nvme =3D no_free_ptr(nvme); > + > +=09if (td->pwdcfg.core.password_state & TLMI_SVP_PWD) > +=09=09td->pwd_admin->pwd_enabled =3D true; > + > +=09if (td->pwdcfg.core.password_state & TLMI_POP_PWD) > +=09=09td->pwd_power->pwd_enabled =3D true; > + > +=09if (td->pwdcfg.core.password_state & TLMI_SMP_PWD) > +=09=09td->pwd_system->pwd_enabled =3D true; > + > +=09if (td->pwdcfg.core.password_state & TLMI_SVC_PWD) > +=09=09td->pwd_admin->cert_installed =3D true; > +=09if (td->pwdcfg.core.password_state & TLMI_SMC_PWD) > +=09=09td->pwd_system->cert_installed =3D true; > + > +=09if (td->pwdcfg.ext.hdd_user_password || > +=09 td->pwdcfg.ext.hdd_master_password) { > +=09=09td->pwd_hdd->pwd_enabled =3D true; > +=09=09if (td->pwdcfg.ext.hdd_master_password) > +=09=09=09index =3D ffs(td->pwdcfg.ext.hdd_master_password); > +=09=09else > +=09=09=09index =3D ffs(td->pwdcfg.ext.hdd_user_password); > +=09=09/* Sanitize result */ > +=09=09if (index > MAX_HDD_INDEX || !index) { > +=09=09=09pr_info("tlmi-auth: Invalid HDD password index %u. Setting to %= d\n", > +=09=09=09=09index, MAX_HDD_INDEX); > +=09=09=09index =3D MAX_HDD_INDEX; > +=09=09} > +=09=09td->pwd_hdd->index =3D index; > +=09} > + > +=09if (td->pwdcfg.ext.nvme_user_password || > +=09 td->pwdcfg.ext.nvme_master_password) { > +=09=09td->pwd_nvme->pwd_enabled =3D true; > +=09=09if (td->pwdcfg.ext.nvme_master_password) > +=09=09=09index =3D=09ffs(td->pwdcfg.ext.nvme_master_password); > +=09=09else > +=09=09=09index =3D=09ffs(td->pwdcfg.ext.nvme_user_password); > +=09=09/* Sanitize result */ > +=09=09if (index > MAX_NVME_INDEX || !index) { > +=09=09=09pr_info("tlmi-auth: Invalid NVME password index %d. Setting to = %d\n", > +=09=09=09=09index, MAX_NVME_INDEX); > +=09=09=09index =3D MAX_NVME_INDEX; > +=09=09} > +=09=09td->pwd_nvme->index =3D index; > +=09} > + > +=09mutex_init(&td->tlmi_auth_lock); > + > +=09return ret; > +} > + > +static int tlmi_auth_component_bind(struct device *auth_dev, struct devi= ce *config_dev, > +=09=09=09=09 void *d) > +{ > +=09struct think_lmi *td =3D d; > +=09int ret; > + > +=09td->auth_wdev =3D to_wmi_device(auth_dev); > +=09dev_set_drvdata(&td->auth_wdev->dev, td); > + > +=09ret =3D tlmi_auth_setup(td); > +=09if (ret) > +=09=09return ret; > + > +=09ret =3D tlmi_auth_sysfs_init(td); > +=09if (ret) > +=09=09tlmi_auth_sysfs_release(td); > +=09return ret; > +} > + > +static void tlmi_auth_component_unbind(struct device *auth_dev, struct d= evice *cfg_dev, void *d) > +{ > +=09struct think_lmi *td =3D d; > + > +=09tlmi_auth_sysfs_release(td); > +} > + > +static const struct component_ops tlmi_auth_component_ops =3D { > +=09.bind =3D tlmi_auth_component_bind, > +=09.unbind =3D tlmi_auth_component_unbind, > +}; > + > +static int tlmi_auth_probe(struct wmi_device *wdev, const void *context) > +{ > +=09return component_add(&wdev->dev, &tlmi_auth_component_ops); > +} > + > +static void tlmi_auth_remove(struct wmi_device *wdev) > +{ > +=09component_del(&wdev->dev, &tlmi_auth_component_ops); > +} > + > +static const struct wmi_device_id tlmi_auth_id_table[] =3D { > +=09{ TLMI_AUTHENTICATION_GUID, NULL }, > +=09{} > +}; > + > +static struct wmi_driver tlmi_auth_driver =3D { > +=09.driver =3D { > +=09=09.name =3D "tlmi-auth", > +=09=09.probe_type =3D PROBE_PREFER_ASYNCHRONOUS, > +=09}, > +=09.id_table =3D tlmi_auth_id_table, > +=09.probe =3D tlmi_auth_probe, > +=09.remove =3D tlmi_auth_remove, > +=09.no_singleton =3D true, > +}; > + > +static int tlmi_auth_match(struct device *dev, void *type) > +{ > +=09return (dev->driver =3D=3D &tlmi_auth_driver.driver); > +} > + > +void tlmi_auth_match_add(struct device *master, struct component_match *= *matchptr) > +{ > +=09component_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 "); > +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, c= onst char *buf, int wmi_i > { > =09char *new_setting __free(kfree) =3D NULL; > =09char *wmidata __free(kfree) =3D NULL; > -=09size_t namelen, valuelen; > +=09size_t namelen, valuelen, siglen; > =09struct wmi_buffer wmibuf; > =09char *start; > =09int ix =3D 0; > @@ -64,10 +64,17 @@ static ssize_t common_current_value_store(char *name,= const char *buf, int wmi_i > =09if (!new_setting) > =09=09return -ENOMEM; > =20 > +=09guard(mutex)(&td->tlmi_auth_lock); > + > =09/* Calculate and alloc buffer for command string */ > =09namelen =3D struct_size_t(struct wmi_string, chars, strlen(name) + 1)= ; > =09valuelen =3D struct_size_t(struct wmi_string, chars, strlen(new_setti= ng) + 1); > -=09wmibuf.length =3D namelen + valuelen; > +=09if (td->pwd_admin && td->pwd_admin->signature) > +=09=09siglen =3D struct_size_t(struct wmi_string, chars, > +=09=09=09=09 strlen(td->pwd_admin->signature) + 1); > +=09else > +=09=09siglen =3D struct_size_t(struct wmi_string, chars, 1 + 1); > +=09wmibuf.length =3D namelen + valuelen + siglen; > =09wmidata =3D kzalloc(wmibuf.length, GFP_KERNEL); > =09wmibuf.data =3D wmidata; > =09if (!wmibuf.data) > @@ -83,6 +90,14 @@ static ssize_t common_current_value_store(char *name, = const char *buf, int wmi_i > =09ix =3D tlmi_populate_string_buffer(start, valuelen, new_setting); > =09if (ix < 0) > =09=09return ix; > +=09start +=3D ix; > +=09if (td->pwd_admin && td->pwd_admin->signature) > +=09=09ix =3D tlmi_populate_string_buffer(start, siglen, > +=09=09=09=09=09=09 td->pwd_admin->signature); > +=09else > +=09=09ix =3D tlmi_populate_string_buffer(start, siglen, "\0"); > +=09if (ix < 0) > +=09=09return ix; > =20 > =09return tlmi_request(td->config_wdev, 0, wmi_id, &wmibuf); > } > @@ -209,8 +224,10 @@ static ssize_t save_settings_store(struct kobject *k= obj, struct kobj_attribute * > =09=09=09=09 const char *buf, size_t count) > { > =09struct think_lmi *td =3D dev_get_drvdata(kobj_to_dev(kobj->parent)); > +=09struct tlmi_pwd_setting *pwd_admin =3D td->pwd_admin; > =09char *wmidata __free(kfree) =3D NULL; > =09struct wmi_buffer wmibuf; > +=09size_t siglen; > =09int ret =3D 0; > =20 > =09/* save command triggered by writing 'save' */ > @@ -224,13 +241,30 @@ static ssize_t save_settings_store(struct kobject *= kobj, struct kobj_attribute * > =09if (!td->save_required) > =09=09return -ENOENT; > =20 > -=09wmibuf.length =3D struct_size_t(struct wmi_string, chars, 1 + 1); > +=09guard(mutex)(&td->tlmi_auth_lock); > + > +=09/* Authorisation (if needed) is done just before save */ > +=09ret =3D tlmi_auth_authenticate_user(td); > +=09/* Ignore most errors. We might be in deployment mode */ > +=09if (ret =3D=3D -ENOMEM) > +=09=09return ret; > + > +=09if (td->pwd_admin && td->pwd_admin->save_signature) > +=09=09siglen =3D struct_size_t(struct wmi_string, chars, > +=09=09=09=09 strlen(td->pwd_admin->save_signature) + 1); > +=09else > +=09=09siglen =3D struct_size_t(struct wmi_string, chars, 1 + 1); > +=09wmibuf.length =3D siglen; > =09wmidata =3D kzalloc(wmibuf.length, GFP_KERNEL); > =09wmibuf.data =3D wmidata; > =09if (!wmibuf.data) > =09=09return -ENOMEM; > =20 > -=09ret =3D tlmi_populate_string_buffer(wmibuf.data, 1, "\0"); > +=09if (pwd_admin && pwd_admin->save_signature) > +=09=09ret =3D tlmi_populate_string_buffer(wmibuf.data, wmibuf.length, > +=09=09=09=09=09=09 pwd_admin->save_signature); > +=09else > +=09=09ret =3D tlmi_populate_string_buffer(wmibuf.data, siglen, "\0"); > =09if (ret < 0) > =09=09return ret; > =20 > @@ -359,16 +393,18 @@ static const struct component_master_ops aggregate_= ops =3D { > static int tlmi_config_probe(struct wmi_device *wdev, const void *contex= t) > { > =09struct component_match *match =3D NULL; > -=09guid_t tlmi_attr_guid, tlmi_authdata_guid; > +=09guid_t tlmi_attr_guid, tlmi_authdata_guid, tlmi_auth_guid; > =09struct think_lmi *td; > =09int ret; > =20 > =09guid_parse(TLMI_ATTRIBUTE_GUID, &tlmi_attr_guid); > =09guid_parse(TLMI_AUTHENTICATION_DATA_GUID, &tlmi_authdata_guid); > +=09guid_parse(TLMI_AUTHENTICATION_GUID, &tlmi_auth_guid); > =20 > =09/* At minimum need to support BIOS and authentication settings */ > =09if (!wmidev_exists(&tlmi_attr_guid) || > -=09 !wmidev_exists(&tlmi_authdata_guid)) > +=09 !wmidev_exists(&tlmi_authdata_guid) || > +=09 !wmidev_exists(&tlmi_auth_guid)) > =09=09return -EOPNOTSUPP; > =20 > =09td =3D 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) > =09/* Register the other components */ > =09tlmi_attr_match_add(&wdev->dev, &match); > =09tlmi_authdata_match_add(&wdev->dev, &match); > +=09tlmi_auth_match_add(&wdev->dev, &match); > =20 > =09ret =3D component_master_add_with_match(&wdev->dev, &aggregate_ops, m= atch); > =09if (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 "); > MODULE_DESCRIPTION("TLMI Config WMI Driver");