Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions c_security_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#define MAX_BUFFER 10

void buffer_overflow_vuln(char* user_input) {
char buffer[MAX_BUFFER];
strcpy(buffer, user_input);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 AI 代码审查发现问题

📋 问题概述

问题描述 CWE-120

【clang-tidy 静态扫描】 本条经 clang-tidy 静态分析命中。

line 10 处声明了 10 字节的栈缓冲区 buffer[MAX_BUFFER](MAX_BUFFER=10),line 11 处使用 strcpy 将外部输入 user_input 无界复制到该缓冲区。strcpy 不会检查源字符串长度,当 user_input 长度超过 9 字节时发生栈缓冲区溢出。main 函数 line 22 直接将命令行参数 argv[1] 传入,攻击者可通过超长命令行参数触发溢出。

检查器:clang-analyzer-security.insecureAPI.strcpy;报告器:clang-tidy

📍 问题详情

🔴 问题 1 | 严重程度: HIGH | 行号: 11

💬 详细说明:

  • 当命令行参数超过 9 字节时触发栈溢出,可能导致程序崩溃或任意代码执行。攻击者通过构造超长参数即可利用。

📝 问题代码:

    strcpy(buffer, user_input); 

💡 修复建议:

将 strcpy 替换为 strncpy 或 snprintf,并确保目标缓冲区大小作为上限传入。同时在使用前校验输入长度。

✅ 修复示例:

void buffer_overflow_vuln(char* user_input) {
    char buffer[MAX_BUFFER];
    if (strlen(user_input) >= MAX_BUFFER) {
        fprintf(stderr, "Input too long\n");
        return;
    }
    strncpy(buffer, user_input, MAX_BUFFER - 1);
    buffer[MAX_BUFFER - 1] = '\0';
}

🔗 参考链接

}


int integer_overflow_vuln(int count, int size) {
int total_bytes = count * size;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 AI 代码审查发现问题

📋 问题概述

问题描述 CWE-190

【clang-tidy 静态扫描】 本条经 clang-tidy 静态分析命中。

line 16 处将两个有符号 int 参数 count 和 size 直接相乘,未做任何溢出检查。C 标准规定有符号整数溢出是未定义行为。main 函数 line 23 以 INT_MAX 和 2 作为参数调用,INT_MAX * 2 必然超出 int 表示范围,触发有符号整数溢出 UB。

检查器:clang-analyzer-security.insecureAPI.strcpy;报告器:clang-tidy

📍 问题详情

🟡 问题 1 | 严重程度: MEDIUM | 行号: 16

💬 详细说明:

  • 有符号整数溢出是 C 标准未定义行为,编译器可能基于此做优化假设导致意外结果。当 count * size 超出 INT_MAX 时触发,main 中以 INT_MAX 和 2 调用必然触发。

📝 问题代码:

    int total_bytes = count * size; 

💡 修复建议:

在乘法前检查操作数是否会导致溢出,或使用 unsigned 类型并在乘法后检查结果是否回绕。可使用 __builtin_mul_overflow 或手动检查。

✅ 修复示例:

int integer_overflow_vuln(int count, int size) {
    if (count > 0 && size > 0 && count > INT_MAX / size) {
        fprintf(stderr, "Integer overflow detected\n");
        return -1;
    }
    int total_bytes = count * size;
    return total_bytes;
}

🔗 参考链接

return total_bytes;
}


int main(int argc, char* argv[]) {
buffer_overflow_vuln(argv[1]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 AI 代码审查发现问题

📋 问题概述

问题描述 CWE-476

【clang-tidy 静态扫描】 本条经 clang-tidy 静态分析命中。

line 22 处直接使用 argv[1] 作为参数调用 buffer_overflow_vuln,未检查 argc 是否 >= 2。当程序不带命令行参数运行时,argv[1] 为 NULL 指针,传入 buffer_overflow_vuln 后在 line 11 的 strcpy 中对 NULL 解引用,导致未定义行为(通常崩溃)。

检查器:clang-analyzer-deadcode.DeadStores;报告器:clang-tidy

📍 问题详情

🟡 问题 1 | 严重程度: MEDIUM | 行号: 22

💬 详细说明:

  • 当程序不带命令行参数运行时,argv[1] 为 NULL,strcpy(NULL, ...) 或 strcpy(dst, NULL) 均触发未定义行为,通常导致段错误崩溃。

📝 问题代码:

    buffer_overflow_vuln(argv[1]);

💡 修复建议:

在 main 函数中使用 argv[1] 前检查 argc >= 2,若不满足则打印用法提示并退出。

✅ 修复示例:

int main(int argc, char* argv[]) {
    if (argc < 2) {
        fprintf(stderr, "Usage: %s <input>\n", argv[0]);
        return 1;
    }
    buffer_overflow_vuln(argv[1]);
    int result = integer_overflow_vuln(INT_MAX, 2);
    return 0;
}

🔗 参考链接

int result = integer_overflow_vuln(INT_MAX, 2);
return 0;
}