From patchwork Fri Nov 18 18:59:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kumar Kartikeya Dwivedi X-Patchwork-Id: 13048628 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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 52426C433FE for ; Fri, 18 Nov 2022 19:00:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241525AbiKRTAs (ORCPT ); Fri, 18 Nov 2022 14:00:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242850AbiKRTAa (ORCPT ); Fri, 18 Nov 2022 14:00:30 -0500 Received: from mail-pf1-x444.google.com (mail-pf1-x444.google.com [IPv6:2607:f8b0:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42F6B2A27C for ; Fri, 18 Nov 2022 11:00:05 -0800 (PST) Received: by mail-pf1-x444.google.com with SMTP id 130so5719192pfu.8 for ; Fri, 18 Nov 2022 11:00:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VNObVLUzMWL/DNto8HuZU0Z5bHWz1PqDjaESNm3v+qA=; b=n117NkfEJSdPPZllVNHgSwGoNGt2YcbXn04Cb1B7VByaPTFh1DLPaO/BreLi02PqJ2 sVJ61ZOWh+7J7OVJsEHZi0dHJ98rffUBvI6R/mFmXGxYFpgqpGm2hnMqZ95eI0x0jCc3 nQZxpAtOycRuiotmsG1tsLPLO1a8tNCZGJa4DO46r7+zTI2uvXtF2M19U/Qkw8NR9TYF mpazxWaUfZKMGiQHf5sy+eR1sUDVzSH9GU9f54Pv7iPTOq9898SVOxn6NgO6zLjzOf47 gMar75tYKk4pqOfsW+VviwgAvaA/d/ddLtA96Or4myJaCPAJkwQXvphyGlNz9sVT9E6S izNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VNObVLUzMWL/DNto8HuZU0Z5bHWz1PqDjaESNm3v+qA=; b=2A7R+DpGGZ6PSbksdhLCLu0p6des1iPFxWcH0qAi16jd642gtJK6GebnFSr0Bhvutc 9/ONZMTgJ+kp8YT6pOF2elmRzkvHgDUihKlSwBfx2R9ggTDq8cHWciuHJR1++DOgvXmF JpjOcE1K0NYnjjo23mHooPOrdSUDjRKKDeHKHHFSygWe0npzqi01FKDRBvvweZgXcJcn Pt1jIYPCZp7oHDwSd5/R+Z+xL455lYq0DNz5H5T6+sYq7T1yVwPwMI14lvDj4GAamI2F btkPnrlp0hxGbypvLHXPX/ZYt5yBfDFAbulMvnXsjnzwDQHwNfC6RkTrRil6NKGV7mMW /wiQ== X-Gm-Message-State: ANoB5pnKjM7Spe5JC0oMWSRPclgyfKKBKaE/cV1WiCsb4qWjRFKz3Ba0 8psOiTcIZHyqpmnKDAau6r2AP17MIVU= X-Google-Smtp-Source: AA0mqf52ANGz/o2yjCvxCivWTP5hHd5tc0Sl3dSBpUpDeomyYCmmyRSzAYI4Y4r2ldOgj5Hxr20rUQ== X-Received: by 2002:a05:6a00:18a9:b0:572:6da6:218e with SMTP id x41-20020a056a0018a900b005726da6218emr9484837pfh.1.1668798004517; Fri, 18 Nov 2022 11:00:04 -0800 (PST) Received: from localhost ([14.96.13.220]) by smtp.gmail.com with ESMTPSA id d20-20020aa797b4000000b005722f4dfbefsm3468598pfq.193.2022.11.18.11.00.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Nov 2022 11:00:04 -0800 (PST) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau Subject: [PATCH bpf-next v1 1/2] bpf: Disallow calling bpf_obj_new_impl on bpf_mem_alloc_init failure Date: Sat, 19 Nov 2022 00:29:37 +0530 Message-Id: <20221118185938.2139616-2-memxor@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221118185938.2139616-1-memxor@gmail.com> References: <20221118185938.2139616-1-memxor@gmail.com> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2408; i=memxor@gmail.com; h=from:subject; bh=hO9444nz970VOtyxTd+RGoyPc12iJAZ/BKHWlQRgS54=; b=owEBbQKS/ZANAwAIAUzgyIZIvxHKAcsmYgBjd9WicXsmm8LVOQl/JB/4ifro6mjVEqhKGsIyRdC2 ZRZgI+iJAjMEAAEIAB0WIQRLvip+Buz51YI8YRFM4MiGSL8RygUCY3fVogAKCRBM4MiGSL8RymzfD/ wMhFs5lPxIm9qI1kSPq3EuBPumSsRtSSy15Gl5U1/zxNBlnes6YH07Afwqm3ux0DxuElheBns9ukoI v2YCVC3sR9KPJxfCHN+rTsPbaOUzJ5mQh3mJLwKjlZXXHdsS3wCZ7Jg7++irRTMcF5+QNqjtBRCIfO 7jlVbfnTNR2c/M98dHSlznewT4D0Suz8JoM3E9Six3NFBnJyE+wmmGT6hUWtc7uNOhdjrSMq9eCn33 rcPOL2WCRoxrU3D5SHJHhcF1Vt7WesTHl5Kj0YB840hpZcA8HBWWt2CQNjf7j9nasxwh3jPpJQNVTf l/w6Ggnt8SK4kG91+KbyG1tp3RJKpANpXH7kolRLQZQcvsGDiGyFslTtKcwUYMMb72j7oabPqsXKio Y3zmn+QXIR3sRfUpODfqgo9UAQBP+wPPIIxKuQaksqXpAPFL8xmRWH9vEsX7CvJNr0j8BRvTXfLmNq kS5Jc8k9deDiMmxaU4DP8XHyaCQ670ivp16ik8QJw97lKzEUb5GRNGdRqS6DEI8wbrCbllEllNhPQz PTKu5aF+IE1cWWM1AW3CRu+IWM79tWNBtfFBdLBtBNiB90inHmG6fQURG9AmlWbiPAIFvmAXTseSb5 jh9Ri3/61kfjZaK8UAA3SmSTpPKhn/mnfTsNq3dG4v+CUEUjf2O7LwS8SZQA== X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=4BBE2A7E06ECF9D5823C61114CE0C88648BF11CA Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Instead of checking bpf_global_ma_set at runtime on each allocation inside bpf_obj_new_impl, simply disallow calling the kfunc in case bpf_global_ma initialization failed during program verification. The error generated when bpf_global_ma initialization fails: ... 21: (18) r1 = 0x7 ; R1_w=7 23: (b7) r2 = 0 ; R2_w=0 24: (85) call bpf_obj_new_impl#36585 bpf_global_ma initialization failed, can't call bpf_obj_new_impl calling kernel function bpf_obj_new_impl is not allowed Suggested-by: Alexei Starovoitov Signed-off-by: Kumar Kartikeya Dwivedi --- kernel/bpf/helpers.c | 2 -- kernel/bpf/verifier.c | 13 ++++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 212e791d7452..bc02f55adc1f 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -1760,8 +1760,6 @@ void *bpf_obj_new_impl(u64 local_type_id__k, void *meta__ign) u64 size = local_type_id__k; void *p; - if (unlikely(!bpf_global_ma_set)) - return NULL; p = bpf_mem_alloc(&bpf_global_ma, size); if (!p) return NULL; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 195d24316750..f04bee7934a8 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -8746,6 +8746,17 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ return 0; } +static bool is_kfunc_disabled(struct bpf_verifier_env *env, const struct btf *btf, u32 func_id) +{ + if (btf != btf_vmlinux) + return false; + if (!bpf_global_ma_set && func_id == special_kfunc_list[KF_bpf_obj_new_impl]) { + verbose(env, "bpf_global_ma initialization failed, can't call bpf_obj_new_impl\n"); + return true; + } + return false; +} + static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, int *insn_idx_p) { @@ -8773,7 +8784,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, func_proto = btf_type_by_id(desc_btf, func->type); kfunc_flags = btf_kfunc_id_set_contains(desc_btf, resolve_prog_type(env->prog), func_id); - if (!kfunc_flags) { + if (!kfunc_flags || is_kfunc_disabled(env, desc_btf, func_id)) { verbose(env, "calling kernel function %s is not allowed\n", func_name); return -EACCES; From patchwork Fri Nov 18 18:59:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kumar Kartikeya Dwivedi X-Patchwork-Id: 13048629 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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1B56CC43217 for ; Fri, 18 Nov 2022 19:00:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241759AbiKRTAt (ORCPT ); Fri, 18 Nov 2022 14:00:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242937AbiKRTAa (ORCPT ); Fri, 18 Nov 2022 14:00:30 -0500 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85B382B268 for ; Fri, 18 Nov 2022 11:00:09 -0800 (PST) Received: by mail-pf1-x441.google.com with SMTP id q9so5737564pfg.5 for ; Fri, 18 Nov 2022 11:00:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dnVNMob24m9mgHfPtleTP0yfbbuXtLElRpSIJwbEj0s=; b=d7RT7grMVwf4OvOE8K5UtldSfszX39OjCZakthlLNrC69We6wTX6Udynuoc9059Qyr umjFZxWV9w3JDn+V9fF2LOOcbW+OUWlS6IStwXh2w/hxgGO36erEvJlgnCk3gWU4eWse A++bSRk/eeId5GDyYTvPUFuicaUznp3aXuCPbFhKWZ0SB35oTt0hX5UpnWYelWYwcvGu oa4gT77k5YoQ5jkYKRy6t2CfMWBuTrMzLWLC1txuZhHZMzw/YkGg3gwaua9QZRcxuAz+ mXYRwX/VwrgyNowGBq61ZO5bUO4nT0DvLZyBQiIcmWNjkkdGjUFTspU2ZCAl0QkyYWKp oaRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dnVNMob24m9mgHfPtleTP0yfbbuXtLElRpSIJwbEj0s=; b=qN1BIJogzGIIff163k+izVlRIBi1QV7RODPvocjdWcOJgIlUIoCM+EkBlsjYYYTUw4 14TGjWR8hLXhz5i6ekJdzsoqjqpnTVH6ovmjWaU9DiE6iyqrdcJkwKl2K8263scPMUev EXsVbVTlT4MBJFDCIV0P1P/bTgJqg7uGpXEpJmHEElfQkMPwKUFEvp/1en2bq/of0so1 +MAocvSo2rMRZM1GEc0D5ZMWJrGUNB51bXK8bsvIp3WND2J5v7OHVftOWG0OhJTPi6C2 QhrwMaY8Wd2rtLsf0WBBKuHx42J/jR4M6ACWVxX2wP9+yndhlDh1Tz83ejgPxpXP+nBX zQCQ== X-Gm-Message-State: ANoB5pkQcX6cfC/12OcQ5wm237PM5DizPfu/ezFyvB7HfIiu0KwJhUJy ckcOe4RraFDbvVdQ6TexLSsgu3/AZuE= X-Google-Smtp-Source: AA0mqf4WxbDKr+VHzX6B2SifYLDHSJB/VnotKjuJqStLwD3A4Qn94j/DlTvkJQvOfWKWdFM8BI/eTQ== X-Received: by 2002:a63:2105:0:b0:476:e987:fcfd with SMTP id h5-20020a632105000000b00476e987fcfdmr7829120pgh.29.1668798007777; Fri, 18 Nov 2022 11:00:07 -0800 (PST) Received: from localhost ([14.96.13.220]) by smtp.gmail.com with ESMTPSA id l125-20020a622583000000b0056baca45977sm3535110pfl.21.2022.11.18.11.00.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Nov 2022 11:00:07 -0800 (PST) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau Subject: [PATCH bpf-next v1 2/2] selftests/bpf: Skip spin lock failure test on s390x Date: Sat, 19 Nov 2022 00:29:38 +0530 Message-Id: <20221118185938.2139616-3-memxor@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221118185938.2139616-1-memxor@gmail.com> References: <20221118185938.2139616-1-memxor@gmail.com> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1158; i=memxor@gmail.com; h=from:subject; bh=NOuDm6z0AAenll3MGsNv6NfyBuJdubDOkWN+3DsfDtU=; b=owEBbQKS/ZANAwAIAUzgyIZIvxHKAcsmYgBjd9WiLB6fpGrK1l3zQzh5GJJXKKLe4LGrb2dVV+Sp rHYQ6WiJAjMEAAEIAB0WIQRLvip+Buz51YI8YRFM4MiGSL8RygUCY3fVogAKCRBM4MiGSL8RyoAlD/ 9vqSYOr9bf2Os3TRWCls/EWSZudgjvoUYLaSpgjNvh+oNxnew4wVB9JBC52jbU1336LJewmUPSo3B9 GyzQ0dFNbYJfZ+OnY1Rsa0f+zECdPx2F7sQUSrPM+7iq7372d4sS5Ai2NRTP+VY1zKoPhj3QjOzLeV l93S0jBGX9Q0ZxvennsXjT7R6nm0mEe3Tpk746eX/mGKnYUxINJJT959o0MmtkV5G+sfixPkj7wxMC al+dvUuuMaVpdwc2Ls8+DJh9c9FzBMP6BrBQIOr+6RdzqpV/3kN30mdDLc/3PcXoc+Xy8MGLq6ciL8 M6IaE6Kb/VS+0tW6O6fOslw7xWxGWP2Pj/MULLpAMSgyGd4QkoJYLZOkZ+jNNaktpKNRKSEBsWCNU6 HeswKCw0NEBvxbv2UyOpdWANu+xcG3X4gIzEwUofTakH8dRTxtxR8+VIz+9yaTCFNF+Yu9hdkPnjdy 0oYewtNAV03QSnJSs4h4Op8oqy/+2XH1w/ha2RFYHoq4oDcQthsiZxmFgP0PVnj31xHzPsn0jae+O9 83SigT3frNWkSpP5y2xn6dZmCb0cx1XIeMsCrIwAenAi7evSDNOxvOGDvi3dFsEtYn3eai/bp+wnZP WQclPFEt5hZbHs8yeBf+5+3wnyMl7Iqf9cVc60VdjuIRH0SlKGQ6pIzXf7IA== X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=4BBE2A7E06ECF9D5823C61114CE0C88648BF11CA Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Instead of adding the whole test to DENYLIST.s390x, which also has success test cases that should be run, just skip over failure test cases in case the JIT does not support kfuncs. Signed-off-by: Kumar Kartikeya Dwivedi --- tools/testing/selftests/bpf/prog_tests/spin_lock.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/spin_lock.c b/tools/testing/selftests/bpf/prog_tests/spin_lock.c index 72282e92a78a..d9270bd3d920 100644 --- a/tools/testing/selftests/bpf/prog_tests/spin_lock.c +++ b/tools/testing/selftests/bpf/prog_tests/spin_lock.c @@ -68,6 +68,12 @@ static void test_spin_lock_fail_prog(const char *prog_name, const char *err_msg) if (!ASSERT_ERR(ret, "test_spin_lock_fail__load must fail")) goto end; + /* Skip check if JIT does not support kfuncs */ + if (strstr(log_buf, "JIT does not support calling kernel function")) { + test__skip(); + goto end; + } + if (!ASSERT_OK_PTR(strstr(log_buf, err_msg), "expected error message")) { fprintf(stderr, "Expected: %s\n", err_msg); fprintf(stderr, "Verifier: %s\n", log_buf);