Yes, it is very possible to work on open-source projects in academic settings. The exact legal and technical details vary from university to university, but many universities are very open to open-source contribution.
There are a lot of variables that you don't specify in your question. I am most familiar with U.S. universities, so my answer will focus on those.
Short version: work with your adviser to discuss it with your Office of Technology Transfer.
In most U.S. universities, the university owns rights to code that is created as a part of sponsored projects or with their resources. The following easily trigger it:
- Using the computer provided by your university
- Being funded on a research grant
- Being funded by the university
Often, work done entirely on your own time on your own computer equipment, sufficently unrelated to your paid work that it cannot be considered part of it, is yours without any problem. Coursework is also often yours, although using university equipment may muddy that point. The exact details depend on university policies.
Your university policies will contain information about the precise rules. However, there is often a fair amount of difference between how the policies are written and how they are implemented in practice. I have been affiliated with 3 universities, and at all of them have had no problems obtaining suitable permission for open-source software release.
The relevant office is generally the Office of Technology Transfer or something like that. You should start by talking with your adviser - they will start the Tech Transfer discussion - but there is a good chance that your adviser will not actually know the detailed rules of your university. In practice, there's a lot of open-source code released without going through the proper channels for official university sign-off.
Some universities officially vest faculty with the authority to authorize open-source release of software developed under their supervision. If that is the case, then the conversation is very short.
In other cases, tech transfer will need to approve to have everything be formally legal. It is possible that they will not fully understand what is going on - I've seen cases where the relevant authority didn't really understand software copyrights and open-source licensing. My current institution's tech transfer office takes the perspective that their job is to help faculty achieve their goals with technology licensing and commercialization, and has worked with me to get university counsel to sign off on a couple of standard open-source licenses, making subsequent releases very easy.
Unfortunately, most universities' formal policies for invention disclosure and review do not interact well with the realities of open-source software, particularly continuous development in the open. You'll need to work with university officials to navigate that.
I have generally found university officials to be receptive to my argument that open-source release is a cornerstone of my approach to disseminating the results of my research and promoting reproducible research in my field.