(misc) fopen:
Check when opening files - can an attacker redirect it (via symlinks),
force the opening of special file type (e.g., device files), move things
around to create a race condition, control its ancestors, or change its
contents? (CWE-362).
为了模拟 CWE-362 中描述的文件打开安全问题,我们可以创建一个简单的示例程序,展示如何在打开文件时可能存在的安全风险。在这个示例中,我们将展示如何通过符号链接来重定向文件操作到意外的位置。
假设我们有一个程序尝试打开一个名为 important_file.txt 的文件,但攻击者在其中插入了一个符号链接,将文件操作重定向到 /etc/passwd 文件,这可能导致泄露敏感信息。
下面是一个简单的示例程序:
- #include <stdio.h>
-
- int main() {
- FILE *file;
- char filename[] = "important_file.txt";
-
- // 尝试打开文件
- file = fopen(filename, "r");
-
- if (file == NULL) {
- perror("Error opening file");
- return 1;
- }
-
- // 读取文件内容
- // 这里我们假设程序会继续操作文件内容,但在这个示例中我们省略了这部分
-
- fclose(file);
-
- return 0;
- }
在这个示例中&