• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CRAZY_LINKER_H
6 #define CRAZY_LINKER_H
7 
8 // This is the crazy linker, a custom dynamic linker that can be used
9 // by NDK applications to load shared libraries (not executables) with
10 // a twist.
11 //
12 // Compared to the dynamic linker, the crazy one has the following
13 // features:
14 //
15 //   - It can use an arbitrary search path.
16 //
17 //   - It can load a library at a memory fixed address, or from a fixed
18 //     file offset (both must be page-aligned).
19 //
20 //   - It can share the RELRO section between two libraries
21 //     loaded at the same address in two distinct processes.
22 //
23 #include <dlfcn.h>
24 #include <stdbool.h>
25 #include <stddef.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 // Function attribute to indicate that it needs to be exported by
32 // the library.
33 #define _CRAZY_PUBLIC __attribute__((__visibility__("default")))
34 
35 // Status values returned by crazy linker functions to indicate
36 // success or failure. They were chosen to match boolean values,
37 // this allows one to test for failures with:
38 //
39 //    if (!crazy_linker_function(....)) {
40 //       ... an error occured.
41 //    }
42 //
43 // If the function called used a crazy_context_t, it is possible to
44 // retrieve the error details with crazy_context_get_error().
45 typedef enum {
46   CRAZY_STATUS_FAILURE = 0,
47   CRAZY_STATUS_SUCCESS = 1
48 } crazy_status_t;
49 
50 // Opaque handle to a context object that will hold parameters
51 // for the crazy linker's operations. For example, this is where
52 // you would set the explicit load address, and other user-provided
53 // values before calling functions like crazy_library_open().
54 //
55 // The context holds a list of library search paths, initialized to
56 // the content of the LD_LIBRARY_PATH variable on creation.
57 //
58 // The context also holds a string buffer to hold error messages that
59 // can be queried with crazy_context_get_error().
60 typedef struct crazy_context_t crazy_context_t;
61 
62 // Create a new context object.
63 // Note that this calls crazy_context_reset_search_paths().
64 crazy_context_t* crazy_context_create(void) _CRAZY_PUBLIC;
65 
66 // Return current error string, or NULL if there was no error.
67 const char* crazy_context_get_error(crazy_context_t* context) _CRAZY_PUBLIC;
68 
69 // Clear error in a given context.
70 void crazy_context_clear_error(crazy_context_t* context) _CRAZY_PUBLIC;
71 
72 // Set the explicit load address in a context object. Value 0 means
73 // the address is randomized.
74 void crazy_context_set_load_address(crazy_context_t* context,
75                                     size_t load_address) _CRAZY_PUBLIC;
76 
77 // Return the current load address in a context.
78 size_t crazy_context_get_load_address(crazy_context_t* context) _CRAZY_PUBLIC;
79 
80 // Set the explicit file offset in a context object. The value should
81 // always page-aligned, or the load will fail.
82 void crazy_context_set_file_offset(crazy_context_t* context,
83                                    size_t file_offset) _CRAZY_PUBLIC;
84 
85 // Return the current file offset in a context object.
86 size_t crazy_context_get_file_offset(crazy_context_t* context);
87 
88 // Add one or more paths to the list of library search paths held
89 // by a given context. |path| is a string using a column (:) as a
90 // list separator. As with the PATH variable, an empty list item
91 // is equivalent to '.', the current directory.
92 // This can fail if too many paths are added to the context.
93 //
94 // NOTE: Calling this function appends new paths to the search list,
95 // but all paths added with this function will be searched before
96 // the ones listed in LD_LIBRARY_PATH.
97 crazy_status_t crazy_context_add_search_path(
98     crazy_context_t* context,
99     const char* file_path) _CRAZY_PUBLIC;
100 
101 // Find the ELF binary that contains |address|, and add its directory
102 // path to the context's list of search directories. This is useful to
103 // load libraries in the same directory than the current program itself.
104 crazy_status_t crazy_context_add_search_path_for_address(
105     crazy_context_t* context,
106     void* address) _CRAZY_PUBLIC;
107 
108 // Reset the search paths to the value of the LD_LIBRARY_PATH
109 // environment variable. This essentially removes any paths
110 // that were added with crazy_context_add_search_path() or
111 // crazy_context_add_search_path_for_address().
112 void crazy_context_reset_search_paths(crazy_context_t* context) _CRAZY_PUBLIC;
113 
114 // Record the value of |java_vm| inside of |context|. If this is not NULL,
115 // which is the default, then after loading any library, the crazy linker
116 // will look for a "JNI_OnLoad" symbol within it, and, if it exists, will call
117 // it, passing the value of |java_vm| to it. If the function returns with
118 // a jni version number that is smaller than |minimum_jni_version|, then
119 // the library load will fail with an error.
120 //
121 // The |java_vm| field is also saved in the crazy_library_t object, and
122 // used at unload time to call JNI_OnUnload() if it exists.
123 void crazy_context_set_java_vm(crazy_context_t* context,
124                                void* java_vm,
125                                int minimum_jni_version);
126 
127 // Retrieves the last values set with crazy_context_set_java_vm().
128 // A value of NUMM in |*java_vm| means the feature is disabled.
129 void crazy_context_get_java_vm(crazy_context_t* context,
130                                void** java_vm,
131                                int* minimum_jni_version);
132 
133 // Destroy a given context object.
134 void crazy_context_destroy(crazy_context_t* context) _CRAZY_PUBLIC;
135 
136 // Some operations performed by the crazy linker might conflict with the
137 // system linker if they are used concurrently in different threads
138 // (e.g. modifying the list of shared libraries seen by GDB). To work
139 // around this, the crazy linker provides a way to delay these conflicting
140 // operations for a later time.
141 //
142 // This works by wrapping each of these operations in a small data structure
143 // (crazy_callback_t), which can later be passed to crazy_callback_run()
144 // to execute it.
145 //
146 // The user must provide a function to record these callbacks during
147 // library loading, by calling crazy_linker_set_callback_poster().
148 //
149 // Once all libraries are loaded, the callbacks can be later called either
150 // in a different thread, or when it is safe to assume the system linker
151 // cannot be running in parallel.
152 
153 // Callback handler.
154 typedef void (*crazy_callback_handler_t)(void* opaque);
155 
156 // A small structure used to model a callback provided by the crazy linker.
157 // Use crazy_callback_run() to run the callback.
158 typedef struct {
159   crazy_callback_handler_t handler;
160   void* opaque;
161 } crazy_callback_t;
162 
163 // Function to call to enable a callback into the crazy linker when delayed
164 // operations are enabled (see crazy_context_set_callback_poster). A call
165 // to crazy_callback_poster_t returns true if the callback was successfully
166 // set up and will occur later, false if callback could not be set up (and
167 // so will never occur).
168 typedef bool (*crazy_callback_poster_t)(
169     crazy_callback_t* callback, void* poster_opaque);
170 
171 // Enable delayed operation, by passing the address of a
172 // |crazy_callback_poster_t| function, that will be called during library
173 // loading to let the user record callbacks for delayed operations.
174 // Callers must copy the |crazy_callback_t| passed to |poster|.
175 // |poster_opaque| is an opaque value for client code use, passed back
176 // on each call to |poster|.
177 // |poster| can be NULL to disable the feature.
178 void crazy_context_set_callback_poster(crazy_context_t* context,
179                                        crazy_callback_poster_t poster,
180                                        void* poster_opaque);
181 
182 // Return the address of the function that the crazy linker can use to
183 // request callbacks, and the |poster_opaque| passed back on each call
184 // to |poster|. |poster| is NULL if the feature is disabled.
185 void crazy_context_get_callback_poster(crazy_context_t* context,
186                                        crazy_callback_poster_t* poster,
187                                        void** poster_opaque);
188 
189 // Run a given |callback| in the current thread. Must only be called once
190 // per callback.
191 void crazy_callback_run(crazy_callback_t* callback);
192 
193 // Opaque handle to a library as seen/loaded by the crazy linker.
194 typedef struct crazy_library_t crazy_library_t;
195 
196 // Try to open or load a library with the crazy linker.
197 // |lib_name| if the library name or path. If it contains a directory
198 // separator (/), this is treated as a explicit file path, otherwise
199 // it is treated as a base name, and the context's search path list
200 // will be used to locate the corresponding file.
201 // |context| is a linker context handle. Can be NULL for defaults.
202 // On success, return CRAZY_STATUS_SUCCESS and sets |*library|.
203 // Libraries are reference-counted, trying to open the same library
204 // twice will return the same library handle.
205 //
206 // NOTE: The load address and file offset from the context only apply
207 // to the library being loaded (when not already in the process). If the
208 // operations needs to load any dependency libraries, these will use
209 // offset and address values of 0 to do so.
210 //
211 // NOTE: It is possible to open NDK system libraries (e.g. "liblog.so")
212 // with this function, but they will be loaded with the system dlopen().
213 crazy_status_t crazy_library_open(crazy_library_t** library,
214                                   const char* lib_name,
215                                   crazy_context_t* context) _CRAZY_PUBLIC;
216 
217 // A structure used to hold information about a given library.
218 // |load_address| is the library's actual (page-aligned) load address.
219 // |load_size| is the library's actual (page-aligned) size.
220 // |relro_start| is the address of the library's RELRO section in memory.
221 // |relso_size| is the size of the library's RELRO section (or 0 if none).
222 // |relro_fd| is the ashmem file descriptor for the shared section, if one
223 // was created with crazy_library_enable_relro_sharing(), -1 otherwise.
224 typedef struct {
225   size_t load_address;
226   size_t load_size;
227   size_t relro_start;
228   size_t relro_size;
229 } crazy_library_info_t;
230 
231 // Retrieve information about a given library.
232 // |library| is a library handle.
233 // |context| will get an error message on failure.
234 // On success, return true and sets |*info|.
235 // Note that this function will fail for system libraries.
236 crazy_status_t crazy_library_get_info(crazy_library_t* library,
237                                       crazy_context_t* context,
238                                       crazy_library_info_t* info);
239 
240 // Checks whether the system can support RELRO section sharing. This is
241 // mainly due to the fact that old Android kernel images have a bug in their
242 // implementation of Ashmem region mapping protection.
243 // If this function returns CRAZY_STATUS_FAILURE, then calls to
244 // crazy_library_enable_relro_sharing() will return a failure to prevent
245 // the exploitation of this security issue in your code.
246 crazy_status_t crazy_system_can_share_relro(void);
247 
248 // Create an ashmem region containing a copy of the RELRO section for a given
249 // |library|. This can be used with crazy_library_use_shared_relro().
250 // |load_address| can be specified as non-0 to ensure that the content of the
251 // ashmem region corresponds to a RELRO relocated for a new load address.
252 // on success, return CRAZY_STATUS_SUCCESS and sets |*relro_start| to the
253 // start of the RELRO section in memory, |*relro_size| to its size in bytes
254 // and |*relro_fd| to a file descriptor to a read-only ashmem region containing
255 // the data. On failure, return false and set error message in |context|.
256 // NOTE: On success, the caller becomes the owner of |*relro_fd| and is shall
257 // close it appropriately.
258 crazy_status_t crazy_library_create_shared_relro(crazy_library_t* library,
259                                                  crazy_context_t* context,
260                                                  size_t load_address,
261                                                  size_t* relro_start,
262                                                  size_t* relro_size,
263                                                  int* relro_fd) _CRAZY_PUBLIC;
264 
265 // Use the shared RELRO section of the same library loaded in a different
266 // address space. On success, return CRAZY_STATUS_SUCCESS and owns |relro_fd|.
267 // On failure, return CRAZY_STATUS_FAILURE and sets error message in |context|.
268 // |library| is the library handle.
269 // |relro_start| is the address of the RELRO section in memory.
270 // |relro_size| is the size of the RELRO section.
271 // |relro_fd| is the file descriptor for the shared RELRO ashmem region.
272 // |context| will receive an error in case of failure.
273 // NOTE: This will fail if this is a system library, or if the RELRO
274 // parameters do not match the library's actual load address.
275 // NOTE: The caller is responsible for closing the file descriptor after this
276 // call.
277 crazy_status_t crazy_library_use_shared_relro(crazy_library_t* library,
278                                               crazy_context_t* context,
279                                               size_t relro_start,
280                                               size_t relro_size,
281                                               int relro_fd) _CRAZY_PUBLIC;
282 
283 // Look for a library named |library_name| in the set of currently
284 // loaded libraries, and return a handle for it in |*library| on success.
285 // Note that this increments the reference count on the library, thus
286 // the caller shall call crazy_library_close() when it's done with it.
287 crazy_status_t crazy_library_find_by_name(const char* library_name,
288                                           crazy_library_t** library);
289 
290 // Find the library that contains a given |address| in memory.
291 // On success, return CRAZY_STATUS_SUCCESS and sets |*library|.
292 crazy_status_t crazy_linker_find_library_from_address(
293     void* address,
294     crazy_library_t** library) _CRAZY_PUBLIC;
295 
296 // Lookup a symbol's address by its |symbol_name| in a given library.
297 // This only looks at the symbols in |library|.
298 // On success, returns CRAZY_STATUS_SUCCESS and sets |*symbol_address|,
299 // which could be NULL for some symbols.
300 crazy_status_t crazy_library_find_symbol(crazy_library_t* library,
301                                          const char* symbol_name,
302                                          void** symbol_address) _CRAZY_PUBLIC;
303 
304 // Lookup a symbol's address in all libraries known by the crazy linker.
305 // |symbol_name| is the symbol name. On success, returns CRAZY_STATUS_SUCCESS
306 // and sets |*symbol_address|.
307 // NOTE: This will _not_ look into system libraries that were not opened
308 // with the crazy linker.
309 crazy_status_t crazy_linker_find_symbol(const char* symbol_name,
310                                         void** symbol_address) _CRAZY_PUBLIC;
311 
312 // Find the in-process library that contains a given memory address.
313 // Note that this works even if the memory is inside a system library that
314 // was not previously opened with crazy_library_open().
315 // |address| is the memory address.
316 // On success, returns CRAZY_STATUS_SUCCESS and sets |*library|.
317 // The caller muyst call crazy_library_close() once it's done with the
318 // library.
319 crazy_status_t crazy_library_find_from_address(
320     void* address,
321     crazy_library_t** library) _CRAZY_PUBLIC;
322 
323 // Close a library. This decrements its reference count. If it reaches
324 // zero, the library be unloaded from the process.
325 void crazy_library_close(crazy_library_t* library) _CRAZY_PUBLIC;
326 
327 // Close a library, with associated context to support delayed operations.
328 void crazy_library_close_with_context(crazy_library_t* library,
329                                       crazy_context_t* context) _CRAZY_PUBLIC;
330 
331 #ifdef __cplusplus
332 } /* extern "C" */
333 #endif
334 
335 #endif /* CRAZY_LINKER_H */
336