From: Zijun Hu quic_zijuhu@quicinc.com
[ Upstream commit faeefc173be40512341b102cf1568aa0b6571acd ]
(assign|release)_proto_idx() wrongly check find_first_zero_bit() failure by condition '(prot->inuse_idx == PROTO_INUSE_NR - 1)' obviously.
Fix by correcting the condition to '(prot->inuse_idx == PROTO_INUSE_NR)'
Signed-off-by: Zijun Hu quic_zijuhu@quicinc.com Reviewed-by: Kuniyuki Iwashima kuniyu@amazon.com Link: https://patch.msgid.link/20250410-fix_net-v2-1-d69e7c5739a4@quicinc.com Signed-off-by: Jakub Kicinski kuba@kernel.org Signed-off-by: Sasha Levin sashal@kernel.org ---
Based on my analysis of the Linux kernel source code in the environment, let me now provide a thorough evaluation: **YES** This commit should be backported to stable kernel trees. Here's my detailed analysis: ## Analysis of the Bug Fix ### The Core Issue The commit fixes a critical off-by-one error in the error checking conditions within `assign_proto_idx()` and `release_proto_idx()` functions in `net/core/sock.c`. ### Current vs. Fixed Code Comparison **Before the fix (incorrect):** ```c // In assign_proto_idx() if (unlikely(prot->inuse_idx == PROTO_INUSE_NR - 1)) { // Wrong: checks for 63 pr_err("PROTO_INUSE_NR exhausted\n"); return -ENOSPC; } // In release_proto_idx() if (prot->inuse_idx != PROTO_INUSE_NR - 1) // Wrong: checks for 63 clear_bit(prot->inuse_idx, proto_inuse_idx); ``` **After the fix (correct):** ```c // In assign_proto_idx() if (unlikely(prot->inuse_idx == PROTO_INUSE_NR)) { // Correct: checks for 64 pr_err("PROTO_INUSE_NR exhausted\n"); return -ENOSPC; } // In release_proto_idx() if (prot->inuse_idx != PROTO_INUSE_NR) // Correct: checks for 64 clear_bit(prot->inuse_idx, proto_inuse_idx); ``` ### Technical Analysis 1. **Understanding the Bug:** - `PROTO_INUSE_NR` is defined as 64, creating a bitmap with valid indices 0-63 - `find_first_zero_bit()` returns `PROTO_INUSE_NR` (64) when no free bits are found - The original code incorrectly checked for `PROTO_INUSE_NR - 1` (63), which is actually a valid index - This meant the error condition would never trigger, and the code would attempt to set bit 64, causing undefined behavior 2. **Impact of the Bug:** - **Memory corruption risk:** Setting bit 64 in a 64-bit bitmap accesses memory beyond the allocated bitmap - **Resource exhaustion not detected:** The system would not properly detect when all protocol slots are exhausted - **Potential crashes:** Accessing invalid memory locations could cause kernel panics 3. **Why This is Backport-Worthy:** - **Fixes a clear bug:** The logic error is objectively wrong and could cause system instability - **Minimal risk change:** The fix only changes two comparison operators, with no architectural impact - **Important subsystem:** Network protocol registration is core kernel functionality - **Well-contained fix:** The change is localized to error checking conditions without affecting normal operation paths ### Comparison with Similar Commits Looking at the historical examples: - **Similar Commit #2 (YES):** Fixed error checking in packet handling - similar pattern of correcting error conditions - **Similar Commit #1 (NO):** More complex memory leak fix with broader changes - **Similar Commit #3 (NO):** Architectural change from BUG() to error returns - **Similar Commit #4 (NO):** API cleanup removing function pointers - **Similar Commit #5 (NO):** Validation fix in newer subsystem This commit most closely resembles Similar Commit #2, which was marked for backporting due to its focused bug fix nature. ### Stable Tree Criteria Met: - ✅ **Fixes important bug:** Prevents potential memory corruption - ✅ **Small and contained:** Only two line changes - ✅ **Low regression risk:** Pure bug fix with no behavioral changes for normal cases - ✅ **Clear side effects:** None beyond fixing the bug - ✅ **No architectural changes:** Maintains existing API and behavior This is exactly the type of focused, low-risk bug fix that stable trees are designed to include.
net/core/sock.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/net/core/sock.c b/net/core/sock.c index 0842dc9189bf8..d06dc259d6296 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -3930,7 +3930,7 @@ static int assign_proto_idx(struct proto *prot) { prot->inuse_idx = find_first_zero_bit(proto_inuse_idx, PROTO_INUSE_NR);
- if (unlikely(prot->inuse_idx == PROTO_INUSE_NR - 1)) { + if (unlikely(prot->inuse_idx == PROTO_INUSE_NR)) { pr_err("PROTO_INUSE_NR exhausted\n"); return -ENOSPC; } @@ -3941,7 +3941,7 @@ static int assign_proto_idx(struct proto *prot)
static void release_proto_idx(struct proto *prot) { - if (prot->inuse_idx != PROTO_INUSE_NR - 1) + if (prot->inuse_idx != PROTO_INUSE_NR) clear_bit(prot->inuse_idx, proto_inuse_idx); } #else