]> git.cworth.org Git - fips/blob - dlwrap.c
configure: Test whether compiler can create both 32 and 64-bit binaries
[fips] / dlwrap.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 /* dladdr is a glibc extension */
23 #define _GNU_SOURCE
24 #include <dlfcn.h>
25
26 #include "fips.h"
27
28 #include "dlwrap.h"
29
30 void *libfips_handle;
31
32 typedef void * (* fips_dlopen_t)(const char * filename, int flag);
33 typedef void * (* fips_dlsym_t)(void *handle, const char *symbol);
34
35 /* Many (most?) OpenGL programs dlopen libGL.so.1 rather than linking
36  * against it directly, which means they would not be seeing our
37  * wrapped GL symbols via LD_PRELOAD. So we catch the dlopen in a
38  * wrapper here and redirect it to our library.
39  */
40 void *
41 dlopen (const char *filename, int flag)
42 {
43         Dl_info info;
44         void *ret;
45
46         /* Before deciding whether to redirect this dlopen to our own
47          * library, we call the real dlopen. This assures that any
48          * expected side-effects from loading the intended library are
49          * resolved. Below, we may still return a handle pointing to
50          * our own library, and not what is opened here. */
51         ret = dlwrap_real_dlopen (filename, flag);
52
53         /* Not libGL, so just return real dlopen */
54         if (STRNCMP_LITERAL (filename, "libGL.so"))
55                 return ret;
56
57         /* Otherwise, for all libGL lookups we redirectl dlopens to
58          * our own library. If we've resolved libfips_handle before,
59          * our work is done. */
60         if (libfips_handle)
61                 return libfips_handle;
62
63         /* We find our own filename by looking up this very function
64          * (that is, this "dlopen"), with dladdr).*/
65         if (dladdr (dlopen, &info) == 0) {
66                 fprintf (stderr, "Error: Failed to redirect dlopen of %s:\n",
67                          filename);
68                 exit (1);
69         }
70
71         libfips_handle = dlwrap_real_dlopen (info.dli_fname, flag);
72
73         return libfips_handle;
74 }
75
76 void *
77 dlwrap_real_dlopen (const char *filename, int flag)
78 {
79         static fips_dlopen_t real_dlopen = NULL;
80
81         if (! real_dlopen) {
82                 real_dlopen = (fips_dlopen_t) dlwrap_real_dlsym (RTLD_NEXT, "dlopen");
83                 if (! real_dlopen) {
84                         fprintf (stderr, "Error: Failed to find symbol for dlopen.\n");
85                         exit (1);
86                 }
87         }
88
89         return real_dlopen (filename, flag);
90 }
91
92 /* Since we redirect a dlopen of libGL.so to libfips we need to ensure
93  * that dlysm succeeds for all functions that might be defined in the
94  * real, underlying libGL library. But we're far too lazy to implement
95  * wrappers for function that would simply pass-through, so instead we
96  * also wrap dlysm and arrange for it to pass things through with
97  * RTLD_next if libfips does not have the function desired.
98 */
99 void *
100 dlsym (void *handle, const char *name)
101 {
102         static void *libgl_handle = NULL;
103         static void *symbol;
104
105         /* All gl symbols are preferentially looked up in libfips. */
106         if (STRNCMP_LITERAL (name, "gl") == 0) {
107                 symbol = dlwrap_real_dlsym (libfips_handle, name);
108                 if (symbol)
109                         return symbol;
110         }
111
112         /* Failing that, anything specifically requested from the
113          * libfips library should be redirected to a real GL
114          * library. */
115         if (handle == libfips_handle) {
116                 if (! libgl_handle)
117                         libgl_handle = dlwrap_real_dlopen ("libGL.so.1", RTLD_LAZY);
118                 return dlwrap_real_dlsym (libgl_handle, name);
119         }
120
121         /* And anything else is some unrelated dlsym. Just pass it through. */
122         return dlwrap_real_dlsym (handle, name);
123 }
124
125 void *
126 dlwrap_real_dlsym (void *handle, const char *name)
127 {
128         static fips_dlsym_t real_dlsym = NULL;
129
130         if (! real_dlsym) {
131                 /* FIXME: This brute-force, hard-coded searching for a versioned
132                  * symbol is really ugly. The only reason I'm doing this is because
133                  * I need some way to lookup the "dlsym" function in libdl, but
134                  * I can't use 'dlsym' to do it. So dlvsym works, but forces me
135                  * to guess what the right version is.
136                  *
137                  * Potential fixes here:
138                  *
139                  *   1. Use libelf to actually inspect libdl.so and
140                  *      find the right version, (finding the right
141                  *      libdl.so can be made easier with
142                  *      dl_iterate_phdr).
143                  *
144                  *   2. Use libelf to find the offset of the 'dlsym'
145                  *      symbol within libdl.so, (and then add this to
146                  *      the base address at which libdl.so is loaded
147                  *      as reported by dl_iterate_phdr).
148                  *
149                  * In the meantime, I'll just keep augmenting this
150                  * hard-coded version list as people report bugs. */
151                 const char *version[] = {
152                         "GLIBC_2.2.5",
153                         "GLIBC_2.0"
154                 };
155                 int num_versions = sizeof(version) / sizeof(version[0]);
156                 int i;
157                 for (i = 0; i < num_versions; i++) {
158                         real_dlsym = (fips_dlsym_t) dlvsym (RTLD_NEXT, "dlsym",
159                                                             version[i]);
160                         if (real_dlsym)
161                                 break;
162                 }
163                 if (i == num_versions) {
164                         fprintf (stderr, "Internal error: Failed to find real dlsym\n");
165                         fprintf (stderr,
166 "This may be a simple matter of fips not knowing about the version of GLIBC that\n"
167 "your program is using. Current known versions are:\n\n\t");
168                         for (i = 0; i < num_versions; i++)
169                                 fprintf (stderr, "%s ", version[i]);
170                         fprintf(stderr,
171 "\n\nYou can inspect your version by first finding libdl.so.2:\n"
172 "\n"
173 "\tldd <your-program> | grep libdl.so\n"
174 "\n"
175 "And then inspecting the version attached to the dlsym symbol:\n"
176 "\n"
177 "\treadelf -s /path/to/libdl.so.2 | grep dlsym\n"
178 "\n"
179 "And finally, adding the version to dlwrap.c:dlwrap_real_dlsym.\n");
180
181                         exit (1);
182                 }
183         }
184
185         return real_dlsym (handle, name);
186 }