Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp3318805iob; Mon, 16 May 2022 19:14:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxaC51pGUWjOnJwaC11B+rVBxGzQUrQMx/aHl/nPoRVHOa1bRrfaFMBi9jnj6YW72RMaKVl X-Received: by 2002:a17:902:ba97:b0:161:524d:5adb with SMTP id k23-20020a170902ba9700b00161524d5adbmr13078409pls.126.1652753676361; Mon, 16 May 2022 19:14:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652753676; cv=none; d=google.com; s=arc-20160816; b=JqZPHlYjt+HsWAZEbvflwEwURgf+FpVyaW/zO5nAHjEejgpLWOydsKJO1r5xmZO179 3dOXmC+ghmGOAwOj2ZGy/6HCwa558Y5wFa0I7rTvU3bhmaIj/X283Qp3FkVn7PhHjfAN XtPP1sXgcZMa570+6IJYXHN1pgTxpMnlNAtKE1xxpvUsRr5kLCx1gMO52csd/9f9DsH3 F5UPL9sQGcPQPuBAYY2d8Ly2Gj515RW/gpPx2fxvY5lmz4QDYdK+/tOl2iBMUVcQLCU/ FsjVYQ07rMEvbuApS7JNFjRWTNUBgHrz9Cc5LWoJcJ+qxbNbXTsj/q0p7Typ20IMbnxA wWag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=qH80CkGInLvkMX2JFapXe6/mirbCbTT5IsB0Ewh+rp8=; b=PcYswqcn90reGyDUUhH/bhaBwmYNQNvt/e0SkS4PGboKXMmC3P7XIh94hXZt2KMI3l 75BU2R22sSlTThyduPTPPmcZPoDH+foGpDjLGtN9BmCmUTsjf8uJaFmEe8+kz+x81u92 cOHidSRaQgAJD52eD/tJuAPmgbQH+RlIAzL5ORN03LV9BHeZDn//rdI90qyMdVhUU1sA HQ6DPoi2E6BFQaLZCUIhJsTn75hp2suebuc03qykXeFXH37CQ0sVsDxiSMGnZ9sL8GuT VxHdSvUvNvfCo600f39mq8MHMdFBVUoNub2FqyVvKT1jSMP2WMmoIpLsWU0K1+tP92xd sNJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=TOCV1Wq+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z8-20020a17090a540800b001df6ce033fasi1137218pjh.57.2022.05.16.19.14.23; Mon, 16 May 2022 19:14:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=TOCV1Wq+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350146AbiEPXBJ (ORCPT + 99 others); Mon, 16 May 2022 19:01:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350136AbiEPXAv (ORCPT ); Mon, 16 May 2022 19:00:51 -0400 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 589E747065 for ; Mon, 16 May 2022 15:59:43 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id w17-20020a17090a529100b001db302efed6so713661pjh.4 for ; Mon, 16 May 2022 15:59:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=qH80CkGInLvkMX2JFapXe6/mirbCbTT5IsB0Ewh+rp8=; b=TOCV1Wq+ft6PCF6l/iA42zY/3oD/IM0GOcYS+7TP3YJrbCBQoBpGYcRJ4FxvAYjGS8 Gg7ZH626PyNKxe6gsPGFcJ6V3ZdRL/vPUeoqYVEMEWtWXHFCjfx4Ox5QLjjGI8779p5h T+G2RVdvA3BaNACIhLLpoN8MhXMLEVheXyyvU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=qH80CkGInLvkMX2JFapXe6/mirbCbTT5IsB0Ewh+rp8=; b=ReM5RxskzVHT8Jg2dJLTrW4FS+1tHjgn+MwelISEYSJolVA3Be/U+Tm5MYlc80TqGJ f2h2q3Eesww2Cpv8rXH0SV/MGNydHRl9DQV/hDhiryAV3R8JH/Aekv6CxCCF682Zv18T Qe/jQzt3SuiT9hlsq+H+OmjSHMzZrloCiUsZb4u2i3p4eacND0irE9eCKGEf3u38dOpz NPzmyZikYyyCAFdPeQnB2H9TqVfFJb4bJkp7w6W3EzphJu3T16X4Nh1XLDsrVeXkr7mv IJ5X7D2DG+m/Oq/pda5NU10skLHzCWJyji4AkURDdgF4R6UdI3qQlmQYMSYVRFd3TcSF KCVA== X-Gm-Message-State: AOAM530c8bRhgycb1EKdmoEXmQ3SuUNOSQbjlbZef8zajnNSY/a3ltAA 87/6yPs5ymvqYPHvCO9f5hEYXA== X-Received: by 2002:a17:90b:3b42:b0:1dc:5cdf:5649 with SMTP id ot2-20020a17090b3b4200b001dc5cdf5649mr33105926pjb.239.1652741982280; Mon, 16 May 2022 15:59:42 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id e16-20020a17090301d000b0015f2d549b46sm7535303plh.237.2022.05.16.15.59.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 May 2022 15:59:41 -0700 (PDT) Date: Mon, 16 May 2022 15:59:41 -0700 From: Kees Cook To: Peter Zijlstra Cc: Sami Tolvanen , linux-kernel@vger.kernel.org, Josh Poimboeuf , x86@kernel.org, Catalin Marinas , Will Deacon , Mark Rutland , Nathan Chancellor , Nick Desaulniers , Joao Moreira , Sedat Dilek , Steven Rostedt , linux-hardening@vger.kernel.org, linux-arm-kernel@lists.infradead.org, llvm@lists.linux.dev Subject: Re: [RFC PATCH v2 20/21] x86: Add support for CONFIG_CFI_CLANG Message-ID: <202205161531.3339CA95@keescook> References: <20220513202159.1550547-1-samitolvanen@google.com> <20220513202159.1550547-21-samitolvanen@google.com> <20220516183047.GM76023@worktop.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220516183047.GM76023@worktop.programming.kicks-ass.net> X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, May 16, 2022 at 08:30:47PM +0200, Peter Zijlstra wrote: > On Mon, May 16, 2022 at 10:15:00AM -0700, Sami Tolvanen wrote: > > On Mon, May 16, 2022 at 2:54 AM Peter Zijlstra wrote: > > > > > > On Fri, May 13, 2022 at 01:21:58PM -0700, Sami Tolvanen wrote: > > > > With CONFIG_CFI_CLANG, the compiler injects a type preamble > > > > immediately before each function and a check to validate the target > > > > function type before indirect calls: > > > > > > > > ; type preamble > > > > __cfi_function: > > > > int3 > > > > int3 > > > > mov , %eax > > > > int3 > > > > int3 > > > > function: > > > > ... > > > > > > When I enable CFI_CLANG and X86_KERNEL_IBT I get: > > > > > > 0000000000000c80 <__cfi_io_schedule_timeout>: > > > c80: cc int3 > > > c81: cc int3 > > > c82: b8 b5 b1 39 b3 mov $0xb339b1b5,%eax > > > c87: cc int3 > > > c88: cc int3 > > > > > > 0000000000000c89 : > > > c89: f3 0f 1e fa endbr64 > > > > > > > > > That seems unfortunate. Would it be possible to get an additional > > > compiler option to suppress the endbr for all symbols that get a __cfi_ > > > preaamble? > > > > What's the concern with the endbr? Dropping it would currently break > > the CFI+IBT combination on newer hardware, no? > > Well, yes, but also that combination isn't very interesting. See, > > https://lore.kernel.org/all/20220420004241.2093-1-joao@overdrivepizza.com/T/#m5d67fb010d488b2f8eee33f1eb39d12f769e4ad2 > > and the patch I did down-thread: > > https://lkml.kernel.org/r/YoJKhHluN4n0kZDm@hirez.programming.kicks-ass.net > > If we have IBT, then FineIBT is a much better option than kCFI+IBT. I'm still not convinced about this, but I'm on the fence. Cons: - FineIBT does callee-based hash verification, which means any attacker-constructed memory region just has to have an endbr and nops at "shellcode - 9". KCFI would need the region to have the hash at "shellcode - 6" and an endbr at "shellcode". However, that hash is well known, so it's not much protection. - Potential performance hit due to making an additional "call" outside the cache lines of both caller and callee. Pros: - FineIBT can be done without read access to the kernel text, which will be nice in the exec-only future. I'd kind of like the "dynamic FineIBT conversion" to be a config option, at least at first. We could at least do performance comparisons between them. > Removing that superfluous endbr also shrinks the whole thing by 4 bytes. > > So I'm fine with the compiler generating working code for that > combination; but please get me an option to supress it in order to save > those pointless bytes. All this CFI stuff is enough bloat as it is. So, in the case of "built for IBT but running on a system without IBT", no rewrite happens, and no endbr is present (i.e. address-taken functions have endbr emission suppressed)? Stock kernel build: function: [normal code] caller: call __x86_indirect_thunk_r11 IBT kernel build: function: endbr [normal code] caller: call __x86_indirect_thunk_r11 CFI kernel build: __cfi_function: [int3/mov/int3 preamble] function: [normal code] caller: cmpl \hash, -6(%r11) je .Ltmp1 ud2 .Ltmp1: call __x86_indirect_thunk_r11 CFI+IBT kernel build: __cfi_function: [int3/mov/int3 preamble] function: endbr [normal code] caller: cmpl \hash, -6(%r11) je .Ltmp1 ud2 .Ltmp1: call __x86_indirect_thunk_r11 CFI+IBT+FineIBT kernel build: __cfi_function: [int3/mov/int3 preamble] function: /* no endbr emitted */ [normal code] caller: cmpl \hash, -6(%r11) je .Ltmp1 ud2 .Ltmp1: call __x86_indirect_thunk_r11 at boot, if IBT is detected: - replace __cfi_function with: endbr call __fineibt_\hash - replace caller with: movl \hash, %r10d sub $9, %r11 nop2 call *%r11 - inject all the __fineibt_\hash elements via module_alloc() __fineibt_\hash: xor \hash, %r10 jz 1f ud2 1: ret int3 -- Kees Cook