Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1576207pxa; Thu, 20 Aug 2020 15:08:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxOc8CE6mXG6Snr/vcXkLIfCJVwmsf1xtLpetkvQs+jYgIOO2tFU2slrN0RL5DPHxbFJKAS X-Received: by 2002:a17:906:4f8f:: with SMTP id o15mr31392eju.220.1597961326318; Thu, 20 Aug 2020 15:08:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597961326; cv=none; d=google.com; s=arc-20160816; b=Ii196iGvx2HqtVurvW/RUIROT8Z2XoatWnY33cipOz79J9b7HI00p3Zp3Y3oGE5U71 w8LSno+R3IhFZ2x3lq7A2pOqvBMy9QyVtQPkUlax56CPUNZKaiNRmb7TTR3B3AiGu87k rgjYdABtfQbrkIPwIr08RIztjsT/k3InPkXca3goON2JfoGL6CX+P4rFiwVPmwmONa2k eCpuAEMfAkQxPuom3rzsbEs+8CWjrC88o1+wRoqL9FP3Xz4GuxjxpJ4cbg8/Lbyu9F1C 9fifO+s4rp2wCnJ6LEgfXT1/yrepZx9qxF9w3HBXQScZOYiBfFPrmeJLiUfeQR9EyKp4 tniA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=lAdAyYh5aN2j5JPQrGtT0EfFNZkeh1mFjZ/MnX14mHg=; b=prFFOtPYVyiYNkb+/T5leUj5OU0miDr/FIFMQCScWTflj9HAXUFuBEPR3mh1OdFv5P zK+W3GYu+4lBRw+qjjZTQkYp8TgN5sFNtdw93XkEyn1YcnG1rAgHgjM1hGK+P7xFvqC4 QDA0q2nwMQW2HuJib2rFX/KIIlp5xSJL6jdWeM22VcE6kKRbTeq/jnYMX4NxvpKZiX3S AthhqT0KXJSsAXESbQSxnhUMCC6DFV6b1B6Defougxg0jsxhaIHhl4S83heh2A3IX4Ga 57/reAbN74A5kq5Wiew2CXhZ+bbK0gYijkoB/w1nrW4iyNVmSvxCKsTRCGWnYdJNlZeR uxqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=jqlWDd6s; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id kt20si2107247ejb.491.2020.08.20.15.08.22; Thu, 20 Aug 2020 15:08:46 -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=@chromium.org header.s=google header.b=jqlWDd6s; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728779AbgHTVqF (ORCPT + 99 others); Thu, 20 Aug 2020 17:46:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726435AbgHTVqB (ORCPT ); Thu, 20 Aug 2020 17:46:01 -0400 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B83F0C061386 for ; Thu, 20 Aug 2020 14:46:00 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id s15so1764101pgc.8 for ; Thu, 20 Aug 2020 14:46:00 -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:content-transfer-encoding:in-reply-to; bh=lAdAyYh5aN2j5JPQrGtT0EfFNZkeh1mFjZ/MnX14mHg=; b=jqlWDd6sxFDmFJ/KUiqRGFnNyGKWDiNJLpoSwupzugFLhqCN9ZS6qtccF0An5Z2pAp nwBWlzga+bXknE60CNsiILhMirgchDDV2Vj7hY7IEMtxh8h6VGzn+3wNGo2+F7ea9pJx uume6sU6+RSqa4UeUUuBxrG3vOAzelifZxGgw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=lAdAyYh5aN2j5JPQrGtT0EfFNZkeh1mFjZ/MnX14mHg=; b=WHpG82QduGcK2Ml7MSvGdu/PH5L+SBmaC9pC7vlKv8e0pkPRNQgcgxmAtWlAv73YmJ 7byTr0/1L/WkUa3g0lW9pA892fZHRsT5hLLeNbbUJ1OY64jv17kw27Vcee6RpYlWUq/B Uk/9LEgZqQYw2lGjWvULcO94fHhdLG4/ilm+pvUju1tD5Z8upNAONfyaHZrW2FeaooDT +Rv2ZUwHCUA1N+PtQTNdXjJw1iaf71WMrGkx4u6YntYLRnCMbDx4F4olmak8Ks0Swr6s 12TQouVX2eIVyddOQOAAeU8+pUk+VJT+LAwEWh3v0M64ck0YXiqb23vLZQTLJcuHLBce Bd/Q== X-Gm-Message-State: AOAM532nmxEgOGF6HxjNqyaDooLyNunUwOdtkf0/imaFfsDRu1Sgs0US JBiD6qr42HERoR4wnJFABl1qow== X-Received: by 2002:a65:66c4:: with SMTP id c4mr64065pgw.442.1597959959876; Thu, 20 Aug 2020 14:45:59 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id k21sm4159pgl.0.2020.08.20.14.45.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Aug 2020 14:45:58 -0700 (PDT) Date: Thu, 20 Aug 2020 14:45:57 -0700 From: Kees Cook To: Brendan Jackman Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-security-module@vger.kernel.org, Paul Renauld , Alexei Starovoitov , Daniel Borkmann , James Morris , pjt@google.com, jannh@google.com, peterz@infradead.org, rafael.j.wysocki@intel.com, thgarnie@chromium.org, kpsingh@google.com, paul.renauld.epfl@gmail.com, Brendan Jackman Subject: Re: [RFC] security: replace indirect calls with static calls Message-ID: <202008201435.97CF8296@keescook> References: <20200820164753.3256899-1-jackmanb@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20200820164753.3256899-1-jackmanb@chromium.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 20, 2020 at 06:47:53PM +0200, Brendan Jackman wrote: > From: Paul Renauld > > LSMs have high overhead due to indirect function calls through > retpolines. This RPC proposes to replace these with static calls [1] typo: RFC > instead. Yay! :) > [...] > This overhead prevents the adoption of bpf LSM on performance critical > systems, and also, in general, slows down all LSMs. I'd be curious to see other workloads too. (Your measurements are a bit synthetic, mostly showing "worst case": one short syscall in a tight loop. I'm curious how much performance gain can be had -- we should still do it, it'll be a direct performance improvement, but I'm curious about "real world" impact too.) > [...] > Previously, the code for this hook would have looked like this: > > ret = DEFAULT_RET; > > for each cb in [A, B, C]: > ret = cb(args); <--- costly indirect call here > if ret != 0: > break; > > return ret; > > Static calls are defined at build time and are initially empty (NOP > instructions). When the LSMs are initialized, the slots are filled as > follows: > > slot idx content > |-----------| > 0 | | > |-----------| > 1 | | > |-----------| > 2 | call A | <-- base_slot_idx = 2 > |-----------| > 3 | call B | > |-----------| > 4 | call C | > |-----------| > > The generated code will unroll the foreach loop to have a static call for > each possible LSM: > > ret = DEFAULT_RET; > switch(base_slot_idx): > > case 0: > NOP > if ret != 0: > break; > // fallthrough > case 1: > NOP > if ret != 0: > break; > // fallthrough > case 2: > ret = A(args); <--- direct call, no retpoline > if ret != 0: > break; > // fallthrough > case 3: > ret = B(args); <--- direct call, no retpoline > if ret != 0: > break; > // fallthrough > > [...] > > default: > break; > > return ret; > > A similar logic is applied for void hooks. > > Why this trick with a switch statement? The table of static call is defined > at compile time. The number of hook callbacks that will be defined is > unknown at that time, and the table cannot be resized at runtime. Static > calls do not define a conditional execution for a non-void function, so the > executed slots must be non-empty. With this use of the table and the > switch, it is possible to jump directly to the first used slot and execute > all of the slots after. This essentially makes the entry point of the table > dynamic. Instead, it would also be possible to start from 0 and break after > the final populated slot, but that would require an additional conditional > after each slot. Instead of just "NOP", having the static branches perform a jump would solve this pretty cleanly, yes? Something like: ret = DEFAULT_RET; ret = A(args); <--- direct call, no retpoline if ret != 0: goto out; ret = B(args); <--- direct call, no retpoline if ret != 0: goto out; goto out; if ret != 0: goto out; out: return ret; > [...] > The number of available slots for each LSM hook is currently fixed at > 11 (the number of LSMs in the kernel). Ideally, it should automatically > adapt to the number of LSMs compiled into the kernel. Seems like a reasonable thing to do and could be a separate patch. > If there’s no practical way to implement such automatic adaptation, an > option instead would be to remove the panic call by falling-back to the old > linked-list mechanism, which is still present anyway (see below). > > A few special cases of LSM don't use the macro call_[int/void]_hook but > have their own calling logic. The linked-lists are kept as a possible slow > path fallback for them. I assume you mean the integrity subsystem? That just needs to be fixed correctly. If we switch to this, let's ditch the linked list entirely. Fixing integrity's stacking can be a separate patch too. > [...] > Signed-off-by: Paul Renauld > Signed-off-by: KP Singh > Signed-off-by: Brendan Jackman This implies a maintainership chain, with Paul as the sole author. If you mean all of you worked on the patch, include Co-developed-by: as needed[1]. -Kees [1] https://www.kernel.org/doc/html/latest/process/submitting-patches.html#when-to-use-acked-by-cc-and-co-developed-by -- Kees Cook