From 0ee5df904d267d9f439b48f89853005730411e3b Mon Sep 17 00:00:00 2001 From: Yury Luneff Date: Sun, 22 Nov 2026 22:29:09 +0300 Subject: [PATCH] Refuse to revive an NBD device with another image netlink-revive /dev/nbdN --image other silently attached the device to another image, so a filesystem mounted from it read and wrote that image. The kernel refuses a RECONFIGURE whose backend identifier differs from the one given on CONNECT, but vitastor-nbd never sent one: the code sat under #ifdef NBD_ATTR_BACKEND_IDENTIFIER, which is an enum value and never defined, and the caller passed NULL anyway. Detect the attribute with CMake and send ":" on map and revive. It survives an image rename and is the same for --image and --pool/--inode. A device mapped without an identifier can still be revived, unprotected. A device mapped with one can't be revived by an older vitastor-nbd, which sends none. --- .gitea/workflows/test.yml | 18 ++++++++++++++++++ docs/usage/nbd.en.md | 2 ++ docs/usage/nbd.ru.md | 2 ++ src/CMakeLists.txt | 3 +++ src/client/CMakeLists.txt | 3 +++ src/client/nbd_proxy.cpp | 7 +++++-- tests/run_tests.sh | 1 + tests/test_nbd_netlink_revive.sh | 30 ++++++++++++++++++++++++++++++ 8 files changed, 64 insertions(+), 2 deletions(-) create mode 100755 tests/test_nbd_netlink_revive.sh diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index ad781d0c..5f80dd77 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -347,6 +347,24 @@ jobs: echo "" done + test_nbd_netlink_revive: + runs-on: ubuntu-latest + needs: build + container: ${{env.TEST_IMAGE}}:${{github.sha}} + steps: + - name: Run test + id: test + timeout-minutes: 3 + run: /root/vitastor/tests/test_nbd_netlink_revive.sh + - name: Print logs + if: always() && steps.test.outcome == 'failure' + run: | + for i in /root/vitastor/testdata/*.log /root/vitastor/testdata/*.txt; do + echo "-------- $i --------" + cat $i + echo "" + done + test_no_uring: runs-on: ubuntu-latest needs: build diff --git a/docs/usage/nbd.en.md b/docs/usage/nbd.en.md index 8d636661..b46fdfa5 100644 --- a/docs/usage/nbd.en.md +++ b/docs/usage/nbd.en.md @@ -133,3 +133,5 @@ vitastor-nbd netlink-revive /dev/nbdX (--image | --pool --inode < ``` Restart a dead NBD netlink-mapped device without removing it. Supports the same options as `netlink-map`. +The kernel refuses to revive the device with another image than `netlink-map` used (Linux 5.6+), +and an older `vitastor-nbd` can't revive a device mapped by this version. diff --git a/docs/usage/nbd.ru.md b/docs/usage/nbd.ru.md index f67f1d0e..c0edcfe9 100644 --- a/docs/usage/nbd.ru.md +++ b/docs/usage/nbd.ru.md @@ -143,3 +143,5 @@ vitastor-nbd netlink-revive /dev/nbdX (--image | --pool --inode < Оживить мёртвое NBD-устройство, ранее подключённое через netlink, без удаления. Поддерживает те же опции, что и `netlink-map`. +Ядро не даст оживить устройство с другим образом, чем при `netlink-map` (Linux 5.6+), а более +старая версия `vitastor-nbd` не оживит устройство, подключённое этой версией. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9b394287..a9d50765 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,6 +4,7 @@ project(vitastor) include(GNUInstallDirs) include(CheckIncludeFile) +include(CheckCSourceCompiles) find_package(PkgConfig) @@ -68,6 +69,8 @@ macro(install_symlink _filepath _sympath) endmacro(install_symlink) check_include_file("linux/nbd-netlink.h" HAVE_NBD_NETLINK_H) +# NBD_ATTR_BACKEND_IDENTIFIER is an enum value, so #ifdef can't detect it +check_c_source_compiles("#include \nint main() { return NBD_ATTR_BACKEND_IDENTIFIER; }" HAVE_NBD_BACKEND_IDENTIFIER) find_package(PkgConfig) if (${WITH_QEMU}) diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index ee674094..c99acd27 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -95,6 +95,9 @@ target_include_directories(vitastor-nbd PUBLIC ${NL3_INCLUDE_DIRS}) target_link_libraries(vitastor-nbd vitastor_client ${NL3_LIBRARIES}) if (HAVE_NBD_NETLINK_H AND NL3_LIBRARIES) target_compile_definitions(vitastor-nbd PUBLIC HAVE_NBD_NETLINK_H) + if (HAVE_NBD_BACKEND_IDENTIFIER) + target_compile_definitions(vitastor-nbd PUBLIC HAVE_NBD_BACKEND_IDENTIFIER) + endif (HAVE_NBD_BACKEND_IDENTIFIER) endif (HAVE_NBD_NETLINK_H AND NL3_LIBRARIES) # vitastor-ublk diff --git a/src/client/nbd_proxy.cpp b/src/client/nbd_proxy.cpp index e49c7322..3e5a70e2 100644 --- a/src/client/nbd_proxy.cpp +++ b/src/client/nbd_proxy.cpp @@ -165,7 +165,7 @@ static int netlink_configure(const int *sockfd, int sock_size, int dev_num, uint NLA_PUT_U64(msg, NBD_ATTR_DEAD_CONN_TIMEOUT, conn_timeout); } -#ifdef NBD_ATTR_BACKEND_IDENTIFIER +#ifdef HAVE_NBD_BACKEND_IDENTIFIER if (backend) { // Backend is an attribute useful for identication of the device @@ -652,7 +652,10 @@ help: if (!cfg["nbd_disconnect_on_close"].is_null()) cflags |= NBD_CFLAG_DISCONNECT_ON_CLOSE; #endif - int err = netlink_configure(sockfd + 1, 1, dev_num, device_size, 4096, flags, cflags, nbd_timeout, nbd_conn_timeout, NULL, revive); + // pool:inode rather than the name: it survives a rename and is the same for --image and --pool/--inode + uint64_t backend_inode = inode ? inode : watch->cfg.num; + std::string backend = std::to_string(INODE_POOL(backend_inode))+":"+std::to_string(INODE_NO_POOL(backend_inode)); + int err = netlink_configure(sockfd + 1, 1, dev_num, device_size, 4096, flags, cflags, nbd_timeout, nbd_conn_timeout, backend.c_str(), revive); if (err < 0) { errno = (err == -NLE_BUSY ? EBUSY : EIO); diff --git a/tests/run_tests.sh b/tests/run_tests.sh index caac41b7..5f521d12 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -28,6 +28,7 @@ ETCD_SCHEME=https ANTIETCD=1 ./test_etcd_fail.sh ETCD_SCHEME=https ./test_snapshot.sh ./test_img_online_resize.sh +./test_nbd_netlink_revive.sh ./test_no_uring.sh diff --git a/tests/test_nbd_netlink_revive.sh b/tests/test_nbd_netlink_revive.sh new file mode 100755 index 00000000..1743a797 --- /dev/null +++ b/tests/test_nbd_netlink_revive.sh @@ -0,0 +1,30 @@ +#!/bin/bash -ex + +. `dirname $0`/run_3osds.sh + +$VITASTOR_CLI create -s 16M testimg +$VITASTOR_CLI create -s 16M testimg2 +dd if=/dev/urandom of=./testdata/img1.bin bs=4096 count=1 +$VITASTOR_CLI dd if=./testdata/img1.bin oimg=testimg bs=4096 count=1 + +if ! (mount | grep '/dev type devtmpfs'); then + sudo bash -c 'mount udev /dev/ -t devtmpfs && mount devpts /dev/pts -t devpts' +fi + +NBD_DEV=$(sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG netlink-map --image testimg) +trap "sudo build/src/client/vitastor-nbd netlink-unmap $NBD_DEV"' || true; kill -9 $(jobs -p)' EXIT + +# Kill the daemon: the device stays and waits for netlink-revive +NBD_PID=$(cat /sys/block/${NBD_DEV#/dev/}/pid) +sudo kill -9 $NBD_PID +while sudo kill -0 $NBD_PID 2>/dev/null; do sleep 0.1; done + +if sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG netlink-revive $NBD_DEV --image testimg2; then + format_error "netlink-revive attached $NBD_DEV of testimg to testimg2" +fi + +sudo build/src/client/vitastor-nbd --config_path $VITASTOR_CFG netlink-revive $NBD_DEV --image testimg +sudo dd if=$NBD_DEV of=./testdata/img1.read bs=4096 count=1 iflag=direct +cmp ./testdata/img1.bin ./testdata/img1.read + +format_green OK -- 2.47.3