Score:0

Can a non-root modify environment variables?

pk flag

Can a non-root (without sudo permissions) run the following command:

export PATH=/tml:$PATH

If so, how does it work?

I thought environment variables are read-only for non-root users, while shell variables can be modified freely.

muru avatar
us flag
"I thought environment variables are read-only," Why did you think that?
Trigosin Darom avatar
pk flag
@muru for non-root users
muru avatar
us flag
The question remains. What did you read or see that makes you think so?
Trigosin Darom avatar
pk flag
To my understanding, environment variables (unlike shell variables) are global. Which means they apply to every process. This is very dangerous if any user can modify values there.
muru avatar
us flag
Every process has a *copy* of environment variables inherited from its parent, and can modify its own set of environment variables and also the set of environment variables passed to child processes upon creation.
Trigosin Darom avatar
pk flag
The reason I'm asking is because I tried to solve a ctf challenge where there was a weak user with a sudo permission to run "date --date 'yesterday'" as root. The solution was to create an executable named 'date' and change the PATH env variable so it will reach the modified 'date' executable. I'm still not sure how it works because root's PATH variable shouldn't be affected.
muru avatar
us flag
That still shouldn't have worked as stated. For sudo, if permission is given for a specific command, the full path to that command has to be specified. If it worked for you, the sudo configuration was some very insecure thing.
Score:1
jp flag

The output of the following commands should convince you that you can modify your environment variables.

$ grep PATH ~/.profile 
# set PATH so it includes user's private bin if it exists
    PATH=~/bin:"${PATH}"
PATH="$PATH:/usr/games"

$ ls -l ~/.profile
-rw-r--r-- 1 sudodus sudodus 632 dec 10  2010 /home/sudodus/.profile

In other words, you can not only modify these variables temporarily, but also make it persist by modifying your configuration file ~/.profile. You, as a regular user can do it.


The following command line shows system files, where the PATH is set or modified

sudo grep -r 'PATH=' /etc/*

You may want to get more details. See for example the following link,

Unix & Linux: Complete view of where the PATH variable is set in bash - particularly the answer by Gilles.

Trigosin Darom avatar
pk flag
So any user can change environment variables GLOBALY? And it will apply on root as well? Seems like I'm missing something here.
sudodus avatar
jp flag
You can change ***your*** environment variables, but not for other users unless you have superuser permisisons (can use `sudo`).
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.