From 11ac0fc26ac23e16776d84b8d27eee593b527f8f Mon Sep 17 00:00:00 2001 From: kib Date: Thu, 6 Apr 2017 15:19:18 +0000 Subject: [PATCH] Improvements for the brand detection and prioritization. MFC r315701 (by ed): Set the interpreter path to /nonexistent. MFC r315749: Adjust r314851 to not require every brand to specify interpreter path. MFC r315753: Add a flag BI_BRAND_ONLY_STATIC to specify that the brand only matches static binaries. MFC r315754: Update r315753 with the proper flag name. MFC r316211: A followup to r315749, two more places where brand->interp_path was accessed unconditionally. --- sys/amd64/cloudabi32/cloudabi32_sysvec.c | 1 + sys/amd64/cloudabi64/cloudabi64_sysvec.c | 2 +- sys/arm/cloudabi32/cloudabi32_sysvec.c | 1 + sys/arm64/cloudabi64/cloudabi64_sysvec.c | 2 +- sys/i386/cloudabi32/cloudabi32_sysvec.c | 1 + sys/kern/imgact_elf.c | 27 ++++++++++++++++++------ sys/sys/imgact_elf.h | 1 + 7 files changed, 26 insertions(+), 9 deletions(-) diff --git a/sys/amd64/cloudabi32/cloudabi32_sysvec.c b/sys/amd64/cloudabi32/cloudabi32_sysvec.c index fa2f9ae6e7b..abede1ac4ce 100644 --- a/sys/amd64/cloudabi32/cloudabi32_sysvec.c +++ b/sys/amd64/cloudabi32/cloudabi32_sysvec.c @@ -227,4 +227,5 @@ Elf32_Brandinfo cloudabi32_brand = { .brand = ELFOSABI_CLOUDABI, .machine = EM_386, .sysvec = &cloudabi32_elf_sysvec, + .flags = BI_BRAND_ONLY_STATIC, }; diff --git a/sys/amd64/cloudabi64/cloudabi64_sysvec.c b/sys/amd64/cloudabi64/cloudabi64_sysvec.c index 3a21ff3600a..84f0cb387de 100644 --- a/sys/amd64/cloudabi64/cloudabi64_sysvec.c +++ b/sys/amd64/cloudabi64/cloudabi64_sysvec.c @@ -212,5 +212,5 @@ Elf64_Brandinfo cloudabi64_brand = { .brand = ELFOSABI_CLOUDABI, .machine = EM_X86_64, .sysvec = &cloudabi64_elf_sysvec, - .flags = BI_CAN_EXEC_DYN, + .flags = BI_CAN_EXEC_DYN | BI_BRAND_ONLY_STATIC, }; diff --git a/sys/arm/cloudabi32/cloudabi32_sysvec.c b/sys/arm/cloudabi32/cloudabi32_sysvec.c index f4e231b0ddc..100fa479acd 100644 --- a/sys/arm/cloudabi32/cloudabi32_sysvec.c +++ b/sys/arm/cloudabi32/cloudabi32_sysvec.c @@ -189,4 +189,5 @@ Elf32_Brandinfo cloudabi32_brand = { .brand = ELFOSABI_CLOUDABI, .machine = EM_ARM, .sysvec = &cloudabi32_elf_sysvec, + .flags = BI_BRAND_ONLY_STATIC, }; diff --git a/sys/arm64/cloudabi64/cloudabi64_sysvec.c b/sys/arm64/cloudabi64/cloudabi64_sysvec.c index fc786de2078..fbad0ba70db 100644 --- a/sys/arm64/cloudabi64/cloudabi64_sysvec.c +++ b/sys/arm64/cloudabi64/cloudabi64_sysvec.c @@ -181,5 +181,5 @@ Elf64_Brandinfo cloudabi64_brand = { .brand = ELFOSABI_CLOUDABI, .machine = EM_AARCH64, .sysvec = &cloudabi64_elf_sysvec, - .flags = BI_CAN_EXEC_DYN, + .flags = BI_CAN_EXEC_DYN | BI_BRAND_ONLY_STATIC, }; diff --git a/sys/i386/cloudabi32/cloudabi32_sysvec.c b/sys/i386/cloudabi32/cloudabi32_sysvec.c index 45b9e5c5b29..c2a21fa9046 100644 --- a/sys/i386/cloudabi32/cloudabi32_sysvec.c +++ b/sys/i386/cloudabi32/cloudabi32_sysvec.c @@ -200,4 +200,5 @@ Elf32_Brandinfo cloudabi32_brand = { .brand = ELFOSABI_CLOUDABI, .machine = EM_386, .sysvec = &cloudabi32_elf_sysvec, + .flags = BI_BRAND_ONLY_STATIC, }; diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c index 33a31c01879..491b9ddb7b0 100644 --- a/sys/kern/imgact_elf.c +++ b/sys/kern/imgact_elf.c @@ -273,6 +273,8 @@ __elfN(get_brandinfo)(struct image_params *imgp, const char *interp, bi = elf_brand_list[i]; if (bi == NULL) continue; + if (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0) + continue; if (hdr->e_machine == bi->machine && (bi->flags & (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) { ret = __elfN(check_note)(imgp, bi->brand_note, osrel); @@ -289,9 +291,11 @@ __elfN(get_brandinfo)(struct image_params *imgp, const char *interp, * this, we return first brand which accepted * our note and, optionally, header. */ - if (ret && bi_m == NULL && (strlen(bi->interp_path) + - 1 != interp_name_len || strncmp(interp, - bi->interp_path, interp_name_len) != 0)) { + if (ret && bi_m == NULL && interp != NULL && + (bi->interp_path == NULL || + (strlen(bi->interp_path) + 1 != interp_name_len || + strncmp(interp, bi->interp_path, interp_name_len) + != 0))) { bi_m = bi; ret = 0; } @@ -305,7 +309,8 @@ __elfN(get_brandinfo)(struct image_params *imgp, const char *interp, /* If the executable has a brand, search for it in the brand list. */ for (i = 0; i < MAX_BRANDS; i++) { bi = elf_brand_list[i]; - if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) + if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 || + (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0)) continue; if (hdr->e_machine == bi->machine && (hdr->e_ident[EI_OSABI] == bi->brand || @@ -319,7 +324,11 @@ __elfN(get_brandinfo)(struct image_params *imgp, const char *interp, * Again, prefer strictly matching * interpreter path. */ - if (strlen(bi->interp_path) + 1 == + if (interp_name_len == 0 && + bi->interp_path == NULL) + return (bi); + if (bi->interp_path != NULL && + strlen(bi->interp_path) + 1 == interp_name_len && strncmp(interp, bi->interp_path, interp_name_len) == 0) return (bi); @@ -348,9 +357,12 @@ __elfN(get_brandinfo)(struct image_params *imgp, const char *interp, if (interp != NULL) { for (i = 0; i < MAX_BRANDS; i++) { bi = elf_brand_list[i]; - if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) + if (bi == NULL || (bi->flags & + (BI_BRAND_NOTE_MANDATORY | BI_BRAND_ONLY_STATIC)) + != 0) continue; if (hdr->e_machine == bi->machine && + bi->interp_path != NULL && /* ELF image p_filesz includes terminating zero */ strlen(bi->interp_path) + 1 == interp_name_len && strncmp(interp, bi->interp_path, interp_name_len) @@ -362,7 +374,8 @@ __elfN(get_brandinfo)(struct image_params *imgp, const char *interp, /* Lacking a recognized interpreter, try the default brand */ for (i = 0; i < MAX_BRANDS; i++) { bi = elf_brand_list[i]; - if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY) + if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 || + (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0)) continue; if (hdr->e_machine == bi->machine && __elfN(fallback_brand) == bi->brand) diff --git a/sys/sys/imgact_elf.h b/sys/sys/imgact_elf.h index 0ec04fc1d5d..40ad7c72ef3 100644 --- a/sys/sys/imgact_elf.h +++ b/sys/sys/imgact_elf.h @@ -81,6 +81,7 @@ typedef struct { #define BI_CAN_EXEC_DYN 0x0001 #define BI_BRAND_NOTE 0x0002 /* May have note.ABI-tag section. */ #define BI_BRAND_NOTE_MANDATORY 0x0004 /* Must have note.ABI-tag section. */ +#define BI_BRAND_ONLY_STATIC 0x0008 /* Match only interp-less binaries. */ } __ElfN(Brandinfo); __ElfType(Auxargs); -- 2.45.0