mcpField
L
llvm-security

llvm-security

github.com/gmh5225安全89051 次安装4 周前更新
代码仓库

关于 llvm-security

llvm-security 是一项介绍 LLVM 安全功能与实践的技能,面向软件开发者。内容涵盖 Sanitizer、编译器加固、符号执行和模糊测试。

主要功能

  1. 01Sanitizer 选择与使用
  2. 02编译器加固技术
  3. 03符号执行指南
  4. 04安全运行时分析
  5. 05结合 Sanitizer 进行模糊测试

适用场景

  1. 01查找 C/C++ 程序中的内存错误
  2. 02加固构建以抵御漏洞利用
  3. 03通过符号执行探索程序路径
  4. 04将模糊测试与运行时检查结合

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/gmh5225/awesome-llvm-security --skill llvm-security

安装后在 Claude Code 中输入/llvm-security确认已加载。

详细介绍

LLVM 安全技能

此技能涵盖基于 LLVM 的安全功能、消毒器、加固机制和安全软件开发实践。

消毒器

AddressSanitizer(ASan)

检测内存错误:缓冲区溢出、释放后使用、作用域结束后使用。

# Compile with ASan
clang -fsanitize=address -g program.c -o program

# Key features
# - Stack buffer overflow detection
# - Heap buffer overflow detection  
# - Use-after-free detection
# - Memory leak detection
MemorySanitizer(MSan)

检测未初始化内存读取。

clang -fsanitize=memory -g program.c -o program
ThreadSanitizer(TSan)

检测多线程程序中的数据竞争。

clang -fsanitize=thread -g program.c -o program
UndefinedBehaviorSanitizer(UBSan)

在运行时检测未定义行为。

clang -fsanitize=undefined -g program.c -o program

# Specific checks
clang -fsanitize=signed-integer-overflow,null program.c
自定义消毒器开发
// Implementing custom memory tracking
extern "C" void __asan_poison_memory_region(void const volatile *addr, size_t size);
extern "C" void __asan_unpoison_memory_region(void const volatile *addr, size_t size);

class SecureAllocator {
public:
    void* allocate(size_t size) {
        // Add red zones around allocation
        void* ptr = malloc(size + 2 * REDZONE_SIZE);
        __asan_poison_memory_region(ptr, REDZONE_SIZE);
        __asan_poison_memory_region((char*)ptr + REDZONE_SIZE + size, REDZONE_SIZE);
        return (char*)ptr + REDZONE_SIZE;
    }
};

加固技术

栈保护
# Stack canaries
clang -fstack-protector-strong program.c

# Stack clash protection
clang -fstack-clash-protection program.c

# Safe stack (separate stacks for safe/unsafe data)
clang -fsanitize=safe-stack program.c
控制流完整性(CFI)
# Forward-edge CFI
clang -fsanitize=cfi -flto program.c

# Specific CFI schemes
clang -fsanitize=cfi-vcall      # Virtual call checks
clang -fsanitize=cfi-nvcall     # Non-virtual member call checks
clang -fsanitize=cfi-icall      # Indirect call checks
影子调用栈
# Backward-edge protection (return address protection)
clang -fsanitize=shadow-call-stack program.c
位置无关可执行文件
# Full ASLR support
clang -fPIE -pie program.c

# Position independent code for shared libraries
clang -fPIC -shared library.c -o library.so

符号执行

与 KLEE 集成
// Mark symbolic inputs
#include <klee/klee.h>

int main() {
    int input;
    klee_make_symbolic(&input, sizeof(input), "input");
    
    if (input > 0) {
        // Path 1
    } else {
        // Path 2
    }
    return 0;
}
SymCC(通过编译实现的符号执行)

用于符号执行的编译时插桩:

  • 比 IR 解释更快
  • 支持复杂的真实世界程序
  • 可集成到模糊测试工作流中
符号分析工具
  • Caffeine:基于 LLVM 的符号执行器
  • SymSan:符号执行 + 消毒器
  • Haybale:基于 Rust 的 LLVM 符号执行器

面向安全的分析

运行时类型检查
// LLVM TypeSanitizer concepts
// Track type information through allocations
struct TypeInfo {
    const char* typeName;
    size_t typeSize;
    uint64_t typeHash;
};

void checkType(void* ptr, TypeInfo expected) {
    TypeInfo* actual = getTypeInfo(ptr);
    if (actual->typeHash != expected.typeHash) {
        reportTypeMismatch(ptr, actual, expected);
    }
}
内存泄漏检测
// LeakSanitizer integration
extern "C" void __lsan_do_leak_check();
extern "C" void __lsan_disable();
extern "C" void __lsan_enable();

// Custom leak tracking
class PreciseLeakSanitizer {
    std::unordered_map<void*, AllocationInfo> allocations;
    
public:
    void recordAlloc(void* ptr, size_t size, const char* file, int line) {
        allocations[ptr] = {size, file, line, getStackTrace()};
    }
    
    void recordFree(void* ptr) {
        allocations.erase(ptr);
    }
    
    void reportLeaks() {
        for (auto& [ptr, info] : allocations) {
            fprintf(stderr, "Leak: %zu bytes at %s:%d\n", 
                    info.size, info.file, info.line);
        }
    }
};

漏洞利用缓解措施的实现

返回地址保护
; Shadow stack concept in LLVM IR
define void @protected_function() {
entry:
    %return_addr = call ptr @llvm.returnaddress(i32 0)
    call void @shadow_stack_push(ptr %return_addr)
    
    ; Function body...
    
    %saved_addr = call ptr @shadow_stack_pop()
    %current_addr = call ptr @llvm.returnaddress(i32 0)
    %match = icmp eq ptr %saved_addr, %current_addr
    br i1 %match, label %safe_return, label %attack_detected
    
safe_return:
    ret void
    
attack_detected:
    call void @abort()
    unreachable
}
指针认证(ARM)
// Using pointer authentication on ARM64
__attribute__((target("sign-return-address")))
void signed_function() {
    // Return address is cryptographically signed
}

安全编译流水线

构建标志清单
# Comprehensive hardening
CFLAGS="-O2 \
    -fstack-protector-strong \
    -fstack-clash-protection \
    -fcf-protection=full \
    -fPIE \
    -D_FORTIFY_SOURCE=2 \
    -Wformat -Wformat-security \
    -fsanitize=cfi -flto"

LDFLAGS="-pie \
    -Wl,-z,relro \
    -Wl,-z,now \
    -Wl,-z,noexecstack"
编译器安全检查
  • -Wformat-security:格式字符串漏洞
  • -Warray-bounds:数组越界
  • -Wshift-overflow:移位操作溢出
  • -Wnull-dereference:空指针解引用

模糊测试集成

libFuzzer
// Fuzz target template
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
    // Parse/process Data
    processInput(Data, Size);
    return 0;
}
消毒器 + 模糊测试器组合
# Comprehensive fuzzing setup
clang -fsanitize=fuzzer,address,undefined \
      -fno-omit-frame-pointer \
      -g fuzz_target.c -o fuzzer

Windows 专属安全功能

控制流防护(CFG)
clang-cl /guard:cf program.c
SEH(结构化异常处理)
  • LLVM 支持 Windows SEH
  • 用于安全的异常处理
  • 与安全监控集成

资源

如需查看全面的工具列表,请参阅 README.md 中的 Security Features、Sanitizer 和 Symbolic Execution 章节。

获取详细信息

如需获取详细且最新的资源链接、工具列表或项目参考资料,请从以下位置获取最新数据:

https://raw.githubusercontent.com/gmh5225/awesome-llvm-security/refs/heads/main/README.md

此 README 包含以下内容的精选综合列表:

  • 安全功能与加固(Security Features 章节)
  • 消毒器与内存安全工具(Sanitizer 章节)
  • 符号执行框架(Symbolic Execution 章节)
  • 内存泄漏检测器与运行时检查器

评论

支持 Markdown