Skip to content

Commit

Permalink
Check file size against memlock
Browse files Browse the repository at this point in the history
  • Loading branch information
paolostivanin committed Mar 9, 2024
1 parent c5d8ada commit 73c9d21
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/common/aegis.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ get_otps_from_encrypted_backup (const gchar *path,
gsize out_len;
guchar *b64decoded_db = g_base64_decode (json_string_value (json_object_get (json, "db")), &out_len);
if (out_len > max_file_size) {
g_set_error (err, file_too_big_gquark (), FILE_TOO_BIG, "File is too big");
g_set_error (err, file_too_big_gquark (), FILE_TOO_BIG, FILE_SIZE_SECMEM_MSG);
g_free (tag);
g_free (nonce);
gcry_free (master_key);
Expand Down
2 changes: 1 addition & 1 deletion src/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ get_data_from_encrypted_backup (const gchar *path,
} else if (enc_buf_size > max_file_size) {
g_object_unref (in_stream);
g_object_unref (in_file);
g_set_error (err, file_too_big_gquark (), FILE_TOO_BIG, "File is too big");
g_set_error (err, file_too_big_gquark (), FILE_TOO_BIG, FILE_SIZE_SECMEM_MSG);
return NULL;
}

Expand Down
13 changes: 9 additions & 4 deletions src/common/freeotp.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@


GSList *
get_freeotpplus_data (const gchar *path,
GError **err)
get_freeotpplus_data (const gchar *path,
gint32 max_file_size,
GError **err)
{
GSList *otps = NULL;
goffset fs = get_file_size (path);
if (fs < 10) {
g_printerr ("Couldn't get the file size (file doesn't exit or wrong file selected\n");
g_set_error (err, file_too_big_gquark (), GENERIC_ERRCODE, "Couldn't get the file size (file doesn't exit or wrong file selected.");
return NULL;
}
if (fs > max_file_size) {
g_set_error (err, file_too_big_gquark (), FILE_TOO_BIG, FILE_SIZE_SECMEM_MSG);
return NULL;
}
gchar *sec_buf = gcry_calloc_secure (fs, 1);
if (!g_file_get_contents (path, &sec_buf, NULL, err)) {
g_printerr("Couldn't read into memory the freeotp txt file\n");
g_printerr("Couldn't read into memory the freeotp txt file.\n");
gcry_free (sec_buf);
return NULL;
}
Expand Down
2 changes: 2 additions & 0 deletions src/common/get-providers-data.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ GSList *get_andotp_data (const gchar *path,
GError **err);

GSList *get_freeotpplus_data (const gchar *path,
gint32 max_file_size,
GError **err);

GSList *get_aegis_data (const gchar *path,
Expand All @@ -24,6 +25,7 @@ GSList *get_authpro_data (const gchar *path,

GSList *get_twofas_data (const gchar *path,
const gchar *password,
gint32 max_file_size,
GError **err);

G_END_DECLS
1 change: 1 addition & 0 deletions src/common/gquarks.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ G_BEGIN_DECLS
#define FILE_TOO_BIG 13
#define GENERIC_ERRCODE 14
#define MEMLOCK_ERRCODE 15
#define FILE_SIZE_SECMEM_MSG "Selected file is too big. Please increase the secure memory size."

GQuark missing_file_gquark (void);

Expand Down
6 changes: 6 additions & 0 deletions src/common/twofas.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <gcrypt.h>
#include "gquarks.h"
#include "common.h"
#include "file-size.h"

#define TWOFAS_KDF_ITERS 10000
#define TWOFAS_SALT 256
Expand Down Expand Up @@ -46,8 +47,13 @@ static GSList *parse_twofas_json_data (const gchar *data,
GSList *
get_twofas_data (const gchar *path,
const gchar *password,
gint32 max_file_size,
GError **err)
{
if (get_file_size (path) > max_file_size) {
g_set_error (err, file_too_big_gquark (), FILE_TOO_BIG, FILE_SIZE_SECMEM_MSG);
return NULL;
}
return (password != NULL) ? get_otps_from_encrypted_backup (path, password, err) : get_otps_from_plain_backup (path, err);
}

Expand Down
4 changes: 2 additions & 2 deletions src/gui/imports.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ parse_data_and_update_db (AppData *app_data,
if (g_strcmp0 (action_name, ANDOTP_IMPORT_ACTION_NAME) == 0 || g_strcmp0 (action_name, ANDOTP_IMPORT_PLAIN_ACTION_NAME) == 0) {
content = get_andotp_data (filename, pwd, app_data->db_data->max_file_size_from_memlock, &err);
} else if (g_strcmp0 (action_name, FREEOTPPLUS_IMPORT_ACTION_NAME) == 0) {
content = get_freeotpplus_data (filename, &err);
content = get_freeotpplus_data (filename, app_data->db_data->max_file_size_from_memlock, &err);
} else if (g_strcmp0 (action_name, AEGIS_IMPORT_ACTION_NAME) == 0 || g_strcmp0 (action_name, AEGIS_IMPORT_ENC_ACTION_NAME) == 0) {
content = get_aegis_data (filename, pwd, app_data->db_data->max_file_size_from_memlock, &err);
} else if (g_strcmp0 (action_name, AUTHPRO_IMPORT_ENC_ACTION_NAME) == 0 || g_strcmp0 (action_name, AUTHPRO_IMPORT_PLAIN_ACTION_NAME) == 0) {
content = get_authpro_data (filename, pwd, app_data->db_data->max_file_size_from_memlock, &err);
} else if (g_strcmp0 (action_name, TWOFAS_IMPORT_ENC_ACTION_NAME) == 0 || g_strcmp0 (action_name, TWOFAS_IMPORT_PLAIN_ACTION_NAME) == 0) {
content = get_twofas_data (filename, pwd, &err);
content = get_twofas_data (filename, pwd, app_data->db_data->max_file_size_from_memlock, &err);
}

if (content == NULL) {
Expand Down

0 comments on commit 73c9d21

Please sign in to comment.