Return-Path: From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Tommi Rantala , Alexander Shishkin , Peter Zijlstra , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH AUTOSEL for 4.9 071/219] perf buildid: Do not assume that readlink() returns a null terminated string Date: Sat, 3 Mar 2018 22:28:37 +0000 Message-ID: <20180303222716.26640-71-alexander.levin@microsoft.com> References: <20180303222716.26640-1-alexander.levin@microsoft.com> In-Reply-To: <20180303222716.26640-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org List-ID: From: Tommi Rantala [ Upstream commit 5a2342111c68e623e27ee7ea3d0492d8dad6bda0 ] Valgrind was complaining: $ valgrind ./perf list >/dev/null =3D=3D11643=3D=3D Memcheck, a memory error detector =3D=3D11643=3D=3D Copyright (C) 2002-2015, and GNU GPL'd, by Julian Sewar= d et al. =3D=3D11643=3D=3D Using Valgrind-3.12.0 and LibVEX; rerun with -h for cop= yright info =3D=3D11643=3D=3D Command: ./perf list =3D=3D11643=3D=3D =3D=3D11643=3D=3D Conditional jump or move depends on uninitialised value= (s) =3D=3D11643=3D=3D at 0x4C30620: rindex (vg_replace_strmem.c:199) =3D=3D11643=3D=3D by 0x49DAA9: build_id_cache__origname (build-id.c:19= 8) =3D=3D11643=3D=3D by 0x49E1C7: build_id_cache__valid_id (build-id.c:22= 2) =3D=3D11643=3D=3D by 0x49E1C7: build_id_cache__list_all (build-id.c:50= 7) =3D=3D11643=3D=3D by 0x4B9C8F: print_sdt_events (parse-events.c:2067) =3D=3D11643=3D=3D by 0x4BB0B3: print_events (parse-events.c:2313) =3D=3D11643=3D=3D by 0x439501: cmd_list (builtin-list.c:53) =3D=3D11643=3D=3D by 0x497150: run_builtin (perf.c:359) =3D=3D11643=3D=3D by 0x428CE0: handle_internal_command (perf.c:421) =3D=3D11643=3D=3D by 0x428CE0: run_argv (perf.c:467) =3D=3D11643=3D=3D by 0x428CE0: main (perf.c:614) [...] Additionally, a zero length result from readlink() is not very interesting. Signed-off-by: Tommi Rantala Cc: Alexander Shishkin Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/20170322130624.21881-3-tommi.t.rantala@nokia= .com Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/perf/util/build-id.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index e528c40739cc..993ef2762508 100644 --- a/tools/perf/util/build-id.c +++ b/tools/perf/util/build-id.c @@ -182,13 +182,17 @@ char *build_id_cache__origname(const char *sbuild_id) char buf[PATH_MAX]; char *ret =3D NULL, *p; size_t offs =3D 5; /* =3D=3D strlen("../..") */ + ssize_t len; =20 linkname =3D build_id_cache__linkname(sbuild_id, NULL, 0); if (!linkname) return NULL; =20 - if (readlink(linkname, buf, PATH_MAX) < 0) + len =3D readlink(linkname, buf, sizeof(buf) - 1); + if (len <=3D 0) goto out; + buf[len] =3D '\0'; + /* The link should be "../../" */ p =3D strrchr(buf, '/'); /* Cut off the "/" */ if (p && (p > buf + offs)) { --=20 2.14.1