/[path-sandbox]/trunk/libsandbox/wrapper-funcs/execve.c
Gentoo

Contents of /trunk/libsandbox/wrapper-funcs/execve.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 410 - (show annotations) (download) (as text)
Sun Nov 16 04:12:34 2008 UTC (5 years, 9 months ago) by vapier
File MIME type: text/x-csrc
File size: 3455 byte(s)
fix up one more old style SB_EERROR
1 /*
2 * execve() wrapper.
3 *
4 * Copyright 1999-2008 Gentoo Foundation
5 * Licensed under the GPL-2
6 *
7 * Partly Copyright (C) 1998-9 Pancrazio `Ezio' de Mauro <p@demauro.net>,
8 * as some of the InstallWatch code was used.
9 */
10
11 #define WRAPPER_ARGS const char *filename, char *const argv[], char *const envp[]
12 extern int EXTERN_NAME(WRAPPER_ARGS);
13 static int (*WRAPPER_TRUE_NAME)(WRAPPER_ARGS) = NULL;
14
15 /* See to see if this an ELF and if so, is it static which we can't wrap */
16 void check_exec(const char *filename)
17 {
18 int fd;
19 unsigned char *elf;
20 struct stat st;
21
22 fd = open(filename, O_RDONLY);
23 if (fd == -1)
24 return;
25 if (stat(filename, &st))
26 goto out_fd;
27 elf = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
28 if (elf == MAP_FAILED)
29 goto out_fd;
30
31 if (elf[EI_MAG0] != ELFMAG0 &&
32 elf[EI_MAG1] != ELFMAG1 &&
33 elf[EI_MAG2] != ELFMAG2 &&
34 elf[EI_MAG3] != ELFMAG3 &&
35 !(elf[EI_CLASS] != ELFCLASS32 ||
36 elf[EI_CLASS] != ELFCLASS64))
37 goto out_mmap;
38
39 #define PARSE_ELF(n) \
40 ({ \
41 Elf##n##_Ehdr *ehdr = (void *)elf; \
42 Elf##n##_Phdr *phdr = (void *)(elf + ehdr->e_phoff); \
43 uint16_t p; \
44 for (p = 0; p < ehdr->e_phnum; ++p) \
45 if (phdr[p].p_type == PT_INTERP) \
46 goto done; \
47 })
48 if (elf[EI_CLASS] == ELFCLASS32)
49 PARSE_ELF(32);
50 else
51 PARSE_ELF(64);
52 SB_EWARN("QA: Static ELF", " %s\n", filename);
53 done:
54
55 out_mmap:
56 munmap(elf, st.st_size);
57 out_fd:
58 close(fd);
59 }
60
61 int WRAPPER_NAME(WRAPPER_ARGS)
62 {
63 char **my_env = NULL;
64 char *entry;
65 char *ld_preload = NULL;
66 char *old_ld_preload = NULL;
67 int old_errno = errno;
68 int result = -1;
69 int count;
70
71 if (!FUNCTION_SANDBOX_SAFE(STRING_NAME, filename))
72 return result;
73
74 check_exec(filename);
75
76 str_list_for_each_item(envp, entry, count) {
77 if (strstr(entry, LD_PRELOAD_EQ) != entry)
78 continue;
79
80 /* Check if we do not have to do anything */
81 if (NULL != strstr(entry, sandbox_lib)) {
82 /* Use the user's envp */
83 my_env = (char **)envp;
84 goto do_execve;
85 } else {
86 old_ld_preload = entry;
87 /* No need to continue, we have to modify LD_PRELOAD */
88 break;
89 }
90 }
91
92 /* Ok, we need to create our own envp, as we need to add LD_PRELOAD,
93 * and we should not touch the user's envp. First we add LD_PRELOAD,
94 * and just all the rest. */
95 count = strlen(LD_PRELOAD_EQ) + strlen(sandbox_lib) + 1;
96 if (NULL != old_ld_preload)
97 count += strlen(old_ld_preload) - strlen(LD_PRELOAD_EQ) + 1;
98 ld_preload = xmalloc(count * sizeof(char));
99 if (NULL == ld_preload)
100 goto error;
101 snprintf(ld_preload, count, "%s%s%s%s", LD_PRELOAD_EQ, sandbox_lib,
102 (old_ld_preload) ? " " : "",
103 (old_ld_preload) ? old_ld_preload + strlen(LD_PRELOAD_EQ) : "");
104 str_list_add_item(my_env, ld_preload, error);
105
106 str_list_for_each_item(envp, entry, count) {
107 if (strstr(entry, LD_PRELOAD_EQ) != entry) {
108 str_list_add_item(my_env, entry, error);
109 continue;
110 }
111 }
112
113 do_execve:
114 errno = old_errno;
115 check_dlsym(WRAPPER_TRUE_NAME, WRAPPER_SYMNAME,
116 WRAPPER_SYMVER);
117 result = WRAPPER_TRUE_NAME(filename, argv, my_env);
118
119 if ((NULL != my_env) && (my_env != envp))
120 /* We do not use str_list_free(), as we did not allocate the
121 * entries except for LD_PRELOAD. */
122 free(my_env);
123 if (NULL != ld_preload)
124 free(ld_preload);
125
126 return result;
127
128 error:
129 if ((NULL != my_env) && (my_env != envp))
130 /* We do not use str_list_free(), as we did not allocate the
131 * entries except for LD_PRELOAD. */
132 free(my_env);
133 if (NULL != ld_preload)
134 free(ld_preload);
135
136 return -1;
137 }

  ViewVC Help
Powered by ViewVC 1.1.20