public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvme-tcp: fix signedness bug in nvme_tcp_init_connection()
@ 2025-02-28  9:39 Dan Carpenter
  2025-02-28 16:19 ` Caleb Sander Mateos
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Dan Carpenter @ 2025-02-28  9:39 UTC (permalink / raw)
  To: Caleb Sander Mateos
  Cc: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Hannes Reinecke, linux-nvme, linux-kernel, kernel-janitors

The kernel_recvmsg() function returns an int which could be either
negative error codes or the number of bytes received.  The problem is
that the condition:

	if (ret < sizeof(*icresp)) {

is type promoted to type unsigned long and negative values are treated
as high positive values which is success, when they should be treated as
failure.  Add a cast so to avoid the type promotion.

Fixes: 578539e09690 ("nvme-tcp: fix connect failure on receiving partial ICResp PDU")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
---
 drivers/nvme/host/tcp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
index 8a9131c95a3d..361b04ec5b5d 100644
--- a/drivers/nvme/host/tcp.c
+++ b/drivers/nvme/host/tcp.c
@@ -1495,7 +1495,7 @@ static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
 	msg.msg_flags = MSG_WAITALL;
 	ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
 			iov.iov_len, msg.msg_flags);
-	if (ret < sizeof(*icresp)) {
+	if (ret < (int)sizeof(*icresp)) {
 		pr_warn("queue %d: failed to receive icresp, error %d\n",
 			nvme_tcp_queue_id(queue), ret);
 		if (ret >= 0)
-- 
2.47.2


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] nvme-tcp: fix signedness bug in nvme_tcp_init_connection()
  2025-02-28  9:39 [PATCH] nvme-tcp: fix signedness bug in nvme_tcp_init_connection() Dan Carpenter
@ 2025-02-28 16:19 ` Caleb Sander Mateos
  2025-03-01 23:16 ` Sagi Grimberg
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Caleb Sander Mateos @ 2025-02-28 16:19 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Hannes Reinecke, linux-nvme, linux-kernel, kernel-janitors

On Fri, Feb 28, 2025 at 1:39 AM Dan Carpenter <dan.carpenter@linaro.org> wrote:
>
> The kernel_recvmsg() function returns an int which could be either
> negative error codes or the number of bytes received.  The problem is
> that the condition:
>
>         if (ret < sizeof(*icresp)) {
>
> is type promoted to type unsigned long and negative values are treated
> as high positive values which is success, when they should be treated as
> failure.  Add a cast so to avoid the type promotion.

"so as to"?

>
> Fixes: 578539e09690 ("nvme-tcp: fix connect failure on receiving partial ICResp PDU")
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>

Good catch, thanks for fixing this.

Reviewed-by: Caleb Sander Mateos <csander@purestorage.com>

> ---
>  drivers/nvme/host/tcp.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
> index 8a9131c95a3d..361b04ec5b5d 100644
> --- a/drivers/nvme/host/tcp.c
> +++ b/drivers/nvme/host/tcp.c
> @@ -1495,7 +1495,7 @@ static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
>         msg.msg_flags = MSG_WAITALL;
>         ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
>                         iov.iov_len, msg.msg_flags);
> -       if (ret < sizeof(*icresp)) {
> +       if (ret < (int)sizeof(*icresp)) {
>                 pr_warn("queue %d: failed to receive icresp, error %d\n",
>                         nvme_tcp_queue_id(queue), ret);
>                 if (ret >= 0)
> --
> 2.47.2
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] nvme-tcp: fix signedness bug in nvme_tcp_init_connection()
  2025-02-28  9:39 [PATCH] nvme-tcp: fix signedness bug in nvme_tcp_init_connection() Dan Carpenter
  2025-02-28 16:19 ` Caleb Sander Mateos
@ 2025-03-01 23:16 ` Sagi Grimberg
  2025-03-03 18:43 ` Chaitanya Kulkarni
  2025-03-05 14:25 ` Christoph Hellwig
  3 siblings, 0 replies; 7+ messages in thread
From: Sagi Grimberg @ 2025-03-01 23:16 UTC (permalink / raw)
  To: Dan Carpenter, Caleb Sander Mateos
  Cc: Keith Busch, Jens Axboe, Christoph Hellwig, Hannes Reinecke,
	linux-nvme, linux-kernel, kernel-janitors

Reviewed-by: Sagi Grimberg <sagi@grimberg.me>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] nvme-tcp: fix signedness bug in nvme_tcp_init_connection()
  2025-02-28  9:39 [PATCH] nvme-tcp: fix signedness bug in nvme_tcp_init_connection() Dan Carpenter
  2025-02-28 16:19 ` Caleb Sander Mateos
  2025-03-01 23:16 ` Sagi Grimberg
@ 2025-03-03 18:43 ` Chaitanya Kulkarni
  2025-03-05 14:25 ` Christoph Hellwig
  3 siblings, 0 replies; 7+ messages in thread
From: Chaitanya Kulkarni @ 2025-03-03 18:43 UTC (permalink / raw)
  To: Dan Carpenter, Caleb Sander Mateos
  Cc: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Hannes Reinecke, linux-nvme@lists.infradead.org,
	linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org

On 2/28/25 01:39, Dan Carpenter wrote:
> The kernel_recvmsg() function returns an int which could be either
> negative error codes or the number of bytes received.  The problem is
> that the condition:
>
> 	if (ret < sizeof(*icresp)) {
>
> is type promoted to type unsigned long and negative values are treated
> as high positive values which is success, when they should be treated as
> failure.  Add a cast so to avoid the type promotion.
>
> Fixes: 578539e09690 ("nvme-tcp: fix connect failure on receiving partial ICResp PDU")
> Signed-off-by: Dan Carpenter<dan.carpenter@linaro.org>

Thanks for the fix, looks good.

Reviewed-by: Chaitanya Kulkarni <kch@nvidia.com>

-ck



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] nvme-tcp: fix signedness bug in nvme_tcp_init_connection()
  2025-02-28  9:39 [PATCH] nvme-tcp: fix signedness bug in nvme_tcp_init_connection() Dan Carpenter
                   ` (2 preceding siblings ...)
  2025-03-03 18:43 ` Chaitanya Kulkarni
@ 2025-03-05 14:25 ` Christoph Hellwig
  2025-03-05 14:37   ` Dan Carpenter
  3 siblings, 1 reply; 7+ messages in thread
From: Christoph Hellwig @ 2025-03-05 14:25 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Caleb Sander Mateos, Keith Busch, Jens Axboe, Christoph Hellwig,
	Sagi Grimberg, Hannes Reinecke, linux-nvme, linux-kernel,
	kernel-janitors

On Fri, Feb 28, 2025 at 12:39:41PM +0300, Dan Carpenter wrote:
> index 8a9131c95a3d..361b04ec5b5d 100644
> --- a/drivers/nvme/host/tcp.c
> +++ b/drivers/nvme/host/tcp.c
> @@ -1495,7 +1495,7 @@ static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
>  	msg.msg_flags = MSG_WAITALL;
>  	ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
>  			iov.iov_len, msg.msg_flags);
> -	if (ret < sizeof(*icresp)) {
> +	if (ret < (int)sizeof(*icresp)) {
>  		pr_warn("queue %d: failed to receive icresp, error %d\n",
>  			nvme_tcp_queue_id(queue), ret);
>  		if (ret >= 0)

I hate these magic casts.  What about something like:

	if (ret >= 0 && ret < sizeof(*icresp))
		ret = -ECONNRESET;
	if (ret < 0) {
		...


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] nvme-tcp: fix signedness bug in nvme_tcp_init_connection()
  2025-03-05 14:25 ` Christoph Hellwig
@ 2025-03-05 14:37   ` Dan Carpenter
  2025-03-05 14:38     ` Christoph Hellwig
  0 siblings, 1 reply; 7+ messages in thread
From: Dan Carpenter @ 2025-03-05 14:37 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Caleb Sander Mateos, Keith Busch, Jens Axboe, Sagi Grimberg,
	Hannes Reinecke, linux-nvme, linux-kernel, kernel-janitors

On Wed, Mar 05, 2025 at 03:25:54PM +0100, Christoph Hellwig wrote:
> On Fri, Feb 28, 2025 at 12:39:41PM +0300, Dan Carpenter wrote:
> > index 8a9131c95a3d..361b04ec5b5d 100644
> > --- a/drivers/nvme/host/tcp.c
> > +++ b/drivers/nvme/host/tcp.c
> > @@ -1495,7 +1495,7 @@ static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
> >  	msg.msg_flags = MSG_WAITALL;
> >  	ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
> >  			iov.iov_len, msg.msg_flags);
> > -	if (ret < sizeof(*icresp)) {
> > +	if (ret < (int)sizeof(*icresp)) {
> >  		pr_warn("queue %d: failed to receive icresp, error %d\n",
> >  			nvme_tcp_queue_id(queue), ret);
> >  		if (ret >= 0)
> 
> I hate these magic casts.  What about something like:
> 
> 	if (ret >= 0 && ret < sizeof(*icresp))
> 		ret = -ECONNRESET;
> 	if (ret < 0) {
> 		...

Sure, I can do that.

I don't love casts either.  I normally have tried to write these as
"if (ret < 0 || ret < sizeof(*icresp)) {" and people don't love that.

regards,
dan carpenter

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] nvme-tcp: fix signedness bug in nvme_tcp_init_connection()
  2025-03-05 14:37   ` Dan Carpenter
@ 2025-03-05 14:38     ` Christoph Hellwig
  0 siblings, 0 replies; 7+ messages in thread
From: Christoph Hellwig @ 2025-03-05 14:38 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Christoph Hellwig, Caleb Sander Mateos, Keith Busch, Jens Axboe,
	Sagi Grimberg, Hannes Reinecke, linux-nvme, linux-kernel,
	kernel-janitors

On Wed, Mar 05, 2025 at 05:37:15PM +0300, Dan Carpenter wrote:
> > 	if (ret >= 0 && ret < sizeof(*icresp))
> > 		ret = -ECONNRESET;
> > 	if (ret < 0) {
> > 		...
> 
> Sure, I can do that.
> 
> I don't love casts either.  I normally have tried to write these as
> "if (ret < 0 || ret < sizeof(*icresp)) {" and people don't love that.

I can see why people don't like it as it's really counterintuitive.
These kinds of interfaces just suck given the type promotion rules
unfortunately.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-03-05 14:38 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-28  9:39 [PATCH] nvme-tcp: fix signedness bug in nvme_tcp_init_connection() Dan Carpenter
2025-02-28 16:19 ` Caleb Sander Mateos
2025-03-01 23:16 ` Sagi Grimberg
2025-03-03 18:43 ` Chaitanya Kulkarni
2025-03-05 14:25 ` Christoph Hellwig
2025-03-05 14:37   ` Dan Carpenter
2025-03-05 14:38     ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox