From patchwork Thu Feb 22 15:41:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13567465 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 87B19156981 for ; Thu, 22 Feb 2024 15:41:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708616515; cv=none; b=IBT8aoNX5G9r/5AlURdIO+YaYgEJjG98QLIa28rdL7nXmDIKz8OXUrmDuwhTOtKFn3zIG85ffhPcIxD9QsupQ5Pe6RrJUYOMLpunR6RnLXk1aI+kzEGYEA4bz/uurEIbCcahjqAUirHeffxNGCQXh8EZ7ri7aFVlQ+OAXpGwNeo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708616515; c=relaxed/simple; bh=eWERnZ4O2P1MAnr2O2Nc/Vg2BFWgkCHIIuvFJ0ML5Tk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lzNWNMSNMMmVhMo5N/grNP2IxBJsX6c3Ju+z4Y2sNzc5xPExaQuLXy97fTCmhyRLu7KNm9EqkEohMy/XGiPqDs+ZuAnvgJQMNXgHnQp0rn0MTRTFh6T13mLP/WfmKs+uIDQvFVIbm/bo3xZvtKYipNznLNVRtO6zTEvbQPOlZ+Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=GiDSZ8LB; arc=none smtp.client-ip=209.85.218.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GiDSZ8LB" Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-a3f3d0d2787so282808366b.3 for ; Thu, 22 Feb 2024 07:41:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708616511; x=1709221311; darn=vger.kernel.org; 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=xtBZWMuNmOCn1H0B1IrCDf+7iRL2yIDiVQd+BqYqEe8=; b=GiDSZ8LBedo2ZFennu3ZJ4AbzU2E5NomgMJFyAWttHDvyoLD55ruoOXuWzAz9uPCbl IYIrQ8vL3hVYt0naCvqIpEdQx7YSWQXErnyf5gXR7ZE4Hvt4Ajx/kUvp1AUfuOzdMMAR uWEU6X5RBgv7lLCD1dJrjA120hqYdpIMmceKd+dG7LnZI18hh84IhxTZsz4kOIdMeKA8 At/PorYaqYyNPXXMYWAQVz0QTIK+wKXZ91l9GUJ5W93o2Dmoj5ZSrrnrhyyqzK6cF8MZ gq0LNzZGDy1+KUaGm8A5h3jyasnjzPmPQsw3Uud4XewKV/lsZq0AmsBSK3Q/jI+7tMZa OdHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708616511; x=1709221311; 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=xtBZWMuNmOCn1H0B1IrCDf+7iRL2yIDiVQd+BqYqEe8=; b=RM1r9B50sJLs84i8YQo+htOrniuwVapTTZgQYUJUauUUyU58E77ua9JDmYn8kn7HGl rTMxZSBR92x2SzbaceE/QLRPOKCzmMVci4+KSzAUR6ENBdBNUrCP1IoLsZTI45Ifl0Lv m8Nj1Azo8ctxBN/ZZqn/Do3Kx1I4Aj8FMz/BIyhIlf31/EIs3LZSu0ASGGIyh4RwMkiO 3p05AezfgtsZXUNRh3B3kCky55lg+4r1d3/z8T9deD9HEibUOe1ih6eFznmrs3bfxFoU Ayq+FWZnoKBG8smLa+AbNXZVv3CnNmyRinNrU0MbeCgIEl4NRVmIb+dAPN/2QrkQpYW4 q7ZA== X-Gm-Message-State: AOJu0Yzu3GYqhaD/HwWYJJDiX1HgIX4rc/BQ+dRJV9NorzilC/2uJPjH VVR/t9wzpfT+KIbXRaUyDwUzxh2BBtfq5PTUQUslBLn3tr3hYgX95qqQyUTv X-Google-Smtp-Source: AGHT+IGwFNP4lGc5/8GCVdp7ygenAGwaxvUNKTDhhXRxNybC9iMU2a8FoU+h+lTXwVKPgTIP2RXPYw== X-Received: by 2002:a17:906:f196:b0:a3f:6717:37ae with SMTP id gs22-20020a170906f19600b00a3f671737aemr2454735ejb.69.1708616511185; Thu, 22 Feb 2024 07:41:51 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id sn24-20020a170906629800b00a3e1939b23bsm5725090ejc.127.2024.02.22.07.41.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 07:41:50 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, kuniyu@amazon.com, Eduard Zingerman Subject: [PATCH bpf v3 1/2] bpf: check bpf_func_state->callback_depth when pruning states Date: Thu, 22 Feb 2024 17:41:20 +0200 Message-ID: <20240222154121.6991-2-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240222154121.6991-1-eddyz87@gmail.com> References: <20240222154121.6991-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net When comparing current and cached states verifier should consider bpf_func_state->callback_depth. Current state cannot be pruned against cached state, when current states has more iterations left compared to cached state. Current state has more iterations left when it's callback_depth is smaller. Below is an example illustrating this bug, minimized from mailing list discussion [0] (assume that BPF_F_TEST_STATE_FREQ is set). The example is not a safe program: if loop_cb point (1) is followed by loop_cb point (2), then division by zero is possible at point (4). struct ctx { __u64 a; __u64 b; __u64 c; }; static void loop_cb(int i, struct ctx *ctx) { /* assume that generated code is "fallthrough-first": * if ... == 1 goto * if ... == 2 goto * */ switch (bpf_get_prandom_u32()) { case 1: /* 1 */ ctx->a = 42; return 0; break; case 2: /* 2 */ ctx->b = 42; return 0; break; default: /* 3 */ ctx->c = 42; return 0; break; } } SEC("tc") __failure __flag(BPF_F_TEST_STATE_FREQ) int test(struct __sk_buff *skb) { struct ctx ctx = { 7, 7, 7 }; bpf_loop(2, loop_cb, &ctx, 0); /* 0 */ /* assume generated checks are in-order: .a first */ if (ctx.a == 42 && ctx.b == 42 && ctx.c == 7) asm volatile("r0 /= 0;":::"r0"); /* 4 */ return 0; } Prior to this commit verifier built the following checkpoint tree for this example: .------------------------------------- Checkpoint / State name | .-------------------------------- Code point number | | .---------------------------- Stack state {ctx.a,ctx.b,ctx.c} | | | .------------------- Callback depth in frame #0 v v v v - (0) {7P,7P,7},depth=0 - (3) {7P,7P,7},depth=1 - (0) {7P,7P,42},depth=1 - (3) {7P,7,42},depth=2 - (0) {7P,7,42},depth=2 loop terminates because of depth limit - (4) {7P,7,42},depth=0 predicted false, ctx.a marked precise - (6) exit (a) - (2) {7P,7,42},depth=2 - (0) {7P,42,42},depth=2 loop terminates because of depth limit - (4) {7P,42,42},depth=0 predicted false, ctx.a marked precise - (6) exit (b) - (1) {7P,7P,42},depth=2 - (0) {42P,7P,42},depth=2 loop terminates because of depth limit - (4) {42P,7P,42},depth=0 predicted false, ctx.{a,b} marked precise - (6) exit - (2) {7P,7,7},depth=1 considered safe, pruned using checkpoint (a) (c) - (1) {7P,7P,7},depth=1 considered safe, pruned using checkpoint (b) Here checkpoint (b) has callback_depth of 2, meaning that it would never reach state {42,42,7}. While checkpoint (c) has callback_depth of 1, and thus could yet explore the state {42,42,7} if not pruned prematurely. This commit makes forbids such premature pruning, allowing verifier to explore states sub-tree starting at (c): (c) - (1) {7,7,7P},depth=1 - (0) {42P,7,7P},depth=1 ... - (2) {42,7,7},depth=2 - (0) {42,42,7},depth=2 loop terminates because of depth limit - (4) {42,42,7},depth=0 predicted true, ctx.{a,b,c} marked precise - (5) division by zero [0] https://lore.kernel.org/bpf/9b251840-7cb8-4d17-bd23-1fc8071d8eef@linux.dev/ Fixes: bb124da69c47 ("bpf: keep track of max number of bpf_loop callback iterations") Suggested-by: Yonghong Song Signed-off-by: Eduard Zingerman Acked-by: Yonghong Song --- kernel/bpf/verifier.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index b263f093ee76..ddea9567f755 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -16602,6 +16602,9 @@ static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_stat { int i; + if (old->callback_depth > cur->callback_depth) + return false; + for (i = 0; i < MAX_BPF_REG; i++) if (!regsafe(env, &old->regs[i], &cur->regs[i], &env->idmap_scratch, exact)) From patchwork Thu Feb 22 15:41:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduard Zingerman X-Patchwork-Id: 13567466 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 282C6157E6D for ; Thu, 22 Feb 2024 15:41:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708616516; cv=none; b=k1R1/v+5k0j7GHhOako6mZzXnBuELBjStw5gUVA5KLGNzXOqq/Ca9KtR2UXZCqZX3CD/nQwP2dbP+i9GDsOEzDTH5CAoW5EBQsD6ZQn3uusVl2qIr4sWBbn99uBwc+M2SUvUX819xBdTpur5ZsvzTSoUd9wZ/h4dGWzpHO4aguE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708616516; c=relaxed/simple; bh=GFDgM9Bjs3QsReMVZ3mlCKb79B6yRpxO4t3XLpHWXj8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=j1XfmZE7Gc1J8vR6NJYxvnon9sXf+QGx1AWVJzNioZ89+76ais704Wh2du85AUguvsQclunEh0fcRaGOm2ry2RJ0F41DEOdL89ivsnewMmUp3ylnNRmQkSruISdJ3vZc2CKLamqm/aYQUemRW0ZNiFEc1td2lsCe3Oc/J2xqEI8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SDQrSykx; arc=none smtp.client-ip=209.85.218.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SDQrSykx" Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-a3e8c1e4aa7so249132566b.2 for ; Thu, 22 Feb 2024 07:41:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708616513; x=1709221313; darn=vger.kernel.org; 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=qNRdrDKg+lLjUQljgCVGrxuXBz2+59VRT1ktOLKZFE8=; b=SDQrSykxNPNbOmEKbVJEWEatrXRKLP6xYn6VUXw5E2aVRbiStr2bQL/DVgfovb2S34 /T/GDLSXe53QqSbXB67JI+AVGTlE8hzmNkJqOCwgK6zSjnLKgitCbySHHJ1L4RpC4ZQv DMnVFaR/zi5pBtE+vqrlAWgRg7SNfwZX8q545osd2rD5UT4ZfM24ydSZbN+mYTrWLM9p pCG+DiL/UXkOVzb9c5L2iRkcAsdBKayXA5B/XlmhKN6KvmtHzjN4cdzpJUMNbF9ZLtow omvhfT8k2Gp3kg1kNGkPhjQsHEQ2B9EvtwbxfxE7FBYo/IKnXPcd+0TkxTQpoCf4woze QyGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708616513; x=1709221313; 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=qNRdrDKg+lLjUQljgCVGrxuXBz2+59VRT1ktOLKZFE8=; b=SQDas0oBfLiY1DklIC7Fb52l7HRlvKiMYzkU8SSPORmaRLfW696UQZHLw3CXiEoHeP GZBCCV1cd6Hl+FrusxxNGh8Hi+iAwdh3d6oE8FezBqCT6cB7VaHluQ79yK/N8tzHF/aY 1CfsckyFfHDZObB5dQJpIVPXJKu0bDH1G1lWrZsjfLmyNd7HYqWuyVBEkzpVJCqihDj4 3nh2wIgRLZ+lrxIcy6zlEzdDkzPqIaTCWTUUyANZgMPIqdxs6UoabyHin7QhM/qboF+j 275dGCpk37mWE5NrSIY4veIkBF4T64pfzuUpTge3KaOPPLecaAXsFhVtXzj73sTF2XF+ FpIw== X-Gm-Message-State: AOJu0YxyLwU88CfohCefBMrpvidmh7Pn/Fv9gh68B8LOOjrf5OBRypxB odaBPTUKMDQFqBlwHuZWe222LFPZlYu/HpkuaNBK9SLgfuFbSmWmwBDpt5Gm X-Google-Smtp-Source: AGHT+IHmQjezh9zQ2HQF5xZVJEhZk0cSYTFTjMlJUqm90XN5qoj5ckyZVmK6bjjcdx4lz4ZwiK40iQ== X-Received: by 2002:a17:906:57ca:b0:a3f:ce8:1234 with SMTP id u10-20020a17090657ca00b00a3f0ce81234mr5631103ejr.68.1708616512602; Thu, 22 Feb 2024 07:41:52 -0800 (PST) Received: from localhost.localdomain (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id sn24-20020a170906629800b00a3e1939b23bsm5725090ejc.127.2024.02.22.07.41.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 07:41:52 -0800 (PST) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, kernel-team@fb.com, yonghong.song@linux.dev, kuniyu@amazon.com, Eduard Zingerman Subject: [PATCH bpf v3 2/2] selftests/bpf: test case for callback_depth states pruning logic Date: Thu, 22 Feb 2024 17:41:21 +0200 Message-ID: <20240222154121.6991-3-eddyz87@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240222154121.6991-1-eddyz87@gmail.com> References: <20240222154121.6991-1-eddyz87@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net The test case was minimized from mailing list discussion [0]. It is equivalent to the following C program: struct iter_limit_bug_ctx { __u64 a; __u64 b; __u64 c; }; static __naked void iter_limit_bug_cb(void) { switch (bpf_get_prandom_u32()) { case 1: ctx->a = 42; break; case 2: ctx->b = 42; break; default: ctx->c = 42; break; } } int iter_limit_bug(struct __sk_buff *skb) { struct iter_limit_bug_ctx ctx = { 7, 7, 7 }; bpf_loop(2, iter_limit_bug_cb, &ctx, 0); if (ctx.a == 42 && ctx.b == 42 && ctx.c == 7) asm volatile("r1 /= 0;":::"r1"); return 0; } The main idea is that each loop iteration changes one of the state variables in a non-deterministic manner. Hence it is premature to prune the states that have two iterations left comparing them to states with one iteration left. E.g. {{7,7,7}, callback_depth=0} can reach state {42,42,7}, while {{7,7,7}, callback_depth=1} can't. [0] https://lore.kernel.org/bpf/9b251840-7cb8-4d17-bd23-1fc8071d8eef@linux.dev/ Acked-by: Yonghong Song Signed-off-by: Eduard Zingerman --- .../bpf/progs/verifier_iterating_callbacks.c | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c index 5905e036e0ea..a955a6358206 100644 --- a/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c +++ b/tools/testing/selftests/bpf/progs/verifier_iterating_callbacks.c @@ -239,4 +239,74 @@ int bpf_loop_iter_limit_nested(void *unused) return 1000 * a + b + c; } +struct iter_limit_bug_ctx { + __u64 a; + __u64 b; + __u64 c; +}; + +static __naked void iter_limit_bug_cb(void) +{ + /* This is the same as C code below, but written + * in assembly to control which branches are fall-through. + * + * switch (bpf_get_prandom_u32()) { + * case 1: ctx->a = 42; break; + * case 2: ctx->b = 42; break; + * default: ctx->c = 42; break; + * } + */ + asm volatile ( + "r9 = r2;" + "call %[bpf_get_prandom_u32];" + "r1 = r0;" + "r2 = 42;" + "r0 = 0;" + "if r1 == 0x1 goto 1f;" + "if r1 == 0x2 goto 2f;" + "*(u64 *)(r9 + 16) = r2;" + "exit;" + "1: *(u64 *)(r9 + 0) = r2;" + "exit;" + "2: *(u64 *)(r9 + 8) = r2;" + "exit;" + : + : __imm(bpf_get_prandom_u32) + : __clobber_all + ); +} + +SEC("tc") +__failure +__flag(BPF_F_TEST_STATE_FREQ) +int iter_limit_bug(struct __sk_buff *skb) +{ + struct iter_limit_bug_ctx ctx = { 7, 7, 7 }; + + bpf_loop(2, iter_limit_bug_cb, &ctx, 0); + + /* This is the same as C code below, + * written in assembly to guarantee checks order. + * + * if (ctx.a == 42 && ctx.b == 42 && ctx.c == 7) + * asm volatile("r1 /= 0;":::"r1"); + */ + asm volatile ( + "r1 = *(u64 *)%[ctx_a];" + "if r1 != 42 goto 1f;" + "r1 = *(u64 *)%[ctx_b];" + "if r1 != 42 goto 1f;" + "r1 = *(u64 *)%[ctx_c];" + "if r1 != 7 goto 1f;" + "r1 /= 0;" + "1:" + : + : [ctx_a]"m"(ctx.a), + [ctx_b]"m"(ctx.b), + [ctx_c]"m"(ctx.c) + : "r1" + ); + return 0; +} + char _license[] SEC("license") = "GPL";