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

[framework] Redundant Existence Check in add_additional_metadata Function #4596

Open
miker83z opened this issue Dec 20, 2024 · 0 comments
Open
Labels
vm-language Issues related to the VM & Language Team
Milestone

Comments

@miker83z
Copy link
Contributor

miker83z commented Dec 20, 2024

FROM THE AUDIT:

Summary

The add_additional_metadata function in the CoinManager module enforces a redundant check to verify the non-existence of a value before adding additional metadata. However, the underlying df::add function already performs this check and aborts if the value with the same name exists.

Vulnerability Detail

In the CoinManager module, the add_additional_metadata function is defined as follows:

public fun add_additional_metadata<T, Value: store>(
    _: &CoinManagerMetadataCap<T>,
    manager: &mut CoinManager<T>,
    value: Value
) {
@>> assert!(!df::exists_(&manager.id, b"additional_metadata"), EAdditionalMetadataAlreadyExists);
    df::add(&mut manager.id, b"additional_metadata", value);
}

This function verifies that the additional_metadata field does not already exist before invoking df::add. However, the df::add function internally performs this exact check and aborts with EFieldAlreadyExists if a field with the same name already exists. The relevant portion of the df::add implementation is as follows:

public fun add<Name: copy + drop + store, Value: store>(
    object: &mut UID,
    name: Name,
    value: Value,
) {
     let object_addr = object.to_address();
     let hash = hash_type_and_key(object_addr, name);
@>>  assert!(!has_child_object(object_addr, hash), EFieldAlreadyExists);
     let field = Field {
        id: object::new_uid_from_hash(hash),
        name,
        value,
     };
     add_child_object(object_addr, field)
}

Since the existence check is already handled by df::add, the additional assertion in add_additional_metadata is redundant and unnecessarily duplicates logic.

Tool used

Manual Review

Recommendation

public fun add_additional_metadata<T, Value: store>(
    _: &CoinManagerMetadataCap<T>,
    manager: &mut CoinManager<T>,
    value: Value
) {
-   assert!(!df::exists_(&manager.id, b"additional_metadata"), EAdditionalMetadataAlreadyExists);
    df::add(&mut manager.id, b"additional_metadata", value);
}
@miker83z miker83z added the vm-language Issues related to the VM & Language Team label Dec 20, 2024
@miker83z miker83z added this to the Mainnet milestone Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vm-language Issues related to the VM & Language Team
Projects
None yet
Development

No branches or pull requests

1 participant