Praise be to Allah.
Wealth that is acquired by investing in haraam shares or via riba-based banks, must be gotten rid of by spending it on various charitable causes. It may be given to the poor and needy, or spent on the general interests of the Muslims, such as building hospitals or schools, or paving roads, or it may be given to seekers of knowledge and the like.
The scholars of the Standing Committee for Issuing Fatwas were asked:
What is the ruling on buying shares in companies and banks? Is it permissible for a person who has bought shares in a company or bank to sell the shares after subscribing to a broker who buys and sells shares, and it is possible that they may be sold for more than the value for which the person bought them? What is the ruling on the interest that the shareholder takes every year on the value of the shares he holds?
Buying shares in banks and companies that deal with riba is not permissible. If the shareholder wants to get rid of his riba-based shares, he may sell his shares at the current market value, and take his original capital only; he should spend the rest on charitable causes, and it is not permissible for him to take any of the interest or profits of his shares. But if the shares were in a company that does not deal in riba, then the profits are permissible. End quote.
Fataawa al-Lajnah al-Daa’imah (13/508).
It also says (14/299): The basic principle is that it is permissible to buy shares in any company if it does not deal in haraam things such as riba and so on, but if it deals in haraam things such as riba, then it is not permissible to buy shares in it. Based on this, if any of the shares mentioned are in a company that deals with riba or any other haraam things, then he must withdraw from it and get rid of the profits by spending them on the poor and needy. End quote.
And Allaah knows best.