Skip to content

Commit 561023d

Browse files
authored
If the account length is zero, no data region is present (#5122)
This only affects unaligned programs, as for aligned programs the resize area address will match the account region address if the data length is zero.
1 parent 18b49da commit 561023d

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

programs/bpf_loader/src/syscalls/mem_ops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ impl<'a> Iterator for MemoryChunkIterator<'a> {
530530
account_index = account_index.saturating_add(1);
531531
self.account_index = Some(account_index);
532532
} else {
533-
region_is_account = region.vm_addr == account_addr
533+
region_is_account = (account.original_data_len != 0 && region.vm_addr == account_addr)
534534
// unaligned programs do not have a resize area
535535
|| (self.resize_area && region.vm_addr == resize_addr);
536536
break;
@@ -604,7 +604,7 @@ impl DoubleEndedIterator for MemoryChunkIterator<'_> {
604604

605605
self.account_index = Some(account_index);
606606
} else {
607-
region_is_account = region.vm_addr == account_addr
607+
region_is_account = (account.original_data_len != 0 && region.vm_addr == account_addr)
608608
// unaligned programs do not have a resize area
609609
|| (self.resize_area && region.vm_addr == resize_addr);
610610
break;

programs/sbf/rust/account_mem/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,15 @@ pub fn process_instruction(
6060
sol_memset(&mut data[data_len.saturating_sub(2)..], 0, 3);
6161
}
6262
5 => {
63+
// memcmp overlaps begining
64+
#[allow(clippy::manual_memcpy)]
65+
for i in 0..3 {
66+
buf[i] = too_early(2)[i];
67+
}
68+
6369
// memset overlaps begin of account area
6470
sol_memset(too_early(2), 3, 3);
71+
sol_memcpy(too_early(2), &buf, 3);
6572
}
6673
6 => {
6774
// memcpy src overlaps end of account

programs/sbf/rust/account_mem_deprecated/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn process_instruction(
2525
let mut too_early = |before: usize| -> &mut [u8] {
2626
let data = data.as_mut_ptr().wrapping_sub(before);
2727

28-
unsafe { std::slice::from_raw_parts_mut(data, data_len) }
28+
unsafe { std::slice::from_raw_parts_mut(data, data_len.wrapping_add(100)) }
2929
};
3030

3131
match instruction_data[0] {
@@ -40,11 +40,11 @@ pub fn process_instruction(
4040
2 => {
4141
// memcmp overlaps begining
4242
#[allow(clippy::manual_memcpy)]
43-
for i in 0..500 {
43+
for i in 0..90 {
4444
buf[i] = too_early(8)[i];
4545
}
4646

47-
sol_memcmp(too_early(8), &buf, 500);
47+
sol_memcmp(too_early(8), &buf, 90);
4848
}
4949
3 => {
5050
// memcmp overlaps begining

programs/sbf/tests/programs.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5194,6 +5194,29 @@ fn test_mem_syscalls_overlap_account_begin_or_end() {
51945194
assert!(!logs.last().unwrap().ends_with(" failed: InvalidLength"));
51955195
}
51965196
}
5197+
5198+
let account = AccountSharedData::new(42, 0, &program_id);
5199+
bank.store_account(&account_keypair.pubkey(), &account);
5200+
5201+
for instr in 0..=15 {
5202+
println!("Testing deprecated:{deprecated} direct_mapping:{direct_mapping} instruction:{instr} zero-length account");
5203+
let instruction =
5204+
Instruction::new_with_bytes(program_id, &[instr, 0], account_metas.clone());
5205+
5206+
let message = Message::new(&[instruction], Some(&mint_pubkey));
5207+
let tx = Transaction::new(&[&mint_keypair], message.clone(), bank.last_blockhash());
5208+
let (result, _, logs, _) = process_transaction_and_record_inner(&bank, tx);
5209+
5210+
if direct_mapping && !deprecated {
5211+
// we have a resize area
5212+
assert!(
5213+
logs.last().unwrap().ends_with(" failed: InvalidLength"),
5214+
"{logs:?}"
5215+
);
5216+
} else {
5217+
assert!(result.is_ok(), "{logs:?}");
5218+
}
5219+
}
51975220
}
51985221
}
51995222
}

0 commit comments

Comments
 (0)