As noted above, it certainly depends on the moral framework. For Aristotle's virtue ethics, the accumulation of wealth by means of trade may fall under pleonexia, an ignoble trait. But, of course, the philosophers of his day lived largely and unproblematically off "shareholdings" in land and slave labor.
For Aquinas and the medieval moralists, usury was immoral, and your dividends represent a sort of compounding interest on a loan to the company. But, of course, tithes and the sale of indulgences formed a morally dubious financialization of the Church that eventually ended in the "credit" or "credibility" crisis of the Reformation.
Morality plays no role in a Marxist analysis of classic Liberalism, but that is the framework most relevant to our modern situation. To put the case very, very crudely, the classical economists, from Smith to Marx, held a labor theory of value, which I argue remains valid and intuitive, beneath the veil of marginal utility "pricing" and the vast, intentional complexities of finance.
Two classes dominate this modern production system, according to Marx, the Capitalist and the Workers, with innumerable subclasses. If you must in any way "work" for a living, you are a worker. The workers collectively produce all material value by selling the only thing they own, their labor power, which is materialized as commodities. The capitalists own the "means of production," land, machinery, financial capital, patents, copyrights, licenses, etc., as well as the final commodities.
Of course, there are myriad complications. The "capitalist" may chose to work or at least yell on the phone and "look busy," even if they are in fact losing value (a certain president comes to mind). A CEO may be "rich," yet still have to sell her labor, and so forth. The idealized point is that a tiny minority will collect a surplus value simply from what they own, "money makes money," while the vast majority "own" only the labor they sell for wages, which are collectively worth less than the commodified value they produce.
The scenario you describe is very pertinent. Today the role of "capitalist" is widely fragmented and distributed and, yes, the retired worker with a 401K is a tiny "owner" of the means of production, collecting money without labor, the value of which can only come from someone's labor somewhere. This was not common in Marx's time, and interestingly Marx did thinks the widely distributed stock market was a potentially democratizing trend.
Here, the potential point is not of morality, but of justice. Despite the naturalized illusion of "investment," money does not create money, or at least money does not magically create value. The "shareholder" is collecting the difference between the value labor produces, collectively and globally, and the value labor is paid, including value-producing intellectual or managerial labor.
There are no pure cases of this model. But the detachment of capital shares from labor value can be seen in present crises, where the stock values can rise on bad economic news or unemployment. Collectively, corporations and their "shares" seek only their own accumulation or pleonexia, quite apart from the external impacts on workers, the environment, national laws, customs, cultures. So the "returns" on shares may reflect sociopathic forces, and thus might be called not only amoral but "immoral."