Sri Lanka's Dhananjaya de Silva (R) watches his shot next to South Africa's wicketkeeper Quinton de Kock. They are in a strong position to push for a win that would give them a 2-0 whitewash of the two-Test series with three more days left in the match.
Karunaratne was unbeaten on 59 at stumps with Angelo Mathews on 12.
After being dismissed for 126 and 73 in the first Test in Galle, the tourists were skittled for 124 in just 34.5 overs on Saturday, with offspinner Akila Dananjaya taking 5-52.
Danushka Gunathilaka and Karunaratne completed their second half-centuries of the match putting together an opening partnership of 91 to go with their first innings effort of 116.
Dananjaya took his second five-wicket haul in Tests while Herath and Perera shared the remaining five South African wickets.
Left-arm orthodox spinner Keshav Maharaj claimed the best test innings figures by a South African bowler since the country's readmission to worldwide cricket in the second match against Sri Lanka at Sinhalese Sports Club grounds on Saturday.
Skipper Faf du Plessis was once again his team's top scorer with a counter-attacking innings of 48 off 51 balls inclusive of eight fours and a six.
Maharaj's nine wicket spree was South Africa's best in an innings since the country's post-apartheid return to Test cricket in 1991.
Dilruwan Perera (4-40) then joined the rout as South Africa lost all 10 wickets to the Sri Lankan spinners.
In reply, the South Africans were bundled out for a paltry 124 in their first essay, with Akila Dananjaya (5/52) and Dilruwan Perera (4/40) even as veteran Hashim Amla became the third Protea to notch 9,000 Test runs.
Maharaj eventually took the wicket of Herath, who gloved a ball to a close fielder as he attempted to sweep.