Imam Nawawi (رحمه الله) wrote in Riyadh as-Saliheen in chapter 256 on Lawful Instances of Backbiting:
Backbiting for a valid Sharia objective is permissible if there is no other alternative for achieving that objective. There are six such objectives:
1. To rectify an injustice. It is permissible for the person who was treated unjustly to complain to the ruler, judge, or others who have the authority or ability to mete out justice to the person who acted unjustly. He may said, “so-and-so person has wronged me.”
2. Seeking to help change a wrong and return the wrongdoer to the right path. He may therefore say to the one whom he hopes has the power to remove the wrong: “so-and-so is committing such-and-such an action. Reprimand him about it.” His objective should be to utilize a means to remove the wrong. If this is not his objective, it is haram (forbidden).
3. Requesting a fatwa (verdict). He may say to the mufti: “my father, brother, spouse, or so-and-so has wronged me by doing such-and-such a thing. Is it permissible for him to do so? How can I save myself, claim my right, and remove the injustice?” It is permissible due to necessity, however, the better and more cautionary approach is to say: “what do you say about a man, person, or spouse who did such-and-such a thing?” In this way, he will be able to achieve his objective without identifying any person. Nonetheless, it is permissible to reveal the identity of the person as proven from the hadith we will quote regarding Hind, insha’Allah.
4. Warning the Muslims about an evil and advising them. There are various forms of this: Declaring certain narrators and witnesses as unreliable. This is permissible by consensus of the Muslim scholars. In fact, it is wajib (obligatory) due to necessity.
Seeking advice and counsel regarding a person for the purposes of marriage, business partnership, giving a trust, engaging in some transaction, or being a neighbor. The person whose advice is requested should not conceal his condition, rather he should mention his qualities with the intention of advice. If one sees a student studying fiqh under a person who is involved in bidat (innovations) or is a flagrant sinner, and he fears that the student will be harmed by it, then it is necessary for him to advise the student of his teacher’s condition, provided that his objective is to give advice. In this one may easily err, as the speaker is sometimes prompted by jealously, and Shaytan confuses him causing him to think that it is good advice. He should be wary of this.
A person in a position of authority which he does not administer correctly either because he is incompetent, a flagrant sinner, negligent, or due to a similar reason. It is necessary to inform a person in higher authority to remove such a person from his position and appoint someone who is competent, or so that he is at least aware of the person and can deal with him accordingly and not be deceived by him, and he may encourage him to rectify his affairs or replace him.
5. A person who publicizes his sin or bid’ah (innovation), such as one who consumes alcohol publicly, usurps the property of people, collects unjust customs and taxes from people, or handles false matters. It is permissible to mention the sins he commits publicly, but it is haram to mention any of his other faults, unless it is for an objective we mentioned previously.
6. For the purposes of identification. If a person is known by a certain defect such as being bear-eyed, lame, deaf, blind, squint-eyed, etc. then it is permissible to mention them as such, but it is haram to use such words in a derogatory way. If it is possible to describe such people by other means, then these should be utilized.