mirror of
https://github.com/torvalds/linux.git
synced 2026-10-22 12:52:02 +02:00
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. This should only be reached when constructing a device ID table, so the failure is a compile time error. Document that intent. Signed-off-by: Georgios Androutsopoulos <georgeandrout13@gmail.com> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Link: https://patch.msgid.link/20260910153844.3987791-1-georgeandrout13@gmail.com Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
parent
d3bdf70cf0
commit
a4aaec5580
|
|
@ -137,10 +137,18 @@ macro_rules! module_auxiliary_driver {
|
|||
|
||||
impl DeviceId {
|
||||
/// Create a new [`DeviceId`] from name.
|
||||
///
|
||||
/// This is only intended to be called in const context, when constructing a
|
||||
/// device ID table, where exceeding `AUXILIARY_NAME_SIZE` is a compile time error.
|
||||
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() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user