Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp3869531ybf; Tue, 3 Mar 2020 14:38:36 -0800 (PST) X-Google-Smtp-Source: ADFU+vvcZjuzqztu6ahRbjXYP/Q6ZLNJUjLO2+rgWJxwom/lbxIfIKDFjTb+Npymf5NPDhmM8oyI X-Received: by 2002:a9d:404b:: with SMTP id o11mr102746oti.368.1583275116204; Tue, 03 Mar 2020 14:38:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583275116; cv=none; d=google.com; s=arc-20160816; b=Y7U+bFo7IngSGXuSROa6/BVmBhvBrj8rIeRN9WqPlSKsOVHdu6Qtj+HeeUmNKpf7dD N9Q8MMaNt9OIk9tgDeovvoyie7uuvGPUe+NY/eISNIgPdtrLKUBA4pxnBFfz8y1i3Jz2 ofCUXH67tbqB0iKnZ9cyAflwBcsmOVhAA/wmZ6O6Eg7c0sXO00NbBHXgk4zOU15ylq/j +p/2yFUtZc6mGpt8pGCjv+pKwfzV4PiyF5JnwrgBOydUcPKGA7AZ0309flmQGnWo9PT4 JxUg0UcPSihJBofNT3zHQrh8KTXO1T8Hv8DzVhdF2hXJ95CgdPJ1c/UzwJTKMthMOcqi M06g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=88lKfSfxqeKfW9gPmeZNxU/n5BEhm6ow0JX/FjM1ow0=; b=Eq5n+ImBsZ1T/GTzqE+I1uDr4qNX80Nad7gpIcq8+IGNnVu7orvUccgwEFXZ0PW3FQ 9U2tDWMqh1vO5mGtu5s0KDxhd+rEtuF9kOkA5ZUsPLN1Ru+HFiYhdaFldBrBOpjhYbCy OIlN/IAwotLGK/Nv6NB1lTUPUxBI4V5eYuBvAIiKHNK0/hh9pdI6yhO4y/2Qowocu5g4 KQoLYdLiaWWWlbK3fq4F7lO9eptSDoJKB4C/2ByxKmvxIL2YmFcETBFUbMJOrwENv++M ZhnlytsdJZxAZD8+SJTDyro5KmpTod8+3TH9UE2os4Tvo88mk300FUEdjc5zfQHPa+vz Z/yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="Eea/ossA"; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 26si88135oiz.107.2020.03.03.14.38.24; Tue, 03 Mar 2020 14:38:36 -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=@gmail.com header.s=20161025 header.b="Eea/ossA"; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727939AbgCCWiL (ORCPT + 99 others); Tue, 3 Mar 2020 17:38:11 -0500 Received: from mail-qk1-f195.google.com ([209.85.222.195]:34846 "EHLO mail-qk1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727274AbgCCWiL (ORCPT ); Tue, 3 Mar 2020 17:38:11 -0500 Received: by mail-qk1-f195.google.com with SMTP id 145so5197812qkl.2; Tue, 03 Mar 2020 14:38:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=88lKfSfxqeKfW9gPmeZNxU/n5BEhm6ow0JX/FjM1ow0=; b=Eea/ossAaGke+BXBqhzBcqWt7LcYZFg+xJy+lW2C2M2TgoHjybgnVxdmtMzivSYm6f rDA8MYlpP2JV7LM+hN4Wqv78PCUnLlLHYkciQHGyLh02hFZ/OVNhKPB39a+/LFsPpPEF EwEJIKoNuEQdPeRP9V0BhSYq2ZeNFTbXaAPLIgoSw8pwj7SP5bGy29QbA3Ec1ZrThLQ1 ylzofUVzUdYcGB3u62u5OOr4owR8TLDxaM1EiJyiaebVQaKCeWek0Rj9vfM3FB62abLS fe2Y+flireZmGdceY3SBZr2li29R3VktpJN8RRQH2o14wpSFXHDvlwJmhAhj36rznadq WQ4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=88lKfSfxqeKfW9gPmeZNxU/n5BEhm6ow0JX/FjM1ow0=; b=NNDHx+UgdGocl7pIO8Y3PYp+L+OW3ttgvEFM3sDuTpGvRhHUIpbbCbXyLWssCeyIGx mbVNZIxr5+eWXy3Ep164grHupch8D4NS5ImY5Y4c5QOC5b8Rw1Ww9oXjafcqSyb9bvYm SlAVbQMMxneSyDoFYalwvR1dS8CyR1TTTp6FRBjkJeUpoNbCbHPkVs1hf2F0rbNG/VAF xMwnbNnOLRv0C0/rfNjo/W6iXjdm2XCvCJH6SUxsAvmYr2S17eca8X1KEZZGDVTpTAvm s+/J+NQfqSWVPykE7FJrBPCQz5Jom3OQ6ELznMK8kLLYZd4aUuILXXT94wP9JXGZoWsl BpuQ== X-Gm-Message-State: ANhLgQ2HS2H4NyARHip0O+klRVzrj4k2Xy2ARj+0JoRFAR3aeVDeVI/X cGBF/wmKrnE9AoHKu3TVevpJMR5DWkDumskDDtQ= X-Received: by 2002:a05:620a:99d:: with SMTP id x29mr270802qkx.39.1583275090204; Tue, 03 Mar 2020 14:38:10 -0800 (PST) MIME-Version: 1.0 References: <20200303140950.6355-1-kpsingh@chromium.org> <20200303140950.6355-4-kpsingh@chromium.org> In-Reply-To: <20200303140950.6355-4-kpsingh@chromium.org> From: Andrii Nakryiko Date: Tue, 3 Mar 2020 14:37:58 -0800 Message-ID: Subject: Re: [PATCH bpf-next 3/7] bpf: Introduce BPF_MODIFY_RETURN To: KP Singh Cc: open list , bpf , Alexei Starovoitov , Daniel Borkmann , Paul Turner , Florent Revest , Brendan Jackman Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > { > // 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 > + 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? > 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); > + } > + [...]