Patchwork getlogin_r: return early when linux sentinel value is set

login
register
mail settings
Submitter Jesse Hathaway
Date March 20, 2018, 5:52 p.m.
Message ID <CANSNSoVxHOAM9XZx5ghighXLmXQzSvBfy4xp_FU8=_+U2ZXk8Q@mail.gmail.com>
Download mbox | patch
Permalink /patch/26390/
State New
Headers show

Comments

Jesse Hathaway - March 20, 2018, 5:52 p.m.
Thanks for the review Adhemerval:

On Tue, Mar 20, 2018 at 3:00 AM, Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
> The change is short enough so I think it won't require a copyright
> assignment.  However it does require a ChangeLog entry, could you please
> resend the patch with a proper one?

added

> > diff --git a/sysdeps/unix/sysv/linux/getlogin_r.c
> > b/sysdeps/unix/sysv/linux/getlogin_r.c
> > index 73ea14c8f9..43f55a2188 100644
> > --- a/sysdeps/unix/sysv/linux/getlogin_r.c
> > +++ b/sysdeps/unix/sysv/linux/getlogin_r.c
> > @@ -55,6 +55,12 @@ __getlogin_r_loginuid (char *name, size_t namesize)
> >     endp == uidbuf || *endp != '\0'))
> >      return -1;
> >
> > +  /* If there is no login uid, linux sets /proc/self/loginid to the sentinel
> > +     value of 4294967295, so check if the value is set and return early to
> > +     avoid making unneeded nss lookups. */
> > +  if (uid == 4294967295)
> > +    return ENXIO;
>
> I prefer to just use either (int)-1 or just 0xffffffff.

changed to (uid_t) - 1

> Also,
> __getlogin_r_loginuid should set errno itself as for ERANGE instead
> of just return its value (errno won't be set in this case and I think
> it got it wrong for ENOMEM in this case).

Would you be so kind as to explain this a little more to someone with
very little C experience. I returned the errno value because the
comment on the function indicates that is what the caller expects:

/* Try to determine login name from /proc/self/loginuid and return 0
   if successful.  If /proc/self/loginuid cannot be read return -1.
   Otherwise return the error number.  */

and getlogin when it calls __getlogin_r_loginuid, only checks the
return value, and not the errno value

Thanks, Jesse

Updated patch:

From 7ddef30f32b30cd7579ff4b0ea666862022aa0ec Mon Sep 17 00:00:00 2001
From: Jesse Hathaway <jesse@mbuki-mvuki.org>
Date: Fri, 16 Mar 2018 10:46:50 -0500
Subject: [PATCH] getlogin_r: return early when linux sentinel value is set

When there is no login uid Linux sets /proc/self/loginid to the sentinel
value of, (uid_t) - 1. If this is set we can return early and avoid
needlessly looking up the sentinel value in any configured nss
databases.
Andreas Schwab - March 20, 2018, 6:30 p.m.
On Mär 20 2018, Jesse Hathaway <jesse@mbuki-mvuki.org> wrote:

>> Also,
>> __getlogin_r_loginuid should set errno itself as for ERANGE instead
>> of just return its value (errno won't be set in this case and I think
>> it got it wrong for ENOMEM in this case).
>
> Would you be so kind as to explain this a little more to someone with
> very little C experience. I returned the errno value because the
> comment on the function indicates that is what the caller expects:
>
> /* Try to determine login name from /proc/self/loginuid and return 0
>    if successful.  If /proc/self/loginuid cannot be read return -1.
>    Otherwise return the error number.  */
>
> and getlogin when it calls __getlogin_r_loginuid, only checks the
> return value, and not the errno value

Depends on whether you want the linux __getlogin_r to fall back to the
generic __getlogin_r.

Andreas.

Patch

diff --git a/ChangeLog b/ChangeLog
index 3399e567b8..1ceff1c094 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@ 
+2018-03-20  Jesse Hathaway  <jesse@mbuki-mvuki.org>  (tiny change)
+
+ * sysdeps/unix/sysv/linux/getlogin_r.c (__getlogin_r_loginuid): return early
+ when linux sentinel value is set.
+
 2018-03-20  Samuel Thibault  <samuel.thibault@ens-lyon.org>

  * manual/errno.texi (EOWNERDEAD, ENOTRECOVERABLE): Remove errno
diff --git a/sysdeps/unix/sysv/linux/getlogin_r.c
b/sysdeps/unix/sysv/linux/getlogin_r.c
index 73ea14c8f9..8f0e997d43 100644
--- a/sysdeps/unix/sysv/linux/getlogin_r.c
+++ b/sysdeps/unix/sysv/linux/getlogin_r.c
@@ -55,6 +55,12 @@  __getlogin_r_loginuid (char *name, size_t namesize)
    endp == uidbuf || *endp != '\0'))
     return -1;

+  /* If there is no login uid, linux sets /proc/self/loginid to the sentinel
+     value of, (uid_t) - 1, so check if that value is set and return early to
+     avoid making unneeded nss lookups. */
+  if (uid == (uid_t) - 1)
+    return ENXIO;
+
   struct passwd pwd;
   struct passwd *tpwd;
   int result = 0;