Your right, when you gain weight you take a chance at some of it being fat as well. Therefore, what you want to try and do is to gain more weight then you had thought about and then go on a "cutting phase" by doing more cardio and eating less. If you truly gained muscle mass, you should end up weighing more and with just muscle mass. Or, you can go around this and simply keep the carbs and fats low and eat lots of protein, while lifting heavy.