Let's say my piece of code scans the directory ./plugins
and loads .dll
s/.so
s with a known symbol ("function" here) in order to extend its functionnality, like this:
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; }
This could lead to a major security issue : If my application runs as root, or has admin privileges, that means any unprivileged attacker can execute code as a privileged user by producing a shared object containing a function named as the known symbol (here, function
).
What can I do to secure my plugins
folder? How can I check if the shared objects I load are secure?
This is a follow-up of this question.