2023-09-13 13:46:36

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 1/2] platform/x86: think-lmi: Replace kstrdup() + strreplace() with kstrdup_and_replace()

Replace open coded functionalify of kstrdup_and_replace() with a call.

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/platform/x86/think-lmi.c | 43 +++++++++++---------------------
1 file changed, 15 insertions(+), 28 deletions(-)

diff --git a/drivers/platform/x86/think-lmi.c b/drivers/platform/x86/think-lmi.c
index 79346881cadb..94a3c7a74bc4 100644
--- a/drivers/platform/x86/think-lmi.c
+++ b/drivers/platform/x86/think-lmi.c
@@ -15,7 +15,7 @@
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/mutex.h>
-#include <linux/string.h>
+#include <linux/string_helpers.h>
#include <linux/types.h>
#include <linux/dmi.h>
#include <linux/wmi.h>
@@ -432,13 +432,11 @@ static ssize_t new_password_store(struct kobject *kobj,
if (!tlmi_priv.can_set_bios_password)
return -EOPNOTSUPP;

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

- /* Strip out CR if one is present, setting password won't work if it is present */
- strip_cr(new_pwd);
-
/* Use lock in case multiple WMI operations needed */
mutex_lock(&tlmi_mutex);

@@ -709,13 +707,11 @@ static ssize_t cert_to_password_store(struct kobject *kobj,
if (!setting->signature || !setting->signature[0])
return -EACCES;

- passwd = kstrdup(buf, GFP_KERNEL);
+ /* Strip out CR if one is present */
+ passwd = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
if (!passwd)
return -ENOMEM;

- /* Strip out CR if one is present */
- strip_cr(passwd);
-
/* Format: 'Password,Signature' */
auth_str = kasprintf(GFP_KERNEL, "%s,%s", passwd, setting->signature);
if (!auth_str) {
@@ -765,11 +761,10 @@ static ssize_t certificate_store(struct kobject *kobj,
return ret ?: count;
}

- new_cert = kstrdup(buf, GFP_KERNEL);
+ /* Strip out CR if one is present */
+ new_cert = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
if (!new_cert)
return -ENOMEM;
- /* Strip out CR if one is present */
- strip_cr(new_cert);

if (setting->cert_installed) {
/* Certificate is installed so this is an update */
@@ -817,13 +812,11 @@ static ssize_t signature_store(struct kobject *kobj,
if (!tlmi_priv.certificate_support)
return -EOPNOTSUPP;

- new_signature = kstrdup(buf, GFP_KERNEL);
+ /* Strip out CR if one is present */
+ new_signature = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
if (!new_signature)
return -ENOMEM;

- /* Strip out CR if one is present */
- strip_cr(new_signature);
-
/* Free any previous signature */
kfree(setting->signature);
setting->signature = new_signature;
@@ -846,13 +839,11 @@ static ssize_t save_signature_store(struct kobject *kobj,
if (!tlmi_priv.certificate_support)
return -EOPNOTSUPP;

- new_signature = kstrdup(buf, GFP_KERNEL);
+ /* Strip out CR if one is present */
+ new_signature = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
if (!new_signature)
return -ENOMEM;

- /* Strip out CR if one is present */
- strip_cr(new_signature);
-
/* Free any previous signature */
kfree(setting->save_signature);
setting->save_signature = new_signature;
@@ -985,13 +976,11 @@ static ssize_t current_value_store(struct kobject *kobj,
if (!tlmi_priv.can_set_bios_settings)
return -EOPNOTSUPP;

- new_setting = kstrdup(buf, GFP_KERNEL);
+ /* Strip out CR if one is present */
+ new_setting = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
if (!new_setting)
return -ENOMEM;

- /* Strip out CR if one is present */
- strip_cr(new_setting);
-
/* Use lock in case multiple WMI operations needed */
mutex_lock(&tlmi_mutex);

@@ -1163,13 +1152,11 @@ static ssize_t debug_cmd_store(struct kobject *kobj, struct kobj_attribute *attr
if (!tlmi_priv.can_debug_cmd)
return -EOPNOTSUPP;

- new_setting = kstrdup(buf, GFP_KERNEL);
+ /* Strip out CR if one is present */
+ new_setting = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
if (!new_setting)
return -ENOMEM;

- /* Strip out CR if one is present */
- strip_cr(new_setting);
-
if (tlmi_priv.pwd_admin->valid && tlmi_priv.pwd_admin->password[0]) {
auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s;",
tlmi_priv.pwd_admin->password,
--
2.40.0.1.gaa8946217a0b


2023-09-13 16:53:08

by Mark Pearson

[permalink] [raw]
Subject: Re: [PATCH v1 1/2] platform/x86: think-lmi: Replace kstrdup() + strreplace() with kstrdup_and_replace()

On Wed, Sep 13, 2023, at 5:27 AM, Andy Shevchenko wrote:
> Replace open coded functionalify of kstrdup_and_replace() with a call.
>
> Signed-off-by: Andy Shevchenko <[email protected]>
> ---
> drivers/platform/x86/think-lmi.c | 43 +++++++++++---------------------
> 1 file changed, 15 insertions(+), 28 deletions(-)
>
> diff --git a/drivers/platform/x86/think-lmi.c b/drivers/platform/x86/think-lmi.c
> index 79346881cadb..94a3c7a74bc4 100644
> --- a/drivers/platform/x86/think-lmi.c
> +++ b/drivers/platform/x86/think-lmi.c
> @@ -15,7 +15,7 @@
> #include <linux/errno.h>
> #include <linux/fs.h>
> #include <linux/mutex.h>
> -#include <linux/string.h>
> +#include <linux/string_helpers.h>
> #include <linux/types.h>
> #include <linux/dmi.h>
> #include <linux/wmi.h>
> @@ -432,13 +432,11 @@ static ssize_t new_password_store(struct kobject *kobj,
> if (!tlmi_priv.can_set_bios_password)
> return -EOPNOTSUPP;
>
> - new_pwd = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present, setting password won't work if it
> is present */
> + new_pwd = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_pwd)
> return -ENOMEM;
>
> - /* Strip out CR if one is present, setting password won't work if it
> is present */
> - strip_cr(new_pwd);
> -
> /* Use lock in case multiple WMI operations needed */
> mutex_lock(&tlmi_mutex);
>
> @@ -709,13 +707,11 @@ static ssize_t cert_to_password_store(struct
> kobject *kobj,
> if (!setting->signature || !setting->signature[0])
> return -EACCES;
>
> - passwd = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + passwd = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!passwd)
> return -ENOMEM;
>
> - /* Strip out CR if one is present */
> - strip_cr(passwd);
> -
> /* Format: 'Password,Signature' */
> auth_str = kasprintf(GFP_KERNEL, "%s,%s", passwd, setting->signature);
> if (!auth_str) {
> @@ -765,11 +761,10 @@ static ssize_t certificate_store(struct kobject *kobj,
> return ret ?: count;
> }
>
> - new_cert = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + new_cert = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_cert)
> return -ENOMEM;
> - /* Strip out CR if one is present */
> - strip_cr(new_cert);
>
> if (setting->cert_installed) {
> /* Certificate is installed so this is an update */
> @@ -817,13 +812,11 @@ static ssize_t signature_store(struct kobject *kobj,
> if (!tlmi_priv.certificate_support)
> return -EOPNOTSUPP;
>
> - new_signature = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + new_signature = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_signature)
> return -ENOMEM;
>
> - /* Strip out CR if one is present */
> - strip_cr(new_signature);
> -
> /* Free any previous signature */
> kfree(setting->signature);
> setting->signature = new_signature;
> @@ -846,13 +839,11 @@ static ssize_t save_signature_store(struct kobject *kobj,
> if (!tlmi_priv.certificate_support)
> return -EOPNOTSUPP;
>
> - new_signature = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + new_signature = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_signature)
> return -ENOMEM;
>
> - /* Strip out CR if one is present */
> - strip_cr(new_signature);
> -
> /* Free any previous signature */
> kfree(setting->save_signature);
> setting->save_signature = new_signature;
> @@ -985,13 +976,11 @@ static ssize_t current_value_store(struct kobject *kobj,
> if (!tlmi_priv.can_set_bios_settings)
> return -EOPNOTSUPP;
>
> - new_setting = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + new_setting = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_setting)
> return -ENOMEM;
>
> - /* Strip out CR if one is present */
> - strip_cr(new_setting);
> -
> /* Use lock in case multiple WMI operations needed */
> mutex_lock(&tlmi_mutex);
>
> @@ -1163,13 +1152,11 @@ static ssize_t debug_cmd_store(struct kobject
> *kobj, struct kobj_attribute *attr
> if (!tlmi_priv.can_debug_cmd)
> return -EOPNOTSUPP;
>
> - new_setting = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + new_setting = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_setting)
> return -ENOMEM;
>
> - /* Strip out CR if one is present */
> - strip_cr(new_setting);
> -
> if (tlmi_priv.pwd_admin->valid && tlmi_priv.pwd_admin->password[0]) {
> auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s;",
> tlmi_priv.pwd_admin->password,
> --
> 2.40.0.1.gaa8946217a0b

Thanks Andy - didn't know about that function and it looks like a great clean-up.
I'll aim to test this out on some systems, but it looks good to me

Mark

2023-09-15 16:18:36

by Ilpo Järvinen

[permalink] [raw]
Subject: Re: [PATCH v1 1/2] platform/x86: think-lmi: Replace kstrdup() + strreplace() with kstrdup_and_replace()

On Wed, 13 Sep 2023, Andy Shevchenko wrote:

> Replace open coded functionalify of kstrdup_and_replace() with a call.
>
> Signed-off-by: Andy Shevchenko <[email protected]>
> ---
> drivers/platform/x86/think-lmi.c | 43 +++++++++++---------------------
> 1 file changed, 15 insertions(+), 28 deletions(-)
>
> diff --git a/drivers/platform/x86/think-lmi.c b/drivers/platform/x86/think-lmi.c
> index 79346881cadb..94a3c7a74bc4 100644
> --- a/drivers/platform/x86/think-lmi.c
> +++ b/drivers/platform/x86/think-lmi.c
> @@ -15,7 +15,7 @@
> #include <linux/errno.h>
> #include <linux/fs.h>
> #include <linux/mutex.h>
> -#include <linux/string.h>
> +#include <linux/string_helpers.h>
> #include <linux/types.h>
> #include <linux/dmi.h>
> #include <linux/wmi.h>
> @@ -432,13 +432,11 @@ static ssize_t new_password_store(struct kobject *kobj,
> if (!tlmi_priv.can_set_bios_password)
> return -EOPNOTSUPP;
>
> - new_pwd = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present, setting password won't work if it is present */
> + new_pwd = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_pwd)
> return -ENOMEM;
>
> - /* Strip out CR if one is present, setting password won't work if it is present */
> - strip_cr(new_pwd);
> -
> /* Use lock in case multiple WMI operations needed */
> mutex_lock(&tlmi_mutex);
>
> @@ -709,13 +707,11 @@ static ssize_t cert_to_password_store(struct kobject *kobj,
> if (!setting->signature || !setting->signature[0])
> return -EACCES;
>
> - passwd = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + passwd = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!passwd)
> return -ENOMEM;
>
> - /* Strip out CR if one is present */
> - strip_cr(passwd);
> -
> /* Format: 'Password,Signature' */
> auth_str = kasprintf(GFP_KERNEL, "%s,%s", passwd, setting->signature);
> if (!auth_str) {
> @@ -765,11 +761,10 @@ static ssize_t certificate_store(struct kobject *kobj,
> return ret ?: count;
> }
>
> - new_cert = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + new_cert = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_cert)
> return -ENOMEM;
> - /* Strip out CR if one is present */
> - strip_cr(new_cert);
>
> if (setting->cert_installed) {
> /* Certificate is installed so this is an update */
> @@ -817,13 +812,11 @@ static ssize_t signature_store(struct kobject *kobj,
> if (!tlmi_priv.certificate_support)
> return -EOPNOTSUPP;
>
> - new_signature = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + new_signature = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_signature)
> return -ENOMEM;
>
> - /* Strip out CR if one is present */
> - strip_cr(new_signature);
> -
> /* Free any previous signature */
> kfree(setting->signature);
> setting->signature = new_signature;
> @@ -846,13 +839,11 @@ static ssize_t save_signature_store(struct kobject *kobj,
> if (!tlmi_priv.certificate_support)
> return -EOPNOTSUPP;
>
> - new_signature = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + new_signature = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_signature)
> return -ENOMEM;
>
> - /* Strip out CR if one is present */
> - strip_cr(new_signature);
> -
> /* Free any previous signature */
> kfree(setting->save_signature);
> setting->save_signature = new_signature;
> @@ -985,13 +976,11 @@ static ssize_t current_value_store(struct kobject *kobj,
> if (!tlmi_priv.can_set_bios_settings)
> return -EOPNOTSUPP;
>
> - new_setting = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + new_setting = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_setting)
> return -ENOMEM;
>
> - /* Strip out CR if one is present */
> - strip_cr(new_setting);
> -
> /* Use lock in case multiple WMI operations needed */
> mutex_lock(&tlmi_mutex);
>
> @@ -1163,13 +1152,11 @@ static ssize_t debug_cmd_store(struct kobject *kobj, struct kobj_attribute *attr
> if (!tlmi_priv.can_debug_cmd)
> return -EOPNOTSUPP;
>
> - new_setting = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + new_setting = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_setting)
> return -ENOMEM;
>
> - /* Strip out CR if one is present */
> - strip_cr(new_setting);
> -
> if (tlmi_priv.pwd_admin->valid && tlmi_priv.pwd_admin->password[0]) {
> auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s;",
> tlmi_priv.pwd_admin->password,
>

Reviewed-by: Ilpo J?rvinen <[email protected]>

--
i.

2023-09-18 14:50:01

by Hans de Goede

[permalink] [raw]
Subject: Re: [PATCH v1 1/2] platform/x86: think-lmi: Replace kstrdup() + strreplace() with kstrdup_and_replace()

Hi Andy,

On 9/13/23 11:27, Andy Shevchenko wrote:
> Replace open coded functionalify of kstrdup_and_replace() with a call.
>
> Signed-off-by: Andy Shevchenko <[email protected]>

Thank you for the patches, but this one does not apply
cleanly.

Can you please rebase the series on top of my latest review-hans branch ?

Regards,

Hans



> ---
> drivers/platform/x86/think-lmi.c | 43 +++++++++++---------------------
> 1 file changed, 15 insertions(+), 28 deletions(-)
>
> diff --git a/drivers/platform/x86/think-lmi.c b/drivers/platform/x86/think-lmi.c
> index 79346881cadb..94a3c7a74bc4 100644
> --- a/drivers/platform/x86/think-lmi.c
> +++ b/drivers/platform/x86/think-lmi.c
> @@ -15,7 +15,7 @@
> #include <linux/errno.h>
> #include <linux/fs.h>
> #include <linux/mutex.h>
> -#include <linux/string.h>
> +#include <linux/string_helpers.h>
> #include <linux/types.h>
> #include <linux/dmi.h>
> #include <linux/wmi.h>
> @@ -432,13 +432,11 @@ static ssize_t new_password_store(struct kobject *kobj,
> if (!tlmi_priv.can_set_bios_password)
> return -EOPNOTSUPP;
>
> - new_pwd = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present, setting password won't work if it is present */
> + new_pwd = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_pwd)
> return -ENOMEM;
>
> - /* Strip out CR if one is present, setting password won't work if it is present */
> - strip_cr(new_pwd);
> -
> /* Use lock in case multiple WMI operations needed */
> mutex_lock(&tlmi_mutex);
>
> @@ -709,13 +707,11 @@ static ssize_t cert_to_password_store(struct kobject *kobj,
> if (!setting->signature || !setting->signature[0])
> return -EACCES;
>
> - passwd = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + passwd = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!passwd)
> return -ENOMEM;
>
> - /* Strip out CR if one is present */
> - strip_cr(passwd);
> -
> /* Format: 'Password,Signature' */
> auth_str = kasprintf(GFP_KERNEL, "%s,%s", passwd, setting->signature);
> if (!auth_str) {
> @@ -765,11 +761,10 @@ static ssize_t certificate_store(struct kobject *kobj,
> return ret ?: count;
> }
>
> - new_cert = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + new_cert = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_cert)
> return -ENOMEM;
> - /* Strip out CR if one is present */
> - strip_cr(new_cert);
>
> if (setting->cert_installed) {
> /* Certificate is installed so this is an update */
> @@ -817,13 +812,11 @@ static ssize_t signature_store(struct kobject *kobj,
> if (!tlmi_priv.certificate_support)
> return -EOPNOTSUPP;
>
> - new_signature = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + new_signature = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_signature)
> return -ENOMEM;
>
> - /* Strip out CR if one is present */
> - strip_cr(new_signature);
> -
> /* Free any previous signature */
> kfree(setting->signature);
> setting->signature = new_signature;
> @@ -846,13 +839,11 @@ static ssize_t save_signature_store(struct kobject *kobj,
> if (!tlmi_priv.certificate_support)
> return -EOPNOTSUPP;
>
> - new_signature = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + new_signature = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_signature)
> return -ENOMEM;
>
> - /* Strip out CR if one is present */
> - strip_cr(new_signature);
> -
> /* Free any previous signature */
> kfree(setting->save_signature);
> setting->save_signature = new_signature;
> @@ -985,13 +976,11 @@ static ssize_t current_value_store(struct kobject *kobj,
> if (!tlmi_priv.can_set_bios_settings)
> return -EOPNOTSUPP;
>
> - new_setting = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + new_setting = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_setting)
> return -ENOMEM;
>
> - /* Strip out CR if one is present */
> - strip_cr(new_setting);
> -
> /* Use lock in case multiple WMI operations needed */
> mutex_lock(&tlmi_mutex);
>
> @@ -1163,13 +1152,11 @@ static ssize_t debug_cmd_store(struct kobject *kobj, struct kobj_attribute *attr
> if (!tlmi_priv.can_debug_cmd)
> return -EOPNOTSUPP;
>
> - new_setting = kstrdup(buf, GFP_KERNEL);
> + /* Strip out CR if one is present */
> + new_setting = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
> if (!new_setting)
> return -ENOMEM;
>
> - /* Strip out CR if one is present */
> - strip_cr(new_setting);
> -
> if (tlmi_priv.pwd_admin->valid && tlmi_priv.pwd_admin->password[0]) {
> auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s;",
> tlmi_priv.pwd_admin->password,

2023-09-18 21:12:26

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v1 1/2] platform/x86: think-lmi: Replace kstrdup() + strreplace() with kstrdup_and_replace()

On Mon, Sep 18, 2023 at 02:30:28PM +0200, Hans de Goede wrote:
> On 9/13/23 11:27, Andy Shevchenko wrote:
> > Replace open coded functionalify of kstrdup_and_replace() with a call.
> >
> > Signed-off-by: Andy Shevchenko <[email protected]>
>
> Thank you for the patches, but this one does not apply
> cleanly.

`git am -C1 ...` works for me.

> Can you please rebase the series on top of my latest review-hans branch ?

But fine, no problem, I'm about to send a v2,

--
With Best Regards,
Andy Shevchenko