Is tertiary education free of cost in Spain and Portugal?
No it's not.
You always have tuition fees, which in absolute term may seems negligible (between 1 and 2 thousands euro per year), but it can be a relevant chunk of a family income, considering the average salary being in the 700-900 euro range for most of the people living outside the major, rich, urban zones (Madrid-Valencia-Barcelona-Lisbon-Oporto, to name the largest).
Specifically to Portugal, there is a rule[1] the Ministry of Education establishes a minimum amount that varies each year depending on the minimum national wage rate.
According to Portuguese regulations, the minimum amount of tuition fees students are required to pay is 1.3 times the minimum national wage rate. For the year, 2010, the minimum national wage rate amounted to 475 EUR per month.
For upper education (like Ph.D enrollment) in Portugal the tuition fees may reach easily 2-3000 Euro.
[1] rather absurd, in my opinion Art. 16, comma 2 https://dre.pt/web/guest/legislacao-consolidada/-/lc/107958193/201708090100/73459622/diploma/indice