No one is quite sure how many people live in Brazil's favelas. According to the 2010 census, 11.25 million individuals in a population of around 205 million are contained in these sprawling, ever-evolving shanty towns. In reality the figure, like most things said about the country's irrepressible poor, is a best guess.

For example, Rocinha, in the “Southern Zone” of Rio de Janeiro, was estimated by census-takers to be home to 70,000 souls. Locals call that laughable. They reckon the biggest of all favelas might have as many as 180,000 residents. Who can really tell? Not those who struggle to count heads on behalf of a state that has neglected the semi-legal slums and their inhabitants for decades.

As Misha Glenny recounts, multitudes have streamed to Rio from Brazil's poorest regions since the 1950s. Sheer numbers have kept wages low. Grotesque inequalities, corrupt politics, thuggish police and fear among the prosperous have meanwhile confined the migrants to their tin-roofed city-states. There they have, after a fashion, governed themselves. In recent decades that has meant the modern feudalism of the narcotics trade.

But to whom, victims aside, does this matter? To anthropologists? To the evangelical Protestant preachers supplanting the Roman Catholic church? To the economists who proclaim that Brazil, occupying almost half of South America's land mass, rich in resources and already the world's seventh largest economy, is destined to be a great 21st-century power?

The country's political class prefer that story. They would rather tourists had enjoyed their World Cup than asked why Brazilians were taking to the streets to protest against the spending of $11.5 billion on football matches. The politicians prefer to talk about Brazil's new-found international influence rather than discuss the fact that it has the world's third-biggest prison population, not to mention one of the highest homicide rates.

Some of those political types are being arrested even now after the theft of $3 billion from Petrobras, the state-run oil company. Corruption is reckoned, conservatively, to cost Brazil $41 billion a year while the vast revenues from cocaine flood the body politic. Much of the trade, most of its revenues and a lot of the violence, begins in favelas such as Rocinha. The trade and the cash it generates is, in essence, another species of addictive corruption.

That is, of course, a bad thing, but not bad, necessarily, in all the ways we might imagine. Glenny tells a larger story – almost a parable – through the career of Antonio Francisco Bonfim Lopes, nicknamed Nem, or “Babe”, of Rocinha, a gang boss now incarcerated in one of Brazil's maximum security establishments. In an obvious way it is the tale of a good man gone badly wrong. It is also an argument made from a life: an argument over poverty and necessity, honour and evil, and the travails of a society. Nem is emblematic.

Underlying Glenny's narrative there is the old insinuated question asked by political folk singers the world over: who are the real criminals? As Woody Guthrie used to put it, “Some will rob you with a six gun/ And some with a fountain pen.” In Nem's Brazil, the stroke of a pen from bought politicians has too often protected drug gangs and police death squads alike. Cocaine has united criminals and “law-abiding” consumers. What, in such circumstances, is an honest man to do?

Nem was honest enough, to begin with. As a youngster growing up in Rocinha, he stayed away from the gangsters. Dirt poor, he nevertheless tried to make a go of marriage and parenthood in his tiny, airless slum. He worked long hours for a magazine distribution company. Until his baby daughter fell seriously ill with a mysterious cancer-like ailment – Langerhans cell disease – this Antonio, not yet Nem, was making as much progress as a favela-dweller could hope to make.

But he could not afford to pay for his child's medical treatment, or to install the basic sanitation that would keep her alive. In this fable, worth a movie, Antonio knew of only one person who could help: the patriarchal drugs “don” of Rocinha known as Lulu, the favela's “benign dictator” who brooked no challengers, but was – for this is part of Glenny's point – as beloved as he was respected.

Lulu kept the peace. He settled disputes and provided law for people who were otherwise prey to the forces of “law and order”. Lulu contributed to the welfare of his community, providing loans to the likes of Antonio and mortgages – also a money-laundering device – to others. He took an interest, with no sense of irony, in local politics.

There was no one else. When Lulu was absent, things in Rocinha fell apart. He was far from unique: in other favelas the smarter bosses had also understood the facts of slum life. The drugs trade needed “law and order” as much as any other business. The community was the sea in which sharks swam. But, as with all feudal societies, the question of succession and rivalries among heavily-armed and ambitious young men was the system's flaw.

Lulu was Nem's mentor, first providing him with a lowly job in “security”, then gradually allowing him the chance to apply the organisational skills he had acquired – for why not? - in magazine distribution. Nem learned all about violence and its uses, but he also learned the crucial importance of bookkeeping in the narcotics trade. Business is business. He was good at it; he was also – on this account – thoughtful, a philosopher-gangster with a social conscience.

A parable? An indictment of capitalism and “development” in a world that no longer questions the meaning of money? A tale as old as poverty? All of that and more. Glenny has spent many hours interviewing Nem in the penitentiary at Camp Grande, far from Rocinha, where he ponders his entrapment and 2011 arrest. He deserved all he had coming to him, this drugs lord, but equally there is an implicit question: what did a man with no real choices truly deserve?

Rocinha has been “pacified” by the state. The old, failed international “war on drugs” goes on. President Dilma Rouseff achieved re-election last October, despite the vast protests and official violence before the World Cup, but the Petrobras scandal rumbles on. The Brazilian economy is in deep trouble. Still the cocaine trade continues, despite – because of? - it all.

Nem was, for a while, “the effective president, prime minister and most powerful businessman of a medium-seized city”. Was he beyond the pale because of violence? Brazil's crooked politicians and their cops are well-armed and often violent. Was this unelected “president” the antithesis of democracy? Corruption taints Brazil's politics, too. Was his trade more than a decent society could bear? That society, its banks and legitimate enterprises, absorbs drugs money without hesitation.

Nemesis is both a crime story and a moral puzzle. It challenges preconceptions. Where are the limits of what any society can afford to tolerate? The likes of Nem cannot be allowed: so much is clear. But if we consider what is allowed, accepted and recognised as lawful, his career, like his favela, is a rebuke to anyone's complacency.