Linux Power Management development
 help / color / mirror / Atom feed
* [PATCH 08/11] fix tt_command_write()
       [not found] ` <20250702211408.GA3406663@ZenIV>
@ 2025-07-02 21:25   ` Al Viro
  2025-07-03 11:14     ` Rafael J. Wysocki
  0 siblings, 1 reply; 2+ messages in thread
From: Al Viro @ 2025-07-02 21:25 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: linux-fsdevel, linux-pm

1) unbalanced debugfs_file_get().  Not needed in the first place -
file_operations are accessed only via debugfs_create_file(), so
debugfs wrappers will take care of that itself.

2) kmalloc() for a buffer used only for duration of a function is not
a problem, but for a buffer no longer than 16 bytes?

3) strstr() is for finding substrings; for finding a character there's
strchr().

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
---
 drivers/thermal/testing/command.c | 30 ++++++++++--------------------
 1 file changed, 10 insertions(+), 20 deletions(-)

diff --git a/drivers/thermal/testing/command.c b/drivers/thermal/testing/command.c
index ba11d70e8021..1159ecea57e7 100644
--- a/drivers/thermal/testing/command.c
+++ b/drivers/thermal/testing/command.c
@@ -139,31 +139,21 @@ static int tt_command_exec(int index, const char *arg)
 	return ret;
 }
 
-static ssize_t tt_command_process(struct dentry *dentry, const char __user *user_buf,
-				  size_t count)
+static ssize_t tt_command_process(char *s)
 {
-	char *buf __free(kfree);
 	char *arg;
 	int i;
 
-	buf = kmalloc(count + 1, GFP_KERNEL);
-	if (!buf)
-		return -ENOMEM;
+	strim(s);
 
-	if (copy_from_user(buf, user_buf, count))
-		return -EFAULT;
-
-	buf[count] = '\0';
-	strim(buf);
-
-	arg = strstr(buf, ":");
+	arg = strchr(s, ':');
 	if (arg) {
 		*arg = '\0';
 		arg++;
 	}
 
 	for (i = 0; i < ARRAY_SIZE(tt_command_strings); i++) {
-		if (!strcmp(buf, tt_command_strings[i]))
+		if (!strcmp(s, tt_command_strings[i]))
 			return tt_command_exec(i, arg);
 	}
 
@@ -173,20 +163,20 @@ static ssize_t tt_command_process(struct dentry *dentry, const char __user *user
 static ssize_t tt_command_write(struct file *file, const char __user *user_buf,
 				size_t count, loff_t *ppos)
 {
-	struct dentry *dentry = file->f_path.dentry;
+	char buf[TT_COMMAND_SIZE];
 	ssize_t ret;
 
 	if (*ppos)
 		return -EINVAL;
 
-	if (count + 1 > TT_COMMAND_SIZE)
+	if (count > TT_COMMAND_SIZE - 1)
 		return -E2BIG;
 
-	ret = debugfs_file_get(dentry);
-	if (unlikely(ret))
-		return ret;
+	if (copy_from_user(buf, user_buf, count))
+		return -EFAULT;
+	buf[count] = '\0';
 
-	ret = tt_command_process(dentry, user_buf, count);
+	ret = tt_command_process(buf);
 	if (ret)
 		return ret;
 
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH 08/11] fix tt_command_write()
  2025-07-02 21:25   ` [PATCH 08/11] fix tt_command_write() Al Viro
@ 2025-07-03 11:14     ` Rafael J. Wysocki
  0 siblings, 0 replies; 2+ messages in thread
From: Rafael J. Wysocki @ 2025-07-03 11:14 UTC (permalink / raw)
  To: Al Viro; +Cc: Greg Kroah-Hartman, linux-fsdevel, linux-pm

On Wed, Jul 2, 2025 at 11:25 PM Al Viro <viro@zeniv.linux.org.uk> wrote:
>
> 1) unbalanced debugfs_file_get().  Not needed in the first place -
> file_operations are accessed only via debugfs_create_file(), so
> debugfs wrappers will take care of that itself.
>
> 2) kmalloc() for a buffer used only for duration of a function is not
> a problem, but for a buffer no longer than 16 bytes?
>
> 3) strstr() is for finding substrings; for finding a character there's
> strchr().
>
> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>

Acked-by: Rafael J. Wysocki <rafael@kernel.org>

Or do you want me to apply this?

> ---
>  drivers/thermal/testing/command.c | 30 ++++++++++--------------------
>  1 file changed, 10 insertions(+), 20 deletions(-)
>
> diff --git a/drivers/thermal/testing/command.c b/drivers/thermal/testing/command.c
> index ba11d70e8021..1159ecea57e7 100644
> --- a/drivers/thermal/testing/command.c
> +++ b/drivers/thermal/testing/command.c
> @@ -139,31 +139,21 @@ static int tt_command_exec(int index, const char *arg)
>         return ret;
>  }
>
> -static ssize_t tt_command_process(struct dentry *dentry, const char __user *user_buf,
> -                                 size_t count)
> +static ssize_t tt_command_process(char *s)
>  {
> -       char *buf __free(kfree);
>         char *arg;
>         int i;
>
> -       buf = kmalloc(count + 1, GFP_KERNEL);
> -       if (!buf)
> -               return -ENOMEM;
> +       strim(s);
>
> -       if (copy_from_user(buf, user_buf, count))
> -               return -EFAULT;
> -
> -       buf[count] = '\0';
> -       strim(buf);
> -
> -       arg = strstr(buf, ":");
> +       arg = strchr(s, ':');
>         if (arg) {
>                 *arg = '\0';
>                 arg++;
>         }
>
>         for (i = 0; i < ARRAY_SIZE(tt_command_strings); i++) {
> -               if (!strcmp(buf, tt_command_strings[i]))
> +               if (!strcmp(s, tt_command_strings[i]))
>                         return tt_command_exec(i, arg);
>         }
>
> @@ -173,20 +163,20 @@ static ssize_t tt_command_process(struct dentry *dentry, const char __user *user
>  static ssize_t tt_command_write(struct file *file, const char __user *user_buf,
>                                 size_t count, loff_t *ppos)
>  {
> -       struct dentry *dentry = file->f_path.dentry;
> +       char buf[TT_COMMAND_SIZE];
>         ssize_t ret;
>
>         if (*ppos)
>                 return -EINVAL;
>
> -       if (count + 1 > TT_COMMAND_SIZE)
> +       if (count > TT_COMMAND_SIZE - 1)
>                 return -E2BIG;
>
> -       ret = debugfs_file_get(dentry);
> -       if (unlikely(ret))
> -               return ret;
> +       if (copy_from_user(buf, user_buf, count))
> +               return -EFAULT;
> +       buf[count] = '\0';
>
> -       ret = tt_command_process(dentry, user_buf, count);
> +       ret = tt_command_process(buf);
>         if (ret)
>                 return ret;
>
> --
> 2.39.5
>
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-07-03 11:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20250702211305.GE1880847@ZenIV>
     [not found] ` <20250702211408.GA3406663@ZenIV>
2025-07-02 21:25   ` [PATCH 08/11] fix tt_command_write() Al Viro
2025-07-03 11:14     ` Rafael J. Wysocki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox