rust: print: avoid evaluating arguments in `pr_*` macros in `unsafe` blocks
Miguel Ojeda
maintainerPATCH 1/1 rust: print: avoid evaluating arguments in `pr_*` macros in `unsafe` blocks
At the moment it is possible to perform unsafe operations in the arguments of pr_* macros since they are evaluated inside an unsafe block:
let x = &10u32 as *const u32;
pr_info!("{}", *x);In other words, this is a soundness issue.
Fix it so that it requires an explicit unsafe block.
--- rust/kernel/print.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-)
rust/kernel/print.rs +18 -11
@@ -142,17 +142,24 @@ pub fn call_printk_cont(args: fmt::Arguments<'_>) { macro_rules! print_macro ( // The non-continuation cases (most of them, e.g. `INFO`). ($format_string:path, false, $($arg:tt)+) => ( - // SAFETY: This hidden macro should only be called by the documented - // printing macros which ensure the format string is one of the fixed - // ones. All `__LOG_PREFIX`s are null-terminated as they are generated - // by the `module!` proc macro or fixed values defined in a kernel - // crate. - unsafe { - $crate::print::call_printk( - &$format_string, - crate::__LOG_PREFIX, - format_args!($($arg)+), - ); + // To remain sound, `arg`s must be expanded outside the `unsafe` block. + // Typically one would use a `let` binding for that; however, `format_args!` + // takes borrows on the arguments, but does not extend the scope of temporaries. + // Therefore, a `match` expression is used to keep them around, since + // the scrutinee is kept until the end of the `match`. + match format_args!($($arg)+) { + // SAFETY: This hidden macro should only be called by the documented + // printing macros which ensure the format string is one of the fixed + // ones. All `__LOG_PREFIX`s are null-terminated as they are generated + // by the `module!` proc macro or fixed values defined in a kernel + // crate. + args => unsafe { + $crate::print::call_printk( + &$format_string, + crate::__LOG_PREFIX, + args, + ); + } } );
base-commit: b7bfaa761d760e72a969d116517eaa12e404c262
Signature · 1 line
2.39.0
Quoted 7 lines · Miguel Ojeda
At the moment it is possible to perform unsafe operations in the arguments of
pr_*macros since they are evaluated inside anunsafeblock:let x = &10u32 as *const u32; pr_info!("{}", *x);
Quoted 8 lines
In other words, this is a soundness issue.
Fix it so that it requires an explicit
unsafeblock.Reported-by Wedson Almeida Filho <[email protected]>Reported-by Domen Puncer Kugler <[email protected]>Signed-off-by Miguel Ojeda <[email protected]>
Quoted 49 lines
--- rust/kernel/print.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-)rust/kernel/print.rs +18
-11@@ -142,17 +142,24 @@ pub fn call_printk_cont(args: fmt::Arguments<'_>) { macro_rules! print_macro ( // The non-continuation cases (most of them, e.g. `INFO`). ($format_string:path, false, $($arg:tt)+) => ( - // SAFETY: This hidden macro should only be called by the documented - // printing macros which ensure the format string is one of the fixed - // ones. All `__LOG_PREFIX`s are null-terminated as they are generated - // by the `module!` proc macro or fixed values defined in a kernel - // crate. - unsafe { - $crate::print::call_printk( - &$format_string, - crate::__LOG_PREFIX, - format_args!($($arg)+), - ); + // To remain sound, `arg`s must be expanded outside the `unsafe` block. + // Typically one would use a `let` binding for that; however, `format_args!` + // takes borrows on the arguments, but does not extend the scope of temporaries. + // Therefore, a `match` expression is used to keep them around, since + // the scrutinee is kept until the end of the `match`. + match format_args!($($arg)+) { + // SAFETY: This hidden macro should only be called by the documented + // printing macros which ensure the format string is one of the fixed + // ones. All `__LOG_PREFIX`s are null-terminated as they are generated + // by the `module!` proc macro or fixed values defined in a kernel + // crate. + args => unsafe { + $crate::print::call_printk( + &$format_string, + crate::__LOG_PREFIX, + args, + ); + } } );base-commit:
b7bfaa761d760e72a969d116517eaa12e404c262Signature · 1 line
2.39.0
Quoted 7 lines · On Mon, 9 Jan 2023 21:49:12 +0100 Miguel Ojeda <[email protected]> wrote:
At the moment it is possible to perform unsafe operations in the arguments of
pr_*macros since they are evaluated inside anunsafeblock:let x = &10u32 as *const u32; pr_info!("{}", *x);
Quoted 8 lines
In other words, this is a soundness issue.
Fix it so that it requires an explicit
unsafeblock.Reported-by Wedson Almeida Filho <[email protected]>Reported-by Domen Puncer Kugler <[email protected]>Signed-off-by Miguel Ojeda <[email protected]>
Quoted 46 lines
--- rust/kernel/print.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-)rust/kernel/print.rs +18
-11@@ -142,17 +142,24 @@ pub fn call_printk_cont(args: fmt::Arguments<'_>) { macro_rules! print_macro ( // The non-continuation cases (most of them, e.g. `INFO`). ($format_string:path, false, $($arg:tt)+) => ( - // SAFETY: This hidden macro should only be called by the documented - // printing macros which ensure the format string is one of the fixed - // ones. All `__LOG_PREFIX`s are null-terminated as they are generated - // by the `module!` proc macro or fixed values defined in a kernel - // crate. - unsafe { - $crate::print::call_printk( - &$format_string, - crate::__LOG_PREFIX, - format_args!($($arg)+), - ); + // To remain sound, `arg`s must be expanded outside the `unsafe` block. + // Typically one would use a `let` binding for that; however, `format_args!` + // takes borrows on the arguments, but does not extend the scope of temporaries. + // Therefore, a `match` expression is used to keep them around, since + // the scrutinee is kept until the end of the `match`. + match format_args!($($arg)+) { + // SAFETY: This hidden macro should only be called by the documented + // printing macros which ensure the format string is one of the fixed + // ones. All `__LOG_PREFIX`s are null-terminated as they are generated + // by the `module!` proc macro or fixed values defined in a kernel + // crate. + args => unsafe { + $crate::print::call_printk( + &$format_string, + crate::__LOG_PREFIX, + args, + ); + } } );base-commit:
b7bfaa761d760e72a969d116517eaa12e404c262
Quoted 7 lines · Miguel Ojeda
At the moment it is possible to perform unsafe operations in the arguments of
pr_*macros since they are evaluated inside anunsafeblock:let x = &10u32 as *const u32; pr_info!("{}", *x);
Quoted 8 lines
In other words, this is a soundness issue.
Fix it so that it requires an explicit
unsafeblock.Reported-by Wedson Almeida Filho <[email protected]>Reported-by Domen Puncer Kugler <[email protected]>Signed-off-by Miguel Ojeda <[email protected]>
Quoted 48 lines
--- rust/kernel/print.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-)rust/kernel/print.rs +18
-11@@ -142,17 +142,24 @@ pub fn call_printk_cont(args: fmt::Arguments<'_>) { macro_rules! print_macro ( // The non-continuation cases (most of them, e.g. `INFO`). ($format_string:path, false, $($arg:tt)+) => ( - // SAFETY: This hidden macro should only be called by the documented - // printing macros which ensure the format string is one of the fixed - // ones. All `__LOG_PREFIX`s are null-terminated as they are generated - // by the `module!` proc macro or fixed values defined in a kernel - // crate. - unsafe { - $crate::print::call_printk( - &$format_string, - crate::__LOG_PREFIX, - format_args!($($arg)+), - ); + // To remain sound, `arg`s must be expanded outside the `unsafe` block. + // Typically one would use a `let` binding for that; however, `format_args!` + // takes borrows on the arguments, but does not extend the scope of temporaries. + // Therefore, a `match` expression is used to keep them around, since + // the scrutinee is kept until the end of the `match`. + match format_args!($($arg)+) { + // SAFETY: This hidden macro should only be called by the documented + // printing macros which ensure the format string is one of the fixed + // ones. All `__LOG_PREFIX`s are null-terminated as they are generated + // by the `module!` proc macro or fixed values defined in a kernel + // crate. + args => unsafe { + $crate::print::call_printk( + &$format_string, + crate::__LOG_PREFIX, + args, + ); + } } );base-commit:
b7bfaa761d760e72a969d116517eaa12e404c262--
2.39.0
Quoted 7 lines · Miguel Ojeda
At the moment it is possible to perform unsafe operations in the arguments of
pr_*macros since they are evaluated inside anunsafeblock:let x = &10u32 as *const u32; pr_info!("{}", *x);
Quoted 8 lines
In other words, this is a soundness issue.
Fix it so that it requires an explicit
unsafeblock.Reported-by Wedson Almeida Filho <[email protected]>Reported-by Domen Puncer Kugler <[email protected]>Signed-off-by Miguel Ojeda <[email protected]>
Quoted 48 lines
--- rust/kernel/print.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-)rust/kernel/print.rs +18
-11@@ -142,17 +142,24 @@ pub fn call_printk_cont(args: fmt::Arguments<'_>) { macro_rules! print_macro ( // The non-continuation cases (most of them, e.g. `INFO`). ($format_string:path, false, $($arg:tt)+) => ( - // SAFETY: This hidden macro should only be called by the documented - // printing macros which ensure the format string is one of the fixed - // ones. All `__LOG_PREFIX`s are null-terminated as they are generated - // by the `module!` proc macro or fixed values defined in a kernel - // crate. - unsafe { - $crate::print::call_printk( - &$format_string, - crate::__LOG_PREFIX, - format_args!($($arg)+), - ); + // To remain sound, `arg`s must be expanded outside the `unsafe` block. + // Typically one would use a `let` binding for that; however, `format_args!` + // takes borrows on the arguments, but does not extend the scope of temporaries. + // Therefore, a `match` expression is used to keep them around, since + // the scrutinee is kept until the end of the `match`. + match format_args!($($arg)+) { + // SAFETY: This hidden macro should only be called by the documented + // printing macros which ensure the format string is one of the fixed + // ones. All `__LOG_PREFIX`s are null-terminated as they are generated + // by the `module!` proc macro or fixed values defined in a kernel + // crate. + args => unsafe { + $crate::print::call_printk( + &$format_string, + crate::__LOG_PREFIX, + args, + ); + } } );base-commit:
b7bfaa761d760e72a969d116517eaa12e404c262Signature · 1 line
2.39.0
Miguel Ojeda
AppliedQuoted 8 lines · Miguel Ojeda
At the moment it is possible to perform unsafe operations in the arguments of
pr_*macros since they are evaluated inside anunsafeblock:let x = &10u32 as *const u32; pr_info!("{}", *x);
Quoted 8 lines
In other words, this is a soundness issue.
Fix it so that it requires an explicit
unsafeblock.Reported-by Wedson Almeida Filho <[email protected]>Reported-by Domen Puncer Kugler <[email protected]>Signed-off-by Miguel Ojeda <[email protected]>
Applied to rust-fixes, thanks all!
Cheers,
Miguel