nvme: Enter string size calculation “subsysnqn”
Subsystems (2)
PATCH 1/1 nvme: Enter string size calculation “subsysnqn”
When memory is allocated, the size of the string
is calculated nvmet_subsys_alloc(...).
When memory was accessed, constant size was used.
--- drivers/nvme/target/configfs.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/nvme/target/configfs.c +6 -4
@@ -2247,14 +2247,15 @@ static struct config_group nvmet_hosts_group;static ssize_t nvmet_root_discovery_nqn_show (struct config_item *item,char *page) { -return snprintf (page,PAGE_SIZE ,"%s\n" , nvmet_disc_subsys->subsysnqn); +return snprintf (page,strnlen (nvmet_disc_subsys->subsysnqn,PAGE_SIZE ), +"%s\n" , nvmet_disc_subsys->subsysnqn); }static ssize_t nvmet_root_discovery_nqn_store (struct config_item *item,const char *page,size_t count) {struct list_head *entry; -size_t len; +size_t len, nqn_len; len =strcspn (page,"\n" );if (!len || len >NVMF_NQN_FIELD_LEN -1 )
@@ -2271,8 +2272,9 @@ static ssize_t nvmet_root_discovery_nqn_store(struct config_item *item,return -EINVAL ; } } -memset (nvmet_disc_subsys->subsysnqn,0 ,NVMF_NQN_FIELD_LEN ); -memcpy (nvmet_disc_subsys->subsysnqn, page, len); + nqn_len =strnlen (nvmet_disc_subsys->subsysnqn,NVMF_NQN_SIZE ); +memset (nvmet_disc_subsys->subsysnqn,0 , nqn_len); +memcpy (nvmet_disc_subsys->subsysnqn, page, nqn_len);up_write (&nvmet_config_sem);return len;
Signature · 1 line
2.43.0
Christoph Hellwig
maintainerQuoted 2 lines · Denis Arefev
When memory is allocated, the size of the string
is calculatednvmet_subsys_alloc(...).
s/is/is in/ ?
Quoted 15 lines
When memory was accessed, constant size was used.
Fixes 95409e277d83 ("nvmet: implement unique discovery NQN")Reported-by [email protected]Signed-off-by Denis Arefev <[email protected]>--- drivers/nvme/target/configfs.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)drivers/nvme/target/configfs.c +0
-0
drivers/nvme/target/configfs.c static struct config_group nvmet_hosts_group; · line 2251
Quoted 6 lines
drivers/nvme/target/configfs.c +2
-1@@ -2247,14 +2247,15 @@ static struct config_group nvmet_hosts_group;static ssize_t nvmet_root_discovery_nqn_show (struct config_item *item,char *page) { -return snprintf (page,PAGE_SIZE ,"%s\n" , nvmet_disc_subsys->subsysnqn); +return snprintf (page,strnlen (nvmet_disc_subsys->subsysnqn,PAGE_SIZE ), +"%s\n" , nvmet_disc_subsys->subsysnqn);
This doesn't really make sense as %s already calculates the length of the string internally.
Quoted 12 lines
drivers/nvme/target/configfs.c +1
-1}static ssize_t nvmet_root_discovery_nqn_store (struct config_item *item,const char *page,size_t count) {struct list_head *entry; -size_t len; +size_t len, nqn_len; len =strcspn (page,"\n" );if (!len || len >NVMF_NQN_FIELD_LEN -1 )
drivers/nvme/target/configfs.c static ssize_t nvmet_root_discovery_nqn_store(struct config_item *item, · line 2277
Quoted 8 lines
drivers/nvme/target/configfs.c +3
-2@@ -2271,8 +2272,9 @@ static ssize_t nvmet_root_discovery_nqn_store(struct config_item *item,return -EINVAL ; } } -memset (nvmet_disc_subsys->subsysnqn,0 ,NVMF_NQN_FIELD_LEN ); -memcpy (nvmet_disc_subsys->subsysnqn, page, len); + nqn_len =strnlen (nvmet_disc_subsys->subsysnqn,NVMF_NQN_SIZE ); +memset (nvmet_disc_subsys->subsysnqn,0 , nqn_len); +memcpy (nvmet_disc_subsys->subsysnqn, page, nqn_len);
The memset should still happen for the entire field, only the copy needs to me limited.
But i really wonder if we should do away with this mess and just use kmemdup()ed allocations for the nqns.
The bug has already been fixed in commit 4db3d750ac7e ("nvmet: Don't overflow subsysnqn"). It is using the kmemdup() approach (technically, kstrndup()) suggested by Christoph.
Quoted 60 lines · Christoph Hellwig
Quoted 2 lines · Denis Arefev
When memory is allocated, the size of the string
is calculatednvmet_subsys_alloc(...).s/is/is in/ ?
Quoted 15 lines
When memory was accessed, constant size was used.
Fixes 95409e277d83 ("nvmet: implement unique discovery NQN")Reported-by [email protected]Signed-off-by Denis Arefev <[email protected]>--- drivers/nvme/target/configfs.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)drivers/nvme/target/configfs.c +0
-0drivers/nvme/target/configfs.c static struct config_group nvmet_hosts_group; · line 2251
Quoted 6 lines
drivers/nvme/target/configfs.c +2
-1@@ -2247,14 +2247,15 @@ static struct config_group nvmet_hosts_group;static ssize_t nvmet_root_discovery_nqn_show (struct config_item *item,char *page) { -return snprintf (page,PAGE_SIZE ,"%s\n" , nvmet_disc_subsys->subsysnqn); +return snprintf (page,strnlen (nvmet_disc_subsys->subsysnqn,PAGE_SIZE ), +"%s\n" , nvmet_disc_subsys->subsysnqn);This doesn't really make sense as %s already calculates the length of the string internally.
Quoted 12 lines
drivers/nvme/target/configfs.c +1
-1}static ssize_t nvmet_root_discovery_nqn_store (struct config_item *item,const char *page,size_t count) {struct list_head *entry; -size_t len; +size_t len, nqn_len; len =strcspn (page,"\n" );if (!len || len >NVMF_NQN_FIELD_LEN -1 )drivers/nvme/target/configfs.c static ssize_t nvmet_root_discovery_nqn_store(struct config_item *item, · line 2277
Quoted 8 lines
drivers/nvme/target/configfs.c +3
-2@@ -2271,8 +2272,9 @@ static ssize_t nvmet_root_discovery_nqn_store(struct config_item *item,return -EINVAL ; } } -memset (nvmet_disc_subsys->subsysnqn,0 ,NVMF_NQN_FIELD_LEN ); -memcpy (nvmet_disc_subsys->subsysnqn, page, len); + nqn_len =strnlen (nvmet_disc_subsys->subsysnqn,NVMF_NQN_SIZE ); +memset (nvmet_disc_subsys->subsysnqn,0 , nqn_len); +memcpy (nvmet_disc_subsys->subsysnqn, page, nqn_len);The memset should still happen for the entire field, only the copy needs to me limited.
But i really wonder if we should do away with this mess and just use kmemdup()ed allocations for the nqns.