From: John Marino Date: Wed, 12 Jun 2013 12:24:28 +0000 (+0200) Subject: rtld: Sync 4/7 - Fix token substitution X-Git-Tag: v3.7.0~974 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/da404cb4ad58fa4443ec9a94a036420276919b79 rtld: Sync 4/7 - Fix token substitution The origin_subst_one() function limits the length of the string to PATH_MAX after the token substitution. This is wrong, because origin_subst_one() performs the substitution on the whole rpath and similar strings, which contain several pathes separated by colon. As result, a long (but correct) rpath consisting of many path elements is rejected by the function. Correct the problem by rewriting the origin_subst_one() to perform two passes, first pass to calculate the number of substitutions to be performed, and second pass to generate the resulting string. Second pass allocates the memory for the result based on the count from the first pass, without enforcing a limit. Taken verbatim from: FreeBSD SVN 249525 (15 APR 2013) FreeBSD SVN 250075 (29 APR 2013) --- diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index 4763eba09b..67da21c7a5 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -146,9 +146,8 @@ static void unlink_object(Obj_Entry *); static void unload_object(Obj_Entry *); static void unref_dag(Obj_Entry *); static void ref_dag(Obj_Entry *); -static int origin_subst_one(char **, const char *, const char *, - const char *, char *); -static char *origin_subst(const char *, const char *); +static char *origin_subst_one(char *, const char *, const char *, bool); +static char *origin_subst(char *, const char *); static void preinitialize_main_object (void); static int rtld_verify_versions(const Objlist *); static int rtld_verify_object_versions(Obj_Entry *); @@ -873,79 +872,81 @@ basename(const char *name) static struct utsname uts; -static int -origin_subst_one(char **res, const char *real, const char *kw, const char *subst, - char *may_free) +static char * +origin_subst_one(char *real, const char *kw, const char *subst, + bool may_free) { - const char *p, *p1; - char *res1; - int subst_len; - int kw_len; - - res1 = *res = NULL; - p = real; - subst_len = kw_len = 0; - for (;;) { - p1 = strstr(p, kw); - if (p1 != NULL) { - if (subst_len == 0) { - subst_len = strlen(subst); - kw_len = strlen(kw); - } - if (*res == NULL) { - *res = xmalloc(PATH_MAX); - res1 = *res; - } - if ((res1 - *res) + subst_len + (p1 - p) >= PATH_MAX) { - _rtld_error("Substitution of %s in %s cannot be performed", - kw, real); - if (may_free != NULL) - free(may_free); - free(res); - return (false); - } - memcpy(res1, p, p1 - p); - res1 += p1 - p; - memcpy(res1, subst, subst_len); - res1 += subst_len; - p = p1 + kw_len; - } else { - if (*res == NULL) { - if (may_free != NULL) - *res = may_free; - else - *res = xstrdup(real); - return (true); - } - *res1 = '\0'; - if (may_free != NULL) - free(may_free); - if (strlcat(res1, p, PATH_MAX - (res1 - *res)) >= PATH_MAX) { - free(res); - return (false); - } - return (true); - } - } + char *p, *p1, *res, *resp; + int subst_len, kw_len, subst_count, old_len, new_len; + + kw_len = strlen(kw); + + /* + * First, count the number of the keyword occurences, to + * preallocate the final string. + */ + for (p = real, subst_count = 0;; p = p1 + kw_len, subst_count++) { + p1 = strstr(p, kw); + if (p1 == NULL) + break; + } + + /* + * If the keyword is not found, just return. + */ + if (subst_count == 0) + return (may_free ? real : xstrdup(real)); + + /* + * There is indeed something to substitute. Calculate the + * length of the resulting string, and allocate it. + */ + subst_len = strlen(subst); + old_len = strlen(real); + new_len = old_len + (subst_len - kw_len) * subst_count; + res = xmalloc(new_len + 1); + + /* + * Now, execute the substitution loop. + */ + for (p = real, resp = res, *resp = '\0';;) { + p1 = strstr(p, kw); + if (p1 != NULL) { + /* Copy the prefix before keyword. */ + memcpy(resp, p, p1 - p); + resp += p1 - p; + /* Keyword replacement. */ + memcpy(resp, subst, subst_len); + resp += subst_len; + *resp = '\0'; + p = p1 + kw_len; + } else + break; + } + + /* Copy to the end of string and finish. */ + strcat(resp, p); + if (may_free) + free(real); + return (res); } static char * -origin_subst(const char *real, const char *origin_path) +origin_subst(char *real, const char *origin_path) { - char *res1, *res2, *res3, *res4; + char *res1, *res2, *res3, *res4; - if (uts.sysname[0] == '\0') { - if (uname(&uts) != 0) { - _rtld_error("utsname failed: %d", errno); - return (NULL); + if (uts.sysname[0] == '\0') { + if (uname(&uts) != 0) { + _rtld_error("utsname failed: %d", errno); + return (NULL); + } } - } - if (!origin_subst_one(&res1, real, "$ORIGIN", origin_path, NULL) || - !origin_subst_one(&res2, res1, "$OSNAME", uts.sysname, res1) || - !origin_subst_one(&res3, res2, "$OSREL", uts.release, res2) || - !origin_subst_one(&res4, res3, "$PLATFORM", uts.machine, res3)) - return (NULL); - return (res4); + res1 = origin_subst_one(real, "$ORIGIN", origin_path, false); + res2 = origin_subst_one(res1, "$OSNAME", uts.sysname, true); + res3 = origin_subst_one(res2, "$OSREL", uts.release, true); + res4 = origin_subst_one(res3, "$PLATFORM", uts.machine, true); + return (res4); } static void @@ -1530,10 +1531,12 @@ find_library(const char *xname, const Obj_Entry *refobj) xname); return NULL; } - if (objgiven && refobj->z_origin) - return origin_subst(xname, refobj->origin_path); - else - return xstrdup(xname); + if (objgiven && refobj->z_origin) { + return (origin_subst(__DECONST(char *, xname), + refobj->origin_path)); + } else { + return (xstrdup(xname)); + } } if (libmap_disable || !objgiven ||