From patchwork Thu Nov 7 21:47:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tao Lyu X-Patchwork-Id: 13867219 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp5.epfl.ch (smtp5.epfl.ch [128.178.224.8]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCF702185A8 for ; Thu, 7 Nov 2024 21:47:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=128.178.224.8 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731016079; cv=none; b=r6//R1kD2VJlUwv4d41/3bEV6wg6c2qmRoGdKm0+HEBOnzHYICNUNsvAuNeua6RUAvpWc3tA2yZvl+Oz6ZOZGeLo/Ch0H0XNgvnkdHRUCmJZjLs+Rk6y6py/d2BAwclJhINCmbUF5S2CvsAziXjz6VXCT4h8tjHit67XmyWvFDI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731016079; c=relaxed/simple; bh=jdPj7QSU++jGS5CNJZcnDyWKnxQWmvx2ow4QQemaTfk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=EPV2EAiB5vbReH1c6kEXsKw0NqnvZ3KlIrZNIpsy9tomuZJmXOzehLedaO55iuaR3cPooFnMYceBFTwHL16qEuqgIZrWr2/NBpqiCvQ6O6x4zYsyEby9itK8g4NfzbhlSsg42uRnCZFVBJv1rGTeARiZQzjd5egqk8wxL2BhWu8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=epfl.ch; spf=pass smtp.mailfrom=epfl.ch; dkim=pass (1024-bit key) header.d=epfl.ch header.i=@epfl.ch header.b=ws37LnI8; arc=none smtp.client-ip=128.178.224.8 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=epfl.ch Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=epfl.ch Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=epfl.ch header.i=@epfl.ch header.b="ws37LnI8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=epfl.ch; s=epfl; t=1731016069; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Transfer-Encoding:Content-Type; bh=jdPj7QSU++jGS5CNJZcnDyWKnxQWmvx2ow4QQemaTfk=; b=ws37LnI8rKHwXrQrvWzkngCsOUrvhy68KOl+XqUCXwkpwHowCuSMfxoreLDaO2Tu/ Vapr7b3BMJ0mChQFNYY2zzGTbqTsVGRYF8kEwz4G7G2aRtituezVtruj5m9vLrQeR w1yjAkX7XBMKZUs33/3FIREmRhvic4I5U4+ejVclE= Received: (qmail 5136 invoked by uid 107); 7 Nov 2024 21:47:49 -0000 Received: from ax-snat-224-178.epfl.ch (HELO ewa07.intranet.epfl.ch) (192.168.224.178) (TLS, ECDHE-RSA-AES256-GCM-SHA384 (P-256 curve) cipher) by mail.epfl.ch (AngelmatoPhylax SMTP proxy) with ESMTPS; Thu, 07 Nov 2024 22:47:49 +0100 X-EPFL-Auth: D2/sjICR6gH59LaPuXEZhDyzqpPWxeecltJSmjS3jlOLqMXQHsg= Received: from rs3labsrv2.iccluster.epfl.ch (10.90.46.62) by ewa07.intranet.epfl.ch (128.178.224.178) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Thu, 7 Nov 2024 22:47:49 +0100 From: Tao Lyu To: , , , , , , CC: , Tao Lyu Subject: [PATCH 1/2] bpf: Check if iter args are stack pointers Date: Thu, 7 Nov 2024 22:47:35 +0100 Message-ID: <20241107214736.347630-2-tao.lyu@epfl.ch> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241107214736.347630-1-tao.lyu@epfl.ch> References: <20241107214736.347630-1-tao.lyu@epfl.ch> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: ewa04.intranet.epfl.ch (128.178.224.170) To ewa07.intranet.epfl.ch (128.178.224.178) X-Patchwork-Delegate: bpf@iogearbox.net The verifier misses the type checking on iter arguments, so any pointer types (e.g., map value pointers) can be passed as iter arguments. We fix this issue by adding a type check to ensure the passed iter arguments are in the type of PTR_TO_STACK. Fixes: 06accc8779c1 ("bpf: add support for open-coded iterator loops") Signed-off-by: Tao Lyu --- kernel/bpf/verifier.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 797cf3ed32e0..98afdcecefbc 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12234,6 +12234,11 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ return -EINVAL; } } + /* Ensure the iter arg is a stack pointer */ + if (reg->type != PTR_TO_STACK) { + verbose(env, "arg#%d expected pointer to the iterator\n", i); + return -EINVAL; + } ret = process_iter_arg(env, regno, insn_idx, meta); if (ret < 0) return ret; From patchwork Thu Nov 7 21:47:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tao Lyu X-Patchwork-Id: 13867220 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp0.epfl.ch (smtp0.epfl.ch [128.178.224.218]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D7E5F2185B0 for ; Thu, 7 Nov 2024 21:47:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=128.178.224.218 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731016080; cv=none; b=lXIt8Co5HPBxVnXbWOAKlQ+RZ+l4OAxvQzDzRSgsbLZpbxa9QJHClaspYeZcEjeXPmD1iPPDn9WkDMNBcpkJTltubwulkLMllFihwXQI/ANidEKpD/GM/cNBELGyp/ajxEyxZ/0GvL7s842QnTyDVne1wPo9Ow+DCsruSXf1S/w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731016080; c=relaxed/simple; bh=r9plYawaNrI6VpRkU6YGwkayLudP/q1oDIkXYwWoGzE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=aETqKqF5MNFm8rg9qw0dCAeoHenNyT7sFcXaP7VnjPWx7y6JoU1YVzed7YLnR3nap1VagXEvo545kJx5yDao8MTgX5i2fGBcA5aUFBRjcdqNYLIRiBa1CIHsfdlNgc73L+ObFJeQ7F9gYlPelzmlfc6MQdowGXHU9c3J64UGwkk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=epfl.ch; spf=pass smtp.mailfrom=epfl.ch; dkim=pass (1024-bit key) header.d=epfl.ch header.i=@epfl.ch header.b=4GcFrYQw; arc=none smtp.client-ip=128.178.224.218 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=epfl.ch Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=epfl.ch Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=epfl.ch header.i=@epfl.ch header.b="4GcFrYQw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=epfl.ch; s=epfl; t=1731016070; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Transfer-Encoding:Content-Type; bh=r9plYawaNrI6VpRkU6YGwkayLudP/q1oDIkXYwWoGzE=; b=4GcFrYQw0j1B6lCVrbrFVHbdwa8fTvG1MtwxhNRBEvEuf4rASJk7zs+2LZC+atT8h W5bGErbVYx+pKlvGF39pgysEWTSr22L5r5H+hTZppmyWKtK5gNCoM9OYblvekPPgu tEUyyhy51NFCo5MSJ4ntuCf942epsBRlrG6dlrk1c= Received: (qmail 24626 invoked by uid 107); 7 Nov 2024 21:47:50 -0000 Received: from ax-snat-224-178.epfl.ch (HELO ewa07.intranet.epfl.ch) (192.168.224.178) (TLS, ECDHE-RSA-AES256-GCM-SHA384 (P-256 curve) cipher) by mail.epfl.ch (AngelmatoPhylax SMTP proxy) with ESMTPS; Thu, 07 Nov 2024 22:47:50 +0100 X-EPFL-Auth: VNs8glDkrlIX4XHkTClhpYSCUj7wn/g1YlyCi8cUx1xLGuj3UKo= Received: from rs3labsrv2.iccluster.epfl.ch (10.90.46.62) by ewa07.intranet.epfl.ch (128.178.224.178) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Thu, 7 Nov 2024 22:47:50 +0100 From: Tao Lyu To: , , , , , , CC: , Tao Lyu Subject: [PATCH 2/2] selftests/bpf: Add a test for the type checking of iter args Date: Thu, 7 Nov 2024 22:47:36 +0100 Message-ID: <20241107214736.347630-3-tao.lyu@epfl.ch> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241107214736.347630-1-tao.lyu@epfl.ch> References: <20241107214736.347630-1-tao.lyu@epfl.ch> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: ewa04.intranet.epfl.ch (128.178.224.170) To ewa07.intranet.epfl.ch (128.178.224.178) X-Patchwork-Delegate: bpf@iogearbox.net This is a selftest for the type checking of iter arguments. When passing a PTR_TO_MAP_VALUE to bpf_iter_num_* kfuncs, the program should be rejected. Signed-off-by: Tao Lyu --- tools/testing/selftests/bpf/progs/iters.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/iters.c b/tools/testing/selftests/bpf/progs/iters.c index ef70b88bccb2..613037ececb9 100644 --- a/tools/testing/selftests/bpf/progs/iters.c +++ b/tools/testing/selftests/bpf/progs/iters.c @@ -1486,4 +1486,24 @@ int iter_subprog_check_stacksafe(const void *ctx) return 0; } +SEC("raw_tp") +__failure __msg("arg#0 expected pointer to the iterator") +int iter_check_arg_type(const void *ctx) +{ + struct bpf_iter_num it; + + int *map_val = NULL; + int key = 0; + + map_val = bpf_map_lookup_elem(&arr_map, &key); + if (!map_val) + return 0; + + bpf_iter_num_new(&it, 0, 3); + while (bpf_iter_num_next((struct bpf_iter_num *)map_val)) + bpf_iter_num_destroy(&it); + + return 0; +} + char _license[] SEC("license") = "GPL";