Praise be to Allah.
It is permissible to buy shares in permissible companies via the banks, and what the riba-based banks charge from the purchaser or company is only in return for serving as an intermediary, and it is a fee that is permissible for them, because it is in return for permissible work.
Shaykh al-‘Uthaymeen (may Allaah have mercy on him) said:
There is nothing wrong with dealing with the banks in permissible transactions, for example, I may buy from them, or get change from them, or transfer money through them – there is nothing wrong with that. Rather what is forbidden is riba. If it is not riba there is nothing wrong with it. It was proven that the Prophet (peace and blessings of Allaah be upon him) bought from the Jews, and accepted gifts from them, although they consumed haraam wealth and took riba, and when he (peace and blessings of Allaah be upon him) died, his shield was being held in pledge by a Jew.
Liqaa’aat al-Baab il-Maftoohah (70/question no. 7)
And Allaah knows best.