From 2aeb9374185c38fac59a51433e48be0cc81d089b Mon Sep 17 00:00:00 2001 From: Yuanjie Huang Date: Fri, 16 Jun 2017 03:23:01 -0700 Subject: [PATCH] glibc: preliminary fix to CVE-2017-100366 [ LIN8-6896 ] CVE: CVE-2017-100366 Signed-off-by: Yuanjie Huang --- .../recipes-core/glibc/glibc/CVE-2017-100366.patch | 32 ++++ .../glibc/glibc/CVE-2017-100366_2.patch | 110 +++++++++++ .../glibc/glibc/CVE-2017-100366_3.patch | 205 +++++++++++++++++++++ .../glibc/glibc/CVE-2017-100366_4.patch | 74 ++++++++ meta/recipes-core/glibc/glibc_2.22.bb | 4 + 5 files changed, 425 insertions(+) create mode 100644 meta/recipes-core/glibc/glibc/CVE-2017-100366.patch create mode 100644 meta/recipes-core/glibc/glibc/CVE-2017-100366_2.patch create mode 100644 meta/recipes-core/glibc/glibc/CVE-2017-100366_3.patch create mode 100644 meta/recipes-core/glibc/glibc/CVE-2017-100366_4.patch diff --git a/meta/recipes-core/glibc/glibc/CVE-2017-100366.patch b/meta/recipes-core/glibc/glibc/CVE-2017-100366.patch new file mode 100644 index 0000000..fb34e02 --- /dev/null +++ b/meta/recipes-core/glibc/glibc/CVE-2017-100366.patch @@ -0,0 +1,32 @@ +From 7b6ec7ab0ad3224add0196998af26d003400dd06 Mon Sep 17 00:00:00 2001 +From: Florian Weimer +Date: Sun, 28 May 2017 20:37:40 +0200 +Subject: rtld: Completely ignore LD_LIBRARY_PATH for AT_SECURE=1 programs + +LD_LIBRARY_PATH can only be used to reorder system search paths, which +is not useful functionality. + +Upstream-Status: Pending +CVE: CVE-2017-100366 +Signed-off-by: Yuanjie Huang +--- + elf/rtld.c | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/elf/rtld.c b/elf/rtld.c +index 83e4b0e716..474576dcd4 100644 +--- a/elf/rtld.c ++++ b/elf/rtld.c +@@ -2443,7 +2443,8 @@ process_envvars (enum mode *modep) + + case 12: + /* The library search path. */ +- if (memcmp (envline, "LIBRARY_PATH", 12) == 0) ++ if (!__libc_enable_secure ++ && memcmp (envline, "LIBRARY_PATH", 12) == 0) + { + library_path = &envline[13]; + break; +-- +2.11.0 + diff --git a/meta/recipes-core/glibc/glibc/CVE-2017-100366_2.patch b/meta/recipes-core/glibc/glibc/CVE-2017-100366_2.patch new file mode 100644 index 0000000..304392a --- /dev/null +++ b/meta/recipes-core/glibc/glibc/CVE-2017-100366_2.patch @@ -0,0 +1,110 @@ +From d5c8cfad526874da933ebbf09c2c21d608de53fb Mon Sep 17 00:00:00 2001 +From: Florian Weimer +Date: Sun, 28 May 2017 20:57:40 +0200 +Subject: rtld: Reject overly long LD_PRELOAD path elements + +Upstream-Status: Pending +CVE: CVE-2017-100366 +Signed-off-by: Yuanjie Huang +--- + elf/rtld.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++--------------- + 1 file changed, 53 insertions(+), 16 deletions(-) + +diff --git a/elf/rtld.c b/elf/rtld.c +index 474576dcd4..77656b26c6 100644 +--- a/elf/rtld.c ++++ b/elf/rtld.c +@@ -100,6 +100,22 @@ uintptr_t __pointer_chk_guard_local + strong_alias (__pointer_chk_guard_local, __pointer_chk_guard) + #endif + ++/* Check that AT_SECURE=0, or that the passed name does not contain ++ directories and is not overly long. Reject empty names ++ unconditionally. */ ++static bool ++dso_name_valid_for_suid (const char *p) ++{ ++ if (__glibc_unlikely (__libc_enable_secure)) ++ { ++ /* Ignore pathnames with directories for AT_SECURE=1 ++ programs, and also skip overlong names. */ ++ size_t len = strlen (p); ++ if (len >= NAME_MAX || memchr (p, '/', len) != NULL) ++ return false; ++ } ++ return *p != '\0'; ++} + + /* List of auditing DSOs. */ + static struct audit_list +@@ -731,6 +747,42 @@ static const char *preloadlist attribute_relro; + /* Nonzero if information about versions has to be printed. */ + static int version_info attribute_relro; + ++/* The LD_PRELOAD environment variable gives list of libraries ++ separated by white space or colons that are loaded before the ++ executable's dependencies and prepended to the global scope list. ++ (If the binary is running setuid all elements containing a '/' are ++ ignored since it is insecure.) Return the number of preloads ++ performed. */ ++unsigned int ++handle_ld_preload (const char *preloadlist, struct link_map *main_map) ++{ ++ unsigned int npreloads = 0; ++ const char *p = preloadlist; ++ char fname[PATH_MAX]; ++ ++ while (*p != '\0') ++ { ++ /* Split preload list at space/colon. */ ++ size_t len = strcspn (p, " :"); ++ if (len > 0 && len < PATH_MAX) ++ { ++ memcpy (fname, p, len); ++ fname[len] = '\0'; ++ } ++ else ++ fname[0] = '\0'; ++ ++ /* Skip over the substring and the following delimiter. */ ++ p += len; ++ if (*p == ' ' || *p == ':') ++ ++p; ++ ++ if (dso_name_valid_for_suid (fname)) ++ npreloads += do_preload (fname, main_map, "LD_PRELOAD"); ++ } ++ return npreloads; ++} ++ + static void + dl_main (const ElfW(Phdr) *phdr, + ElfW(Word) phnum, +@@ -1482,23 +1534,8 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n", + + if (__glibc_unlikely (preloadlist != NULL)) + { +- /* The LD_PRELOAD environment variable gives list of libraries +- separated by white space or colons that are loaded before the +- executable's dependencies and prepended to the global scope +- list. If the binary is running setuid all elements +- containing a '/' are ignored since it is insecure. */ +- char *list = strdupa (preloadlist); +- char *p; +- + HP_TIMING_NOW (start); +- +- /* Prevent optimizing strsep. Speed is not important here. */ +- while ((p = (strsep) (&list, " :")) != NULL) +- if (p[0] != '\0' +- && (__builtin_expect (! __libc_enable_secure, 1) +- || strchr (p, '/') == NULL)) +- npreloads += do_preload (p, main_map, "LD_PRELOAD"); +- ++ npreloads += handle_ld_preload (preloadlist, main_map); + HP_TIMING_NOW (stop); + HP_TIMING_DIFF (diff, start, stop); + HP_TIMING_ACCUM_NT (load_time, diff); +-- +2.11.0 + diff --git a/meta/recipes-core/glibc/glibc/CVE-2017-100366_3.patch b/meta/recipes-core/glibc/glibc/CVE-2017-100366_3.patch new file mode 100644 index 0000000..f8c9449 --- /dev/null +++ b/meta/recipes-core/glibc/glibc/CVE-2017-100366_3.patch @@ -0,0 +1,205 @@ +From b2ff9c2aaf997094ab56c2432f60bed750a9f78e Mon Sep 17 00:00:00 2001 +From: Florian Weimer +Date: Sun, 28 May 2017 20:44:52 +0200 +Subject: rtld: Reject overly long LD_AUDIT path elements + +Also only process the last LD_AUDIT entry. + +Upstream-Status: Pending +CVE: CVE-2017-100366 +Signed-off-by: Yuanjie Huang +--- + elf/rtld.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------- + 1 file changed, 95 insertions(+), 15 deletions(-) + +diff --git a/elf/rtld.c b/elf/rtld.c +index 77656b26c6..bd83e2207e 100644 +--- a/elf/rtld.c ++++ b/elf/rtld.c +@@ -117,13 +117,91 @@ dso_name_valid_for_suid (const char *p) + return *p != '\0'; + } + +-/* List of auditing DSOs. */ ++/* LD_AUDIT variable contents. Must be processed before the ++ audit_list below. */ ++const char *audit_list_string; ++ ++/* Cyclic list of auditing DSOs. audit_list->next is the first ++ element. */ + static struct audit_list + { + const char *name; + struct audit_list *next; + } *audit_list; + ++/* Iterator for audit_list_string followed by audit_list. */ ++struct audit_list_iter ++{ ++ /* Tail of audit_list_string still needing processing, or NULL. */ ++ const char *audit_list_tail; ++ ++ /* The list element returned in the previous iteration. NULL before ++ the first element. */ ++ struct audit_list *previous; ++ ++ /* Scratch buffer for returning a name which is part of ++ audit_list_string. */ ++ char fname[PATH_MAX]; ++}; ++ ++/* Initialize an audit list iterator. */ ++static void ++audit_list_iter_init (struct audit_list_iter *iter) ++{ ++ iter->audit_list_tail = audit_list_string; ++ iter->previous = NULL; ++} ++ ++/* Iterate through both audit_list_string and audit_list. */ ++static const char * ++audit_list_iter_next (struct audit_list_iter *iter) ++{ ++ if (iter->audit_list_tail != NULL) ++ { ++ /* First iterate over audit_list_string. */ ++ while (*iter->audit_list_tail != '\0') ++ { ++ /* Split audit list at colon. */ ++ size_t len = strcspn (iter->audit_list_tail, ":"); ++ if (len > 0 && len < PATH_MAX) ++ { ++ memcpy (iter->fname, iter->audit_list_tail, len); ++ iter->fname[len] = '\0'; ++ } ++ else ++ /* Do not return this name to the caller. */ ++ iter->fname[0] = '\0'; ++ ++ /* Skip over the substring and the following delimiter. */ ++ iter->audit_list_tail += len; ++ if (*iter->audit_list_tail == ':') ++ ++iter->audit_list_tail; ++ ++ /* If the name is valid, return it. */ ++ if (dso_name_valid_for_suid (iter->fname)) ++ return iter->fname; ++ /* Otherwise, wrap around and try the next name. */ ++ } ++ /* Fall through to the procesing of audit_list. */ ++ } ++ ++ if (iter->previous == NULL) ++ { ++ if (audit_list == NULL) ++ /* No pre-parsed audit list. */ ++ return NULL; ++ /* Start of audit list. The first list element is at ++ audit_list->next (cyclic list). */ ++ iter->previous = audit_list->next; ++ return iter->previous->name; ++ } ++ if (iter->previous == audit_list) ++ /* Cyclic list wrap-around. */ ++ return NULL; ++ iter->previous = iter->previous->next; ++ return iter->previous->name; ++} ++ + #ifndef HAVE_INLINED_SYSCALLS + /* Set nonzero during loading and initialization of executable and + libraries, cleared before the executable's entry point runs. This +@@ -1310,11 +1388,13 @@ of this helper program; chances are you did not intend to run this program.\n\ + GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid (); + + /* If we have auditing DSOs to load, do it now. */ +- if (__glibc_unlikely (audit_list != NULL)) ++ bool need_security_init = true; ++ if (__glibc_unlikely (audit_list != NULL) ++ || __glibc_unlikely (audit_list_string != NULL)) + { +- /* Iterate over all entries in the list. The order is important. */ + struct audit_ifaces *last_audit = NULL; +- struct audit_list *al = audit_list->next; ++ struct audit_list_iter al_iter; ++ audit_list_iter_init (&al_iter); + + /* Since we start using the auditing DSOs right away we need to + initialize the data structures now. */ +@@ -1325,9 +1405,14 @@ of this helper program; chances are you did not intend to run this program.\n\ + use different values (especially the pointer guard) and will + fail later on. */ + security_init (); ++ need_security_init = false; + +- do ++ while (true) + { ++ const char *name = audit_list_iter_next (&al_iter); ++ if (name == NULL) ++ break; ++ + int tls_idx = GL(dl_tls_max_dtv_idx); + + /* Now it is time to determine the layout of the static TLS +@@ -1336,7 +1421,7 @@ of this helper program; chances are you did not intend to run this program.\n\ + no DF_STATIC_TLS bit is set. The reason is that we know + glibc will use the static model. */ + struct dlmopen_args dlmargs; +- dlmargs.fname = al->name; ++ dlmargs.fname = name; + dlmargs.map = NULL; + + const char *objname; +@@ -1349,7 +1434,7 @@ of this helper program; chances are you did not intend to run this program.\n\ + not_loaded: + _dl_error_printf ("\ + ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n", +- al->name, err_str); ++ name, err_str); + if (malloced) + free ((char *) err_str); + } +@@ -1453,10 +1538,7 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n", + goto not_loaded; + } + } +- +- al = al->next; + } +- while (al != audit_list->next); + + /* If we have any auditing modules, announce that we already + have two objects loaded. */ +@@ -1720,7 +1802,7 @@ ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n", + if (tcbp == NULL) + tcbp = init_tls (); + +- if (__glibc_likely (audit_list == NULL)) ++ if (__glibc_likely (need_security_init)) + /* Initialize security features. But only if we have not done it + earlier. */ + security_init (); +@@ -2353,9 +2435,7 @@ process_dl_audit (char *str) + char *p; + + while ((p = (strsep) (&str, ":")) != NULL) +- if (p[0] != '\0' +- && (__builtin_expect (! __libc_enable_secure, 1) +- || strchr (p, '/') == NULL)) ++ if (dso_name_valid_for_suid (p)) + { + /* This is using the local malloc, not the system malloc. The + memory can never be freed. */ +@@ -2422,7 +2502,7 @@ process_envvars (enum mode *modep) + } + #endif + if (memcmp (envline, "AUDIT", 5) == 0) +- process_dl_audit (&envline[6]); ++ audit_list_string = &envline[6]; + break; + + case 7: +-- +2.11.0 + diff --git a/meta/recipes-core/glibc/glibc/CVE-2017-100366_4.patch b/meta/recipes-core/glibc/glibc/CVE-2017-100366_4.patch new file mode 100644 index 0000000..801ba39 --- /dev/null +++ b/meta/recipes-core/glibc/glibc/CVE-2017-100366_4.patch @@ -0,0 +1,74 @@ +From cccbfda1963ed04abd47ac0f6056920f13d0b341 Mon Sep 17 00:00:00 2001 +From: Siddhesh Poyarekar +Date: Tue, 7 Mar 2017 20:52:04 +0530 +Subject: Ignore and remove LD_HWCAP_MASK for AT_SECURE programs (bug #21209) + +The LD_HWCAP_MASK environment variable may alter the selection of +function variants for some architectures. For AT_SECURE process it +means that if an outdated routine has a bug that would otherwise not +affect newer platforms by default, LD_HWCAP_MASK will allow that bug +to be exploited. + +To be on the safe side, ignore and disable LD_HWCAP_MASK for setuid +binaries. + + [BZ #21209] + * elf/rtld.c (process_envvars): Ignore LD_HWCAP_MASK for + AT_SECURE processes. + * sysdeps/generic/unsecvars.h: Add LD_HWCAP_MASK. + +(cherry picked from commit 1c1243b6fc33c029488add276e56570a07803bfd) + +Upstream-Status: Backport[master] +CVE: CVE-2017-100366 +Signed-off-by: Yuanjie Huang +--- + ChangeLog | 7 +++++++ + elf/rtld.c | 3 ++- + sysdeps/generic/unsecvars.h | 1 + + 3 files changed, 10 insertions(+), 1 deletion(-) + +diff --git a/ChangeLog b/ChangeLog +index c20fdd2b09..66348258e4 100644 +--- a/ChangeLog ++++ b/ChangeLog +@@ -1,3 +1,10 @@ ++2017-03-07 Siddhesh Poyarekar ++ ++ [BZ #21209] ++ * elf/rtld.c (process_envvars): Ignore LD_HWCAP_MASK for ++ AT_SECURE processes. ++ * sysdeps/generic/unsecvars.h: Add LD_HWCAP_MASK. ++ + 2016-03-15 Andreas Schwab + + [BZ #19257] +diff --git a/elf/rtld.c b/elf/rtld.c +index bd83e2207e..9599b56820 100644 +--- a/elf/rtld.c ++++ b/elf/rtld.c +@@ -2546,7 +2546,8 @@ process_envvars (enum mode *modep) + + case 10: + /* Mask for the important hardware capabilities. */ +- if (memcmp (envline, "HWCAP_MASK", 10) == 0) ++ if (!__libc_enable_secure ++ && memcmp (envline, "HWCAP_MASK", 10) == 0) + GLRO(dl_hwcap_mask) = __strtoul_internal (&envline[11], NULL, + 0, 0); + break; +diff --git a/sysdeps/generic/unsecvars.h b/sysdeps/generic/unsecvars.h +index d5b8119c9c..358dfb31dd 100644 +--- a/sysdeps/generic/unsecvars.h ++++ b/sysdeps/generic/unsecvars.h +@@ -9,6 +9,7 @@ + "LD_DEBUG\0" \ + "LD_DEBUG_OUTPUT\0" \ + "LD_DYNAMIC_WEAK\0" \ ++ "LD_HWCAP_MASK\0" \ + "LD_LIBRARY_PATH\0" \ + "LD_ORIGIN_PATH\0" \ + "LD_PRELOAD\0" \ +-- +2.11.0 + diff --git a/meta/recipes-core/glibc/glibc_2.22.bb b/meta/recipes-core/glibc/glibc_2.22.bb index 36da7c4..ad2f95b 100644 --- a/meta/recipes-core/glibc/glibc_2.22.bb +++ b/meta/recipes-core/glibc/glibc_2.22.bb @@ -62,6 +62,10 @@ SRC_URI = "${GLIBC_GIT_URI};branch=${SRCBRANCH};name=glibc \ file://0034-Bug-20116-Fix-use-after-free-in-pthread_create.patch \ file://CVE-2016-5417.patch \ file://0001-Fix-nscd-assertion-failure-in-gc-bug-19755.patch \ + file://CVE-2017-100366.patch \ + file://CVE-2017-100366_2.patch \ + file://CVE-2017-100366_3.patch \ + file://CVE-2017-100366_4.patch \ " SRC_URI += "\ -- 1.9.1