Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2008474imu; Fri, 14 Dec 2018 04:26:00 -0800 (PST) X-Google-Smtp-Source: AFSGD/X+AcW/pD5iNhcSg2p29IEHaDIeac5W3utlnReyH90zzekNWops6PK2oL0UdSDdTiNhgAS9 X-Received: by 2002:a63:cf48:: with SMTP id b8mr2514852pgj.17.1544790360047; Fri, 14 Dec 2018 04:26:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544790360; cv=none; d=google.com; s=arc-20160816; b=E6OpXFHngN6B8nS6CgeqZa2StPCwEV1FYtr1JJRVeMQmQxnr7ZxRmzRo/bPyTsgoJ3 BbUaw88UAzNXp9AnPCowzAj8m99UAwk3lk5aUfIAVcPTx3GS9FCWgd4G7/qGScV4tIYg nYYtNiN0ge9wnWnRWk3cmxo/oKvb/pe2YeK/PzSmGcs6KhL6Z8uvN8j9Pb2Rx3yB/SqA Gnp5l2TfZoAVSMNFcLCkVFXyokoMxeG+s9RFwLjRpRDzDzeakhV4daF34CoskKhuYYVQ 6LGxeWrPWCOe4kp9Cmi8VNG+E9F3Wr5ZPfccRtxPY7XVLtej+WvCOtCIMLv2m86Vq5xw 6X1w== 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=5fmObCGo6MqTYEVj+/iOiCmBwvNqtpVBSHjoMyyBGUc=; b=buZOi931k4YRICAr+pQfroK4FvpYYbZwIt0g0ZbuCx0KH1Rf01eq5Q3f8gF9u9Tw4K vWDKrRPB7VTaeRcmHygM67BG/AL+sToaOORbmvLfgsd4dh3hHYJFZaqWY+kfpdFDOVhu HquWYHHQ7oOXMvsHgTyHaf9dkxjl5Gfak5wv0WyJTUewDrdUBe5J0axwcIixZ+TnQRN7 Kx1pOWmCouXqUWGi6EICmhJFq3EkctLpqO70BafQ3Te3yaQkz8GLjrPtn6V2NSPU2gAH +KViJRIo6K1NfERLhu2/kilf4KAmK8L7ZuI08z1DCIGvFQcJRhodb3xira++MLf9nHcE ElBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0EXTzpTf; 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 s5si3770382plr.211.2018.12.14.04.25.45; Fri, 14 Dec 2018 04:26:00 -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=0EXTzpTf; 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 S1732010AbeLNMMw (ORCPT + 99 others); Fri, 14 Dec 2018 07:12:52 -0500 Received: from mail.kernel.org ([198.145.29.99]:60156 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731653AbeLNMMu (ORCPT ); Fri, 14 Dec 2018 07:12:50 -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 E2DA320892; Fri, 14 Dec 2018 12:12:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544789569; bh=XfX/MPy75tFDAOzxEDwKsbJVyRvpamlSAf/pGDOJ82w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0EXTzpTfzy6MLEMh184+W9Y58e4gno23zkpZFMbgtSybpnKBhro2sj3dVtkyPl+J9 GGjWeOokJYJXELrZXScFlUIeT8iBnFGwwgshPbnlZelsCrfYT+7lz3QMrPzUFj52ac Qw+7eA4ayWT8ivcsK3sSr4UoQfKeXIanllVKf/bw= 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.9 22/51] objtool: Fix segfault in .cold detection with -ffunction-sections Date: Fri, 14 Dec 2018 13:00:24 +0100 Message-Id: <20181214115715.473913882@linuxfoundation.org> X-Mailer: git-send-email 2.20.0 In-Reply-To: <20181214115713.244259772@linuxfoundation.org> References: <20181214115713.244259772@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.9-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