Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp668692pxb; Fri, 16 Apr 2021 15:21:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJykqDvifU64tXZlC7+Q4gO5QaAfh+LjcCh1fAfS427r27ybjTITkI7yuwgZeDIes76sOnLp X-Received: by 2002:a05:6402:706:: with SMTP id w6mr5653911edx.15.1618611708392; Fri, 16 Apr 2021 15:21:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618611708; cv=none; d=google.com; s=arc-20160816; b=JGXGu+yXysOdrykhGjOoQY2baJR4tX+1zvtrDKrfFdGLNAASO28CBhXXJx9X1Fv6PH DZMTSAb5jTPZyqK9NOMwyJm2KG4wgros7ud/kAVyZcpkojpOwvx6j2yh91CiSfIWvKpQ WD7ms6LHRowjzw8FMFWrX5lddlChA7llkTdmYZQRNQjEZ9v54wM6eu+xWnZNyEZUS9v2 ElXxiHOKfRHr3JignZs4fp5S8LHuGvzN0TmL1At49T2E2Wb1CmBqO6Ax0YHn8vRHVVHC GRU2EAfsH2gACV1Lz48iXOB1iDRJDj0pNVFGwu9wlYn2rqKcTw9bIx9Gm+zWMlTjRhEd Nh+w== 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=1Y8X/9tzKNDBLbhj4hdWEjv4HZ2ztBOxnrwMv8b5hbk=; b=gozxWisaaEozLjKQiMmCMHnupt8raG16UYTDE7RtTAL3zxCO3kj3ZJZzjaX9Q+r9Ge jFTG0qdoDrmaiEUk2v5Dpma5BMIEJh1IghRG2IVdrJ5zkqCSBdnTXaO4/jMOU+ByHVa6 SoJYDqOpnHk+rNXiHvQk/uocLfUA4A2pnkPl1uSoueoOxRSLgAyXICR4LSm6VGcfh/u8 UhnVgJjXZnh3I03JqSbBx5wEl1+/m0hyIqtNhRDwfzer8E19sEeXfcuFMDBloXdFeJVl YNlJQ9ziIWFaFYdVK42mJynGBmLaqLk5MkQxwfwB6wQL4jvXDnBrTMYGRVjpUo+gc0hi 2mOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jxKsP0Fr; 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 m3si5696552edr.251.2021.04.16.15.21.25; Fri, 16 Apr 2021 15:21:48 -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=jxKsP0Fr; 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 S233822AbhDPWU4 (ORCPT + 99 others); Fri, 16 Apr 2021 18:20:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:48180 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231510AbhDPWUy (ORCPT ); Fri, 16 Apr 2021 18:20:54 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8D71E613D3 for ; Fri, 16 Apr 2021 22:20:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1618611629; bh=cw+o8cfEuCo6PQdHXEsdN0LoqovEdVoWI1b2P0UF/Og=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=jxKsP0Fr9kLKdD37KZFr2gu8uqJ4eMaAup4AizAaAIdv9BKvzqPDEgdgbRBqum7zC lWKQeigSIzxGjTJHEiwDs9xOo/IJUbEib4DA3B84aEQ5lLth2jUfE9lUj4h8QdAD1E 9Z2obm0iE+pEWoFyv7etI8gGBQtinezT/unZfPuCAuFvRIfPS4bKZ7yN9BeXLOXjNl lVBK12qMeYLlfofHvBxBmpPC1+zZX59BpJkWseUTGLjyQuWLncZGNvRiMC4y2Qys4I lhxVTpq9Aa87Zkp9Jcnq1KIFqAcjpNd7gurH56WiNZd/0MotMjImyhskUQyILS3pOl CQiDJVampjCng== Received: by mail-ed1-f51.google.com with SMTP id w18so34081898edc.0 for ; Fri, 16 Apr 2021 15:20:29 -0700 (PDT) X-Gm-Message-State: AOAM531PLaZ6BNfptW3V3jSgRBcOtDtfmcpS9+OK/xww1M8PBrcxqoY9 Gr9kofvlmXT2ePT9rj2rHEYGl3Z1krF28BnSYNMQsw== X-Received: by 2002:a50:f395:: with SMTP id g21mr12764633edm.238.1618611628139; Fri, 16 Apr 2021 15:20:28 -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> <20210416221414.GF22348@zn.tnic> In-Reply-To: <20210416221414.GF22348@zn.tnic> From: Andy Lutomirski Date: Fri, 16 Apr 2021 15:20:17 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 05/15] x86: Implement function_nocfi To: Borislav Petkov Cc: Andy Lutomirski , Sami Tolvanen , X86 ML , Kees Cook , 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:14 PM Borislav Petkov 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 > > I'm just reading the patch: > > ... The function_nocfi macro always returns the address of the > + * actual function instead. > + */ > +#define function_nocfi(x) ({ \ > + void *addr; \ > + asm("leaq " __stringify(x) "(%%rip), %0\n\t" : "=r" (addr)); \ > + addr; > > so it does a rip-relative load into a reg which ends up with the function > address. This is horrible. We made a mistake adapting the kernel to GCC's nonsensical stack protector ABI, especially on 32-bit, instead of making GCC fix it. Let's not repeat this with clang please. Sami, I'm assuming that: extern void func(void); results in anything that takes a pointer to func getting a pointer to some special magic descriptor instead of to func, so that: void (*ptr)(void); ptr = func; ptr(); does the right thing. Then void (*)(void) is no longer a raw pointer. Fine. But obviously there is code that needs real function pointers. How about making this a first-class feature, or at least hacking around it more cleanly. For example, what does this do: char entry_whatever[]; wrmsrl(..., (unsigned long)entry_whatever); or, alternatively, extern void func() __attribute__((nocfi)); void (*ptr)(void); ptr = func; /* probably fails to compile -- invalid conversion */ (unsigned long)func /* returns the actual pointer */ func(); /* works like normal */ And maybe allow this too: void (*ptr)(void) __attribute__((nocfi); ptr = func; ptr(); /* emits an unchecked call. maybe warns, too. anyone who does this needs to be extremely careful. */ --Andy