A Massachusetts law blocking towns and cities from returning excess funds from foreclosure sales may be an unconstitutional taking, a federal judge has said.