Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am writing a regular compiled application that needs to create a special file and write a magic cookie into it. I can’t write the file directly from the application, the system security model requires me to launch a helper tool with elevated privileges to do the trick. I can supply any number of arguments to the helper tool. Now I would like to pick some very simple system command that would serve as the helper tool and create the file for me. Something like this:

/bin/sh -c "/bin/echo -n 'magic' > /some/where/file"

Simple touch does not cut it as I need to write the cookie into the file, a simple echo without the shell wrapper does not work as it needs redirection to write the file. I don’t feel comfortable calling the shell with root privileges to do such a trivial task. Is there some really simple, constrained system command that I could call to write the file for me?

share|improve this question
    
Is there a reason why /bin/sh -c 'echo magic > /path/to/magic/file' does not work? That would be an executable file and two arguments. You would need to build the last argument as a string (with sprintf or equivalent). Is there as reason this wouldn't work for you? From your question it sounds like doCommandAsRoot() does not take input to stream to the command, correct? Otherwise you could replace the last argument with 'cat > /path/to/magic/file' and pass the data instead of constructing a string. –  Arcege Feb 22 '11 at 16:56
    
The shell example works, but I’d hate to call the shell with root privileges just to create a simple file. The library does take a communication pipe argument (it’s AuthorizationExecuteWithPrivileges), that could be used to write the cookie using tee. Thanks! –  zoul Feb 22 '11 at 17:05
add comment

2 Answers 2

up vote 8 down vote accepted

How about this:

echo -n 'magic' | sudo tee /some/where/file > /dev/null

Sure there are redirections in this but only tee runs as root not a shell. Works with dd of=... too.

share|improve this answer
1  
Sorry, I obviously have trouble explaining myself lately. The problem is that when I want to run something with the elevated privileges, I have to go through a special library call (something like doCommandAsRoot). And this function only accepts a path to the command and its arguments, so there’s no way for me to use output redirection right away. I could go through shell (as shown in the question), but I don’t like that security-wise. –  zoul Feb 22 '11 at 16:10
    
@zoul: I thought you are writing a shell script. Is it OK to write the magic cookie to a file that is accessible for unprivileged processes? If it is then write it to such a file and call doCommandAsRoot("dd", "if=/tmp/unprotected_file", "of=/some/where/file"). –  stribika Feb 22 '11 at 16:28
    
No, it’s a regular compiled application (question updated). I’m just looking for “shell” commands to serve as the helper so that I don’t have to write it myself. Yes, the cookie is nothing sensitive. The dd example is pretty close to what I want, thank you. Could you think of something even simpler? –  zoul Feb 22 '11 at 16:38
    
Sure — you could use cp or mv instead of dd. –  mattdm Feb 22 '11 at 16:51
    
I mean simpler as without the need to copy :) –  zoul Feb 22 '11 at 16:54
show 1 more comment

There's another consideration, which is that you don't want to put the value of the magic cookie on a command line, since that can be observed by other users. Even if the program is short-lived (including if the program zeros out the command line string), there is the opportunity for attack. So, a theoretical:

writestringtofile 'magic' /some/where/file

is a dangerous approach. Therefore, I endorse @stribika's suggestion: write the value to a temporary file and copy it into place. Make sure to use a secure function to create the temporary file (mkstemp()) so that there's not a race condition there as well.

share|improve this answer
    
Thank you, this is a useful discussion. Fortunately the “magic cookie” is not a security device, it can be plainly seen by anybody. –  zoul Feb 22 '11 at 18:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.