From 75c22eb0622916b74754a06bc35f3a3301659206 Mon Sep 17 00:00:00 2001 From: Rafal Krypa Date: Fri, 26 Jul 2013 12:36:08 +0200 Subject: [PATCH] Create format strings for scanf statically. [Issue#] SSDWSSP-372 [Feature] Remove unneeded memory allocations for scanf format strings. [Cause] When reading Smack rules, fields length must be checked. [Solution] Create format strings with legnth specifiers at build time. [Verification] Build, install, run tests. Change-Id: Ib0b20e3d46fe0d4af957f13a37627d14831283d2 --- src/access-db.c | 9 +-------- src/privilege-control.c | 15 ++++++--------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/access-db.c b/src/access-db.c index 68b9bf9..ec6c2a4 100644 --- a/src/access-db.c +++ b/src/access-db.c @@ -120,18 +120,11 @@ static int add_id_to_database_internal(const char * id, db_app_type_t app_type) static int get_all_ids_internal (char *** ids, int * len, db_app_type_t app_type) { int ret; - char* scanf_label_format AUTO_FREE; FILE* file_db AUTO_FCLOSE; const char* db_file_name = db_file_names[app_type]; char smack_label[SMACK_LABEL_LEN + 1]; element_t* begin_of_list = NULL; - if (asprintf(&scanf_label_format, "%%%ds\\n", SMACK_LABEL_LEN) < 0) { - C_LOGE("Error while creating scanf input label format"); - ret = PC_ERR_MEM_OPERATION; - goto out; - } - file_db = fopen(db_file_name, "r"); if (NULL == file_db) { SECURE_SLOGE("Error while opening database file: %s", db_file_name); @@ -153,7 +146,7 @@ static int get_all_ids_internal (char *** ids, int * len, db_app_type_t app_type // reading from file ("database") // notice that first element always stays with empty "value" - while (fscanf(file_db, scanf_label_format, smack_label) == 1) { + while (fscanf(file_db, "%" TOSTRING(SMACK_LABEL_LEN) "s\n", smack_label) == 1) { smack_label[SMACK_LABEL_LEN] = '\0'; if (!smack_label_is_valid(smack_label)) { C_LOGD("Found entry in database, but it's not correct SMACK label: \"%s\"", smack_label); diff --git a/src/privilege-control.c b/src/privilege-control.c index bd990b1..488f0f5 100644 --- a/src/privilege-control.c +++ b/src/privilege-control.c @@ -728,18 +728,11 @@ static int perm_to_smack_from_file(struct smack_accesses* smack, { C_LOGD("Enter function: %s", __func__); - char* format_string AUTO_FREE; char smack_subject[SMACK_LABEL_LEN + 1]; char smack_object[SMACK_LABEL_LEN + 1]; - char smack_accesses[10]; + char smack_accesses[ACC_LEN + 1]; FILE* file AUTO_FCLOSE; - if (asprintf(&format_string,"%%%ds %%%ds %%%lus\n", - SMACK_LABEL_LEN, SMACK_LABEL_LEN, (unsigned long)sizeof(smack_accesses)) == -1) { - C_LOGE("asprintf failed"); - return PC_ERR_MEM_OPERATION; - } - file = fopen(rules_file_path, "r"); C_LOGD("path = %s", rules_file_path); if (file == NULL) { @@ -747,7 +740,11 @@ static int perm_to_smack_from_file(struct smack_accesses* smack, return PC_OPERATION_SUCCESS; } - while (fscanf(file, format_string, smack_subject, smack_object, smack_accesses) == 3) { + while (fscanf(file, + "%" TOSTRING(SMACK_LABEL_LEN) "s " + "%" TOSTRING(SMACK_LABEL_LEN) "s " + "%" TOSTRING(ACC_LEN) "s\n", + smack_subject, smack_object, smack_accesses) == 3) { if (!strcmp(smack_subject, app_label_template)) strcpy(smack_subject, app_label); -- 2.7.4