The drug dealers for all the bad they do they also support and help the community a lot. This is part of their strategy, the community in turn protect them, hide them. In many favelas the drug lord is the law, and the law is don't poop where you eat. You may be able to steal from people outside the favela but not inside the favela, if you do the punishment can be medieval, like cut hands or even death. It's not only for residents though. Even as a tourist you are probably safe in a favela, it's their interest you feel safe so you don't feel intimidated to go buy drugs. But again after living in Rio and São Paulo for many years I wouldn't recommend anyone to do that.
I spent some years in Sao Paulo and in Rio too. I had many friends and visited almost daily the Morro do Cantagalo, a huge favela there. You can live pretty well there, but they make sure that you know who is the boss there.
What means legal? It is part of Brazil, and therefore the same rules applies there, so its not legal. If there is murder in Favela that the Police cannot investigate and punish the responsible for that? For sure. Specially if who died was criminal.