From 78d6a052fc6431ad45ff3a24461fd7744a3c1c28 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Tue, 17 Nov 2020 20:32:26 -0500 Subject: enforce device type of mmcblk, fail if /sys/block//device is not a readable directory --- deonebook.c | 12 +++++++++++- deonebook.h | 2 +- genkey.c | 26 ++++++++++++++------------ 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/deonebook.c b/deonebook.c index 23b61ea..f943396 100644 --- a/deonebook.c +++ b/deonebook.c @@ -1,4 +1,5 @@ #define _XOPEN_SOURCE 700 +#include #include #include #include @@ -37,12 +38,21 @@ int main(int argc, char *argv[]) if (optind < argc) { device = argv[optind]; } + if (strstr(device, "mmcblk") != device) { fprintf(stderr, "device must be an mmcblk device\n"); return 1; } - key = genkey(device); + char path[256]; + snprintf(path, sizeof(path), "/sys/block/%s/device", device); + DIR *d = opendir(path); + if (d == NULL) { + fprintf(stderr, "device '%s' not found\n", device); + return 1; + } + + key = genkey(dirfd(d)); } if (key == NULL) { diff --git a/deonebook.h b/deonebook.h index 23ef2f7..6daf22a 100644 --- a/deonebook.h +++ b/deonebook.h @@ -3,7 +3,7 @@ #define KEY_SIZE 16 -unsigned char *genkey(const char *dev); +unsigned char *genkey(int dirfd); unsigned char *readkey(const char *path); #endif diff --git a/genkey.c b/genkey.c index 8bb224a..fd17060 100644 --- a/genkey.c +++ b/genkey.c @@ -1,8 +1,12 @@ +#define _XOPEN_SOURCE 700 #include +#include #include #include #include #include +#include + #include "deonebook.h" #define SCR_SIZE 8 @@ -34,27 +38,25 @@ static unsigned char twiddle(unsigned char buf[2]) return c; } -static bool get_sd_register(const char *dev, const char *reg, size_t n, unsigned char buf[n]) +static bool get_sd_register(int dirfd, const char *reg, size_t n, unsigned char buf[n]) { - char path[256]; - snprintf(path, sizeof(path), "/sys/block/%s/device/%s", dev, reg); - - FILE *f = fopen(path, "r"); - if (f == NULL) { + int fd = openat(dirfd, reg, O_RDONLY); + if (fd == -1) { return false; } for (size_t i = 0; i < n; i++) { unsigned char hex[2]; - fread(hex, 2, 1, f); + read(fd, hex, sizeof(hex)); buf[i] = twiddle(hex); } - fclose(f); + close(fd); + return true; } -unsigned char * genkey(const char *dev) +unsigned char * genkey(int dirfd) { static unsigned char key[KEY_SIZE]; const unsigned char keycode[] = "eone"; @@ -65,15 +67,15 @@ unsigned char * genkey(const char *dev) unsigned char csd[CSD_SIZE] = "testtesttesttest"; unsigned char cid[CID_SIZE] = "testtesttesttest"; - if (!get_sd_register(dev, "cid", sizeof(cid), cid)) { + if (!get_sd_register(dirfd, "cid", sizeof(cid), cid)) { fprintf(stderr, "warning: CID register not read\n"); allread = false; } - if (!get_sd_register(dev, "csd", sizeof(csd), csd)) { + if (!get_sd_register(dirfd, "csd", sizeof(csd), csd)) { fprintf(stderr, "warning: CSD register not read\n"); allread = false; } - if (!get_sd_register(dev, "scr", sizeof(scr), scr)) { + if (!get_sd_register(dirfd, "scr", sizeof(scr), scr)) { fprintf(stderr, "warning: SCR register not read\n"); allread = false; } -- cgit v1.2.1