lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Fri, 21 Jun 2024 14:51:42 +0100
From: Simon Horman <horms@...nel.org>
To: Yafang Shao <laoar.shao@...il.com>
Cc: torvalds@...ux-foundation.org, ebiederm@...ssion.com,
	alexei.starovoitov@...il.com, rostedt@...dmis.org,
	catalin.marinas@....com, akpm@...ux-foundation.org,
	penguin-kernel@...ove.sakura.ne.jp, linux-mm@...ck.org,
	linux-fsdevel@...r.kernel.org, linux-trace-kernel@...r.kernel.org,
	audit@...r.kernel.org, linux-security-module@...r.kernel.org,
	selinux@...r.kernel.org, bpf@...r.kernel.org,
	netdev@...r.kernel.org, dri-devel@...ts.freedesktop.org
Subject: Re: [PATCH v3 06/11] mm/util: Deduplicate code in
 {kstrdup,kstrndup,kmemdup_nul}

On Fri, Jun 21, 2024 at 10:29:54AM +0800, Yafang Shao wrote:
> These three functions follow the same pattern. To deduplicate the code,
> let's introduce a common help __kstrndup().
> 
> Suggested-by: Andrew Morton <akpm@...ux-foundation.org>
> Signed-off-by: Yafang Shao <laoar.shao@...il.com>

Hi Yafang Shao,

Some minor nits from my side.

> ---
>  mm/internal.h | 24 ++++++++++++++++++++++++
>  mm/util.c     | 27 ++++-----------------------
>  2 files changed, 28 insertions(+), 23 deletions(-)
> 
> diff --git a/mm/internal.h b/mm/internal.h
> index b2c75b12014e..fd87f685739b 100644
> --- a/mm/internal.h
> +++ b/mm/internal.h
> @@ -1521,4 +1521,28 @@ static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry,
>  void workingset_update_node(struct xa_node *node);
>  extern struct list_lru shadow_nodes;
>  
> +/**
> + * __kstrndup - Create a NUL-terminated string from @s, which might be unterminated.
> + * @s: The data to stringify
> + * @len: The size of the data, including the null terminator
> + * @gfp: the GFP mask used in the kmalloc() call when allocating memory
> + *
> + * Return: newly allocated copy of @s with NUL-termination or %NULL in
> + * case of error
> + */
> +static __always_inline char *__kstrndup(const char *s, size_t len, gfp_t gfp)
> +{
> +	char *buf;
> +
> +	buf = kmalloc_track_caller(len, gfp);
> +	if (!buf)
> +		return NULL;
> +
> +	memcpy(buf, s, len);
> +	/* Ensure the buf is always NUL-terminated, regardless of @s. */
> +	buf[len - 1] = '\0';
> +	return buf;
> +}
> +
> +

nit: One blank line is enough.

>  #endif	/* __MM_INTERNAL_H */
> diff --git a/mm/util.c b/mm/util.c
> index 41c7875572ed..d9135c5fdf7f 100644
> --- a/mm/util.c
> +++ b/mm/util.c
> @@ -58,17 +58,8 @@ char *kstrdup(const char *s, gfp_t gfp)
>  	if (!s)
>  		return NULL;
>  
> -	len = strlen(s) + 1;
> -	buf = kmalloc_track_caller(len, gfp);
> -	if (buf) {
> -		memcpy(buf, s, len);
> -		/* During memcpy(), the string might be updated to a new value,
> -		 * which could be longer than the string when strlen() is
> -		 * called. Therefore, we need to add a null termimator.
> -		 */
> -		buf[len - 1] = '\0';
> -	}
> -	return buf;

nit: The local variable buf is now unused, and should be removed from kstrdup().
     Likewise for kstrndup() and kmemdup_nul()

     Flagged by W=1 builds with gcc-13 and clang-18, and Smatch.

> +	len = strlen(s);
> +	return __kstrndup(s, len + 1, gfp);
>  }
>  EXPORT_SYMBOL(kstrdup);
>  
> @@ -111,12 +102,7 @@ char *kstrndup(const char *s, size_t max, gfp_t gfp)
>  		return NULL;
>  
>  	len = strnlen(s, max);
> -	buf = kmalloc_track_caller(len+1, gfp);
> -	if (buf) {
> -		memcpy(buf, s, len);
> -		buf[len] = '\0';
> -	}
> -	return buf;
> +	return __kstrndup(s, len + 1, gfp);
>  }
>  EXPORT_SYMBOL(kstrndup);
>  
> @@ -195,12 +181,7 @@ char *kmemdup_nul(const char *s, size_t len, gfp_t gfp)
>  	if (!s)
>  		return NULL;
>  
> -	buf = kmalloc_track_caller(len + 1, gfp);
> -	if (buf) {
> -		memcpy(buf, s, len);
> -		buf[len] = '\0';
> -	}
> -	return buf;
> +	return __kstrndup(s, len + 1, gfp);
>  }
>  EXPORT_SYMBOL(kmemdup_nul);
>  
> -- 
> 2.39.1
> 
> 

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ