From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from verein.lst.de (verein.lst.de [213.95.11.211]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 06CAE14AD02 for ; Fri, 3 Jan 2025 07:01:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.95.11.211 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735887702; cv=none; b=D6zTz766u8/1K1mwqzGofLnD4RscP6QAeP35/8ly/gRSy8WopWX1WevXIZqNjfYbLa3RQBlC7mhXpek3kZ76dCJ3f/1rehBiL5Fn9c5KumeLkNssgKGubItSrEUdKl3fEnG9e4ohpC36K2F7jUx1ICBP48Lz+A4HrGZccx25slA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735887702; c=relaxed/simple; bh=+lOFuZEz+m32ubpLmxsYDYr9xxy+SwvCraiJrHF9M+0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=P5hJ/EFBwnNIo8pS5WNEfY0IXjKtxZGBNGqNm7iX2U/vIVHUmDv6nFwQVgnYvgRujW3zHhXQkU0G/+ErdkTaXi7d1AB6k+ajX+U0shidO6f1hH/VJBMERyPuNjjZM8YFH7rH3XScyywYB2Mk8lb/DcGN5acCfDLT5wHcq3d3Fa4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=pass smtp.mailfrom=lst.de; arc=none smtp.client-ip=213.95.11.211 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=lst.de Received: by verein.lst.de (Postfix, from userid 2407) id 0745868C7B; Fri, 3 Jan 2025 08:01:31 +0100 (CET) Date: Fri, 3 Jan 2025 08:01:30 +0100 From: Christoph Hellwig To: Denis Arefev Cc: Christoph Hellwig , Sagi Grimberg , Chaitanya Kulkarni , Hannes Reinecke , Keith Busch , linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, syzbot+a84181c81389771eb46a@syzkaller.appspotmail.com Subject: Re: [PATCH] nvme: Enter string =?utf-8?Q?s?= =?utf-8?Q?ize_calculation_=E2=80=9Csubsysnqn=E2=80=9D?= Message-ID: <20250103070130.GB28303@lst.de> References: <20241226104546.13705-1-arefev@swemel.ru> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20241226104546.13705-1-arefev@swemel.ru> User-Agent: Mutt/1.5.17 (2007-11-01) On Thu, Dec 26, 2024 at 01:45:35PM +0300, Denis Arefev wrote: > When memory is allocated, the size of the string > is calculated nvmet_subsys_alloc(...). s/is/is in/ ? > When memory was accessed, constant size was used. > > Fixes: 95409e277d83 ("nvmet: implement unique discovery NQN") > Reported-by: syzbot+a84181c81389771eb46a@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=a84181c81389771eb46a > Signed-off-by: Denis Arefev > --- > drivers/nvme/target/configfs.c | 10 ++++++---- > 1 file changed, 6 insertions(+), 4 deletions(-) > > diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c > index eeee9e9b854c..2f74204d000e 100644 > --- a/drivers/nvme/target/configfs.c > +++ b/drivers/nvme/target/configfs.c > @@ -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. > } > > 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); 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.