Skip to contentSkip to conversation

rust: auxiliary: validate DeviceId name length

PATCH Superseded Georgios Androutsopoulos 6 participants 8 messages Sep 9

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: auxiliary: validate DeviceId name length

DeviceId::new() copies modname and name into the fixed 40-byte auxiliary_device_id::name array without checking that they fit. An oversized name is caught by the array bounds check, but the error reports an out-of-bounds index in the copy loop rather than the constraint the caller violated.

Check the invariant explicitly instead, so the failure states the length limit rather than an array index.

In a constant context exceeding the limit leads to a build error; at runtime it panics, so add a # Panics section for it.

Fixes ce735e73dd59 ("rust: auxiliary: add auxiliary device / driver abstractions")
---
 rust/kernel/auxiliary.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)
rust/kernel/auxiliary.rs +10 -0
index 60dfbec8f330..1f3ba86d6d96 100644
@@ -137,10 +137,20 @@ macro_rules! module_auxiliary_driver {
 
 impl DeviceId {
     /// Create a new [`DeviceId`] from name.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the combined module and device name, including the
+    /// separator and trailing NUL, exceeds `AUXILIARY_NAME_SIZE` bytes.
     pub const fn new(modname: &'static CStr, name: &'static CStr) -> Self {
         let name = name.to_bytes_with_nul();
         let modname = modname.to_bytes_with_nul();
 
+        assert!(
+            modname.len().saturating_add(name.len()) <= bindings::AUXILIARY_NAME_SIZE as usize,
+            "auxiliary device ID is too long"
+        );
+
         let mut id: bindings::auxiliary_device_id = pin_init::zeroed();
         let mut i = 0;
         while i < modname.len() {

base-commit: 28924df2a08f440c73991b83028032c901de2ae4

Signature · 1 line
2.47.3
#1 lore raw
Quoted 29 lines · Georgios Androutsopoulos

DeviceId::new() copies modname and name into the fixed 40-byte auxiliary_device_id::name array without checking that they fit. An oversized name is caught by the array bounds check, but the error reports an out-of-bounds index in the copy loop rather than the constraint the caller violated.

Check the invariant explicitly instead, so the failure states the length limit rather than an array index.

In a constant context exceeding the limit leads to a build error; at runtime it panics, so add a # Panics section for it.

Fixes ce735e73dd59 ("rust: auxiliary: add auxiliary device / driver abstractions")
---
 rust/kernel/auxiliary.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)
rust/kernel/auxiliary.rs +3 -0
index 60dfbec8f330..1f3ba86d6d96 100644
@@ -137,10 +137,20 @@ macro_rules! module_auxiliary_driver {
 
 impl DeviceId {
     /// Create a new [`DeviceId`] from name.
+    ///
+    /// # Panics
+    ///

rust/kernel/auxiliary.rs macro_rules! module_auxiliary_driver { · line 152

Quoted 10 lines
rust/kernel/auxiliary.rs +6 -0
+    /// Panics if the combined module and device name, including the
+    /// separator and trailing NUL, exceeds `AUXILIARY_NAME_SIZE` bytes.
     pub const fn new(modname: &'static CStr, name: &'static CStr) -> Self {
         let name = name.to_bytes_with_nul();
         let modname = modname.to_bytes_with_nul();
 
+        assert!(
+            modname.len().saturating_add(name.len()) <= bindings::AUXILIARY_NAME_SIZE as usize,
+            "auxiliary device ID is too long"
+        );

We really shouldn't panic, we should error out and fail the creation instead.

But what is placing the constraint of the name size here? The C api just takes a pointer, it doesn't care about the size, why does the rust binding care?

thanks,

greg k-h

maintainer
#2 lore raw
Quoted 7 lines · Greg Kroah-Hartman
Quoted 6 lines · Georgios Androutsopoulos

DeviceId::new() copies modname and name into the fixed 40-byte auxiliary_device_id::name array without checking that they fit. An oversized name is caught by the array bounds check, but the error reports an out-of-bounds index in the copy loop rather than the constraint the caller violated.

Quoted 7 lines
Quoted 7 lines

Check the invariant explicitly instead, so the failure states the length limit rather than an array index.

In a constant context exceeding the limit leads to a build error; at runtime it panics, so add a # Panics section for it.

Fixes ce735e73dd59 ("rust: auxiliary: add auxiliary device / driver abstractions")

It's not actually fixing a bug, so I don't think this needs a Fixes: tag.

Quoted 11 lines
Quoted 11 lines
---
 rust/kernel/auxiliary.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)
rust/kernel/auxiliary.rs +0 -0
index 60dfbec8f330..1f3ba86d6d96 100644
@@ -137,10 +137,20 @@ macro_rules! module_auxiliary_driver {
 
Quoted 7 lines

rust/kernel/auxiliary.rs macro_rules! module_auxiliary_driver { · line 144

Quoted 7 lines
rust/kernel/auxiliary.rs +5 -0
 impl DeviceId {
     /// Create a new [`DeviceId`] from name.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the combined module and device name, including the
+    /// separator and trailing NUL, exceeds `AUXILIARY_NAME_SIZE` bytes.

I'd rather document that this is only intended to be called within device ID table creation; in const context a panic is just a compile time error.

Quoted 8 lines

rust/kernel/auxiliary.rs macro_rules! module_auxiliary_driver { · line 152

Quoted 8 lines
rust/kernel/auxiliary.rs +4 -0
     pub const fn new(modname: &'static CStr, name: &'static CStr) -> Self {
         let name = name.to_bytes_with_nul();
         let modname = modname.to_bytes_with_nul();
 
+        assert!(
+            modname.len().saturating_add(name.len()) <= bindings::AUXILIARY_NAME_SIZE as usize,
+            "auxiliary device ID is too long"
+        );

Isn't this missing to consider the separator and NULL terminator?

Quoted 3 lines

We really shouldn't panic, we should error out and fail the creation instead.

This is only ever used from const context to construct the device ID table, e.g. as in

	kernel::auxiliary_device_table!(
	    AUX_TABLE,
	    <NovaDriver as auxiliary::Driver>::IdInfo,
	    [(
	        auxiliary::DeviceId::new(NOVA_CORE_MODULE_NAME, AUXILIARY_NAME),
	        ()
	    )]
	);

and a panic in const context makes the compilation fail, so it works as intended.

Unfortunately, we can't enforce that is function can only be called from const context, so technically it could also be called outside of the device ID table in non-const context, but it would be odd to construct outside of a device ID table.

Quoted 3 lines

But what is placing the constraint of the name size here? The C api just takes a pointer, it doesn't care about the size, why does the rust binding care?

I assume you were thinking of something else? This struct represents

	#define AUXILIARY_NAME_SIZE 40
	
	struct auxiliary_device_id {
		char name[AUXILIARY_NAME_SIZE];
		kernel_ulong_t driver_data;
	};
#5 lore raw
In reply to Greg Kroah-Hartman
Quoted 44 lines · Greg Kroah-Hartman
Quoted 29 lines · Georgios Androutsopoulos

DeviceId::new() copies modname and name into the fixed 40-byte auxiliary_device_id::name array without checking that they fit. An oversized name is caught by the array bounds check, but the error reports an out-of-bounds index in the copy loop rather than the constraint the caller violated.

Check the invariant explicitly instead, so the failure states the length limit rather than an array index.

In a constant context exceeding the limit leads to a build error; at runtime it panics, so add a # Panics section for it.

Fixes ce735e73dd59 ("rust: auxiliary: add auxiliary device / driver abstractions")
---
 rust/kernel/auxiliary.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)
rust/kernel/auxiliary.rs +3 -0
index 60dfbec8f330..1f3ba86d6d96 100644
@@ -137,10 +137,20 @@ macro_rules! module_auxiliary_driver {
 
 impl DeviceId {
     /// Create a new [`DeviceId`] from name.
+    ///
+    /// # Panics
+    ///

rust/kernel/auxiliary.rs macro_rules! module_auxiliary_driver { · line 152

Quoted 10 lines
rust/kernel/auxiliary.rs +6 -0
+    /// Panics if the combined module and device name, including the
+    /// separator and trailing NUL, exceeds `AUXILIARY_NAME_SIZE` bytes.
     pub const fn new(modname: &'static CStr, name: &'static CStr) -> Self {
         let name = name.to_bytes_with_nul();
         let modname = modname.to_bytes_with_nul();
 
+        assert!(
+            modname.len().saturating_add(name.len()) <= bindings::AUXILIARY_NAME_SIZE as usize,
+            "auxiliary device ID is too long"
+        );

We really shouldn't panic, we should error out and fail the creation instead.

Quoted 3 lines

But what is placing the constraint of the name size here? The C api just takes a pointer, it doesn't care about the size, why does the rust binding care?

Note that this is not a runtime panic, this method is only ever called in const context by the auxiliary_device_table! macro, so a panic here translates to a build error. The current code also panicks if the name is larger than the target array, but it did so when the array was accessed out-of-bounds, with a more obscure error message. What this patch does is provide a better error message for a condition that was already checked.

So while the patch is arguably an improvement, I would suggest to drop its Fixes: tag as it doesn't really fixes a condition that wasn't already checked. Also the commit message's "at runtime it panics" gives the wrong idea of when this code is evaluated.

#6 lore raw
Quoted 4 lines · Alexandre Courbot

Note that this is not a runtime panic, this method is only ever called in const context by the auxiliary_device_table! macro, so a panic here translates to a build error.

Yeah, this is one instance where #[const_eval_only] (or #[consteval] or ...) will make it clearer.

Cheers,
Miguel

#3 lore raw
In reply to Danilo Krummrich
Quoted 79 lines · Danilo Krummrich
Quoted 7 lines · Greg Kroah-Hartman
Quoted 6 lines · Georgios Androutsopoulos

DeviceId::new() copies modname and name into the fixed 40-byte auxiliary_device_id::name array without checking that they fit. An oversized name is caught by the array bounds check, but the error reports an out-of-bounds index in the copy loop rather than the constraint the caller violated.

Quoted 7 lines
Quoted 7 lines

Check the invariant explicitly instead, so the failure states the length limit rather than an array index.

In a constant context exceeding the limit leads to a build error; at runtime it panics, so add a # Panics section for it.

Fixes ce735e73dd59 ("rust: auxiliary: add auxiliary device / driver abstractions")

It's not actually fixing a bug, so I don't think this needs a Fixes: tag.

Quoted 11 lines
Quoted 11 lines
---
 rust/kernel/auxiliary.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)
rust/kernel/auxiliary.rs +0 -0
index 60dfbec8f330..1f3ba86d6d96 100644
@@ -137,10 +137,20 @@ macro_rules! module_auxiliary_driver {
 
Quoted 7 lines

rust/kernel/auxiliary.rs macro_rules! module_auxiliary_driver { · line 144

Quoted 7 lines
rust/kernel/auxiliary.rs +5 -0
 impl DeviceId {
     /// Create a new [`DeviceId`] from name.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the combined module and device name, including the
+    /// separator and trailing NUL, exceeds `AUXILIARY_NAME_SIZE` bytes.

I'd rather document that this is only intended to be called within device ID table creation; in const context a panic is just a compile time error.

Quoted 8 lines

rust/kernel/auxiliary.rs macro_rules! module_auxiliary_driver { · line 152

Quoted 8 lines
rust/kernel/auxiliary.rs +4 -0
     pub const fn new(modname: &'static CStr, name: &'static CStr) -> Self {
         let name = name.to_bytes_with_nul();
         let modname = modname.to_bytes_with_nul();
 
+        assert!(
+            modname.len().saturating_add(name.len()) <= bindings::AUXILIARY_NAME_SIZE as usize,
+            "auxiliary device ID is too long"
+        );

Isn't this missing to consider the separator and NULL terminator?

Quoted 3 lines

We really shouldn't panic, we should error out and fail the creation instead.

This is only ever used from const context to construct the device ID table, e.g. as in

	kernel::auxiliary_device_table!(
	    AUX_TABLE,
	    <NovaDriver as auxiliary::Driver>::IdInfo,
	    [(
	        auxiliary::DeviceId::new(NOVA_CORE_MODULE_NAME, AUXILIARY_NAME),
	        ()
	    )]
	);

and a panic in const context makes the compilation fail, so it works as intended.

Unfortunately, we can't enforce that is function can only be called from const context, so technically it could also be called outside of the device ID table in non-const context, but it would be odd to construct outside of a device ID table.

Quoted 3 lines

But what is placing the constraint of the name size here? The C api just takes a pointer, it doesn't care about the size, why does the rust binding care?

Quoted 9 lines

I assume you were thinking of something else? This struct represents

	#define AUXILIARY_NAME_SIZE 40
	
	struct auxiliary_device_id {
		char name[AUXILIARY_NAME_SIZE];
		kernel_ulong_t driver_data;
	};

Ah, sorry, I was looking at auxiliary_device_create() which just takes a pointer to a name, which is not the device_id, but the name by which the device_id gets created from, my bad.

greg k-h

reviewer
#7 lore raw
In reply to Alexandre Courbot
Quoted 49 lines · Alexandre Courbot
Quoted 44 lines · Greg Kroah-Hartman
Quoted 29 lines · Georgios Androutsopoulos

DeviceId::new() copies modname and name into the fixed 40-byte auxiliary_device_id::name array without checking that they fit. An oversized name is caught by the array bounds check, but the error reports an out-of-bounds index in the copy loop rather than the constraint the caller violated.

Check the invariant explicitly instead, so the failure states the length limit rather than an array index.

In a constant context exceeding the limit leads to a build error; at runtime it panics, so add a # Panics section for it.

Fixes ce735e73dd59 ("rust: auxiliary: add auxiliary device / driver abstractions")
---
 rust/kernel/auxiliary.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)
rust/kernel/auxiliary.rs +3 -0
index 60dfbec8f330..1f3ba86d6d96 100644
@@ -137,10 +137,20 @@ macro_rules! module_auxiliary_driver {
 
 impl DeviceId {
     /// Create a new [`DeviceId`] from name.
+    ///
+    /// # Panics
+    ///

rust/kernel/auxiliary.rs macro_rules! module_auxiliary_driver { · line 152

Quoted 10 lines
rust/kernel/auxiliary.rs +6 -0
+    /// Panics if the combined module and device name, including the
+    /// separator and trailing NUL, exceeds `AUXILIARY_NAME_SIZE` bytes.
     pub const fn new(modname: &'static CStr, name: &'static CStr) -> Self {
         let name = name.to_bytes_with_nul();
         let modname = modname.to_bytes_with_nul();
 
+        assert!(
+            modname.len().saturating_add(name.len()) <= bindings::AUXILIARY_NAME_SIZE as usize,
+            "auxiliary device ID is too long"
+        );

We really shouldn't panic, we should error out and fail the creation instead.

Quoted 3 lines

But what is placing the constraint of the name size here? The C api just takes a pointer, it doesn't care about the size, why does the rust binding care?

Quoted 7 lines

Note that this is not a runtime panic, this method is only ever called in const context by the auxiliary_device_table! macro, so a panic here translates to a build error. The current code also panicks if the name is larger than the target array, but it did so when the array was accessed out-of-bounds, with a more obscure error message. What this patch does is provide a better error message for a condition that was already checked.

Yeah, I think the message is good, however we don't need the panic annotation for const-eval-only code.

Best,
Gary

Quoted 5 lines

So while the patch is arguably an improvement, I would suggest to drop its Fixes: tag as it doesn't really fixes a condition that wasn't already checked. Also the commit message's "at runtime it panics" gives the wrong idea of when this code is evaluated.

#4 lore raw
In reply to Danilo Krummrich
Quoted 1 line · Danilo Krummrich

It's not actually fixing a bug, so I don't think this needs a Fixes: tag.

Agreed, dropping it in v2.

Quoted 2 lines

I'd rather document that this is only intended to be called within device ID table creation; in const context a panic is just a compile time error.

Agreed, replacing the # Panics section with that in v2.

Quoted 10 lines
Quoted 8 lines

rust/kernel/auxiliary.rs macro_rules! module_auxiliary_driver { · line 152

Quoted 8 lines
     pub const fn new(modname: &'static CStr, name: &'static CStr) -> Self {
         let name = name.to_bytes_with_nul();
         let modname = modname.to_bytes_with_nul();
rust/kernel/auxiliary.rs +4 -0
+        assert!(
+            modname.len().saturating_add(name.len()) <= bindings::AUXILIARY_NAME_SIZE as usize,
+            "auxiliary device ID is too long"
+        );

Isn't this missing to consider the separator and NULL terminator?

I believe both are covered. to_bytes_with_nul() includes each string's terminator, and after the copy loop modname's NUL is overwritten with the . separator, so modname.len() + name.len() is exactly the required capacity.

At the boundary, DeviceId::new(c"a", <37 bytes>) stores "a." plus 37 bytes plus the NUL, which is 1 + 1 + 37 + 1 = 40 and is accepted; one byte more is rejected.

Quoted 4 lines

Unfortunately, we can't enforce that is function can only be called from const context, so technically it could also be called outside of the device ID table in non-const context, but it would be odd to construct outside of a device ID table.

Ideally that would be enforced rather than documented. Miguel pointed at Gary's #[const_eval_only] [1] in the other branch, and DeviceId::new() looks like a good candidate for it: it exists only to build device ID tables, so a non-const call is a mistake rather than a supported use.

For v2 I will document the intent, and the attribute can be applied as a follow-up once that series lands.

[1] https://lore.kernel.org/rust-for-linux/[email protected]/

Quoted 88 lines · Danilo Krummrich
Quoted 7 lines · Greg Kroah-Hartman
Quoted 6 lines · Georgios Androutsopoulos

DeviceId::new() copies modname and name into the fixed 40-byte auxiliary_device_id::name array without checking that they fit. An oversized name is caught by the array bounds check, but the error reports an out-of-bounds index in the copy loop rather than the constraint the caller violated.

Quoted 7 lines
Quoted 7 lines

Check the invariant explicitly instead, so the failure states the length limit rather than an array index.

In a constant context exceeding the limit leads to a build error; at runtime it panics, so add a # Panics section for it.

Fixes ce735e73dd59 ("rust: auxiliary: add auxiliary device / driver abstractions")

It's not actually fixing a bug, so I don't think this needs a Fixes: tag.

Quoted 11 lines
Quoted 11 lines
---
 rust/kernel/auxiliary.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)
rust/kernel/auxiliary.rs +0 -0
index 60dfbec8f330..1f3ba86d6d96 100644
@@ -137,10 +137,20 @@ macro_rules! module_auxiliary_driver {
 
Quoted 7 lines

rust/kernel/auxiliary.rs macro_rules! module_auxiliary_driver { · line 144

Quoted 7 lines
rust/kernel/auxiliary.rs +5 -0
 impl DeviceId {
     /// Create a new [`DeviceId`] from name.
+    ///
+    /// # Panics
+    ///
+    /// Panics if the combined module and device name, including the
+    /// separator and trailing NUL, exceeds `AUXILIARY_NAME_SIZE` bytes.

I'd rather document that this is only intended to be called within device ID table creation; in const context a panic is just a compile time error.

Quoted 8 lines

rust/kernel/auxiliary.rs macro_rules! module_auxiliary_driver { · line 152

Quoted 8 lines
rust/kernel/auxiliary.rs +4 -0
     pub const fn new(modname: &'static CStr, name: &'static CStr) -> Self {
         let name = name.to_bytes_with_nul();
         let modname = modname.to_bytes_with_nul();
 
+        assert!(
+            modname.len().saturating_add(name.len()) <= bindings::AUXILIARY_NAME_SIZE as usize,
+            "auxiliary device ID is too long"
+        );

Isn't this missing to consider the separator and NULL terminator?

Quoted 3 lines

We really shouldn't panic, we should error out and fail the creation instead.

This is only ever used from const context to construct the device ID table, e.g. as in

        kernel::auxiliary_device_table!(
            AUX_TABLE,
            <NovaDriver as auxiliary::Driver>::IdInfo,
            [(
                auxiliary::DeviceId::new(NOVA_CORE_MODULE_NAME, AUXILIARY_NAME),
                ()
            )]
        );

and a panic in const context makes the compilation fail, so it works as intended.

Unfortunately, we can't enforce that is function can only be called from const context, so technically it could also be called outside of the device ID table in non-const context, but it would be odd to construct outside of a device ID table.

Quoted 3 lines

But what is placing the constraint of the name size here? The C api just takes a pointer, it doesn't care about the size, why does the rust binding care?

I assume you were thinking of something else? This struct represents

        #define AUXILIARY_NAME_SIZE 40

        struct auxiliary_device_id {
                char name[AUXILIARY_NAME_SIZE];
                kernel_ulong_t driver_data;
        };