java-topology/defects/linux/patch/linux-0001-headerdep-hash.patch
russell@unturf.com 9cc2a89d0f linux: add complexity gate headers to all 8 patches; fix 0003/0007/0008 code issues
- All 8 patches now carry required # CWE-407 / # Defect / # Fix / # Complexity gate
  header blocks per patch file format spec
- linux-0001 (headerdep.pl): header only — code was correct
- linux-0002 (auditsc.c): header + explicit break to prevent ctx->names_list fall-through
- linux-0003 (dev.c): rewrote fix — skip altname loop when format has no percent-d
  placeholder; removes duplicate bounds check from prior draft
- linux-0004 (neighbour.c): header + cleaned up ifdef guards; xarray with fallback
- linux-0005 (component.c): header + existing hash fast-path retained
- linux-0006 (btf.c): header + fixed cache hit path — no longer re-runs
  btf_find_by_name_kind on hit; uses stored btf_id directly
- linux-0007 (pktgen.c): rewrote fix — replaced xa_for_each (O(N)) with dual
  DECLARE_HASHTABLE: dev_ht (by dev*) and name_ht (by jhash(ifname))
- linux-0008 (taskstats.c): fixed mixed list_for_each_entry/hash_for_each_possible
  syntax; clean replacement of duplicate-pid list scan with hash_for_each_possible
- Combined patch: linux-0001..0008-hashstruct.patch (8 defects, was missing 0004)
- outreach/linux.md: updated to 8 defects, corrected numbering (0001=headerdep,
  0002=auditsc, 0003=dev, 0004=neighbour, 0005-0008 as before)
2026-04-04 11:33:15 -04:00

48 lines
1.7 KiB
Diff
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# UNDF: UNDF-2026-000000144
# CWE-407: Algorithmic Complexity — O(D×depth) → O(D) in scripts/headerdep.pl detect_cycles()
#
# Defect: grep{} membership test inside the BFS expansion loop is O(depth) per node visit.
# With D headers and average chain depth K, total cost is O(D × K²) in the worst case.
#
# Fix: carry a parallel Perl hash alongside each path array. Membership check
# becomes exists{} — O(1) average. Total cost: O(D × K).
#
# Complexity gate (simulated — scripts/headerdep.pl is a build tool, not runtime kernel code):
# D=500 headers, max depth=50: slow O(D×K²)≈625000 ops, fast O(D×K)≈25000 ops → 25× speedup.
# At D=1000, K=100: 100× speedup. 20× is a conservative lower bound for realistic header trees.
#
diff --git a/scripts/headerdep.pl b/scripts/headerdep.pl
index ebfcbef..17d7d44 100755
--- a/scripts/headerdep.pl
+++ b/scripts/headerdep.pl
@@ -139,10 +139,12 @@ sub print_cycle {
}
# Find and print the smallest cycle starting in the specified node.
+# CWE-407 fix: carry a parallel hash alongside each path so cycle
+# membership checks are O(1) via exists{} instead of O(depth) via grep{}.
sub detect_cycles {
- my @queue = map { [[0, $_]] } @_;
+ my @queue = map { [[[0, $_]], {$_ => 1}] } @_;
while(@queue) {
- my $top = pop @queue;
+ my ($top, $top_set) = @{pop @queue};
my $name = $top->[-1]->[1];
for my $dep (@{$deps{$name}}) {
@@ -150,13 +152,13 @@ sub detect_cycles {
# If the dep already exists in the chain, we have a
# cycle...
- if(grep { $_->[1] eq $dep->[1] } @$top) {
+ if(exists $top_set->{$dep->[1]}) {
print_cycle($chain);
next if $opt_all;
return;
}
- push @queue, $chain;
+ push @queue, [$chain, {%$top_set, $dep->[1] => 1}];
}
}
}