Sherlock is pretty much controversial regarding his behaviour to the ones he cared, be it John or Molly or Mrs. Hudson.
At first, he may have needed Mrs. Hudson for a place to stay at a prime location in London. And Mrs. Hudson is doing him a favour by providing that at a cheaper rate.
Then the landlady-tenant relationship may have moved to a closer relation. That too probably because Sherlock knows how intolerable he can be as a person to live with.
He says to Mike: "I told Mike this morning I must be a difficult man to find a flatmate for."
Now Mrs. Hudson lives alone. She not only tolerates his extreme behaviour (gun practice at home, violin at midnight, keeping skullheads in refrigerator etc.) and allows him to stay as he likes, but also cares for him. She cooks for him, cleans the place occasionally. Sherlock knows she is always there for him and she somehow can control him when he loses his mind.She allows Sherlock to keep a flatmate too.
Those probably are the reasons why Sherlock has a caring and protective outlook towards her though he behaves rudely sometimes.