Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp3878050ybf; Tue, 3 Mar 2020 14:52:00 -0800 (PST) X-Google-Smtp-Source: ADFU+vtJ2i7/A61MjsEC8/6mZ9TkAUzgWtzO/zjjbgmugjtuWanZzb2q8YuZdyXYX/UsFd4LN3Ml X-Received: by 2002:a9d:6050:: with SMTP id v16mr129759otj.151.1583275920675; Tue, 03 Mar 2020 14:52:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583275920; cv=none; d=google.com; s=arc-20160816; b=nr2lh5MHr/yyvf6q6V4TBQHkFCw9QlqJjer5XfUfgnoa5EoySod+4P7olmFp3zlldG mKS1fFj8Vh5xlo7Aslgh+BZ84yj4TA7JqezW1QwrIWcPTqRDNEfolpATYOgy6Li4okRf Dh8yPk4xCJeRKVjw1yDwFmau6te3gXYB7wQxDZddURtYHdkf40lZn3VKi/6cOUNOsNkn C+C/iboZtlmEHC4KTFvte+zwGfgmnitrGQXL4iSkFL1PrqxXDA5HaSTguhUqZZYeA1Z7 rbQIzrsylbq30uqjGjtUejdIB5ivpq8YsurJ9aTCSCMJSL2fwm9/sDJ8ZIcoDPnYk+d3 V5SQ== 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=pMsUvjiwCOPF/dDyeOX60wF1NNKJdi7xivScA5hcfdI=; b=RRkffaL+4oEcbhoQQ7HtM62YCkVErDHNkQr+CijO+J4DABRUdPWD1lVoUVW/NbhEnQ zFRNGRDl6RpIroBV5tHWhT1OLaK4Y+j9AecZOjWSvlDEpRDgfMCY0qB+k0tZTVYNno+6 QoN6s/7AxLDUzlmhaG+u8ioZYY5P3ONYfUeK37vzUujxcdzOEVx/T74FG7WTZyClT1q0 2IPhAK+vg+6W2ZTwzM+3M2fzLx/0DzpXRz7x2EZ4uJ7DC/YzNVsPJrqOGEFoywC4XJZB 5MQwR3jxpDO5jgKnsf0ghOcSw0u9k5uVSEC9ss6texpwzbqBBOb0bPtjXmU+AlRl0X8t ZzpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=fTDiEPY2; 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 u143si84739oif.84.2020.03.03.14.51.48; Tue, 03 Mar 2020 14:52:00 -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=fTDiEPY2; 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 S1728177AbgCCWv3 (ORCPT + 99 others); Tue, 3 Mar 2020 17:51:29 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:32797 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727026AbgCCWv3 (ORCPT ); Tue, 3 Mar 2020 17:51:29 -0500 Received: by mail-wr1-f65.google.com with SMTP id x7so43660wrr.0 for ; Tue, 03 Mar 2020 14:51:27 -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=pMsUvjiwCOPF/dDyeOX60wF1NNKJdi7xivScA5hcfdI=; b=fTDiEPY2YGw4Lu1DTg0xD1e/rsCytBZOd9CmqrBJ7oF5LvzePImM+P5Xmpc8nq07ZR +Jo4oPrjRllpKyj97yUOIcTndvt46U59N2S0ByhdWTpRm95HrMvWUkf5BI7mwFi1pYZl Rq/XgI9uY9vSHvQJkZk0ZdexQjbN7kAPmuRvE= 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=pMsUvjiwCOPF/dDyeOX60wF1NNKJdi7xivScA5hcfdI=; b=siTWMLOLfXdn5sl/LBPbZlCtx7dnP99dmlPRUVPaN4dE2AEYbfpPaqdAJ4glml2fLK kNy1+eq4JiwFT8eujivQ4LztItDeVfUU8EN8TSMNfDVe+dXlBq47B74SSjErmBgP3RN2 nKl/crp3EbSrjl3rB0RlVYX6PoQoNgivDRvKVEtM63PsrNq/WReIA8KZeR1U4I7Y91YE GHkeDLXHLYF/hgIVzsNy4adDoXGgqJp9fDGl04RUHyozeFK+LnnJ62K+xJ3ayq3bid+r h4ZUdMUdv4ZXRqgALKp9IaYt3Bobcymgz5UO1Lhrwpyovyr2idW4nTNh9ipx9WcTreKl nxkg== X-Gm-Message-State: ANhLgQ0+u/Ssuw/nLclerh8QCciiZTAy7LiHagiHcsy4TSTEeDOAKLp8 GlgmwGnNNErNmtrPOnQmpAXYQQ== X-Received: by 2002:adf:a18b:: with SMTP id u11mr283288wru.148.1583275886534; Tue, 03 Mar 2020 14:51:26 -0800 (PST) Received: from chromium.org (77-56-209-237.dclient.hispeed.ch. [77.56.209.237]) by smtp.gmail.com with ESMTPSA id 133sm916008wmd.5.2020.03.03.14.51.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2020 14:51:26 -0800 (PST) From: KP Singh X-Google-Original-From: KP Singh Date: Tue, 3 Mar 2020 23:51:24 +0100 To: Andrii Nakryiko Cc: open list , bpf , Alexei Starovoitov , Daniel Borkmann , Paul Turner , Florent Revest , Brendan Jackman Subject: Re: [PATCH bpf-next 3/7] bpf: Introduce BPF_MODIFY_RETURN Message-ID: <20200303225124.GA12968@chromium.org> References: <20200303140950.6355-1-kpsingh@chromium.org> <20200303140950.6355-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 14:37, Andrii Nakryiko wrote: > On Tue, Mar 3, 2020 at 6:12 AM 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") > > BPF_PROG(func_name, int a, int b, int ret) > > same as on cover letter, return type is missing Fixed. Thanks! > > > { > > // 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 | 96 ++++++++++++++++++++++++++++++++-- > > 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, 103 insertions(+), 6 deletions(-) > > > > [...] > > > > > + if (fmod_ret->nr_progs) { > > + branches = kcalloc(fmod_ret->nr_progs, sizeof(u8 *), > > + GFP_KERNEL); > > + if (!branches) > > + return -ENOMEM; > > + if (invoke_bpf_mod_ret(m, &prog, fmod_ret, stack_size, > > + branches)) > > branches leaks here Good catch, sloppy work here by me. > > > + return -EINVAL; > > + } > > + > > if (flags & BPF_TRAMP_F_CALL_ORIG) { > > - if (fentry->nr_progs) > > + if (fentry->nr_progs || fmod_ret->nr_progs) > > restore_regs(m, &prog, nr_args, stack_size); > > > > /* call original function */ > > @@ -1573,6 +1649,14 @@ int arch_prepare_bpf_trampoline(void *image, void *image_end, > > there is early return one line above here, you need to free branches > in that case to not leak memory > > So I guess it's better to do goto cleanup approach at this point? yeah, agreed, updated to doing a cleanup at the end. - KP > > > emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8); > > } > > > > + if (fmod_ret->nr_progs) { > > + align16_branch_target(&prog); > > + for (i = 0; i < fmod_ret->nr_progs; i++) > > + emit_cond_near_jump(&branches[i], prog, branches[i], > > + X86_JNE); > > + kfree(branches); > > + } > > + > > [...]