security/nss/lib/freebl/genload.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /*
michael@0 6 * This file is meant to be included by other .c files.
michael@0 7 * This file takes a "parameter", the scope which includes this
michael@0 8 * code shall declare this variable:
michael@0 9 * const char *NameOfThisSharedLib;
michael@0 10 *
michael@0 11 * NameOfThisSharedLib:
michael@0 12 * The file name of the shared library that shall be used as the
michael@0 13 * "reference library". The loader will attempt to load the requested
michael@0 14 * library from the same directory as the reference library.
michael@0 15 */
michael@0 16
michael@0 17 #ifdef XP_UNIX
michael@0 18 #include <unistd.h>
michael@0 19 #define BL_MAXSYMLINKS 20
michael@0 20
michael@0 21 /*
michael@0 22 * If 'link' is a symbolic link, this function follows the symbolic links
michael@0 23 * and returns the pathname of the ultimate source of the symbolic links.
michael@0 24 * If 'link' is not a symbolic link, this function returns NULL.
michael@0 25 * The caller should call PR_Free to free the string returned by this
michael@0 26 * function.
michael@0 27 */
michael@0 28 static char* loader_GetOriginalPathname(const char* link)
michael@0 29 {
michael@0 30 #ifdef __GLIBC__
michael@0 31 char* tmp = realpath(link, NULL);
michael@0 32 char* resolved;
michael@0 33 if (! tmp)
michael@0 34 return NULL;
michael@0 35 resolved = PR_Malloc(strlen(tmp) + 1);
michael@0 36 strcpy(resolved, tmp); /* This is necessary because PR_Free might not be using free() */
michael@0 37 free(tmp);
michael@0 38 return resolved;
michael@0 39 #else
michael@0 40 char* resolved = NULL;
michael@0 41 char* input = NULL;
michael@0 42 PRUint32 iterations = 0;
michael@0 43 PRInt32 len = 0, retlen = 0;
michael@0 44 if (!link) {
michael@0 45 PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
michael@0 46 return NULL;
michael@0 47 }
michael@0 48 len = PR_MAX(1024, strlen(link) + 1);
michael@0 49 resolved = PR_Malloc(len);
michael@0 50 input = PR_Malloc(len);
michael@0 51 if (!resolved || !input) {
michael@0 52 if (resolved) {
michael@0 53 PR_Free(resolved);
michael@0 54 }
michael@0 55 if (input) {
michael@0 56 PR_Free(input);
michael@0 57 }
michael@0 58 return NULL;
michael@0 59 }
michael@0 60 strcpy(input, link);
michael@0 61 while ( (iterations++ < BL_MAXSYMLINKS) &&
michael@0 62 ( (retlen = readlink(input, resolved, len - 1)) > 0) ) {
michael@0 63 char* tmp = input;
michael@0 64 resolved[retlen] = '\0'; /* NULL termination */
michael@0 65 input = resolved;
michael@0 66 resolved = tmp;
michael@0 67 }
michael@0 68 PR_Free(resolved);
michael@0 69 if (iterations == 1 && retlen < 0) {
michael@0 70 PR_Free(input);
michael@0 71 input = NULL;
michael@0 72 }
michael@0 73 return input;
michael@0 74 #endif
michael@0 75 }
michael@0 76 #endif /* XP_UNIX */
michael@0 77
michael@0 78 /*
michael@0 79 * Load the library with the file name 'name' residing in the same
michael@0 80 * directory as the reference library, whose pathname is 'referencePath'.
michael@0 81 */
michael@0 82 static PRLibrary *
michael@0 83 loader_LoadLibInReferenceDir(const char *referencePath, const char *name)
michael@0 84 {
michael@0 85 PRLibrary *dlh = NULL;
michael@0 86 char *fullName = NULL;
michael@0 87 char* c;
michael@0 88 PRLibSpec libSpec;
michael@0 89
michael@0 90 /* Remove the trailing filename from referencePath and add the new one */
michael@0 91 c = strrchr(referencePath, PR_GetDirectorySeparator());
michael@0 92 if (c) {
michael@0 93 size_t referencePathSize = 1 + c - referencePath;
michael@0 94 fullName = (char*) PORT_Alloc(strlen(name) + referencePathSize + 1);
michael@0 95 if (fullName) {
michael@0 96 memcpy(fullName, referencePath, referencePathSize);
michael@0 97 strcpy(fullName + referencePathSize, name);
michael@0 98 #ifdef DEBUG_LOADER
michael@0 99 PR_fprintf(PR_STDOUT, "\nAttempting to load fully-qualified %s\n",
michael@0 100 fullName);
michael@0 101 #endif
michael@0 102 libSpec.type = PR_LibSpec_Pathname;
michael@0 103 libSpec.value.pathname = fullName;
michael@0 104 dlh = PR_LoadLibraryWithFlags(libSpec, PR_LD_NOW | PR_LD_LOCAL);
michael@0 105 PORT_Free(fullName);
michael@0 106 }
michael@0 107 }
michael@0 108 return dlh;
michael@0 109 }
michael@0 110
michael@0 111 /*
michael@0 112 * We use PR_GetLibraryFilePathname to get the pathname of the loaded
michael@0 113 * shared lib that contains this function, and then do a PR_LoadLibrary
michael@0 114 * with an absolute pathname for the softoken shared library.
michael@0 115 */
michael@0 116
michael@0 117 static PRLibrary *
michael@0 118 loader_LoadLibrary(const char *nameToLoad)
michael@0 119 {
michael@0 120 PRLibrary *lib = NULL;
michael@0 121 char* fullPath = NULL;
michael@0 122 PRLibSpec libSpec;
michael@0 123
michael@0 124 /* Get the pathname for nameOfAlreadyLoadedLib, i.e. /usr/lib/libnss3.so
michael@0 125 * PR_GetLibraryFilePathname works with either the base library name or a
michael@0 126 * function pointer, depending on the platform. We can't query an exported
michael@0 127 * symbol such as NSC_GetFunctionList, because on some platforms we can't
michael@0 128 * find symbols in loaded implicit dependencies.
michael@0 129 * But we can just get the address of this function !
michael@0 130 */
michael@0 131 fullPath = PR_GetLibraryFilePathname(NameOfThisSharedLib,
michael@0 132 (PRFuncPtr)&loader_LoadLibrary);
michael@0 133
michael@0 134 if (fullPath) {
michael@0 135 lib = loader_LoadLibInReferenceDir(fullPath, nameToLoad);
michael@0 136 #ifdef XP_UNIX
michael@0 137 if (!lib) {
michael@0 138 /*
michael@0 139 * If fullPath is a symbolic link, resolve the symbolic
michael@0 140 * link and try again.
michael@0 141 */
michael@0 142 char* originalfullPath = loader_GetOriginalPathname(fullPath);
michael@0 143 if (originalfullPath) {
michael@0 144 PR_Free(fullPath);
michael@0 145 fullPath = originalfullPath;
michael@0 146 lib = loader_LoadLibInReferenceDir(fullPath, nameToLoad);
michael@0 147 }
michael@0 148 }
michael@0 149 #endif
michael@0 150 PR_Free(fullPath);
michael@0 151 }
michael@0 152 if (!lib) {
michael@0 153 #ifdef DEBUG_LOADER
michael@0 154 PR_fprintf(PR_STDOUT, "\nAttempting to load %s\n", nameToLoad);
michael@0 155 #endif
michael@0 156 libSpec.type = PR_LibSpec_Pathname;
michael@0 157 libSpec.value.pathname = nameToLoad;
michael@0 158 lib = PR_LoadLibraryWithFlags(libSpec, PR_LD_NOW | PR_LD_LOCAL);
michael@0 159 }
michael@0 160 if (NULL == lib) {
michael@0 161 #ifdef DEBUG_LOADER
michael@0 162 PR_fprintf(PR_STDOUT, "\nLoading failed : %s.\n", nameToLoad);
michael@0 163 #endif
michael@0 164 }
michael@0 165 return lib;
michael@0 166 }
michael@0 167

mercurial