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'm aware its best to create temporary files with mktemp, but what about named pipes?

I have an idea, I'm sure it would work, but I would really like an expert opinion.

I prefer things to be as POSIX compliant as possible, but Linux only is acceptable. Avoiding Bashisms is my only hard criteria, as I write in dash.

share|improve this question
add comment

5 Answers

up vote 8 down vote accepted
tmppipe=$(mktemp -u)
mkfifo -m 600 "$tmppipe"

Unlike regular file creation, which is prone to being hijacked by an existing file or a symbolic link, the creation of a name pipe through mkfifo or the underlying function either creates a new file in the specified place or fails. Something like : >foo is unsafe because if the attacker can predict the output of mktemp then the attacker can create the target file for himself. But mkfifo foo would fail in such a scenario.

If you need full POSIX portability, mkfifo -m 600 /tmp/myfifo is safe against hijacking but prone to a denial of service; without access to a strong random file name generator, you would need to manage retry attemps.

If you don't care for the subtle security problems around temporary files, you can follow a simple rule: create a private directory, and keep everything in there.

tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT INT TERM HUP
mkfifo "$tmpdir/pipe"
share|improve this answer
    
I actually already chose this as my solution earlier yesterday, I was waiting to see if someone posted it, or a better one. I also felt the dir was the way to go, in my case. Regardless... Thanks for the help, I appreciate it. –  TechZilla Jan 25 '12 at 2:48
add comment

You can use mktemp to create a temporary file, then delete it and create a named pipe with the same name.

For example:

TMPPIPE=$(mktemp -t pipe.XXX) && {
    rm -f $TMPPIPE
    mkfifo $TMPPIPE
}
share|improve this answer
    
Does deleting the $TMPPIPE before the mkfifo avoid the 'unsafe' problem associated from doing TMPPIPE=`mktemp -u` ; mkfifo $TMPPIPE ? –  TechZilla Jan 24 '12 at 9:46
    
@TechZilla mkfifo is actually safe, unlike usual regular file creation from the shell. If it wasn't, creating a file then deleting it would not help at all (in fact it would greatly facilitate the attacker's job by not requiring him to guess the file name). So dogbane's answer works, but the intermediate file creation is a useless complication. –  Gilles Jan 24 '12 at 17:46
    
@Gilles, Do you know in what regard the mktemp man page called the -u option 'unsafe'? –  TechZilla Jan 25 '12 at 2:39
    
@dogbane, I voted up on your answer, and your comments which I felt were solid. Although, I already completed this yesterday.. I was just waiting to see when someone posted either my solution, or hopefully a better one. So even though I used mktemp -d you still got some points for your input. Thank you for all your help, I really appreciate it! –  TechZilla Jan 25 '12 at 2:46
1  
@TechZilla mktemp -u is unsafe when creating a regular file, because it provides the protection against denial of service (if the name it generates is sufficiently unpredictable) but does not prevent an attacker from creating the file under the program's nose. Creating a fifo instead of a regular file is a rare use case that the man page doesn't address. –  Gilles Jan 25 '12 at 9:41
add comment

A safer alternative is to use mktemp to create a directory safely, then put your named pipe inside that directory, do an rm -R $dir to get rid of it in the end.

share|improve this answer
    
I opted to create the FIFO in a mktemp directory, as it was the really the only acceptable answer, Just in case you didn't notice, @Gilles already posted this answer in-depth. –  TechZilla Dec 9 '12 at 19:14
add comment

Use mkfifo or mknod in Unix, where by two separate processes can access the pipe by name — one process can open it as a reader, and the other as a writer.

mkfifo my_pipe
gzip -9 -c < my_pipe > out.gz
cat file > my_pipe

The named pipe can be deleted just like any file:

rm my_pipe

mkfifo --mode=0666 /tmp/namedPipe
gzip --stdout -d file.gz > /tmp/namedPipe

NamedPipe can be used a regular file for only reading once.

http://www.linuxjournal.com/article/2156

share|improve this answer
1  
I'm fully aware about named pipes, and mkfifo. It doesn't address my question about temporary named pipes, anymore than mkdir would address creating temporary directories. –  TechZilla Jan 24 '12 at 7:16
2  
How does this address the issues that mktemp addresses to safely create a named pipe? –  camh Jan 24 '12 at 7:17
1  
oh ok, best is to create them under /tmp, they are temporary files by definition and will clear off once the system reboots. Or better yet, have a shell function which will create the namedpipe out of the mktemp result itself (ofcourse by deleting the temp file first and then running mkfifo on the same). mktemp can also be used to create a temporary directory, try with -t -d switch. –  Nikhil Mulley Jan 24 '12 at 7:24
add comment

Use the "dry-run" option:

mkfifo $(mktemp -ut pipe.XXX)
share|improve this answer
1  
According to the man pages, the use of the -u option "is not encouraged". –  dogbane Jan 24 '12 at 10:04
    
@dogbane So is the use of -t, but as long as it works reliably, I'd go with it. –  polemon Jan 24 '12 at 10:07
    
sorry, where does it say that -t is discouraged? –  dogbane Jan 24 '12 at 10:11
1  
@dogbane If it is in any way critical, I'd make a tiny C application, calling the mkstemp() function (linux.die.net/man/3/mkstemp). The -t switch isn't discouraged, it is -p, my bad. –  polemon Jan 24 '12 at 10:12
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.