Linux-NVME Archive mirror
 help / color / mirror / Atom feed
From: Nilay Shroff <nilay@linux.ibm.com>
To: linux-nvme@lists.infradead.org
Cc: kbusch@kernel.org, dwagner@suse.de, gjoyce@ibm.com, nilay@linux.ibm.com
Subject: [PATCH nvme-cli] nvme : Don't seg fault if given device is not char/block device
Date: Sun, 17 Mar 2024 17:06:22 +0530	[thread overview]
Message-ID: <20240317113758.3055071-1-nilay@linux.ibm.com> (raw)

If the given device name is not char/block device then in
open_dev_direct() set errno to ENODEV and err to -1 before
returning to the dev_fd(). This would then ensure that in
case of error, dev_fd() returns the corerct negative error
code back to its callers. So now the callers of dev_fd()
would handle the error appropriately instead of try
accessing the nvme_dev which would be NULL.

Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
Hi all,

I was running nvme-cli command "id-ns" where I inadvertently
passed a device name which was neither char device nor block
device. Surprisingly, after running the above command I found 
that nvme-cli reported the device name is invalid but then it
crashed. Further debugging into it, I found that it requires 
a trivial fix to avoid the crash. Here's what I tried:

# nvme id-ns /sys/block/nvme0n1 
/sys/block/nvme0n1 is not a block or character device
Segmentation fault

And this is the corresponding gdb backtrace:

Program received signal SIGSEGV, Segmentation fault.
0x000000000040a6ca in __dev_fd (dev=0x0, func=0x8772e1 <__func__.36> "id_ns", line=3639) at ../nvme.h:76
76		if (dev->type != NVME_DEV_DIRECT) {
(gdb) bt
#0  0x000000000040a6ca in __dev_fd (dev=0x0, func=0x8772e1 <__func__.36> "id_ns", line=3639) at ../nvme.h:76
#1  0x000000000041620b in id_ns (argc=2, argv=0x7fffffffdeb0, cmd=0xa14280 <id_ns_cmd>, plugin=0xa15280 <builtin>) at ../nvme.c:3639
#2  0x00000000004482f5 in handle_plugin (argc=2, argv=0x7fffffffdeb0, plugin=0xa15280 <builtin>) at ../plugin.c:171
#3  0x000000000042a2bb in main (argc=3, argv=0x7fffffffdea8) at ../nvme.c:9040

This patch fixes the above seg fault.

Thanks,
--Nilay
---
 nvme.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/nvme.c b/nvme.c
index 3f0f2ff4..00625151 100644
--- a/nvme.c
+++ b/nvme.c
@@ -254,7 +254,8 @@ static int open_dev_direct(struct nvme_dev **devp, char *devstr, int flags)
 	}
 	if (!is_chardev(dev) && !is_blkdev(dev)) {
 		nvme_show_error("%s is not a block or character device", devstr);
-		err = -ENODEV;
+		errno = ENODEV;
+		err = -1;
 		goto err_close;
 	}
 	*devp = dev;
-- 
2.43.0



             reply	other threads:[~2024-03-17 11:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-17 11:36 Nilay Shroff [this message]
2024-03-20  7:16 ` [PATCH nvme-cli] nvme : Don't seg fault if given device is not char/block device Daniel Wagner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240317113758.3055071-1-nilay@linux.ibm.com \
    --to=nilay@linux.ibm.com \
    --cc=dwagner@suse.de \
    --cc=gjoyce@ibm.com \
    --cc=kbusch@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).