Search for answers or browse our knowledge base.
The 401k structure itself is not problematic, rather the underlying investments are what determine permissibility from a religious perspective. You might consider checking with your company if they offer a self-directed brokerage account plan where you could have more control over your own investments.
Some scholars of the AMJA Council of Jurists have examined this issue in their fatwa bank http://www.amjaonline.org/en/fatwa-search and provided some guidance. If you are unable to invest in fully Sharia-compliant investment vehicles as we advise above, some scholars have allowed you would spend a part of the wealth to purify the money of the questionable investments, e.g. below; others preferred to avoid it.
http://www.amjaonline.org/fatwa-1871/info
Additional info on preferred investment types:
http://www.amjaonline.org/fatwa-83534/info
The Islamic permissibility of the 401k account is based on the permissibility of the underlying investments. The employer contributions/principal are legitimately yours. As for the gains, those tied to Islamically prohibited investments would be considered haram. You may estimate the amount of these and donate to the poor with the intention of purifying the money / disavowing the haram; the remainder of the funds are yours. You may keep them or you may give them away as sadaqa.