Refuse to revive an NBD device with another image #155

Open
koolkhel wants to merge 5 commits from koolkhel/vitastor:nbd-backend-identifier into master
Contributor

Description

netlink-revive /dev/nbdN --image other silently attached a dead netlink device to another image: it exited 0, the device read the other image, and writes went there. A filesystem mounted from the device ends up corrupting both images.

The kernel already guards against this. A RECONFIGURE whose NBD_ATTR_BACKEND_IDENTIFIER differs from the one given on CONNECT fails with backend image doesn't match with ..., and one without it fails with must specify backend. But vitastor-nbd never sent the attribute. The code was under #ifdef NBD_ATTR_BACKEND_IDENTIFIER, which is an enum value in linux/nbd-netlink.h and never defined, and the caller passed NULL anyway.

This PR:

  • detects the attribute with check_c_source_compiles (HAVE_NBD_BACKEND_IDENTIFIER), since #ifdef can't see an enum value. Headers older than Linux 5.14 build without it, as before;
  • sends the image name with --image, and "<pool>:<inode>" with --pool --inode, on netlink-map and netlink-revive;
  • says so in the netlink-revive section of docs/usage/nbd.*.md;
  • adds tests/test_nbd_netlink_revive.sh: map an image, kill -9 the daemon, check that a revive with another image fails and one with the same image restores the data.

A live snapshot of a mapped image, then a server restart (fourth commit, Use the image name as the NBD backend identifier with --image). snap-create img@s moves the name img to a new inode, and the running vitastor-nbd follows the name. The first version of this PR sent "<pool>:<inode>" of the inode at map time. So after the snapshot, the kernel kept the old inode's identifier and refused every netlink-revive --image img with backend image doesn't match with 1:1, until netlink-unmap. Reproduced on kernel 6.8.

So the identifier now follows the way the device was mapped: the image name with --image, "<pool>:<inode>" with --pool --inode. This is the one-line switch the first version offered. The costs, also in the docs:

  • revive the device the way it was mapped: a device mapped with --image is not revived with --pool --inode of the same image, nor the other way round;
  • with --image the kernel compares the name, not the image: after a rename of a mapped image, the new name is refused, and the old name revives the device with whatever image has that name by then (fifth commit, docs only);
  • an image named like 1:5 passes for --pool 1 --inode 5.

Compatibility, checked on kernel 6.8:

  • A device mapped by an older vitastor-nbd (no identifier) can be revived by the new one. It stays unprotected until it is mapped again, because the kernel doesn't set the identifier on RECONFIGURE.
  • A device mapped by the new vitastor-nbd can't be revived by an older one: the kernel says must specify backend. I don't see a way around that short of not sending the identifier.

The second commit, Show netlink-revived devices in vitastor-nbd ls, is about the same command: after netlink-revive the device works, but ls printed nothing. ls took the device's server from /sys/block/nbdN/pid, which the kernel sets on CONNECT and keeps on RECONFIGURE, so after a revive it names the killed process. It also accepted only map and netlink-map command lines. Now a device whose sysfs pid isn't a running map/netlink-map is looked up among the running netlink-revive processes in /proc (only then, so ls doesn't scan /proc otherwise). The help and docs said netlink devices never show up in ls, which stopped being true when netlink-map was added there; they now say that the sysfs pid of a revived device stays the old one. The test checks ls --json after the revive.

The third commit fixes the kernel version in the docs (the attribute appeared in Linux 5.14, not 5.6; older kernels ignore it), logs vitastor-nbd in the test, and ends the test's wait for the killed daemon once it is a zombie: kill -0 succeeds on a zombie, and pid 1 of a container may never reap it.

Found: the wrong-image revive was reproduced on kernel 6.8 as described below; the empty ls after a revive was seen in a test run (dd from the revived device worked, ls printed nothing, /sys/block/nbdN/pid still had the killed pid); the snapshot case came up in a review of this PR and was then reproduced on kernel 6.8 as described above.

Tested as root on kernel 6.8 against a local 3-OSD cluster (tests/run_3osds.sh):

  • The new test passes, and fails on master with netlink-revive attached /dev/nbd0 of testimg to testimg2.
  • The test, with its new snap-create step, fails with the first three commits (netlink-revive ... --image testimg: backend image doesn't match with 1:1) and passes with the fourth, kernel 6.8.0-142.
  • Revive with another image: exit 1, backend image doesn't match with testimg. Revive with the same image by --image, also after a live snap-create: exit 0, the device reads the image.
  • A device mapped with --pool 1 --inode 1 --size ...: revive with --image testimg fails (backend image doesn't match with 1:1), revive with the same --pool --inode --size works and reads the image.
  • Old map with new revive, and new map with old revive, as described above.
  • map, netlink-map, busy devices, resize, revive after kill -9 and unmap work as before.
  • The ls check of the test fails with the first commit alone (ls doesn't show the revived /dev/nbd0) and passes with the second, kernel 6.8.0-142.

Contributor Name

Yury Luneff (MIND Software LLC, https://mindsw.io)

CLA

### Description `netlink-revive /dev/nbdN --image other` silently attached a dead netlink device to another image: it exited 0, the device read the other image, and writes went there. A filesystem mounted from the device ends up corrupting both images. The kernel already guards against this. A RECONFIGURE whose `NBD_ATTR_BACKEND_IDENTIFIER` differs from the one given on CONNECT fails with `backend image doesn't match with ...`, and one without it fails with `must specify backend`. But vitastor-nbd never sent the attribute. The code was under `#ifdef NBD_ATTR_BACKEND_IDENTIFIER`, which is an enum value in `linux/nbd-netlink.h` and never defined, and the caller passed `NULL` anyway. This PR: - detects the attribute with `check_c_source_compiles` (`HAVE_NBD_BACKEND_IDENTIFIER`), since `#ifdef` can't see an enum value. Headers older than Linux 5.14 build without it, as before; - sends the image name with `--image`, and `"<pool>:<inode>"` with `--pool --inode`, on `netlink-map` and `netlink-revive`; - says so in the `netlink-revive` section of `docs/usage/nbd.*.md`; - adds `tests/test_nbd_netlink_revive.sh`: map an image, `kill -9` the daemon, check that a revive with another image fails and one with the same image restores the data. **A live snapshot of a mapped image, then a server restart** (fourth commit, **Use the image name as the NBD backend identifier with --image**). `snap-create img@s` moves the name `img` to a new inode, and the running vitastor-nbd follows the name. The first version of this PR sent `"<pool>:<inode>"` of the inode at map time. So after the snapshot, the kernel kept the old inode's identifier and refused every `netlink-revive --image img` with `backend image doesn't match with 1:1`, until `netlink-unmap`. Reproduced on kernel 6.8. So the identifier now follows the way the device was mapped: the image name with `--image`, `"<pool>:<inode>"` with `--pool --inode`. This is the one-line switch the first version offered. The costs, also in the docs: - revive the device the way it was mapped: a device mapped with `--image` is not revived with `--pool --inode` of the same image, nor the other way round; - with `--image` the kernel compares the name, not the image: after a rename of a mapped image, the new name is refused, and the old name revives the device with whatever image has that name by then (fifth commit, docs only); - an image named like `1:5` passes for `--pool 1 --inode 5`. **Compatibility, checked on kernel 6.8:** - A device mapped by an older vitastor-nbd (no identifier) can be revived by the new one. It stays unprotected until it is mapped again, because the kernel doesn't set the identifier on RECONFIGURE. - A device mapped by the new vitastor-nbd can't be revived by an older one: the kernel says `must specify backend`. I don't see a way around that short of not sending the identifier. The second commit, **Show netlink-revived devices in vitastor-nbd ls**, is about the same command: after `netlink-revive` the device works, but `ls` printed nothing. `ls` took the device's server from `/sys/block/nbdN/pid`, which the kernel sets on CONNECT and keeps on RECONFIGURE, so after a revive it names the killed process. It also accepted only `map` and `netlink-map` command lines. Now a device whose sysfs pid isn't a running `map`/`netlink-map` is looked up among the running `netlink-revive` processes in `/proc` (only then, so `ls` doesn't scan `/proc` otherwise). The help and docs said netlink devices never show up in `ls`, which stopped being true when `netlink-map` was added there; they now say that the sysfs pid of a revived device stays the old one. The test checks `ls --json` after the revive. The third commit fixes the kernel version in the docs (the attribute appeared in Linux 5.14, not 5.6; older kernels ignore it), logs vitastor-nbd in the test, and ends the test's wait for the killed daemon once it is a zombie: `kill -0` succeeds on a zombie, and pid 1 of a container may never reap it. Found: the wrong-image revive was reproduced on kernel 6.8 as described below; the empty `ls` after a revive was seen in a test run (`dd` from the revived device worked, `ls` printed nothing, `/sys/block/nbdN/pid` still had the killed pid); the snapshot case came up in a review of this PR and was then reproduced on kernel 6.8 as described above. Tested as root on kernel 6.8 against a local 3-OSD cluster (`tests/run_3osds.sh`): - The new test passes, and fails on master with `netlink-revive attached /dev/nbd0 of testimg to testimg2`. - The test, with its new `snap-create` step, fails with the first three commits (`netlink-revive ... --image testimg`: `backend image doesn't match with 1:1`) and passes with the fourth, kernel 6.8.0-142. - Revive with another image: exit 1, `backend image doesn't match with testimg`. Revive with the same image by `--image`, also after a live `snap-create`: exit 0, the device reads the image. - A device mapped with `--pool 1 --inode 1 --size ...`: revive with `--image testimg` fails (`backend image doesn't match with 1:1`), revive with the same `--pool --inode --size` works and reads the image. - Old map with new revive, and new map with old revive, as described above. - `map`, `netlink-map`, busy devices, resize, revive after `kill -9` and `unmap` work as before. - The `ls` check of the test fails with the first commit alone (`ls doesn't show the revived /dev/nbd0`) and passes with the second, kernel 6.8.0-142. ### Contributor Name Yury Luneff (MIND Software LLC, https://mindsw.io) ### CLA - [x] I accept Vitastor CLA agreement: https://git.yourcmc.ru/vitalif/vitastor/src/branch/master/CLA-en.md
koolkhel added 1 commit 2026-11-22 22:40:14 +03:00
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 "<pool>:<inode>" 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.
koolkhel added 1 commit 2026-11-25 20:28:37 +03:00
ls listed a device only when /sys/block/nbdN/pid was a running map or
netlink-map process. The kernel sets that pid on CONNECT and keeps it on
RECONFIGURE, so after netlink-revive it names the dead process, and the
netlink-revive command wasn't accepted anyway: ls showed nothing for a
working device. Look such a device up among the running netlink-revive
processes in /proc.

The help and docs said netlink devices never show up in ls; say instead
that the sysfs pid of a revived device stays the old one.
koolkhel added 2 commits 2026-11-26 04:55:41 +03:00
NBD_ATTR_BACKEND_IDENTIFIER appeared in Linux 5.14, not 5.6. Earlier
kernels ignore the attribute, so the revive check does nothing there.

The test now logs vitastor-nbd, and stops waiting for the killed one
once it is a zombie: kill -0 succeeds on a zombie, and pid 1 of a
container may never reap it.
A snapshot moves the image name to a new inode, and vitastor-nbd follows
the name. With "<pool>:<inode>", a device mapped with --image could not
be revived after a live snapshot of its image: the kernel refused every
netlink-revive until netlink-unmap. Send the image name with --image and
keep "<pool>:<inode>" with --pool/--inode, so a device is revived the
way it was mapped.
koolkhel added 1 commit 2026-11-26 13:52:14 +03:00
With --image the kernel compares the name, not the image: after a
rename, a new image that takes the old name passes, and the revive
attaches it.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u nbd-backend-identifier:koolkhel-nbd-backend-identifier
git checkout koolkhel-nbd-backend-identifier
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: vitalif/vitastor#155
No description provided.