CodeQL 文档

不受控制的格式字符串

ID: cpp/tainted-format-string
Kind: path-problem
Security severity: 9.3
Severity: warning
Precision: high
Tags:
   - reliability
   - security
   - external/cwe/cwe-134
Query suites:
   - cpp-code-scanning.qls
   - cpp-security-extended.qls
   - cpp-security-and-quality.qls

点击查看 CodeQL 代码库中的查询

程序使用来自用户的输入作为 printf 样式函数的格式字符串。这可能导致缓冲区溢出或数据表示问题。攻击者可以利用此漏洞使程序崩溃、泄露信息甚至执行任意代码。

此规则的结果不包括通过全局变量传输的来自用户的输入。这些可以在相关规则“不受控制的格式字符串(通过全局变量)”中找到。

建议

使用常量表达式作为格式字符串。如果需要打印来自用户的字符串,请使用 printf("%s", value_from_user)

示例

#include <stdio.h>

void printWrapper(char *str) {
	printf(str);
}

int main(int argc, char **argv) {
	// This should be avoided
	printf(argv[1]);

	// This should be avoided too, because it has the same effect
	printWrapper(argv[1]);

	// This is fine
	printf("%s", argv[1]);
}

参考

  • ©GitHub 公司
  • 条款
  • 隐私