Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add discriminator length checks #1678

Merged
merged 2 commits into from Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@ The minor version will be incremented upon a breaking change and the patch versi
### Fixes

* avm: `avm install` no longer downloads the version if already installed in the machine ([#1670](https://github.com/project-serum/anchor/pull/1670)).
* lang: Return proper error instead of panicking if account length is smaller than discriminator in functions of `(Account)Loader` ([#1678](https://github.com/project-serum/anchor/pull/1678)).

### Breaking

Expand Down
9 changes: 9 additions & 0 deletions lang/src/accounts/account_loader.rs
Expand Up @@ -124,6 +124,9 @@ impl<'info, T: ZeroCopy + Owner> AccountLoader<'info, T> {
.with_pubkeys((*acc_info.owner, T::owner())));
}
let data: &[u8] = &acc_info.try_borrow_data()?;
if data.len() < T::discriminator().len() {
return Err(ErrorCode::AccountDiscriminatorNotFound.into());
}
// Discriminator must match.
let disc_bytes = array_ref![data, 0, 8];
if disc_bytes != &T::discriminator() {
Expand All @@ -149,6 +152,9 @@ impl<'info, T: ZeroCopy + Owner> AccountLoader<'info, T> {
/// Returns a Ref to the account data structure for reading.
pub fn load(&self) -> Result<Ref<T>> {
let data = self.acc_info.try_borrow_data()?;
if data.len() < T::discriminator().len() {
return Err(ErrorCode::AccountDiscriminatorNotFound.into());
}

let disc_bytes = array_ref![data, 0, 8];
if disc_bytes != &T::discriminator() {
Expand All @@ -169,6 +175,9 @@ impl<'info, T: ZeroCopy + Owner> AccountLoader<'info, T> {
}

let data = self.acc_info.try_borrow_mut_data()?;
if data.len() < T::discriminator().len() {
return Err(ErrorCode::AccountDiscriminatorNotFound.into());
}

let disc_bytes = array_ref![data, 0, 8];
if disc_bytes != &T::discriminator() {
Expand Down
9 changes: 9 additions & 0 deletions lang/src/accounts/loader.rs
Expand Up @@ -63,6 +63,9 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
.with_pubkeys((*acc_info.owner, *program_id)));
}
let data: &[u8] = &acc_info.try_borrow_data()?;
if data.len() < T::discriminator().len() {
return Err(ErrorCode::AccountDiscriminatorNotFound.into());
}
// Discriminator must match.
let disc_bytes = array_ref![data, 0, 8];
if disc_bytes != &T::discriminator() {
Expand Down Expand Up @@ -90,6 +93,9 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
#[allow(deprecated)]
pub fn load(&self) -> Result<Ref<T>> {
let data = self.acc_info.try_borrow_data()?;
if data.len() < T::discriminator().len() {
return Err(ErrorCode::AccountDiscriminatorNotFound.into());
}

let disc_bytes = array_ref![data, 0, 8];
if disc_bytes != &T::discriminator() {
Expand All @@ -109,6 +115,9 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
}

let data = self.acc_info.try_borrow_mut_data()?;
if data.len() < T::discriminator().len() {
return Err(ErrorCode::AccountDiscriminatorNotFound.into());
}

let disc_bytes = array_ref![data, 0, 8];
if disc_bytes != &T::discriminator() {
Expand Down