Skip to content

Commit d15dcd0

Browse files
Dan Carpenterkeithbusch
authored andcommitted
nvmet: prevent sprintf() overflow in nvmet_subsys_nsid_exists()
The nsid value is a u32 that comes from nvmet_req_find_ns(). It's endian data and we're on an error path and both of those raise red flags. So let's make this safer. 1) Make the buffer large enough for any u32. 2) Remove the unnecessary initialization. 3) Use snprintf() instead of sprintf() for even more safety. 4) The sprintf() function returns the number of bytes printed, not counting the NUL terminator. It is impossible for the return value to be <= 0 so delete that. Fixes: 5053639 ("nvmet: fix nvme status code when namespace is disabled") Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org> Reviewed-by: Sagi Grimberg <sagi@grimberg.me> Signed-off-by: Keith Busch <kbusch@kernel.org>
1 parent 34cfb09 commit d15dcd0

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

drivers/nvme/target/configfs.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -757,10 +757,9 @@ static struct configfs_attribute *nvmet_ns_attrs[] = {
757757
bool nvmet_subsys_nsid_exists(struct nvmet_subsys *subsys, u32 nsid)
758758
{
759759
struct config_item *ns_item;
760-
char name[4] = {};
760+
char name[12];
761761

762-
if (sprintf(name, "%u", nsid) <= 0)
763-
return false;
762+
snprintf(name, sizeof(name), "%u", nsid);
764763
mutex_lock(&subsys->namespaces_group.cg_subsys->su_mutex);
765764
ns_item = config_group_find_item(&subsys->namespaces_group, name);
766765
mutex_unlock(&subsys->namespaces_group.cg_subsys->su_mutex);

0 commit comments

Comments
 (0)