Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4490391ybb; Tue, 14 Apr 2020 08:18:16 -0700 (PDT) X-Google-Smtp-Source: APiQypJhnLtaEmsIKpDkrQOmiSOF4tmCAOB7rP2z3C0dbofbKZSGmbeZ/NUJ0oCGU2ajAswQBV/j X-Received: by 2002:aa7:cd5b:: with SMTP id v27mr21138511edw.286.1586877496561; Tue, 14 Apr 2020 08:18:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586877496; cv=none; d=google.com; s=arc-20160816; b=I34BbZa5Po2WJmCLN1RVHWxLoAe4KUsIyg4PzHrIpYVPJO1tBhQan7MM7cO0tBPU4q 7k0SDQYYbK0WBKCIx5u3EK/fa5TLkJRpogOrBZnFw8Uw6gIPzEqLme0X/7Gcf1ZvJ6f2 RHaou8hb4byFc7rfvlcPEAOMsXjyLs1Y3uwCx6LzjEtiMJauV0m02KTqZfcwloDrd6IB LjiQgL1B4CBl58d97GxjDTwMeE16cwUudu34jL372/DunEs9XWna2RxCmIe8Kdr26thV KeIhXnnhCwiEBwl/ZXZae6PJqCAQ+PNszMy6zCWhtc72BHZkzbKmQ8UOfxdU1qA+WXnQ WjyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=MDCxMbwZCIalkERKeoAG/k67T3C98x/pTWapXX1xkMk=; b=twnLh9iuWos98CERYI1AqYBeyYCtLcJ5kMWF2ql7RRISleRTHcgJ84Ma+wnykRQhqT FjQJE9ImNiUoT4sVZZ4WdTIuwkI2sW7JZlgs64B60ziKUz9vmDFOllPqzoeGXat2cGZN pMUF8DI8NxIbruPar0LMmXPHNQIlAX8Kq39b2AOdB9KBuwpjEl0iBPw0PwXoGBvDOUHW gyDzoajH59J9SQsO7i7UX5Eh86uIgPmGuid7r7R89ildJLcwN0yil7TjLJkRR+RtDD/m FH0j4/yBuMNKpjyke2wTwkEPWVxhLoRIfjc+pE4L1hVQ6ctZpzVlXohsydyWFT+66kxb AvKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2020-01-29 header.b=p1SpySKC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o17si8990236ejj.483.2020.04.14.08.17.53; Tue, 14 Apr 2020 08:18:16 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2020-01-29 header.b=p1SpySKC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2438438AbgDNKf0 (ORCPT + 99 others); Tue, 14 Apr 2020 06:35:26 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:40214 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2438373AbgDNKci (ORCPT ); Tue, 14 Apr 2020 06:32:38 -0400 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 03EATIE1098998; Tue, 14 Apr 2020 10:32:03 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2020-01-29; bh=MDCxMbwZCIalkERKeoAG/k67T3C98x/pTWapXX1xkMk=; b=p1SpySKC/FngDeAc5GiZkWb0Y/SW2geU1EWRcPOB9FLkIhbc6R6AbqTWGcJSFowL8Vca Pf9xiQMGBeGey0pFmAsOtJI/yuFLgYwBdEJym1QIcT3Q9FilDHUkuBpT5fqwo6B5Plh4 RtY8D0Hs/VTlGeGPEeBkxOVHG5ecjpgSRenDVWNiBZ9SExU59jr7Me+KzNcGhwqEF3AM NEolvtnVjNq28fGX3my6KzZSAlXY2Q7OqfvwJxWiKVS/mOKsHDvlbIVEijt2P2mxwwrU n8HNcgB+/dTRwwtUYVBJYtVhSSl/i4irLqtmOLhYZzkjHB/Un36mSQmMPyk3f05j9YBG tg== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2120.oracle.com with ESMTP id 30b6hpkmeq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 14 Apr 2020 10:32:02 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 03EAVxaJ123210; Tue, 14 Apr 2020 10:32:02 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserp3020.oracle.com with ESMTP id 30bqpfxnx9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 14 Apr 2020 10:32:02 +0000 Received: from abhmp0004.oracle.com (abhmp0004.oracle.com [141.146.116.10]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id 03EAVwlD013715; Tue, 14 Apr 2020 10:31:59 GMT Received: from linux-1.home (/92.157.90.160) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 14 Apr 2020 03:31:58 -0700 From: Alexandre Chartre To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, jpoimboe@redhat.com, peterz@infradead.org, jthierry@redhat.com, tglx@linutronix.de, alexandre.chartre@oracle.com Subject: [PATCH V3 5/9] objtool: Add return address unwind hints Date: Tue, 14 Apr 2020 12:36:14 +0200 Message-Id: <20200414103618.12657-6-alexandre.chartre@oracle.com> X-Mailer: git-send-email 2.18.2 In-Reply-To: <20200414103618.12657-1-alexandre.chartre@oracle.com> References: <20200414103618.12657-1-alexandre.chartre@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9590 signatures=668686 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 malwarescore=0 adultscore=0 bulkscore=0 spamscore=0 suspectscore=1 phishscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2004140087 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9590 signatures=668686 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 adultscore=0 mlxlogscore=999 clxscore=1015 mlxscore=0 phishscore=0 suspectscore=1 lowpriorityscore=0 bulkscore=0 malwarescore=0 priorityscore=1501 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2004140086 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add the UNWIND_HINT_RADDR_DELETE and UNWIND_HINT_RADDR_ALTER unwind hint macros to flag instructions which remove or modify return addresses. Signed-off-by: Alexandre Chartre --- arch/x86/include/asm/orc_types.h | 2 + arch/x86/include/asm/unwind_hints.h | 23 +++++++++ tools/arch/x86/include/asm/orc_types.h | 2 + tools/objtool/check.c | 67 ++++++++++++++++++++++++++ 4 files changed, 94 insertions(+) diff --git a/arch/x86/include/asm/orc_types.h b/arch/x86/include/asm/orc_types.h index 6e060907c163..5c1141175d51 100644 --- a/arch/x86/include/asm/orc_types.h +++ b/arch/x86/include/asm/orc_types.h @@ -60,6 +60,8 @@ #define ORC_TYPE_REGS_IRET 2 #define UNWIND_HINT_TYPE_SAVE 3 #define UNWIND_HINT_TYPE_RESTORE 4 +#define UNWIND_HINT_TYPE_RADDR_ALTER 6 +#define UNWIND_HINT_TYPE_RADDR_DELETE 7 #ifndef __ASSEMBLY__ /* diff --git a/arch/x86/include/asm/unwind_hints.h b/arch/x86/include/asm/unwind_hints.h index f5e2eb12cb71..5211d2c5b7a2 100644 --- a/arch/x86/include/asm/unwind_hints.h +++ b/arch/x86/include/asm/unwind_hints.h @@ -94,6 +94,23 @@ UNWIND_HINT type=UNWIND_HINT_TYPE_RESTORE .endm +/* + * RADDR_DELETE: Used on instructions (other than ret instruction) + * which remove a return address from the stack. count is the number + * of return address which are removed. + */ +.macro UNWIND_HINT_RADDR_DELETE count=1 + UNWIND_HINT type=UNWIND_HINT_TYPE_RADDR_DELETE sp_offset=\count +.endm + +/* + * RADDR_ALTER: Used on instructions which change a return address on + * the stack. count is the number of return address which are changed. + */ +.macro UNWIND_HINT_RADDR_ALTER count=1 + UNWIND_HINT type=UNWIND_HINT_TYPE_RADDR_ALTER sp_offset=\count +.endm + #else /* !__ASSEMBLY__ */ #define UNWIND_HINT(sp_reg, sp_offset, type, end) \ @@ -112,6 +129,12 @@ #define UNWIND_HINT_RESTORE UNWIND_HINT(0, 0, UNWIND_HINT_TYPE_RESTORE, 0) +#define UNWIND_HINT_RADDR_ALTER(count) \ + UNWIND_HINT(0, count, UNWIND_HINT_TYPE_RADDR_ALTER, 0) + +#define UNWIND_HINT_RADDR_DELETE(count) \ + UNWIND_HINT(0, count, UNWIND_HINT_TYPE_RADDR_DELETE, 0) + #endif /* __ASSEMBLY__ */ #endif /* _ASM_X86_UNWIND_HINTS_H */ diff --git a/tools/arch/x86/include/asm/orc_types.h b/tools/arch/x86/include/asm/orc_types.h index 6e060907c163..5c1141175d51 100644 --- a/tools/arch/x86/include/asm/orc_types.h +++ b/tools/arch/x86/include/asm/orc_types.h @@ -60,6 +60,8 @@ #define ORC_TYPE_REGS_IRET 2 #define UNWIND_HINT_TYPE_SAVE 3 #define UNWIND_HINT_TYPE_RESTORE 4 +#define UNWIND_HINT_TYPE_RADDR_ALTER 6 +#define UNWIND_HINT_TYPE_RADDR_DELETE 7 #ifndef __ASSEMBLY__ /* diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 8b1df659cd68..0574ce8e232d 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -66,6 +66,28 @@ static bool raddr_pop(struct instruction **insn) return true; } +static bool raddr_delete(int count) +{ + if (raddr_count < count) + return false; + + raddr_count -= count; + return true; +} + +static bool raddr_alter(int count) +{ + int i; + + if (raddr_count < count) + false; + + for (i = 0; i < count; i++) + raddr_list[raddr_count - 1 - i] = RADDR_ALTERED; + + return true; +} + static int validate_branch(struct objtool_file *file, struct symbol *func, struct instruction *from, struct instruction *first, struct insn_state state); @@ -1314,6 +1336,14 @@ static int read_unwind_hints(struct objtool_file *file) insn->restore = true; insn->hint = true; continue; + + } else if (hint->type == UNWIND_HINT_TYPE_RADDR_DELETE) { + insn->raddr_delete = hint->sp_offset; + continue; + + } else if (hint->type == UNWIND_HINT_TYPE_RADDR_ALTER) { + insn->raddr_alter = hint->sp_offset; + continue; } insn->hint = true; @@ -1526,6 +1556,13 @@ static bool has_modified_stack_frame(struct insn_state *state) state->drap) return true; + /* + * If the stack was altered then don't check registers because + * a callee-saved registers might have been pushed on the stack. + */ + if (state->stack_altered) + return false; + for (i = 0; i < CFI_NUM_REGS; i++) if (state->regs[i].base != initial_func_cfi.regs[i].base || state->regs[i].offset != initial_func_cfi.regs[i].offset) @@ -2103,6 +2140,15 @@ static int validate_return_address(struct objtool_file *file, if (!raddr_insn) break; + /* + * If this is a dynamic branch then we expect this + * branch to return or not, depending on the defined + * list of RA we have, so just continue the processing + * of the RA list. + */ + if (raddr_insn == RADDR_ALTERED) + continue; + /* * If we are branching to a defined address then * just do an unconditional jump there. @@ -2287,6 +2333,27 @@ static int validate_branch(struct objtool_file *file, struct symbol *func, return 0; } + if (insn->raddr_delete) { + /* delete return address */ + if (!raddr_delete(insn->raddr_delete)) { + WARN_FUNC("fail to delete %d return address", + insn->sec, insn->offset, + insn->raddr_delete); + return 1; + } + state.stack_altered = true; + + } else if (insn->raddr_alter) { + /* alter return address */ + if (!raddr_alter(insn->raddr_alter)) { + WARN_FUNC("fail to alter %d return address", + insn->sec, insn->offset, + insn->raddr_alter); + return 1; + } + state.stack_altered = true; + } + switch (insn->type) { case INSN_RETURN: -- 2.18.2