Skip to content

Commit

Permalink
nvmet: Don't overflow subsysnqn
Browse files Browse the repository at this point in the history
[ Upstream commit 4db3d75 ]

nvmet_root_discovery_nqn_store treats the subsysnqn string like a fixed
size buffer, even though it is dynamically allocated to the size of the
string.

Create a new string with kstrndup instead of using the old buffer.

Reported-by: [email protected]
Closes: https://syzkaller.appspot.com/bug?extid=ff4aab278fa7e27e0f9e
Fixes: 95409e2 ("nvmet: implement unique discovery NQN")
Signed-off-by: Leo Stone <[email protected]>
Reviewed-by: Sagi Grimberg <[email protected]>
Reviewed-by: Christoph Hellwig <[email protected]>
Signed-off-by: Keith Busch <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
  • Loading branch information
leocstone authored and gregkh committed Jan 9, 2025
1 parent 1ee54d5 commit 86645d8
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions drivers/nvme/target/configfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2227,12 +2227,17 @@ static ssize_t nvmet_root_discovery_nqn_store(struct config_item *item,
const char *page, size_t count)
{
struct list_head *entry;
char *old_nqn, *new_nqn;
size_t len;

len = strcspn(page, "\n");
if (!len || len > NVMF_NQN_FIELD_LEN - 1)
return -EINVAL;

new_nqn = kstrndup(page, len, GFP_KERNEL);
if (!new_nqn)
return -ENOMEM;

down_write(&nvmet_config_sem);
list_for_each(entry, &nvmet_subsystems_group.cg_children) {
struct config_item *item =
Expand All @@ -2241,13 +2246,15 @@ static ssize_t nvmet_root_discovery_nqn_store(struct config_item *item,
if (!strncmp(config_item_name(item), page, len)) {
pr_err("duplicate NQN %s\n", config_item_name(item));
up_write(&nvmet_config_sem);
kfree(new_nqn);
return -EINVAL;
}
}
memset(nvmet_disc_subsys->subsysnqn, 0, NVMF_NQN_FIELD_LEN);
memcpy(nvmet_disc_subsys->subsysnqn, page, len);
old_nqn = nvmet_disc_subsys->subsysnqn;
nvmet_disc_subsys->subsysnqn = new_nqn;
up_write(&nvmet_config_sem);

kfree(old_nqn);
return len;
}

Expand Down

0 comments on commit 86645d8

Please sign in to comment.