Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp4607725ybf; Wed, 4 Mar 2020 07:13:50 -0800 (PST) X-Google-Smtp-Source: ADFU+vsZ+p1Zcnp5TLUn9IgS/FPdNoSiMHSfHMKinFW/4GzWCxMAPQ3IEtR9bqPeqi5EftXV4Tix X-Received: by 2002:aca:2b0f:: with SMTP id i15mr2013340oik.29.1583334830332; Wed, 04 Mar 2020 07:13:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583334830; cv=none; d=google.com; s=arc-20160816; b=ygePEFPLt9YBvDGqv22IMyqE712spj9QalZnmlJJm8efpERWGnpf2DXCoocTRr6VPr HqMuAIwk4G9eUy6k3W/RN6ezMCy9d7o1mjU5b9TXkjBHspdaTNhQSiYvfzLZgWMI2R1c SAYLil2E4cbNW4SfGwZXG9wWini/99SRFtPBxJYbsMNl7SeqGtLubwLd0c82v3nuf1Bq ghRnpBE/TWtDfylloDx2z4xhWitWHfQ/zZzJp/ramMnztjhcWq6sWgFCM6ZcomC72rYz H7hykCYwH3y8fhUTsxuxx0PwnsBnaZXU4KHvVX3PjIBxqQs4kJkA2mXlmQ6/AjEN8QCP agKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:date:from:dkim-signature; bh=hZJyRWg+l1EQKudArZuRb63dEwzdCahRlAsMV0GZXUw=; b=m1T32Eud4h3kbv40JkrfMsJpF9QvMWpiV4kWMyOE43MN6IiDssQx84vGTvborzNGdW Z0lEQWrJ1TI0ffa4O2yltyFnzF65FTvASEvd8KcsfuKSfOj+F32rFj+6b/p2eOC+uDGV IAHHtvW902vPI3UDX3u9QGvuDmbNaEK5HtcNuztHNCo02uzjpMlD3ijgLcF2rZA13A9Z 0nWsaFuwmzUh4fUYfJkkTdH2vonSMLeT+IvUuez7n7oNbEqWyrExLfFNhfm32SgQvsud AHEqOCDVVGNXKWj0V27bxzs3ysiHqSKlz/Fw1ho1OaHcsOqaiIl2PyQIvBBKlEievD35 aYwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Og0kIOAh; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id t25si1340125otm.71.2020.03.04.07.13.27; Wed, 04 Mar 2020 07:13:50 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Og0kIOAh; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1729538AbgCDPNI (ORCPT + 99 others); Wed, 4 Mar 2020 10:13:08 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:34698 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725795AbgCDPNI (ORCPT ); Wed, 4 Mar 2020 10:13:08 -0500 Received: by mail-wr1-f65.google.com with SMTP id z15so2866126wrl.1 for ; Wed, 04 Mar 2020 07:13:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:date:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=hZJyRWg+l1EQKudArZuRb63dEwzdCahRlAsMV0GZXUw=; b=Og0kIOAhyFhYo3ateVctfHnwO5v/TiVer2LAVTuJ+JE681tg1mBEauXqfv73ltQLMI Xcu6v/I1lrQ4GzL3amoni5XrY0fVwPdqea8/iV3yOz3SW2GGIunF83E2lKzpsz/Kem2N TtJEWU3/NnYISird4p5efZ7QX3nJzYV9Qlpt8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=hZJyRWg+l1EQKudArZuRb63dEwzdCahRlAsMV0GZXUw=; b=BE1IIP5/Ios1wy4X8lh9CwjEH2IWTerap3ee+yIcLTx2RlrKCgyRk3v6UA+F+ERjdU 2ntxFkts7z/0CitxGNu+qdBAVkIW0xaO8qmT2bwg3QgmqOdoz585HIUXEshKHWqq0oxX AovH55NHF66QsIDInfrBBxRbZIXgnSZG+jXeGgEGAeKiHpUjak4dXV8wxc6dvN+CfHOM O1Dy5LEdUfRL3LzAH+fHrZDfPy2ZPnDk415bRQbcbhXwg5cicLB5NPuJie7QKHy1gxMA 9TN6DgQ5o/aEuel1z8K1apSuSo5uMt45nGaM1lnyGojBeHuljsOue81GRAuro4YxhSEb Il+Q== X-Gm-Message-State: ANhLgQ27CX/vy+5H+Drs52eoULpJN32zin1r3ZPLZj0ol4dlZgo+MhhA DjPH13iM8k8uBMbnrQAcEjYz/w== X-Received: by 2002:adf:df8d:: with SMTP id z13mr2907749wrl.302.1583334784893; Wed, 04 Mar 2020 07:13:04 -0800 (PST) Received: from chromium.org ([2a00:79e1:abc:308:8ca0:6f80:af01:b24]) by smtp.gmail.com with ESMTPSA id z131sm4420307wmg.25.2020.03.04.07.13.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2020 07:13:04 -0800 (PST) From: KP Singh X-Google-Original-From: KP Singh Date: Wed, 4 Mar 2020 16:13:03 +0100 To: Andrii Nakryiko Cc: linux-security-module@vger.kernel.org, open list , bpf , Alexei Starovoitov , Daniel Borkmann , Paul Turner , Jann Horn , Florent Revest , Brendan Jackman Subject: Re: [PATCH bpf-next v2 3/7] bpf: Introduce BPF_MODIFY_RETURN Message-ID: <20200304151303.GC9984@chromium.org> References: <20200304015528.29661-1-kpsingh@chromium.org> <20200304015528.29661-4-kpsingh@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 03-M?r 21:08, Andrii Nakryiko wrote: > On Tue, Mar 3, 2020 at 5:56 PM KP Singh wrote: > > > > From: KP Singh > > > > When multiple programs are attached, each program receives the return > > value from the previous program on the stack and the last program > > provides the return value to the attached function. > > > > The fmod_ret bpf programs are run after the fentry programs and before > > the fexit programs. The original function is only called if all the > > fmod_ret programs return 0 to avoid any unintended side-effects. The > > success value, i.e. 0 is not currently configurable but can be made so > > where user-space can specify it at load time. > > > > For example: > > > > int func_to_be_attached(int a, int b) > > { <--- do_fentry > > > > do_fmod_ret: > > > > if (ret != 0) > > goto do_fexit; > > > > original_function: > > > > > > > > } <--- do_fexit > > > > The fmod_ret program attached to this function can be defined as: > > > > SEC("fmod_ret/func_to_be_attached") > > int BPF_PROG(func_name, int a, int b, int ret) > > { > > // This will skip the original function logic. > > return 1; > > } > > > > The first fmod_ret program is passed 0 in its return argument. > > > > Signed-off-by: KP Singh > > --- > > arch/x86/net/bpf_jit_comp.c | 130 ++++++++++++++++++++++++++++++--- > > include/linux/bpf.h | 1 + > > include/uapi/linux/bpf.h | 1 + > > kernel/bpf/btf.c | 3 +- > > kernel/bpf/syscall.c | 1 + > > kernel/bpf/trampoline.c | 5 +- > > kernel/bpf/verifier.c | 1 + > > tools/include/uapi/linux/bpf.h | 1 + > > 8 files changed, 130 insertions(+), 13 deletions(-) > > > > This looks good, but I'll Alexei check all the assembly generation > logic, not too big of an expert on that. > > [...] > > > > static int emit_fallback_jump(u8 **pprog) > > diff --git a/include/linux/bpf.h b/include/linux/bpf.h > > index 98ec10b23dbb..3cfdc216a2f4 100644 > > --- a/include/linux/bpf.h > > +++ b/include/linux/bpf.h > > @@ -473,6 +473,7 @@ void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start); > > > > enum bpf_tramp_prog_type { > > BPF_TRAMP_FENTRY, > > + BPF_TRAMP_MODIFY_RETURN, > > This is probably bad idea to re-number BPF_TRAMP_FEXIT for no good > reason. E.g., if there are some drgn scripts that do some internal > state printing, this is major inconvenience, while really providing no > benefit in itself. Consider putting it right before BPF_TRAMP_MAX. Makes sense, I somehow initially (incorrectly) assumed that the order represented the order of execution. But the only real demarcation is the BPF_TRAMP_MAX. Updated it for v3. - KP > > > BPF_TRAMP_FEXIT, > > BPF_TRAMP_MAX, > > BPF_TRAMP_REPLACE, /* more than MAX */ > > [...]