7

On my implementation, only the owner of the contract's function was able to call some important functions that I defined. I changed the implementation as by removing onlyOwner, and adding cluster_constract[msg.sender].deleteAll() that will allow only the message sender to call that important function, if their address mapped to a list. cluster_constract[<my_address>] = list.

[Q] If an attacker knows the address of the msg.sender, can he calls deleteAll() function (cluster_constract["<valid_address>"].deleteAll(); ) from an external contract? Is my approach safe to use?

contract owned { 
  address  public owner;
  function owned() {
    owner = msg.sender;
  }
  function ownerOnly() {
    if (msg.sender != owner) throw;
  }
  modifier onlyOwner {
    if (msg.sender != owner) throw;
       _
  }
  function transferOwnership(address newOwner) onlyOwner {
    owner = newOwner;
  }
}

Before:

  LinkedList.data list;
  function deleteAll() onlyOwner {      
     list.deleteAll();
  }

After:

  mapping(address => LinkedList.data) cluster_constract;

  function add_cluster() {
     cluster_constract[msg.sender] = list;
  }

  function deleteAll() { //if cluster_constract[msg.sender] exits deleteAll() can be called.
     cluster_constract[msg.sender].deleteAll();        
  }

2 Answers 2

4

Yes, msg.sender is secure as elliptic curve cryptography. Ethereum uses the same curve and key size as Bitcoin. Everyone knows msg.sender but only possession of the private key will allow someone to sign a transaction as that msg.sender.

A transaction actually doesn't contain a sender. The address of the sender is derived from the signature on the transaction.

If you look at token contracts, you'll see they usually use a mapping of balances where the key is msg.sender.

As @Edmund wrote, it's simpler and more secure, whereas a modifier like onlyOwner needs to be explicitly attached to all functions that need to be locked down.

3

Yes, if anything it's probably more secure to enforce ownership by keeping it under a mapping key, because you can't write code to change it without considering who it belongs to, whereas if you're relying on explicit ownership checks you may forget to apply them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.