From patchwork Fri Aug 23 01:48:28 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: 13774496 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 38A8C18E3F for ; Fri, 23 Aug 2024 01:48:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724377719; cv=none; b=KIJb+DLvnfOPKGyIeWFoIrD9mSfU02/3UXVOo6mrpUgvEos5P4cltxXTLo4x8IMWBI5Y/i4k445l5Veb4MVy483yFS/9BGZTU9g98cEGHbQP5LYSWHcx0yvhf25FWtJPwIo/kQrpzRY4f9k2BBgykEWePdf7CCyTfpZ7rPrFLQo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724377719; c=relaxed/simple; bh=4hKbplegsIw81cNQ5WnyXPLM8uqwna6/0A25fiGrYYE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=P2GUVoKmqK3IneoO3NWg/Cp17HOGOpE6xZIjJM2rqQIJtzl5sGOmqXwtbpj+Z2/4Ay2lWHKq6Au1B1OZJCKv0Ho4g9RcP5CPSLyKEFPSRGiJIlPJzmCg2T7I3OI5S+w6fRzJyTnfu6sPnN5ImlD7nr8HgruiVO2o6q820PD/Uu4= 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=DPAaQN3k; arc=none smtp.client-ip=209.85.128.49 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="DPAaQN3k" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-42816ca782dso10312935e9.2 for ; Thu, 22 Aug 2024 18:48:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1724377715; x=1724982515; 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=FMhmmCDtW4ujiSa3oMXcmqVGqRJv4lFRXkcZHpGm9i4=; b=DPAaQN3kyP+HUA+69tvNNMY5wOCcR+ndGsqTs2/VWOq++8ku9s0nVEaNOE1Rg7juDS 8JCeGt+fwv5Axai2v7MM4pQcBHWEQnY0UzcJc0yGva5z3MmD+SOS9kIlDiXVxGR5fpeh KljCUI44huX5bFInSm8Jc54Eipr/bwnHqqHr3SI/e8YuS2QBz+SzIB2zQAetF+oB+4oH MBByucwImSgJDG7qT6g+MFU2Q1SwJLUup2vycqmXsqlFUrk7bHlqIwvpSZ/3GBwfgXBs DEa76WKhexOmVASFMFEd2HhiD8sfpyJXTZFyy6Q9hAn96pVINECwVunr7GRBxWdpvKAK ptGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724377715; x=1724982515; 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=FMhmmCDtW4ujiSa3oMXcmqVGqRJv4lFRXkcZHpGm9i4=; b=Qd8xvGl6SBA+bjstu6Hs5ufS6osgxQfEvyVxCC+VHo5yJ+VfZo/ZdlbubdYdAgQGzA RZu/0f79mMmpvGM8zzlWGIDZuo1LZMux/YLAN4kPsiXUuQnMK43Whbhb3YuLmAJibS0g Z3AOJ6tPyacrtoIbtjcCxXdFc6X30f9nj44voRDRbfF31zQh1Ec9wKNz226T+7aHBnft IRe8vaCttFEX2x+2VI7gNnRylRP3Bg0AXPl0MUSayKS/En7FDzRLGqD0aYEWaRvwVPAm hdNoREt9dIDOQduzNmbtnRGBxkHry1iRLheArThDUgSYSaYGmbOKV1wwAzRDkYlyzx/5 d6CQ== X-Gm-Message-State: AOJu0YwhRTxcZHz9OGUsZo6nCisYJ0K7AhzsScs1JLf76McdUYURxwQB Ey422UA75EE4UWyag6YsVxfJbKJ80bxzALudbympov0R+8GtZwywzCme1sAKpAU= X-Google-Smtp-Source: AGHT+IHk01EO7ycizahG1PJ7kp0j+rAUMJ0RqMuMbKWO67qXCheWVipXYM16MUpGo+l/0O4m55S89g== X-Received: by 2002:adf:fe4e:0:b0:36b:b297:1419 with SMTP id ffacd0b85a97d-37311855d3amr280118f8f.20.1724377715448; Thu, 22 Aug 2024 18:48:35 -0700 (PDT) Received: from localhost (27-51-129-77.adsl.fetnet.net. [27.51.129.77]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d5eba23d78sm4939192a91.29.2024.08.22.18.48.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 18:48:35 -0700 (PDT) From: Shung-Hsi Yu To: stable@vger.kernel.org Cc: bpf@vger.kernel.org, Shung-Hsi Yu , Eduard Zingerman , Daniel Hodges , Yonghong Song , Alexei Starovoitov Subject: [PATCH stable 6.6 1/2] bpf: Fix a kernel verifier crash in stacksafe() Date: Fri, 23 Aug 2024 09:48:28 +0800 Message-ID: <20240823014829.115038-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 shung-hsi.yu: "exact" variable is bool instead enum because commit 4f81c16f50ba ("bpf: Recognize that two registers are safe when their ranges match") is not present. Signed-off-by: Shung-Hsi Yu --- 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 171045b6956d..3f1a9cd7fc9e 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -16124,8 +16124,9 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, spi = i / BPF_REG_SIZE; if (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) && !exact) { From patchwork Fri Aug 23 01:48:29 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: 13774497 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.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 48C231CD31 for ; Fri, 23 Aug 2024 01:48:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724377724; cv=none; b=qT6KoP8c6CCVVFJ3qy9I+YJqfM6pXsStIyzd3bjTJZcVUW61rTVelkRGnqbaxKrXPdXA7f9NPzN1XCEetVV3TE3esGPRCq6fpZ8X/lFv2N9fLUJAV+SJ5HlDBtFpVjuSo3UNJMTskAfukxRKvy28wLTKrhCSbAPJRhlgj2Bzxy4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724377724; c=relaxed/simple; bh=0BbKF++PKaq1i5yYuCZNH2yPvoAqTN3M876SxYRmVWc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rbOeQu3Zhrn/8/tXkt9jHHKXw4lnBfj9gLDLG4MC3b4e85+E765D7cA6OkQ2bypU8OrDHeNCY394TOQDCVLwP6SHrKYYbo4TNpbWZO03PCM1DhSCj22qwFH3MFtgXkVP3h7gRN/pAcYfocO28asRfF/yIs5ggyP1xBw59vXBYCE= 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=Lsm/MmQO; arc=none smtp.client-ip=209.85.221.45 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="Lsm/MmQO" Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-371afae614aso696743f8f.0 for ; Thu, 22 Aug 2024 18:48:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1724377721; x=1724982521; 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=i4JwmTdEUSSkkUga1FYXioC0KGPP04BzkybNO2RcnDY=; b=Lsm/MmQO7Zp5KTAPPrg/SpHWDyjm1nhlSvoQB2VMA62BlvRXk/BRD57DViL4A+Q26Y 82yoQMQyBjX1C0u5OF1La2C3QTG93fkaJ7XnOP84gdP4gZTDPb4a1q+zPSn4KHdfHCmb CGK1jnK8JgTm7ZQ4ZwTL4Jhnt1qdDqLmfXy+WnAw4ryN2fA/o84pmuPpzgE1X3YDaspL V5erJpJAERwO0qs7FZbmaNyMFX6p8uUps5cLWJ86Dxg4s9LJIHRxCEp3sZXalYWaCTvk zyEst1ZnQxnq0Osw+/WnQcJU8qVacOBkZ9glm7zhTaIYZwCMcyAW3E0ispB6BqDY35JA 2FZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724377721; x=1724982521; 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=i4JwmTdEUSSkkUga1FYXioC0KGPP04BzkybNO2RcnDY=; b=rXjGlXtH5vGx34Bkw9pwAdSjwCRge8Kl8JgBzZfXHSeIGi4pNM17V0pkki1+XPwvly eSgo5OPGn3mIW8Tznp5+Xivda7Rj3uAzspr3MLrXhEfCbsZZ1QCGK0tcljsraNLoHybe IvGYFCdDIa4A/t+jy7+IBxuvrItJ6tmInZAGC/7UG5KIyVvvZMOhusp5vRxUMB5dckj6 X75fbNRlO5tEDFCXtMKViUYGmu2Xp6lXDALygdqdadW0RlNIzMMkUae+F0/tyGsi8XJX 4NntJvw34oEVGpGBkNkJ5oWBVXzU2bt4o7+1q4+25jOI1DVXLzzACZiTHMoyokVn1Yge r80A== X-Gm-Message-State: AOJu0YxgXEOL1C/GBTL2KvLm93DQqZ6Vd0wb4Ti456yEsv+Rw4zh9+OS UYu/2RZcYr0g2ZK5H2YSbU9vYWzTn/fD8vYebClalVj4BS15FiK95KFoi9yrZAU= X-Google-Smtp-Source: AGHT+IFCJDkWLwsDk4+ydS60CisBZYncl8PYxYWq5iTjPTVgqyPIHKblpr7HOlXJA/hk3vkNIYUv5g== X-Received: by 2002:adf:b309:0:b0:367:4e05:bb7b with SMTP id ffacd0b85a97d-373118e3c8emr265065f8f.53.1724377720650; Thu, 22 Aug 2024 18:48:40 -0700 (PDT) Received: from localhost (27-51-129-77.adsl.fetnet.net. [27.51.129.77]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-714343403b1sm1982337b3a.211.2024.08.22.18.48.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2024 18:48:40 -0700 (PDT) From: Shung-Hsi Yu To: stable@vger.kernel.org Cc: bpf@vger.kernel.org, Shung-Hsi Yu , Yonghong Song , Alexei Starovoitov Subject: [PATCH stable 6.6 2/2] selftests/bpf: Add a test to verify previous stacksafe() fix Date: Fri, 23 Aug 2024 09:48:29 +0800 Message-ID: <20240823014829.115038-2-shung-hsi.yu@suse.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240823014829.115038-1-shung-hsi.yu@suse.com> References: <20240823014829.115038-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 c20c4e38b71c..5685c2810fe5 100644 --- a/tools/testing/selftests/bpf/progs/iters.c +++ b/tools/testing/selftests/bpf/progs/iters.c @@ -1411,4 +1411,58 @@ __naked int checkpoint_states_deletion(void) ); } +__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";