From patchwork Thu Mar 16 19:41:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 70961 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp681775wrt; Thu, 16 Mar 2023 13:12:05 -0700 (PDT) X-Google-Smtp-Source: AK7set8msBX1XO7MdZ/Bk522aS2VlliwtU99f25XbbsqwDBKmi3k8jv81d2TiNpmlVvhNPdcBKbi X-Received: by 2002:a17:903:22d2:b0:19e:8452:1944 with SMTP id y18-20020a17090322d200b0019e84521944mr5928450plg.5.1678997525604; Thu, 16 Mar 2023 13:12:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678997525; cv=none; d=google.com; s=arc-20160816; b=jfoO3K/YKyndbQG4gFJASRAkqGmyixN1aOf9fQMJ+8xf+CHH8R/pwrjHjBj1rwYHxk 6aTaF1yD1F3cc7UT86c5VK47qnyly9Uq8l/cvvOSHT6ur2eY01NanQb/suy4IL9UN9jR OMO38TATAOVMCyv1AR0+d4r2lQT4Feo4VEWEf+YBqNfREt8NVfq1QhZDVq/DgDwOJfp7 I4kjmfHISrt8jbsx0hEhkR9CLMyEiXMqjhbYUEX9yOwQzWzvPVajzTRpxjZsKVbNrtcK SZMFv2pHLxOIecoF5AEfz4qOLMTuBgEBhkdwjiTKjOTRPGPoYklTQeeWY1kyE9X3vYtg Ne6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=bVjcBy+hTIksLzJT1oEhkBeJIebyZAc+YCZpa2hhl+o=; b=vH0NifYTCrwoV9qeW7PLECU4IlKMMgmaGkU/Ai3cUTHM3QVXzMYecCmonXfFHvGwuF PqVVxzHHbCoEZ8jQ52eaO/eFW7944IkmNBzhc4w5+SOlrWfawDhM+1PE1jvuNZHIz47s IYkdx3GzkpTgYq/srd93ZQ4e9j6LtPyneO4CYNq2BXSaHfX9x7P6YOch5pxzo7OHvGjX edC0JIjHxmXr3mnEqSTXcaFdGwec/6y5m2KeLpOa+55L/ndoIYOLKWGcJ8E8h6UFH1ck n/LcBvK6aLMRIWSrHHxU3lrdTzh+W2a1f58c41d6uHcX/O0psLEeI1UKuAmrLSpc4ERg zfyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZVai50OV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ay6-20020a1709028b8600b001a0561c726dsi267121plb.264.2023.03.16.13.11.52; Thu, 16 Mar 2023 13:12:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZVai50OV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230211AbjCPTmS (ORCPT + 99 others); Thu, 16 Mar 2023 15:42:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229708AbjCPTmP (ORCPT ); Thu, 16 Mar 2023 15:42:15 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8296E442CB; Thu, 16 Mar 2023 12:42:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678995734; x=1710531734; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eIZyYOcJoDlch2zoEcNQZVhXa/4V+wzyFtHKXdVX4jU=; b=ZVai50OVrZ/OOMm+iJC3qqv6EpZsNG//b23JljgSC7dhH3Ge8M0Q1lfH 6sILO94ZEox0Z4R+ya5mU4IcA8snIdt+CoA70+FBG2vgd1IFFZPwM2GCP Wg/bGCdWDaW1GGeLGBtuxZzgSyBULOlq8iTIIsg3PWBw3HbSBSYcvslPj 9VngzW52hDt0dEpOz8yGBNqGTmd0h5GxH9wz+EuS9iEFLg8ffkMVtrGNs uuiQ/KjNqSv1rCiMZNO3JYDbSdMqirPdxcvZYUqB6PUQFFIRUprOe8O1U gqWeMOhCwieiGGWXRH+WOGEEqhR0WeKWIH4b6sPF8jKEC3n3PmeOtpHhl w==; X-IronPort-AV: E=McAfee;i="6600,9927,10651"; a="326465402" X-IronPort-AV: E=Sophos;i="5.98,265,1673942400"; d="scan'208";a="326465402" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Mar 2023 12:42:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10651"; a="823392391" X-IronPort-AV: E=Sophos;i="5.98,265,1673942400"; d="scan'208";a="823392391" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.251.221.172]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Mar 2023 12:42:12 -0700 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Ian Rogers , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH 1/3] perf symbols: Fix use-after-free in get_plt_got_name() Date: Thu, 16 Mar 2023 21:41:54 +0200 Message-Id: <20230316194156.8320-2-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230316194156.8320-1-adrian.hunter@intel.com> References: <20230316194156.8320-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760556509795025974?= X-GMAIL-MSGID: =?utf-8?q?1760556509795025974?= Fix use-after-free in get_plt_got_name(). Discovered using EXTRA_CFLAGS="-fsanitize=undefined -fsanitize=address". Reported-by: kernel test robot Link: https://lore.kernel.org/oe-lkp/202303061424.6ad43294-yujie.liu@intel.com Fixes: ce4c8e7966f3 ("perf symbols: Get symbols for .plt.got for x86-64") Signed-off-by: Adrian Hunter --- tools/perf/util/symbol-elf.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index c0a2de42c51b..7ef5f6d7d415 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -557,6 +557,7 @@ static bool get_plt_got_name(GElf_Shdr *shdr, size_t i, const char *sym_name; char *demangled; GElf_Sym sym; + bool result; u32 disp; if (!di->sorted) @@ -583,9 +584,11 @@ static bool get_plt_got_name(GElf_Shdr *shdr, size_t i, snprintf(buf, buf_sz, "%s@plt", sym_name); + result = *sym_name; + free(demangled); - return *sym_name; + return result; } static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf, From patchwork Thu Mar 16 19:41:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 70955 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp674209wrt; Thu, 16 Mar 2023 12:56:08 -0700 (PDT) X-Google-Smtp-Source: AK7set/xiaUdPowkDuLJjoMypOFpbruMwJmZwmBlpar9RFZykLBH9zJLPLTwOowz7kOmAJhjvxzx X-Received: by 2002:a62:1749:0:b0:625:9426:d60b with SMTP id 70-20020a621749000000b006259426d60bmr4434406pfx.15.1678996568678; Thu, 16 Mar 2023 12:56:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678996568; cv=none; d=google.com; s=arc-20160816; b=h+IAZ7oDKrAHO2v3eyWpt5mpPaHwWvmW/fvvN6dn1dL0aKuWf2biZBSqOSK4m5PeRT 4gXT95RlJubaHPCeUh0G7lgQbmqDBswV4GJsIX6LFCh+/ZIVVikEwFETnnIEh2UKk/zC BX9GCqQ9BUKANl1E6pbu07LujEmFoU01nFm5/y0ooF10B3aZhoU1+xxFJpmh3eNQuNhD 5QqtudVlRIHSkY+rW6zWGOsVszzvd/TQneNHIRLeWInqALJYNDF2WAeaY8YDu/+d1mO6 0YAXT6gJWSuJxoZyDPdlNTM6MxuoLIJacqji6eZnDp5jS6OR9Q96kNiC/qcworDRlXFz 2spw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=nUD+ZGYrRbDW1z2HEEm4V97hi9yje2DoL7z+f5bhaPY=; b=RyyDhHkiaqYfbGDYw+iWUJ0xouhQsKd83Y3exRsTbCdxeVjHz64kcn2YGKWyjJgR9P A0NiAptj5VFzhAoZyka040eB72Aq56G8+PuCen+J1yHHPT+C0hZgxnuhcq/nX5cconno HRjO7jEGApV+neLVcT+Sz86fdd85trK2lxDD2hIZa+aahSf/RAjvGCrexF/W4oX97kYO inf1EBpgnUPfUfFr6vMlVzEZ0CnlqLbHEb8ZCjGHZVfwfFVwQhdnPw3NONPXAvcJt/kC Q2GQ/3OnkcsFv0XUp5K/VRhtIAiIjrXnVuly+1rFQiINijVET2FF0qzSm0CgHAzTX3PX 7gxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ji6AjRdV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b29-20020a631b1d000000b00507249cdbb4si119362pgb.296.2023.03.16.12.55.55; Thu, 16 Mar 2023 12:56:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ji6AjRdV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229799AbjCPTmW (ORCPT + 99 others); Thu, 16 Mar 2023 15:42:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230255AbjCPTmR (ORCPT ); Thu, 16 Mar 2023 15:42:17 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6536974B1; Thu, 16 Mar 2023 12:42:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678995737; x=1710531737; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HNjn4+Xoz1pdCG0Q0z/t1kpbDRFkSjxY3sgAwJC83HU=; b=ji6AjRdV9ev5qZdK66A+pYsvKUyE0ftSEVE05t67icFF/ywOe0zE+/MT k/+f7t5WskF9y79XbnjxYvdpZPyYhLazD0WMQw+yUhOmmbwIk2v+2rHXl 34ZyDy3ZMeuMePEDRaDiXldInKTxMq/x0YhypvuB1Qo5F3yIK9ugNOX84 4qK23KT5GD3EphAEmILmtKPJtRYngVhd8zdtV360/xZ6CVuUJL440jEQm PbS1tUxFiO/ZHqpOXgi+A3cSL7YynvPqJkB1kl3VmRcFB0Kds/mHXfOZP RrZjEkit1Pz3qa4KiNW7Su8czPAEtZge8qmT3yiMFtR70cl9V06yNC26J g==; X-IronPort-AV: E=McAfee;i="6600,9927,10651"; a="326465414" X-IronPort-AV: E=Sophos;i="5.98,265,1673942400"; d="scan'208";a="326465414" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Mar 2023 12:42:16 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10651"; a="823392397" X-IronPort-AV: E=Sophos;i="5.98,265,1673942400"; d="scan'208";a="823392397" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.251.221.172]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Mar 2023 12:42:14 -0700 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Ian Rogers , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH 2/3] perf symbols: Fix unaligned access in get_x86_64_plt_disp() Date: Thu, 16 Mar 2023 21:41:55 +0200 Message-Id: <20230316194156.8320-3-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230316194156.8320-1-adrian.hunter@intel.com> References: <20230316194156.8320-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760555506220819416?= X-GMAIL-MSGID: =?utf-8?q?1760555506220819416?= Use memcpy() to avoid unaligned access. Discovered using EXTRA_CFLAGS="-fsanitize=undefined -fsanitize=address". Reported-by: kernel test robot Link: https://lore.kernel.org/oe-lkp/202303061424.6ad43294-yujie.liu@intel.com Fixes: ce4c8e7966f3 ("perf symbols: Get symbols for .plt.got for x86-64") Signed-off-by: Adrian Hunter --- tools/perf/util/symbol-elf.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 7ef5f6d7d415..ae810d4cf3cd 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -542,9 +542,12 @@ static u32 get_x86_64_plt_disp(const u8 *p) n += 1; /* jmp with 4-byte displacement */ if (p[n] == 0xff && p[n + 1] == 0x25) { + u32 disp; + n += 2; /* Also add offset from start of entry to end of instruction */ - return n + 4 + le32toh(*(const u32 *)(p + n)); + memcpy(&disp, p + n, sizeof(disp)); + return n + 4 + le32toh(disp); } return 0; } From patchwork Thu Mar 16 19:41:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 70962 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp686192wrt; Thu, 16 Mar 2023 13:22:11 -0700 (PDT) X-Google-Smtp-Source: AK7set/ZiZu6c7unEubmNpoXPsdLwU9BF1JLT9KiycWOOzFW8ZTfCrzY3zRA/uyclTukxP44x4Ya X-Received: by 2002:a05:6a20:bb23:b0:d7:34a1:859a with SMTP id fc35-20020a056a20bb2300b000d734a1859amr1861043pzb.27.1678998131090; Thu, 16 Mar 2023 13:22:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678998131; cv=none; d=google.com; s=arc-20160816; b=bhxvoxamFviC7IjoHjvXMCSXPFq3+H2fkN5loLPMXX7rTlwkySFdHq+fXYbXVhiXAe bnU/Hs1Z8QUnIoEz0oSNAkcsdWSw8PY0VY7amz+/aURot/LQzbjKPgah+bbMk4s9FEO2 ssoWq3cmV4Dq8IoYf+PT7Boa/WPAmbfaVdK+enwaGF1sULK6ND4fCEcAih9LQjo/G1DC +cE3SSPVAn4Q5qLYLrhCeH0VsJJ4hYguaqfw2Q2gUO/XNnM+pQTpVfLdPKxDqa6xpQIr zhsuNLUY7mJX9pgmVosDyFc8QPbsoH5X9FR7tgaUVZkpMaDdPh07dTlYtQg8WIEjWotY xS8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=aEeSYGkAZbql3fMjhBZuybWeYuDR+pUlyIMbw1vqV9c=; b=g4fRe9Rcz8a9ChkjFXfv25HWjYrIywrDC5NUeQ+uZ9LV6m9tcMSy53JjOGjBw7ynYx 4a8xQXJl+B8B6Uh+8Q/ZzwSUBMphYVjMFAjfBtPOdvRLVeiy0CEVkG6FH628YFpKRw+T YBpnDMY7IsLYcIl6GtnVzEzkzTAchu1FmjCSAHgfNfqE43eMBklZn/Jb53JUhSAu7OS6 F7rLE+6XprQZgQPyaf8DWI/EtlEw7vAKW33jAmA5RMkBNOVoeV6/LVYPER25/MsvS+wD lpn+nYz4YpvWkDdcyJiaaDGQQ3taauW5dUDddwFwiK3x5tYdhxH5NIWLfqGdXnGrnuE5 Uw/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=V0az4KmS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t22-20020a632256000000b00502e3fcec97si129610pgm.739.2023.03.16.13.21.56; Thu, 16 Mar 2023 13:22:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=V0az4KmS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230367AbjCPTmZ (ORCPT + 99 others); Thu, 16 Mar 2023 15:42:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230200AbjCPTmX (ORCPT ); Thu, 16 Mar 2023 15:42:23 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DD2CE5008; Thu, 16 Mar 2023 12:42:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678995739; x=1710531739; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Uj6dg8HajoHWiLlcjfZ1cC5m+o7lfOdaDPvUfS+JTUc=; b=V0az4KmSNpQhRsXgK7F4AjZ1dyvrBdTMCIsXSUnFhoxpvn1w6T4RjfcO piGmFQ1WfDFSJiMFjyvg+GwFAi2sIhh37IgZVoBluYEpfy5SO/O8DRFdc s14Vaa+y062VyBLZJTz+M2u8r+AyzEaiUMHVXeUwF37OyHrlqEXn5JLBK 1pK/K+gpojAtjtr8ywu3K3+9X7xY5+4SfWs/tmUhJgIpTpoLBBCT/se4P ZhTk1MuJ/XuaeMShPIuAHRYaGa/23ubdmOz03PkPvCEazMdTlu3C0JDl0 TOYoexKdJNmg9Jy0We2Sjy6cRQw5+TLU8Oo1WGpmB3o8EHcKy/IjBW+Rx A==; X-IronPort-AV: E=McAfee;i="6600,9927,10651"; a="326465427" X-IronPort-AV: E=Sophos;i="5.98,265,1673942400"; d="scan'208";a="326465427" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Mar 2023 12:42:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10651"; a="823392406" X-IronPort-AV: E=Sophos;i="5.98,265,1673942400"; d="scan'208";a="823392406" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.251.221.172]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Mar 2023 12:42:16 -0700 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Ian Rogers , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH 3/3] perf tools: Avoid warning in do_realloc_array_as_needed() Date: Thu, 16 Mar 2023 21:41:56 +0200 Message-Id: <20230316194156.8320-4-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230316194156.8320-1-adrian.hunter@intel.com> References: <20230316194156.8320-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760557144269267266?= X-GMAIL-MSGID: =?utf-8?q?1760557144269267266?= do_realloc_array_as_needed() used memcpy() of zero size with a NULL pointer. Check the size first to avoid sanitize warning. Discovered using EXTRA_CFLAGS="-fsanitize=undefined -fsanitize=address". Reported-by: kernel test robot Link: https://lore.kernel.org/oe-lkp/202303061424.6ad43294-yujie.liu@intel.com Signed-off-by: Adrian Hunter --- tools/perf/util/util.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index b356c9f7f0c3..089208b51e68 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c @@ -524,7 +524,8 @@ int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x, size_t msz, new_arr = calloc(new_sz, msz); if (!new_arr) return -ENOMEM; - memcpy(new_arr, *arr, *arr_sz * msz); + if (*arr_sz) + memcpy(new_arr, *arr, *arr_sz * msz); if (init_val) { for (i = *arr_sz; i < new_sz; i++) memcpy(new_arr + (i * msz), init_val, msz);