Skip to content

Instantly share code, notes, and snippets.

@nadiaholmquist
Last active April 7, 2025 00:24
Show Gist options
  • Save nadiaholmquist/eb2f0d81782e5a85e44a51b4b3708781 to your computer and use it in GitHub Desktop.
Save nadiaholmquist/eb2f0d81782e5a85e44a51b4b3708781 to your computer and use it in GitHub Desktop.
Hack to filter input devices available to Clone Hero
#include <sys/types.h>
#include <stdio.h>
#define __USE_GNU
#include <dlfcn.h>
#include <linux/limits.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
static int (*real_openat64)(int, const char*, int, mode_t) = NULL;
static const char* device_dirs[] = {
"/sys/devices/virtual/misc/uhid",
"/sys/devices/virtual/input"
};
static const char* allowed_devices[] = {
"Nintendo Wii Remote Guitar",
};
static bool veto_input_open(int dirfd, const char* name) {
char namebuf[PATH_MAX] = {0};
char dirpath[PATH_MAX] = {0};
snprintf(namebuf, PATH_MAX, "/proc/self/fd/%d", dirfd);
int res = readlink(namebuf, dirpath, PATH_MAX);
if (res == -1) return true;
bool dir_found = false;
for (int i = 0; i < sizeof(device_dirs) / sizeof(char*); i++) {
if (memcmp(dirpath, device_dirs[i], strlen(device_dirs[i])) == 0) {
dir_found = true;
break;
}
}
if (!dir_found)
return true;
snprintf(namebuf, PATH_MAX, "%s/%s/name", dirpath, name);
FILE* namefile = fopen(namebuf, "r");
if (!namefile)
return true;
int namelen = fread(namebuf, 1, PATH_MAX, namefile);
namebuf[namelen - 1] = 0;
for (int i = 0; i < sizeof(allowed_devices) / sizeof(char*); i++) {
if (strcmp(namebuf, allowed_devices[i]) == 0)
return true;
}
return false;
}
int openat64(int dirfd, const char* pathname, int flags, mode_t mode) {
if (!real_openat64) real_openat64 = dlsym(RTLD_NEXT, "openat64");
bool allowed = true;
//if (memcmp(pathname, "input", 5) == 0 && pathname[5] != 0)
allowed = veto_input_open(dirfd, pathname);
if (allowed) {
return real_openat64(dirfd, pathname, flags, mode);
} else {
errno = EACCES;
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment