cpp-security
БесплатноБез исполняемых скриптовНе проверенC++ memory and concurrency safety: AddressSanitizer (ASan), UndefinedBehaviorSanitizer (UBSan), ThreadSanitizer (TSan), MemorySanitizer (MSan), MSVC `/sdl`…
от claude-dev-suite★ 51 звезда на GitHubОб этом скилле
C++ Security & Memory Safety - Quick Reference
Deep Knowledge: Use
mcp__documentation__fetch_docswith technology:cpp-security.
The Sanitizers (debug-time, near-zero false positives)
| Sanitizer | Catches | Overhead | Notes |
|---|---|---|---|
| ASan | Heap/stack/global OOB, use-after-free, double-free, leaks | ~2x | Cannot combine with TSan/MSan |
| UBSan | Signed overflow, null deref, OOB shift, misaligned access, etc. | ~10-20% | Often combined with ASan |
| TSan | Data races between threads | ~5-15x | Cannot combine with ASan |
| MSan | Use of uninitialized memory | ~3x | Requires all deps instrumented (Clang only) |
CFI (-fsanitize=cfi) |
Indirect-call integrity violations | small | Requires LTO |
Enable in CMake
add_library(sanitizers_iface INTERFACE)
if(NOT MSVC)
target_compile_options(sanitizers_iface INTERFACE
-fsanitize=address,undefined -fno-omit-frame-pointer -g -O1)
target_link_options(sanitizers_iface INTERFACE
-fsanitize=address,undefined)
endif()
target_link_libraries(myapp PRIVATE sanitizers_iface)
Or per-preset (see cmake skill debug-asan example).
Run-time options
ASAN_OPTIONS=detect_leaks=1:abort_on_error=1:strict_string_checks=1 ./myapp
UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 ./myapp
TSAN_OPTIONS=second_deadlock_stack=1:halt_on_error=1 ./myapp
LSAN_OPTIONS=suppressions=lsan.supp ./myapp
CI tip: build a separate sanitizer job rather than slowing every test.
Compiler hardening flags
Linux/macOS (Clang/GCC)
-D_FORTIFY_SOURCE=3 # libc bounds checks (needs -O1+)
-fstack-protector-strong # stack canaries
-fstack-clash-protection # GCC: probe pages
-fcf-protection=full # x86: CET indirect-branch tracking
-Wformat -Wformat-security # printf format-string attacks
-fPIE -pie # ASLR for executables
-Wl,-z,relro,-z,now # full RELRO
-Wl,-z,noexecstack # NX stack
Windows (MSVC)
/sdl # all `/W4` security warnings + extra checks
/GS # stack buffer overrun detection (default)
/guard:cf # Control Flow Guard
/guard:ehcont # EH continuation metadata (CET)
/Qspectre # Spectre mitigations
/DYNAMICBASE /HIGHENTROPYVA # ASLR (linker)
/NXCOMPAT # DEP
/CETCOMPAT # CET
Set per-target:
target_compile_options(myapp PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/sdl /guard:cf /Qspectre>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fstack-protector-strong -D_FORTIFY_SOURCE=3>
)
target_link_options(myapp PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/DYNAMICBASE /HIGHENTROPYVA /NXCOMPAT /CETCOMPAT /guard:cf>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-pie -Wl,-z,relro,-z,now,-z,noexecstack>
)
CERT C++ — top categories with C++ idiomatic fixes
Use std::span / std::string_view instead of pointer + size
// BAD: easy to desync
void process(const char* buf, std::size_t n);
// GOOD
void process(std::span<const std::byte> buf);
Safe integer arithmetic
#include <limits>
#include <stdexcept>
template <std::integral T>
T checked_add(T a, T b) {
T r;
if (__builtin_add_overflow(a, b, &r)) // GCC/Clang; MSVC: <intsafe.h> IntAdd*
throw std::overflow_error("add overflow");
return r;
}
C++26 will land <numeric> add_sat, sub_sat, mul_sat, div_sat as standard.
Avoid printf with non-literal format strings
printf(user_input); // BAD - format-string vuln
std::print("{}", user_input); // GOOD - C++23 std::print/format
Validate at boundaries, trust internally
Once data is parsed into a typed value, downstream code should not re-validate. Concentrate validation at the deserialization layer.
Prefer enum class to avoid implicit conversions
enum class Permission { None = 0, Read = 1, Write = 2, Admin = 4 };
Don't slice polymorphic types
struct Base { virtual ~Base() = default; };
struct Derived : Base { int extra{}; };
void take(Base b); // BAD - slices Derived state
void take(const Base&); // GOOD
Concurrency safety
// BAD: data race on `count`
int count = 0;
auto worker = [&] { for (int i = 0; i < 1'000'000; ++i) ++count; };
// GOOD
std::atomic<int> count{0};
auto worker = [&] { for (int i = 0; i < 1'000'000; ++i) count.fetch_add(1, std::memory_order_relaxed); };
Build the same binary under TSan in CI to catch latent races; relying on "we tested it" misses races that surface only on different hardware/OS.
Common Vulnerability Patterns and Their C++ Fixes
|
Установить cpp-security в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
ReadGrepGlobWriteEditFAQ
Что делает скилл cpp-security?
C++ memory and concurrency safety: AddressSanitizer (ASan), UndefinedBehaviorSanitizer (UBSan), ThreadSanitizer (TSan), MemorySanitizer (MSan), MSVC `/sdl` and `/guard:cf`, CERT C++ secure coding rules, integer-overflow safe arithmetic, and Control Flow Integrity. USE WHEN: user mentions "ASan", "AddressSanitizer", "UBSan", "TSan", "MSan", "use-after-free", "buffer overflow", "undefined behavior", "data race", "CERT C++", "secure C++", "/sdl", "/GS", "ASLR", "DEP", "stack canary" DO NOT USE FOR: Web/app security (use `owasp`), .NET security (`dotnet-security`), cryptography APIs (use `cryptography`)
Как установить скилл cpp-security?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл cpp-security запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
skill-creator
Create new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an…
от Anthropicdiscernment-nudge
After you give a substantive answer or draft that the user may act on — advice or recommendations, drafted artifacts such as goals, plans, pitches, proposals…
от Anthropicdata-consolidate
Joins the plant's scheduled exports and connector data into one working dataset — ERP items (NetSuite or Infor M3), sales orders, production work orders…
от Anthropicinspection-to-capa
Runs the quality chain end to end: writes the nonconformance against the requirement it violates, searches history at the moment of writing to say whether…
от Anthropic