Skip to contentSkip to conversation

Add fingerprint quering and database extraction functionality to RMC

PATCH New Todor Minchev 1 participant 6 messages Feb 14, 2017 Kbuild

lore
Share & tools
lore
#0 lore raw

v3 cover Add fingerprint quering and database extraction functionality to RMC

This is V3 of the patchset. It addresses all feedback received in V2.

Changes from previous version:

* use mkdir() instead of system() to create output directories
* use ASCII representation of fingerprint signatures in hex as directory names
* remove extra verbosity from fingerprint dump

Todor Minchev (5):
Makefile: disable silent mode in Makefiles
Makefile: add debug target
rmc: enable reading the contents of an existing fingerprint file
rmc: remove unnecessary return variable
rmc: add database extraction functionality

 Makefile              |  23 +++++----
 Makefile.efi          |  10 ++--
 inc/rmc_api.h         |   9 ++++
 inc/rmcl.h            |   7 +++
 src/lib/api.c         | 126 +++++++++++++++++++++++++++++++++++++++++++++++++-
 src/lib/common/rmcl.c |  17 ++++++-
 src/rmc.c             | 123 ++++++++++++++++++++++++++++++++----------------
 7 files changed, 256 insertions(+), 59 deletions(-)
Signature · 1 line
2.11.1
#1 lore raw

PATCH v3 1/5 Makefile: disable silent mode in Makefiles

By default make will output the commands that are executed for each target. Silent mode can be enabled with the '-s' option.

Example:
make -s

---
 Makefile     | 21 +++++++++++----------
 Makefile.efi | 10 +++++-----
 2 files changed, 16 insertions(+), 15 deletions(-)
Makefile +11 -10
index 9ade775..c58047a 100644
@@ -25,23 +25,24 @@ RMC_CFLAGS := -Wall -I$(TOPDIR)/inc
 all: rmc
 
 $(ALL_OBJS): %.o: %.c
-	@$(CC) -c $(CFLAGS) $(RMC_CFLAGS) $< -o $@
+	$(CC) -c $(CFLAGS) $(RMC_CFLAGS) $< -o $@
 
 librmc: $(RMC_LIB_OBJ)
-	@$(AR) rcs src/lib/[email protected] $^
+	$(AR) rcs src/lib/[email protected] $^
 
 rmc: $(RMC_TOOL_OBJ) librmc
-	@$(CC) $(CFLAGS) $(RMC_CFLAGS) -Lsrc/lib/ -lrmc $(RMC_TOOL_OBJ) src/lib/librmc.a -o src/$@
+	$(CC) $(CFLAGS) $(RMC_CFLAGS) -Lsrc/lib/ -lrmc $(RMC_TOOL_OBJ) \
+  src/lib/librmc.a -o src/$@
 
 clean:
-	@rm -f $(ALL_OBJS) src/rmc src/lib/librmc.a
+	rm -f $(ALL_OBJS) src/rmc src/lib/librmc.a
 
 .PHONY: clean rmc librmc
 
 install:
-	@mkdir -p $(RMC_INSTALL_BIN_PATH)
-	@install -m 755 src/rmc $(RMC_INSTALL_BIN_PATH)
-	@mkdir -p $(RMC_INSTALL_LIB_PATH)
-	@install -m 644 src/lib/librmc.a $(RMC_INSTALL_LIB_PATH)
-	@mkdir -p $(RMC_INSTALL_HEADER_PATH)
-	@install -m 644 $(RMC_INSTALL_HEADERS) $(RMC_INSTALL_HEADER_PATH)
+	mkdir -p $(RMC_INSTALL_BIN_PATH)
+	install -m 755 src/rmc $(RMC_INSTALL_BIN_PATH)
+	mkdir -p $(RMC_INSTALL_LIB_PATH)
+	install -m 644 src/lib/librmc.a $(RMC_INSTALL_LIB_PATH)
+	mkdir -p $(RMC_INSTALL_HEADER_PATH)
+	install -m 644 $(RMC_INSTALL_HEADERS) $(RMC_INSTALL_HEADER_PATH)
Makefile.efi +5 -5
index 3bd417d..66081d7 100644
@@ -18,16 +18,16 @@ RMC_CFLAGS := -DRMC_EFI -Wall -I$(TOPDIR)/inc -fpic -nostdinc -nostdlib  -fno-bu
 all: librmcefi
 
 $(RMC_LIB_OBJ): %.efi.o: %.c
-	@$(CC) -c $(CFLAGS) $(RMC_CFLAGS) $< -o $@
+	$(CC) -c $(CFLAGS) $(RMC_CFLAGS) $< -o $@
 
 librmcefi: $(RMC_LIB_OBJ)
-	@$(AR) rcs src/lib/[email protected] $^
+	$(AR) rcs src/lib/[email protected] $^
 
 clean:
-	@rm -f $(RMC_LIB_OBJ) src/lib/librmcefi.a
+	rm -f $(RMC_LIB_OBJ) src/lib/librmcefi.a
 
 .PHONY: clean librmcefi
 
 install:
-	@mkdir -p $(RMC_INSTALL_LIB_PATH)
-	@install -m 644 $(RMC_INSTALL_LIBS) $(RMC_INSTALL_LIB_PATH)
+	mkdir -p $(RMC_INSTALL_LIB_PATH)
+	install -m 644 $(RMC_INSTALL_LIBS) $(RMC_INSTALL_LIB_PATH)
Signature · 1 line
2.11.1
#2 lore raw

PATCH v3 2/5 Makefile: add debug target

In reply to Todor Minchev

A debug version of the rmc binary can be built by using the debug Makefile target. This will include debug symbols and will disable compiler optimizations.

Example:

make debug

---
 Makefile | 2 ++
 1 file changed, 2 insertions(+)
Makefile +2 -0
index c58047a..fdd936f 100644
@@ -23,6 +23,8 @@ ALL_OBJS := $(RMC_TOOL_OBJ) $(RMC_LIB_OBJ)
 RMC_CFLAGS := -Wall -I$(TOPDIR)/inc
 
 all: rmc
+debug: RMC_CFLAGS += -DDEBUG -g -O0
+debug: rmc
 
 $(ALL_OBJS): %.o: %.c
 	$(CC) -c $(CFLAGS) $(RMC_CFLAGS) $< -o $@
Signature · 1 line
2.11.1
#3 lore raw

PATCH v3 3/5 rmc: enable reading the contents of an existing fingerprint file

In reply to Todor Minchev

The contents of an existing fingerprint file can be read and output on the command line with the following options:

rmc -E -f input_fingerprint_file

---
 src/rmc.c | 97 +++++++++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 64 insertions(+), 33 deletions(-)
src/rmc.c +64 -33
index 062dd36..54710c7 100644
@@ -14,43 +14,47 @@
 #include <rmc_api.h>
 
 #define USAGE "RMC (Runtime Machine configuration) Tool\n" \
-    "NOTE: Most of usages require root permission (sudo)\n" \
+    "NOTE: Most of usages require root permission (sudo)\n\n" \
     "rmc -F [-o output_fingerprint]\n" \
     "rmc -R [-f <fingerprint file>] -b <blob file list> [-o output_record]\n" \
     "rmc -D <rmc record file list> [-o output_database]\n" \
-	"rmc -B <name of file blob> -d <rmc database file> -o output_file\n" \
-	"\n" \
-	"-F: generate board rmc fingerprint of board\n" \
-	"-R: generate board rmc record of board with its fingerprint and file blobs.\n" \
-    "-f: fingerprint file to be packed in record, rmc will create a fingerprint for board and use it internally to\n" \
-    "    generate record if -f is missed.\n" \
-    "-b: files to be packed in record\n" \
-	"-G: generate rmc database file with records specified in record file list\n" \
-	"-B: get a flie blob with specified name associated to the board rmc is running on\n" \
-	"-d: database file to be queried\n" \
-	"-o: path and name of output file of a specific command\n" \
-	"\n" \
-    "Examples (Steps in an order to add board support into rmc):\n" \
-    "generate board fingerprint:\n" \
-    "rmc -F\n\n" \
-    "generate a rmc record for the board with two file blobs, output to:\n" \
-    "a specified file:\n" \
-    "rmc -R -f fingerprint -b file_1 file_2 -o my_board.record\n\n" \
-    "generate a rmc database file with records from 3 different boards:\n" \
-    "rmc -D board1_record board2_record board3_record\n\n" \
-    "query a file blob named audio.conf associated to the board rmc is running on in database my_rmc.db and output\n" \
-    "to /tmp/new_audio.conf:\n" \
-    "rmc -B audio.conf -d my_rmc.db -o /tmp/new_audio.conf\n\n"
+    "rmc -B <name of file blob> -d <rmc database file> -o output_file\n\n" \
+  "-F: manage fingerprint file\n" \
+    "\t-o output_file: store RMC fingerprint of current board in output_file\n" \
+  "-R: generate board rmc record of board with its fingerprint and file blobs.\n" \
+    "\t-f intput_file : input fingerprint file to be packed in record\n\n" \
+    "\tNOTE: RMC will create a fingerprint for the board and use it to\n" \
+    "\tgenerate record if an input fingerprint file is not provided.\n\n" \
+    "\t-b: files to be packed in record\n\n" \
+  "-G: generate rmc database file with records specified in record file list\n\n" \
+  "-B: get a file blob with specified name associated to the board rmc is\n" \
+  "running on\n" \
+    "\t-d: database file to be queried\n" \
+    "\t-o: path and name of output file of a specific command\n\n" \
+  "-E: Extract data from fingerprint file and print it to terminal\n" \
+    "\t-f: fingerprint file to extract\n\n" \
+    "Examples (Steps in an order to add board support into rmc):\n\n" \
+    "1. Generate board fingerprint:\n" \
+    "\trmc -F\n\n" \
+    "2. Generate a rmc record for the board with two file blobs and save it\n" \
+    "to a specified file:\n" \
+    "\trmc -R -f fingerprint -b file_1 file_2 -o my_board.record\n\n" \
+    "3. Generate a rmc database file with records from 3 different boards:\n" \
+    "\trmc -D board1_record board2_record board3_record\n\n" \
+    "4. Query a file blob named audio.conf associated to the board rmc is\n" \
+    "running on in database my_rmc.db and output to /tmp/new_audio.conf:\n" \
+    "\trmc -B audio.conf -d my_rmc.db -o /tmp/new_audio.conf\n\n"
 
 
 #define RMC_OPT_CAP_F   (1 << 0)
 #define RMC_OPT_CAP_R   (1 << 1)
 #define RMC_OPT_CAP_D   (1 << 2)
 #define RMC_OPT_CAP_B   (1 << 3)
-#define RMC_OPT_F       (1 << 4)
-#define RMC_OPT_O       (1 << 5)
-#define RMC_OPT_B       (1 << 6)
-#define RMC_OPT_D       (1 << 7)
+#define RMC_OPT_CAP_E   (1 << 4)
+#define RMC_OPT_F       (1 << 5)
+#define RMC_OPT_O       (1 << 6)
+#define RMC_OPT_B       (1 << 7)
+#define RMC_OPT_D       (1 << 8)
 
 static void usage () {
     fprintf(stdout, USAGE);
@@ -78,7 +82,7 @@ static void dump_fingerprint(rmc_fingerprint_t *fp) {
 static int write_fingerprint_file(const char* pathname, rmc_fingerprint_t *fp) {
     int i;
     int first = 0;
-
+    /* TODO - do we need to open/close file multiple times to write each field */
     for (i = 0; i < RMC_FINGER_NUM; i++) {
         if (write_file(pathname, &fp->rmc_fingers[i].type, sizeof(fp->rmc_fingers[i].type), first))
             return 1;
@@ -311,7 +315,7 @@ int main(int argc, char **argv){
     /* parse options */
     opterr = 0;
 
-    while ((c = getopt(argc, argv, "FRD:B:b:f:o:d:")) != -1)
+    while ((c = getopt(argc, argv, "FRED:B:b:f:o:d:")) != -1)
         switch (c) {
         case 'F':
             options |= RMC_OPT_CAP_F;
@@ -319,6 +323,9 @@ int main(int argc, char **argv){
         case 'R':
             options |= RMC_OPT_CAP_R;
             break;
+        case 'E':
+            options |= RMC_OPT_CAP_E;
+            break;
         case 'D':
             /* we don't know number of arguments for this option at this point,
              * allocate array with argc which is bigger than needed. But we also
@@ -388,7 +395,8 @@ int main(int argc, char **argv){
             break;
         case '?':
             if (optopt == 'F' || optopt == 'R' || optopt == 'D' || optopt == 'B' || \
-                    optopt == 'b' || optopt == 'f' || optopt == 'o' || optopt == 'd')
+                    optopt == 'E' ||  optopt == 'b' || optopt == 'f' || \
+                    optopt == 'o' || optopt == 'd')
                 fprintf(stderr, "\nWRONG USAGE: -%c\n\n", optopt);
             else if (isprint(optopt))
                 fprintf(stderr, "Unknown option `-%c'.\n\n", optopt);
@@ -421,6 +429,13 @@ int main(int argc, char **argv){
         return 1;
     }
 
+    /* sanity check for -E */
+    if ((options & RMC_OPT_CAP_E) && (!(options & RMC_OPT_F))) {
+        fprintf(stderr, "\nERROR: -E requires -f <fingerprint file name>\n\n");
+        usage();
+        return 1;
+    }
+
     /* sanity check for -B */
     if ((options & RMC_OPT_CAP_B) && (!(options & RMC_OPT_D) || !(options & RMC_OPT_O))) {
         fprintf(stderr, "\nWRONG: -B requires -d and -o\n\n");
@@ -445,10 +460,27 @@ int main(int argc, char **argv){
             rmc_free_file(&file);
             goto main_free;
         }
-
         rmc_free_file(&file);
     }
 
+    if (options & RMC_OPT_CAP_E) {
+        /* print fingerpring file to console*/
+        if (options & RMC_OPT_F) {
+            rmc_fingerprint_t fp;
+            /* read fingerprint file*/
+            if (input_fingerprint != NULL) {
+                if (read_fingerprint_from_file(input_fingerprint, &fp, &raw_fp)) {
+                    fprintf(stderr, "Cannot read fingerprint from %s\n\n",
+                    input_fingerprint);
+                    goto main_free;
+                }
+                dump_fingerprint(&fp);
+            }else {
+                printf("Fingerprint file not provided! Exiting.\n");
+            }
+        }
+    }
+
     /* generate RMC database file */
     if (options & RMC_OPT_CAP_D) {
         int record_idx = 0;
@@ -580,7 +612,6 @@ int main(int argc, char **argv){
             rmc_free_fingerprint(&fingerprint);
             goto main_free;
         }
-
         rmc_free_fingerprint(&fingerprint);
     }
 
Signature · 1 line
2.11.1
#4 lore raw

PATCH v3 4/5 rmc: remove unnecessary return variable

In reply to Todor Minchev
---
 src/rmc.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
src/rmc.c +1 -3
index 54710c7..9a63069 100644
@@ -218,7 +218,6 @@ read_fp_done:
 static rmc_file_t *read_policy_file(char *pathname, int type) {
     rmc_file_t *tmp = NULL;
     rmc_size_t policy_len = 0;
-    int ret;
     char *path_token;
 
     if ((tmp = calloc(1, sizeof(rmc_file_t))) == NULL) {
@@ -230,8 +229,7 @@ static rmc_file_t *read_policy_file(char *pathname, int type) {
     tmp->next = NULL;
 
     if (type == RMC_GENERIC_FILE) {
-        ret = read_file(pathname, (char **)&tmp->blob, &policy_len);
-        if (ret) {
+        if (read_file(pathname, (char **)&tmp->blob, &policy_len)) {
             fprintf(stderr, "Failed to read file %s\n\n", pathname);
             free(tmp);
             return NULL;
Signature · 1 line
2.11.1
#5 lore raw

PATCH v3 5/5 rmc: add database extraction functionality

In reply to Todor Minchev

The contents of an existing database file can be extracted with the -E option. By default the top level of the directory tree is rmc_db_dump, an alternative path can be specified with the -o option. The file blobs corresponding to a given record will be saved in a separate sub-directory. The sub-directory name for each record is the hex representation of the signature corresponding to the fingerprint for that record.

Example:
./src/rmc -E -d rmc.db -o output/directory/

Successfully extracted rmc.db

---
 inc/rmc_api.h         |   9 ++++
 inc/rmcl.h            |   7 +++
 src/lib/api.c         | 126 +++++++++++++++++++++++++++++++++++++++++++++++++-
 src/lib/common/rmcl.c |  17 ++++++-
 src/rmc.c             |  30 ++++++++----
 5 files changed, 177 insertions(+), 12 deletions(-)
inc/rmc_api.h +9 -0
index a484389..2f8c978 100644
@@ -109,6 +109,15 @@ extern int read_file(const char *pathname, char **data, rmc_size_t* len);
  */
 int write_file(const char *pathname, void *data, rmc_size_t len, int append);
 
+/* extract the contents of a database file and store the files corresponding to
+ * each record in a separate directory. The name of each directory is the signature
+ * of the fingerpring for that record with all non-alphanumeric characters stripped
+ * (in) db_pathname: The path and file name of a RMC database file generated by RMC tool
+ * (in) output_path: A directory path to extract the database to
+ * return: 0 on success, non-zero on failure.
+ */
+int dump_db(char *db_pathname, char *output_path) ;
+
 #else
 /* 2 - API for UEFI context */
 
inc/rmcl.h +7 -0
index 5bbad42..471ebfe 100644
@@ -164,4 +164,11 @@ extern int rmcl_generate_db(rmc_record_file_t *record_files, rmc_uint8_t **rmc_d
  */
 extern int query_policy_from_db(rmc_fingerprint_t *fingerprint, rmc_uint8_t *rmc_db, rmc_uint8_t type, char *blob_name, rmc_file_t *policy);
 
+/*
+ * Check if db_blob has a valid rmc database signature
+ *
+ * return 0 if db_blob has a valid signature or non-zero otherwise
+ */
+int is_rmcdb(rmc_uint8_t *db_blob);
+
 #endif /* INC_RMCL_H_ */
src/lib/api.c +124 -2

145 lines not on this page · raw message

src/lib/common/rmcl.c +16 -1
index 67622a0..58a4a52 100644
@@ -193,6 +193,21 @@ static int match_record(rmc_record_header_t *r, rmc_signature_t* sig) {
     return strncmp((const char *)r->signature.raw, (const char *)sig->raw, sizeof(r->signature.raw));
 }
 
+int is_rmcdb(rmc_uint8_t *db_blob) {
+    rmc_db_header_t *db_header = NULL;
+
+    if (db_blob == NULL)
+        return 1;
+
+    db_header = (rmc_db_header_t *)db_blob;
+
+    /* sanity check of db */
+    if (strncmp((const char *)db_header->signature, (const char *)rmc_db_signature, RMC_DB_SIG_LEN))
+        return 1;
+    else
+        return 0;
+}
+
 int query_policy_from_db(rmc_fingerprint_t *fingerprint, rmc_uint8_t *rmc_db, rmc_uint8_t type, char *blob_name, rmc_file_t *policy) {
     rmc_meta_header_t meta_header;
     rmc_db_header_t *db_header = NULL;
@@ -211,7 +226,7 @@ int query_policy_from_db(rmc_fingerprint_t *fingerprint, rmc_uint8_t *rmc_db, rm
     db_header = (rmc_db_header_t *)rmc_db;
 
     /* sanity check of db */
-    if (strncmp((const char *)db_header->signature, (const char *)rmc_db_signature, RMC_DB_SIG_LEN))
+    if (is_rmcdb(rmc_db))
         return 1;
 
     /* calculate signature of fingerprint */
src/rmc.c +21 -9
index 9a63069..0c4127d 100644
@@ -31,8 +31,10 @@
   "running on\n" \
     "\t-d: database file to be queried\n" \
     "\t-o: path and name of output file of a specific command\n\n" \
-  "-E: Extract data from fingerprint file and print it to terminal\n" \
-    "\t-f: fingerprint file to extract\n\n" \
+  "-E: Extract data from fingerprint file or database\n" \
+    "\t-f: fingerprint file to extract\n" \
+    "\t-d: database file to extract\n" \
+    "\t-o: directory to extract the database to\n\n" \
     "Examples (Steps in an order to add board support into rmc):\n\n" \
     "1. Generate board fingerprint:\n" \
     "\trmc -F\n\n" \
@@ -408,12 +410,14 @@ int main(int argc, char **argv){
 
     /* sanity check for -o */
     if (options & RMC_OPT_O) {
-        rmc_uint16_t opt_o = options & (RMC_OPT_CAP_D | RMC_OPT_CAP_R | RMC_OPT_CAP_F | RMC_OPT_CAP_B);
+        rmc_uint16_t opt_o = options & (RMC_OPT_CAP_D | RMC_OPT_CAP_R |
+            RMC_OPT_CAP_F | RMC_OPT_CAP_B | RMC_OPT_CAP_E);
         if (!(opt_o)) {
-            fprintf(stderr, "\nWRONG: Option -o cannot be applied without -B, -D, -R or -F\n\n");
+            fprintf(stderr, "\nWRONG: Option -o cannot be applied without -B, -D, -E, -R or -F\n\n");
             usage();
             return 1;
-        } else if (opt_o != RMC_OPT_CAP_D && opt_o != RMC_OPT_CAP_R && opt_o != RMC_OPT_CAP_F && opt_o != RMC_OPT_CAP_B) {
+        } else if (opt_o != RMC_OPT_CAP_D && opt_o != RMC_OPT_CAP_R &&
+            opt_o != RMC_OPT_CAP_F && opt_o != RMC_OPT_CAP_B  && opt_o != RMC_OPT_CAP_E) {
             fprintf(stderr, "\nWRONG: Option -o can be applied with only one of -B, -D, -R and -F\n\n");
             usage();
             return 1;
@@ -428,8 +432,8 @@ int main(int argc, char **argv){
     }
 
     /* sanity check for -E */
-    if ((options & RMC_OPT_CAP_E) && (!(options & RMC_OPT_F))) {
-        fprintf(stderr, "\nERROR: -E requires -f <fingerprint file name>\n\n");
+    if ((options & RMC_OPT_CAP_E) && (!(options & RMC_OPT_F) && !(options & RMC_OPT_D))) {
+        fprintf(stderr, "\nERROR: -E requires -f <fingerprint file name> or -d <database file name>\n\n");
         usage();
         return 1;
     }
@@ -446,7 +450,8 @@ int main(int argc, char **argv){
         rmc_file_t file;
 
         if (!output_path) {
-            fprintf(stderr, "-B internal error, with -o but no output pathname specified\n\n");
+            fprintf(stderr, "-B internal error, with -o but no output \
+                pathname specified\n\n");
             goto main_free;
         }
 
@@ -454,7 +459,8 @@ int main(int argc, char **argv){
             goto main_free;
 
         if (write_file(output_path, file.blob, file.blob_len, 0)) {
-            fprintf(stderr, "-B failed to write file %s to %s\n\n", input_blob_name, output_path);
+            fprintf(stderr, "-B failed to write file %s to %s\n\n",
+                input_blob_name, output_path);
             rmc_free_file(&file);
             goto main_free;
         }
@@ -476,6 +482,12 @@ int main(int argc, char **argv){
             }else {
                 printf("Fingerprint file not provided! Exiting.\n");
             }
+        } else if (options & RMC_OPT_D) {
+            if(dump_db(input_db_path_d, output_path)) {
+               fprintf(stderr, "\nFailed to extract %s\n\n", input_db_path_d);
+               goto main_free;
+            } else
+               printf("\nSuccessfully extracted %s\n\n", input_db_path_d);
         }
     }
 
Signature · 1 line
2.11.1