Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp685775pxb; Fri, 16 Apr 2021 15:55:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx3MZmGc8LI4IwVYp4+MqQ9tTG6mLY7BO/zSRCJp+xf6i/tYFhJmnrf2y/zGVn7NMzvE9zi X-Received: by 2002:aa7:c1c9:: with SMTP id d9mr12443269edp.155.1618613706742; Fri, 16 Apr 2021 15:55:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618613706; cv=none; d=google.com; s=arc-20160816; b=EjD1dUQfeizq+vF4adanfjZGMH2E5OfX+b8U9qBQv/jwqXL0xO2IC3qmuxaVO323/4 2fw/XYtqWVUHXz31SAMx7LHoweTzorGDzE6PpU3YzGT5YqfCzwKjYGXQ2VLIi/yVM+uj A+vVrfIOMfTurtX73OwnqxtAbsrOLylFJ3tCEwU8Gx/isMyauIStxMksVl7MgnF6D2+l b1xf6r1FtyJwwHw6RQLS7X66IbVOlCwzZS8j/52moOEUpdUZOb9pTaJp+MVM3LOQ1T8L Y80RovttWeYHNiIDf7yxEOkgooJgDzA2gzvcvjQF3ZfQ0etUThBe9xj7KZPPd2zIVqsO z6hg== 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=WiJrIM8nhHQwH+PZxGueXKdC6EEh9H2HinOOF/AhrBk=; b=D/D66tUm7MHDbY0hrIxe1HaphPpZYBv4RS2jCobA7vT0SVezjy5QCwapTOFMbPaWD1 35jZSRBOK8VrS9/hb58kX1GOuwvRkM/nopfYkO7R/sEWdwC5/yQ4kNrG0gFUqkAg+nZj HIwszK+vY3QCfQXHTYuBWRs2l0z+LOgpu60x6wkTtZ9nAY6K/K2Bz56eJO/H79QEBdpK gz9QwpdgsQ8dpg8+TuwlCNPS6xZOVBLxXnpNZPw5uMpv+/Az+rYY951YI1zVLwgNA/Oc TkOWcVHDA1SwuyaJgpMxAi6e62+qcO4gPCKv7+UETsdiIaeU300njAtS/jNbBzVW9DcR k/Ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Fslg4x88; 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 ca23si5973322edb.92.2021.04.16.15.54.43; Fri, 16 Apr 2021 15:55:06 -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=Fslg4x88; 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 S230489AbhDPWxX (ORCPT + 99 others); Fri, 16 Apr 2021 18:53:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:54554 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231666AbhDPWxW (ORCPT ); Fri, 16 Apr 2021 18:53:22 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 92296613C3 for ; Fri, 16 Apr 2021 22:52:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1618613577; bh=Z82q1BpPPjb/4+mIJ2BwX5XB1pq+e///2Kil21+66OE=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Fslg4x88dgjvn5+aUKFtTvfE91E7eFDrSNke+y7kGUBhxCs/24D2TSpYYiK9shHkq J5IlTcHp3puivOto80kYcaUDwXXQnvKUwdV7tgcVS98dpC/Xq1WvtkDX9Z1+CJpM78 +o3wPx/l5q4OwhRGOteBLITcZw4eLqvYARAZk2yAFYFz89tQxpE3N0lib26s7VLYGp q++U6noVR8YEgEZhs5vLfcdMSnb6qHAewsggpy+ix/jVq5BpZ0EbhG8Rh25CMXX6d3 G9EtP9wONnnjuMHWm1J5dGUMOEjJZG41uxOJl8iiV/H2DzdJxti+VXu/3feK3BHFoU 8LifSWZqesy7Q== Received: by mail-ej1-f44.google.com with SMTP id l4so44271111ejc.10 for ; Fri, 16 Apr 2021 15:52:57 -0700 (PDT) X-Gm-Message-State: AOAM532T3UEK6zzWQngsszyr+TBl/PFLAO1r1dQqoqdWKIxP9EQWx5rA Db98pDxWk1MqNBnB56qk1Ywa86tktgqCiNUHd9c+iA== X-Received: by 2002:a17:906:35ca:: with SMTP id p10mr10739772ejb.199.1618613576049; Fri, 16 Apr 2021 15:52:56 -0700 (PDT) MIME-Version: 1.0 References: <20210416203844.3803177-1-samitolvanen@google.com> <20210416203844.3803177-6-samitolvanen@google.com> <20210416211855.GD22348@zn.tnic> <20210416220251.GE22348@zn.tnic> <202104161519.1D37B6D26@keescook> In-Reply-To: <202104161519.1D37B6D26@keescook> From: Andy Lutomirski Date: Fri, 16 Apr 2021 15:52:44 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 05/15] x86: Implement function_nocfi To: Kees Cook Cc: Andy Lutomirski , Borislav Petkov , Sami Tolvanen , X86 ML , Josh Poimboeuf , Peter Zijlstra , Nathan Chancellor , Nick Desaulniers , Sedat Dilek , linux-hardening@vger.kernel.org, LKML , clang-built-linux Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 16, 2021 at 3:28 PM Kees Cook wrote: > > On Fri, Apr 16, 2021 at 03:06:17PM -0700, Andy Lutomirski wrote: > > On Fri, Apr 16, 2021 at 3:03 PM Borislav Petkov wrote: > > > > > > On Fri, Apr 16, 2021 at 02:49:23PM -0700, Sami Tolvanen wrote: > > > > __nocfi only disables CFI checking in a function, the compiler still > > > > changes function addresses to point to the CFI jump table, which is > > > > why we need function_nocfi(). > > > > > > So call it __func_addr() or get_function_addr() or so, so that at least > > > it is clear what this does. > > > > > > > This seems backwards to me. If I do: > > > > extern void foo(some signature); > > > > then I would, perhaps naively, expect foo to be the actual symbol that > > gets called > > Yes. > > > and for the ABI to be changed to do the CFI checks. > > Uh, no? There's no ABI change -- indirect calls are changed to do the > checking. Maybe ABI is the wrong word, or maybe I'm not fully clued in. But, if I do: extern void call_it(void (*ptr)(void)); and I define call_it in one translation unit and call it from another, the ABI effectively changed, right? Because ptr is (depending on the "canonical" mode) no longer a regular function pointer. > > char entry_whatever[]; > > wrmsrl(..., (unsigned long)entry_whatever); > > This is just casting. It'll still resolve to the jump table entry. How? As far as clang is concerned, entry_whatever isn't a function at all. What jump table entry? > > > or, alternatively, > > > > extern void func() __attribute__((nocfi)); > > __nocfi says func() should not perform checking of correct jump table > membership for indirect calls. > > But we don't want a global marking for a function to be ignored by CFI; > we don't want functions to escape CFI -- we want specific _users_ to > either not check CFI for indirect calls (__nocfi) or we want specific > passed addresses to avoid going through the jump table > (function_nocfi()). Maybe I spelled it badly, and I maybe I requested the wrong thing. Here are actual required use cases. 1. I defined a function in asm. I want to tell clang that this function is defined in asm, and for clang to behave accordingly: .globl func func: ; do stuff later: extern void func(void) [something here]; There really should be a way to write this correctly such that it works regardless of the setting of -fsanitize-cfi-canonical-jump-tables. This should not bypass CFI. It should *work*, with CFI enforced. If I read all the various things you linked correctly, this would be something like __cfi_noncanonical, and I reserve the right to think that this is a horrible name. 2. I need a raw function pointer, thank you very much. I would like to be honest about it, and I don't really want to bypass CFI, but I need the actual bits in the actual symbol. translation unit 1 defines func. Maybe it's C with -fsanitize-cfi-canonical-jump-tables, maybe it's C with -fno-sanitize-cfi-canonical-jump-tables or however it's spelled, and maybe it's plain asm. Now translation unit 2 does: 2a. Uses a literal symbol, because it's going to modify function text or poke an MSR or whatever: wrmsrl(MSR_WHATEVER, func); clang needs to give us *some* way to have a correct declaration of func such that we can, without resorting to inline asm kludges, get the actual bit pattern of the actual symbol. 2b. Maybe optional: convert from function pointer to bit pattern of actual symbol. If someone gives me a real, correctly typed C pointer representing a function pointer, I want a way to find the address of the body of the function. Then we can use it for things that aren't *calling* it per se, e.g. disassembling it. This is not necessarily a fully formed thought right now, but I think something along these lines might be needed. The reverse of 2b (converting from actual symbol to function pointer) might be equivalent to 1, or it might not. I suppose there are some subtleties here. Be that as it may, it sounds like right now clang has some issues interoperating with asm when CFI is enabled. If so, clang needs to be improved. (The unsigned long hack is not necessarily good enough. I should be able to do: .global func func: ; C ABI compliant code here extern void func(void) [attribute as in 1] unsigned long actual_address = [something clang actually understands](func); If this thing refuses to work when fed a nonconstant function pointer because of some genuinely good reason, fine. But, if 'func' is an actual literal symbol name, this thing needs to be compile-time constant expression. > > So, instead of a cast, a wrapper is used to bypass instrumentation in > the very few cases its needed. NAK. The compiler needs to cooperate IMO. > > (Note that such a wrapper is no-op without CFI enabled.) > But note that this shouldn't turn into a discussion of "maybe Clang could > do CFI differently"; this is what Clang has. > > https://clang.llvm.org/docs/ControlFlowIntegrity.html If this is what Clang has, and Clang won't improve, then we can just not apply these patches...