Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2025142imu; Fri, 14 Dec 2018 04:42:02 -0800 (PST) X-Google-Smtp-Source: AFSGD/UQWNnI2FI/THjE+08wCkmRZdcpZOrZBgSdaazbYYG4DJutLfsjixs+juDSRenQEstcjUTe X-Received: by 2002:a63:df50:: with SMTP id h16mr2579709pgj.421.1544791322858; Fri, 14 Dec 2018 04:42:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544791322; cv=none; d=google.com; s=arc-20160816; b=Be8E6p2VNvqzVDmoxd3yZVJUhlkYIQrJQQXXMyo6E4fQH48WpA5oBKtCKNxNHOOy/d AL9lRUEV95vI6DM6TBM5GHcrQ3G09sx+VOTpZLdbNgjujY40VGbWN55/xNuVraBfqRfn GORjqCyhSA2EfSZjfggxotN94bM5seDoRL6ohvgMlrkrWajQ/WU1e68CI1m+WQF1fFhB +wSBifA7EFLF61eSOqsW8m32zi9r9IzJlO2WRlye4Xnovz3ftlFk808t5SEnTwD7Vbsu anmG9muRP9bS2fqRn1eAZtUSTEB2PoI2IxaUmTC8kFhe9Aad7YJBFbOzM30HnFrUUmex IE4Q== 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=1BY62Mp0Xgzu3hjJsGXW8C9OVk9hdEcQvQ8RkaO8hoQ=; b=qJ48+8iUT2P+mGWer3SOmC6sfvEVAnmkJZRK3g1gOVk5MO4104SnqPPFc6h59ZSy2x cxFYOBC30TDzB2mhdC1UEGiHySGmtMWh+yoAnUqSgANEik+xRfkGLL7TVLq++heebFYF 3lE1W+y4Ty+I2VAqrWNRVHEZj19C+Ybz1O7mlKocW+D8FqrF03fuRCCk19/UE9GFGgaF z0jwbg7bnR+vWKmGW8i24vvzulAEagnh1SA44we3jXuLVLKYCYGBcgE8Ztl7QdGA09LZ Hv/COUWQ/b6BNcRfALfGZxIWkUJDlOS+f/nqT+Kzi1LBD9JuJbSooMFHRGghOt4p/9v7 OdTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ksoLFmyd; 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 z22si3861716plo.202.2018.12.14.04.41.48; Fri, 14 Dec 2018 04:42:02 -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=ksoLFmyd; 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 S1730653AbeLNMky (ORCPT + 99 others); Fri, 14 Dec 2018 07:40:54 -0500 Received: from mail.kernel.org ([198.145.29.99]:48064 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729723AbeLNMDu (ORCPT ); Fri, 14 Dec 2018 07:03: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 362A02146F; Fri, 14 Dec 2018 12:03:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544789029; bh=/zDJ8chzyhA8EUf1s76LF/XfD1vov0ASjL8PaXDIGSk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ksoLFmydxM/H/Y3gZNtYjJC8K8ByKU/k4w3jaHDq4YWFLI0ndj946n72GHqAAacKk eeGtDrN+j4P0aKd+XSNBAiX231npJamVvomM7S11thgt/BVeiXJyfNPpKASp/dofwI 7OZGZMGDf73Ny2FMAREafeWPDwZZLaCgZwzx0tRU= 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.19 066/142] objtool: Fix segfault in .cold detection with -ffunction-sections Date: Fri, 14 Dec 2018 12:59:11 +0100 Message-Id: <20181214115749.693326606@linuxfoundation.org> X-Mailer: git-send-email 2.20.0 In-Reply-To: <20181214115747.053633987@linuxfoundation.org> References: <20181214115747.053633987@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.19-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 a6f543e0b387..b75d004f6482 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