I am not a lawyer and you should consult one for real, accurate legal advice.
The name and design are likely trademarked (you can see here for example that there have been trademark issues with the M4 in the past; other guns will likely have similar issues).
EDIT: It is possible that as far as a trademark is concerned I may have been misinterpreting things, and it may be okay depending on your circumstances. However, you will still potentially need to worry about other intellectual property restrictions (such as any copyrights held in relation to the item in question) which do apply regardless of whether or not your product is commercially available.
At the end of the day, if you violate somebody's intellectual property laws and they call you on it you can be in a world of hurt so it's best to get real legal advice because the risk is pretty severe. There's a good set of articles on intellectual property issues by a Real Lawyer here.