From patchwork Fri Aug 23 01:46:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shung-Hsi Yu X-Patchwork-Id: 13774494 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (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 2271FA934 for ; Fri, 23 Aug 2024 01:46:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724377606; cv=none; b=hpYZtZddAA7owWX5wJyyRV+MIPpSTxc0mwObm3OViGMZ6SJ+s7lwfPhdwNu4g6pv35Cx2ciQ2oU79lDxsLP+dKRPZXN7z2Zp94An2HPhJQ3UXYTEsyoJO78IGW/BLgJ/oNuvfE75aa9diYODJZmOh8HHTIRgTpfhix2HHqfj+jc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724377606; c=relaxed/simple; bh=sGl2nL6mq3MynEP9qoi7xkphD3I99W2n1NgVTRy6SXI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=VxZW7VS297TpjHb+aal6MgqeNCw8WThc1n3HVS6k1Tojw9eujwHaG2e1WYwufnemWMxFCUGjDw6n7cqm0hAxmkSdi3uZbrAEtF5R9jHmcTNw/eQ26c8X4sCR6g/6KuAuk705kWcFEfzUJ9zIJvZ7bPKIIOjDnPP1eLrQi5cWOxI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=Mvqd39yN; arc=none smtp.client-ip=209.85.221.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="Mvqd39yN" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-371a9bea8d4so678096f8f.2 for ; Thu, 22 Aug 2024 18:46:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1724377602; x=1724982402; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=sh2/784i7y6ih3W1qXtxCpk2D8g/ZXKSQt7qq695bYg=; b=Mvqd39yNxVMa9etvhhkC1cUvWjE3u2ap9noWLuuF7vp1W9bCdftDZuIeGC4GNrMXzY fi2ZCzBTJME8KXAoOrUydWGrr+EHhMvuE5EHn6pRO4Be9YjvX1I8aQnjdnDIhvkoNwbP FVnCLQ1010J1COXWeYwzQ4hD016TeXgCWvvv++NGLDkSImSRzEkkMI3bTfIs6oEhr+SS DiXRE3taAi1VhbRUbt12LXDxkZnkp9Rneb045XV2veroa6MO1fYZnStyMvZ1rhnD6BsZ 86hQEj8lQYv83mdT82QnzInSsXilZ+205js9t/uhXVp0Z73ruLbfeYrKQOQQ93cA1Yot V2AQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724377602; x=1724982402; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=sh2/784i7y6ih3W1qXtxCpk2D8g/ZXKSQt7qq695bYg=; b=uCN/ThBnZL6kMj3oYIrk6t4LGT2Gc7p3EURG48ohKm7tFQzsTGkuwMH5l+yGGse7ge MIAeaoHpwIBhfktCvCX2TZxX/rMfQ6huSjfrkgVKEBKTM9+qlTnaCe3E6ETYDXkQPcQx ZyDKhFIVVwQgHiPqljlx+AlNcVnMwsUlqkPCKbeCiTEXBDJjq20C4ByN+ziuWfwE2Jn4 Yj8h7ESdeonYYSIYaBMx4NKCYrkfbgWSUQmrXcu8iB7Ywg4GZmWrEMntakWiuQZG8Neo gLk3JfynqcIhSpgeqzlWQtyIbraon4vsCZezVf5PkHHfuzbp126BK+/TGlchPvi/iYrF AF8A== X-Gm-Message-State: AOJu0YyE4dAtFo9g+AcDVsNQDbjB97xYZmR4GV0L0kxkZBVRF7cqqVRs m04p/t8bihdmt6LDjLmKgdAfCQjptEwUkTRFS/PDP8EL7QXgcDS9IFS8cUkGAOs= X-Google-Smtp-Source: AGHT+IF7CyDabRkMdoNMuAnD2PW4KlZA09KkyuQsdjJy5JrfXQJbkF1i2OMKkT5iHdDF8mZqXFiDdg== X-Received: by 2002:adf:e3c1:0:b0:368:669c:3bd3 with SMTP id ffacd0b85a97d-373118c84fbmr317594f8f.48.1724377602318; Thu, 22 Aug 2024 18:46:42 -0700 (PDT) Received: from localhost (27-51-129-77.adsl.fetnet.net. [27.51.129.77]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7144401ac8fsm378324b3a.19.2024.08.22.18.46.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 18:46:41 -0700 (PDT) From: Shung-Hsi Yu To: stable@vger.kernel.org, Sasha Levin Cc: bpf@vger.kernel.org, Shung-Hsi Yu , Eduard Zingerman , Daniel Hodges , Yonghong Song , Alexei Starovoitov Subject: [PATCH stable 6.10 1/2] bpf: Fix a kernel verifier crash in stacksafe() Date: Fri, 23 Aug 2024 09:46:30 +0800 Message-ID: <20240823014631.114866-1-shung-hsi.yu@suse.com> X-Mailer: git-send-email 2.46.0 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 From: Yonghong Song [ Upstream commit bed2eb964c70b780fb55925892a74f26cb590b25 ] Daniel Hodges reported a kernel verifier crash when playing with sched-ext. Further investigation shows that the crash is due to invalid memory access in stacksafe(). More specifically, it is the following code: if (exact != NOT_EXACT && old->stack[spi].slot_type[i % BPF_REG_SIZE] != cur->stack[spi].slot_type[i % BPF_REG_SIZE]) return false; The 'i' iterates old->allocated_stack. If cur->allocated_stack < old->allocated_stack the out-of-bound access will happen. To fix the issue add 'i >= cur->allocated_stack' check such that if the condition is true, stacksafe() should fail. Otherwise, cur->stack[spi].slot_type[i % BPF_REG_SIZE] memory access is legal. Fixes: 2793a8b015f7 ("bpf: exact states comparison for iterator convergence checks") Cc: Eduard Zingerman Reported-by: Daniel Hodges Acked-by: Eduard Zingerman Signed-off-by: Yonghong Song Link: https://lore.kernel.org/r/20240812214847.213612-1-yonghong.song@linux.dev Signed-off-by: Alexei Starovoitov Signed-off-by: Shung-Hsi Yu --- I see this patch itself was already picked up[1] by Sasha. This thread additional includes the associated selftest that was sent in the same series (not entirely sure if backporting selftest goes against the stable backporting rule though). 1: https://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git/commit/queue-6.10/bpf-fix-a-kernel-verifier-crash-in-stacksafe.patch?id=e1b8b4a2ab5985f048c5e4ba7575deaa7bb65df7 --- kernel/bpf/verifier.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index a8845cc299fe..521bd7efae03 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -16881,8 +16881,9 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, spi = i / BPF_REG_SIZE; if (exact != NOT_EXACT && - old->stack[spi].slot_type[i % BPF_REG_SIZE] != - cur->stack[spi].slot_type[i % BPF_REG_SIZE]) + (i >= cur->allocated_stack || + old->stack[spi].slot_type[i % BPF_REG_SIZE] != + cur->stack[spi].slot_type[i % BPF_REG_SIZE])) return false; if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ) From patchwork Fri Aug 23 01:46:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shung-Hsi Yu X-Patchwork-Id: 13774495 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 8D208171C2 for ; Fri, 23 Aug 2024 01:46:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724377612; cv=none; b=UKs+YgBvuWO4YF5vX4TYTpZX8KsP0+ueVElrvfZy84uFr/evESszFVvyKDKU4+8Il6jCoYYm4svnXR2qLLN/R3AV/gL3ZNRHTJ/LpEENAtPcslqCUCfOCgPRh3aLSc8LxKZDVV4RoLICKVbkXwqbOibKO1vXLTj8EwIIIb+j+h4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724377612; c=relaxed/simple; bh=GE1g0r64qT92If7xW8WdHoAA4djZYeFOynlZtd+eUyU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mo9jmNN+oYQV0vRC/OVXL6O62ZtyR2i6f163K/Ow2Sfud7LOhqUKSDnauSrRg0XVpZ0z+5tkqO2SW1+YReWjP+xvSKIcaShsQP5EUB60FN5PRw1kvM1plHnkXHzOHIuzzJ2SGbVE5LIeiBQK67vYErrKD3qRsqKCH2zHtQX1Wz4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=eF7peOQn; arc=none smtp.client-ip=209.85.128.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="eF7peOQn" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-42808071810so11206425e9.1 for ; Thu, 22 Aug 2024 18:46:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1724377609; x=1724982409; 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=QYzWq5z7WQYhVMJfatGwNPUU4L/alJEM3TD5Z1W4W28=; b=eF7peOQnSdTJxle+KWzXrONmXd7HhuuCPTfV6mxbEDGWesKvQ9sG/Am86oU3u3Yofx ROPc2L0myk0fJ6Ku7bEJZVUWwWovIPlYxqmGmfHFPrh+x1+Xw76644XsYFG4599y7ZfW xzN0UpVeECqgv2i+FZGBBWr7TfwPbOZ++HXXq1SSjJ2RN2TMdQxTjOnJ6XcsruhLYO1C wvQPVenlIqHJntXZKNbvOebSJcguZPsVsY5h8fsNQOdcawoh+dM0PwMZXoJSplNrbfYS ZaxxzEX3KiigKDUCXe9M/7Mxk5z4uztnb/jjsVHqYR7qiXh5ADcwZf+iNW1sxYjY6tAR dIJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724377609; x=1724982409; 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=QYzWq5z7WQYhVMJfatGwNPUU4L/alJEM3TD5Z1W4W28=; b=bv9BQrl2jmsUEswy6jyxSkiYm39WWi/bok5+2Xcr/brSUVcok7gW7a1PIbrKbf41mE bihrw/q0X82ekNfkOFhvCoZh7ayQTve7W/MslJpT+E8oU365WsLecNpLYg/UwoM4w8fI 8ld3YnB+iQ5kWtDmDkJvNqfXIJ5hvXK9cQXT3Ok66byV9lj8eqG9z4NkzcmbaidHh+zj KHqTifmFNH8ix5wD7/yWWS80vy95/TQRpPNagCjbf4xsiWVCwr0/BtSwqVChINUoYdUP ffak6yRlfmaBn0KlCGsyU/42hPlQp9cnAGEbfxCIaMEa1P1g3ZGKCqhn5DMxR8bypj/H SqOw== X-Gm-Message-State: AOJu0YzxbrTrqtcmAwEAyiuopOZqklS9GtiD9VWW5+oPdgds9VybTDuA jy0GsW2SNPeTT2sgitLu2Teql7CURLzZwUJ2pxpwBt1pWKvJWh4COR1SN5nxxaA= X-Google-Smtp-Source: AGHT+IF8EM5OpS8vaaRheq/z7Al8vYW4FlT2Cp6tbBG+VyeOeI4ZZMj0Tcud+GD0ao4gE76dFf6/7Q== X-Received: by 2002:a05:6000:1fa3:b0:368:504d:c3a4 with SMTP id ffacd0b85a97d-3731185c346mr377428f8f.17.1724377608804; Thu, 22 Aug 2024 18:46:48 -0700 (PDT) Received: from localhost (27-51-129-77.adsl.fetnet.net. [27.51.129.77]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7cd9ad55a94sm2031326a12.57.2024.08.22.18.46.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 18:46:48 -0700 (PDT) From: Shung-Hsi Yu To: stable@vger.kernel.org, Sasha Levin Cc: bpf@vger.kernel.org, Shung-Hsi Yu , Yonghong Song , Alexei Starovoitov Subject: [PATCH stable 6.10 2/2] selftests/bpf: Add a test to verify previous stacksafe() fix Date: Fri, 23 Aug 2024 09:46:31 +0800 Message-ID: <20240823014631.114866-2-shung-hsi.yu@suse.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240823014631.114866-1-shung-hsi.yu@suse.com> References: <20240823014631.114866-1-shung-hsi.yu@suse.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 From: Yonghong Song [ Upstream commit 662c3e2db00f92e50c26e9dc4fe47c52223d9982 ] A selftest is added such that without the previous patch, a crash can happen. With the previous patch, the test can run successfully. The new test is written in a way which mimics original crash case: main_prog static_prog_1 static_prog_2 where static_prog_1 has different paths to static_prog_2 and some path has stack allocated and some other path does not. A stacksafe() checking in static_prog_2() triggered the crash. Signed-off-by: Yonghong Song Link: https://lore.kernel.org/r/20240812214852.214037-1-yonghong.song@linux.dev Signed-off-by: Alexei Starovoitov Signed-off-by: Shung-Hsi Yu --- tools/testing/selftests/bpf/progs/iters.c | 54 +++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/iters.c b/tools/testing/selftests/bpf/progs/iters.c index fe65e0952a1e..179bfe25dbc6 100644 --- a/tools/testing/selftests/bpf/progs/iters.c +++ b/tools/testing/selftests/bpf/progs/iters.c @@ -1434,4 +1434,58 @@ int iter_arr_with_actual_elem_count(const void *ctx) return sum; } +__u32 upper, select_n, result; +__u64 global; + +static __noinline bool nest_2(char *str) +{ + /* some insns (including branch insns) to ensure stacksafe() is triggered + * in nest_2(). This way, stacksafe() can compare frame associated with nest_1(). + */ + if (str[0] == 't') + return true; + if (str[1] == 'e') + return true; + if (str[2] == 's') + return true; + if (str[3] == 't') + return true; + return false; +} + +static __noinline bool nest_1(int n) +{ + /* case 0: allocate stack, case 1: no allocate stack */ + switch (n) { + case 0: { + char comm[16]; + + if (bpf_get_current_comm(comm, 16)) + return false; + return nest_2(comm); + } + case 1: + return nest_2((char *)&global); + default: + return false; + } +} + +SEC("raw_tp") +__success +int iter_subprog_check_stacksafe(const void *ctx) +{ + long i; + + bpf_for(i, 0, upper) { + if (!nest_1(select_n)) { + result = 1; + return 0; + } + } + + result = 2; + return 0; +} + char _license[] SEC("license") = "GPL";