Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp4507610pxb; Tue, 2 Nov 2021 10:49:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyiqardDW1fnpTSaFNCU8yGanCHEC3MZy2+fuMGd4ia7V03imoW85GSHEf+7+RjqZfswCsz X-Received: by 2002:a17:907:6291:: with SMTP id nd17mr46432587ejc.194.1635875372916; Tue, 02 Nov 2021 10:49:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635875372; cv=none; d=google.com; s=arc-20160816; b=aUwOiJMlYg0RytzffDloWSv8zIzJZ+nV7/byp1GyJb/kB167glbCN3O1VItdjQf6JF WzT+E0pwJdrg9pYYqaSZQrXIrL6mt1epyycF703CyWcZMwjIV52sPQvv649iYyJgweOq X34Ubjtsmi9a7bjuzJbgeHDp2EJbRTl1QU0AqImnOcP7qAi9bipP+wJBsPy6Twlsdx8e kTWsSONR6lj3co3pYSONmGyWjXN0SAHXPR3uzMN1AYp5TwnestGcffKtXM9kxjHk0gGl jca9wN2arBxmc0YujGeKgT8N8RX/+HOYnZGqpqctlYE8eLLQIotW6/1opYto0G/5Jixb HOmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=zVyRKF00SPBusKKPU9+4hgeYM+6L4aad7S/z4U5vlDI=; b=CVWl22kqz/H9WUeCRjk6FRvvJM9s+J8vq20EO4f8zu9h+XGxiOc/4PnoZwM0rIsXIm anYsY60x+cadRUCtQy8qJ33NYVdC2ytPgfEd4GYqdupSxh5n4+/xZo4PlmBqMelzRdsu VWEo5b/YYK9SaybxKuRyvLFUyauqyxLGnjbB0StOG+EARgoawCxHtlOriVcjgmHfkqoE 2WTZLi9HAcMb77n0lEwxnRXw7GUIH2t/Mijtfpg3+FjF7+1Qa1PGX5T9ctNy7ahFehSc IVUsm1h5T9OF6sU8EsSPX/qnvHuGK338rvfaPb0vi/PNxMA5Q4kyxAOelQ7Omi/hAI5j x1Rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JrTNH7Kt; spf=pass (google.com: 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q18si12827726ejf.639.2021.11.02.10.49.09; Tue, 02 Nov 2021 10:49:32 -0700 (PDT) Received-SPF: pass (google.com: 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=@kernel.org header.s=k20201202 header.b=JrTNH7Kt; spf=pass (google.com: 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234557AbhKBRrp (ORCPT + 99 others); Tue, 2 Nov 2021 13:47:45 -0400 Received: from mail.kernel.org ([198.145.29.99]:43224 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234183AbhKBRro (ORCPT ); Tue, 2 Nov 2021 13:47:44 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 45BE460E78; Tue, 2 Nov 2021 17:45:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1635875109; bh=yWjcLlrvgIGtTAiG1zZiA6MB6dlr4uGs44gsa0DhfcU=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=JrTNH7KtDSdoJkcNTJywHXN25W0vuQJMdUtC4NjMV13YC/sSZDjs8+ySNzelWwpOK K8hb6VLzvZxqlYZZWJ3U3Vb6HkeTHqQQ09CaxYSTL9X6T9jFKiDxXeL9ePopdUmSIA Rrfe95VE9NAjURH+08rFNaEGYIt6slNtIXGpFPAmD5lMsPOHHo+07d64LFNCMkuL1y KVaj+owdcC4LhI9diyB059pfRR4acfoqNUL7LjqeBl2655ladKAG/75ff4Xx9K8fkB VAY8fUIJERKM3AULK6fsIyjykDEiZdapq34o0MPczEN80mSkOlzQVN8VSuxiNOz9Et EmxIM5qrsDdmA== Received: by mail-ot1-f48.google.com with SMTP id q33-20020a056830442100b0055abeab1e9aso1998459otv.7; Tue, 02 Nov 2021 10:45:09 -0700 (PDT) X-Gm-Message-State: AOAM530QleEtDYVhQ5HClUYCzRsytOAIqdsGcnxehwsu2cc9Sv2Xtp34 06TIygVgdbnfWEZ0mShhuSaDAZht7EVDHw5yV7s= X-Received: by 2002:a05:6830:1d6e:: with SMTP id l14mr27846692oti.147.1635875108561; Tue, 02 Nov 2021 10:45:08 -0700 (PDT) MIME-Version: 1.0 References: <20211031163920.GV174703@worktop.programming.kicks-ass.net> <20211101090155.GW174703@worktop.programming.kicks-ass.net> In-Reply-To: From: Ard Biesheuvel Date: Tue, 2 Nov 2021 18:44:56 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] static_call,x86: Robustify trampoline patching To: Peter Zijlstra Cc: Sami Tolvanen , Mark Rutland , X86 ML , Kees Cook , Josh Poimboeuf , Nathan Chancellor , Nick Desaulniers , Sedat Dilek , Steven Rostedt , linux-hardening@vger.kernel.org, Linux Kernel Mailing List , llvm@lists.linux.dev, joao@overdrivepizza.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2 Nov 2021 at 16:15, Peter Zijlstra wrote: > > On Tue, Nov 02, 2021 at 01:57:44PM +0100, Peter Zijlstra wrote: > > > So how insane is something like this, have each function: > > > > foo.cfi: > > endbr64 > > xorl $0xdeadbeef, %r10d > > jz foo > > ud2 > > nop # make it 16 bytes > > foo: > > # actual function text goes here > > > > > > And for each hash have two thunks: > > > > > > # arg: r11 > > # clobbers: r10, r11 > > __x86_indirect_cfi_deadbeef: > > movl -9(%r11), %r10 # immediate in foo.cfi > > xorl $0xdeadbeef, %r10 # our immediate > > jz 1f > > ud2 > > 1: ALTERNATIVE_2 "jmp *%r11", > > "jmp __x86_indirect_thunk_r11", X86_FEATURE_RETPOLINE > > "lfence; jmp *%r11", X86_FEATURE_RETPOLINE_AMD > > So are these supposed to go into the jump tables? If so, there still needs to be a check against the boundary of the table at the call site, to ensure that we are not calling something that we shouldn't. If they are not going into the jump tables, I don't see the point of having them, as only happy flow/uncomprised code would bother to use them. > > > > > > # arg: r11 > > # clobbers: r10, r11 > > __x86_indirect_ibt_deadbeef: > > movl $0xdeadbeef, %r10 > > subq $0x10, %r11 > > ALTERNATIVE "", "lfence", X86_FEATURE_RETPOLINE > > jmp *%r11 > > > > These two thunks could of course be one big alternative. > > > And have the actual indirect callsite look like: > > > > # r11 - &foo > > ALTERNATIVE_2 "cs call __x86_indirect_thunk_r11", > > "cs call __x86_indirect_cfi_deadbeef", X86_FEATURE_CFI > > "cs call __x86_indirect_ibt_deadbeef", X86_FEATURE_IBT > > Also simplifying this. > > > Although if the compiler were to emit: > > > > cs call __x86_indirect_cfi_deadbeef > > > > we could probaly fix it up from there. > > > > > > Then we can at runtime decide between: > > > > {!cfi, cfi, ibt} x {!retpoline, retpoline, retpoline-amd} > >