diff options
author | Yaniv Kaul <ykaul@redhat.com> | 2018-08-21 20:41:20 +0300 |
---|---|---|
committer | Atin Mukherjee <amukherj@redhat.com> | 2018-09-07 16:17:18 +0000 |
commit | a880d6f6aa7a2979df8aa32b58f716ef0c578d3f (patch) | |
tree | bb95d28a14afc944c3755e612c05a44ca97a4979 /xlators | |
parent | df697c68f6e1d676ae101e18b19f69e7241dc84a (diff) |
multiple xlators (mgmt): strncpy()->sprintf(), reduce strlen()'s
xlators/mgmt/glusterd/src/glusterd-geo-rep.c
xlators/mgmt/glusterd/src/glusterd-handshake.c
xlators/mgmt/glusterd/src/glusterd-sm.c
xlators/mgmt/glusterd/src/glusterd-store.c
xlators/mgmt/glusterd/src/glusterd-utils.c
xlators/mgmt/glusterd/src/glusterd-volgen.c
xlators/mgmt/glusterd/src/glusterd-volume-ops.c
xlators/mgmt/glusterd/src/glusterd.c
strncpy may not be very efficient for short strings copied into
a large buffer: If the length of src is less than n,
strncpy() writes additional null bytes to dest to ensure
that a total of n bytes are written.
Instead, use snprintf(). Try to ensure output is not
truncated.
Also:
- save the result of strlen() and re-use it when possible.
- move from strlen to SLEN (sizeof() ) for const strings.
Compile-tested only!
Change-Id: Ib5d001857236f43e41c4a51b5f48e1a33110aaeb
updates: bz#1193929
Signed-off-by: Yaniv Kaul <ykaul@redhat.com>
Diffstat (limited to 'xlators')
-rw-r--r-- | xlators/mgmt/glusterd/src/glusterd-geo-rep.c | 82 | ||||
-rw-r--r-- | xlators/mgmt/glusterd/src/glusterd-handshake.c | 30 | ||||
-rw-r--r-- | xlators/mgmt/glusterd/src/glusterd-sm.c | 11 | ||||
-rw-r--r-- | xlators/mgmt/glusterd/src/glusterd-store.c | 263 | ||||
-rw-r--r-- | xlators/mgmt/glusterd/src/glusterd-utils.c | 302 | ||||
-rw-r--r-- | xlators/mgmt/glusterd/src/glusterd-volgen.c | 15 | ||||
-rw-r--r-- | xlators/mgmt/glusterd/src/glusterd-volume-ops.c | 23 | ||||
-rw-r--r-- | xlators/mgmt/glusterd/src/glusterd.c | 24 |
8 files changed, 482 insertions, 268 deletions
diff --git a/xlators/mgmt/glusterd/src/glusterd-geo-rep.c b/xlators/mgmt/glusterd/src/glusterd-geo-rep.c index c6ddf6ca6f6..67aac44e491 100644 --- a/xlators/mgmt/glusterd/src/glusterd-geo-rep.c +++ b/xlators/mgmt/glusterd/src/glusterd-geo-rep.c @@ -239,7 +239,7 @@ __glusterd_handle_gsync_set (rpcsvc_request_t *req) glusterd_op_t cli_op = GD_OP_GSYNC_SET; char *master = NULL; char *slave = NULL; - char operation[256] = {0,}; + char operation[64] = {0,}; int type = 0; glusterd_conf_t *priv = NULL; char *host_uuid = NULL; @@ -317,32 +317,32 @@ __glusterd_handle_gsync_set (rpcsvc_request_t *req) switch (type) { case GF_GSYNC_OPTION_TYPE_CREATE: - strncpy (operation, "create", sizeof (operation)); + snprintf (operation, sizeof (operation), "create"); cli_op = GD_OP_GSYNC_CREATE; break; case GF_GSYNC_OPTION_TYPE_START: - strncpy (operation, "start", sizeof (operation)); + snprintf (operation, sizeof (operation), "start"); break; case GF_GSYNC_OPTION_TYPE_STOP: - strncpy (operation, "stop", sizeof (operation)); + snprintf (operation, sizeof (operation), "stop"); break; case GF_GSYNC_OPTION_TYPE_PAUSE: - strncpy (operation, "pause", sizeof (operation)); + snprintf (operation, sizeof (operation), "pause"); break; case GF_GSYNC_OPTION_TYPE_RESUME: - strncpy (operation, "resume", sizeof (operation)); + snprintf (operation, sizeof (operation), "resume"); break; case GF_GSYNC_OPTION_TYPE_CONFIG: - strncpy (operation, "config", sizeof (operation)); + snprintf (operation, sizeof (operation), "config"); break; case GF_GSYNC_OPTION_TYPE_STATUS: - strncpy (operation, "status", sizeof (operation)); + snprintf (operation, sizeof (operation), "status"); break; } @@ -447,8 +447,14 @@ _glusterd_urltransform_add_iter (dict_t *dict, char *key, data_t *value, void *d gf_msg_debug (this->name, 0, "value->data %s", value->data); - strncpy (slv_url, value->data, sizeof(slv_url)); - slv_url[sizeof(slv_url) - 1] = 0; + if (snprintf (slv_url, sizeof(slv_url), "%s", value->data) >= + sizeof (slv_url)) { + gf_msg (this->name, GF_LOG_ERROR, 0, + GD_MSG_SLAVE_VOL_PARSE_FAIL, + "Error in copying slave: %s!", value->data); + goto out; + } + ret = parse_slave_url (slv_url, &slave); if (ret == -1) { gf_msg (this->name, GF_LOG_ERROR, 0, @@ -1297,8 +1303,14 @@ _get_status_mst_slv (dict_t *dict, char *key, data_t *value, void *data) priv = this->private; GF_VALIDATE_OR_GOTO (this->name, priv, out); - strncpy (slv_url, value->data, sizeof(slv_url)); - slv_url[sizeof(slv_url) - 1] = 0; + if (snprintf (slv_url, sizeof(slv_url), "%s", value->data) >= + sizeof (slv_url)) { + gf_msg (this->name, GF_LOG_ERROR, 0, + GD_MSG_SLAVE_VOL_PARSE_FAIL, + "Error in copying slave: %s!", value->data); + goto out; + } + ret = parse_slave_url (slv_url, &slave); if (ret == -1) { gf_msg (this->name, GF_LOG_ERROR, 0, @@ -1551,8 +1563,13 @@ update_slave_voluuid (dict_t *dict, char *key, data_t *value, void *data) * With volume uuid, number of ':' is 5 and is 4 without. */ if (cnt == 4) { - strncpy (slv_url, value->data, sizeof(slv_url)); - slv_url[sizeof(slv_url) - 1] = 0; + if (snprintf (slv_url, sizeof(slv_url), "%s", value->data) >= + sizeof (slv_url)) { + gf_msg (this->name, GF_LOG_ERROR, 0, + GD_MSG_SLAVE_VOL_PARSE_FAIL, + "Error in copying slave: %s!", value->data); + goto out; + } ret = parse_slave_url (slv_url, &slave); if (ret == -1) { @@ -1775,14 +1792,14 @@ glusterd_store_slave_in_info (glusterd_volinfo_t *volinfo, char *slave, goto out; /* Given the slave volume uuid, check and get any existing slave */ - strncpy (slave1.slave_voluuid, slave_voluuid, GF_UUID_BUF_SIZE); + memcpy (slave1.slave_voluuid, slave_voluuid, GF_UUID_BUF_SIZE); ret = dict_foreach (volinfo->gsync_slaves, _get_slave_idx_slave_voluuid, &slave1); if (ret == 0) { /* New slave */ dict_foreach (volinfo->gsync_slaves, _get_max_gsync_slave_num, &maxslv); - snprintf (key, 512, "slave%d", maxslv + 1); + snprintf (key, sizeof (key), "slave%d", maxslv + 1); ret = dict_set_dynstr (volinfo->gsync_slaves, key, value); if (ret) { @@ -1790,7 +1807,7 @@ glusterd_store_slave_in_info (glusterd_volinfo_t *volinfo, char *slave, goto out; } } else if (ret == -1) { /* Existing slave */ - snprintf (key, 512, "slave%d", slave1.old_slvidx); + snprintf (key, sizeof (key), "slave%d", slave1.old_slvidx); gf_msg_debug (this->name, 0, "Replacing key:%s with new value" ":%s", key, value); @@ -1855,8 +1872,14 @@ glusterd_op_verify_gsync_start_options (glusterd_volinfo_t *volinfo, /* check session directory as statefile may not present * during upgrade */ - strncpy (statefiledir, statefile, sizeof(statefiledir)); - statefiledir[sizeof(statefiledir) - 1] = 0; + if (snprintf (statefiledir, sizeof (statefiledir), "%s", statefile) >= + sizeof (statefiledir)) { + snprintf (msg, sizeof (msg), "statefiledir truncated"); + gf_msg (this->name, GF_LOG_ERROR, errno, GD_MSG_FILE_OP_FAILED, + "%s", msg); + *op_errstr = gf_strdup (msg); + goto out; + } statedir = dirname (statefiledir); ret = sys_lstat (statedir, &stbuf); @@ -2000,7 +2023,7 @@ is_geo_rep_active (glusterd_volinfo_t *volinfo, char *slave, GD_MSG_STAT_FILE_READ_FAILED, "Unable to read the status file for %s(master), " "%s(slave)", master, slave); - strncpy (monitor_status, "defunct", sizeof (monitor_status)); + snprintf (monitor_status, sizeof (monitor_status), "defunct"); } if ((!strcmp(monitor_status, "Stopped")) || @@ -3316,8 +3339,12 @@ glusterd_op_stage_gsync_create (dict_t *dict, char **op_errstr) goto out; } - strncpy (statefiledir, statefile, sizeof(statefiledir)); - statefiledir[sizeof(statefiledir) - 1] = 0; + if (snprintf (statefiledir, sizeof (statefiledir), "%s", statefile) >= + sizeof (statefiledir)) { + snprintf (errmsg, sizeof (errmsg), + "Failed copying statefiledir"); + goto out; + } statedir = dirname (statefiledir); ret = sys_lstat (statedir, &stbuf); @@ -3650,8 +3677,13 @@ glusterd_op_stage_gsync_set (dict_t *dict, char **op_errstr) /* check session directory as statefile may not present * during upgrade */ - strncpy (statefiledir, statefile, sizeof(statefiledir)); - statefiledir[sizeof(statefiledir) - 1] = 0; + if (snprintf (statefiledir , sizeof (statefiledir), "%s", + statefile) >= sizeof (statefiledir)) { + snprintf (errmsg, sizeof (errmsg), + "Failed copying statefiledir"); + ret = -1; + goto out; + } statedir = dirname (statefiledir); ret = sys_lstat (statedir, &stbuf); @@ -4625,7 +4657,7 @@ fetch_data: "Unable to read the status file for %s(master), " "%s(slave) statefile: %s", master, slave, statefile); - strncpy (monitor_status, "defunct", sizeof (monitor_status)); + snprintf (monitor_status, sizeof (monitor_status), "defunct"); } ret = dict_get_int32 (dict, "gsync-count", &gsync_count); diff --git a/xlators/mgmt/glusterd/src/glusterd-handshake.c b/xlators/mgmt/glusterd/src/glusterd-handshake.c index ce0304a3d73..6b5940ce565 100644 --- a/xlators/mgmt/glusterd/src/glusterd-handshake.c +++ b/xlators/mgmt/glusterd/src/glusterd-handshake.c @@ -458,7 +458,9 @@ gotvolinfo: ret = sys_stat (path, &stbuf); if ((ret == -1) && (errno == ENOENT)) { - strncpy (dup_volid, volid_ptr, (PATH_MAX - 1)); + if (snprintf (dup_volid, PATH_MAX, "%s", volid_ptr) + >= PATH_MAX) + goto out; if (!strchr (dup_volid, '.')) { switch (volinfo->transport_type) { case GF_TRANSPORT_TCP: @@ -668,8 +670,16 @@ glusterd_create_missed_snap (glusterd_missed_snap_info *missed_snapinfo, ret = -1; goto out; } - strncpy (brickinfo->device_path, device, - sizeof(brickinfo->device_path) - 1); + if (snprintf (brickinfo->device_path , + sizeof (brickinfo->device_path), "%s", device) >= + sizeof (brickinfo->device_path)) { + gf_msg (this->name, GF_LOG_ERROR, ENXIO, + GD_MSG_SNAP_DEVICE_NAME_GET_FAIL, + "cannot copy the device_path " + "(device_path: %s)", brickinfo->device_path); + ret = -1; + goto out; + } /* Update the backend file-system type of snap brick in * snap volinfo. */ @@ -914,9 +924,19 @@ __server_getspec (rpcsvc_request_t *req) * support nfs style mount parameters for native gluster mount */ if (volume[0] == '/') - strncpy (peerinfo->volname, &volume[1], strlen(&volume[1])); + ret = snprintf (peerinfo->volname, sizeof (peerinfo->volname), + "%s", &volume[1]); else - strncpy (peerinfo->volname, volume, strlen(volume)); + ret = snprintf (peerinfo->volname, sizeof (peerinfo->volname), + "%s", volume); + if (ret < 0 || ret >= sizeof (peerinfo->volname)) { + gf_msg (this->name, GF_LOG_ERROR, 0, + GD_MSG_VOLINFO_GET_FAIL, + "peerinfo->volname %s truncated or error occured: " + "(ret: %d)", peerinfo->volname, ret); + ret = -1; + goto fail; + } ret = glusterd_get_args_from_dict (&args, peerinfo, &brick_name); if (ret) { diff --git a/xlators/mgmt/glusterd/src/glusterd-sm.c b/xlators/mgmt/glusterd/src/glusterd-sm.c index 0a52cc92a58..59a2f635106 100644 --- a/xlators/mgmt/glusterd/src/glusterd-sm.c +++ b/xlators/mgmt/glusterd/src/glusterd-sm.c @@ -1017,9 +1017,14 @@ glusterd_ac_handle_friend_add_req (glusterd_friend_sm_event_t *event, void *ctx) if (ret || !hostname) { gf_msg_debug (this->name, 0, "Unable to fetch local hostname from peer"); - } else - strncpy (local_node_hostname, hostname, - sizeof(local_node_hostname)); + } else if (snprintf (local_node_hostname, + sizeof (local_node_hostname), "%s", hostname) >= + sizeof (local_node_hostname)) { + gf_msg_debug (this->name, 0, + "local_node_hostname truncated"); + ret = -1; + goto out; + } glusterd_friend_sm_inject_event (new_event); new_event = NULL; diff --git a/xlators/mgmt/glusterd/src/glusterd-store.c b/xlators/mgmt/glusterd/src/glusterd-store.c index 1f43b7fa82d..343f0ba4883 100644 --- a/xlators/mgmt/glusterd/src/glusterd-store.c +++ b/xlators/mgmt/glusterd/src/glusterd-store.c @@ -193,7 +193,7 @@ glusterd_store_is_valid_brickpath (char *volname, char *brick) bpath_len = strlen (brickinfo->path); if (brickinfo->path[bpath_len - 1] != '/') { - if (strlen (brickinfo->path) >= PATH_MAX) { + if (bpath_len >= PATH_MAX) { ret = 0; goto out; } @@ -201,7 +201,7 @@ glusterd_store_is_valid_brickpath (char *volname, char *brick) /* Path has a trailing "/" which should not be considered in * length check validation */ - if (strlen (brickinfo->path) >= PATH_MAX + 1) { + if (bpath_len >= PATH_MAX + 1) { ret = 0; goto out; } @@ -2423,7 +2423,7 @@ glusterd_store_retrieve_snapd (glusterd_volinfo_t *volinfo) while (!ret) { if (!strncmp (key, GLUSTERD_STORE_KEY_SNAPD_PORT, - strlen (GLUSTERD_STORE_KEY_SNAPD_PORT))) { + SLEN (GLUSTERD_STORE_KEY_SNAPD_PORT))) { volinfo->snapd.port = atoi (value); } @@ -2520,18 +2520,43 @@ glusterd_store_retrieve_bricks (glusterd_volinfo_t *volinfo) } while (!ret) { if (!strncmp (key, GLUSTERD_STORE_KEY_BRICK_HOSTNAME, - strlen (GLUSTERD_STORE_KEY_BRICK_HOSTNAME))) { - strncpy (brickinfo->hostname, value, 1024); + SLEN (GLUSTERD_STORE_KEY_BRICK_HOSTNAME))) { + if (snprintf (brickinfo->hostname, + sizeof (brickinfo->hostname), + "%s", value) >= + sizeof (brickinfo->hostname)) { + gf_msg ("glusterd", GF_LOG_ERROR, op_errno, + GD_MSG_PARSE_BRICKINFO_FAIL, + "brick hostname truncated: %s", + brickinfo->hostname); + goto out; + } } else if (!strncmp (key, GLUSTERD_STORE_KEY_BRICK_PATH, - strlen (GLUSTERD_STORE_KEY_BRICK_PATH))) { - strncpy (brickinfo->path, value, - sizeof (brickinfo->path)); + SLEN (GLUSTERD_STORE_KEY_BRICK_PATH))) { + if (snprintf (brickinfo->path, + sizeof (brickinfo->path), + "%s", value) >= + sizeof (brickinfo->path)) { + gf_msg ("glusterd", GF_LOG_ERROR, op_errno, + GD_MSG_PARSE_BRICKINFO_FAIL, + "brick path truncated: %s", + brickinfo->path); + goto out; + } } else if (!strncmp (key, GLUSTERD_STORE_KEY_BRICK_REAL_PATH, - strlen (GLUSTERD_STORE_KEY_BRICK_REAL_PATH))) { - strncpy (brickinfo->real_path, value, - sizeof (brickinfo->real_path)); + SLEN (GLUSTERD_STORE_KEY_BRICK_REAL_PATH))) { + if (snprintf (brickinfo->real_path, + sizeof (brickinfo->real_path), + "%s", value) >= + sizeof (brickinfo->real_path)) { + gf_msg ("glusterd", GF_LOG_ERROR, op_errno, + GD_MSG_PARSE_BRICKINFO_FAIL, + "real_path truncated: %s", + brickinfo->real_path); + goto out; + } } else if (!strncmp (key, GLUSTERD_STORE_KEY_BRICK_PORT, - strlen (GLUSTERD_STORE_KEY_BRICK_PORT))) { + SLEN (GLUSTERD_STORE_KEY_BRICK_PORT))) { gf_string2int (value, &brickinfo->port); if (brickinfo->port < priv->base_port) { @@ -2547,7 +2572,7 @@ glusterd_store_retrieve_bricks (glusterd_volinfo_t *volinfo) brickinfo->port + 1; } } else if (!strncmp (key, GLUSTERD_STORE_KEY_BRICK_RDMA_PORT, - strlen (GLUSTERD_STORE_KEY_BRICK_RDMA_PORT))) { + SLEN (GLUSTERD_STORE_KEY_BRICK_RDMA_PORT))) { gf_string2int (value, &brickinfo->rdma_port); if (brickinfo->rdma_port < priv->base_port) { @@ -2565,7 +2590,7 @@ glusterd_store_retrieve_bricks (glusterd_volinfo_t *volinfo) } } else if (!strncmp (key, GLUSTERD_STORE_KEY_BRICK_DECOMMISSIONED, - strlen (GLUSTERD_STORE_KEY_BRICK_DECOMMISSIONED))) { + SLEN (GLUSTERD_STORE_KEY_BRICK_DECOMMISSIONED))) { ret = gf_string2int (value, &brickinfo->decommissioned); if (ret == -1) { @@ -2577,15 +2602,31 @@ glusterd_store_retrieve_bricks (glusterd_volinfo_t *volinfo) } } else if (!strncmp (key, GLUSTERD_STORE_KEY_BRICK_DEVICE_PATH, - strlen (GLUSTERD_STORE_KEY_BRICK_DEVICE_PATH))) { - strncpy (brickinfo->device_path, value, - sizeof (brickinfo->device_path)); + SLEN (GLUSTERD_STORE_KEY_BRICK_DEVICE_PATH))) { + if (snprintf (brickinfo->device_path, + sizeof (brickinfo->device_path), + "%s", value) >= + sizeof (brickinfo->device_path)) { + gf_msg ("glusterd", GF_LOG_ERROR, op_errno, + GD_MSG_PARSE_BRICKINFO_FAIL, + "device_path truncated: %s", + brickinfo->device_path); + goto out; + } } else if (!strncmp (key, GLUSTERD_STORE_KEY_BRICK_MOUNT_DIR, - strlen (GLUSTERD_STORE_KEY_BRICK_MOUNT_DIR))) { - strncpy (brickinfo->mount_dir, value, - sizeof (brickinfo->mount_dir)); + SLEN (GLUSTERD_STORE_KEY_BRICK_MOUNT_DIR))) { + if (snprintf (brickinfo->mount_dir, + sizeof (brickinfo->mount_dir), + "%s", value) >= + sizeof (brickinfo->mount_dir)) { + gf_msg ("glusterd", GF_LOG_ERROR, op_errno, + GD_MSG_PARSE_BRICKINFO_FAIL, + "mount_dir truncated: %s", + brickinfo->mount_dir); + goto out; + } } else if (!strncmp (key, GLUSTERD_STORE_KEY_BRICK_SNAP_STATUS, - strlen (GLUSTERD_STORE_KEY_BRICK_SNAP_STATUS))) { + SLEN (GLUSTERD_STORE_KEY_BRICK_SNAP_STATUS))) { ret = gf_string2int (value, &brickinfo->snap_status); if (ret == -1) { @@ -2594,27 +2635,59 @@ glusterd_store_retrieve_bricks (glusterd_volinfo_t *volinfo) GD_MSG_INCOMPATIBLE_VALUE, "Failed to convert " "string to integer"); - } + } } else if (!strncmp (key, GLUSTERD_STORE_KEY_BRICK_FSTYPE, - strlen (GLUSTERD_STORE_KEY_BRICK_FSTYPE))) { - strncpy (brickinfo->fstype, value, - sizeof (brickinfo->fstype)); + SLEN (GLUSTERD_STORE_KEY_BRICK_FSTYPE))) { + if (snprintf (brickinfo->fstype, + sizeof (brickinfo->fstype), + "%s", value) >= + sizeof (brickinfo->fstype)) { + gf_msg ("glusterd", GF_LOG_ERROR, op_errno, + GD_MSG_PARSE_BRICKINFO_FAIL, + "fstype truncated: %s", + brickinfo->fstype); + goto out; + } } else if (!strncmp (key, GLUSTERD_STORE_KEY_BRICK_MNTOPTS, - strlen (GLUSTERD_STORE_KEY_BRICK_MNTOPTS))) { - strncpy (brickinfo->mnt_opts, value, - sizeof (brickinfo->mnt_opts)); + SLEN (GLUSTERD_STORE_KEY_BRICK_MNTOPTS))) { + if (snprintf (brickinfo->mnt_opts, + sizeof (brickinfo->mnt_opts), + "%s", value) >= + sizeof (brickinfo->mnt_opts)) { + gf_msg ("glusterd", GF_LOG_ERROR, op_errno, + GD_MSG_PARSE_BRICKINFO_FAIL, + "mnt_opts truncated: %s", + brickinfo->mnt_opts); + goto out; + } } else if (!strncmp (key, GLUSTERD_STORE_KEY_BRICK_VGNAME, - strlen (GLUSTERD_STORE_KEY_BRICK_VGNAME))) { - strncpy (brickinfo->vg, value, - sizeof (brickinfo->vg)); + SLEN (GLUSTERD_STORE_KEY_BRICK_VGNAME))) { + if (snprintf (brickinfo->vg, + sizeof (brickinfo->vg), "%s", + value) >= + sizeof (brickinfo->vg)) { + gf_msg ("glusterd", GF_LOG_ERROR, op_errno, + GD_MSG_PARSE_BRICKINFO_FAIL, + "brickinfo->vg truncated: %s", + brickinfo->vg); + goto out; + } } else if (!strcmp(key, GLUSTERD_STORE_KEY_BRICK_ID)) { - strncpy (brickinfo->brick_id, value, - sizeof (brickinfo->brick_id)); + if (snprintf (brickinfo->brick_id, + sizeof (brickinfo->brick_id), + "%s", value) >= + sizeof (brickinfo->brick_id)) { + gf_msg ("glusterd", GF_LOG_ERROR, op_errno, + GD_MSG_PARSE_BRICKINFO_FAIL, + "brick_id truncated: %s", + brickinfo->brick_id); + goto out; + } } else if (!strncmp (key, GLUSTERD_STORE_KEY_BRICK_FSID, - strlen (GLUSTERD_STORE_KEY_BRICK_FSID))) { + SLEN (GLUSTERD_STORE_KEY_BRICK_FSID))) { ret = gf_string2uint64 (value, &brickinfo->statfs_fsid); if (ret) { @@ -2765,69 +2838,69 @@ glusterd_store_retrieve_node_state (glusterd_volinfo_t *volinfo) while (ret == 0) { if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_DEFRAG, - strlen (GLUSTERD_STORE_KEY_VOL_DEFRAG))) { + SLEN (GLUSTERD_STORE_KEY_VOL_DEFRAG))) { volinfo->rebal.defrag_cmd = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_DEFRAG_STATUS, - strlen (GLUSTERD_STORE_KEY_VOL_DEFRAG_STATUS))) { + SLEN (GLUSTERD_STORE_KEY_VOL_DEFRAG_STATUS))) { volinfo->rebal.defrag_status = atoi (value); } else if (!strncmp (key, GF_REBALANCE_TID_KEY, - strlen (GF_REBALANCE_TID_KEY))) { + SLEN (GF_REBALANCE_TID_KEY))) { gf_uuid_parse (value, volinfo->rebal.rebalance_id); } else if (!strncmp (key, GLUSTERD_STORE_KEY_DEFRAG_OP, - strlen (GLUSTERD_STORE_KEY_DEFRAG_OP))) { + SLEN (GLUSTERD_STORE_KEY_DEFRAG_OP))) { volinfo->rebal.op = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_DEFRAG_REB_FILES, - strlen (GLUSTERD_STORE_KEY_VOL_DEFRAG_REB_FILES))) { + SLEN (GLUSTERD_STORE_KEY_VOL_DEFRAG_REB_FILES))) { volinfo->rebal.rebalance_files = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_DEFRAG_SIZE, - strlen (GLUSTERD_STORE_KEY_VOL_DEFRAG_SIZE))) { + SLEN (GLUSTERD_STORE_KEY_VOL_DEFRAG_SIZE))) { volinfo->rebal.rebalance_data = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_DEFRAG_SCANNED, - strlen (GLUSTERD_STORE_KEY_VOL_DEFRAG_SCANNED))) { + SLEN (GLUSTERD_STORE_KEY_VOL_DEFRAG_SCANNED))) { volinfo->rebal.lookedup_files = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_DEFRAG_FAILURES, - strlen (GLUSTERD_STORE_KEY_VOL_DEFRAG_FAILURES))) { + SLEN (GLUSTERD_STORE_KEY_VOL_DEFRAG_FAILURES))) { volinfo->rebal.rebalance_failures = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_DEFRAG_SKIPPED, - strlen (GLUSTERD_STORE_KEY_VOL_DEFRAG_SKIPPED))) { + SLEN (GLUSTERD_STORE_KEY_VOL_DEFRAG_SKIPPED))) { volinfo->rebal.skipped_files = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_DEFRAG_RUN_TIME, - strlen (GLUSTERD_STORE_KEY_VOL_DEFRAG_RUN_TIME))) { + SLEN (GLUSTERD_STORE_KEY_VOL_DEFRAG_RUN_TIME))) { volinfo->rebal.rebalance_time = atoi (value); /* if none of the above keys match then its related to tier * so we get the values and store it on volinfo->tier */ } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_TIER_STATUS, - strlen (GLUSTERD_STORE_KEY_VOL_TIER_STATUS))) { + SLEN (GLUSTERD_STORE_KEY_VOL_TIER_STATUS))) { volinfo->tier.defrag_status = atoi (value); } else if (!strncmp (key, GF_TIER_TID_KEY, - strlen (GF_TIER_TID_KEY))) { + SLEN (GF_TIER_TID_KEY))) { gf_uuid_parse (value, volinfo->tier.rebalance_id); } else if (!strncmp (key, GLUSTERD_STORE_KEY_TIER_DETACH_OP, - strlen (GLUSTERD_STORE_KEY_TIER_DETACH_OP))) { + SLEN (GLUSTERD_STORE_KEY_TIER_DETACH_OP))) { volinfo->tier.op = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_MIGRATED_FILES, - strlen (GLUSTERD_STORE_KEY_VOL_MIGRATED_FILES))) { + SLEN (GLUSTERD_STORE_KEY_VOL_MIGRATED_FILES))) { volinfo->tier.rebalance_files = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_MIGRATED_SIZE, - strlen (GLUSTERD_STORE_KEY_VOL_MIGRATED_SIZE))) { + SLEN (GLUSTERD_STORE_KEY_VOL_MIGRATED_SIZE))) { volinfo->tier.rebalance_data = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_MIGRATIONS_SCANNED, - strlen (GLUSTERD_STORE_KEY_VOL_MIGRATIONS_SCANNED))) { + SLEN (GLUSTERD_STORE_KEY_VOL_MIGRATIONS_SCANNED))) { volinfo->tier.lookedup_files = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_MIGRATIONS_FAILURES, - strlen (GLUSTERD_STORE_KEY_VOL_MIGRATIONS_FAILURES))) { + SLEN (GLUSTERD_STORE_KEY_VOL_MIGRATIONS_FAILURES))) { volinfo->tier.rebalance_failures = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_MIGRATIONS_SKIPPED, - strlen (GLUSTERD_STORE_KEY_VOL_MIGRATIONS_SKIPPED))) { + SLEN (GLUSTERD_STORE_KEY_VOL_MIGRATIONS_SKIPPED))) { volinfo->tier.skipped_files = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_MIGRATION_RUN_TIME, - strlen (GLUSTERD_STORE_KEY_VOL_MIGRATION_RUN_TIME))) { + SLEN (GLUSTERD_STORE_KEY_VOL_MIGRATION_RUN_TIME))) { volinfo->tier.rebalance_time = atoi (value); } else { if (!tmp_dict) { @@ -2952,42 +3025,42 @@ glusterd_store_update_volinfo (glusterd_volinfo_t *volinfo) while (!ret) { gf_msg_debug (this->name, 0, "key = %s value = %s", key, value); if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_TYPE, - strlen (GLUSTERD_STORE_KEY_VOL_TYPE))) { + SLEN (GLUSTERD_STORE_KEY_VOL_TYPE))) { volinfo->type = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_COUNT, - strlen (GLUSTERD_STORE_KEY_VOL_COUNT))) { + SLEN (GLUSTERD_STORE_KEY_VOL_COUNT))) { volinfo->brick_count = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_STATUS, - strlen (GLUSTERD_STORE_KEY_VOL_STATUS))) { + SLEN (GLUSTERD_STORE_KEY_VOL_STATUS))) { volinfo->status = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_VERSION, - strlen (GLUSTERD_STORE_KEY_VOL_VERSION))) { + SLEN (GLUSTERD_STORE_KEY_VOL_VERSION))) { volinfo->version = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_PORT, - strlen (GLUSTERD_STORE_KEY_VOL_PORT))) { + SLEN (GLUSTERD_STORE_KEY_VOL_PORT))) { volinfo->port = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_SUB_COUNT, - strlen (GLUSTERD_STORE_KEY_VOL_SUB_COUNT))) { + SLEN (GLUSTERD_STORE_KEY_VOL_SUB_COUNT))) { volinfo->sub_count = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_STRIPE_CNT, - strlen (GLUSTERD_STORE_KEY_VOL_STRIPE_CNT))) { + SLEN (GLUSTERD_STORE_KEY_VOL_STRIPE_CNT))) { volinfo->stripe_count = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_REPLICA_CNT, - strlen (GLUSTERD_STORE_KEY_VOL_REPLICA_CNT))) { + SLEN (GLUSTERD_STORE_KEY_VOL_REPLICA_CNT))) { volinfo->replica_count = atoi (value); } else if (!strcmp (key, GLUSTERD_STORE_KEY_VOL_ARBITER_CNT)) { volinfo->arbiter_count = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_DISPERSE_CNT, - strlen (GLUSTERD_STORE_KEY_VOL_DISPERSE_CNT))) { + SLEN (GLUSTERD_STORE_KEY_VOL_DISPERSE_CNT))) { volinfo->disperse_count = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_REDUNDANCY_CNT, - strlen (GLUSTERD_STORE_KEY_VOL_REDUNDANCY_CNT))) { + SLEN (GLUSTERD_STORE_KEY_VOL_REDUNDANCY_CNT))) { volinfo->redundancy_count = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_TRANSPORT, - strlen (GLUSTERD_STORE_KEY_VOL_TRANSPORT))) { + SLEN (GLUSTERD_STORE_KEY_VOL_TRANSPORT))) { volinfo->transport_type = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_ID, - strlen (GLUSTERD_STORE_KEY_VOL_ID))) { + SLEN (GLUSTERD_STORE_KEY_VOL_ID))) { ret = gf_uuid_parse (value, volinfo->volume_id); if (ret) gf_msg (this->name, GF_LOG_WARNING, 0, @@ -2995,12 +3068,12 @@ glusterd_store_update_volinfo (glusterd_volinfo_t *volinfo) "failed to parse uuid"); } else if (!strncmp (key, GLUSTERD_STORE_KEY_USERNAME, - strlen (GLUSTERD_STORE_KEY_USERNAME))) { + SLEN (GLUSTERD_STORE_KEY_USERNAME))) { glusterd_auth_set_username (volinfo, value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_PASSWORD, - strlen (GLUSTERD_STORE_KEY_PASSWORD))) { + SLEN (GLUSTERD_STORE_KEY_PASSWORD))) { glusterd_auth_set_password (volinfo, value); @@ -3017,30 +3090,38 @@ glusterd_store_update_volinfo (glusterd_volinfo_t *volinfo) " slave:key=%s,value:%s", key, value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_OP_VERSION, - strlen (GLUSTERD_STORE_KEY_VOL_OP_VERSION))) { + SLEN (GLUSTERD_STORE_KEY_VOL_OP_VERSION))) { volinfo->op_version = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_CLIENT_OP_VERSION, - strlen (GLUSTERD_STORE_KEY_VOL_CLIENT_OP_VERSION))) { + SLEN (GLUSTERD_STORE_KEY_VOL_CLIENT_OP_VERSION))) { volinfo->client_op_version = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_CAPS, - strlen (GLUSTERD_STORE_KEY_VOL_CAPS))) { + SLEN (GLUSTERD_STORE_KEY_VOL_CAPS))) { volinfo->caps = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_SNAP_MAX_HARD_LIMIT, - strlen (GLUSTERD_STORE_KEY_SNAP_MAX_HARD_LIMIT))) { + SLEN (GLUSTERD_STORE_KEY_SNAP_MAX_HARD_LIMIT))) { volinfo->snap_max_hard_limit = (uint64_t) atoll (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_RESTORED_SNAP, - strlen (GLUSTERD_STORE_KEY_VOL_RESTORED_SNAP))) { + SLEN (GLUSTERD_STORE_KEY_VOL_RESTORED_SNAP))) { ret = gf_uuid_parse (value, volinfo->restored_from_snap); if (ret) gf_msg (this->name, GF_LOG_WARNING, 0, GD_MSG_UUID_PARSE_FAIL, "failed to parse restored snap's uuid"); } else if (!strncmp (key, GLUSTERD_STORE_KEY_PARENT_VOLNAME, - strlen (GLUSTERD_STORE_KEY_PARENT_VOLNAME))) { - strncpy (volinfo->parent_volname, value, - sizeof(volinfo->parent_volname) - 1); + SLEN (GLUSTERD_STORE_KEY_PARENT_VOLNAME))) { + if (snprintf (volinfo->parent_volname, + sizeof(volinfo->parent_volname), "%s", + value) >= + sizeof(volinfo->parent_volname)) { + gf_msg ("glusterd", GF_LOG_ERROR, op_errno, + GD_MSG_PARSE_BRICKINFO_FAIL, + "parent_volname truncated: %s", + volinfo->parent_volname); + goto out; + } } else if (!strncmp (key, GF_TIER_ENABLED, - strlen (GF_TIER_ENABLED))) { + SLEN (GF_TIER_ENABLED))) { volinfo->is_tier_enabled = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_COLD_COUNT, strlen (key))) { @@ -3068,7 +3149,7 @@ glusterd_store_update_volinfo (glusterd_volinfo_t *volinfo) strlen (key))) { volinfo->tier_info.cold_type = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_VOL_QUOTA_VERSION, - strlen (GLUSTERD_STORE_KEY_VOL_QUOTA_VERSION))) { + SLEN (GLUSTERD_STORE_KEY_VOL_QUOTA_VERSION))) { volinfo->quota_xattr_version = atoi (value); } else { @@ -3223,7 +3304,9 @@ glusterd_store_retrieve_volume (char *volname, glusterd_snap_t *snap) if (ret) goto out; - strncpy (volinfo->volname, volname, GD_VOLUME_NAME_MAX); + if (snprintf (volinfo->volname, NAME_MAX + 1, "%s", volname) >= + NAME_MAX + 1) + goto out; volinfo->snapshot = snap; if (snap) volinfo->is_snap_volume = _gf_true; @@ -3797,23 +3880,23 @@ glusterd_store_update_snap (glusterd_snap_t *snap) key, value); if (!strncmp (key, GLUSTERD_STORE_KEY_SNAP_ID, - strlen (GLUSTERD_STORE_KEY_SNAP_ID))) { + SLEN (GLUSTERD_STORE_KEY_SNAP_ID))) { ret = gf_uuid_parse (value, snap->snap_id); if (ret) gf_msg (this->name, GF_LOG_WARNING, 0, GD_MSG_UUID_PARSE_FAIL, "Failed to parse uuid"); } else if (!strncmp (key, GLUSTERD_STORE_KEY_SNAP_RESTORED, - strlen (GLUSTERD_STORE_KEY_SNAP_RESTORED))) { + SLEN (GLUSTERD_STORE_KEY_SNAP_RESTORED))) { snap->snap_restored = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_SNAP_STATUS, - strlen (GLUSTERD_STORE_KEY_SNAP_STATUS))) { + SLEN (GLUSTERD_STORE_KEY_SNAP_STATUS))) { snap->snap_status = atoi (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_SNAP_DESC, - strlen (GLUSTERD_STORE_KEY_SNAP_DESC))) { + SLEN (GLUSTERD_STORE_KEY_SNAP_DESC))) { snap->description = gf_strdup (value); } else if (!strncmp (key, GLUSTERD_STORE_KEY_SNAP_TIMESTAMP, - strlen (GLUSTERD_STORE_KEY_SNAP_TIMESTAMP))) { + SLEN (GLUSTERD_STORE_KEY_SNAP_TIMESTAMP))) { snap->time_stamp = atoi (value); } @@ -3861,7 +3944,9 @@ glusterd_store_retrieve_snap (char *snapname) goto out; } - strncpy (snap->snapname, snapname, strlen(snapname)); + if (snprintf (snap->snapname, sizeof (snap->snapname), "%s", snapname) + >= sizeof (snap->snapname)) + goto out; ret = glusterd_store_update_snap (snap); if (ret) { gf_msg (this->name, GF_LOG_ERROR, 0, @@ -4527,16 +4612,16 @@ glusterd_store_retrieve_peers (xlator_t *this) while (!ret) { if (!strncmp (GLUSTERD_STORE_KEY_PEER_UUID, key, - strlen (GLUSTERD_STORE_KEY_PEER_UUID))) { + SLEN (GLUSTERD_STORE_KEY_PEER_UUID))) { if (value) gf_uuid_parse (value, peerinfo->uuid); } else if (!strncmp (GLUSTERD_STORE_KEY_PEER_STATE, key, - strlen (GLUSTERD_STORE_KEY_PEER_STATE))) { + SLEN (GLUSTERD_STORE_KEY_PEER_STATE))) { peerinfo->state.state = atoi (value); } else if (!strncmp (GLUSTERD_STORE_KEY_PEER_HOSTNAME, key, - strlen (GLUSTERD_STORE_KEY_PEER_HOSTNAME))) { + SLEN (GLUSTERD_STORE_KEY_PEER_HOSTNAME))) { ret = gd_add_address_to_peer (peerinfo, value); if (ret) { gf_msg (this->name, GF_LOG_ERROR, 0, @@ -4987,10 +5072,10 @@ glusterd_quota_conf_write_header (int fd) if (conf->op_version < GD_OP_VERSION_3_7_0) { - header_len = strlen (QUOTA_CONF_HEADER_1_1); + header_len = SLEN (QUOTA_CONF_HEADER_1_1); ret = gf_nwrite (fd, QUOTA_CONF_HEADER_1_1, header_len); } else { - header_len = strlen (QUOTA_CONF_HEADER); + header_len = SLEN (QUOTA_CONF_HEADER); ret = gf_nwrite (fd, QUOTA_CONF_HEADER, header_len); } diff --git a/xlators/mgmt/glusterd/src/glusterd-utils.c b/xlators/mgmt/glusterd/src/glusterd-utils.c index 1adf38faed1..a46530c532c 100644 --- a/xlators/mgmt/glusterd/src/glusterd-utils.c +++ b/xlators/mgmt/glusterd/src/glusterd-utils.c @@ -827,12 +827,16 @@ glusterd_create_sub_tier_volinfo (glusterd_volinfo_t *volinfo, (*dup_volinfo)->status = volinfo->status; (*dup_volinfo)->snapshot = volinfo->snapshot; + if (snprintf ((*dup_volinfo)->volname, + sizeof ((*dup_volinfo)->volname), "%s", new_volname) >= + sizeof ((*dup_volinfo)->volname)) { + ret = -1; + goto out; + } + memcpy (&(*dup_volinfo)->tier_info, &volinfo->tier_info, sizeof (volinfo->tier_info)); - strncpy ((*dup_volinfo)->volname, new_volname, - sizeof((*dup_volinfo)->volname)-1); - cds_list_for_each_entry (brickinfo, &volinfo->bricks, brick_list) { i++; @@ -1243,7 +1247,11 @@ glusterd_brickinfo_new_from_brick (char *brick, vg = strchr (path, '?'); /* ? is used as a delimiter for vg */ if (vg) { - strncpy (new_brickinfo->vg, vg + 1, PATH_MAX - 1); + if (snprintf (new_brickinfo->vg, PATH_MAX, "%s", + vg + 1) >= PATH_MAX) { + ret = -1; + goto out; + } *vg = '\0'; } new_brickinfo->caps = CAPS_BD; @@ -1251,9 +1259,18 @@ glusterd_brickinfo_new_from_brick (char *brick, ret = gf_canonicalize_path (path); if (ret) goto out; - gf_strncpy (new_brickinfo->hostname, hostname, - sizeof(new_brickinfo->hostname)); - gf_strncpy (new_brickinfo->path, path, sizeof(new_brickinfo->path)); + ret = snprintf (new_brickinfo->hostname, + sizeof (new_brickinfo->hostname), "%s", hostname); + if (ret < 0 || ret >= sizeof (new_brickinfo->hostname)) { + ret = -1; + goto out; + } + ret = snprintf (new_brickinfo->path, sizeof (new_brickinfo->path), + "%s", path); + if (ret < 0 || ret >= sizeof (new_brickinfo->path)) { + ret = -1; + goto out; + } if (construct_real_path) { ret = glusterd_hostname_to_uuid (new_brickinfo->hostname, @@ -1364,7 +1381,8 @@ glusterd_is_brickpath_available (uuid_t uuid, char *path) priv = THIS->private; - strncpy (tmp_path, path, PATH_MAX); + if (snprintf (tmp_path, PATH_MAX, "%s", path) >= PATH_MAX) + goto out; /* path may not yet exist */ if (!realpath (path, tmp_path)) { if (errno != ENOENT) { @@ -1902,8 +1920,10 @@ glusterd_set_brick_socket_filepath (glusterd_volinfo_t *volinfo, char sock_filepath[PATH_MAX] = ""; int32_t slen = 0; - expected_file_len = strlen (GLUSTERD_SOCK_DIR) + strlen ("/") + - SHA256_DIGEST_LENGTH*2 + strlen (".socket") + 1; + expected_file_len = SLEN (GLUSTERD_SOCK_DIR) + + SLEN ("/") + + SHA256_DIGEST_LENGTH*2 + + SLEN (".socket") + 1; GF_ASSERT (len >= expected_file_len); this = THIS; GF_ASSERT (this); @@ -3120,7 +3140,7 @@ glusterd_add_volume_to_dict (glusterd_volinfo_t *volinfo, goto out; } - snprintf (key, 256, "%s%d.rebalance", prefix, count); + snprintf (key, sizeof (key), "%s%d.rebalance", prefix, count); ret = dict_set_int32 (dict, key, volinfo->rebal.defrag_cmd); if (ret) goto out; @@ -3131,7 +3151,7 @@ glusterd_add_volume_to_dict (glusterd_volinfo_t *volinfo, ret = -1; goto out; } - snprintf (key, 256, "%s%d.rebalance-id", prefix, count); + snprintf (key, sizeof (key), "%s%d.rebalance-id", prefix, count); ret = dict_set_dynstr (dict, key, rebalance_id_str); if (ret) goto out; @@ -3724,9 +3744,18 @@ glusterd_import_new_brick (dict_t *peer_data, int32_t vol_count, if (ret) goto out; - strncpy (new_brickinfo->path, path, sizeof (new_brickinfo->path) - 1); - strncpy (new_brickinfo->hostname, hostname, - sizeof (new_brickinfo->hostname) - 1); + ret = snprintf (new_brickinfo->path, sizeof (new_brickinfo->path), + "%s", path); + if (ret < 0 || ret >= sizeof (new_brickinfo->path)) { + ret = -1; + goto out; + } + ret = snprintf (new_brickinfo->hostname, + sizeof (new_brickinfo->hostname), "%s", hostname); + if (ret < 0 || ret >= sizeof (new_brickinfo->hostname)) { + ret = -1; + goto out; + } new_brickinfo->decommissioned = decommissioned; if (brick_id) strcpy (new_brickinfo->brick_id, brick_id); @@ -4002,9 +4031,12 @@ glusterd_import_volinfo (dict_t *peer_data, int count, ret = glusterd_volinfo_new (&new_volinfo); if (ret) goto out; - strncpy (new_volinfo->volname, volname, - sizeof(new_volinfo->volname) - 1); - + ret = snprintf (new_volinfo->volname, + sizeof (new_volinfo->volname), "%s", volname); + if (ret < 0 || ret >= sizeof (new_volinfo->volname)) { + ret = -1; + goto out; + } snprintf (key, sizeof (key), "%s%d.type", prefix, count); ret = dict_get_int32 (peer_data, key, &new_volinfo->type); if (ret) { @@ -4015,10 +4047,15 @@ glusterd_import_volinfo (dict_t *peer_data, int count, snprintf (key, sizeof (key), "%s%d.parent_volname", prefix, count); ret = dict_get_str (peer_data, key, &parent_volname); - if (!ret) - strncpy (new_volinfo->parent_volname, parent_volname, - sizeof(new_volinfo->parent_volname)-1); - + if (!ret) { + ret = snprintf (new_volinfo->parent_volname, + sizeof(new_volinfo->parent_volname), "%s", + parent_volname); + if (ret < 0 || ret >= sizeof (new_volinfo->volname)) { + ret = -1; + goto out; + } + } snprintf (key, sizeof (key), "%s%d.brick_count", prefix, count); ret = dict_get_int32 (peer_data, key, &new_volinfo->brick_count); if (ret) { @@ -7680,7 +7717,9 @@ glusterd_is_path_in_use (char *path, gf_boolean_t *in_use, char **op_errstr) if (!path) goto out; - strncpy (dir, path, (sizeof (dir) - 1)); + if (snprintf (dir, PATH_MAX, "%s", path) >= PATH_MAX) + goto out; + curdir = dir; do { for (i = 0; !used && keys[i]; i++) { @@ -9997,13 +10036,11 @@ glusterd_volume_status_add_peer_rsp (dict_t *this, char *key, data_t *value, if (index > rsp_ctx->brick_index_max) { len = snprintf (new_key, sizeof (new_key), "brick%d.%s", index + rsp_ctx->other_count, brick_key); - if ((len < 0) || (len >= sizeof(new_key))) { - goto out; - } } else { - strncpy (new_key, key, sizeof (new_key)); - new_key[sizeof (new_key) - 1] = 0; + len = snprintf (new_key, sizeof (new_key), "%s", key); } + if (len < 0 || len >= sizeof(new_key)) + goto out; ret = dict_set (rsp_ctx->dict, new_key, new_value); out: @@ -10499,21 +10536,21 @@ glusterd_volume_bitrot_scrub_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) gf_msg (this->name, GF_LOG_ERROR, 0, GD_MSG_DICT_SET_FAILED, "Failed to set count in dictonary"); - snprintf (key, 256, "node-uuid-%d", src_count); + snprintf (key, sizeof (key), "node-uuid-%d", src_count); ret = dict_get_str (rsp_dict, key, &node_uuid); if (!ret) { node_uuid_str = gf_strdup (node_uuid); - snprintf (key, 256, "node-uuid-%d", src_count+dst_count); + snprintf (key, sizeof (key), "node-uuid-%d", src_count+dst_count); ret = dict_set_dynstr (aggr, key, node_uuid_str); if (ret) { gf_msg_debug (this->name, 0, "failed to set node-uuid"); } } - snprintf (key, 256, "scrub-running-%d", src_count); + snprintf (key, sizeof (key), "scrub-running-%d", src_count); ret = dict_get_int8 (rsp_dict, key, &scrub_running); if (!ret) { - snprintf (key, 256, "scrub-running-%d", src_count+dst_count); + snprintf (key, sizeof (key), "scrub-running-%d", src_count+dst_count); ret = dict_set_int8 (aggr, key, scrub_running); if (ret) { gf_msg_debug (this->name, 0, "Failed to set " @@ -10521,10 +10558,10 @@ glusterd_volume_bitrot_scrub_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "scrubbed-files-%d", src_count); + snprintf (key, sizeof (key), "scrubbed-files-%d", src_count); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "scrubbed-files-%d", src_count+dst_count); + snprintf (key, sizeof (key), "scrubbed-files-%d", src_count+dst_count); ret = dict_set_uint64 (aggr, key, value); if (ret) { gf_msg_debug (this->name, 0, "Failed to set " @@ -10532,10 +10569,10 @@ glusterd_volume_bitrot_scrub_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "unsigned-files-%d", src_count); + snprintf (key, sizeof (key), "unsigned-files-%d", src_count); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "unsigned-files-%d", src_count+dst_count); + snprintf (key, sizeof (key), "unsigned-files-%d", src_count+dst_count); ret = dict_set_uint64 (aggr, key, value); if (ret) { gf_msg_debug (this->name, 0, "Failed to set " @@ -10543,11 +10580,11 @@ glusterd_volume_bitrot_scrub_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "last-scrub-time-%d", src_count); + snprintf (key, sizeof (key), "last-scrub-time-%d", src_count); ret = dict_get_str (rsp_dict, key, &last_scrub_time); if (!ret) { scrub_time = gf_strdup (last_scrub_time); - snprintf (key, 256, "last-scrub-time-%d", src_count+dst_count); + snprintf (key, sizeof (key), "last-scrub-time-%d", src_count+dst_count); ret = dict_set_dynstr (aggr, key, scrub_time); if (ret) { gf_msg_debug (this->name, 0, "Failed to set " @@ -10555,10 +10592,10 @@ glusterd_volume_bitrot_scrub_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "scrub-duration-%d", src_count); + snprintf (key, sizeof (key), "scrub-duration-%d", src_count); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "scrub-duration-%d", src_count+dst_count); + snprintf (key, sizeof (key), "scrub-duration-%d", src_count+dst_count); ret = dict_set_uint64 (aggr, key, value); if (ret) { gf_msg_debug (this->name, 0, "Failed to set " @@ -10566,10 +10603,10 @@ glusterd_volume_bitrot_scrub_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "error-count-%d", src_count); + snprintf (key, sizeof (key), "error-count-%d", src_count); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "error-count-%d", src_count+dst_count); + snprintf (key, sizeof (key), "error-count-%d", src_count+dst_count); ret = dict_set_uint64 (aggr, key, value); if (ret) { gf_msg_debug (this->name, 0, "Failed to set error " @@ -10578,10 +10615,10 @@ glusterd_volume_bitrot_scrub_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) /* Storing all the bad files in the dictionary */ for (j = 0; j < value; j++) { - snprintf (key, 256, "quarantine-%d-%d", j, src_count); + snprintf (key, sizeof (key), "quarantine-%d-%d", j, src_count); ret = dict_get_str (rsp_dict, key, &bad_gfid_str); if (!ret) { - snprintf (key, 256, "quarantine-%d-%d", j, + snprintf (key, sizeof (key), "quarantine-%d-%d", j, src_count+dst_count); ret = dict_set_dynstr_with_alloc (aggr, key, bad_gfid_str); @@ -10721,7 +10758,7 @@ glusterd_bitrot_volume_node_rsp (dict_t *aggr, dict_t *rsp_dict) snprintf (buf, 1024, "%s", uuid_utoa (MY_UUID)); - snprintf (key, 256, "node-uuid-%d", i); + snprintf (key, sizeof (key), "node-uuid-%d", i); ret = dict_set_dynstr_with_alloc (aggr, key, buf); if (ret) gf_msg (this->name, GF_LOG_ERROR, 0, GD_MSG_DICT_SET_FAILED, @@ -10785,7 +10822,7 @@ glusterd_bitrot_volume_node_rsp (dict_t *aggr, dict_t *rsp_dict) ret = dict_get_int8 (rsp_dict, "scrub-running", &scrub_running); if (!ret) { - snprintf (key, 256, "scrub-running-%d", i); + snprintf (key, sizeof (key), "scrub-running-%d", i); ret = dict_set_uint64 (aggr, key, scrub_running); if (ret) { gf_msg_debug (this->name, 0, "Failed to set " @@ -10795,7 +10832,7 @@ glusterd_bitrot_volume_node_rsp (dict_t *aggr, dict_t *rsp_dict) ret = dict_get_uint64 (rsp_dict, "scrubbed-files", &value); if (!ret) { - snprintf (key, 256, "scrubbed-files-%d", i); + snprintf (key, sizeof (key), "scrubbed-files-%d", i); ret = dict_set_uint64 (aggr, key, value); if (ret) { gf_msg_debug (this->name, 0, "Failed to set " @@ -10805,7 +10842,7 @@ glusterd_bitrot_volume_node_rsp (dict_t *aggr, dict_t *rsp_dict) ret = dict_get_uint64 (rsp_dict, "unsigned-files", &value); if (!ret) { - snprintf (key, 256, "unsigned-files-%d", i); + snprintf (key, sizeof (key), "unsigned-files-%d", i); ret = dict_set_uint64 (aggr, key, value); if (ret) { gf_msg_debug (this->name, 0, "Failed to set " @@ -10815,7 +10852,7 @@ glusterd_bitrot_volume_node_rsp (dict_t *aggr, dict_t *rsp_dict) ret = dict_get_str (rsp_dict, "last-scrub-time", &last_scrub_time); if (!ret) { - snprintf (key, 256, "last-scrub-time-%d", i); + snprintf (key, sizeof (key), "last-scrub-time-%d", i); scrub_time = gf_strdup (last_scrub_time); ret = dict_set_dynstr (aggr, key, scrub_time); @@ -10827,7 +10864,7 @@ glusterd_bitrot_volume_node_rsp (dict_t *aggr, dict_t *rsp_dict) ret = dict_get_uint64 (rsp_dict, "scrub-duration", &value); if (!ret) { - snprintf (key, 256, "scrub-duration-%d", i); + snprintf (key, sizeof (key), "scrub-duration-%d", i); ret = dict_set_uint64 (aggr, key, value); if (ret) { gf_msg_debug (this->name, 0, "Failed to set " @@ -10837,7 +10874,7 @@ glusterd_bitrot_volume_node_rsp (dict_t *aggr, dict_t *rsp_dict) ret = dict_get_uint64 (rsp_dict, "total-count", &value); if (!ret) { - snprintf (key, 256, "error-count-%d", i); + snprintf (key, sizeof (key), "error-count-%d", i); ret = dict_set_uint64 (aggr, key, value); if (ret) { gf_msg_debug (this->name, 0, "Failed to set error " @@ -10846,10 +10883,10 @@ glusterd_bitrot_volume_node_rsp (dict_t *aggr, dict_t *rsp_dict) /* Storing all the bad files in the dictionary */ for (j = 0; j < value; j++) { - snprintf (key, 256, "quarantine-%d", j); + snprintf (key, sizeof (key), "quarantine-%d", j); ret = dict_get_str (rsp_dict, key, &bad_gfid_str); if (!ret) { - snprintf (key, 256, "quarantine-%d-%d", j, i); + snprintf (key, sizeof (key), "quarantine-%d-%d", j, i); ret = dict_set_dynstr_with_alloc (aggr, key, bad_gfid_str); if (ret) { @@ -10921,7 +10958,7 @@ glusterd_volume_rebalance_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) GD_MSG_DICT_GET_FAILED, "failed to get index"); - snprintf (key, 256, "node-uuid-%d", index); + snprintf (key, sizeof (key), "node-uuid-%d", index); ret = dict_get_str (rsp_dict, key, &node_uuid); if (!ret) { node_uuid_str = gf_strdup (node_uuid); @@ -10949,7 +10986,7 @@ glusterd_volume_rebalance_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } /* Setting the same index for the node, as is in the peerlist.*/ - snprintf (key, 256, "node-uuid-%d", current_index); + snprintf (key, sizeof (key), "node-uuid-%d", current_index); ret = dict_set_dynstr (ctx_dict, key, node_uuid_str); if (ret) { gf_msg_debug (THIS->name, 0, @@ -10957,10 +10994,10 @@ glusterd_volume_rebalance_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "files-%d", index); + snprintf (key, sizeof (key), "files-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "files-%d", current_index); + snprintf (key, sizeof (key), "files-%d", current_index); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (THIS->name, 0, @@ -10968,10 +11005,10 @@ glusterd_volume_rebalance_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "size-%d", index); + snprintf (key, sizeof (key), "size-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "size-%d", current_index); + snprintf (key, sizeof (key), "size-%d", current_index); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (THIS->name, 0, @@ -10979,10 +11016,10 @@ glusterd_volume_rebalance_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "lookups-%d", index); + snprintf (key, sizeof (key), "lookups-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "lookups-%d", current_index); + snprintf (key, sizeof (key), "lookups-%d", current_index); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (THIS->name, 0, @@ -10990,10 +11027,10 @@ glusterd_volume_rebalance_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "status-%d", index); + snprintf (key, sizeof (key), "status-%d", index); ret = dict_get_int32 (rsp_dict, key, &value32); if (!ret) { - snprintf (key, 256, "status-%d", current_index); + snprintf (key, sizeof (key), "status-%d", current_index); ret = dict_set_int32 (ctx_dict, key, value32); if (ret) { gf_msg_debug (THIS->name, 0, @@ -11001,10 +11038,10 @@ glusterd_volume_rebalance_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "failures-%d", index); + snprintf (key, sizeof (key), "failures-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "failures-%d", current_index); + snprintf (key, sizeof (key), "failures-%d", current_index); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (THIS->name, 0, @@ -11012,20 +11049,20 @@ glusterd_volume_rebalance_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "skipped-%d", index); + snprintf (key, sizeof (key), "skipped-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "skipped-%d", current_index); + snprintf (key, sizeof (key), "skipped-%d", current_index); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (THIS->name, 0, "failed to set skipped count"); } } - snprintf (key, 256, "run-time-%d", index); + snprintf (key, sizeof (key), "run-time-%d", index); ret = dict_get_double (rsp_dict, key, &elapsed_time); if (!ret) { - snprintf (key, 256, "run-time-%d", current_index); + snprintf (key, sizeof (key), "run-time-%d", current_index); ret = dict_set_double (ctx_dict, key, elapsed_time); if (ret) { gf_msg_debug (THIS->name, 0, @@ -11033,30 +11070,30 @@ glusterd_volume_rebalance_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "time-left-%d", index); + snprintf (key, sizeof (key), "time-left-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "time-left-%d", current_index); + snprintf (key, sizeof (key), "time-left-%d", current_index); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (THIS->name, 0, "failed to set time-left"); } } - snprintf (key, 256, "demoted-%d", index); + snprintf (key, sizeof (key), "demoted-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "demoted-%d", current_index); + snprintf (key, sizeof (key), "demoted-%d", current_index); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (THIS->name, 0, "failed to set demoted count"); } } - snprintf (key, 256, "promoted-%d", index); + snprintf (key, sizeof (key), "promoted-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "promoted-%d", current_index); + snprintf (key, sizeof (key), "promoted-%d", current_index); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (THIS->name, 0, @@ -11121,7 +11158,7 @@ glusterd_volume_tier_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) GD_MSG_DICT_GET_FAILED, "failed to get index"); - snprintf (key, 256, "node-uuid-%d", index); + snprintf (key, sizeof (key), "node-uuid-%d", index); ret = dict_get_str (rsp_dict, key, &node_uuid); if (!ret) { node_uuid_str = gf_strdup (node_uuid); @@ -11135,17 +11172,17 @@ glusterd_volume_tier_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) GD_MSG_DICT_SET_FAILED, "Failed to set count"); - snprintf (key, 256, "node-uuid-%d", count); + snprintf (key, sizeof (key), "node-uuid-%d", count); ret = dict_set_dynstr (ctx_dict, key, node_uuid_str); if (ret) { gf_msg_debug (this->name, 0, "failed to set node-uuid"); } - snprintf (key, 256, "files-%d", index); + snprintf (key, sizeof (key), "files-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "files-%d", count); + snprintf (key, sizeof (key), "files-%d", count); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (this->name, 0, @@ -11153,10 +11190,10 @@ glusterd_volume_tier_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "size-%d", index); + snprintf (key, sizeof (key), "size-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "size-%d", count); + snprintf (key, sizeof (key), "size-%d", count); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (this->name, 0, @@ -11164,10 +11201,10 @@ glusterd_volume_tier_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "lookups-%d", index); + snprintf (key, sizeof (key), "lookups-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "lookups-%d", count); + snprintf (key, sizeof (key), "lookups-%d", count); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (this->name, 0, @@ -11175,10 +11212,10 @@ glusterd_volume_tier_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "status-%d", index); + snprintf (key, sizeof (key), "status-%d", index); ret = dict_get_int32 (rsp_dict, key, &value32); if (!ret) { - snprintf (key, 256, "status-%d", count); + snprintf (key, sizeof (key), "status-%d", count); ret = dict_set_int32 (ctx_dict, key, value32); if (ret) { gf_msg_debug (this->name, 0, @@ -11186,10 +11223,10 @@ glusterd_volume_tier_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "failures-%d", index); + snprintf (key, sizeof (key), "failures-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "failures-%d", count); + snprintf (key, sizeof (key), "failures-%d", count); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (this->name, 0, @@ -11197,20 +11234,20 @@ glusterd_volume_tier_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "skipped-%d", index); + snprintf (key, sizeof (key), "skipped-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "skipped-%d", count); + snprintf (key, sizeof (key), "skipped-%d", count); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (this->name, 0, "failed to set skipped count"); } } - snprintf (key, 256, "run-time-%d", index); + snprintf (key, sizeof (key), "run-time-%d", index); ret = dict_get_double (rsp_dict, key, &elapsed_time); if (!ret) { - snprintf (key, 256, "run-time-%d", count); + snprintf (key, sizeof (key), "run-time-%d", count); ret = dict_set_double (ctx_dict, key, elapsed_time); if (ret) { gf_msg_debug (this->name, 0, @@ -11218,20 +11255,20 @@ glusterd_volume_tier_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "demoted-%d", index); + snprintf (key, sizeof (key), "demoted-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "demoted-%d", count); + snprintf (key, sizeof (key), "demoted-%d", count); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (this->name, 0, "failed to set demoted count"); } } - snprintf (key, 256, "promoted-%d", index); + snprintf (key, sizeof (key), "promoted-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "promoted-%d", count); + snprintf (key, sizeof (key), "promoted-%d", count); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (this->name, 0, @@ -11239,10 +11276,10 @@ glusterd_volume_tier_use_rsp_dict (dict_t *aggr, dict_t *rsp_dict) } } - snprintf (key, 256, "time-left-%d", index); + snprintf (key, sizeof (key), "time-left-%d", index); ret = dict_get_uint64 (rsp_dict, key, &value); if (!ret) { - snprintf (key, 256, "time-left-%d", count); + snprintf (key, sizeof (key), "time-left-%d", count); ret = dict_set_uint64 (ctx_dict, key, value); if (ret) { gf_msg_debug (THIS->name, 0, @@ -11298,9 +11335,12 @@ glusterd_sys_exec_output_rsp_dict (dict_t *dst, dict_t *src) } for (i = 1; i <= src_output_count; i++) { - len = snprintf (output_name, sizeof(output_name) - 1, + len = snprintf (output_name, sizeof (output_name), "output_%d", i); - output_name[len] = '\0'; + if (len <= 0 || len >= sizeof (output_name)) { + ret = -1; + goto out; + } ret = dict_get_str (src, output_name, &output); if (ret) { gf_msg ("glusterd", GF_LOG_ERROR, 0, @@ -11310,9 +11350,12 @@ glusterd_sys_exec_output_rsp_dict (dict_t *dst, dict_t *src) goto out; } - len = snprintf (output_name, sizeof(output_name) - 1, + len = snprintf (output_name, sizeof (output_name), "output_%d", i+dst_output_count); - output_name[len] = '\0'; + if (len <= 0 || len >= sizeof (output_name)) { + ret = -1; + goto out; + } ret = dict_set_dynstr (dst, output_name, gf_strdup (output)); if (ret) { gf_msg ("glusterd", GF_LOG_ERROR, 0, @@ -11458,7 +11501,7 @@ glusterd_volume_quota_copy_to_op_ctx_dict (dict_t *dict, dict_t *rsp_dict) " copied from rsp_dict into op_ctx"); for (i = 0; i < rsp_dict_count; i++) { - snprintf (key, sizeof(key)-1, "gfid%d", i); + snprintf (key, sizeof (key), "gfid%d", i); ret = dict_get_str (rsp_dict, key, &uuid_str); if (ret) { @@ -11469,7 +11512,7 @@ glusterd_volume_quota_copy_to_op_ctx_dict (dict_t *dict, dict_t *rsp_dict) goto out; } - snprintf (key, sizeof (key)-1, "gfid%d", i + count); + snprintf (key, sizeof (key), "gfid%d", i + count); uuid_str_dup = gf_strdup (uuid_str); if (!uuid_str_dup) { @@ -11560,6 +11603,7 @@ _heal_volume_add_shd_rsp (dict_t *this, char *key, data_t *value, void *data) char int_str[16] = ""; data_t *new_value = NULL; char *rxl_end = NULL; + int rxl_end_len; char *rxl_child_end = NULL; glusterd_volinfo_t *volinfo = NULL; int rxl_id = 0; @@ -11575,20 +11619,23 @@ _heal_volume_add_shd_rsp (dict_t *this, char *key, data_t *value, void *data) if (!rxl_end) goto out; - int_len = strlen (key) - strlen (rxl_end); + rxl_child_end = strchr (rxl_end + 1, '-'); + if (!rxl_child_end) + goto out; + + rxl_end_len = strlen (rxl_end); + int_len = strlen (key) - rxl_end_len; strncpy (int_str, key, int_len); int_str[int_len] = '\0'; + ret = gf_string2int (int_str, &rxl_id); if (ret) goto out; - rxl_child_end = strchr (rxl_end + 1, '-'); - if (!rxl_child_end) - goto out; - - int_len = strlen (rxl_end) - strlen (rxl_child_end) - 1; + int_len = rxl_end_len - strlen (rxl_child_end) - 1; strncpy (int_str, rxl_end + 1, int_len); int_str[int_len] = '\0'; + ret = gf_string2int (int_str, &rxl_child_id); if (ret) goto out; @@ -11622,9 +11669,11 @@ _heal_volume_add_shd_rsp_of_statistics (dict_t *this, char *key, data_t char key_begin_string[128] = ""; data_t *new_value = NULL; char *rxl_end = NULL; + int rxl_end_len; char *rxl_child_end = NULL; glusterd_volinfo_t *volinfo = NULL; char *key_begin_str = NULL; + int key_begin_strlen; int rxl_id = 0; int rxl_child_id = 0; int brick_id = 0; @@ -11638,27 +11687,29 @@ _heal_volume_add_shd_rsp_of_statistics (dict_t *this, char *key, data_t if (!key_begin_str) goto out; - int_len = strlen (key) - strlen (key_begin_str); - strncpy (key_begin_string, key, int_len); - key_begin_string[int_len] = '\0'; - rxl_end = strchr (key_begin_str + 1, '-'); if (!rxl_end) goto out; - int_len = strlen (key_begin_str) - strlen (rxl_end) - 1; + rxl_child_end = strchr (rxl_end + 1, '-'); + if (!rxl_child_end) + goto out; + + key_begin_strlen = strlen (key_begin_str); + int_len = strlen (key) - key_begin_strlen; + + strncpy (key_begin_string, key, int_len); + key_begin_string[int_len] = '\0'; + + rxl_end_len = strlen (rxl_end); + int_len = key_begin_strlen - rxl_end_len - 1; strncpy (int_str, key_begin_str + 1, int_len); int_str[int_len] = '\0'; ret = gf_string2int (int_str, &rxl_id); if (ret) goto out; - - rxl_child_end = strchr (rxl_end + 1, '-'); - if (!rxl_child_end) - goto out; - - int_len = strlen (rxl_end) - strlen (rxl_child_end) - 1; + int_len = rxl_end_len - strlen (rxl_child_end) - 1; strncpy (int_str, rxl_end + 1, int_len); int_str[int_len] = '\0'; ret = gf_string2int (int_str, &rxl_child_id); @@ -12097,10 +12148,10 @@ glusterd_defrag_volume_node_rsp (dict_t *req_dict, dict_t *rsp_dict, GD_MSG_DICT_SET_FAILED, "Failed to set count"); - snprintf (buf, 1024, "%s", uuid_utoa (MY_UUID)); + snprintf (buf, sizeof (buf), "%s", uuid_utoa (MY_UUID)); node_str = gf_strdup (buf); - snprintf (key, 256, "node-uuid-%d",i); + snprintf (key, sizeof (key), "node-uuid-%d", i); ret = dict_set_dynstr (op_ctx, key, node_str); if (ret) gf_msg (THIS->name, GF_LOG_ERROR, 0, @@ -13014,8 +13065,11 @@ glusterd_update_mntopts (char *brick_path, glusterd_brickinfo_t *brickinfo) goto out; } - strncpy (brickinfo->fstype, entry->mnt_type, - (sizeof (brickinfo->fstype) - 1)); + if (snprintf (brickinfo->fstype, sizeof (brickinfo->fstype), "%s", + entry->mnt_type) >= sizeof (brickinfo->fstype)) { + ret = -1; + goto out; + } strcpy (brickinfo->mnt_opts, entry->mnt_opts); ret = 0; diff --git a/xlators/mgmt/glusterd/src/glusterd-volgen.c b/xlators/mgmt/glusterd/src/glusterd-volgen.c index 35e49bf56fc..33e6dc064bd 100644 --- a/xlators/mgmt/glusterd/src/glusterd-volgen.c +++ b/xlators/mgmt/glusterd/src/glusterd-volgen.c @@ -4073,8 +4073,11 @@ build_distribute: goto out; } if (volinfo->tier_info.hot_brick_count) { - strncpy (tmp_volname, volinfo->volname, - GD_VOLUME_NAME_MAX - 1); + if (snprintf (tmp_volname, GD_VOLUME_NAME_MAX_TIER, "%s", + volinfo->volname) >= GD_VOLUME_NAME_MAX_TIER) { + ret = -1; + goto out; + } if (volinfo->tier_info.cur_tier_hot) strcat (volinfo->volname, "-hot"); else @@ -4490,12 +4493,12 @@ client_graph_builder (volgen_graph_t *graph, glusterd_volinfo_t *volinfo, goto out; } else { gfproxy_clnt = _gf_true; - namelen = strlen (volinfo->volname) + strlen ("gfproxyd-") + 1; + namelen = strlen (volinfo->volname) + SLEN ("gfproxyd-") + 1; subvol = alloca (namelen); snprintf (subvol, namelen, "gfproxyd-%s", volinfo->volname); namelen = strlen (volinfo->volname) + - strlen ("-gfproxy-client") + 1; + SLEN ("-gfproxy-client") + 1; xl_id = alloca (namelen); snprintf (xl_id, namelen, "%s-gfproxy-client", volinfo->volname); @@ -6448,7 +6451,7 @@ build_bitd_volume_graph (volgen_graph_t *graph, goto out; get_transport_type (volinfo, set_dict, transt, _gf_false); - if (!strncmp (transt, "tcp,rdma", strlen ("tcp,rdma"))) + if (!strncmp (transt, "tcp,rdma", SLEN ("tcp,rdma"))) strncpy (transt, "tcp", sizeof(transt)); cds_list_for_each_entry (brickinfo, &volinfo->bricks, brick_list) { @@ -6610,7 +6613,7 @@ build_scrub_volume_graph (volgen_graph_t *graph, glusterd_volinfo_t *volinfo, goto out; get_transport_type (volinfo, set_dict, transt, _gf_false); - if (!strncmp (transt, "tcp,rdma", strlen ("tcp,rdma"))) + if (!strncmp (transt, "tcp,rdma", SLEN ("tcp,rdma"))) strncpy (transt, "tcp", sizeof(transt)); cds_list_for_each_entry (brickinfo, &volinfo->bricks, brick_list) { diff --git a/xlators/mgmt/glusterd/src/glusterd-volume-ops.c b/xlators/mgmt/glusterd/src/glusterd-volume-ops.c index 030006b5459..14638625d47 100644 --- a/xlators/mgmt/glusterd/src/glusterd-volume-ops.c +++ b/xlators/mgmt/glusterd/src/glusterd-volume-ops.c @@ -1148,7 +1148,7 @@ glusterd_is_valid_vg (glusterd_brickinfo_t *brick, int check_tag, char *msg) dm_list_iterate_items (strl, taglist) { if (!strncmp(strl->str, GF_XATTR_VOL_ID_KEY, - strlen (GF_XATTR_VOL_ID_KEY))) { + SLEN (GF_XATTR_VOL_ID_KEY))) { sprintf (msg, "VG %s is already part of" " a brick", vg_name); retval = -1; @@ -2226,7 +2226,12 @@ glusterd_op_create_volume (dict_t *dict, char **op_errstr) goto out; } - strncpy (volinfo->volname, volname, sizeof(volinfo->volname) - 1); + if (snprintf (volinfo->volname, sizeof (volinfo->volname), + "%s", volname) >= sizeof (volinfo->volname)) { + ret = -1; + goto out; + } + GF_ASSERT (volinfo->volname); ret = dict_get_int32 (dict, "type", &volinfo->type); @@ -2477,8 +2482,9 @@ glusterd_op_create_volume (dict_t *dict, char **op_errstr) "%s not present", key); goto out; } - strncpy (brickinfo->mount_dir, brick_mount_dir, - sizeof(brickinfo->mount_dir)); + snprintf (brickinfo->mount_dir, + sizeof (brickinfo->mount_dir), "%s", + brick_mount_dir); } if (!gf_uuid_compare (brickinfo->uuid, MY_UUID)) { @@ -2696,8 +2702,13 @@ glusterd_op_start_volume (dict_t *dict, char **op_errstr) "%s not present", key); goto out; } - strncpy (brickinfo->mount_dir, brick_mount_dir, - sizeof(brickinfo->mount_dir)); + if (snprintf (brickinfo->mount_dir, + sizeof (brickinfo->mount_dir), + "%s", brick_mount_dir) + >= sizeof (brickinfo->mount_dir)) { + ret = -1; + goto out; + } } } } diff --git a/xlators/mgmt/glusterd/src/glusterd.c b/xlators/mgmt/glusterd/src/glusterd.c index f72953cd319..2d1b6fc7094 100644 --- a/xlators/mgmt/glusterd/src/glusterd.c +++ b/xlators/mgmt/glusterd/src/glusterd.c @@ -574,7 +574,7 @@ glusterd_crt_georep_folders (char *georepdir, glusterd_conf_t *conf) GF_ASSERT (georepdir); GF_ASSERT (conf); - if (strlen (conf->workdir)+2 > PATH_MAX-strlen(GEOREP)) { + if (strlen (conf->workdir)+2 > PATH_MAX - SLEN (GEOREP)) { ret = -1; gf_msg ("glusterd", GF_LOG_CRITICAL, 0, GD_MSG_DIRPATH_TOO_LONG, @@ -597,7 +597,7 @@ glusterd_crt_georep_folders (char *georepdir, glusterd_conf_t *conf) goto out; } - if (strlen (DEFAULT_LOG_FILE_DIRECTORY"/"GEOREP) >= PATH_MAX) { + if (SLEN (DEFAULT_LOG_FILE_DIRECTORY"/"GEOREP) >= PATH_MAX) { ret = -1; gf_msg ("glusterd", GF_LOG_CRITICAL, 0, GD_MSG_DIRPATH_TOO_LONG, @@ -614,7 +614,7 @@ glusterd_crt_georep_folders (char *georepdir, glusterd_conf_t *conf) } /* Slave log file directory */ - if (strlen(DEFAULT_LOG_FILE_DIRECTORY"/"GEOREP"-slaves") >= PATH_MAX) { + if (SLEN (DEFAULT_LOG_FILE_DIRECTORY"/"GEOREP"-slaves") >= PATH_MAX) { ret = -1; gf_msg ("glusterd", GF_LOG_CRITICAL, 0, GD_MSG_DIRPATH_TOO_LONG, @@ -632,7 +632,7 @@ glusterd_crt_georep_folders (char *georepdir, glusterd_conf_t *conf) } /* MountBroker log file directory */ - if (strlen(DEFAULT_LOG_FILE_DIRECTORY"/"GEOREP"-slaves/mbr") >= PATH_MAX) { + if (SLEN (DEFAULT_LOG_FILE_DIRECTORY"/"GEOREP"-slaves/mbr") >= PATH_MAX) { ret = -1; gf_msg ("glusterd", GF_LOG_CRITICAL, 0, GD_MSG_DIRPATH_TOO_LONG, @@ -1435,21 +1435,25 @@ init (xlator_t *this) if (!dir_data) { /* Use default working dir */ - strncpy (rundir, DEFAULT_VAR_RUN_DIRECTORY, PATH_MAX); + len = snprintf (rundir, PATH_MAX, "%s", + DEFAULT_VAR_RUN_DIRECTORY); } else { - strncpy (rundir, dir_data->data, PATH_MAX); + len = snprintf (rundir, PATH_MAX, "%s", dir_data->data); } - - dir_data = NULL; + if (len < 0 || len >= PATH_MAX) + exit (2); dir_data = dict_get (this->options, "working-directory"); if (!dir_data) { //Use default working dir - strncpy (workdir, GLUSTERD_DEFAULT_WORKDIR, PATH_MAX); + len = snprintf (workdir, PATH_MAX, "%s", + GLUSTERD_DEFAULT_WORKDIR); } else { - strncpy (workdir, dir_data->data, PATH_MAX); + len = snprintf (workdir, PATH_MAX, "%s", dir_data->data); } + if (len < 0 || len >= PATH_MAX) + exit (2); ret = sys_stat (workdir, &buf); if ((ret != 0) && (ENOENT != errno)) { |