Avoid use of strlen in getlogin_r (bug 22447)

Message ID alpine.DEB.2.20.1711221730500.30870@digraph.polyomino.org.uk
State New, archived
Headers

Commit Message

Joseph Myers Nov. 22, 2017, 5:31 p.m. UTC
  Building glibc with current mainline GCC fails, among other reasons,
because of an error for use of strlen on the nonstring ut_user field.
This patch changes the problem code in getlogin_r to use __strnlen
instead.  It also needs to set the trailing NUL byte of the result
explicitly, because of the case where ut_user does not have such a
trailing NUL byte (but the result should always have one).

Tested for x86_64.  Also tested that, in conjunction with
<https://sourceware.org/ml/libc-alpha/2017-11/msg00797.html>, it fixes
the build for arm with mainline GCC.

2017-11-22  Joseph Myers  <joseph@codesourcery.com>

	[BZ #22447]
	* sysdeps/unix/getlogin_r.c (__getlogin_r): Use __strnlen not
	strlen to compute length of ut_user and set trailing NUL byte of
	result explicitly.
  

Comments

Paul Eggert Nov. 22, 2017, 6:08 p.m. UTC | #1
Thanks, this looks good.
  
Carlos O'Donell Nov. 22, 2017, 7:27 p.m. UTC | #2
On 11/22/2017 09:31 AM, Joseph Myers wrote:
> Building glibc with current mainline GCC fails, among other reasons,
> because of an error for use of strlen on the nonstring ut_user field.
> This patch changes the problem code in getlogin_r to use __strnlen
> instead.  It also needs to set the trailing NUL byte of the result
> explicitly, because of the case where ut_user does not have such a
> trailing NUL byte (but the result should always have one).
> 
> Tested for x86_64.  Also tested that, in conjunction with
> <https://sourceware.org/ml/libc-alpha/2017-11/msg00797.html>, it fixes
> the build for arm with mainline GCC.
> 
> 2017-11-22  Joseph Myers  <joseph@codesourcery.com>
> 
> 	[BZ #22447]
> 	* sysdeps/unix/getlogin_r.c (__getlogin_r): Use __strnlen not
> 	strlen to compute length of ut_user and set trailing NUL byte of
> 	result explicitly.

LGTM.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>

> diff --git a/sysdeps/unix/getlogin_r.c b/sysdeps/unix/getlogin_r.c
> index 4a6a40e..ad8e911 100644
> --- a/sysdeps/unix/getlogin_r.c
> +++ b/sysdeps/unix/getlogin_r.c
> @@ -80,7 +80,7 @@ __getlogin_r (char *name, size_t name_len)
>  
>    if (result == 0)
>      {
> -      size_t needed = strlen (ut->ut_user) + 1;
> +      size_t needed = __strnlen (ut->ut_user, UT_NAMESIZE) + 1;
>  
>        if (needed > name_len)
>  	{
> @@ -89,7 +89,8 @@ __getlogin_r (char *name, size_t name_len)
>  	}
>        else
>  	{
> -	  memcpy (name, ut->ut_user, needed);
> +	  memcpy (name, ut->ut_user, needed - 1);
> +	  name[needed - 1] = 0;
>  	  result = 0;
>  	}
>      }
>
  

Patch

diff --git a/sysdeps/unix/getlogin_r.c b/sysdeps/unix/getlogin_r.c
index 4a6a40e..ad8e911 100644
--- a/sysdeps/unix/getlogin_r.c
+++ b/sysdeps/unix/getlogin_r.c
@@ -80,7 +80,7 @@  __getlogin_r (char *name, size_t name_len)
 
   if (result == 0)
     {
-      size_t needed = strlen (ut->ut_user) + 1;
+      size_t needed = __strnlen (ut->ut_user, UT_NAMESIZE) + 1;
 
       if (needed > name_len)
 	{
@@ -89,7 +89,8 @@  __getlogin_r (char *name, size_t name_len)
 	}
       else
 	{
-	  memcpy (name, ut->ut_user, needed);
+	  memcpy (name, ut->ut_user, needed - 1);
+	  name[needed - 1] = 0;
 	  result = 0;
 	}
     }