Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 587230 | Differences between
and this patch

Collapse All | Expand All

(-)a/linux-user/elfload.c (-1 / +2 lines)
Lines 2363-2368 void probe_guest_base(const char *image_name, abi_ulong guest_loaddr, Link Here
2363
2363
2364
   IMAGE_NAME is the filename of the image, to use in error messages.
2364
   IMAGE_NAME is the filename of the image, to use in error messages.
2365
   IMAGE_FD is the open file descriptor for the image.
2365
   IMAGE_FD is the open file descriptor for the image.
2366
   WARNING : IMAGE_FD won't be closed.
2366
2367
2367
   BPRM_BUF is a copy of the beginning of the file; this of course
2368
   BPRM_BUF is a copy of the beginning of the file; this of course
2368
   contains the elf file header at offset 0.  It is assumed that this
2369
   contains the elf file header at offset 0.  It is assumed that this
Lines 2632-2638 static void load_elf_image(const char *image_name, int image_fd, Link Here
2632
2633
2633
    mmap_unlock();
2634
    mmap_unlock();
2634
2635
2635
    close(image_fd);
2636
    return;
2636
    return;
2637
2637
2638
 exit_read:
2638
 exit_read:
Lines 2666-2671 static void load_elf_interp(const char *filename, struct image_info *info, Link Here
2666
    }
2666
    }
2667
2667
2668
    load_elf_image(filename, fd, info, NULL, bprm_buf);
2668
    load_elf_image(filename, fd, info, NULL, bprm_buf);
2669
    close(fd);
2669
    return;
2670
    return;
2670
2671
2671
 exit_perror:
2672
 exit_perror:
(-)a/linux-user/main.c (-2 / +9 lines)
Lines 629-634 int main(int argc, char **argv, char **envp) Link Here
629
    int target_argc;
629
    int target_argc;
630
    int i;
630
    int i;
631
    int ret;
631
    int ret;
632
    int at_execfd;
632
    int execfd;
633
    int execfd;
633
    int log_mask;
634
    int log_mask;
634
    unsigned long max_reserved_va;
635
    unsigned long max_reserved_va;
Lines 690-702 int main(int argc, char **argv, char **envp) Link Here
690
691
691
    init_qemu_uname_release();
692
    init_qemu_uname_release();
692
693
693
    execfd = qemu_getauxval(AT_EXECFD);
694
    at_execfd = qemu_getauxval(AT_EXECFD);
694
    if (execfd == 0) {
695
    if (at_execfd == 0) {
695
        execfd = open(exec_path, O_RDONLY);
696
        execfd = open(exec_path, O_RDONLY);
696
        if (execfd < 0) {
697
        if (execfd < 0) {
697
            printf("Error while loading %s: %s\n", exec_path, strerror(errno));
698
            printf("Error while loading %s: %s\n", exec_path, strerror(errno));
698
            _exit(EXIT_FAILURE);
699
            _exit(EXIT_FAILURE);
699
        }
700
        }
701
    } else {
702
        execfd = at_execfd;
700
    }
703
    }
701
704
702
    if (cpu_model == NULL) {
705
    if (cpu_model == NULL) {
Lines 811-816 int main(int argc, char **argv, char **envp) Link Here
811
814
812
    ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs,
815
    ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs,
813
        info, &bprm);
816
        info, &bprm);
817
    /* We shouldn't close at_execfd, it may be used later. */
818
    if (execfd != at_execfd) {
819
        close(execfd);
820
    }
814
    if (ret != 0) {
821
    if (ret != 0) {
815
        printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
822
        printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
816
        _exit(EXIT_FAILURE);
823
        _exit(EXIT_FAILURE);
(-)a/linux-user/syscall.c (-28 / +62 lines)
Lines 7482-7519 static int open_self_auxv(void *cpu_env, int fd) Link Here
7482
    return 0;
7482
    return 0;
7483
}
7483
}
7484
7484
7485
static int is_proc_myself(const char *filename, const char *entry)
7485
#define PROC "/proc/"
7486
{
7486
#define SELF "self/"
7487
    if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
7487
7488
        filename += strlen("/proc/");
7488
#define STARTS_WITH(path, CONSTANT) (              \
7489
        if (!strncmp(filename, "self/", strlen("self/"))) {
7489
    strlen(path) >= strlen(CONSTANT) &&            \
7490
            filename += strlen("self/");
7490
    strncmp(path, CONSTANT, strlen(CONSTANT)) == 0 \
7491
        } else if (*filename >= '1' && *filename <= '9') {
7491
)
7492
            char myself[80];
7492
7493
            snprintf(myself, sizeof(myself), "%d/", getpid());
7493
static inline char *scope_to_proc(const char *path)
7494
            if (!strncmp(filename, myself, strlen(myself))) {
7494
{
7495
                filename += strlen(myself);
7495
    if (STARTS_WITH(path, PROC)) {
7496
            } else {
7496
        return (char *)path + strlen(PROC);
7497
                return 0;
7498
            }
7499
        } else {
7500
            return 0;
7501
        }
7502
        if (!strcmp(filename, entry)) {
7503
            return 1;
7504
        }
7505
    }
7497
    }
7506
    return 0;
7498
7499
    return NULL;
7500
}
7501
7502
static inline char *scope_to_proc_myself(const char *path)
7503
{
7504
    char *scope_path = scope_to_proc(path);
7505
    if (scope_path == NULL) {
7506
        return NULL;
7507
    }
7508
7509
    if (STARTS_WITH(scope_path, SELF)) {
7510
        return scope_path + strlen(SELF);
7511
    }
7512
7513
    if (strlen(scope_path) >= 1 &&
7514
        *scope_path >= '1' && *scope_path <= '9') {
7515
        char pid_path[80];
7516
        snprintf(pid_path, sizeof(pid_path), "%d/", getpid());
7517
        if (STARTS_WITH(scope_path, pid_path)) {
7518
            return scope_path + strlen(pid_path);
7519
        }
7520
     }
7521
7522
    return NULL;
7507
}
7523
}
7508
7524
7509
#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) || \
7525
#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) || \
7510
    defined(TARGET_SPARC) || defined(TARGET_M68K) || defined(TARGET_HPPA)
7526
    defined(TARGET_SPARC) || defined(TARGET_M68K) || defined(TARGET_HPPA)
7511
static int is_proc(const char *filename, const char *entry)
7527
static int is_proc(const char *path, const char *entry)
7512
{
7528
{
7513
    return strcmp(filename, entry) == 0;
7529
    char *scope_path = scope_to_proc(path);
7530
    if (scope_path == NULL) {
7531
        return 0;
7532
    }
7533
7534
    return strcmp(scope_path, entry) == 0;
7514
}
7535
}
7515
#endif
7536
#endif
7516
7537
7538
static int is_proc_myself(const char *path, const char *entry)
7539
{
7540
    char *scope_path = scope_to_proc_myself(path);
7541
    if (scope_path == NULL) {
7542
        return 0;
7543
    }
7544
7545
    return strcmp(scope_path, entry) == 0;
7546
}
7547
7517
#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7548
#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7518
static int open_net_route(void *cpu_env, int fd)
7549
static int open_net_route(void *cpu_env, int fd)
7519
{
7550
{
Lines 7601-7620 static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, Link Here
7601
        { "auxv", open_self_auxv, is_proc_myself },
7632
        { "auxv", open_self_auxv, is_proc_myself },
7602
        { "cmdline", open_self_cmdline, is_proc_myself },
7633
        { "cmdline", open_self_cmdline, is_proc_myself },
7603
#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7634
#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7604
        { "/proc/net/route", open_net_route, is_proc },
7635
        { "net/route", open_net_route, is_proc },
7605
#endif
7636
#endif
7606
#if defined(TARGET_SPARC) || defined(TARGET_HPPA)
7637
#if defined(TARGET_SPARC) || defined(TARGET_HPPA)
7607
        { "/proc/cpuinfo", open_cpuinfo, is_proc },
7638
        { "cpuinfo", open_cpuinfo, is_proc },
7608
#endif
7639
#endif
7609
#if defined(TARGET_M68K)
7640
#if defined(TARGET_M68K)
7610
        { "/proc/hardware", open_hardware, is_proc },
7641
        { "hardware", open_hardware, is_proc },
7611
#endif
7642
#endif
7612
        { NULL, NULL, NULL }
7643
        { NULL, NULL, NULL }
7613
    };
7644
    };
7614
7645
7615
    if (is_proc_myself(pathname, "exe")) {
7646
    if (is_proc_myself(pathname, "exe")) {
7616
        int execfd = qemu_getauxval(AT_EXECFD);
7647
        /*
7617
        return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
7648
         * We can't use AT_EXECFD here.
7649
         * User can close provided fd and another user will receive closed fd.
7650
         */
7651
        return safe_openat(dirfd, exec_path, flags, mode);
7618
    }
7652
    }
7619
7653
7620
    for (fake_open = fakes; fake_open->filename; fake_open++) {
7654
    for (fake_open = fakes; fake_open->filename; fake_open++) {
(-)a/tests/tcg/multiarch/linux-test.c (+85 lines)
Lines 517-522 static void test_shm(void) Link Here
517
    chk_error(shmdt(ptr));
517
    chk_error(shmdt(ptr));
518
}
518
}
519
519
520
static void test_proc_myself_file(void)
521
{
522
    int fd1, fd2;
523
    char link1[PATH_MAX], link2[PATH_MAX];
524
    char buf1[PATH_MAX], buf2[PATH_MAX];
525
    int buf1_length, buf2_length;
526
527
    /* We can open any file that will always exist. */
528
    const char *file_path = "/proc/self/comm";
529
530
    char file_realpath[PATH_MAX];
531
    if (realpath(file_path, file_realpath) == NULL) {
532
        error("proc myself: invalid file");
533
    }
534
535
    fd1 = chk_error(open(file_path, O_RDONLY));
536
    sprintf(link1, "/proc/self/fd/%d", fd1);
537
538
    /* Lets try to open same file by first link. */
539
    fd2 = chk_error(open(link1, O_RDONLY));
540
    sprintf(link2, "/proc/self/fd/%d", fd2);
541
542
    /* Two links should point to the same file path. */
543
    buf1_length = chk_error(readlink(link1, buf1, PATH_MAX));
544
    if (strlen(file_realpath) != buf1_length ||
545
        strncmp(file_realpath, buf1, buf1_length) != 0) {
546
        error("proc myself: invalid link");
547
    }
548
    buf2_length = chk_error(readlink(link2, buf2, PATH_MAX));
549
    if (strlen(file_realpath) != buf2_length ||
550
        strncmp(file_realpath, buf2, buf2_length) != 0) {
551
        error("proc myself: invalid link");
552
    }
553
554
    /* We should be able to read same data from each fd. */
555
    buf1_length = chk_error(read(fd1, buf1, PATH_MAX));
556
    buf2_length = chk_error(read(fd2, buf2, PATH_MAX));
557
    if (buf1_length == 0 || buf1_length != buf2_length ||
558
        strncmp(buf1, buf2, buf2_length) != 0) {
559
        error("proc myself: invalid file content");
560
    }
561
562
    chk_error(close(fd2));
563
    chk_error(close(fd1));
564
}
565
566
static void test_proc_myself_exe(void)
567
{
568
    int fd1, fd2;
569
    char link1[PATH_MAX], link2[PATH_MAX];
570
    char buf1[PATH_MAX], buf2[PATH_MAX];
571
    int buf1_length, buf2_length;
572
573
    const char *exe_path = "/proc/self/exe";
574
575
    char exe_realpath[PATH_MAX];
576
    if (realpath(exe_path, exe_realpath) == NULL) {
577
        error("proc myself: invalid exe");
578
    }
579
580
    fd1 = chk_error(open(exe_path, O_RDONLY));
581
    sprintf(link1, "/proc/self/fd/%d", fd1);
582
583
    /* Lets try to open link once again. */
584
    fd2 = chk_error(open(link1, O_RDONLY));
585
    sprintf(link2, "/proc/self/fd/%d", fd2);
586
587
    /* Two links should point to the same exe path. */
588
    buf1_length = chk_error(readlink(link1, buf1, PATH_MAX));
589
    if (strlen(exe_realpath) != buf1_length ||
590
        strncmp(exe_realpath, buf1, buf1_length) != 0) {
591
        error("proc myself: invalid link");
592
    }
593
    buf2_length = chk_error(readlink(link2, buf2, PATH_MAX));
594
    if (strlen(exe_realpath) != buf2_length ||
595
        strncmp(exe_realpath, buf2, buf2_length) != 0) {
596
        error("proc myself: invalid link");
597
    }
598
599
    chk_error(close(fd2));
600
    chk_error(close(fd1));
601
}
602
520
int main(int argc, char **argv)
603
int main(int argc, char **argv)
521
{
604
{
522
    test_file();
605
    test_file();
Lines 532-536 int main(int argc, char **argv) Link Here
532
615
533
    test_signal();
616
    test_signal();
534
    test_shm();
617
    test_shm();
618
    test_proc_myself_file();
619
    test_proc_myself_exe();
535
    return 0;
620
    return 0;
536
}
621
}

Return to bug 587230