Discussion:
Messaging and GCC - "use of `mktemp' is dangerous" warning
(too old to reply)
Brendan
2006-05-02 01:19:43 UTC
Permalink
Hi,

I'm trying to mimic the IPC/messaging system of an specific OS in a
portable way by using GCC's library. The IPC system uses buffered
asynchronous messages, where any thread can send a message to any other
thread (i.e. to the "threadID") without blocking, and the receiver does
any security checks necessary.

I'm trying to implement the portable/linux version on top of
sockets/datagrams ("SOCK_DGRAM" in the local namespace), and so far
it's working. The first problem I had is that you can't send a datagram
directly to a PID. To get around this, each process creates a temporary
file for it's socket. When any process is created the file name it's
parent used is passed as a command line argument ("excl"), and the new
process uses this information to send an "init" message back to it's
parent containing the file name it used for it's socket. When the
parent receives this "init" message, it broadcasts a "new process"
message to all previous processes within the application. It's an ugly
mess, but after all processes have started (and built a "directory" of
message port ID's) it does work like the IPC system I'm trying to
mimic.

To allow several instances of the application to run at the same time
(without file name conflicts), I'm using the "mktemp" function to
create unique file names for each process's socket.

Unfortunately, GCC keeps reporting warnings - "warning: the use of
`mktemp' is dangerous, better use `mkstemp'".

Is there a way to do this more securely or more cleanly? The "mkstemp"
function isn't quite the same as "bind", and using "mkstemp" and then
closing/deleting the file before calling "bind" seems stupid (it's just
as insecure and messier).

Alternatively, is there a way to disable this warning? For an open
source project, it's a little embarassing when the compiler decides to
tell others that your code is "dangerous" (especially if there is no
viable alternative)....


Thanks,

Brendan
Paul Pluzhnikov
2006-05-02 16:02:52 UTC
Permalink
Post by Brendan
Unfortunately, GCC keeps reporting warnings - "warning: the use of
`mktemp' is dangerous, better use `mkstemp'".
Actually, the complaint comes from /usr/bin/ld as 'gcc' knows
nothing about mktemp().

This is implemented as a special .gnu.warning.mktemp section in
libc.so.6, which ld simply prints:

$ objdump -sj.gnu.warning.mktemp /lib/libc.so.6

/lib/libc.so.6: file format elf32-i386

Contents of section .gnu.warning.mktemp:
0000 74686520 75736520 6f662060 6d6b7465 the use of `mkte
0010 6d702720 69732064 616e6765 726f7573 mp' is dangerous
0020 2c206265 74746572 20757365 20606d6b , better use `mk
0030 7374656d 702700 stemp'.
Post by Brendan
Is there a way to do this more securely or more cleanly?
AFAICT from your description, you don't really need mktemp, and
could use a simple "/tmp/my-fancy-messaging-emulator-<pid>" instead.
Post by Brendan
Alternatively, is there a way to disable this warning?
You could strip the section from libc.so.6, but that cure is likely
worse then the disease.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Brendan
2006-05-02 17:52:22 UTC
Permalink
Hi,
Post by Paul Pluzhnikov
AFAICT from your description, you don't really need mktemp, and
could use a simple "/tmp/my-fancy-messaging-emulator-<pid>" instead.
You're right! :-)

I just spent 4 hours changing to "mkdtemp()" - a securely created
temporary directory containing non-random file names in the form of
"/tmp/<mkdtemp-name>/<pretend-message-portID>", but this doesn't allow
for communication between seperate applications.

Your "/tmp/my-fancy-messaging-emulator-<pid>" idea would be simpler and
more like the IPC system I'm trying to mimic....


Thanks,

Brendan

Loading...