Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1990666imu; Fri, 14 Dec 2018 04:10:08 -0800 (PST) X-Google-Smtp-Source: AFSGD/WyodqVtr481leJ08a1UdZA0Px8OTB6P5PClZNG4qRLrJTINwtJJhH+iO5nbVLpZXyS1xtu X-Received: by 2002:a17:902:14e:: with SMTP id 72mr2630905plb.287.1544789408674; Fri, 14 Dec 2018 04:10:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544789408; cv=none; d=google.com; s=arc-20160816; b=us6TjwqjZ8PDPeofhJeMHhtxeg0HQ251DVswNXqdu8YmLeZd3QHsZv7VHhurnN5HFx IdcflNIEdvHZB3pPA1xq1P0hyi/qriRtZ6mDbxVshUiPvQ3k96FVmhiROK1u8ALsWrdj D74kYoT3X0JddI1JLez6qcz+S/MuTtMztn3ArqmY9UPhi1dci3Q16IpYiB1E0xlDJJ6I shuc1sa6tT2DJc8Gj430omS+YAdpHJ5YyP9rRuAtqldS1czZIRU2zHbrFD7f6GEfwIJH GN4TAPbULd+VUfjfVM5B5wckTja9QJcK/QCZuIKisQ//AeDBVLp5GQAuSsdxtaXdc6T4 zpsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=I9BdiXk0qk/cDDHRpKb6TLU/+NLptCuihDUKziogtTk=; b=Sp1fahCitUmyIrkRJCfVtTYSmWEyB3ihLLCHY4+U3d5zOLWxQQhAtxwBI5EK1/ReSF 5oVuxAGgkPOmQqEMcfZLQKNooagvBH9pZOAeGVpkZCQAoXY+ILyCAwK3O4KNlX3emAOA bNFTpHX2sS74Y7Rz5EBeQkng9pY7RkExJ80EracC/u2SOyfha2nFRQ15jPddpeNwXjUf 2hdsMA7BgMyjwORT9+hbX1LmSM5faUxWt+hmWaoTQFOPz6vvn4X5uRNIGW9myJVCUCMx WERX9WaOmG1phK1aXKqQ/Kd1cpd6nerqacJ8/oof5b75rHcnWzdWl5pLpdfnwQ/XcWUx YL3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=UvGNkSzq; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t186si2035281pfd.68.2018.12.14.04.09.49; Fri, 14 Dec 2018 04:10:08 -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=@kernel.org header.s=default header.b=UvGNkSzq; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730945AbeLNMIo (ORCPT + 99 others); Fri, 14 Dec 2018 07:08:44 -0500 Received: from mail.kernel.org ([198.145.29.99]:54214 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729989AbeLNMIm (ORCPT ); Fri, 14 Dec 2018 07:08:42 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3EA31214DB; Fri, 14 Dec 2018 12:08:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544789320; bh=7PlOOTdMvKOJfkaMfdQdn8MjkhVuhdmRueVtZqUOgdg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UvGNkSzqQkIde0jNfMQhmCiqe16TIc9HuQOsk50KbmlarSkwAR6U0HArknWxxRS+w cs6EWBiRtSXtb0pmYcSF25MIHC6YKwc96djHhOOFoomQnG+q+zXmsZcElv/CTpyXJI 2IlbMSgoXTYG/NE8mY63UNdmRQTZhnLtuQyvN1JM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Artem Savkov , Josh Poimboeuf , Linus Torvalds , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Sasha Levin Subject: [PATCH 4.14 36/89] objtool: Fix segfault in .cold detection with -ffunction-sections Date: Fri, 14 Dec 2018 12:59:49 +0100 Message-Id: <20181214115731.463727814@linuxfoundation.org> X-Mailer: git-send-email 2.20.0 In-Reply-To: <20181214115729.658859279@linuxfoundation.org> References: <20181214115729.658859279@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 22566c1603030f0a036ad564634b064ad1a55db2 ] Because find_symbol_by_name() traverses the same lists as read_symbols(), changing sym->name in place without copying it affects the result of find_symbol_by_name(). In the case where a ".cold" function precedes its parent in sec->symbol_list, it can result in a function being considered a parent of itself. This leads to function length being set to 0 and other consequent side-effects including a segfault in add_switch_table(). The effects of this bug are only visible when building with -ffunction-sections in KCFLAGS. Fix by copying the search string instead of modifying it in place. Signed-off-by: Artem Savkov Signed-off-by: Josh Poimboeuf Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Fixes: 13810435b9a7 ("objtool: Support GCC 8's cold subfunctions") Link: http://lkml.kernel.org/r/910abd6b5a4945130fd44f787c24e07b9e07c8da.1542736240.git.jpoimboe@redhat.com Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin --- tools/objtool/elf.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 3616d626991e..dd4ed7c3c062 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -31,6 +31,8 @@ #include "elf.h" #include "warn.h" +#define MAX_NAME_LEN 128 + struct section *find_section_by_name(struct elf *elf, const char *name) { struct section *sec; @@ -298,6 +300,8 @@ static int read_symbols(struct elf *elf) /* Create parent/child links for any cold subfunctions */ list_for_each_entry(sec, &elf->sections, list) { list_for_each_entry(sym, &sec->symbol_list, list) { + char pname[MAX_NAME_LEN + 1]; + size_t pnamelen; if (sym->type != STT_FUNC) continue; sym->pfunc = sym->cfunc = sym; @@ -305,9 +309,16 @@ static int read_symbols(struct elf *elf) if (!coldstr) continue; - coldstr[0] = '\0'; - pfunc = find_symbol_by_name(elf, sym->name); - coldstr[0] = '.'; + pnamelen = coldstr - sym->name; + if (pnamelen > MAX_NAME_LEN) { + WARN("%s(): parent function name exceeds maximum length of %d characters", + sym->name, MAX_NAME_LEN); + return -1; + } + + strncpy(pname, sym->name, pnamelen); + pname[pnamelen] = '\0'; + pfunc = find_symbol_by_name(elf, pname); if (!pfunc) { WARN("%s(): can't find parent function", -- 2.19.1