Feature - Stop truncating strings in syscalls (#31030)

* Adds the feature gate stop_truncating_strings_in_syscalls.

* Removes the truncation of everything after the first NULL byte in translate_string_and_do().
This commit is contained in:
Alexander Meißner 2023-04-04 19:33:53 +02:00 committed by GitHub
parent aa3e0b9c20
commit 911489d373
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 5 deletions

View File

@ -24,6 +24,9 @@ declare_syscall!(
len, len,
invoke_context.get_check_aligned(), invoke_context.get_check_aligned(),
invoke_context.get_check_size(), invoke_context.get_check_size(),
invoke_context
.feature_set
.is_active(&stop_truncating_strings_in_syscalls::id()),
&mut |string: &str| { &mut |string: &str| {
stable_log::program_log(&invoke_context.get_log_collector(), string); stable_log::program_log(&invoke_context.get_log_collector(), string);
Ok(0) Ok(0)

View File

@ -40,7 +40,8 @@ use {
enable_early_verification_of_account_modifications, enable_early_verification_of_account_modifications,
error_on_syscall_bpf_function_hash_collisions, libsecp256k1_0_5_upgrade_enabled, error_on_syscall_bpf_function_hash_collisions, libsecp256k1_0_5_upgrade_enabled,
limit_secp256k1_recovery_id, reject_callx_r10, limit_secp256k1_recovery_id, reject_callx_r10,
stop_sibling_instruction_search_at_parent, switch_to_new_elf_parser, stop_sibling_instruction_search_at_parent, stop_truncating_strings_in_syscalls,
switch_to_new_elf_parser,
}, },
hash::{Hasher, HASH_BYTES}, hash::{Hasher, HASH_BYTES},
instruction::{ instruction::{
@ -428,14 +429,19 @@ fn translate_string_and_do(
len: u64, len: u64,
check_aligned: bool, check_aligned: bool,
check_size: bool, check_size: bool,
stop_truncating_strings_in_syscalls: bool,
work: &mut dyn FnMut(&str) -> Result<u64, EbpfError>, work: &mut dyn FnMut(&str) -> Result<u64, EbpfError>,
) -> Result<u64, EbpfError> { ) -> Result<u64, EbpfError> {
let buf = translate_slice::<u8>(memory_mapping, addr, len, check_aligned, check_size)?; let buf = translate_slice::<u8>(memory_mapping, addr, len, check_aligned, check_size)?;
let msg = if stop_truncating_strings_in_syscalls {
buf
} else {
let i = match buf.iter().position(|byte| *byte == 0) { let i = match buf.iter().position(|byte| *byte == 0) {
Some(i) => i, Some(i) => i,
None => len as usize, None => len as usize,
}; };
let msg = buf.get(..i).ok_or(SyscallError::InvalidLength)?; buf.get(..i).ok_or(SyscallError::InvalidLength)?
};
match from_utf8(msg) { match from_utf8(msg) {
Ok(message) => work(message), Ok(message) => work(message),
Err(err) => Err(SyscallError::InvalidString(err, msg.to_vec()).into()), Err(err) => Err(SyscallError::InvalidString(err, msg.to_vec()).into()),
@ -508,6 +514,9 @@ declare_syscall!(
len, len,
invoke_context.get_check_aligned(), invoke_context.get_check_aligned(),
invoke_context.get_check_size(), invoke_context.get_check_size(),
invoke_context
.feature_set
.is_active(&stop_truncating_strings_in_syscalls::id()),
&mut |string: &str| Err(SyscallError::Panic(string.to_string(), line, column).into()), &mut |string: &str| Err(SyscallError::Panic(string.to_string(), line, column).into()),
) )
} }
@ -2046,6 +2055,7 @@ mod tests {
string.len() as u64, string.len() as u64,
true, true,
true, true,
true,
&mut |string: &str| { &mut |string: &str| {
assert_eq!(string, "Gaggablaghblagh!"); assert_eq!(string, "Gaggablaghblagh!");
Ok(42) Ok(42)

View File

@ -641,10 +641,15 @@ pub mod include_loaded_accounts_data_size_in_fee_calculation {
pub mod native_programs_consume_cu { pub mod native_programs_consume_cu {
solana_sdk::declare_id!("8pgXCMNXC8qyEFypuwpXyRxLXZdpM4Qo72gJ6k87A6wL"); solana_sdk::declare_id!("8pgXCMNXC8qyEFypuwpXyRxLXZdpM4Qo72gJ6k87A6wL");
} }
pub mod simplify_writable_program_account_check { pub mod simplify_writable_program_account_check {
solana_sdk::declare_id!("5ZCcFAzJ1zsFKe1KSZa9K92jhx7gkcKj97ci2DBo1vwj"); solana_sdk::declare_id!("5ZCcFAzJ1zsFKe1KSZa9K92jhx7gkcKj97ci2DBo1vwj");
} }
pub mod stop_truncating_strings_in_syscalls {
solana_sdk::declare_id!("16FMCmgLzCNNz6eTwGanbyN2ZxvTBSLuQ6DZhgeMshg");
}
lazy_static! { lazy_static! {
/// Map of feature identifiers to user-visible description /// Map of feature identifiers to user-visible description
pub static ref FEATURE_NAMES: HashMap<Pubkey, &'static str> = [ pub static ref FEATURE_NAMES: HashMap<Pubkey, &'static str> = [
@ -801,6 +806,7 @@ lazy_static! {
(include_loaded_accounts_data_size_in_fee_calculation::id(), "include transaction loaded accounts data size in base fee calculation #30657"), (include_loaded_accounts_data_size_in_fee_calculation::id(), "include transaction loaded accounts data size in base fee calculation #30657"),
(native_programs_consume_cu::id(), "Native program should consume compute units #30620"), (native_programs_consume_cu::id(), "Native program should consume compute units #30620"),
(simplify_writable_program_account_check::id(), "Simplify checks performed for writable upgradeable program accounts #30559"), (simplify_writable_program_account_check::id(), "Simplify checks performed for writable upgradeable program accounts #30559"),
(stop_truncating_strings_in_syscalls::id(), "Stop truncating strings in syscalls #31029"),
/*************** ADD NEW FEATURES HERE ***************/ /*************** ADD NEW FEATURES HERE ***************/
] ]
.iter() .iter()