Search code examples
unixposixipcmessage-queue

POSIX message queues permission denied issue


I have a server.c program that is initialising a message queue with the following permissions:

#define SERVER "/serverqueue"
...
struct mq_attr attr;
attr.mq_flags = 0;
attr.mq_maxmsg = MAX_MSGS;
attr.mq_msgsize = MAX_MSG_SIZE;
attr.mq_curmsgs = 0;
server = mq_open(SERVER, O_RDWR | O_CREAT, 666, &attr)
...

In the first run, the mq_open() is successful and the program exits with no error. On subsequent executions, I get Permission denied errors at mq_open(). Why is this happening?
In case its relevant, I am not explicitly closing/unlinking the message queue descriptors as the OS does that automatically when the program exits, if i am not wrong


Solution

  • Message queues persist after process exit. The reason the second creation attempt fails is because you specify the mode as 666, which results rather strange permissions:

    $ ls -l /dev/mqueue/serverqueue
    --w--wx--T. 1 fw fw 80 Feb 17 13:13 serverqueue
    

    There are no read permissions, so opening with O_RDWR fails.

    Furthermore, since the queue names are a shared resource, it usually results in a security vulnerability if you create queues with O_CREAT instead of O_CREAT | O_EXCL. Another user could have created the same queue, with different permissions, and thus gain access to what you are trying to do with the queue.