Print this page
5578 file(1) should validate Elf_Shdr->sh_name

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/file/elf_read.c
          +++ new/usr/src/cmd/file/elf_read.c
↓ open down ↓ 411 lines elided ↑ open up ↑
 412  412   *                      information sections. Set the "stripped" field
 413  413   *                      in Elf_Info with corresponding flags.
 414  414   */
 415  415  static int
 416  416  process_shdr(Elf_Info *EI)
 417  417  {
 418  418          int             capn, mac;
 419  419          int             i, j, idx;
 420  420          FILE_ELF_OFF_T  cap_off;
 421  421          FILE_ELF_SIZE_T csize;
 422      -        char            *section_name;
      422 +        char            *strtab;
      423 +        size_t          strtab_sz;
 423  424          Elf_Cap         Chdr;
 424  425          Elf_Shdr        *shdr = &EI_Shdr;
 425  426  
 426  427  
 427  428          csize = sizeof (Elf_Cap);
 428  429          mac = EI_Ehdr.e_machine;
 429  430  
 430  431          /* if there are no sections, return success anyway */
 431  432          if (EI_Ehdr.e_shoff == 0 && EI_Ehdr_shnum == 0)
 432  433                  return (ELF_READ_OKAY);
 433  434  
 434  435          /* read section names from String Section */
 435  436          if (get_shdr(EI, EI_Ehdr_shstrndx) == ELF_READ_FAIL)
 436  437                  return (ELF_READ_FAIL);
 437  438  
 438      -        if ((section_name = malloc(shdr->sh_size)) == NULL)
      439 +        if ((strtab = malloc(shdr->sh_size)) == NULL)
 439  440                  return (ELF_READ_FAIL);
 440  441  
 441      -        if (pread64(EI->elffd, section_name, shdr->sh_size, shdr->sh_offset)
      442 +        if (pread64(EI->elffd, strtab, shdr->sh_size, shdr->sh_offset)
 442  443              != shdr->sh_size)
 443  444                  return (ELF_READ_FAIL);
 444  445  
      446 +        strtab_sz = shdr->sh_size;
      447 +
 445  448          /* read all the sections and process them */
 446  449          for (idx = 1, i = 0; i < EI_Ehdr_shnum; idx++, i++) {
 447      -                char *str;
      450 +                char *shnam;
 448  451  
 449  452                  if (get_shdr(EI, i) == ELF_READ_FAIL)
 450  453                          return (ELF_READ_FAIL);
 451  454  
 452  455                  if (shdr->sh_type == SHT_NULL) {
 453  456                          idx--;
 454  457                          continue;
 455  458                  }
 456  459  
 457  460                  cap_off = shdr->sh_offset;
↓ open down ↓ 73 lines elided ↑ open up ↑
 531  534                   */
 532  535                  if ((EI->stripped & E_NOSTRIP) == E_NOSTRIP)
 533  536                          continue;
 534  537  
 535  538                  if (!(EI->stripped & E_SYMTAB) &&
 536  539                      (shdr->sh_type == SHT_SYMTAB)) {
 537  540                          EI->stripped |= E_SYMTAB;
 538  541                          continue;
 539  542                  }
 540  543  
 541      -                str = &section_name[shdr->sh_name];
      544 +                if (shdr->sh_name >= strtab_sz)
      545 +                        shnam = NULL;
      546 +                else
      547 +                        shnam = &strtab[shdr->sh_name];
 542  548  
 543  549                  if (!(EI->stripped & E_DBGINF) &&
 544  550                      ((shdr->sh_type == SHT_SUNW_DEBUG) ||
 545  551                      (shdr->sh_type == SHT_SUNW_DEBUGSTR) ||
 546      -                    (is_in_list(str)))) {
      552 +                    (shnam != NULL && is_in_list(shnam)))) {
 547  553                          EI->stripped |= E_DBGINF;
 548  554                  }
 549  555          }
 550      -        free(section_name);
      556 +        free(strtab);
 551  557  
 552  558          return (ELF_READ_OKAY);
 553  559  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX