Creating a temporary fifo name on a * nix system

I have some tasks that require massive temporary named pipes to handle.

Initially, I just think of generating random numbers, and then add it as the <number>.fifo name of the named pipe.

However, I found this post: Creates a temporary FIFO (named pipe) in Python?

Something seems to be something I don’t know that might cause some security issues.

So my question is, what is the best way to create a named pipe?

Please note that even if I refer to a post related to Python, I really do not want to ask only in Python.

UPDATE:

Since I want to use a named pipe to connect unrelated processes, my plan has a process. The process of calling B first through the shell and capturing stdout to get the name of the channel, then both know what to open.

Here I am just worried about whether the name of the pipe will leak. I had never thought about this before until I read this Python post.

+5
source share
2 answers

If you need to use named FIFOs and you need to make sure that a match / rewrite cannot happen, it is best to use some combination of mktemp and mkfifo .

Although mktemp itself cannot create FIFOs, it can be used to create unique temporary directories that you can put in your FIFOs.

This documentation is GNU mktemp .

+2
source

Alternatively, you can create some name containing well random letters. You can read some random bytes from /dev/random (or /dev/urandom , read random (4) ), for example. seed PRNG (e.g. random (3) seeded srandom ) and / or mix PID and time, etc.

And since the named fifo (7) are files, you must use a permission system (and / or ACL ). In particular, you can create a Linux user team to run all your processes and restrict FIFOs to read only, etc.

Of course, and in all cases, you need to “store” or “transfer” these FIFO names securely.

If you run your programs in some bash script, you might want your fifo names to use mktemp (1) as:

 fifoname=$(mktemp -u -t yourprog_XXXXXX).fifo-$RANDOM-$$ mkfifo -m 0600 $fifoname 

(possibly in some cycle). I think this would be safe enough if the script runs on a dedicated user (and then pass $fifoname in some channel or file, and not as a program argument)

The recent renameat2 (2) syscall may be useful (atomicity RENAME_EXCHANGE ).

By the way, you may need SElinux. Remember that open file descriptors - and which include your fifos - are available as symbolic links in proc (5) !

PS. it all depends on how paranoid you are. A well-established Linux system can be quite safe ...

0
source

Source: https://habr.com/ru/post/1206118/


All Articles