Skip to content

Commit

Permalink
[storage] Secure use of cached keyfiles
Browse files Browse the repository at this point in the history
This is a Jolla-unique change due to heavy customization of storage.c

Upstream decided to save its service data to file with an assumption
that it is empty by default and it is ok to create a new keyfile.

Meanwhile, we have decided to cache the keyfiles read from config files.
This causes a conflict, where service_save may order storage_save to
write a different keyfile to disk than the one loaded to cache with
storage_load. Afterwards, reading key-var values with storage_load would
return values stored in cache instead of those stored in file.

Thus, just like upstream storage_save would write to disk, and
subsequent storage_load would read from disk, we clean the cache, and
any subsequent storage_load would use the keyfile that was ordered to be
stored.

Although the fact that the keyfile variables are different would already
be suspicious, and is indicated by a debug print, this order may work
perfectly in upstream, and with this change we can be relatively sure
that we are not introducing any regressions compared to upstream.
  • Loading branch information
ballock committed Apr 21, 2021
1 parent cec70c1 commit 2f7a4a6
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions connman/src/storage.c
Expand Up @@ -831,9 +831,18 @@ static int storage_save(GKeyFile *keyfile, char *pathname)
gsize length = 0;
GError *error = NULL;
int ret = 0;
struct keyfile_record *record = NULL;
const mode_t perm = STORAGE_FILE_MODE;
const mode_t old_mask = umask(~perm & 0777);

record = g_hash_table_lookup(keyfile_hash, pathname);
if (record && record->keyfile != keyfile) {
DBG("Keyfile cache pollution. Trying to write different data "
"than in cache. Cache %p, writing %p.",
record->keyfile, keyfile);
keyfile_free(record);
keyfile_insert(pathname, keyfile);
}
data = g_key_file_to_data(keyfile, &length, NULL);

if (!g_file_set_contents(pathname, data, length, &error)) {
Expand Down

0 comments on commit 2f7a4a6

Please sign in to comment.