The basic principle is that a Muslim is not allowed to buy shares in a company that deals with riba or deals with haram products and projects.

Dr. Monzer Kahf, a prominent economist and counselor, states: The principle is that it is haram. There is no doubt about it because buying shares makes the purchaser a partner in the company of which the management is a mere agent of the partners. This means that the partners themselves are making riba transactions. Of course, this is not permissible in Shari`ah, whether it is a small or a large percentage.
Moreover, establishing a company whose by-laws mentions borrowing on interest is also not permissible.
however, if the application of this principle creates difficulty and hardship for a large number of Muslims, then we must relax it and tolerate some violation to the extent that is needed in order to remove the hardship. This is based on the over-riding principles that Shari`ah and religion in general are not sent down to create hardship but rather to remove it and that hardship and difficulties, when it applies to a large number of people, is treated as necessities in regard to relaxing prohibitions.
This means that under such circumstances we may tolerate some prohibited practices by our agent or manager, when a shareholder is a small minority, provided that we do not extend this tolerance beyond the degree that is required to remove the hardship and that we apply the rule of purification. The latter requires that we must estimate the amount of the total gain or profit that comes from haram transactions and dealings and give it to Muslim charity. Estimating the degree of tolerance that is required to remove hardship differs from one market to another. Some scholars estimate it for the New York stock market as 5% interest and other haram out of the net revenues of the company. They also stipulate that no more than one third of assets and liability can be related to haram.