]> git.cworth.org Git - fips/blob - execute.c
Compile both 32-bit and 64-bit versions of the wrapper library.
[fips] / execute.c
1 /* Copyright © 2013, Intel Corporation
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include <string.h>
28 #include <errno.h>
29
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/wait.h>
34
35 #include <talloc.h>
36
37 #include <linux/limits.h>
38
39 #include <fcntl.h>
40 #include <gelf.h>
41
42 #include "execute.h"
43
44 /* Terminate a string representing a filename at the final '/' to
45  * eliminate the final filename component, (leaving only the directory
46  * portions of the original path).
47  *
48  * Notes: A path containing no '/' character will not be modified.
49  *        A path consisting only of "/" will not be modified.
50  */
51 static void
52 chop_trailing_path_component (char *path)
53 {
54         char *slash;
55
56         slash = strrchr (path, '/');
57
58         if (slash == NULL)
59                 return;
60
61         if (slash == path)
62                 return;
63
64         *slash = '\0';
65 }
66
67 /* Find the absolute path of the currently executing binary.
68  *
69  * Returns: a string talloc'ed to 'ctx'
70  */
71 static char *
72 get_bin_name (void *ctx)
73 {
74         const char *link = "/proc/self/exe";
75         char *name;
76
77         /* Yes, PATH_MAX is cheesy. I would have preferred to have
78          * used lstat and read the resulting st_size, but everytime I
79          * did that with /proc/self/exe I got a value of 0, (whereas
80          * with a "real" symbolic link I make myself I get the length
81          * of the filename being linked to). Go figure. */
82         int name_len = PATH_MAX + 1;
83
84         name = talloc_size (ctx, name_len - 1);
85         if (name == NULL) {
86                 fprintf (stderr, "Out of memory.\n");
87                 exit (1);
88         }
89
90         name_len = readlink (link, name, name_len);
91         if (name_len < 0) {
92                 fprintf (stderr, "Failed to readlink %s: %s\n", link,
93                          strerror (errno));
94                 exit (1);
95         }
96
97         name[name_len + 1] = '\0';
98
99         return name;
100 }
101
102 /* Does path exist? */
103 static int
104 exists (const char *path)
105 {
106         struct stat st;
107         int err;
108
109         err = stat (path, &st);
110
111         /* Failed to stat. It either doesn't exist, or might as well not. */
112         if (err == -1)
113                 return 0;
114
115         return 1;
116 }
117
118 /* Is the given elf program 32 or 64 bit?
119  *
120  * Note: This function aborts the current program if 'program' cannot
121  * be opened as a valid ELF file. */
122 static int
123 elf_bits (const char *program)
124 {
125         Elf *elf;
126         GElf_Ehdr ehdr;
127         int fd, class;
128
129         fd = open (program, O_RDONLY, 0);
130         if (fd < 0) {
131                 fprintf (stderr, "Failed to open %s: %s\n", program,
132                          strerror (errno));
133                 exit (1);
134         }
135
136         if (elf_version (EV_CURRENT ) == EV_NONE) {
137                 fprintf (stderr, "Failed to initialize elf library: %s\n",
138                          elf_errmsg (-1));
139                 exit (1);
140         }
141
142         elf = elf_begin (fd, ELF_C_READ, NULL);
143         if (elf == NULL) {
144                 fprintf (stderr, "Call to elf_begin on %s failed: %s\n",
145                          program, elf_errmsg(-1));
146                 exit (1);
147         }
148
149         if (elf_kind (elf) != ELF_K_ELF) {
150                 fprintf (stderr, "Not an ELF object: %s\n", program);
151                 exit (1);
152         }
153
154         if (gelf_getehdr (elf, &ehdr) == NULL) {
155                 fprintf (stderr, "getehdr on %s failed: %s\n",
156                          program, elf_errmsg (-1));
157                 exit (1);
158         }
159
160         class = gelf_getclass (elf);
161
162         if (class == ELFCLASSNONE) {
163                 fprintf (stderr, "getclass on %s failed: %s\n", program,
164                          elf_errmsg (-1));
165                 exit (1);
166         }
167
168         if (class == ELFCLASS32)
169                 return 32;
170         else
171                 return 64;
172
173 }
174
175 /* Find the appropriate path to the libfips wrapper.
176  *
177  * This involves, first, examining the elf header of the 'program'
178  * binary to be executed to know whether we should look for
179  * libfips-32.so or libfips-64.so.
180  *
181  * Next, we find the absolute patch containing the library as follows:
182  *
183  *   1. Look in same directory as current executable image
184  *
185  *      This is to support running from the source directory, without
186  *      having installed anything.
187  *
188  *   2. Look in relative path from $(foo)/$(bindir) to
189  *      $(foo)/$(libdir)/fips based on $(foo) from current executable
190  *      image and configured $(bindir) and $(libdir).
191  *
192  *      We do this rather than looking directly at the configured
193  *      $(libdir) to support cases where the application may have been
194  *      moved after being installed, (in particular, we want to be
195  *      particularly careful not to mix one program with a different
196  *      wrapper---so this "nearest search" should most often be
197  *      correct.
198  *
199  * Returns: a string talloc'ed to 'ctx'
200  */
201 static char *
202 find_libfips_path (void *ctx, const char *program)
203 {
204         char *bin_path, *library, *lib_path;
205         int bits;
206
207         bits = elf_bits (program);
208
209         library = talloc_asprintf(ctx, "libfips-%d.so", bits);
210
211         bin_path = get_bin_name (ctx);
212
213         chop_trailing_path_component (bin_path);
214
215         lib_path = talloc_asprintf(ctx, "%s/%s", bin_path, library);
216
217         if (exists (lib_path))
218                 return lib_path;
219
220         talloc_free (lib_path);
221
222         lib_path = talloc_asprintf(ctx, "%s/" BINDIR_TO_LIBFIPSDIR "/%s",
223                                    bin_path, library);
224
225         if (exists (lib_path))
226                 return lib_path;
227
228         fprintf (stderr, "Error: Failed to find library %s.\n", library);
229         fprintf (stderr, "Looked in both:\n"
230                  "\t%s\n"
231                  "and\n"
232                  "\t%s/" BINDIR_TO_LIBFIPSDIR "\n", bin_path, bin_path);
233         exit (1);
234 }
235
236 /* After forking, set LD_PRELOAD to preload libfips-{32,64}.so within
237  * child environment, then exec given arguments.
238  */
239 static int
240 fork_exec_with_fips_preload_and_wait (char * const argv[])
241 {
242         pid_t pid;
243         int i, status;
244
245         pid = fork ();
246
247         /* Child */
248         if (pid == 0) {
249                 void *ctx = talloc_new (NULL);
250                 char *lib_path;
251
252                 lib_path = find_libfips_path (ctx, argv[0]);
253
254                 setenv ("LD_PRELOAD", lib_path, 1);
255
256                 talloc_free (ctx);
257                 
258                 execvp (argv[0], argv);
259                 fprintf (stderr, "Failed to execute:");
260                 for (i = 0; argv[i]; i++) {
261                         fprintf (stderr, " %s", argv[i]);
262                 }
263                 fprintf (stderr, "\n");
264                 exit (1);
265         }
266
267         /* Parent */
268         waitpid (pid, &status, 0);
269         if (WIFEXITED (status)) {
270                 return (WEXITSTATUS (status));
271         }
272         if (WIFSIGNALED (status)) {
273                 fprintf (stderr, "Child terminated by signal %d\n",
274                          WTERMSIG (status));
275         }
276         return 1;
277 }
278
279 int
280 execute_with_fips_preload (int argc, char * const argv[])
281 {
282         char **execvp_args;
283         int i;
284
285         execvp_args = malloc((argc + 1) * sizeof(char *));
286         if (execvp_args == NULL) {
287                 fprintf (stderr, "Out of memory,\n");
288                 return 1;
289         }
290
291         for (i = 0; i < argc; i++) {
292                 execvp_args[i] = argv[i];
293         }
294
295         /* execvp needs final NULL */
296         execvp_args[i] = NULL;
297
298         return fork_exec_with_fips_preload_and_wait (execvp_args);
299 }