LCOV - code coverage report
Current view: top level - libdwfl - elf-from-memory.c (source / functions) Coverage Total Hit
Test: elfutils-0.197 Lines: 66.7 % 189 126
Test Date: 2026-10-19 20:07:25 Functions: 100.0 % 1 1
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 56.9 % 109 62

             Branch data     Line data    Source code
       1                 :             : /* Reconstruct an ELF file by reading the segments out of remote memory.
       2                 :             :    Copyright (C) 2005-2011, 2014, 2015 Red Hat, Inc.
       3                 :             :    This file is part of elfutils.
       4                 :             : 
       5                 :             :    This file is free software; you can redistribute it and/or modify
       6                 :             :    it under the terms of either
       7                 :             : 
       8                 :             :      * the GNU Lesser General Public License as published by the Free
       9                 :             :        Software Foundation; either version 3 of the License, or (at
      10                 :             :        your option) any later version
      11                 :             : 
      12                 :             :    or
      13                 :             : 
      14                 :             :      * the GNU General Public License as published by the Free
      15                 :             :        Software Foundation; either version 2 of the License, or (at
      16                 :             :        your option) any later version
      17                 :             : 
      18                 :             :    or both in parallel, as here.
      19                 :             : 
      20                 :             :    elfutils is distributed in the hope that it will be useful, but
      21                 :             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      22                 :             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      23                 :             :    General Public License for more details.
      24                 :             : 
      25                 :             :    You should have received copies of the GNU General Public License and
      26                 :             :    the GNU Lesser General Public License along with this program.  If
      27                 :             :    not, see <http://www.gnu.org/licenses/>.  */
      28                 :             : 
      29                 :             : #include <config.h>
      30                 :             : #include "libelfP.h"
      31                 :             : 
      32                 :             : #include "libdwflP.h"
      33                 :             : 
      34                 :             : #include <gelf.h>
      35                 :             : #include <sys/types.h>
      36                 :             : #include <stdbool.h>
      37                 :             : #include <stdlib.h>
      38                 :             : #include <string.h>
      39                 :             : 
      40                 :             : /* Reconstruct an ELF file by reading the segments out of remote memory
      41                 :             :    based on the ELF file header at EHDR_VMA and the ELF program headers it
      42                 :             :    points to.  If not null, *LOADBASEP is filled in with the difference
      43                 :             :    between the addresses from which the segments were read, and the
      44                 :             :    addresses the file headers put them at.
      45                 :             : 
      46                 :             :    The function READ_MEMORY is called to copy at least MINREAD and at most
      47                 :             :    MAXREAD bytes from the remote memory at target address ADDRESS into the
      48                 :             :    local buffer at DATA; it should return -1 for errors (with code in
      49                 :             :    `errno'), 0 if it failed to read at least MINREAD bytes due to EOF, or
      50                 :             :    the number of bytes read if >= MINREAD.  ARG is passed through.
      51                 :             : 
      52                 :             :    PAGESIZE is the minimum page size and alignment used for the PT_LOAD
      53                 :             :    segments.  */
      54                 :             : 
      55                 :             : Elf *
      56                 :          20 : elf_from_remote_memory (GElf_Addr ehdr_vma,
      57                 :             :                         GElf_Xword pagesize,
      58                 :             :                         GElf_Addr *loadbasep,
      59                 :             :                         ssize_t (*read_memory) (void *arg, void *data,
      60                 :             :                                                 GElf_Addr address,
      61                 :             :                                                 size_t minread,
      62                 :             :                                                 size_t maxread),
      63                 :             :                         void *arg)
      64                 :             : {
      65                 :             :   /* We might have to reserve some memory for the phdrs.  Set to NULL
      66                 :             :      here so we can always safely free it.  */
      67                 :          20 :   void *phdrsp = NULL;
      68                 :             : 
      69                 :             :   /* First read in the file header and check its sanity.  */
      70                 :             : 
      71                 :          20 :   const size_t initial_bufsize = 256;
      72                 :          20 :   unsigned char *buffer = malloc (initial_bufsize);
      73         [ -  + ]:          20 :   if (unlikely (buffer == NULL))
      74                 :             :     {
      75                 :           0 :     no_memory:
      76                 :           0 :       __libdwfl_seterrno (DWFL_E_NOMEM);
      77                 :           0 :       return NULL;
      78                 :             :     }
      79                 :             : 
      80                 :             :   /* Note minread is just enough for an Elf32_Ehdr.  */
      81                 :          20 :   ssize_t nread = (*read_memory) (arg, buffer, ehdr_vma,
      82                 :             :                                   sizeof (Elf32_Ehdr), initial_bufsize);
      83         [ -  + ]:          20 :   if (nread <= 0)
      84                 :             :     {
      85                 :           0 :     read_error:
      86                 :           2 :       free (buffer);
      87                 :           2 :       free (phdrsp);
      88         [ +  - ]:           2 :       __libdwfl_seterrno (nread < 0 ? DWFL_E_ERRNO : DWFL_E_TRUNCATED);
      89                 :           2 :       return NULL;
      90                 :             :     }
      91                 :             : 
      92         [ -  + ]:          20 :   if (memcmp (buffer, ELFMAG, SELFMAG) != 0)
      93                 :             :     {
      94                 :           0 :     bad_elf:
      95                 :           0 :       free (buffer);
      96                 :           0 :       free (phdrsp);
      97                 :           0 :       __libdwfl_seterrno (DWFL_E_BADELF);
      98                 :           0 :       return NULL;
      99                 :             :     }
     100                 :             : 
     101                 :             :   /* Extract the information we need from the file header.  */
     102                 :             : 
     103                 :          20 :   union
     104                 :             :   {
     105                 :             :     Elf32_Ehdr e32;
     106                 :             :     Elf64_Ehdr e64;
     107                 :             :   } ehdr;
     108                 :          20 :   Elf_Data xlatefrom =
     109                 :             :     {
     110                 :             :       .d_type = ELF_T_EHDR,
     111                 :             :       .d_buf = buffer,
     112                 :             :       .d_version = EV_CURRENT,
     113                 :             :     };
     114                 :          20 :   Elf_Data xlateto =
     115                 :             :     {
     116                 :             :       .d_type = ELF_T_EHDR,
     117                 :             :       .d_buf = &ehdr,
     118                 :             :       .d_size = sizeof ehdr,
     119                 :             :       .d_version = EV_CURRENT,
     120                 :             :     };
     121                 :             : 
     122                 :          20 :   GElf_Off phoff;
     123                 :          20 :   uint_fast16_t phnum;
     124                 :          20 :   uint_fast16_t phentsize;
     125                 :          20 :   GElf_Off shdrs_end;
     126                 :             : 
     127      [ -  +  - ]:          20 :   switch (buffer[EI_CLASS])
     128                 :             :     {
     129                 :           0 :     case ELFCLASS32:
     130                 :           0 :       xlatefrom.d_size = sizeof (Elf32_Ehdr);
     131         [ #  # ]:           0 :       if (elf32_xlatetom (&xlateto, &xlatefrom, buffer[EI_DATA]) == NULL)
     132                 :             :         {
     133                 :           0 :         libelf_error:
     134                 :           0 :           free (buffer);
     135                 :           0 :           free (phdrsp);
     136                 :           0 :           __libdwfl_seterrno (DWFL_E_LIBELF);
     137                 :           0 :           return NULL;
     138                 :             :         }
     139                 :           0 :       phoff = ehdr.e32.e_phoff;
     140                 :           0 :       phnum = ehdr.e32.e_phnum;
     141                 :           0 :       phentsize = ehdr.e32.e_phentsize;
     142         [ #  # ]:           0 :       if (phentsize != sizeof (Elf32_Phdr) || phnum == 0)
     143                 :           0 :         goto bad_elf;
     144                 :             :       /* NOTE if the number of sections is > 0xff00 then e_shnum
     145                 :             :          is zero and the actual number would come from the section
     146                 :             :          zero sh_size field. We ignore this here because getting shdrs
     147                 :             :          is just a nice bonus (see below where we trim the last phdrs
     148                 :             :          PT_LOAD segment).  */
     149                 :           0 :       GElf_Xword shnum32 = ehdr.e32.e_shnum;
     150                 :           0 :       GElf_Xword shentsize32 = ehdr.e32.e_shentsize;
     151         [ #  # ]:           0 :       if (shentsize32 != sizeof (Elf32_Shdr) && shentsize32 != 0)
     152                 :           0 :         goto bad_elf;
     153                 :           0 :       shdrs_end = ehdr.e32.e_shoff + shnum32 * shentsize32;
     154                 :           0 :       break;
     155                 :             : 
     156                 :          20 :     case ELFCLASS64:
     157                 :             :       /* Unlikely, but minread was for just an Elf32_Ehdr.  */
     158         [ -  + ]:          20 :       if (unlikely ((size_t) nread < sizeof (Elf64_Ehdr)))
     159                 :           0 :         goto bad_elf;
     160                 :          20 :       xlatefrom.d_size = sizeof (Elf64_Ehdr);
     161         [ -  + ]:          20 :       if (elf64_xlatetom (&xlateto, &xlatefrom, buffer[EI_DATA]) == NULL)
     162                 :           0 :         goto libelf_error;
     163                 :          20 :       phoff = ehdr.e64.e_phoff;
     164                 :          20 :       phnum = ehdr.e64.e_phnum;
     165                 :          20 :       phentsize = ehdr.e64.e_phentsize;
     166         [ -  + ]:          20 :       if (phentsize != sizeof (Elf64_Phdr) || phnum == 0)
     167                 :           0 :         goto bad_elf;
     168                 :             :       /* See the NOTE above for shdrs_end and ehdr.e32.e_shnum.  */
     169                 :          20 :       GElf_Xword shnum64 = ehdr.e64.e_shnum;
     170                 :          20 :       GElf_Xword shentsize64 = ehdr.e64.e_shentsize;
     171         [ -  + ]:          20 :       if (shentsize64 != sizeof (Elf64_Shdr) && shentsize64 != 0)
     172                 :           0 :         goto bad_elf;
     173                 :          20 :       shdrs_end = ehdr.e64.e_shoff + shnum64 * shentsize64;
     174                 :          20 :       break;
     175                 :             : 
     176                 :           0 :     default:
     177                 :           0 :       goto bad_elf;
     178                 :             :     }
     179                 :             : 
     180                 :             : 
     181                 :             :   /* The file header tells where to find the program headers.
     182                 :             :      These are what we use to actually choose what to read.  */
     183                 :             : 
     184                 :          20 :   xlatefrom.d_type = xlateto.d_type = ELF_T_PHDR;
     185                 :          20 :   xlatefrom.d_size = phnum * phentsize;
     186                 :             : 
     187         [ +  + ]:          20 :   if (phoff <= (size_t) nread
     188         [ +  + ]:          18 :       && (size_t) phnum * phentsize <= (size_t) nread - phoff)
     189                 :             :     /* We already have all the phdrs from the initial read.  */
     190                 :           2 :     xlatefrom.d_buf = buffer + phoff;
     191                 :             :   else
     192                 :             :     {
     193                 :             :       /* Read in the program headers.  */
     194                 :             : 
     195         [ +  + ]:          18 :       if (initial_bufsize < (size_t)phnum * phentsize)
     196                 :             :         {
     197                 :          16 :           unsigned char *newbuf = realloc (buffer, phnum * phentsize);
     198         [ -  + ]:          16 :           if (newbuf == NULL)
     199                 :             :             {
     200                 :           0 :               free (buffer);
     201                 :           0 :               free (phdrsp);
     202                 :           0 :               goto no_memory;
     203                 :             :             }
     204                 :             :           buffer = newbuf;
     205                 :             :         }
     206                 :          18 :       nread = (*read_memory) (arg, buffer, ehdr_vma + phoff,
     207                 :             :                               phnum * phentsize, phnum * phentsize);
     208         [ +  + ]:          18 :       if (nread <= 0)
     209                 :           2 :         goto read_error;
     210                 :             : 
     211                 :          16 :       xlatefrom.d_buf = buffer;
     212                 :             :     }
     213                 :             : 
     214                 :          18 :   bool class32 = ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32;
     215         [ +  - ]:          18 :   size_t phdr_size = class32 ? sizeof (Elf32_Phdr) : sizeof (Elf64_Phdr);
     216         [ -  + ]:          18 :   if (unlikely (phnum > SIZE_MAX / phdr_size))
     217                 :             :     {
     218                 :           0 :       free (buffer);
     219                 :           0 :       goto no_memory;
     220                 :             :     }
     221                 :          18 :   const size_t phdrsp_bytes = phnum * phdr_size;
     222                 :          18 :   phdrsp = malloc (phdrsp_bytes);
     223         [ -  + ]:          18 :   if (unlikely (phdrsp == NULL))
     224                 :             :     {
     225                 :           0 :       free (buffer);
     226                 :           0 :       goto no_memory;
     227                 :             :     }
     228                 :             : 
     229                 :          18 :   xlateto.d_buf = phdrsp;
     230                 :          18 :   xlateto.d_size = phdrsp_bytes;
     231                 :             : 
     232                 :             :   /* Scan for PT_LOAD segments to find the total size of the file image.  */
     233                 :          18 :   size_t contents_size = 0;
     234                 :          18 :   GElf_Off segments_end = 0;
     235                 :          18 :   GElf_Off segments_end_mem = 0;
     236                 :          18 :   GElf_Addr loadbase = ehdr_vma;
     237                 :          18 :   bool found_base = false;
     238                 :          18 :   Elf32_Phdr (*p32)[phnum] = phdrsp;
     239                 :          18 :   Elf64_Phdr (*p64)[phnum] = phdrsp;
     240                 :             : 
     241         [ -  + ]:          18 :   if (class32)
     242                 :             :     {
     243         [ #  # ]:           0 :       if (! elf32_xlatetom (&xlateto, &xlatefrom, ehdr.e32.e_ident[EI_DATA]))
     244                 :           0 :         goto libelf_error;
     245                 :             :     }
     246                 :             :   else
     247                 :             :     {
     248         [ -  + ]:          18 :       if (! elf64_xlatetom (&xlateto, &xlatefrom, ehdr.e64.e_ident[EI_DATA]))
     249                 :           0 :         goto libelf_error;
     250                 :             :     }
     251                 :             : 
     252         [ +  + ]:         124 :   for (uint_fast16_t i = 0; i < phnum; ++i)
     253                 :             :     {
     254         [ -  + ]:         106 :       GElf_Word type = class32 ? (*p32)[i].p_type : (*p64)[i].p_type;
     255                 :             : 
     256         [ +  + ]:         106 :       if (type != PT_LOAD)
     257                 :          84 :         continue;
     258                 :             : 
     259         [ -  + ]:          22 :       GElf_Addr vaddr = class32 ? (*p32)[i].p_vaddr : (*p64)[i].p_vaddr;
     260                 :          22 :       GElf_Xword memsz = class32 ? (*p32)[i].p_memsz : (*p64)[i].p_memsz;
     261                 :          22 :       GElf_Off offset = class32 ? (*p32)[i].p_offset : (*p64)[i].p_offset;
     262                 :          22 :       GElf_Xword filesz = class32 ? (*p32)[i].p_filesz : (*p64)[i].p_filesz;
     263                 :             : 
     264                 :             :       /* Sanity check the segment load aligns with the pagesize.  */
     265         [ -  + ]:          22 :       if (((vaddr - offset) & (pagesize - 1)) != 0)
     266                 :           0 :         goto bad_elf;
     267                 :             : 
     268                 :          22 :       GElf_Off segment_end = ((offset + filesz + pagesize - 1)
     269                 :          22 :                               & -pagesize);
     270                 :             : 
     271                 :             :       /* Check for overflow or too big segment size.  */
     272   [ +  -  -  + ]:          22 :       if (unlikely (segment_end < (offset & -pagesize)
     273                 :             :                     || segment_end > SSIZE_MAX))
     274                 :           0 :         goto bad_elf;
     275                 :             : 
     276         [ +  - ]:          22 :       if (segment_end > (GElf_Off) contents_size)
     277                 :          22 :         contents_size = segment_end;
     278                 :             : 
     279   [ +  +  +  - ]:          22 :       if (!found_base && (offset & -pagesize) == 0)
     280                 :             :         {
     281                 :          18 :           loadbase = ehdr_vma - (vaddr & -pagesize);
     282                 :          18 :           found_base = true;
     283                 :             :         }
     284                 :             : 
     285                 :          22 :       segments_end = offset + filesz;
     286                 :          22 :       segments_end_mem = offset + memsz;
     287                 :             :     }
     288                 :             : 
     289                 :             :   /* Trim the last segment so we don't bother with zeros in the last page
     290                 :             :      that are off the end of the file.  However, if the extra bit in that
     291                 :             :      page includes the section headers and the memory isn't extended (which
     292                 :             :      might indicate it will have been reused otherwise), keep them.  */
     293                 :          18 :   if ((GElf_Off) contents_size > segments_end
     294         [ +  + ]:          18 :       && (GElf_Off) contents_size >= shdrs_end
     295         [ +  - ]:          16 :       && segments_end == segments_end_mem)
     296                 :             :     {
     297                 :          16 :       contents_size = segments_end;
     298         [ +  + ]:          16 :       if ((GElf_Off) contents_size < shdrs_end)
     299                 :          14 :         contents_size = shdrs_end;
     300                 :             :     }
     301                 :             :   else
     302                 :             :     contents_size = segments_end;
     303                 :             : 
     304                 :             :   /* Make sure the image is big enough for the ELF header written out
     305                 :             :      below; otherwise the elfNN_xlatetof of the header overflows it.  */
     306   [ +  -  -  + ]:          36 :   if (contents_size < (class32 ? sizeof ehdr.e32 : sizeof ehdr.e64))
     307                 :           0 :     goto bad_elf;
     308                 :             : 
     309                 :          18 :   free (buffer);
     310                 :             : 
     311                 :             :   /* Now we know the size of the whole image we want read in.  */
     312                 :          18 :   buffer = calloc (1, contents_size);
     313         [ -  + ]:          18 :   if (buffer == NULL)
     314                 :             :     {
     315                 :           0 :       free (phdrsp);
     316                 :           0 :       goto no_memory;
     317                 :             :     }
     318                 :             : 
     319         [ +  + ]:         124 :   for (uint_fast16_t i = 0; i < phnum; ++i)
     320                 :             :     {
     321         [ -  + ]:         106 :       GElf_Word type = class32 ? (*p32)[i].p_type : (*p64)[i].p_type;
     322                 :             : 
     323         [ +  + ]:         106 :       if (type != PT_LOAD)
     324                 :          84 :         continue;
     325                 :             : 
     326         [ -  + ]:          22 :       GElf_Addr vaddr = class32 ? (*p32)[i].p_vaddr : (*p64)[i].p_vaddr;
     327                 :          22 :       GElf_Off offset = class32 ? (*p32)[i].p_offset : (*p64)[i].p_offset;
     328                 :          22 :       GElf_Xword filesz = class32 ? (*p32)[i].p_filesz : (*p64)[i].p_filesz;
     329                 :             : 
     330                 :          22 :       GElf_Off start = offset & -pagesize;
     331                 :          22 :       GElf_Off end = (offset + filesz + pagesize - 1) & -pagesize;
     332                 :             : 
     333                 :             :       /* Make sure end didn't wrap around. */
     334         [ -  + ]:          22 :       if (unlikely (end < start))
     335                 :           0 :         goto bad_elf;
     336                 :             : 
     337                 :             :       /* The final contents_size is the trimmed last segment's end, which
     338                 :             :          may be smaller than an earlier segment's start (segments_end above
     339                 :             :          tracks the last PT_LOAD, not the maximum).  Skip any segment that
     340                 :             :          falls entirely past it: otherwise buffer + start would be out of
     341                 :             :          bounds and end - start would underflow into a huge read length.
     342                 :             :          Mirrors the file_trimmed_end check in dwfl_segment_report_module.  */
     343         [ -  + ]:          22 :       if (start >= (GElf_Off) contents_size)
     344                 :           0 :         continue;
     345         [ +  + ]:          22 :       if (end > (GElf_Off) contents_size)
     346                 :          18 :         end = contents_size;
     347                 :          44 :       nread = (*read_memory) (arg, buffer + start,
     348                 :          22 :                               (loadbase + vaddr) & -pagesize,
     349                 :          22 :                               end - start, end - start);
     350         [ -  + ]:          22 :       if (nread <= 0)
     351                 :           0 :         goto read_error;
     352                 :             :     }
     353                 :             : 
     354                 :             :   /* If the segments visible in memory didn't include the section
     355                 :             :      headers, then clear them from the file header.  */
     356         [ +  + ]:          18 :   if (contents_size < shdrs_end)
     357                 :             :     {
     358         [ -  + ]:           2 :       if (class32)
     359                 :             :         {
     360                 :           0 :           ehdr.e32.e_shoff = 0;
     361                 :           0 :           ehdr.e32.e_shnum = 0;
     362                 :           0 :           ehdr.e32.e_shstrndx = 0;
     363                 :             :         }
     364                 :             :       else
     365                 :             :         {
     366                 :           2 :           ehdr.e64.e_shoff = 0;
     367                 :           2 :           ehdr.e64.e_shnum = 0;
     368                 :           2 :           ehdr.e64.e_shstrndx = 0;
     369                 :             :         }
     370                 :             :     }
     371                 :             : 
     372                 :             :   /* This will normally have been in the first PT_LOAD segment.  But it
     373                 :             :      conceivably could be missing, and we might have just changed it.  */
     374                 :          18 :   xlatefrom.d_type = xlateto.d_type = ELF_T_EHDR;
     375                 :          18 :   xlateto.d_buf = buffer;
     376         [ -  + ]:          18 :   if (class32)
     377                 :             :     {
     378                 :           0 :       xlatefrom.d_size = xlateto.d_size = sizeof ehdr.e32;
     379                 :           0 :       xlatefrom.d_buf = &ehdr.e32;
     380         [ #  # ]:           0 :       if (elf32_xlatetof (&xlateto, &xlatefrom,
     381                 :           0 :                           ehdr.e32.e_ident[EI_DATA]) == NULL)
     382                 :           0 :         goto libelf_error;
     383                 :             :     }
     384                 :             :   else
     385                 :             :     {
     386                 :          18 :       xlatefrom.d_size = xlateto.d_size = sizeof ehdr.e64;
     387                 :          18 :       xlatefrom.d_buf = &ehdr.e64;
     388         [ -  + ]:          18 :       if (elf64_xlatetof (&xlateto, &xlatefrom,
     389                 :          18 :                           ehdr.e64.e_ident[EI_DATA]) == NULL)
     390                 :           0 :         goto libelf_error;
     391                 :             :     }
     392                 :             : 
     393                 :          18 :   free (phdrsp);
     394                 :          18 :   phdrsp = NULL;
     395                 :             : 
     396                 :             :   /* Now we have the image.  Open libelf on it.  */
     397                 :             : 
     398                 :          18 :   Elf *elf = elf_memory ((char *) buffer, contents_size);
     399         [ -  + ]:          18 :   if (elf == NULL)
     400                 :           0 :     goto libelf_error;
     401                 :             : 
     402                 :          18 :   elf->flags |= ELF_F_MALLOCED;
     403         [ -  + ]:          18 :   if (loadbasep != NULL)
     404                 :           0 :     *loadbasep = loadbase;
     405                 :             :   return elf;
     406                 :             : }
        

Generated by: LCOV version 2.0-1