mesh: Fix file open error checking and permissions
authorBrian Gix <brian.gix@intel.com>
Wed, 15 May 2019 16:34:16 +0000 (09:34 -0700)
committerAnupam Roy <anupam.r@samsung.com>
Tue, 17 Dec 2019 14:27:19 +0000 (19:57 +0530)
Change-Id: Ie3836862c91e779fc208b920071aa970d9681bc3
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
mesh/keyring.c
mesh/storage.c

index 59aa1ea..4b1460a 100644 (file)
@@ -67,7 +67,7 @@ bool keyring_put_net_key(struct mesh_node *node, uint16_t net_idx,
                                                                net_idx);
        l_debug("Put Net Key %s", key_file);
 
-       fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC);
+       fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
        if (fd >= 0) {
                if (write(fd, key, sizeof(*key)) == sizeof(*key))
                        result = true;
@@ -112,7 +112,8 @@ bool keyring_put_app_key(struct mesh_node *node, uint16_t app_idx,
                }
                lseek(fd, 0, SEEK_SET);
        } else
-               fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC);
+               fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC,
+                                                       S_IRUSR | S_IWUSR);
 
        if (fd >= 0) {
                if (write(fd, key, sizeof(*key)) == sizeof(*key))
@@ -148,7 +149,8 @@ bool keyring_put_remote_dev_key(struct mesh_node *node, uint16_t unicast,
                                                dev_key_dir, unicast + i);
                l_debug("Put Dev Key %s", key_file);
 
-               fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC);
+               fd = open(key_file, O_WRONLY | O_CREAT | O_TRUNC,
+                                                       S_IRUSR | S_IWUSR);
                if (fd >= 0) {
                        if (write(fd, dev_key, 16) != 16)
                                result = false;
index 40c9895..57e37e1 100644 (file)
@@ -196,7 +196,7 @@ static bool parse_config(char *in_file, char *out_dir, const uint8_t uuid[16])
        l_info("Loading configuration from %s", in_file);
 
        fd = open(in_file, O_RDONLY);
-       if (!fd)
+       if (fd < 0)
                return false;
 
        if (fstat(fd, &st) == -1) {