【发布时间】:2016-01-11 13:07:37
【问题描述】:
假设我的一段代码扫描目录./plugins 并加载.dlls/.sos 和一个已知符号(此处为“函数”)以扩展其功能,如下所示:
main.c
#include <stdlib.h> #include <dirent.h> #include <string.h> #include <stdio.h> #include <dlfcn.h> int main(void) { DIR *dir; struct dirent *entry; dir = opendir("./plugins"); if (dir == NULL) return -1; while ((entry = readdir(dir)) != NULL) { void *handle; char path[PATH_MAX]; int (*function)(char *); if (strstr(entry->d_name, ".so") == NULL) continue; if (snprintf(path, sizeof(path), "./%s", entry->d_name) >= sizeof(path)) continue; handle = dlopen(path, RTLD_LAZY); if (handle == NULL) continue; // Better: report the error with `dlerror()' function = (int (*)(char *)) dlsym(handle, "function"); if (function != NULL) fprintf(stdout, "function: %d\n", function("example")); else fprintf(stderr, "symbol-not-found: %s\n", entry->d_name); dlclose(handle); } closedir(dir); return 0; }
这可能会导致一个重大的安全问题:如果我的应用程序以 root 身份运行,或者具有管理员权限,这意味着任何非特权攻击者都可以通过生成包含名为已知符号的函数的共享对象以特权用户身份执行代码 (在这里,function)。
如何保护我的plugins 文件夹?如何检查我加载的共享对象是否安全?
这是this question的后续。
【问题讨论】:
-
你能解释一下你为什么关心插件安全吗?
-
这是一个普遍的问题。每个软件开发人员都应该考虑到安全性,无论是其应用程序的安全性还是整个系统的安全性。在这里,由于我的代码,攻击者可能会破坏整个系统。
-
但是你为什么不相信这个插件呢?
-
因为任何人都可以将其放入文件夹中,包括恶意攻击者。
-
那么你的整个方法都是错误的。
标签: c security plugins shared-libraries