Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp4163098imm; Mon, 6 Aug 2018 18:33:57 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfg611zB8rBE0eoNfh7PpjrIwdFbiURkyo1+iYyyS5C0nBNVuiZF56Zdc5g++jvPW2YYg8p X-Received: by 2002:a63:f45:: with SMTP id 5-v6mr16923092pgp.447.1533605637554; Mon, 06 Aug 2018 18:33:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533605637; cv=none; d=google.com; s=arc-20160816; b=fXEPgUkg5Kc5xpxpi3e+iCt2ayY+fI2A+Wj6ULu9aIpKwtbnHqpMYXblgxdLO8T2fJ 8xv1siYejYhyk+pHqHvF2prqtCpc0qJK+4ArilkgGw0K28w9pV4bbbaa6qZtc6RwuFx5 p2f59fQTyNSTZn5bIYPDghm/WQMa433iuPz+lvFWEjD4uwklPm1D93B9NDus/yXua1pb pUwl+rVDvdpg7XytJQMBusUKF2kXTGEMIKHdRt/g15z7XNIopqINC4NrMMrP++iDxewH VEUWOP1eRsVIDBbH696eB9OZvnAZPYiumAvNwIeL1hF96a+rnEu0r9DB0iM+v+3jTOSd J3yQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=OmEbpA5E3TywmtVkscx5dzs2EkbrqmpVYG01UCZaGwE=; b=rNI/ACuyeGfkWDRMenFTbLQsMKmsUXdQfLeagvYnU8NHGXVbBKLD7uaECxcFVIcIym uoumhjVUiMtYcHanDwmS9rl7mHht/CdX/BqBeq7O7CJdQPHO6XwBai+okQ/k7buleb1r ndTHPIm4Xt/SZy2K5IQYJkukxn+Wxfrf6ie81KcSKGWt+/05rgtIkSusrn1jumECnuHs MrYqvbSAL21nsDHLdc+phS+YWboPMqgnInAIv5nItEjZhjqQS/G5FiY4vi/wLfnPNmp2 BEfnOGT9cXyDVv7sPuBszd1+sap15aZmy8mD9Rcc3j1t584xHlYQCWo7ljceQznRubs3 HZyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=NAUg+HbJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j8-v6si10943407pll.493.2018.08.06.18.33.42; Mon, 06 Aug 2018 18:33:57 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=NAUg+HbJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387776AbeHGDf4 (ORCPT + 99 others); Mon, 6 Aug 2018 23:35:56 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:40973 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731331AbeHGDf4 (ORCPT ); Mon, 6 Aug 2018 23:35:56 -0400 Received: by mail-pf1-f194.google.com with SMTP id y10-v6so7701296pfn.8 for ; Mon, 06 Aug 2018 18:24:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=OmEbpA5E3TywmtVkscx5dzs2EkbrqmpVYG01UCZaGwE=; b=NAUg+HbJeL2eeMflsmYRhLfvrLALdFInERKAPoiNduuMYb+lzZvGsef+vfBLahbj8P FjFDXInCkPRifSgEg3++LnK/URLucH1MaJvyZGlYILiuIeazb/JdW1rN3iUYKOUEoesG EnG7enSi13NR7p1p1DeYghF7/24yXAHPazzeS4Q1D90HE2IAkCHVAK5yrTNmGj1UukwV 8J4lsQIhR9TS4KdnW2z9bLHgX3LihEIu/Tk9QJNGz66w4C8u4bFsr8kJgTZ47A/LMtA9 Io05OCAiHusbVqSIROcYNbYmtPuolWJ00mNuktqxDYEoetlqslXoHCekTcVrRm92m/Bi Uusg== 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; bh=OmEbpA5E3TywmtVkscx5dzs2EkbrqmpVYG01UCZaGwE=; b=kpsQ+/zZ/6YhyECEq88YImyXXzHlPXwEjhKGuVCU/6CFkb+xNn9wBIwQWwvawam0tQ CI9olr3urLY5xLAHl4oybgKfQFELzaz6rjSnLP/rFW95hutoFQ2poTm7XHb48jax10dI qsy0OEPkQACTnzxR5RQAFexhUf4C9zN7g+L7zEPyXAqmq0QAxB0jM9bP0jzp5bBKcdZj BdDzWnMnKBil96844OnNRQDxY6k1lpEpt+0ZUup8SWuRoMZXOe1QKMgEYO27QbsBxVcZ 9fNU6e9yNjyLsJH/PbevV7BRwH1rE6DL5zUbBw+72rsSaX7GZU+2w4g4+y40maYuPFDN LFSA== X-Gm-Message-State: AOUpUlFFQUmiR7qeGGomaDJ7HCqQuekw5LMjdiKCBexDFknNCNB1Y3GG 9EV7u10nm63QoFkzWE10ylRztZwLA0o= X-Received: by 2002:a65:5144:: with SMTP id g4-v6mr16513819pgq.21.1533605046591; Mon, 06 Aug 2018 18:24:06 -0700 (PDT) Received: from localhost ([104.133.8.110]) by smtp.gmail.com with ESMTPSA id x2-v6sm25859005pfi.166.2018.08.06.18.24.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Aug 2018 18:24:05 -0700 (PDT) From: Stephane Eranian To: linux-kernel@vger.kernel.org Cc: acme@redhat.com, peterz@infradead.org, mingo@elte.hu, jolsa@redhat.com Subject: [PATCH] perf ordered_events: fix crash in free_dup_event() Date: Mon, 6 Aug 2018 18:23:35 -0700 Message-Id: <1533605015-19514-1-git-send-email-eranian@google.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Depending on memory allocations, it was possible to get a SEGFAULT in free_dup_event() because the event pointer was bogus: perf[1354]: segfault at ffffffff00000006 ip 00000000004b7fc7 Initially, I thought it was some double free. But it turns out it looked more like a buffer overrun. Adding padding before the union perf_event field in struct ordered_event avoided the problem. But it was not obvious where this could be coming from given the accesses to the struct, i.e., no internal array. Then, it struck me that the following was bogus in __dup_event(): __dup_event(struct ordered_events *oe, union perf_event *event) { ... union perf_event *new_event; new_event = memdup(event, event->header.size); ... } The problem here is that header.size <= sizeof(*new_event). The code was trying to copy only what was necessary, but then, the allocation was also only partial. In other words if the event was not the largest possible for the union, it would not be fully backed by memory, likely causing troubles. This patch fixes the problem by passing the size of the union and not that of the actual event. Signed-off-by: Stephane Eranian --- tools/perf/util/ordered-events.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/ordered-events.c b/tools/perf/util/ordered-events.c index bad9e0296e9a..a90dbe5df019 100644 --- a/tools/perf/util/ordered-events.c +++ b/tools/perf/util/ordered-events.c @@ -66,9 +66,9 @@ static union perf_event *__dup_event(struct ordered_events *oe, union perf_event *new_event = NULL; if (oe->cur_alloc_size < oe->max_alloc_size) { - new_event = memdup(event, event->header.size); + new_event = memdup(event, sizeof(*event)); if (new_event) - oe->cur_alloc_size += event->header.size; + oe->cur_alloc_size += sizeof(*event); } return new_event; -- 2.18.0.597.ga71716f1ad-goog