Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp928559pxb; Fri, 22 Apr 2022 14:34:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzKl9F6x8KWu4+bCKFRhkviQnE06l2isfomyeiIf9F+Gqm+xTIBDndjy9tMBZsBDiaYakSy X-Received: by 2002:a17:902:db08:b0:15b:f17a:dc96 with SMTP id m8-20020a170902db0800b0015bf17adc96mr5256523plx.65.1650663248101; Fri, 22 Apr 2022 14:34:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650663248; cv=none; d=google.com; s=arc-20160816; b=dZ/iUK/ZMRZOO+nvBGLfMCHI9OhSEd1iMsbwm14HDGwM6Lg6O8n+YDdMhgsDDuGBZ0 rG8caPt9+P5fPJR6+bqKglyeoLIByYJ2pAqIfAo0AAdCNYKmiBZCvIZ4FtTITU5cThmB tRsZA4yR/d/LFpa0yBh5R87+WfIePdpVLJrYHFo/S8sxHzl/N8zSju9QDyzSqO5Bwf5w tCQ2LIgOUC+Iy1VR5BGFO7n8dxXS7G3hk/vS4C5UlktoQRM+57l4RfqBmogyTHwhHC9p qJSj7myiC7WTLswilhiY4Pha458qxoRSzr80W6tuj7vC89oUrWH4odhNSY/RJtlrWB9M 2vqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:message-id:references :in-reply-to:subject:cc:to:from:date:mime-version; bh=E6wQTS/jvlXC0/qqWtqEmB2VV3JycaDN3e0iQo/J3gE=; b=c8KjuPDdj+N0tVCxNEv+JQ8oaNhXvs5vVEa8yAxC53DndzAfo4GyiDRXaf8J9YBhyr YW4I2rI3mA07Tsd3uy9M92+oC22TBCxg33k7YG6C1hNswnOWko07Mu69hW6AI7ne/XbU NSTifXxUs7WIIQcFVyjSFCYPSTvB3QoUC8IO1HnRtmpQboL9BPoB/tawAVmunmjT03ii BMwW6pGDRHU3ZlqxXvgOIhC7U9TBUGWjIV4zWxK0uyPKFTPN4R2I191Z9FG5qdM3SYsO ogsLsl7wiON1CnDiWBp61FXujEUWogrQtp1ZG/PJiUH0fty2BotaR+5WifBb+IrzJTJg 9Wbg== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id j3-20020a170903028300b001590504ef9fsi1342506plr.60.2022.04.22.14.34.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 14:34:07 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6560629A8F9; Fri, 22 Apr 2022 12:41:48 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1390015AbiDUP0F (ORCPT + 99 others); Thu, 21 Apr 2022 11:26:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1389997AbiDUP0D (ORCPT ); Thu, 21 Apr 2022 11:26:03 -0400 Received: from relay11.mail.gandi.net (relay11.mail.gandi.net [IPv6:2001:4b98:dc4:8::231]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5EA033F31A; Thu, 21 Apr 2022 08:23:13 -0700 (PDT) Received: (Authenticated sender: joao@overdrivepizza.com) by mail.gandi.net (Postfix) with ESMTPA id A4749100002; Thu, 21 Apr 2022 15:23:08 +0000 (UTC) MIME-Version: 1.0 Date: Thu, 21 Apr 2022 08:23:08 -0700 From: Joao Moreira To: Peter Zijlstra Cc: Nick Desaulniers , Josh Poimboeuf , hjl.tools@gmail.com, Fangrui Song , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, andrew.cooper3@citrix.com, keescook@chromium.org, samitolvanen@google.com, mark.rutland@arm.com, alyssa.milburn@linux.intel.com, gabriel.gomes@linux.intel.com, rick.p.edgecombe@intel.com Subject: Re: [RFC PATCH 00/11] Kernel FineIBT Support In-Reply-To: <20220421074920.GK2731@worktop.programming.kicks-ass.net> References: <20220420004241.2093-1-joao@overdrivepizza.com> <20220420074044.GC2731@worktop.programming.kicks-ass.net> <20220420151714.fderdz4dzea75rvg@treble> <20220421074920.GK2731@worktop.programming.kicks-ass.net> Message-ID: <643dac8c3ccb0720b4a8e67a472b08de@overdrivepizza.com> X-Sender: joao@overdrivepizza.com Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE autolearn=no 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 2022-04-21 00:49, Peter Zijlstra wrote: > On Wed, Apr 20, 2022 at 03:40:41PM -0700, Joao Moreira wrote: >> > > >> > > If FineIBT needs it, I could reconsider. But I think there's a strong >> > > case to be made that the linker should be doing that instead. >> > >> > That sounds reasonable to me (and reminds me of linker relaxation). >> > Joao, can you please work with Fangrui (LLD) and HJ (GNU binutils) to >> > determine how feasible this would be? I assume code outside the kernel >> > might enjoy such an optimization, too. When that's the case, then it >> > probably makes more sense to "upstream" such "optimizations" from the >> > kernel-specific objtool into the toolchains. >> >> Alright, these are the greenlights I was hoping for. >> >> I went quickly into this with HJ and he mentioned that it should be >> doable >> in the linker, and that he has a patch for it in gcc (for local >> function, >> from what I could see): >> https://gcc.gnu.org/pipermail/gcc-patches/2022-February/590832.html >> >> If @Fangrui is fine with it, I would like to try implementing this >> myself in >> lld (I'm still learning a lot about lld and having an actual problem >> to >> solve is the kind of fuel I need). Should take me a while, but I think >> this >> is not urgent, right? I can also go ahead and replicate HJ's gcc patch >> into >> clang, so we can also handle the local functions within the compiler >> (I >> think this makes a lot of sense). >> >> Once we have these in, I'll revisit FineIBT and extend the features to >> handle the FineIBT instrumentation. Hopefully we'll be released from >> needing >> objtool (famous last words?!). >> >> This sounds like a plan, but I'm ofc open to suggestions or different >> ideas/plans. > > So trivially the plan sounds like: compiler fixes STB_LOCAL because it > has the scope, and the linker fixes everything else. However, that > seems > to assume that !STB_LOCAL will have ENDBR. > > This latter isn't true; for one there's __attribute__((nocf_check)) > that > can be used to suppress ENDBR generation on a function. > > Alternatively the linker will need to 'read' the function to determine > if it has ENDBR, or we need to augment the ELF format such that we can > tell from that. > > So what exactly is the plan? I ran into too many broken dreams by trying to infer the presence of ENDBRs just by the symbol locality/linkage... not only because of the attribute, but also because of ancient assembly. So, my first thought was to use something similar to the __patchable_function_entries section (https://man7.org/linux/man-pages/man1/gcc.1.html), where we would have a section to mark all the placed ENDBR. But then it occurred to me that if we follow that road we'll miss the ENDBR placed in assembly unless we mark it manually, so I started thinking that reading the target instructions from the ELF object could be a more simplified approach, although a little more treacherous. I didn't decide yet what to try first -- any thoughts? @Fangrui's and @HJ's thoughts about this could be gold.