From patchwork Mon Dec 14 19:52:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitrii Banshchikov X-Patchwork-Id: 11972875 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D132EC4361B for ; Mon, 14 Dec 2020 19:54:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 81511205CB for ; Mon, 14 Dec 2020 19:54:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2502859AbgLNTyB (ORCPT ); Mon, 14 Dec 2020 14:54:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2502856AbgLNTx7 (ORCPT ); Mon, 14 Dec 2020 14:53:59 -0500 Received: from mail-ed1-x541.google.com (mail-ed1-x541.google.com [IPv6:2a00:1450:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75E37C0613D3 for ; Mon, 14 Dec 2020 11:53:18 -0800 (PST) Received: by mail-ed1-x541.google.com with SMTP id dk8so18515544edb.1 for ; Mon, 14 Dec 2020 11:53:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ubique-spb-ru.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=31fJCR82BsbBke40rmpflWa3sSApV5mPUqlCTPLF8jo=; b=XrB5QRzVm6IDnKBHYOni3pOhJ6KFsgoMOCIkM57TkA/IphuZp/VmULohoWtTjALPj6 Dbzls6SnxvFqnoEyBlApfojsEVl4HNT9LD7/xNBsJE5pSxaqdjBSuVddomRKrLoj4deT Ta5b9XAIPAAmGkiOVCGen85v1UqxONe3tyZqGOuYR7LuJjywtKyJyizA6uLCQ19mYDCq 4debtR5t4GcHNaNeFQKGGBuUkBVKCv53h+ZqRibId7v4H5twhlFtHRKeGE1vEoDhmi+5 0dm6Hsr+bM5uwsU9a4uGs5mf8zM4WKfcPQhwAhLkgTbIyI56/2RKwKG1tnaVeG0vJMwJ IrlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=31fJCR82BsbBke40rmpflWa3sSApV5mPUqlCTPLF8jo=; b=bLhdYcGpSgQ6VMRcQ8yLoUA0hJvbKwI71kzJMnE6LGYx1Mli8TiVsMFGSVN2v43dUY u3uiPxSNJf2k9KPuIOpl5EpU1Tg32uHb+2qZeAL5Ot/E8t0exijvYCsT20xXy913yy7r QwdWNDK6QyJGrWCFL26daSsKMPn/jwlVkgVWeV5NP6p81PsfIrU8Ore3tnEqli0pK+Pv 1h0aJWcV6d4tpYkkeIy9e6SkVNf4MiqnZTWiyDSRCghVlyOhl0NS+3NIkZl/B98Iq4Tz IIC4USPGayqWiLpWeL84uyGA4f8ddGKVWxiR6+N9k6lrcnRTB7koVM4XKkNMMlcfJ8o6 cj4w== X-Gm-Message-State: AOAM532Dds9+vlJ64fE7wfDfU/SmXJT5z8wOUU46YOXObr6UscUUp0U4 3vx51vCv+3K6aBbvkz+WOyoRiE16KIg+iSMUCmw= X-Google-Smtp-Source: ABdhPJxI/YKISoFcvucVLx2AQXGLgI4YvixH63+FqkN2Dkx3kR6D3i+VuYVpMDcg5Z8PdlT0tsCIRQ== X-Received: by 2002:aa7:da01:: with SMTP id r1mr26847786eds.45.1607975596958; Mon, 14 Dec 2020 11:53:16 -0800 (PST) Received: from localhost (bba163592.alshamil.net.ae. [217.165.22.16]) by smtp.gmail.com with ESMTPSA id j7sm16849273edp.52.2020.12.14.11.53.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Dec 2020 11:53:16 -0800 (PST) From: Dmitrii Banshchikov To: bpf@vger.kernel.org Cc: Dmitrii Banshchikov , ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@chromium.org, rdna@fb.com Subject: [PATCH bpf-next 1/3] bpf: Factor out nullable reg type conversion Date: Mon, 14 Dec 2020 23:52:48 +0400 Message-Id: <0ff8927166f6e18e72adab8a94cb6d694c610cc0.1607973529.git.me@ubique.spb.ru> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Factor out helper function for conversion nullable register type to its corresponding type with value. Signed-off-by: Dmitrii Banshchikov --- kernel/bpf/verifier.c | 77 ++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 93def76cf32b..dee296dbc7a1 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -1073,6 +1073,43 @@ static void mark_reg_known_zero(struct bpf_verifier_env *env, __mark_reg_known_zero(regs + regno); } +static int mark_ptr_not_null_reg(struct bpf_reg_state *reg) +{ + if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { + const struct bpf_map *map = reg->map_ptr; + + if (map->inner_map_meta) { + reg->type = CONST_PTR_TO_MAP; + reg->map_ptr = map->inner_map_meta; + } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { + reg->type = PTR_TO_XDP_SOCK; + } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || + map->map_type == BPF_MAP_TYPE_SOCKHASH) { + reg->type = PTR_TO_SOCKET; + } else { + reg->type = PTR_TO_MAP_VALUE; + } + } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { + reg->type = PTR_TO_SOCKET; + } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { + reg->type = PTR_TO_SOCK_COMMON; + } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { + reg->type = PTR_TO_TCP_SOCK; + } else if (reg->type == PTR_TO_BTF_ID_OR_NULL) { + reg->type = PTR_TO_BTF_ID; + } else if (reg->type == PTR_TO_MEM_OR_NULL) { + reg->type = PTR_TO_MEM; + } else if (reg->type == PTR_TO_RDONLY_BUF_OR_NULL) { + reg->type = PTR_TO_RDONLY_BUF; + } else if (reg->type == PTR_TO_RDWR_BUF_OR_NULL) { + reg->type = PTR_TO_RDWR_BUF; + } else { + return -EINVAL; + } + + return 0; +} + static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) { return type_is_pkt_pointer(reg->type); @@ -7323,50 +7360,24 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state, } if (is_null) { reg->type = SCALAR_VALUE; - } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { - const struct bpf_map *map = reg->map_ptr; - - if (map->inner_map_meta) { - reg->type = CONST_PTR_TO_MAP; - reg->map_ptr = map->inner_map_meta; - } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { - reg->type = PTR_TO_XDP_SOCK; - } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || - map->map_type == BPF_MAP_TYPE_SOCKHASH) { - reg->type = PTR_TO_SOCKET; - } else { - reg->type = PTR_TO_MAP_VALUE; - } - } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { - reg->type = PTR_TO_SOCKET; - } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { - reg->type = PTR_TO_SOCK_COMMON; - } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { - reg->type = PTR_TO_TCP_SOCK; - } else if (reg->type == PTR_TO_BTF_ID_OR_NULL) { - reg->type = PTR_TO_BTF_ID; - } else if (reg->type == PTR_TO_MEM_OR_NULL) { - reg->type = PTR_TO_MEM; - } else if (reg->type == PTR_TO_RDONLY_BUF_OR_NULL) { - reg->type = PTR_TO_RDONLY_BUF; - } else if (reg->type == PTR_TO_RDWR_BUF_OR_NULL) { - reg->type = PTR_TO_RDWR_BUF; - } - if (is_null) { /* We don't need id and ref_obj_id from this point * onwards anymore, thus we should better reset it, * so that state pruning has chances to take effect. */ reg->id = 0; reg->ref_obj_id = 0; - } else if (!reg_may_point_to_spin_lock(reg)) { - /* For not-NULL ptr, reg->ref_obj_id will be reset + } else { + mark_ptr_not_null_reg(reg); + + if (!reg_may_point_to_spin_lock(reg)) { + /* For not-NULL ptr, reg->ref_obj_id will be reset * in release_reg_references(). * * reg->id is still used by spin_lock ptr. Other * than spin_lock ptr type, reg->id can be reset. */ - reg->id = 0; + reg->id = 0; + } } } } From patchwork Mon Dec 14 19:52:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitrii Banshchikov X-Patchwork-Id: 11972879 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8F785C2BB48 for ; Mon, 14 Dec 2020 19:54:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 57E5922241 for ; Mon, 14 Dec 2020 19:54:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2502870AbgLNTyP (ORCPT ); Mon, 14 Dec 2020 14:54:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2502847AbgLNTyE (ORCPT ); Mon, 14 Dec 2020 14:54:04 -0500 Received: from mail-ej1-x641.google.com (mail-ej1-x641.google.com [IPv6:2a00:1450:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11E80C061793 for ; Mon, 14 Dec 2020 11:53:24 -0800 (PST) Received: by mail-ej1-x641.google.com with SMTP id ce23so24254702ejb.8 for ; Mon, 14 Dec 2020 11:53:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ubique-spb-ru.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LOWpV9lRo6kWGpzmW7Fn/629/O9f1famaE6ps2CxfmY=; b=yWiwENx4QU1rsL4JvCTKQIL8dx7nW8cvui8kdZgYAN/di0cTwP8nrQNvwj9FYvMyCg WXZnZNNp2KP7kjOGFikeOy03ILkA9t80oXZypdb2cnCR4Puyw7/kAe5qITtS/vfYxo0r VqqRllrrYlCBG67bSb61wsOBXR5u2M3PX1kwtSdxaqnnjIem+LmXURXdndx+uN4HrLAA HS+978vMpySPZi+zqFPeMGRzBiwbh1ly3n8KqFnCAyuV35UeH7uomqv1MeNFyKq0On0w aIDYUmjiJlR1+dIpGE6m5DHF8/shPUN4VMAGNwMPHgNGhOG2LYL3D2aj2Av666LGkn9C Rfwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LOWpV9lRo6kWGpzmW7Fn/629/O9f1famaE6ps2CxfmY=; b=Pb3y2lEqjb0bJSlXAfP2H6VIduB4wr0YQ8Zka2Vm23KBGX4DRjCUjNBnysF1N4Atoy b72OmwHF2yng2qH4B+/8+V3ti/MfhxSQKZ/Uc/Rp+oZ5tJNtbhElhybYD9HBOQEQXSKx ptTajqmY6tRxlAnu73CeP0kUB+OsbdY31NfRyCTTqEZngYu2Un8GOOG5D/3nhJM7Dilv CXEhVpEFoan2dVbISrqmVDq+mxDAXzfdDJFSnyRprxOZmVYehPO12A8OGK9GLAiAHwwt bAQ6MI0alW/3J2XxJ0hGI++sxzB0VgTmiXO91EDBGfRl2hlw+iLIn7Z0eWrP5936e5Cu kSGQ== X-Gm-Message-State: AOAM531nyOf2uRazLK/T+OLL5tEFrS89L8koh4NLu2+coPNp9xOxIwAF amzBGoyDDEtnrX9SELnxVUqlDSDbMtj2JlpSWUY= X-Google-Smtp-Source: ABdhPJya+WO1A06q3q5c287jg3Do+nRKQQKaitA1xcuxYECP1exwAOyerd6Iv+tDupOrvFukdlCD7w== X-Received: by 2002:a17:906:d152:: with SMTP id br18mr3295964ejb.297.1607975602554; Mon, 14 Dec 2020 11:53:22 -0800 (PST) Received: from localhost (bba163592.alshamil.net.ae. [217.165.22.16]) by smtp.gmail.com with ESMTPSA id w10sm14525939ejq.121.2020.12.14.11.53.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Dec 2020 11:53:22 -0800 (PST) From: Dmitrii Banshchikov To: bpf@vger.kernel.org Cc: Dmitrii Banshchikov , ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@chromium.org, rdna@fb.com Subject: [PATCH bpf-next 2/3] bpf: Support pointer to struct in global func args Date: Mon, 14 Dec 2020 23:52:49 +0400 Message-Id: <5e2ca46ecadda0bde060a7cc0da7edba746b68da.1607973529.git.me@ubique.spb.ru> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Add an ability to pass a pointer to a struct in arguments for a global function. The struct may not have any pointers as it isn't possible to verify them in a general case. Passing a struct pointer to a global function allows to overcome the limit on maximum number of arguments and avoid expensive and tricky workarounds. The implementation consists of two parts: if a global function has an argument that is a pointer to struct then: 1) In btf_check_func_arg_match(): check that the corresponding register points to NULL or to a valid memory region that is large enough to contain the struct. 2) In btf_prepare_func_args(): set the corresponding register type to PTR_TO_MEM_OR_NULL and its size to the size of the struct. Signed-off-by: Dmitrii Banshchikov --- include/linux/bpf_verifier.h | 2 ++ kernel/bpf/btf.c | 59 +++++++++++++++++++++++++++++++----- kernel/bpf/verifier.c | 30 ++++++++++++++++++ 3 files changed, 83 insertions(+), 8 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index e941fe1484e5..dbd00a7743d8 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -467,6 +467,8 @@ bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt); int check_ctx_reg(struct bpf_verifier_env *env, const struct bpf_reg_state *reg, int regno); +int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, + int regno, u32 mem_size); /* this lives here instead of in bpf.h because it needs to dereference tgt_prog */ static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog, diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 8d6bdb4f4d61..0bb5ea523486 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -5352,10 +5352,6 @@ int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog, goto out; } if (btf_type_is_ptr(t)) { - if (reg[i + 1].type == SCALAR_VALUE) { - bpf_log(log, "R%d is not a pointer\n", i + 1); - goto out; - } /* If function expects ctx type in BTF check that caller * is passing PTR_TO_CTX. */ @@ -5370,6 +5366,30 @@ int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog, goto out; continue; } + + t = btf_type_by_id(btf, t->type); + while (btf_type_is_modifier(t)) + t = btf_type_by_id(btf, t->type); + if (btf_type_is_struct(t)) { + u32 mem_size; + const struct btf_type *ret = + btf_resolve_size(btf, t, &mem_size); + + if (IS_ERR(ret)) { + bpf_log(log, + "unable to resolve the size of type '%s': %ld\n", + btf_name_by_offset(btf, + t->name_off), + PTR_ERR(ret)); + return -EINVAL; + } + + if (check_mem_reg(env, ®[i + 1], i + 1, + mem_size)) + goto out; + + continue; + } } bpf_log(log, "Unrecognized arg#%d type %s\n", i, btf_kind_str[BTF_INFO_KIND(t->info)]); @@ -5471,10 +5491,33 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog, reg[i + 1].type = SCALAR_VALUE; continue; } - if (btf_type_is_ptr(t) && - btf_get_prog_ctx_type(log, btf, t, prog_type, i)) { - reg[i + 1].type = PTR_TO_CTX; - continue; + if (btf_type_is_ptr(t)) { + if (btf_get_prog_ctx_type(log, btf, t, prog_type, i)) { + reg[i + 1].type = PTR_TO_CTX; + continue; + } + + t = btf_type_by_id(btf, t->type); + while (btf_type_is_modifier(t)) + t = btf_type_by_id(btf, t->type); + if (btf_type_is_struct(t)) { + const struct btf_type *ret = btf_resolve_size( + btf, t, ®[i + 1].mem_size); + + if (IS_ERR(ret)) { + const char *tname = btf_name_by_offset( + btf, t->name_off); + bpf_log(log, + "unable to resolve the size of type '%s': %ld\n", + tname, PTR_ERR(ret)); + return -EINVAL; + } + + reg[i + 1].type = PTR_TO_MEM_OR_NULL; + reg[i + 1].id = i + 1; + + continue; + } } bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n", i, btf_kind_str[BTF_INFO_KIND(t->info)], tname); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index dee296dbc7a1..a08f85fffdb2 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -3886,6 +3886,29 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, } } +int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, + int regno, u32 mem_size) +{ + if (register_is_null(reg)) + return 0; + + if (reg_type_may_be_null(reg->type)) { + const struct bpf_reg_state saved_reg = *reg; + int rv; + + if (mark_ptr_not_null_reg(reg)) { + verbose(env, "R%d type=%s expected nullable\n", regno, + reg_type_str[reg->type]); + return -EINVAL; + } + rv = check_helper_mem_access(env, regno, mem_size, 1, NULL); + *reg = saved_reg; + return rv; + } + + return check_helper_mem_access(env, regno, mem_size, 1, NULL); +} + /* Implementation details: * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL * Two bpf_map_lookups (even with the same key) will have different reg->id. @@ -11435,6 +11458,13 @@ static int do_check_common(struct bpf_verifier_env *env, int subprog) mark_reg_known_zero(env, regs, i); else if (regs[i].type == SCALAR_VALUE) mark_reg_unknown(env, regs, i); + else if (regs[i].type == PTR_TO_MEM_OR_NULL) { + const u32 mem_size = regs[i].mem_size; + + mark_reg_known_zero(env, regs, i); + regs[i].mem_size = mem_size; + regs[i].id = i; + } } } else { /* 1st arg to a function */ From patchwork Mon Dec 14 19:52:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitrii Banshchikov X-Patchwork-Id: 11972877 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7EDCDC4361B for ; Mon, 14 Dec 2020 19:54:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2E1CB205CB for ; Mon, 14 Dec 2020 19:54:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2502869AbgLNTyP (ORCPT ); Mon, 14 Dec 2020 14:54:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2502870AbgLNTyJ (ORCPT ); Mon, 14 Dec 2020 14:54:09 -0500 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0318AC061794 for ; Mon, 14 Dec 2020 11:53:29 -0800 (PST) Received: by mail-ej1-x633.google.com with SMTP id qw4so24222261ejb.12 for ; Mon, 14 Dec 2020 11:53:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ubique-spb-ru.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=el0TJ7q4MDmfKGG0DgnylUlh4OW6z1KyD9stwxZA2oM=; b=qrE8M8NdgIYQEJhuwz32OyRpHgumS9TgE13cvGkQyMto63GSUspt9H4jDqfa/TfhBx ZJuDQ3wmfi8fcoLYwJT+VGZsoaCSHsodcuN1CR8lUo65TsDvJg+Bu/y9zspsEXVOJm/y BCX/2GcWu8gKF+0aI3S1gODW7LM5frkYHLn6U35cX1pr6QlhbA8SIJU8Wa4vjJ1SlG9L il9fNuuaDUay7ROLkQKLBS2kP8LSn5fYkf0vIzdqnrGhKdcUQ1HD3IRYSJzSanfRtQ89 rdSnGxHtiOHgNU0kYqhZPRLhXT5CsM7E0KKwrudAACOnkJ832J6hTPEO5y+EAyY+Yes6 Wtjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=el0TJ7q4MDmfKGG0DgnylUlh4OW6z1KyD9stwxZA2oM=; b=hvegRulOC4TcpNcxnPS/+gHmJ+fuBr1Xtcs1rlU2ozZmS/zmaG+taPqp2XVeqhdIxC G6MjVGrUeWUvPCyoSKrKn8CAHuXVSN28BHZYYfLjJWylnlsyMXMch1GpEp9M/605X4T9 tGaDDZfhbmQhAgngCwIevvgAutADK1hDr5+NTP+dJv/hfwZeliB5GuzQJVa53nIwpP0m rAAvTNQabsG3lt/Op6MeBjcGjENF2QTmD/KklXxcCQR17bTJwsD2ES0sJDE3gEa7TFkg kTUNZAYaZBW39xA1+sHgxtNTlRkuJoeIs3khB7FXEDFP+DFLWRz3C4fu0TaBQ8MtvylM m4jw== X-Gm-Message-State: AOAM532E/1nYLoR0j5kjAqqif2KsADgAvCFfyjZRKX0oYd6XgRit3egh bHTwngJ0Ljut6uDAH865mFQErPThLPn6AgHpTSM= X-Google-Smtp-Source: ABdhPJw+bMvKSYV9h7byjX6zYD6ZbmM81sSOsHrK9Pv8wy6e8CkugqIwYy8s5MPW+rg/bxqFHOOPfQ== X-Received: by 2002:a17:906:6c94:: with SMTP id s20mr3465457ejr.0.1607975607478; Mon, 14 Dec 2020 11:53:27 -0800 (PST) Received: from localhost (bba163592.alshamil.net.ae. [217.165.22.16]) by smtp.gmail.com with ESMTPSA id rs27sm14814365ejb.21.2020.12.14.11.53.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Dec 2020 11:53:27 -0800 (PST) From: Dmitrii Banshchikov To: bpf@vger.kernel.org Cc: Dmitrii Banshchikov , ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@chromium.org, rdna@fb.com Subject: [PATCH bpf-next 3/3] selftests/bpf: Add unit tests for global functions Date: Mon, 14 Dec 2020 23:52:50 +0400 Message-Id: <4a0f45692b124b7bca139a6c58c131496ec2dc12.1607973529.git.me@ubique.spb.ru> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net test_global_func9 - check valid scenarios for struct pointers test_global_func10 - check that the smaller struct cannot be passed as a the larger one test_global_func11 - check that CTX pointer cannot be passed as a struct pointer test_global_func12 - check access to a null pointer test_global_func13 - check access to an arbitrary pointer value Signed-off-by: Dmitrii Banshchikov --- .../bpf/prog_tests/test_global_funcs.c | 5 ++ .../selftests/bpf/progs/test_global_func10.c | 29 +++++++++ .../selftests/bpf/progs/test_global_func11.c | 19 ++++++ .../selftests/bpf/progs/test_global_func12.c | 21 +++++++ .../selftests/bpf/progs/test_global_func13.c | 24 ++++++++ .../selftests/bpf/progs/test_global_func9.c | 59 +++++++++++++++++++ 6 files changed, 157 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/test_global_func10.c create mode 100644 tools/testing/selftests/bpf/progs/test_global_func11.c create mode 100644 tools/testing/selftests/bpf/progs/test_global_func12.c create mode 100644 tools/testing/selftests/bpf/progs/test_global_func13.c create mode 100644 tools/testing/selftests/bpf/progs/test_global_func9.c diff --git a/tools/testing/selftests/bpf/prog_tests/test_global_funcs.c b/tools/testing/selftests/bpf/prog_tests/test_global_funcs.c index 32e4348b714b..c4895e6c83c2 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_global_funcs.c +++ b/tools/testing/selftests/bpf/prog_tests/test_global_funcs.c @@ -61,6 +61,11 @@ void test_test_global_funcs(void) { "test_global_func6.o" , "modified ctx ptr R2" }, { "test_global_func7.o" , "foo() doesn't return scalar" }, { "test_global_func8.o" }, + { "test_global_func9.o" }, + { "test_global_func10.o", "invalid indirect read from stack off -8+4 size 8" }, + { "test_global_func11.o", "Caller passes invalid args into func#1" }, + { "test_global_func12.o", "invalid mem access 'mem_or_null'" }, + { "test_global_func13.o", "Caller passes invalid args into func#1" }, }; libbpf_print_fn_t old_print_fn = NULL; int err, i, duration = 0; diff --git a/tools/testing/selftests/bpf/progs/test_global_func10.c b/tools/testing/selftests/bpf/progs/test_global_func10.c new file mode 100644 index 000000000000..61c2ae92ce41 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_global_func10.c @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include + +struct Small { + int x; +}; + +struct Big { + int x; + int y; +}; + +__noinline int foo(const struct Big *big) +{ + if (big == 0) + return 0; + + return bpf_get_prandom_u32() < big->y; +} + +SEC("cgroup_skb/ingress") +int test_cls(struct __sk_buff *skb) +{ + const struct Small small = {.x = skb->len }; + + return foo((struct Big *)&small) ? 1 : 0; +} diff --git a/tools/testing/selftests/bpf/progs/test_global_func11.c b/tools/testing/selftests/bpf/progs/test_global_func11.c new file mode 100644 index 000000000000..28488047c849 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_global_func11.c @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include + +struct S { + int x; +}; + +__noinline int foo(const struct S *s) +{ + return s ? bpf_get_prandom_u32() < s->x : 0; +} + +SEC("cgroup_skb/ingress") +int test_cls(struct __sk_buff *skb) +{ + return foo(skb); +} diff --git a/tools/testing/selftests/bpf/progs/test_global_func12.c b/tools/testing/selftests/bpf/progs/test_global_func12.c new file mode 100644 index 000000000000..62343527cc59 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_global_func12.c @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include + +struct S { + int x; +}; + +__noinline int foo(const struct S *s) +{ + return bpf_get_prandom_u32() < s->x; +} + +SEC("cgroup_skb/ingress") +int test_cls(struct __sk_buff *skb) +{ + const struct S s = {.x = skb->len }; + + return foo(&s); +} diff --git a/tools/testing/selftests/bpf/progs/test_global_func13.c b/tools/testing/selftests/bpf/progs/test_global_func13.c new file mode 100644 index 000000000000..ff8897c1ac22 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_global_func13.c @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include + +struct S { + int x; +}; + +__noinline int foo(const struct S *s) +{ + if (s) + return bpf_get_prandom_u32() < s->x; + + return 0; +} + +SEC("cgroup_skb/ingress") +int test_cls(struct __sk_buff *skb) +{ + const struct S *s = (const struct S *)(0xbedabeda); + + return foo(s); +} diff --git a/tools/testing/selftests/bpf/progs/test_global_func9.c b/tools/testing/selftests/bpf/progs/test_global_func9.c new file mode 100644 index 000000000000..17217d0fcd81 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_global_func9.c @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include + +struct S { + int x; +}; + +struct C { + int x; + int y; +}; + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, struct S); +} map SEC(".maps"); + +__noinline int foo(const struct S *s) +{ + if (s) + return bpf_get_prandom_u32() < s->x; + + return 0; +} + +SEC("cgroup_skb/ingress") +int test_cls(struct __sk_buff *skb) +{ + int result = 0; + + { + const struct S s = {.x = skb->len }; + + result |= foo(&s); + } + + { + const __u32 key = 1; + const struct S *s = bpf_map_lookup_elem(&map, &key); + + result |= foo(s); + } + + { + const struct C c = {.x = skb->len, .y = skb->family }; + + result |= foo((const struct S *)&c); + } + + { + result |= foo(NULL); + } + + return result ? 1 : 0; +}