Skip to contentSkip to conversation

Allowed characters in strings passed to/from device-mapper APIs

DISCUSSION Zack Weinberg 4 participants 7 messages Mar 9, 2025 – Mar 10, 2025 Device mapper

lore
Share & tools
lore
#0 lore raw

I am developing a device-mapper client library in Rust. The language is relevant here because Rust's standard library makes a strong distinction between strings that are known to be encoded in UTF-8 and strings whose encoding is not known (str vs OsStr), with the latter being awkward to use compared to the former.

The device-mapper ioctls use a lot of strings, and it's not clear to me from either the documentation or the kernel's source code what expectations the *kernel* has of the character encoding of these strings. I don't expect the kernel to care about UTF-8 versus ISO-8859-n or whatever, but it is plausible to me that at least some of the strings used in device-mapper ioctls can be expected always to be ASCII, in which case I can safely use str for them on the library side and it'll be more ergonomic for end users.

There are, I think, four different kinds of strings used in device- mapper ioctls: target names, device names and UUIDs, table parameters, and target messages. For each of these, I would like to ask:

* Are there any circumstances where non-ASCII bytes *should* be used in this type of string *when supplied to the kernel*?

* Are there any circumstances where a client library should expect non-ASCII bytes to *appear* in this type of string *when reported back to the client by the kernel*?

* If there are any such circumstances (either way), does the kernel try to interpret non-ASCII bytes in this type of string as characters in a specific encoding, or does it just treat them as an opaque token (as is typical e.g. for pathname components)?

Thanks,
zw

please cc: me directly on all responses, I'm not subscribed to the list

#1 lore raw

Dne 09. 03. 25 v 17:00 Zack Weinberg napsal(a):

Quoted 6 lines

I am developing a device-mapper client library in Rust. The language is relevant here because Rust's standard library makes a strong distinction between strings that are known to be encoded in UTF-8 and strings whose encoding is not known (str vs OsStr), with the latter being awkward to use compared to the former.

Quoted 8 lines

The device-mapper ioctls use a lot of strings, and it's not clear to me from either the documentation or the kernel's source code what expectations the *kernel* has of the character encoding of these strings. I don't expect the kernel to care about UTF-8 versus ISO-8859-n or whatever, but it is plausible to me that at least some of the strings used in device-mapper ioctls can be expected always to be ASCII, in which case I can safely use str for them on the library side and it'll be more ergonomic for end users.

Hi

While you could surely interface kernel directly through ioctl(),
you should probably base your tool around libdevmapper as this library
is there to solve version differences or possible kernel or udev
issue that pops up here and there - if you will go your own path,
you will need to reimplement these things within Rust - which
might potentially be problematic - as we are solving i.e. udev trouble
to be working only within our 'small world' across different version of kernel and udev version installed on the system.

Quoted 4 lines

There are, I think, four different kinds of strings used in device- mapper ioctls: target names, device names and UUIDs, table parameters, and target messages. For each of these, I would like to ask:

Device name and UUID are always plain 7bit ASCII.
But there is a catch in udev process - as udev has even further limited char subset usable for device names - libdevmapper uses name mangling for those.

Again - you are free to reimplement your Rust version - but eventually this may lead to a completely different set of udev rules needed for management of Rust devices - as libdevmapper is free to 'change' DM udev rules as long as we are 'internally' consistent - as these rules are seen as 'internal' property for DM world.

Regards

Zdenek

#2 lore raw

This is the Rust library developed by the Stratis project, and I use it in a couple of my projects (blk-archive and thin-provisioning-tools).

https://github.com/stratis-storage/devicemapper-rs

- Joe

Quoted 48 lines · Zdenek Kabelac

Dne 09. 03. 25 v 17:00 Zack Weinberg napsal(a):

Quoted 6 lines

I am developing a device-mapper client library in Rust. The language is relevant here because Rust's standard library makes a strong distinction between strings that are known to be encoded in UTF-8 and strings whose encoding is not known (str vs OsStr), with the latter being awkward to use compared to the former.

Quoted 8 lines

The device-mapper ioctls use a lot of strings, and it's not clear to me from either the documentation or the kernel's source code what expectations the *kernel* has of the character encoding of these strings. I don't expect the kernel to care about UTF-8 versus ISO-8859-n or whatever, but it is plausible to me that at least some of the strings used in device-mapper ioctls can be expected always to be ASCII, in which case I can safely use str for them on the library side and it'll be more ergonomic for end users.

Hi

While you could surely interface kernel directly through ioctl(), you should probably base your tool around libdevmapper as this library is there to solve version differences or possible kernel or udev issue that pops up here and there - if you will go your own path, you will need to reimplement these things within Rust - which might potentially be problematic - as we are solving i.e. udev trouble to be working only within our 'small world' across different version of kernel and udev version installed on the system.

Quoted 4 lines

There are, I think, four different kinds of strings used in device- mapper ioctls: target names, device names and UUIDs, table parameters, and target messages. For each of these, I would like to ask:

Device name and UUID are always plain 7bit ASCII.
But there is a catch in udev process - as udev has even further limited char subset usable for device names - libdevmapper uses name mangling for those.

Again - you are free to reimplement your Rust version - but eventually this may lead to a completely different set of udev rules needed for management of Rust devices - as libdevmapper is free to 'change' DM udev rules as long as we are 'internally' consistent - as these rules are seen as 'internal' property for DM world.

Regards

Zdenek

#3 lore raw
Quoted 3 lines · Joe Thornber

This is the Rust library developed by the Stratis project, and I use it in a couple of my projects (blk-archive and thin-provisioning-tools).

I am aware of this library and in fact have stolen a bunch of code from it, but I can't use it directly for the same reason I can't use libdevmapper: my library needs to work in a context where the base C library is not available and neither is udev.

zw

#4 lore raw
Quoted 9 lines · Zack Weinberg
Quoted 3 lines · Joe Thornber

This is the Rust library developed by the Stratis project, and I use it in a couple of my projects (blk-archive and thin-provisioning-tools).

I am aware of this library and in fact have stolen a bunch of code from it, but I can't use it directly for the same reason I can't use libdevmapper: my library needs to work in a context where the base C library is not available and neither is udev.

Oh, also, stealing code from that library doesn't resolve the questions I came here to ask; in fact it's what brought the issue to my attention in the first place. The Stratis people seem to have *blindly assumed* that *all* C-strings passed to or from the device-mapper ioctls would always be printable ASCII, and I do not trust that assumption, particularly for table parameters and target messages.

zw

#5 lore raw
In reply to Zdenek Kabelac
Quoted 2 lines · Zdenek Kabelac

Dne 09. 03. 25 v 17:00 Zack Weinberg napsal(a):

Quoted 1 line

I am developing a device-mapper client library in Rust.

[...]

Quoted 8 lines

While you could surely interface kernel directly through ioctl(), you should probably base your tool around libdevmapper as this library is there to solve version differences or possible kernel or udev issue that pops up here and there - if you will go your own path, you will need to reimplement these things within Rust - which might potentially be problematic - as we are solving i.e. udev trouble to be working only within our 'small world' across different version of kernel and udev version installed on the system.

Unfortunately this is not an option for me, as my library needs to be usable in a context where neither the base C library nor udev is available.

Quoted 5 lines

Again - you are free to reimplement your Rust version - but eventually this may lead to a completely different set of udev rules needed for management of Rust devices - as libdevmapper is free to 'change' DM udev rules as long as we are 'internally' consistent - as these rules are seen as 'internal' property for DM world.

I don't immediately see how that could happen, can you be more specific about the potential problems you have in mind?

Quoted 1 line

Device name and UUID are always plain 7bit ASCII.

Is that enforced by the kernel, and if so, where? I was not able to find any code in the kernel that does that, but I could easily have missed it.

Quoted 2 lines

But there is a catch in udev process - as udev has even further limited char subset usable for device names - libdevmapper uses name mangling for those.

Thanks, this is helpful. Do you happen to know offhand where udev documents its character restrictions?

How about target names, table parameters, and target message strings? What are the restrictions on those?

zw

maintainer
#6 lore raw
In reply to Zack Weinberg

Hi

Quoted 15 lines · Zack Weinberg

I am developing a device-mapper client library in Rust. The language is relevant here because Rust's standard library makes a strong distinction between strings that are known to be encoded in UTF-8 and strings whose encoding is not known (str vs OsStr), with the latter being awkward to use compared to the former.

The device-mapper ioctls use a lot of strings, and it's not clear to me from either the documentation or the kernel's source code what expectations the *kernel* has of the character encoding of these strings. I don't expect the kernel to care about UTF-8 versus ISO-8859-n or whatever, but it is plausible to me that at least some of the strings used in device-mapper ioctls can be expected always to be ASCII, in which case I can safely use str for them on the library side and it'll be more ergonomic for end users.

Quoted 6 lines

There are, I think, four different kinds of strings used in device- mapper ioctls: target names, device names and UUIDs, table parameters, and target messages. For each of these, I would like to ask:

* Are there any circumstances where non-ASCII bytes *should* be used in this type of string *when supplied to the kernel*?

I think there's no reason to use non-ASCII characters in device mapper.

Quoted 3 lines

* Are there any circumstances where a client library should expect non-ASCII bytes to *appear* in this type of string *when reported back to the client by the kernel*?

If the user creates a device name with non-ascii characters, it will be reported as such in the kernel ioctls.

Quoted 4 lines

* If there are any such circumstances (either way), does the kernel try to interpret non-ASCII bytes in this type of string as characters in a specific encoding, or does it just treat them as an opaque token (as is typical e.g. for pathname components)?

The kernel just passes non-ascii characters as opaque tokens. It doesn't attempt to interpret them.

But udev mangles non-ascii characters - i.e. if you create
dmsetup create ěščřžýáíé --table '0 3951256 zero'
you get
ls -la /dev/mapper/ '\xc4\x9b\xc5\xa1\xc4\x8d\xc5\x99\xc5\xbe\xc3\xbd\xc3\xa1\xc3\xad\xc3\xa9' -> ../dm-0 ls -al /dev/disk/by-id/ 'dm-name-\xc4\x9b\xc5\xa1\xc4\x8d\xc5\x99\xc5\xbe\xc3\xbd\xc3\xa1\xc3\xad\xc3\xa9' -> ../../dm-0

dmsetup table will show
ěščřžýáíé: 0 3951256 zero

dmsetup remove ěščřžýáíé will remove the device

Mikulas

Quoted 5 lines

Thanks,
zw

please cc: me directly on all responses, I'm not subscribed to the list