Add O_EXCL flag when opening segment for creation
authorAlex Pyrgiotis <apyrgio@grnet.gr>
Fri, 1 Feb 2013 10:52:53 +0000 (12:52 +0200)
committerFilippos Giannakos <philipgian@grnet.gr>
Mon, 11 Mar 2013 09:39:46 +0000 (11:39 +0200)
Security fix. One could potentially pass a wrong argument in xseg_create
and destroy an already initialized segment. Ensuring creation
exclusiveness makes xseg less error-prone.

xseg/drivers/user/xseg_posix.c
xseg/drivers/user/xseg_pthread.c

index 523900b..69b4783 100644 (file)
@@ -54,7 +54,7 @@ char errbuf[ERRSIZE];
 static long posix_allocate(const char *name, uint64_t size)
 {
        int fd, r;
-       fd = shm_open(name, O_RDWR | O_CREAT, 0770);
+       fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0770);
        if (fd < 0) {
                XSEGLOG("Cannot create shared segment: %s\n",
                        strerror_r(errno, errbuf, ERRSIZE));
index 55168bc..d1ddc98 100644 (file)
@@ -57,7 +57,7 @@ static void pthread_mfree(void *mem);
 static long pthread_allocate(const char *name, uint64_t size)
 {
        int fd, r;
-       fd = shm_open(name, O_RDWR | O_CREAT, 0770);
+       fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0770);
        if (fd < 0) {
                XSEGLOG("Cannot create shared segment: %s\n",
                        strerror_r(errno, errbuf, ERRSIZE));