#!/usr/bin/env bash # CWE-407 scan — BIRD Internet Routing Daemon (BGP, OSPF, IS-IS, RIP) set -euo pipefail echo "# scan=bird host=$(hostname) date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" cd /tmp && git clone --depth 1 https://gitlab.nic.cz/labs/bird bird 2>&1 | tail -1 GRAPH_KW="shortest.path|dijkstra|spf\b|scc\b|topolog|cycle|reachab|path.find|dfs\b|bfs\b|strongly.connected|next.hop" C_MEM='node_find|list_walk|WALK_LIST|WALK_TLIST|linear_search|find.*next|chain.*next.*next|loop.*->next.*->next' for proto in proto/ospf proto/bgp proto/isis proto/rip nest; do [ -d /tmp/bird/$proto ] || continue echo "# roots: /tmp/bird/$proto" find /tmp/bird/$proto -name "*.c" -o -name "*.h" 2>/dev/null | sort | while IFS= read -r f; do gl=$(grep -inE "$GRAPH_KW" "$f" 2>/dev/null | cut -d: -f1 | head -5 || true) [ -z "$gl" ] && continue while IFS= read -r ln; do s=$(( ln > 20 ? ln - 20 : 1 )); e=$(( ln + 20 )) hit=$(sed -n "${s},${e}p" "$f" 2>/dev/null | grep -iE "$C_MEM" | head -1 | sed 's/^\s*//' || true) [ -n "$hit" ] && echo "CANDIDATE ${f}:${ln} ${hit}" done <<< "$gl" done done echo "# scan complete"