Accept only digits in NBD device numbers #160

Open
koolkhel wants to merge 2 commits from koolkhel/vitastor:nbd-dev-num-overflow into master
Contributor

Description

sscanf("%d") takes a sign and leading spaces, and silently overflows. So vitastor-nbd unmap /dev/nbd+0, unmap "/dev/nbd 0" and unmap /dev/nbd4294967296 all acted on /dev/nbd0 (and /dev/nbd4294967297 on /dev/nbd1). That is the fallback #120 removed for mistyped device names, reached another way.

This PR parses the device only in get_dev_num(), which is the only reader of dev_num: N or /dev/nbdN, where N must read back the same through std::to_string(strtol(N)), so signs, spaces, leading zeros and numbers that overflow int are refused. The positional argument is stored as given. A side effect: --dev_num /dev/nbd5 now works, which is the form the error message asks for.

tests/test_img_online_resize.sh now checks that unmap of /dev/nbd+0, /dev/nbd 0 and /dev/nbd4294967296 fails on the device name, before the test maps anything. On master it fails with /dev/nbd0 is not mapped: /sys/block/nbd0/pid does not exist.

Tested: /dev/nbd+0, /dev/nbd 0, /dev/nbd-1, /dev/nbd4294967296, /dev/nbd2147483648, /dev/nbd05, /dev/nbd0x, nbd0, /dev/nbd, udev and --dev_num +7 fail with invalid device name. /dev/nbd0, /dev/nbd5, 5, --dev_num 7, --dev_num /dev/nbd5 and /dev/nbd2147483647 reach the device.

Contributor Name

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

CLA

### Description `sscanf("%d")` takes a sign and leading spaces, and silently overflows. So `vitastor-nbd unmap /dev/nbd+0`, `unmap "/dev/nbd 0"` and `unmap /dev/nbd4294967296` all acted on `/dev/nbd0` (and `/dev/nbd4294967297` on `/dev/nbd1`). That is the fallback #120 removed for mistyped device names, reached another way. This PR parses the device only in `get_dev_num()`, which is the only reader of `dev_num`: `N` or `/dev/nbdN`, where `N` must read back the same through `std::to_string(strtol(N))`, so signs, spaces, leading zeros and numbers that overflow `int` are refused. The positional argument is stored as given. A side effect: `--dev_num /dev/nbd5` now works, which is the form the error message asks for. `tests/test_img_online_resize.sh` now checks that `unmap` of `/dev/nbd+0`, `/dev/nbd 0` and `/dev/nbd4294967296` fails on the device name, before the test maps anything. On master it fails with `/dev/nbd0 is not mapped: /sys/block/nbd0/pid does not exist`. Tested: `/dev/nbd+0`, `/dev/nbd 0`, `/dev/nbd-1`, `/dev/nbd4294967296`, `/dev/nbd2147483648`, `/dev/nbd05`, `/dev/nbd0x`, `nbd0`, `/dev/nbd`, `udev` and `--dev_num +7` fail with `invalid device name`. `/dev/nbd0`, `/dev/nbd5`, `5`, `--dev_num 7`, `--dev_num /dev/nbd5` and `/dev/nbd2147483647` reach the device. ### 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-23 12:59:33 +03:00
sscanf("%d") silently wraps a number too big for int, so
"vitastor-nbd unmap /dev/nbd4294967296" acted on /dev/nbd0 and
4294967297 on /dev/nbd1: the fallback that #120 removed for
mistyped device names. Limit the device number to 9 digits.
koolkhel changed title from Reject NBD device numbers that overflow int to Accept only digits in NBD device numbers 2026-11-23 18:07:40 +03:00
koolkhel force-pushed nbd-dev-num-overflow from fd5aa1c7da to 2e289d4fc5 2026-11-23 18:07:40 +03:00 Compare
vitalif requested changes 2026-11-25 21:20:46 +03:00
vitalif left a comment
Owner

Если так хочется проверять что нет переполнения, ну сделай что-то простое, типа std::to_string(n) == str

Если так хочется проверять что нет переполнения, ну сделай что-то простое, типа std::to_string(n) == str
koolkhel added 1 commit 2026-11-26 04:21:43 +03:00
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.
Author
Contributor

Сделал как предложено (70833382): strtol и проверка std::to_string(n) == s; /dev/nbd+0, "/dev/nbd 0" и /dev/nbd4294967296 по-прежнему отвергаются, тест тот же.

Сделал как предложено (70833382): strtol и проверка std::to_string(n) == s; /dev/nbd+0, "/dev/nbd 0" и /dev/nbd4294967296 по-прежнему отвергаются, тест тот же.
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-dev-num-overflow:koolkhel-nbd-dev-num-overflow
git checkout koolkhel-nbd-dev-num-overflow
Sign in to join this conversation.
No Reviewers
No Label
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: vitalif/vitastor#160
No description provided.