From 2e289d4fc5ff1d7ab80940b6ffd5c0fd54c57ae5 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Mon, 23 Nov 2026 12:27:01 +0300 Subject: [PATCH 1/2] Accept only digits in NBD device numbers sscanf("%d") takes a sign and leading spaces and overflows, so "vitastor-nbd unmap /dev/nbd+0", "/dev/nbd 0" and /dev/nbd4294967296 all acted on /dev/nbd0: the fallback that #120 removed for mistyped device names. Parse N and /dev/nbdN only in get_dev_num(), with a 9-digit [0-9] scanset, and keep the positional argument as given. --dev_num now also accepts /dev/nbdN, the form the error asks for. --- src/client/nbd_proxy.cpp | 18 +++++++----------- tests/test_img_online_resize.sh | 8 ++++++++ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/client/nbd_proxy.cpp b/src/client/nbd_proxy.cpp index 26b6c6f9..5d7c8cf7 100644 --- a/src/client/nbd_proxy.cpp +++ b/src/client/nbd_proxy.cpp @@ -412,12 +412,7 @@ public: } else if (pos == 1) { - char c = 0; - int n = 0; - if (sscanf(args[i], "/dev/nbd%d%c", &n, &c) == 1) - cfg["dev_num"] = n; - else - cfg["dev_num"] = args[i]; + cfg["dev_num"] = args[i]; pos++; } } @@ -436,14 +431,15 @@ public: } return -1; } - char c = 0; - int n = -1; - if (sscanf(cfg["dev_num"].as_string().c_str(), "%d%c", &n, &c) != 1 || n < 0) + // Digits only: %d takes signs and spaces, and overflows into another device number + std::string s = cfg["dev_num"].as_string(); + char digits[10] = { 0 }, c = 0; + if (sscanf(s.c_str(), "%9[0-9]%c", digits, &c) != 1 && sscanf(s.c_str(), "/dev/nbd%9[0-9]%c", digits, &c) != 1) { - fprintf(stderr, "invalid device name: %s, expected /dev/nbdN\n", cfg["dev_num"].as_string().c_str()); + fprintf(stderr, "invalid device name: %s, expected /dev/nbdN\n", s.c_str()); exit(1); } - return n; + return atoi(digits); } void request_stop() diff --git a/tests/test_img_online_resize.sh b/tests/test_img_online_resize.sh index e096d673..fdeda373 100755 --- a/tests/test_img_online_resize.sh +++ b/tests/test_img_online_resize.sh @@ -9,6 +9,14 @@ if ! (mount | grep '/dev type devtmpfs'); then sudo bash -c 'mount udev /dev/ -t devtmpfs && mount devpts /dev/pts -t devpts' fi +# A sign, a space or a number that overflows int must not resolve to /dev/nbd0 +for NAME in /dev/nbd+0 "/dev/nbd 0" /dev/nbd4294967296; do + OUT=$(sudo build/src/client/vitastor-nbd unmap "$NAME" 2>&1 || true) + if [[ "$OUT" != *"invalid device name"* ]]; then + format_error "unmap '$NAME' did not fail on the device name: $OUT" + fi +done + NBD_DEV=$(sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG map --image testimg) trap "sudo build/src/client/vitastor-nbd unmap $NBD_DEV"' || true; kill -9 $(jobs -p)' EXIT if sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG netlink-map $NBD_DEV --image testimg; then -- 2.47.3 From 708333820fced51ffdcb513e93c1a52e16fe9a71 Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Wed, 25 Nov 2026 22:49:27 +0300 Subject: [PATCH 2/2] Check NBD device numbers with a round trip instead of a scanset Parse the number with strtol and accept it only if std::to_string() gives back the same text: that refuses signs, spaces, leading zeros and numbers that overflow int. --- src/client/nbd_proxy.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/client/nbd_proxy.cpp b/src/client/nbd_proxy.cpp index 5d7c8cf7..27c58e92 100644 --- a/src/client/nbd_proxy.cpp +++ b/src/client/nbd_proxy.cpp @@ -431,15 +431,16 @@ public: } return -1; } - // Digits only: %d takes signs and spaces, and overflows into another device number - std::string s = cfg["dev_num"].as_string(); - char digits[10] = { 0 }, c = 0; - if (sscanf(s.c_str(), "%9[0-9]%c", digits, &c) != 1 && sscanf(s.c_str(), "/dev/nbd%9[0-9]%c", digits, &c) != 1) + std::string dev = cfg["dev_num"].as_string(); + std::string s = dev.substr(0, 8) == "/dev/nbd" ? dev.substr(8) : dev; + // The round trip refuses signs, spaces and overflows, which would name another device + int n = strtol(s.c_str(), NULL, 10); + if (n < 0 || std::to_string(n) != s) { - fprintf(stderr, "invalid device name: %s, expected /dev/nbdN\n", s.c_str()); + fprintf(stderr, "invalid device name: %s, expected /dev/nbdN\n", dev.c_str()); exit(1); } - return atoi(digits); + return n; } void request_stop() -- 2.47.3