Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POSIX shared memory and semaphores permissions set incorrectly by open calls

I'm trying to create a shared memory which will be used by several processes, which will not necessarily be started by the same user, so I create the segment with the following line:

fd = shm_open(SHARE_MEM_NAME,O_RDWR | O_CREAT,0606);

however, when I check out the permissions of the file created in /dev/shm they are:

-rw----r-- 1 lmccauslin lmccauslin 1784 2012-08-10 17:11 /dev/shm/CubeConfigShare not -rw----rw- as I'd expected.

the permissions for /dev/shm are lrwxrwxrwx.

The exact same thing happens with the semaphore created similarly.

kernel version: 3.0.0-23-generic

glibc version: EGLIBC 2.13-20ubuntu5.1

Anyone got any ideas?

like image 962
L.McCauslin Avatar asked Aug 10 '12 21:08

L.McCauslin


People also ask

Is POSIX shared memory?

The POSIX shared memory API allows processes to communicate information by sharing a region of memory. The interfaces employed in the API are: shm_open(3) Create and open a new object, or open an existing object.

What is POSIX semaphore?

A POSIX semaphore structure defines a single semaphore, not an array of up to 25 semaphores. The POSIX semaphore interfaces are shown below. sem_open(3RT) Connects to, and optionally creates, a named semaphore. sem_init(3RT)

Where is POSIX shared memory stored?

A POSIX shared memory object is a memory-mapped file. POSIX shared memory files are provided from a tmpfs filesystem mounted at /dev/shm.

What is the difference between System V and POSIX?

that in System V you can control how much the semaphore count can be increased or decreased; whereas in POSIX, the semaphore count is increased and decreased by 1. V semaphores allow you to change the permissions of semaphores to a subset of the original permission. semaphores. System V semaphores.


1 Answers

It's probably umask.

Citing the manpage of shm_open:

   O_CREAT    Create  the  shared memory object if it does not exist.  The user and
              group ownership of the object are taken from the corresponding effec‐
              tive IDs of the calling process, and the object's permission bits are
              set according to the low-order 9 bits of mode, except that those bits
              set in the process file mode creation mask (see umask(2)) are cleared
              for the new object.  A set of macro constants which can  be  used  to
              define  mode  is  listed  in open(2).  (Symbolic definitions of these
              constants can be obtained by including <sys/stat.h>.)

So, in order to allow creating files which are world-writable, you'd need to set an umask permitting it, for example:

umask(0);

Set like this, umask won't affect any permissions on created files anymore. However, you should note that if you will then create another file without specifying permissions explicitly, it will be world-writable as well.

Thus, you may want to clear the umask only temporarily, and then restore it:

#include <sys/types.h>
#include <sys/stat.h>

...

void yourfunc()
{
    // store old
    mode_t old_umask = umask(0);

    int fd = shm_open(SHARE_MEM_NAME,O_RDWR | O_CREAT,0606);

    // restore old
    umask(old_umask);
}
like image 149
Michał Górny Avatar answered Oct 20 '22 01:10

Michał Górny