Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4168644pxj; Mon, 21 Jun 2021 15:24:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzW2oRmEhlPEFsQktMQPaDs390D0YyfCYcMrcPUqrUijAdTMpmu/1DeWWa5IHDmnRyHlp1B X-Received: by 2002:a02:8246:: with SMTP id q6mr654927jag.130.1624314280750; Mon, 21 Jun 2021 15:24:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624314280; cv=none; d=google.com; s=arc-20160816; b=WABU9l7MYR+7SknZ8Xxf8rSi4JXmOvH0g2BL9rzIFIZ7U2pYoxSPnSaoJFPUDCk5hC HSEDIgBgaotKM1hMoZvpEBdZvfaMdV/qEtlYE4wCxX0FxRVDXZxCss8XEB1rseTiagr+ vuKTaG0XG0jqN2wZYUgziqZk3nyoZJO+aMQaWSm4qCML4qvGOuv+QfEJi9Vm08YhnFet vkZX9PqEv9fyEmLCeWgZWX0r64GgpiLCuHp1vvPglEchQP8d3johPhQiO0KVAKHDmLAx UeIO5xAyAlTXQgBhbzV53HBkqgfjKFf+b41O6m5Wzftrtb50ijxuZkdiP6G62PD6DVuR a/Cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=LYaZ+b8NJ+/BUcNW71lTDH1EznpA/+87PIfC9PBqpI0=; b=U7uNbpq1tJpskRQvV5RxP8AOZL236QoHE5a3dE052xJAD9VB+KYvF7Tf+LZChLxqcU dyYG7bRgyQO+ujfQfEO/ESHvsZQOLe47Bw+xVfdr5cBkh5k0f31aEejVzmOuECJo5RjY SiEluytE/2/SffB3s1DUeKIg0mUe1rGmdt9/HZC8EitlqnWtj4Xo8/bWkYbTsgX1IzwQ ilz01IZX9062gS2yblPDw7cnlagBZswk13QwVf0ImMoFWzWFdBingWoGQTUHaU55kR9W krZjCxqgdpgVzaR9bcxcVRm3LW2Gt+NCU7Eys36p59b6RohIrZDPq2GSY9LuWEdRssCz MRsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=AYpCBItp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y11si13377784jai.99.2021.06.21.15.24.28; Mon, 21 Jun 2021 15:24:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=AYpCBItp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231826AbhFUWZH (ORCPT + 99 others); Mon, 21 Jun 2021 18:25:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229789AbhFUWZG (ORCPT ); Mon, 21 Jun 2021 18:25:06 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AEB47C061574; Mon, 21 Jun 2021 15:22:51 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id b3so11075582wrm.6; Mon, 21 Jun 2021 15:22:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LYaZ+b8NJ+/BUcNW71lTDH1EznpA/+87PIfC9PBqpI0=; b=AYpCBItpYllNc4vPjzGAOBkuAqXtDRQvwkaF25IVLWrmUn7GSRSuiIDRKZFRzBiL8N xnlcEYU6tkpoJFMMqczrxkHHWI3zEMG2D4IfyQWeahNU3RWp8swl+26C6rI+bsHaBrA4 Ue+qnSZcnvTNL87Dhk8OlKDaoVi67U7foJNpX18bYw214/3VdYc9shbf23Nj0hele5ak 8WNxTnZbvxL58UAqqIchR3Pp7DqOS+HZlQaj9nUQtuZMfzK7HM2TQbl4nrPZGX4JcP3W nJGIc5d1VzwLdWV2IQboTH6ye7v9CS2vrk+6pKVhxp2Ze5BuiKZ6FvM5q4ilHcWXHOX0 TLrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LYaZ+b8NJ+/BUcNW71lTDH1EznpA/+87PIfC9PBqpI0=; b=G4W0plAUZ804iswYAVGL8R22jRzr/sHJn0lEVSSy9kebblOS384dugLHyERgCV1lYQ HNrX28bDLTNDpaY0yVwp1sVgr4sNiFJ6gZGQtiOjwcJd5KZtzzF4oASXY+UPzUncZS/y pB3/TzxJ8veCvgCxPRLyMYigMpUZbpoqbybSfsrqvj6pdr5TPzCYyXzP/qTeUH3282Xv WgrGn3jNiwwsVP3BXxjrsZbhqCPTiYkzceQQN7FT965dHXXzXKLVsZvKrQ0y03GHWgw6 vtoAoLruppfqbl4U1M9Gckw/E7QBNR0Fvy/eiCNg3zLiy5LcPBr/IZXSRTNmVO8PAPY9 pYRQ== X-Gm-Message-State: AOAM532kYQ1jU6LCs2lR2gQuCYnpkwgFzprknS+7KU8BKS7VbuxNq3VJ 6rY6E7d37RM9t72smbDE1s0= X-Received: by 2002:adf:a1c8:: with SMTP id v8mr780453wrv.300.1624314170236; Mon, 21 Jun 2021 15:22:50 -0700 (PDT) Received: from honeypot.. ([151.29.45.200]) by smtp.googlemail.com with ESMTPSA id m23sm584830wms.2.2021.06.21.15.22.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Jun 2021 15:22:49 -0700 (PDT) From: Riccardo Mancini To: Arnaldo Carvalho de Melo Cc: Namhyung Kim , Ian Rogers , Riccardo Mancini , Jiri Slaby , Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Remi Bernon , Fabian Hemmer , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] perf top: fix overflow in elf_sec__is_text Date: Tue, 22 Jun 2021 00:21:08 +0200 Message-Id: <20210621222108.196219-1-rickyman7@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ASan reports a heap-buffer-overflow in elf_sec__is_text when using perf-top. The bug is caused by the fact that secstrs is built from runtime_ss, while shdr is built from syms_ss if shdr.sh_type != SHT_NOBITS. Therefore, they point to two different ELF files. This patch renames secstrs to secstrs_run and adds secstrs_sym, so that the correct secstrs is chosen depending on shdr.sh_type. $ ASAN_OPTIONS=abort_on_error=1:disable_coredump=0:unmap_shadow_on_exit=1 ./perf top ================================================================= ==363148==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61300009add6 at pc 0x00000049875c bp 0x7f4f56446440 sp 0x7f4f56445bf0 READ of size 1 at 0x61300009add6 thread T6 #0 0x49875b in StrstrCheck(void*, char*, char const*, char const*) (/home/user/linux/tools/perf/perf+0x49875b) #1 0x4d13a2 in strstr (/home/user/linux/tools/perf/perf+0x4d13a2) #2 0xacae36 in elf_sec__is_text /home/user/linux/tools/perf/util/symbol-elf.c:176:9 #3 0xac3ec9 in elf_sec__filter /home/user/linux/tools/perf/util/symbol-elf.c:187:9 #4 0xac2c3d in dso__load_sym /home/user/linux/tools/perf/util/symbol-elf.c:1254:20 #5 0x883981 in dso__load /home/user/linux/tools/perf/util/symbol.c:1897:9 #6 0x8e6248 in map__load /home/user/linux/tools/perf/util/map.c:332:7 #7 0x8e66e5 in map__find_symbol /home/user/linux/tools/perf/util/map.c:366:6 #8 0x7f8278 in machine__resolve /home/user/linux/tools/perf/util/event.c:707:13 #9 0x5f3d1a in perf_event__process_sample /home/user/linux/tools/perf/builtin-top.c:773:6 #10 0x5f30e4 in deliver_event /home/user/linux/tools/perf/builtin-top.c:1197:3 #11 0x908a72 in do_flush /home/user/linux/tools/perf/util/ordered-events.c:244:9 #12 0x905fae in __ordered_events__flush /home/user/linux/tools/perf/util/ordered-events.c:323:8 #13 0x9058db in ordered_events__flush /home/user/linux/tools/perf/util/ordered-events.c:341:9 #14 0x5f19b1 in process_thread /home/user/linux/tools/perf/builtin-top.c:1109:7 #15 0x7f4f6a21a298 in start_thread /usr/src/debug/glibc-2.33-16.fc34.x86_64/nptl/pthread_create.c:481:8 #16 0x7f4f697d0352 in clone ../sysdeps/unix/sysv/linux/x86_64/clone.S:95 0x61300009add6 is located 10 bytes to the right of 332-byte region [0x61300009ac80,0x61300009adcc) allocated by thread T6 here: #0 0x4f3f7f in malloc (/home/user/linux/tools/perf/perf+0x4f3f7f) #1 0x7f4f6a0a88d9 (/lib64/libelf.so.1+0xa8d9) Thread T6 created by T0 here: #0 0x464856 in pthread_create (/home/user/linux/tools/perf/perf+0x464856) #1 0x5f06e0 in __cmd_top /home/user/linux/tools/perf/builtin-top.c:1309:6 #2 0x5ef19f in cmd_top /home/user/linux/tools/perf/builtin-top.c:1762:11 #3 0x7b28c0 in run_builtin /home/user/linux/tools/perf/perf.c:313:11 #4 0x7b119f in handle_internal_command /home/user/linux/tools/perf/perf.c:365:8 #5 0x7b2423 in run_argv /home/user/linux/tools/perf/perf.c:409:2 #6 0x7b0c19 in main /home/user/linux/tools/perf/perf.c:539:3 #7 0x7f4f696f7b74 in __libc_start_main /usr/src/debug/glibc-2.33-16.fc34.x86_64/csu/../csu/libc-start.c:332:16 SUMMARY: AddressSanitizer: heap-buffer-overflow (/home/user/linux/tools/perf/perf+0x49875b) in StrstrCheck(void*, char*, char const*, char const*) Shadow bytes around the buggy address: 0x0c268000b560: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c268000b570: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c268000b580: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c268000b590: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c268000b5a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x0c268000b5b0: 00 00 00 00 00 00 00 00 00 04[fa]fa fa fa fa fa 0x0c268000b5c0: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00 0x0c268000b5d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c268000b5e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c268000b5f0: 07 fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c268000b600: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==363148==ABORTING Signed-off-by: Riccardo Mancini Suggested-by: Jiri Slaby --- tools/perf/util/symbol-elf.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 4c56aa837434..7a6e38bf87b0 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -1081,7 +1081,7 @@ int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss, struct maps *kmaps = kmap ? map__kmaps(map) : NULL; struct map *curr_map = map; struct dso *curr_dso = dso; - Elf_Data *symstrs, *secstrs; + Elf_Data *symstrs, *secstrs, *secstrs_run, *secstrs_sym; uint32_t nr_syms; int err = -1; uint32_t idx; @@ -1150,8 +1150,16 @@ int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss, if (sec_strndx == NULL) goto out_elf_end; - secstrs = elf_getdata(sec_strndx, NULL); - if (secstrs == NULL) + secstrs_run = elf_getdata(sec_strndx, NULL); + if (secstrs_run == NULL) + goto out_elf_end; + + sec_strndx = elf_getscn(elf, ehdr.e_shstrndx); + if (sec_strndx == NULL) + goto out_elf_end; + + secstrs_sym = elf_getdata(sec_strndx, NULL); + if (secstrs_sym == NULL) goto out_elf_end; nr_syms = shdr.sh_size / shdr.sh_entsize; @@ -1237,6 +1245,8 @@ int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss, gelf_getshdr(sec, &shdr); + secstrs = secstrs_sym; + /* * We have to fallback to runtime when syms' section header has * NOBITS set. NOBITS results in file offset (sh_offset) not @@ -1249,6 +1259,7 @@ int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss, goto out_elf_end; gelf_getshdr(sec, &shdr); + secstrs = secstrs_run; } if (is_label && !elf_sec__filter(&shdr, secstrs)) -- 2.23.0