Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp729671ybx; Thu, 7 Nov 2019 01:45:41 -0800 (PST) X-Google-Smtp-Source: APXvYqwzikmtGWF6SvzbYufaBn2k/5HpJbW6E+mTI7d60p+dpP8FPEt4/2K8RXiYsUr/UyelVPi5 X-Received: by 2002:a50:b2c4:: with SMTP id p62mr2453520edd.128.1573119941880; Thu, 07 Nov 2019 01:45:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573119941; cv=none; d=google.com; s=arc-20160816; b=dP4UbXRDExm9+fhgY/qPkkTm7qqscj/XtPere3ncoiX5/XXsIGq1AcDRPb/OwLZcOa nu7C1GdhQ5S//oN/1dcGEhsKf0ptEXiDtuBZht7L5zIgQ1Ja17lLNqv0IVf8WjtUGVj1 ZYMd2mYOR3BlKe8fiqWJw3C0c1tkfa+acP1YCI0n4glQWGGBkZd6zdkmyCRpkJ+wiVIr yItD4ogcXxJf4RhzDSeHjwmve6tk/lwVBUkFuQuzrwIgbQil1kf+FZ/wPj1K0ec4q4nr hleD92lj8tq0ys6zLIMyrd9au4TNS62WDSqEW3n5ZnpZjkofQ2zfu2lDUvc7nhPjLJOW RLTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:user-agent:in-reply-to:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=jKoCIjcW28Hko0qMcEIiY97Qrw+BX0MIbxPtgok3aZA=; b=YBp1/ZrkYJZNl1lt+sb9iZVrfz3BTW938hyuz1NDkCojyxAIu3fmV1cWR690Tk6/yr dvio9yYpi19kscids1tv22QzksflHeG9FwcEzlsn0vwYasGbJUyLeT4T9ZYHIHh6+kyj LsctLJhtVjfKw+Z8CFH/RB26ubeKD3ty2MDVSuuS9aMAkMOEbFeDODdKJPDHn7PjDey6 vXNyOUYU9u8ZDMgPCRMnl/qWc2dHRz/Pu7clJeJaF4K3j8vV+BEx30j/Nx5nS0Mr7cVJ AOUckoVzHWTsAgschB4Osm47Px4ziRiOfSmIKxO5u54Jm1y8ZDdkOZSgxU5VF/Tu7f6o V6dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=a+W5vLX9; 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=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 a1si1050450ejr.320.2019.11.07.01.45.16; Thu, 07 Nov 2019 01:45:41 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=a+W5vLX9; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387817AbfKGJmh (ORCPT + 99 others); Thu, 7 Nov 2019 04:42:37 -0500 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:44239 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726734AbfKGJmg (ORCPT ); Thu, 7 Nov 2019 04:42:36 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573119755; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jKoCIjcW28Hko0qMcEIiY97Qrw+BX0MIbxPtgok3aZA=; b=a+W5vLX9lz2qF0YHTaTlGSSatxGFntipjgzq5d2l4TxXhe7oUOTqkG/E1XimWyMms9aj1R 7uVZ9Nu8XFwICHA26fGGLda2MgusH9xk4FWlzNwcDlAZeBeHq+5q0IXk+a03Fmi0wV++Fb 8KimzDCyVkqFk1HnP9+aTu3n00w9qS0= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-384--ng_66w-Ptuq0G0jHTRgeg-1; Thu, 07 Nov 2019 04:42:31 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7B4AC800C61; Thu, 7 Nov 2019 09:42:30 +0000 (UTC) Received: from krava (unknown [10.43.17.48]) by smtp.corp.redhat.com (Postfix) with SMTP id 9348D60BFB; Thu, 7 Nov 2019 09:42:27 +0000 (UTC) Date: Thu, 7 Nov 2019 10:42:26 +0100 From: Jiri Olsa To: Leo Yan Cc: Arnaldo Carvalho de Melo , Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Namhyung Kim , linux-kernel@vger.kernel.org, Naresh Kamboju Subject: Re: [PATCH v2] perf tests: Fix out of bounds memory access Message-ID: <20191107094226.GC14657@krava> References: <20191107020244.2427-1-leo.yan@linaro.org> MIME-Version: 1.0 In-Reply-To: <20191107020244.2427-1-leo.yan@linaro.org> User-Agent: Mutt/1.12.1 (2019-06-15) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: -ng_66w-Ptuq0G0jHTRgeg-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Nov 07, 2019 at 10:02:44AM +0800, Leo Yan wrote: > The test case 'Read backward ring buffer' failed on 32-bit architectures > which were found by LKFT perf testing. The test failed on arm32 x15 > device, qemu_arm32, qemu_i386, and found intermittent failure on i386; > the failure log is as below: >=20 > 50: Read backward ring buffer : > --- start --- > test child forked, pid 510 > Using CPUID GenuineIntel-6-9E-9 > mmap size 1052672B > mmap size 8192B > Finished reading overwrite ring buffer: rewind > free(): invalid next size (fast) > test child interrupted > ---- end ---- > Read backward ring buffer: FAILED! >=20 > The log hints there have issue for memory usage, thus free() reports > error 'invalid next size' and directly exit for the case. Finally, this > issue is root caused as out of bounds memory access for the data array > 'evsel->id'. >=20 > The backward ring buffer test invokes do_test() twice. 'evsel->id' is > allocated at the first call with the flow: >=20 > test__backward_ring_buffer() > `-> do_test() > =09 `-> evlist__mmap() > =09 `-> evlist__mmap_ex() > =09 `-> perf_evsel__alloc_id() >=20 > So 'evsel->id' is allocated with one item, and it will be used in > function perf_evlist__id_add(): >=20 > evsel->id[0] =3D id > evsel->ids =3D 1 >=20 > At the second call for do_test(), it skips to initialize 'evsel->id' > and reuses the array which is allocated in the first call. But > 'evsel->ids' contains the stale value. Thus: >=20 > evsel->id[1] =3D id -> out of bound access > evsel->ids =3D 2 >=20 > To fix this issue, we will use evlist__open() and evlist__close() pair > functions to prepare and cleanup context for evlist; so 'evsel->id' and > 'evsel->ids' can be initialized properly when invoke do_test() and avoid > the out of bounds memory access. right, we need to solve this on libperf level, so it's possible to call mmap/munmap multiple time without close/open.. I'll try to send something, but meanwhile this is good workaround Reviewed-by: Jiri Olsa thanks, jirka >=20 > Signed-off-by: Leo Yan > --- > tools/perf/tests/backward-ring-buffer.c | 9 +++++++++ > 1 file changed, 9 insertions(+) >=20 > diff --git a/tools/perf/tests/backward-ring-buffer.c b/tools/perf/tests/b= ackward-ring-buffer.c > index 338cd9faa835..5128f727c0ef 100644 > --- a/tools/perf/tests/backward-ring-buffer.c > +++ b/tools/perf/tests/backward-ring-buffer.c > @@ -147,6 +147,15 @@ int test__backward_ring_buffer(struct test *test __m= aybe_unused, int subtest __m > =09=09goto out_delete_evlist; > =09} > =20 > +=09evlist__close(evlist); > + > +=09err =3D evlist__open(evlist); > +=09if (err < 0) { > +=09=09pr_debug("perf_evlist__open: %s\n", > +=09=09=09 str_error_r(errno, sbuf, sizeof(sbuf))); > +=09=09goto out_delete_evlist; > +=09} > + > =09err =3D do_test(evlist, 1, &sample_count, &comm_count); > =09if (err !=3D TEST_OK) > =09=09goto out_delete_evlist; > --=20 > 2.17.1 >=20