linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexey Dobriyan <adobriyan@gmail.com>
To: Timur Tabi <timur@freescale.com>
Cc: alan@lxorguk.ukuu.org.uk, linux-kernel@vger.kernel.org,
	scottwood@freescale.com, akpm@linux-foundation.org
Subject: Re: [PATCH] lib: introduce strdup_from_user
Date: Fri, 3 Jun 2011 20:45:10 +0300	[thread overview]
Message-ID: <BANLkTikFjF0WF---8A9_CBybf+bUji2smw@mail.gmail.com> (raw)
In-Reply-To: <1307119552-15573-1-git-send-email-timur@freescale.com>

NAK
It accesses userspace data twice.

On Fri, Jun 3, 2011 at 7:45 PM, Timur Tabi <timur@freescale.com> wrote:
> Add function strdup_from_user(), which kmallocs a block of memory and
> copies a user-space NULL-terminated string into it.  NULL is returned if
> the string is too large or cannot be accessed.
>
> This function is added to lib/string_helpers.c, so this file is repurposed
> to store generic "string helper" functions, and not just a function to
> assist in sprintfs.
>
> Signed-off-by: Timur Tabi <timur@freescale.com>
> ---
>  include/linux/string_helpers.h |    2 +
>  lib/string_helpers.c           |   43 +++++++++++++++++++++++++++++++++++++++-
>  2 files changed, 44 insertions(+), 1 deletions(-)
>
> diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h
> index a3eb2f6..6231838 100644
> --- a/include/linux/string_helpers.h
> +++ b/include/linux/string_helpers.h
> @@ -13,4 +13,6 @@ enum string_size_units {
>  int string_get_size(u64 size, enum string_size_units units,
>                    char *buf, int len);
>
> +char *strdup_from_user(const char __user *ustr, size_t max);
> +
>  #endif
> diff --git a/lib/string_helpers.c b/lib/string_helpers.c
> index ab431d4..61323e6 100644
> --- a/lib/string_helpers.c
> +++ b/lib/string_helpers.c
> @@ -1,13 +1,19 @@
>  /*
> - * Helpers for formatting and printing strings
> + * Additional arch-independent string helper functions
>  *
>  * Copyright 31 August 2008 James Bottomley
> + * Copyright (C) 2011 Freescale Semiconductor, Inc.
> + *
>  */
>  #include <linux/kernel.h>
>  #include <linux/math64.h>
>  #include <linux/module.h>
>  #include <linux/string_helpers.h>
>
> +#include <linux/slab.h>
> +#include <linux/err.h>
> +#include <asm/uaccess.h>
> +
>  /**
>  * string_get_size - get the size in the specified units
>  * @size:      The size to be converted
> @@ -66,3 +72,38 @@ int string_get_size(u64 size, const enum string_size_units units,
>        return 0;
>  }
>  EXPORT_SYMBOL(string_get_size);
> +
> +/**
> + * strdup_from_user - copy a user-space string to a kmalloc'd buffer
> + * @ustr: user-space pointer to null-terminated string
> + * @max: maximum size the string is allowed to be, include NULL terminator
> + *
> + * This function allocates a block of memory (using kmalloc) and copies a
> + * user-space string into that buffer.  It returns a pointer to that string,
> + * or an error code.
> + */
> +char *strdup_from_user(const char __user *ustr, size_t max)
> +{
> +       size_t len;
> +       char *str;
> +
> +       len = strnlen_user(ustr, max);
> +       if (len >= max)
> +               return ERR_PTR(-ENAMETOOLONG);
> +
> +       /* strnlen_user returns 0 on access error */
> +       if (!len)
> +               return ERR_PTR(-EFAULT);
> +
> +       str = kzalloc(len, GFP_KERNEL);
> +       if (!str)
> +               return ERR_PTR(-ENOMEM);
> +
> +       if (copy_from_user(str, ustr, len)) {
> +               kfree(str);
> +               return ERR_PTR(-EFAULT);
> +       }
> +
> +       return str;
> +}
> +EXPORT_SYMBOL(strdup_from_user);
> --
> 1.7.3.4
>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/
>

  reply	other threads:[~2011-06-03 17:45 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-03 16:45 [PATCH] lib: introduce strdup_from_user Timur Tabi
2011-06-03 17:45 ` Alexey Dobriyan [this message]
2011-06-03 18:22   ` Timur Tabi
2011-06-03 18:26     ` Alexey Dobriyan
2011-06-03 18:34       ` Timur Tabi
2011-06-03 18:39         ` Alexey Dobriyan
2011-06-03 18:41           ` Timur Tabi
2011-06-03 18:47             ` Alexey Dobriyan
2011-06-03 18:54               ` Timur Tabi
2011-06-03 18:53           ` Scott Wood
2011-06-03 18:57             ` Timur Tabi
2011-06-03 19:12             ` Alexey Dobriyan
2011-06-03 19:31               ` Scott Wood
2011-06-03 22:41               ` Al Viro
2011-06-03 18:37       ` Scott Wood
2011-06-03 21:58   ` Alan Cox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=BANLkTikFjF0WF---8A9_CBybf+bUji2smw@mail.gmail.com \
    --to=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=scottwood@freescale.com \
    --cc=timur@freescale.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).