]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Avoid use of strlen in getlogin_r (bug 22447).
authorJoseph Myers <joseph@codesourcery.com>
Wed, 22 Nov 2017 18:44:23 +0000 (18:44 +0000)
committerFlorian Weimer <fweimer@redhat.com>
Mon, 22 Oct 2018 11:58:39 +0000 (13:58 +0200)
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.

[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.

(cherry picked from commit 4bae615022cb5a5da79ccda83cc6c9ba9f2d479c)

ChangeLog
NEWS
sysdeps/unix/getlogin_r.c

index 9f477058718236ee24dcf9f3e9cca591882826a5..8f5098701c73f4b01bb5e575f3b1d2e0336f3a38 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+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.
+
 2018-10-19  Ilya Yu. Malakhov  <malakhov@mcst.ru>
 
        [BZ #23562]
diff --git a/NEWS b/NEWS
index 33bc5196c89c2d6f7113b0be89db32899a3df403..5569e8d8d2c85a65beb850ae93e93ea9bd6c5b2a 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -127,6 +127,7 @@ The following bugs are resolved with this release:
   [22375] malloc returns pointer from tcache instead of NULL (CVE-2017-17426)
   [22377] Provide a C++ version of iseqsig
   [22442] if_nametoindex: Check length of ifname before copying it
+  [22447] Avoid use of strlen in getlogin_r
   [22627] $ORIGIN in $LD_LIBRARY_PATH is substituted twice
   [22636] PTHREAD_STACK_MIN is too small on x86-64
   [22637] nptl: Fix stack guard size accounting
index 4a6a40eeb2d67e7f6a461c056126589235ba9cbf..ad8e9111f6aa7beed532d94229eb9872860e40e9 100644 (file)
@@ -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;
        }
     }