CodeQL 文档

使用 O_CREAT 标记但没有模式参数打开文件

ID: cpp/open-call-with-mode-argument
Kind: problem
Security severity: 7.8
Severity: error
Precision: high
Tags:
   - security
   - external/cwe/cwe-732
Query suites:
   - cpp-code-scanning.qls
   - cpp-security-extended.qls
   - cpp-security-and-quality.qls

单击查看 CodeQL 存储库中的查询

使用 O_CREATO_TMPFILE 标记打开文件时,必须提供 mode。如果省略 mode 参数,则会使用来自堆栈的某些任意字节作为文件模式。这会将堆栈中的某些位泄露到文件的权限中。

建议

指定 O_CREATO_TMPFILE 时,必须提供 mode

示例

第一个示例使用 O_CREAT 标记打开文件,但没有提供 mode 参数。在这种情况下,将使用来自堆栈的任意字节作为 mode 参数。第二个示例正确地提供了 mode 参数,并创建了一个用户可读可写的文件。

int open_file_bad() {
	// BAD - this uses arbitrary bytes from the stack as mode argument
        return open(FILE, O_CREAT)
}

int open_file_good() {
	// GOOD - the mode argument is supplied
        return open(FILE, O_CREAT, S_IRUSR | S_IWUSR)
}
  • ©2025GitHub, Inc.
  • 条款
  • 隐私