One person has been confirmed dead and three more are missing following the blaze in Sao Paulo.
The tower block was occupied by about 150 rough sleepers and a number of people are thought to be trapped in the rubble, authorities said.
Firefighters were alerted at 1.30am local time (5.30am in the UK). More than 170 firefighters attended the scene, along with a civil defence response team and the army.
Screams for help were heard from the top floor of the building, according to reports from the scene.
It is not clear how many people were in the building at the time but 50 families are registered as living in the block, local media reported.
Live television footage showed a firefighter on a rooftop next to the site, talking to a man clinging to a rescue rope and trying to escape from the upper part of the burning building.
Firefighter Andre Elias told Globo TV that they expected to find more victims but did not know how many.
Local workers said the former Federal Police building was 26 storeys tall and occupied by groups of homeless people.
Flabio Gabia, a receptionist at a nearby hotel, said: “When I went to see what it was, the streets were filled with desperate people.
“I heard several people shouting.”
The blaze is understood to have started on the third floor and then spread rapidly to the levels above.
A neighbouring building was evacuated as the flames spread and the area was sealed off by police.
Frederick Ludwig, a pastor at a nearby church, said he had been warning authorities for years about the appalling conditions for people living in the building.
He told the Folha de Sao Paulo newspaper: “There was sewage in the open air in there as well as outbreaks of dengue mosquito.”
Sao Paulo state Governor Marcio Franca told the newspaper it was a “tragedy foretold”.
The city and state governments have been working for years to forcibly remove squatters from buildings in central Sao Paulo, with plans for regenerating the area.
Mr Franca said about 150 buildings in the region were occupied by organised groups of squatters who have pressured the government to provide housing for the city’s homeless.
The governor said it was legally difficult to force people to leave the old and decaying building.
More from Brazil
Child abuse scandal reaches top of Brazilian football
Ex-Brazilian president Lula da Silva in custody after stand-off
Bishop ‘used church money to buy cattle ranch’
Off-duty police officer ‘shoots armed robbers dead’ while holding baby in Brazil
Brazilian lawmaker waves model foetus as MPs vote to ban all abortions – even for rape victims
Military enter Rio de Janeiro favela in gang raids
He added: “There is not even a minimal condition for people to live in there. People live there in desperation. This was a tragedy foretold.”
The cause of the fire is being investigated.