public inbox for linux-kernel-mentees@lists.linux-foundation.org
 help / color / mirror / Atom feed
* [PATCH] drivers: block: rnbd: Handle generic ERR_PTR returns safely in find_and_get_or_create_sess()
@ 2025-10-21  6:21 Ranganath V N
  2025-10-21  8:01 ` Jinpu Wang
  0 siblings, 1 reply; 2+ messages in thread
From: Ranganath V N @ 2025-10-21  6:21 UTC (permalink / raw)
  To: Md. Haris Iqbal, Jack Wang, Jens Axboe
  Cc: linux-block, linux-kernel, skhan, david.hunter.linux, khalid,
	linux-kernel-mentees, Ranganath V N

Fix the issue detected by the smatch tool.
drivers/block/rnbd/rnbd-clt.c:1241 find_and_get_or_create_sess()
error: 'sess' dereferencing possible ERR_PTR()

find_and_get_or_create_sess() only checks for ERR_PTR(-ENOMEM) after
calling find_or_create_sess(). In other encoded failures, the code
may dereference the error pointer when accessing sess->nr_poll_queues,
resulting ina kernel oops.

By preserving the existing -ENOMEM behaviour and log unexpected
errors to assist in debugging. This change eliminates a potential
invalid pointer dereference without altering the function's logic
or intenet.

Tested by compiling using smatch tool.

Signed-off-by: Ranganath V N <vnranganath.20@gmail.com>
---
 drivers/block/rnbd/rnbd-clt.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/block/rnbd/rnbd-clt.c b/drivers/block/rnbd/rnbd-clt.c
index f1409e54010a..57ca694210b9 100644
--- a/drivers/block/rnbd/rnbd-clt.c
+++ b/drivers/block/rnbd/rnbd-clt.c
@@ -1236,9 +1236,14 @@ find_and_get_or_create_sess(const char *sessname,
 	struct rtrs_clt_ops rtrs_ops;
 
 	sess = find_or_create_sess(sessname, &first);
-	if (sess == ERR_PTR(-ENOMEM)) {
-		return ERR_PTR(-ENOMEM);
-	} else if ((nr_poll_queues && !first) ||  (!nr_poll_queues && sess->nr_poll_queues)) {
+	if (IS_ERR(sess)) {
+		err = PTR_ERR(sess);
+		if (err != -ENOMEM)
+			pr_warn("rndb: find_or_create_sess(%s) failed with %d\n",
+				sessname, err);
+		return ERR_PTR(err);
+	}
+	if ((nr_poll_queues && !first) ||  (!nr_poll_queues && sess->nr_poll_queues)) {
 		/*
 		 * A device MUST have its own session to use the polling-mode.
 		 * It must fail to map new device with the same session.

---
base-commit: 211ddde0823f1442e4ad052a2f30f050145ccada
change-id: 20251021-rnbd_fix-9b478fb52403

Best regards,
-- 
Ranganath V N <vnranganath.20@gmail.com>


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

* Re: [PATCH] drivers: block: rnbd: Handle generic ERR_PTR returns safely in find_and_get_or_create_sess()
  2025-10-21  6:21 [PATCH] drivers: block: rnbd: Handle generic ERR_PTR returns safely in find_and_get_or_create_sess() Ranganath V N
@ 2025-10-21  8:01 ` Jinpu Wang
  0 siblings, 0 replies; 2+ messages in thread
From: Jinpu Wang @ 2025-10-21  8:01 UTC (permalink / raw)
  To: Ranganath V N
  Cc: Md. Haris Iqbal, Jens Axboe, linux-block, linux-kernel, skhan,
	david.hunter.linux, khalid, linux-kernel-mentees

On Tue, Oct 21, 2025 at 8:21 AM Ranganath V N <vnranganath.20@gmail.com> wrote:
>
> Fix the issue detected by the smatch tool.
> drivers/block/rnbd/rnbd-clt.c:1241 find_and_get_or_create_sess()
> error: 'sess' dereferencing possible ERR_PTR()
>
> find_and_get_or_create_sess() only checks for ERR_PTR(-ENOMEM) after
> calling find_or_create_sess(). In other encoded failures, the code
> may dereference the error pointer when accessing sess->nr_poll_queues,
> resulting ina kernel oops.
>
> By preserving the existing -ENOMEM behaviour and log unexpected
> errors to assist in debugging. This change eliminates a potential
> invalid pointer dereference without altering the function's logic
> or intenet.
>
> Tested by compiling using smatch tool.
Thx for the patch.
But I read the code again, find_or_create_sess -> alloc_sess, which
only return ERR_PTR(-ENOMEM) or a valid pointer.
I think this is just a false positive.
>
> Signed-off-by: Ranganath V N <vnranganath.20@gmail.com>
> ---
>  drivers/block/rnbd/rnbd-clt.c | 11 ++++++++---
>  1 file changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/block/rnbd/rnbd-clt.c b/drivers/block/rnbd/rnbd-clt.c
> index f1409e54010a..57ca694210b9 100644
> --- a/drivers/block/rnbd/rnbd-clt.c
> +++ b/drivers/block/rnbd/rnbd-clt.c
> @@ -1236,9 +1236,14 @@ find_and_get_or_create_sess(const char *sessname,
>         struct rtrs_clt_ops rtrs_ops;
>
>         sess = find_or_create_sess(sessname, &first);
> -       if (sess == ERR_PTR(-ENOMEM)) {
> -               return ERR_PTR(-ENOMEM);
> -       } else if ((nr_poll_queues && !first) ||  (!nr_poll_queues && sess->nr_poll_queues)) {
> +       if (IS_ERR(sess)) {
> +               err = PTR_ERR(sess);
> +               if (err != -ENOMEM)
> +                       pr_warn("rndb: find_or_create_sess(%s) failed with %d\n",
> +                               sessname, err);
> +               return ERR_PTR(err);
> +       }
> +       if ((nr_poll_queues && !first) ||  (!nr_poll_queues && sess->nr_poll_queues)) {
>                 /*
>                  * A device MUST have its own session to use the polling-mode.
>                  * It must fail to map new device with the same session.
>
> ---
> base-commit: 211ddde0823f1442e4ad052a2f30f050145ccada
> change-id: 20251021-rnbd_fix-9b478fb52403
>
> Best regards,
> --
> Ranganath V N <vnranganath.20@gmail.com>
>

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

end of thread, other threads:[~2025-10-21  8:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-21  6:21 [PATCH] drivers: block: rnbd: Handle generic ERR_PTR returns safely in find_and_get_or_create_sess() Ranganath V N
2025-10-21  8:01 ` Jinpu Wang

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