Skip to contentSkip to conversation

ndctl: fix option table termination and multi-ID filters

PATCH Applied YeeLi 2 participants 5 messages Nov 17 – Nov 19 libnvdimm: non-volatile memory device

lore
Share & tools
lore

YeeLi

author
#0 lore raw

cover ndctl: fix option table termination and multi-ID filters

From: yeeli <[email protected]>

This series fixes two small bugs found while reviewing ndctl v85.

Patch 1 adds the missing OPT_END() to key_options[]. Without it the option parser walks past the end of the array and interprets the adjacent sanitize_options[] array, so setup-passphrase lists and accepts options that belong to sanitize-dimm.

Patch 2 makes util_bus_filter(), util_region_filter() and util_dimm_filter() parse the current strtok_r() token instead of the start of the duplicated identifier string. Numeric lists such as "0 1" therefore only matched their first entry.

Both issues were reproduced with the commands and harnesses described in the individual patches. The fixes were built and tested on an x86 test system.

AI disclosure: the bug analysis, reproducers and initial fixes were produced with the help of an AI coding assistant (DeepSeek V4.1 Flash, running in OpenCode). The prompts asked for a review of ndctl v85 for reproducible bugs, then for fixes to the option-table termination and filter token parsing. The assistant identified the missing OPT_END(), wrote a harness for the filter bug, and proposed both changes. The submitter reviewed and verified the changes and added the Signed-off-by tags; the assistant did not add them.

yeeli (2):
ndctl/dimm: add missing OPT_END() to key_options ndctl/filter: parse the current token in util_*_filter

 ndctl/dimm.c   |  1 +
 ndctl/filter.c | 12 ++++++------
 2 files changed, 7 insertions(+), 6 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 ndctl/dimm: add missing OPT_END() to key_options

From: yeeli <[email protected]>

key_options[] is used by setup-passphrase and update-passphrase, but it is not terminated with OPT_END(). The option parser walks the array until it finds an OPTION_END entry, so it runs past the end of key_options[] and interprets the adjacent sanitize_options[] array.

Before the fix, --crypto-erase is silently accepted and the command prints the bogus usage:

    $ ndctl setup-passphrase --crypto-erase
    usage: ndctl setup-passphrase <nmem0> [<nmem1>..<nmemN>] [<options>]

-b, --bus <bus-id> <nmem> must be on a bus with an
id/provider of <bus-id>
-v, --verbose turn on debug
-k, --key-handle <key-handle>
master encryption key handle
-m, --master-passphrase
use master passphrase
-b, --bus <bus-id> <nmem> must be on a bus with an
id/provider of <bus-id>
-v, --verbose turn on debug
-c, --crypto-erase crypto erase a dimm
-o, --overwrite overwrite a dimm
-z, --zero-key pass in a zero key
-m, --master-passphrase
use master passphrase

After the fix, the same command rejects the invalid option and prints only the valid options:

    $ ndctl setup-passphrase --crypto-erase
      Error: unknown option `crypto-erase'

usage: ndctl setup-passphrase <nmem0> [<nmem1>..<nmemN>] [<options>]

-b, --bus <bus-id> <nmem> must be on a bus with an
id/provider of <bus-id>
-v, --verbose turn on debug
-k, --key-handle <key-handle>
master encryption key handle
-m, --master-passphrase
use master passphrase

An AddressSanitizer build reports a global-buffer-overflow immediately after key_options[].

Terminate key_options[] with OPT_END() so parsing stops at the end of the array.

Tested by building ndctl on the target test system: both passphrase commands now show only the four expected options, and --crypto-erase is rejected as an unknown option.

Fixes 374369ac2f67 ("ndctl: master phassphrase management support")
---
 ndctl/dimm.c | 1 +
 1 file changed, 1 insertion(+)
ndctl/dimm.c +1 -0
index 533ab04..bae7782 100644
@@ -1276,6 +1276,7 @@ static const struct option key_options[] = {
 	BASE_OPTIONS(),
 	KEY_OPTIONS(),
 	MASTER_OPTIONS(),
+	OPT_END(),
 };
 
 static const struct option sanitize_options[] = {
Signature · 1 line
2.34.1
maintainer Applied
#2 lore raw

Re: [ndctl PATCH 1/2] ndctl/dimm: add missing OPT_END() to key_options

Quoted 51 lines · YeeLi

From: yeeli <[email protected]>

key_options[] is used by setup-passphrase and update-passphrase, but it is not terminated with OPT_END(). The option parser walks the array until it finds an OPTION_END entry, so it runs past the end of key_options[] and interprets the adjacent sanitize_options[] array.

Before the fix, --crypto-erase is silently accepted and the command prints the bogus usage:

    $ ndctl setup-passphrase --crypto-erase
    usage: ndctl setup-passphrase <nmem0> [<nmem1>..<nmemN>] [<options>]

-b, --bus <bus-id> <nmem> must be on a bus with an
id/provider of <bus-id>
-v, --verbose turn on debug
-k, --key-handle <key-handle>
master encryption key handle
-m, --master-passphrase
use master passphrase
-b, --bus <bus-id> <nmem> must be on a bus with an
id/provider of <bus-id>
-v, --verbose turn on debug
-c, --crypto-erase crypto erase a dimm
-o, --overwrite overwrite a dimm
-z, --zero-key pass in a zero key
-m, --master-passphrase
use master passphrase

After the fix, the same command rejects the invalid option and prints only the valid options:

    $ ndctl setup-passphrase --crypto-erase
      Error: unknown option `crypto-erase'

usage: ndctl setup-passphrase <nmem0> [<nmem1>..<nmemN>] [<options>]

-b, --bus <bus-id> <nmem> must be on a bus with an
id/provider of <bus-id>
-v, --verbose turn on debug
-k, --key-handle <key-handle>
master encryption key handle
-m, --master-passphrase
use master passphrase

An AddressSanitizer build reports a global-buffer-overflow immediately after key_options[].

Terminate key_options[] with OPT_END() so parsing stops at the end of the array.

Quoted 6 lines

Tested by building ndctl on the target test system: both passphrase commands now show only the four expected options, and --crypto-erase is rejected as an unknown option.

Fixes 374369ac2f67 ("ndctl: master phassphrase management support")

Thanks for the patch!

Applied to pending for NDCTL v86
https://github.com/pmem/ndctl/commits/pending/

I made two updates to the commit log when applying:
[ as: fix Fixes tag, describe the overrun as build dependent ]

Fixes tag: 374369ac2f67 only added MASTER_OPTIONS() to key_options[], which was already unterminated by then. The terminator was lost in 8e4193885357 ("ndctl: add an overwrite option to 'sanitize-dimm'"), when sanitize_options[] was split out of key_options[] and took the OPT_END() with it.

Overrun impact: what follows key_options[] in memory depends on how the binary was built. It may be sanitize_options[], as in your example, or unrelated data, in which case "setup-passphrase --crypto-erase", "-h" and "--list-opts" segfault instead of printing the bogus usage. The log now says the parser reads whatever follows the array, and labels the example output as coming from a build where sanitize_options[] follows.

-- Alison

Quoted 1 line
author
#3 lore raw

PATCH 2/2 ndctl/filter: parse the current token in util_*_filter

In reply to YeeLi

From: yeeli <[email protected]>

util_bus_filter(), util_region_filter() and util_dimm_filter() split the identifier string with strtok_r(), but then parse "ident" instead of the current token "name" with strtoul(). ident is the start of the strdup()ed buffer, so every iteration parses the first token again.

A numeric list therefore only matches its first entry. For example, with a dimm whose id is 1:

    util_dimm_filter(dimm, "0 1")    -> no match
    util_dimm_filter(dimm, "1")      -> match

The same happens for --bus, --region and --dimm lists; name lists such as "nmem0 nmem1" still work because they take the string comparison path.

Reproduce with a small harness that calls util_dimm_filter() with a dimm named nmem1 and id 1:

    "0 1"    -> no match (expected match)
    "0 2 1"  -> no match (expected match)

Parse the current token with strtoul(name, ...), and compare the end pointer with name when checking for a failed conversion.

Tested on the target test system with harnesses for all three
functions: the original code fails for "0 1" and "1 0" as expected, and the patched code matches for numeric lists, hex ids, name lists, "all" and rejects trailing junk.

The bus and region helpers have had the same bug since 56b6e85 ("ndctl: refactor bus and region filtering to utility routines").

Fixes 4c9185d60c0b ("ndctl, list: add 'filter by dimm' capability")
---
 ndctl/filter.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
ndctl/filter.c +6 -6
index 5b810dc..e6e9aba 100644
@@ -31,8 +31,8 @@ struct ndctl_bus *util_bus_filter(struct ndctl_bus *bus, const char *__ident)
 		if (strcmp(name, "all") == 0)
 			break;
 
-		bus_id = strtoul(ident, &end, 0);
-		if (end == ident || end[0])
+		bus_id = strtoul(name, &end, 0);
+		if (end == name || end[0])
 			bus_id = ULONG_MAX;
 
 		provider = ndctl_bus_get_provider(bus);
@@ -72,8 +72,8 @@ struct ndctl_region *util_region_filter(struct ndctl_region *region,
 		if (strcmp(name, "all") == 0)
 			break;
 
-		region_id = strtoul(ident, &end, 0);
-		if (end == ident || end[0])
+		region_id = strtoul(name, &end, 0);
+		if (end == name || end[0])
 			region_id = ULONG_MAX;
 
 		region_name = ndctl_region_get_devname(region);
@@ -146,8 +146,8 @@ struct ndctl_dimm *util_dimm_filter(struct ndctl_dimm *dimm,
 		if (strcmp(name, "all") == 0)
 			break;
 
-		dimm_id = strtoul(ident, &end, 0);
-		if (end == ident || end[0])
+		dimm_id = strtoul(name, &end, 0);
+		if (end == name || end[0])
 			dimm_id = ULONG_MAX;
 
 		dimm_name = ndctl_dimm_get_devname(dimm);
Signature · 1 line
2.34.1
maintainer Applied
#4 lore raw

Re: [ndctl PATCH 2/2] ndctl/filter: parse the current token in util_*_filter

Quoted 32 lines · YeeLi

From: yeeli <[email protected]>

util_bus_filter(), util_region_filter() and util_dimm_filter() split the identifier string with strtok_r(), but then parse "ident" instead of the current token "name" with strtoul(). ident is the start of the strdup()ed buffer, so every iteration parses the first token again.

A numeric list therefore only matches its first entry. For example, with a dimm whose id is 1:

    util_dimm_filter(dimm, "0 1")    -> no match
    util_dimm_filter(dimm, "1")      -> match

The same happens for --bus, --region and --dimm lists; name lists such as "nmem0 nmem1" still work because they take the string comparison path.

Reproduce with a small harness that calls util_dimm_filter() with a dimm named nmem1 and id 1:

    "0 1"    -> no match (expected match)
    "0 2 1"  -> no match (expected match)

Parse the current token with strtoul(name, ...), and compare the end pointer with name when checking for a failed conversion.

Tested on the target test system with harnesses for all three
functions: the original code fails for "0 1" and "1 0" as expected, and the patched code matches for numeric lists, hex ids, name lists, "all" and rejects trailing junk.

Quoted 6 lines

The bus and region helpers have had the same bug since 56b6e85 ("ndctl: refactor bus and region filtering to utility routines").

Fixes 4c9185d60c0b ("ndctl, list: add 'filter by dimm' capability")

---

Thanks for the patch!

Applied to pending for NDCTL v86
https://github.com/pmem/ndctl/commits/pending/

I updated the commit log upon applying with this note:
[ as: updates to commit log - fixes tag, drop 56b6e85 note, add
names-after-a-number impact ]

Fixes tag: before c70adc3cf6bf ("ndctl, filter: refacor util_<obj>_filter() to support multiple space-seperated arguments"), each filter took a single identifier, so strtoul(ident) was correct. That commit added the strtok_r() loop to all three functions and kept parsing ident, so it introduced the bug, and one tag covers bus, region and dimm. For the same reason I dropped the paragraph saying the bus and region helpers had the bug since 56b6e85.

Names after a number: once the first token is numeric, every later token is compared against that number, so a name that follows it never matches either. For example, "ndctl list -D -d '14 nmem15'" lists only nmem14. I added a sentence about this to the impact paragraph.

-- Alison