(root)/
glibc-2.38/
elf/
tst-auditmod28.c
       1  /* Check the usability of <dlfcn.h> functions in audit modules.  Audit module.
       2     Copyright (C) 2022-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <dlfcn.h>
      20  #include <first-versions.h>
      21  #include <gnu/lib-names.h>
      22  #include <link.h>
      23  #include <stdio.h>
      24  #include <string.h>
      25  #include <unistd.h>
      26  
      27  #include <support/check.h>
      28  #include <support/xdlfcn.h>
      29  
      30  unsigned int
      31  la_version (unsigned int current)
      32  {
      33    /* Exercise various <dlfcn.h> functions.  */
      34  
      35    /* Check dlopen, dlsym, dlclose.   */
      36    void *handle = xdlopen (LIBM_SO, RTLD_LOCAL | RTLD_NOW);
      37    void *ptr = xdlsym (handle, "sincos");
      38    TEST_VERIFY (ptr != NULL);
      39    ptr = dlsym (handle, "SINCOS");
      40    TEST_VERIFY (ptr == NULL);
      41    const char *message = dlerror ();
      42    TEST_VERIFY (strstr (message, ": undefined symbol: SINCOS") != NULL);
      43    ptr = dlsym (handle, "SINCOS");
      44    TEST_VERIFY (ptr == NULL);
      45    xdlclose (handle);
      46    TEST_COMPARE_STRING (dlerror (), NULL);
      47  
      48    handle = xdlopen (LIBC_SO, RTLD_LOCAL | RTLD_NOW | RTLD_NOLOAD);
      49  
      50    /* Check dlvsym.  _exit is unlikely to gain another symbol
      51       version.  */
      52    TEST_VERIFY (xdlsym (handle, "_exit")
      53                 == xdlvsym (handle, "_exit", FIRST_VERSION_libc__exit_STRING));
      54  
      55    /* Check dlinfo.  */
      56    {
      57      void *handle2 = NULL;
      58      TEST_COMPARE (dlinfo (handle, RTLD_DI_LINKMAP, &handle2), 0);
      59      TEST_VERIFY (handle2 == handle);
      60    }
      61  
      62    /* Check dladdr and dladdr1.  */
      63    Dl_info info = { };
      64    TEST_VERIFY (dladdr (&_exit, &info) != 0);
      65    if (strcmp (info.dli_sname, "_Exit") != 0) /* _Exit is an alias.  */
      66      TEST_COMPARE_STRING (info.dli_sname, "_exit");
      67    TEST_VERIFY (info.dli_saddr == &_exit);
      68    TEST_VERIFY (strstr (info.dli_fname, LIBC_SO));
      69    void *extra_info;
      70    memset (&info, 0, sizeof (info));
      71    TEST_VERIFY (dladdr1 (&_exit, &info, &extra_info, RTLD_DL_LINKMAP) != 0);
      72    TEST_VERIFY (extra_info == handle);
      73  
      74    /* Check _dl_find_object.  */
      75    struct dl_find_object dlfo;
      76    void *ret_addr = __builtin_extract_return_addr (__builtin_return_address (0));
      77    int ret_dl_find_object =_dl_find_object (ret_addr, &dlfo);
      78    TEST_COMPARE (ret_dl_find_object, 0);
      79    if (ret_dl_find_object == 0)
      80      {
      81        /* "ld.so" is seen with --enable-hardcoded-path-in-tests.  */
      82        if (strcmp (basename (dlfo.dlfo_link_map->l_name), "ld.so") != 0)
      83  	TEST_COMPARE_STRING (basename (dlfo.dlfo_link_map->l_name), LD_SO);
      84      }
      85    ret_dl_find_object = _dl_find_object (dlsym (handle, "environ"), &dlfo);
      86    TEST_COMPARE (ret_dl_find_object, 0);
      87    if (ret_dl_find_object == 0)
      88      TEST_COMPARE_STRING (basename (dlfo.dlfo_link_map->l_name), LIBC_SO);
      89    TEST_COMPARE (_dl_find_object ((void *) 1, &dlfo), -1);
      90    TEST_COMPARE (_dl_find_object ((void *) -1, &dlfo), -1);
      91  
      92    /* Verify that dlmopen creates a new namespace.  */
      93    void *dlmopen_handle = xdlmopen (LM_ID_NEWLM, LIBC_SO, RTLD_NOW);
      94    TEST_VERIFY (dlmopen_handle != handle);
      95    memset (&info, 0, sizeof (info));
      96    extra_info = NULL;
      97    ptr = xdlsym (dlmopen_handle, "_exit");
      98    TEST_VERIFY (dladdr1 (ptr, &info, &extra_info, RTLD_DL_LINKMAP) != 0);
      99    TEST_VERIFY (extra_info == dlmopen_handle);
     100    xdlclose (dlmopen_handle);
     101  
     102    /* Terminate the process with an error state.  This does not happen
     103       automatically because the audit module state is not shared with
     104       the main program.  */
     105    if (support_record_failure_is_failed ())
     106      {
     107        fflush (stdout);
     108        fflush (stderr);
     109        _exit (1);
     110      }
     111  
     112    return LAV_CURRENT;
     113  }
     114  
     115  char *
     116  la_objsearch (const char *name, uintptr_t *cookie, unsigned int flag)
     117  {
     118    if (strcmp (name, "mapped to libc") == 0)
     119      return (char *) LIBC_SO;
     120    else
     121      return (char *) name;
     122  }