Hi!

On 2017-06-27T13:10:19+0200, Tom de Vries <[email protected]> wrote:
> On 06/27/2017 09:38 AM, Jakub Jelinek wrote:
>> On Tue, Jun 27, 2017 at 09:17:57AM +0200, Tom de Vries wrote:
>>> This patch uses secure_getenv for GOMP_DEBUG.
>>>
>>> It factors out the secure_getenv code from plugin-hsa.c into
>>> libgomp/secure_getenv.h, and reuses it in env.c.

>> Another thing is that we probably want to follow what libgfortran does for
>> the case where secure_getenv isn't available, but __secure_getenv is -
>> in particular emit this function (if geteuid and getegid are present), but
>> emit a weakref call to __secure_getenv first if non-NULL.
>> 
>
> I've copied the approach from libgfortran.

That is:

> --- /dev/null
> +++ b/libgomp/secure_getenv.h
> @@ -0,0 +1,60 @@
> +[...]
> +
> +/* Secure getenv() which returns NULL if running as SUID/SGID.  */
> +#ifndef HAVE_SECURE_GETENV
> +#if defined (HAVE_UNISTD_H) && defined(HAVE_GETUID) && defined(HAVE_GETEUID) 
> \
> +  && defined(HAVE_GETGID) && defined(HAVE_GETEGID)
> +
> +#include <unistd.h>
> +
> +#if SUPPORTS_WEAKREF && defined(HAVE___SECURE_GETENV)
> +static char* weak_secure_getenv (const char*)
> +  __attribute__((__weakref__("__secure_getenv")));
> +#endif
> +
> +/* Implementation of secure_getenv() for targets where it is not provided but
> +   we have at least means to test real and effective IDs.  */
> +
> +static inline char *
> +secure_getenv (const char *name)
> +{
> +#if SUPPORTS_WEAKREF && defined(HAVE___SECURE_GETENV)
> +  if (weak_secure_getenv)
> +    return weak_secure_getenv (name);
> +#endif
> +
> +  if ((getuid () == geteuid ()) && (getgid () == getegid ()))
> +    return getenv (name);
> +  else
> +    return NULL;
> +}
> +#else
> +#define secure_getenv getenv
> +#endif
> +#endif
> +
> +#endif /* _SECURE_GETENV_H.  */

However, what "copied the approach from libgfortran" didn't include was
also copying from libgfortran the 'configure'-time
'libgfortran/acinclude.m4:LIBGFOR_CHECK_WEAKREF' check.  That means, in
the libgomp setting cited above, '#if SUPPORTS_WEAKREF' never holds, and
thus, the 'weak_secure_getenv' handling is never active.

Per glibc 'NEWS', '__secure_getenv' was a pre-glibc 2.16 feature, and
with glibc 2.17 (released 2012-12-25), it became 'secure_getenv' (which
libgomp handles fine).  Is it, now, almost 14 years after glibc 2.17,
still necessary to make the 'weak_secure_getenv' case work, or may I turn
the code cited above into the following?

    #ifndef HAVE_SECURE_GETENV
    #ifdef HAVE___SECURE_GETENV
    #define secure_getenv __secure_getenv
    #elif defined (HAVE_UNISTD_H) && defined(HAVE_GETUID) && 
defined(HAVE_GETEUID) \
      && defined(HAVE_GETGID) && defined(HAVE_GETEGID)
    
    #include <unistd.h>
    
    /* Implementation of secure_getenv() for targets where it is not provided 
but
       we have at least means to test real and effective IDs. */
    
    static char *
    secure_getenv (const char *name)
    {
      if ((getuid () == geteuid ()) && (getgid () == getegid ()))
        return getenv (name);
      else
        return NULL;
    }
    
    #else
    #define secure_getenv getenv
    #endif
    #endif


Grüße
 Thomas

Reply via email to