|
38 | 38 | #include <rpm/rpmlog.h>
|
39 | 39 | #include <magic.h>
|
40 | 40 | #include <libudev.h>
|
| 41 | +#include <elf.h> |
41 | 42 | #include "file.h"
|
42 | 43 | #include "message.h"
|
| 44 | +#include "process.h" // For elf info bit mask |
43 | 45 |
|
44 | 46 | // Local variables
|
45 | 47 | static struct udev *udev;
|
@@ -351,3 +353,194 @@ char *get_hash_from_fd(int fd)
|
351 | 353 | return digest;
|
352 | 354 | }
|
353 | 355 |
|
| 356 | +static unsigned char e_ident[EI_NIDENT]; |
| 357 | + |
| 358 | +static int read_preliminary_header(int fd) |
| 359 | +{ |
| 360 | + ssize_t rc = safe_read(fd, (char *)e_ident, EI_NIDENT); |
| 361 | + if (rc == EI_NIDENT) |
| 362 | + return 0; |
| 363 | + return 1; |
| 364 | +} |
| 365 | + |
| 366 | +static Elf32_Ehdr *read_header32(int fd) |
| 367 | +{ |
| 368 | + Elf32_Ehdr *ptr = malloc(sizeof(Elf32_Ehdr)); |
| 369 | + strcpy(ptr->e_ident, e_ident); |
| 370 | + ssize_t rc = safe_read(fd, (char *)&(ptr->e_type), sizeof(Elf32_Ehdr) - EI_NIDENT); |
| 371 | + if (rc == (sizeof(Elf32_Ehdr) - EI_NIDENT)) |
| 372 | + return ptr; |
| 373 | + free(ptr); |
| 374 | + return NULL; |
| 375 | +} |
| 376 | + |
| 377 | +static Elf64_Ehdr *read_header64(int fd) |
| 378 | +{ |
| 379 | + Elf64_Ehdr *ptr = malloc(sizeof(Elf64_Ehdr)); |
| 380 | + strcpy(ptr->e_ident, e_ident); |
| 381 | + ssize_t rc = safe_read(fd, (char *)&(ptr->e_type), |
| 382 | + sizeof(Elf64_Ehdr) - EI_NIDENT); |
| 383 | + if (rc == (sizeof(Elf64_Ehdr) - EI_NIDENT)) |
| 384 | + return ptr; |
| 385 | + free(ptr); |
| 386 | + return NULL; |
| 387 | +} |
| 388 | + |
| 389 | +uint32_t gather_elf(int fd) |
| 390 | +{ |
| 391 | + //struct elf_info *e; |
| 392 | + uint32_t info = 0; |
| 393 | + if (read_preliminary_header(fd)) |
| 394 | + return 0; |
| 395 | + |
| 396 | + if (strncmp((char *)e_ident, ELFMAG, 4)) |
| 397 | + return 0; |
| 398 | + |
| 399 | + /* e = malloc(sizeof(struct elf_info)); |
| 400 | + if (e == NULL) |
| 401 | + return 0; |
| 402 | + e->first_lib = NULL; */ |
| 403 | + info |= IS_ELF; |
| 404 | + if (e_ident[4] == 1) { |
| 405 | + unsigned i; |
| 406 | + Elf32_Phdr *ph_tbl = NULL; |
| 407 | + |
| 408 | + Elf32_Ehdr *hdr = read_header32(fd); |
| 409 | + if (hdr == NULL) |
| 410 | + return 0; |
| 411 | + |
| 412 | + // Look for program header information |
| 413 | + // FIXME: Should there be a size check? |
| 414 | + ph_tbl = malloc(hdr->e_phentsize * hdr->e_phnum); |
| 415 | + if ((unsigned int)lseek(fd, (off_t)hdr->e_phoff, SEEK_SET) != |
| 416 | + hdr->e_phoff) |
| 417 | + goto err_out32; |
| 418 | + |
| 419 | + // Read in complete table |
| 420 | + if ((unsigned int)safe_read(fd, (char *)ph_tbl, |
| 421 | + hdr->e_phentsize * hdr->e_phnum) != |
| 422 | + hdr->e_phentsize * hdr->e_phnum) |
| 423 | + goto err_out32; |
| 424 | + |
| 425 | + // Check for rpath record |
| 426 | + for (i = 0; i < hdr->e_phnum; i++) { |
| 427 | + if (ph_tbl[i].p_type == PT_LOAD) |
| 428 | + info |= HAS_LOAD; |
| 429 | + else if (ph_tbl[i].p_type == PT_DYNAMIC) { |
| 430 | + unsigned int j = 0; |
| 431 | + unsigned int num; |
| 432 | + |
| 433 | + info |= HAS_DYNAMIC; |
| 434 | + Elf64_Dyn *dyn_tbl = malloc(ph_tbl[i].p_filesz); |
| 435 | + if((unsigned int)lseek(fd, ph_tbl[i].p_offset, |
| 436 | + SEEK_SET) != |
| 437 | + ph_tbl[i].p_offset) |
| 438 | + goto err_out32; |
| 439 | + |
| 440 | + num = ph_tbl[i].p_filesz / sizeof(Elf64_Dyn); |
| 441 | + if (num > 1000) |
| 442 | + goto err_out32; |
| 443 | + |
| 444 | + if ((unsigned int)safe_read(fd, (char *)dyn_tbl, |
| 445 | + ph_tbl[i].p_filesz) != |
| 446 | + ph_tbl[i].p_filesz) |
| 447 | + goto err_out32; |
| 448 | + |
| 449 | + while (j < num) { |
| 450 | + if (dyn_tbl[j].d_tag == DT_NEEDED) { |
| 451 | + } else if (dyn_tbl[j].d_tag == DT_RUNPATH) |
| 452 | + info |= HAS_RPATH; |
| 453 | + else if (dyn_tbl[j].d_tag == DT_RPATH) { |
| 454 | + info |= HAS_RPATH; |
| 455 | + break; |
| 456 | + } |
| 457 | + j++; |
| 458 | + } |
| 459 | + free(dyn_tbl); |
| 460 | + } |
| 461 | + if (info & HAS_RPATH) |
| 462 | + break; |
| 463 | + } |
| 464 | + goto done32; |
| 465 | +err_out32: |
| 466 | +// free(e->first_lib); |
| 467 | +// free(e); |
| 468 | +// e = NULL; |
| 469 | + info |= HAS_ERROR; |
| 470 | +done32: |
| 471 | + free(ph_tbl); |
| 472 | + free(hdr); |
| 473 | + } else if (e_ident[4] == 2) { |
| 474 | + unsigned i; |
| 475 | + Elf64_Phdr *ph_tbl; |
| 476 | + |
| 477 | + Elf64_Ehdr *hdr = read_header64(fd); |
| 478 | + if (hdr == NULL) |
| 479 | + return 0; |
| 480 | + |
| 481 | + // Look for program header information |
| 482 | + // FIXME: Should there be a size check? |
| 483 | + ph_tbl = malloc(hdr->e_phentsize * hdr->e_phnum); |
| 484 | + if ((unsigned int)lseek(fd, (off_t)hdr->e_phoff, SEEK_SET) != |
| 485 | + hdr->e_phoff) |
| 486 | + goto err_out64; |
| 487 | + |
| 488 | + // Read in complete table |
| 489 | + if ((unsigned int)safe_read(fd, (char *)ph_tbl, |
| 490 | + hdr->e_phentsize * hdr->e_phnum) != |
| 491 | + hdr->e_phentsize * hdr->e_phnum) |
| 492 | + goto err_out64; |
| 493 | + |
| 494 | + // Check for rpath record |
| 495 | + for (i = 0; i < hdr->e_phnum; i++) { |
| 496 | + if (ph_tbl[i].p_type == PT_LOAD) |
| 497 | + info |= HAS_LOAD; |
| 498 | + if (ph_tbl[i].p_type == PT_DYNAMIC) { |
| 499 | + unsigned int j = 0; |
| 500 | + unsigned int num; |
| 501 | + |
| 502 | + info |= HAS_DYNAMIC; |
| 503 | + Elf64_Dyn *dyn_tbl = malloc(ph_tbl[i].p_filesz); |
| 504 | + if ((unsigned int)lseek(fd, ph_tbl[i].p_offset, |
| 505 | + SEEK_SET) != |
| 506 | + ph_tbl[i].p_offset) |
| 507 | + goto err_out64; |
| 508 | + |
| 509 | + num = ph_tbl[i].p_filesz / sizeof(Elf64_Dyn); |
| 510 | + if (num > 1000) |
| 511 | + goto err_out64; |
| 512 | + |
| 513 | + if ((unsigned int)safe_read(fd, (char *)dyn_tbl, |
| 514 | + ph_tbl[i].p_filesz) != |
| 515 | + ph_tbl[i].p_filesz) |
| 516 | + goto err_out64; |
| 517 | + |
| 518 | + while (j < num) { |
| 519 | + if (dyn_tbl[j].d_tag == DT_NEEDED) { |
| 520 | + } else if (dyn_tbl[j].d_tag == DT_RUNPATH) |
| 521 | + info |= HAS_RPATH; |
| 522 | + else if (dyn_tbl[j].d_tag == DT_RPATH) { |
| 523 | + info |= HAS_RPATH; |
| 524 | + break; |
| 525 | + } |
| 526 | + j++; |
| 527 | + } |
| 528 | + free(dyn_tbl); |
| 529 | + } |
| 530 | + if (info & HAS_RPATH) |
| 531 | + break; |
| 532 | + } |
| 533 | + goto done64; |
| 534 | +err_out64: |
| 535 | +// free(e->first_lib); |
| 536 | +// free(e); |
| 537 | +// e = NULL; |
| 538 | + info |= HAS_ERROR; |
| 539 | +done64: |
| 540 | + free(ph_tbl); |
| 541 | + free(hdr); |
| 542 | + } |
| 543 | + lseek(fd, 0, SEEK_SET); |
| 544 | + return info; |
| 545 | +} |
| 546 | + |
0 commit comments