>From 7f6db4a128e4f9ecd99a493d50b0fb58c1fc8aa6 Mon Sep 17 00:00:00 2001 From: Ander Juaristi Date: Wed, 6 Apr 2016 12:55:17 +0200 Subject: [PATCH] Check the HSTS file is not world-writable * hsts.c (hsts_file_access_valid): check that the file is a regular file, and that it's not world-writable. (hsts_store_open): if the HSTS database file does not meet the above requirements, disable HSTS at all. --- src/hsts.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/hsts.c b/src/hsts.c index d5e0bee..b29f352 100644 --- a/src/hsts.c +++ b/src/hsts.c @@ -334,6 +334,22 @@ hsts_store_dump (hsts_store_t store, FILE *fp) } } +/* + * Test: + * - The file is a regular file (ie. not a symlink), and + * - The file is not world-writable. + */ +static bool +hsts_file_access_valid (const char *filename) +{ + struct_stat st; + + if (stat (filename, &st) == -1) + return false; + + return !(st.st_mode & S_IWOTH) && S_ISREG (st.st_mode); +} + /* HSTS API */ /* @@ -471,7 +487,7 @@ hsts_store_open (const char *filename) store->table = hash_table_new (0, hsts_hash_func, hsts_cmp_func); store->last_mtime = 0; - if (file_exists_p (filename)) + if (hsts_file_access_valid (filename)) { fp = fopen (filename, "r"); @@ -486,6 +502,18 @@ hsts_store_open (const char *filename) if (fstat (fileno (fp), &st) == 0) store->last_mtime = st.st_mtime; } + else if (file_exists_p (filename)) + { + /* + * If we're not reading the HSTS database, + * then by all means act as if HSTS was disabled. + */ + hsts_store_close (store); + xfree (store); + + logprintf (LOG_NOTQUIET, "Will not apply HSTS. " + "The HSTS database must be a regular and non-world-writable file.\n"); + } out: if (fp) -- 2.1.4