diff mbox series

[v2] proc: alloc PATH_MAX bytes for /proc/${pid}/fd/ symlinks

Message ID Ye1fCxyZZ0I5lgOL@localhost.localdomain (mailing list archive)
State New, archived
Headers show
Series [v2] proc: alloc PATH_MAX bytes for /proc/${pid}/fd/ symlinks | expand

Commit Message

Alexey Dobriyan Jan. 23, 2022, 1:58 p.m. UTC
From: Hao Lee <haolee.swjtu@gmail.com>

It's not a standard approach that use __get_free_page() to alloc path
buffer directly. We'd better use kmalloc and PATH_MAX.

	PAGE_SIZE is different on different archs. An unlinked file
	with very long canonical pathname will readlink differently
	because "(deleted)" eats into a buffer.	--adobriyan

Signed-off-by: Hao Lee <haolee.swjtu@gmail.com>
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---

 fs/proc/base.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Hao Lee Jan. 23, 2022, 2:23 p.m. UTC | #1
On Sun, Jan 23, 2022 at 9:58 PM Alexey Dobriyan <adobriyan@gmail.com> wrote:
>
> From: Hao Lee <haolee.swjtu@gmail.com>
>
> It's not a standard approach that use __get_free_page() to alloc path
> buffer directly. We'd better use kmalloc and PATH_MAX.
>
>         PAGE_SIZE is different on different archs.

This is indeed a worthy addition. Thanks.

Regards,
Hao Lee

> An unlinked file
>         with very long canonical pathname will readlink differently
>         because "(deleted)" eats into a buffer. --adobriyan
>
> Signed-off-by: Hao Lee <haolee.swjtu@gmail.com>
> Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
> ---
>
>  fs/proc/base.c |    8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> --- a/fs/proc/base.c
> +++ b/fs/proc/base.c
> @@ -1764,25 +1764,25 @@ static const char *proc_pid_get_link(struct dentry *dentry,
>
>  static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
>  {
> -       char *tmp = (char *)__get_free_page(GFP_KERNEL);
> +       char *tmp = (char *)kmalloc(PATH_MAX, GFP_KERNEL);
>         char *pathname;
>         int len;
>
>         if (!tmp)
>                 return -ENOMEM;
>
> -       pathname = d_path(path, tmp, PAGE_SIZE);
> +       pathname = d_path(path, tmp, PATH_MAX);
>         len = PTR_ERR(pathname);
>         if (IS_ERR(pathname))
>                 goto out;
> -       len = tmp + PAGE_SIZE - 1 - pathname;
> +       len = tmp + PATH_MAX - 1 - pathname;
>
>         if (len > buflen)
>                 len = buflen;
>         if (copy_to_user(buffer, pathname, len))
>                 len = -EFAULT;
>   out:
> -       free_page((unsigned long)tmp);
> +       kfree(tmp);
>         return len;
>  }
>
diff mbox series

Patch

--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -1764,25 +1764,25 @@  static const char *proc_pid_get_link(struct dentry *dentry,
 
 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
 {
-	char *tmp = (char *)__get_free_page(GFP_KERNEL);
+	char *tmp = (char *)kmalloc(PATH_MAX, GFP_KERNEL);
 	char *pathname;
 	int len;
 
 	if (!tmp)
 		return -ENOMEM;
 
-	pathname = d_path(path, tmp, PAGE_SIZE);
+	pathname = d_path(path, tmp, PATH_MAX);
 	len = PTR_ERR(pathname);
 	if (IS_ERR(pathname))
 		goto out;
-	len = tmp + PAGE_SIZE - 1 - pathname;
+	len = tmp + PATH_MAX - 1 - pathname;
 
 	if (len > buflen)
 		len = buflen;
 	if (copy_to_user(buffer, pathname, len))
 		len = -EFAULT;
  out:
-	free_page((unsigned long)tmp);
+	kfree(tmp);
 	return len;
 }