Bug 22299 - Problem with $PLATFORM on x86_64 platform
Summary: Problem with $PLATFORM on x86_64 platform
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: dynamic-link (show other bugs)
Version: 2.26
: P2 normal
Target Milestone: 2.27
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-10-15 14:18 UTC by Valery
Modified: 2017-10-26 15:10 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2017-10-16 00:00:00
fweimer: security-


Attachments
patch that avoid reset GLRO(dl_platform) to the NULL (587 bytes, patch)
2017-10-17 06:08 UTC, Valery
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Valery 2017-10-15 14:18:15 UTC
For glibc 2.26 when running
strace -ELD_PRELOAD='/sss/${PLATFORM}/dddd'  -s300  /bin/cat

I got following output:

execve("/bin/cat", ["/bin/cat"], [/* 32 vars */]) = 0
brk(NULL)                               = 0x556740829000
readlink("/proc/self/exe", "/bin/cat", 4096) = 8
openat(AT_FDCWD, "", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
writev(2, [{"ERROR: ld.so: object '", 22}, {"/sss/${PLATFORM}/dddd", 21}, {"' from ", 7}, {"LD_PRELOAD", 10}, {" cannot be preloaded (", 22}, {"cannot open shared object file", 30}, {"): ignored.\n", 12}], 7ERROR: ld.so: object '/sss/${PLATFORM}/dddd' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.

For glibc-2.22:
execve("/bin/cat", ["/bin/cat"], [/* 32 vars */]) = 0
brk(NULL)                               = 0x55f02a3b2000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f410eaec000
readlink("/proc/self/exe", "/usr/bin/cat", 4096) = 12
open("/sss/x86_64/dddd", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
writev(2, [{"ERROR: ld.so: object '", 22}, {"/sss/${PLATFORM}/dddd", 21}, {"' from ", 7}, {"LD_PRELOAD", 10}, {" cannot be preloaded (", 22}, {"cannot open shared object file", 30}, {"): ignored.\n", 12}], 7ERROR: ld.so: object '/sss/${PLATFORM}/dddd' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
) = 124

Version 2.22 correctly expand $PLATFORM and attempts to load "/sss/x86_64/dddd", as expected

Version 2.26 instead tried to openat file with empty name:
openat(AT_FDCWD, "", O_RDONLY|O_CLOEXEC)

I think that problem is GLRO(dl_platform) == NULL.

How it came to be?
File elf/dl-sysdep.c
function _dl_sysdep_start set PLATFORM at line 156:
      case AT_PLATFORM:                                                         
       GLRO(dl_platform) = (void *) av->a_un.a_val;                            
       break;    

But after that at line 231 calls DL_PLATFORM_INIT
#ifdef DL_PLATFORM_INIT                                                         
  DL_PLATFORM_INIT;                                                             
#endif 

DL_PLATFORM_INIT defined to dl_platform_init();

File sysdeps/x86_64/dl-machine.h, line 240
static inline void __attribute__ ((unused))                                     
dl_platform_init (void)                                                         
{                                                                               
#if IS_IN (rtld)                                                                
  /* init_cpu_features has been called early from __libc_start_main in          
     static executable.  */                                                     
  init_cpu_features (&GLRO(dl_x86_cpu_features));                               
#else                                                                           
  if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')                  
    /* Avoid an empty string which would disturb us.  */                        
    GLRO(dl_platform) = NULL;                                                   
#endif                                                                          
}   


function init_cpu_features first of all reset dl_platform:
File sysdeps/x86/cpu-features.c, line 334
  /* Reuse dl_platform, dl_hwcap and dl_hwcap_mask for x86.  */                 
  GLRO(dl_platform) = NULL;                                                     
  GLRO(dl_hwcap) = 0; 

And then, if special processor detected, set it to "haswell" or "xeon_phi"
But if no special processor detected, dl_platform will remain NULL, not x86_64

File sysdeps/x86/cpu-features.c, line 343
#ifdef __x86_64__                                                               
  if (cpu_features->kind == arch_kind_intel)                                    
    {                                                                           
      if (CPU_FEATURES_ARCH_P (cpu_features, AVX512F_Usable)                    
|         && CPU_FEATURES_CPU_P (cpu_features, AVX512CD))                       
|       {                                                                       
|         if (CPU_FEATURES_CPU_P (cpu_features, AVX512ER))                      
|           {                                                                   
|             if (CPU_FEATURES_CPU_P (cpu_features, AVX512PF))                  
|       |       GLRO(dl_platform) = "xeon_phi";                                 
|           }                                                                   
|         else                                                                  
|           {                                                                   
|             if (CPU_FEATURES_CPU_P (cpu_features, AVX512BW)                   
|       |         && CPU_FEATURES_CPU_P (cpu_features, AVX512DQ)                
|       |         && CPU_FEATURES_CPU_P (cpu_features, AVX512VL))               
|       |       GLRO(dl_hwcap) |= HWCAP_X86_AVX512_1;                           
|           }                                                                   
|       }                                                                       
                                                                                
      if (GLRO(dl_platform) == NULL                                             
|         && CPU_FEATURES_ARCH_P (cpu_features, AVX2_Usable)                    
|         && CPU_FEATURES_ARCH_P (cpu_features, FMA_Usable)                     
|         && CPU_FEATURES_CPU_P (cpu_features, BMI1)                            
|         && CPU_FEATURES_CPU_P (cpu_features, BMI2)                            
|         && CPU_FEATURES_CPU_P (cpu_features, LZCNT)                           
|         && CPU_FEATURES_CPU_P (cpu_features, MOVBE)                           
|         && CPU_FEATURES_CPU_P (cpu_features, POPCNT))                         
|       GLRO(dl_platform) = "haswell";                                          
    }                                                                           
#else
Comment 1 H.J. Lu 2017-10-16 20:31:21 UTC
Is this a dup of BZ 22093?
Comment 2 H.J. Lu 2017-10-16 20:32:08 UTC
A dup of PR 22093?
Comment 3 Valery 2017-10-17 06:08:44 UTC
Created attachment 10534 [details]
patch that avoid reset GLRO(dl_platform) to the NULL
Comment 4 Sourceware Commits 2017-10-19 15:29:55 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU C Library master sources".

The branch, master has been updated
       via  4d916f0f12b230f49967797f98b2b613c734a047 (commit)
      from  4b0fa403ab14d046e1b13de397016cef1c06acae (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=4d916f0f12b230f49967797f98b2b613c734a047

commit 4d916f0f12b230f49967797f98b2b613c734a047
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Oct 19 08:28:09 2017 -0700

    x86-64: Don't set GLRO(dl_platform) to NULL [BZ #22299]
    
    Since ld.so expands $PLATFORM with GLRO(dl_platform), don't set
    GLRO(dl_platform) to NULL.
    
    	[BZ #22299]
    	* sysdeps/x86/cpu-features.c (init_cpu_features): Don't set
    	GLRO(dl_platform) to NULL.
    	* sysdeps/x86_64/Makefile (tests): Add tst-platform-1.
    	(modules-names): Add tst-platformmod-1 and
    	x86_64/tst-platformmod-2.
    	(CFLAGS-tst-platform-1.c): New.
    	(CFLAGS-tst-platformmod-1.c): Likewise.
    	(CFLAGS-tst-platformmod-2.c): Likewise.
    	(LDFLAGS-tst-platformmod-2.so): Likewise.
    	($(objpfx)tst-platform-1): Likewise.
    	($(objpfx)tst-platform-1.out): Likewise.
    	(tst-platform-1-ENV): Likewise.
    	($(objpfx)x86_64/tst-platformmod-2.os): Likewise.
    	* sysdeps/x86_64/tst-platform-1.c: New file.
    	* sysdeps/x86_64/tst-platformmod-1.c: Likewise.
    	* sysdeps/x86_64/tst-platformmod-2.c: Likewise.

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog                          |   21 +++++++++++++++++++++
 sysdeps/x86/cpu-features.c         |   12 ++++++++----
 sysdeps/x86_64/Makefile            |   20 ++++++++++++++++++++
 sysdeps/x86_64/tst-platform-1.c    |   29 +++++++++++++++++++++++++++++
 sysdeps/x86_64/tst-platformmod-1.c |   23 +++++++++++++++++++++++
 sysdeps/x86_64/tst-platformmod-2.c |   23 +++++++++++++++++++++++
 6 files changed, 124 insertions(+), 4 deletions(-)
 create mode 100644 sysdeps/x86_64/tst-platform-1.c
 create mode 100644 sysdeps/x86_64/tst-platformmod-1.c
 create mode 100644 sysdeps/x86_64/tst-platformmod-2.c
Comment 5 H.J. Lu 2017-10-19 15:44:47 UTC
Fixed for 2.27.
Comment 6 Sourceware Commits 2017-10-26 15:10:28 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU C Library master sources".

The branch, release/2.26/master has been updated
       via  4b692dffb95ac4812b161eb6a16113d7e824982e (commit)
      from  77eea8950ce5495ff033bca3465ccfd2db072ae6 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=4b692dffb95ac4812b161eb6a16113d7e824982e

commit 4b692dffb95ac4812b161eb6a16113d7e824982e
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Oct 19 08:28:09 2017 -0700

    x86-64: Don't set GLRO(dl_platform) to NULL [BZ #22299]
    
    Since ld.so expands $PLATFORM with GLRO(dl_platform), don't set
    GLRO(dl_platform) to NULL.
    
    	[BZ #22299]
    	* sysdeps/x86/cpu-features.c (init_cpu_features): Don't set
    	GLRO(dl_platform) to NULL.
    	* sysdeps/x86_64/Makefile (tests): Add tst-platform-1.
    	(modules-names): Add tst-platformmod-1 and
    	x86_64/tst-platformmod-2.
    	(CFLAGS-tst-platform-1.c): New.
    	(CFLAGS-tst-platformmod-1.c): Likewise.
    	(CFLAGS-tst-platformmod-2.c): Likewise.
    	(LDFLAGS-tst-platformmod-2.so): Likewise.
    	($(objpfx)tst-platform-1): Likewise.
    	($(objpfx)tst-platform-1.out): Likewise.
    	(tst-platform-1-ENV): Likewise.
    	($(objpfx)x86_64/tst-platformmod-2.os): Likewise.
    	* sysdeps/x86_64/tst-platform-1.c: New file.
    	* sysdeps/x86_64/tst-platformmod-1.c: Likewise.
    	* sysdeps/x86_64/tst-platformmod-2.c: Likewise.
    
    (cherry picked from commit 4d916f0f12b230f49967797f98b2b613c734a047)

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog                          |   21 +++++++++++++++++++++
 NEWS                               |    1 +
 sysdeps/x86/cpu-features.c         |   12 ++++++++----
 sysdeps/x86_64/Makefile            |   20 ++++++++++++++++++++
 sysdeps/x86_64/tst-platform-1.c    |   29 +++++++++++++++++++++++++++++
 sysdeps/x86_64/tst-platformmod-1.c |   23 +++++++++++++++++++++++
 sysdeps/x86_64/tst-platformmod-2.c |   23 +++++++++++++++++++++++
 7 files changed, 125 insertions(+), 4 deletions(-)
 create mode 100644 sysdeps/x86_64/tst-platform-1.c
 create mode 100644 sysdeps/x86_64/tst-platformmod-1.c
 create mode 100644 sysdeps/x86_64/tst-platformmod-2.c