diff options
author | Lee Duncan <lduncan@suse.com> | 2018-03-21 18:44:44 -0700 |
---|---|---|
committer | Lee Duncan <lduncan@suse.com> | 2018-03-21 18:50:54 -0700 |
commit | af02412d3dd3839441ae48e2c9f9c307889d5fc1 (patch) | |
tree | fe968aa770255da42e2f1985401ff09606a42b5e /libopeniscsiusr | |
parent | 1846d2c995f38fc5fa6e1960f6644b4b3b543bb0 (diff) | |
download | open-iscsi-af02412d3dd3839441ae48e2c9f9c307889d5fc1.tar.gz |
libopeniscsiusr: ensure sysfs pathname doesn't overflow.
When instantiating a pathname in sysfs, make sure there's enough
room to prevent possible overflow.
Found by gcc-8, which gave this warning:
> [54s] sysfs.c:326:48: error: '/iscsi_host/' directive output may be
> truncated writing 12 bytes into a region of size between 1 and 4096
> [-Werror=format-truncation=]
Diffstat (limited to 'libopeniscsiusr')
-rw-r--r-- | libopeniscsiusr/sysfs.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/libopeniscsiusr/sysfs.c b/libopeniscsiusr/sysfs.c index 70298f2..6f590f4 100644 --- a/libopeniscsiusr/sysfs.c +++ b/libopeniscsiusr/sysfs.c @@ -311,6 +311,9 @@ int _iscsi_host_id_of_session(struct iscsi_context *ctx, uint32_t sid, int n = 0; const char *host_id_str = NULL; int i = 0; + const char iscsi_host_dir_str[] = "/iscsi_host/"; + const unsigned int iscsi_host_dir_strlen = strlen(iscsi_host_dir_str); + assert(ctx != NULL); assert(sid != 0); @@ -323,8 +326,16 @@ int _iscsi_host_id_of_session(struct iscsi_context *ctx, uint32_t sid, _good(sysfs_get_dev_path(ctx, sys_se_dir_path, sys_dev_path), rc, out); - snprintf(sys_scsi_host_dir_path, PATH_MAX, "%s/iscsi_host/", - sys_dev_path); + if ((strlen(sys_dev_path) + iscsi_host_dir_strlen) >= PATH_MAX) { + rc = LIBISCSI_ERR_SYSFS_LOOKUP; + _error(ctx, "Pathname too long: %s%s", + sys_dev_path, iscsi_host_dir_str); + goto out; + } + + strncpy(sys_scsi_host_dir_path, sys_dev_path, PATH_MAX); + strncat(sys_scsi_host_dir_path, iscsi_host_dir_str, + PATH_MAX - iscsi_host_dir_strlen); n = scandir(sys_scsi_host_dir_path, &namelist, _scan_filter_skip_dot, alphasort); |