Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp4430442imm; Tue, 7 Aug 2018 01:07:56 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcRtofyG/4bUp/tkl6ytweLHajwuOfcaD/iNGmm7tA7d0LGrfTMQv5HoaB18nHVC29qhcaS X-Received: by 2002:a63:b605:: with SMTP id j5-v6mr17891219pgf.437.1533629276309; Tue, 07 Aug 2018 01:07:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533629276; cv=none; d=google.com; s=arc-20160816; b=MHfbBd1QNRhSEE+CchaIGvY4ez/B/zZmCDghXFxKLhqCwmcF1oPgvzvuMZUVIYd1w1 ijlRNAdKpdv3nVxR94XY8JuaZIhT6Xzq3/2o4HIkJ6oTr0pISyJjx06fT856gNPbmv8A g3ZI2sppUgF9c7bnOt5EyqIlWuBkgWMu99LCUj+BT9Mctg+oD251OHfp9jNjLVLqcOQ1 OwBb+a58NKp9SBHzbARRqjMxaXUjqr2uftkaWAiKirSZqhq+6xnNZRFerh1RFPYzC5Zw wO6NzrPD+EoMVxHi/j5ZLpGk/68I+PGZyFCPu53qmVD+pBtRm0ZLKOaV6p0NfKGNoHQQ Ki+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=onVr5UU3lX3XbX/E6qLVNpYg7MklVCqKGbkpTSAVYJg=; b=SdLIJ5T/AwPTg0wdMizUe7LYC/J04KKB55vREYckLdFJUgpjNPzk9uaXvRNPt0C1aF OrtKafaTHLS/ySpadTPA57PKmMZnlmJUgdxeaFLYV4wxzYgeINm6qaFjP4iaxVejMV6Q DzuiIW9UZ9TqdBYfIz4De+sYIsQYLYt8LiD1SKeL4UKfLamd/okxSGM315NEjGA0fazc Dx7d2VDI9A5fN88aSoMfMEtUYWwLNVMvNFf1XAwa9gL/DP/KOszuv8m1GVcGGocpDqpF /Ujb9tPu4NKDxnrMJw94yWOjWYRKhRsiPNeIGitaSTkIUBesX40FZGc5cmGJOO4H02Ng Fh/w== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j62-v6si855626pfb.348.2018.08.07.01.07.41; Tue, 07 Aug 2018 01:07:56 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388847AbeHGJdb (ORCPT + 99 others); Tue, 7 Aug 2018 05:33:31 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:33488 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727360AbeHGJdb (ORCPT ); Tue, 7 Aug 2018 05:33:31 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 827377C6CA; Tue, 7 Aug 2018 07:20:31 +0000 (UTC) Received: from krava (unknown [10.43.17.214]) by smtp.corp.redhat.com (Postfix) with SMTP id 47CD1112D19D; Tue, 7 Aug 2018 07:20:30 +0000 (UTC) Date: Tue, 7 Aug 2018 09:20:29 +0200 From: Jiri Olsa To: Stephane Eranian Cc: linux-kernel@vger.kernel.org, acme@redhat.com, peterz@infradead.org, mingo@elte.hu Subject: Re: [PATCH] perf ordered_events: fix crash in free_dup_event() Message-ID: <20180807072029.GA7716@krava> References: <1533605015-19514-1-git-send-email-eranian@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1533605015-19514-1-git-send-email-eranian@google.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Tue, 07 Aug 2018 07:20:31 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Tue, 07 Aug 2018 07:20:31 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'jolsa@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Aug 06, 2018 at 06:23:35PM -0700, Stephane Eranian wrote: > 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 is there any reproducer? > > 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 hum, and I think we should continue to do so.. we can't allocate ~4000 bytes space for 30 bytes sample > 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. how? using that event allocated space for another type of event? > > This patch fixes the problem by passing the size of the union and not > that of the actual event. I think that's just bypassing the real issue, please share more details on how to reproduce this thanks, jirka > > 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 >