Skip to content

Commit 4db3d75

Browse files
leocstonekeithbusch
authored andcommitted
nvmet: Don't overflow subsysnqn
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: syzbot+ff4aab278fa7e27e0f9e@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=ff4aab278fa7e27e0f9e Fixes: 95409e2 ("nvmet: implement unique discovery NQN") Signed-off-by: Leo Stone <leocstone@gmail.com> Reviewed-by: Sagi Grimberg <sagi@grimberg.me> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Keith Busch <kbusch@kernel.org>
1 parent ebefac5 commit 4db3d75

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

drivers/nvme/target/configfs.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,12 +2254,17 @@ static ssize_t nvmet_root_discovery_nqn_store(struct config_item *item,
22542254
const char *page, size_t count)
22552255
{
22562256
struct list_head *entry;
2257+
char *old_nqn, *new_nqn;
22572258
size_t len;
22582259

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

2264+
new_nqn = kstrndup(page, len, GFP_KERNEL);
2265+
if (!new_nqn)
2266+
return -ENOMEM;
2267+
22632268
down_write(&nvmet_config_sem);
22642269
list_for_each(entry, &nvmet_subsystems_group.cg_children) {
22652270
struct config_item *item =
@@ -2268,13 +2273,15 @@ static ssize_t nvmet_root_discovery_nqn_store(struct config_item *item,
22682273
if (!strncmp(config_item_name(item), page, len)) {
22692274
pr_err("duplicate NQN %s\n", config_item_name(item));
22702275
up_write(&nvmet_config_sem);
2276+
kfree(new_nqn);
22712277
return -EINVAL;
22722278
}
22732279
}
2274-
memset(nvmet_disc_subsys->subsysnqn, 0, NVMF_NQN_FIELD_LEN);
2275-
memcpy(nvmet_disc_subsys->subsysnqn, page, len);
2280+
old_nqn = nvmet_disc_subsys->subsysnqn;
2281+
nvmet_disc_subsys->subsysnqn = new_nqn;
22762282
up_write(&nvmet_config_sem);
22772283

2284+
kfree(old_nqn);
22782285
return len;
22792286
}
22802287

0 commit comments

Comments
 (0)