Bug 634 - Not Able to use strstr or strlen function in program run with sudo
Not Able to use strstr or strlen function in program run with sudo
Status: RESOLVED INVALID
Product: Sudo
Classification: Unclassified
Component: Sudo
1.6.9
PC Linux
: normal normal
Assigned To: Todd C. Miller
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2014-02-25 00:08 MST by Avishek
Modified: 2014-02-25 09:13 MST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Avishek 2014-02-25 00:08:24 MST
Hi,
I have currently a code that is something like following -

========================== code snippet ================================
char *cwd = 0;
char *tmp = 0;
cwd = getenv("PWD"); // This path contains 'Linux'

tmp = strstr(cwd, "/Linux"); <--------------- Problem starts here

*tmp = '\0';
========================== code snippet ================================

in fourth step process ends abnormally while running with sudo.

But if I run this without sudo this works fine. If I use something like strlen then also it have the same behaviour. 

From valgrind it is not showing any invalid read in that step also.

I am running sudo as a root user. 

But If I run this with sudo from some other user than root then this problem is not happenning.
After call to getenv cwd is populated with correct value. So the assumption that getenv returning Null pointer for PWD is out of question.

I wonder if sudo have some side effects when using it as a root user or not. 
 

Can you please take a look at this issue.
 
Thanks.
Comment 1 Todd C. Miller 2014-02-25 09:13:30 MST
The PWD environment variable is not preserved by default.  The bug in your code is that  you are not checking the return value of getenv() to see if it is NULL.  You could tell sudo to preserve the PWD variable with a line like:

Defaults env_keep += PWD

However, it would be more secure to simply use the getcwd() function in your code instead since you cannot really trust the environment of the process running sudo.