From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yw1-x1135.google.com (mail-yw1-x1135.google.com [IPv6:2607:f8b0:4864:20::1135]) by sourceware.org (Postfix) with ESMTPS id 723373858D3C for ; Mon, 17 Oct 2022 07:24:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 723373858D3C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yw1-x1135.google.com with SMTP id 00721157ae682-333a4a5d495so99267127b3.10 for ; Mon, 17 Oct 2022 00:24:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=NaL1uIklTlYehn6FU0cr5SJYL+n062NEIdLSKCFsJDo=; b=c01tP73Gchr6YLNZdbbOUohTIr4ztN3eJhTfca4aWMtKeaPm13ea8mZ5AlEI1mec0h KwliY+9vQvxoUKtOaOsQcwOVP9DKPGyLoGSWCivskp0Ey9X9HXC0kcXpbjZp0gV8Prt8 tBXKFJlrZE/DpJ0jyNzlXbxJ/JPC59GysGXgS8vxb0+b7PZTBWUHtjMaMqyNn2dq1T9L OjbW1IzPNdgmknSolttg8IiKPOC8FeAQYuG+RUnr0ycykwISjJEn3PgiVppUZ9We95Ng Hy3onpxfYrkzbaiNtj3ylY2kMY36NoT57rBCCMzMjQErsiIggqs3onlrt1UdSaXymDJQ GWxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NaL1uIklTlYehn6FU0cr5SJYL+n062NEIdLSKCFsJDo=; b=gzsdzfLeA3cw4Gwr2wXDeDMQSmUEwf5ca7QmulOfjrKoi/crFR5THgt+DxWvS3MGLS MoNMbog+a/3MdKB0e6kI10Y1YNJSBb8Vx//EpYjceAuawVrsHp98k6feY3j5pNjcHQ+O DIUW+32Os7k2pri7AAogpIBy4MaYZAdgW5vGJhEuSbmTSlf3mxN0t78QGsSsqXOqaUBI 3zoHv4jLmOVn83pPohrDEJoeWLObE9etCcjy68HM/gr2ah4iZ9qxkwgnSgYSy36ErPS3 piL4glbIXQAj1hzVpVDtmsRcJU2mQAyB+F5rP7C5AK9aljzRNfaxyG2OPVgSLm+50eoz F3RQ== X-Gm-Message-State: ACrzQf38KbFZTkeCvUc7gxOIsEke/FJhPp1XlouRd15W6ZoxnxzTLfc/ tmT/0jWwYQlHYBnLbZncbI8jgjA6MLWORnX/qw8= X-Google-Smtp-Source: AMsMyM5HSL1vuwZYzxRVliwvQICA3dD2KTqxX7FeHpT/ICw8xl7uoMazdtw8UWQEdYpmYkMyAeCC3gCXaT7fCPDo5AQ= X-Received: by 2002:a81:1c53:0:b0:354:ce32:2d82 with SMTP id c80-20020a811c53000000b00354ce322d82mr8031226ywc.249.1665991472703; Mon, 17 Oct 2022 00:24:32 -0700 (PDT) MIME-Version: 1.0 References: <20220921204206.95480-1-hjl.tools@gmail.com> In-Reply-To: From: Hongtao Liu Date: Mon, 17 Oct 2022 15:24:20 +0800 Message-ID: Subject: Re: PING^1: [PATCH] x86: Check corrupted return address when unwinding stack To: "H.J. Lu" Cc: GCC Patches , Uros Bizjak Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, Oct 5, 2022 at 5:33 AM H.J. Lu wrote: > > On Wed, Sep 21, 2022 at 1:42 PM H.J. Lu wrote: > > > > If shadow stack is enabled, when unwinding stack, we count how many stack > > frames we pop to reach the landing pad and adjust shadow stack by the same > > amount. When counting the stack frame, we compare the return address on > > normal stack against the return address on shadow stack. If they don't > > match, return _URC_FATAL_PHASE2_ERROR for the corrupted return address on > > normal stack. Don't check the return address for > > > > 1. Non-catchable exception where exception_class == 0. Process will be > > terminated. > > 2. Zero return address which marks the outermost stack frame. > > 3. Signal stack frame since kernel puts a restore token on shadow stack. Ok. > > > > * unwind-generic.h (_Unwind_Frames_Increment): Add the EXC > > argument. > > * unwind.inc (_Unwind_RaiseException_Phase2): Pass EXC to > > _Unwind_Frames_Increment. > > (_Unwind_ForcedUnwind_Phase2): Likewise. > > * config/i386/shadow-stack-unwind.h (_Unwind_Frames_Increment): > > Take the EXC argument. Return _URC_FATAL_PHASE2_ERROR if the > > return address on normal stack doesn't match the return address > > on shadow stack. > > --- > > libgcc/config/i386/shadow-stack-unwind.h | 51 ++++++++++++++++++++++-- > > libgcc/unwind-generic.h | 2 +- > > libgcc/unwind.inc | 4 +- > > 3 files changed, 50 insertions(+), 7 deletions(-) > > > > diff --git a/libgcc/config/i386/shadow-stack-unwind.h b/libgcc/config/i386/shadow-stack-unwind.h > > index 2b02682bdae..89d44165000 100644 > > --- a/libgcc/config/i386/shadow-stack-unwind.h > > +++ b/libgcc/config/i386/shadow-stack-unwind.h > > @@ -54,10 +54,39 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see > > aligned. If the original shadow stack is 8 byte aligned, we just > > need to pop 2 slots, one restore token, from shadow stack. Otherwise, > > we need to pop 3 slots, one restore token + 4 byte padding, from > > - shadow stack. */ > > -#ifndef __x86_64__ > > + shadow stack. > > + > > + When popping a stack frame, we compare the return address on normal > > + stack against the return address on shadow stack. If they don't match, > > + return _URC_FATAL_PHASE2_ERROR for the corrupted return address on > > + normal stack. Don't check the return address for > > + 1. Non-catchable exception where exception_class == 0. Process will > > + be terminated. > > + 2. Zero return address which marks the outermost stack frame. > > + 3. Signal stack frame since kernel puts a restore token on shadow > > + stack. > > + */ > > #undef _Unwind_Frames_Increment > > -#define _Unwind_Frames_Increment(context, frames) \ > > +#ifdef __x86_64__ > > +#define _Unwind_Frames_Increment(exc, context, frames) \ > > + { \ > > + frames++; \ > > + if (exc->exception_class != 0 \ > > + && _Unwind_GetIP (context) != 0 \ > > + && !_Unwind_IsSignalFrame (context)) \ > > + { \ > > + _Unwind_Word ssp = _get_ssp (); \ > > + if (ssp != 0) \ > > + { \ > > + ssp += 8 * frames; \ > > + _Unwind_Word ra = *(_Unwind_Word *) ssp; \ > > + if (ra != _Unwind_GetIP (context)) \ > > + return _URC_FATAL_PHASE2_ERROR; \ > > + } \ > > + } \ > > + } > > +#else > > +#define _Unwind_Frames_Increment(exc, context, frames) \ > > if (_Unwind_IsSignalFrame (context)) \ > > do \ > > { \ > > @@ -83,5 +112,19 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see > > } \ > > while (0); \ > > else \ > > - frames++; > > + { \ > > + frames++; \ > > + if (exc->exception_class != 0 \ > > + && _Unwind_GetIP (context) != 0) \ > > + { \ > > + _Unwind_Word ssp = _get_ssp (); \ > > + if (ssp != 0) \ > > + { \ > > + ssp += 4 * frames; \ > > + _Unwind_Word ra = *(_Unwind_Word *) ssp; \ > > + if (ra != _Unwind_GetIP (context)) \ > > + return _URC_FATAL_PHASE2_ERROR; \ > > + } \ > > + } \ > > + } > > #endif > > diff --git a/libgcc/unwind-generic.h b/libgcc/unwind-generic.h > > index a87c9b3ccf6..bf721282d03 100644 > > --- a/libgcc/unwind-generic.h > > +++ b/libgcc/unwind-generic.h > > @@ -292,6 +292,6 @@ EXCEPTION_DISPOSITION _GCC_specific_handler (PEXCEPTION_RECORD, void *, > > #define _Unwind_Frames_Extra(frames) > > > > /* Increment frame count. */ > > -#define _Unwind_Frames_Increment(context, frames) frames++ > > +#define _Unwind_Frames_Increment(exc, context, frames) frames++ > > > > #endif /* unwind.h */ > > diff --git a/libgcc/unwind.inc b/libgcc/unwind.inc > > index 5efd8af1b15..a7111a7b3a8 100644 > > --- a/libgcc/unwind.inc > > +++ b/libgcc/unwind.inc > > @@ -73,7 +73,7 @@ _Unwind_RaiseException_Phase2(struct _Unwind_Exception *exc, > > gcc_assert (!match_handler); > > > > uw_update_context (context, &fs); > > - _Unwind_Frames_Increment (context, frames); > > + _Unwind_Frames_Increment (exc, context, frames); > > } > > > > *frames_p = frames; > > @@ -191,7 +191,7 @@ _Unwind_ForcedUnwind_Phase2 (struct _Unwind_Exception *exc, > > /* Update cur_context to describe the same frame as fs, and discard > > the previous context if necessary. */ > > uw_advance_context (context, &fs); > > - _Unwind_Frames_Increment (context, frames); > > + _Unwind_Frames_Increment (exc, context, frames); > > } > > > > *frames_p = frames; > > -- > > 2.37.3 > > > > PING. Jeff has approved the generic changes. > > Thanks. > > -- > H.J. -- BR, Hongtao