8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-24 09:23:03 +01:00

Fixed shared memory access rights issues

This commit is contained in:
alexpeshkoff 2009-04-20 10:34:59 +00:00
parent a0bf1bf07c
commit e6d8c3b399

View File

@ -1848,7 +1848,7 @@ void ISC_remove_map_file(const TEXT* filename)
if (key) if (key)
{ {
int shmid = shmget(key, 0, PRIV); int shmid = shmget(key, 0, 0);
if (shmid > 0) if (shmid > 0)
{ {
shmid_ds dummy; shmid_ds dummy;
@ -2069,6 +2069,27 @@ UCHAR* ISC_map_file(ISC_STATUS* status_vector,
#else // no HAVE_MMAP #else // no HAVE_MMAP
static bool setSharedMemoryAccessRights(ISC_STATUS* status_vector, SLONG shmid)
{
char secDb[MAXPATHLEN];
SecurityDatabase::getPath(secDb);
struct stat st;
if (stat(secDb, &st) == 0)
{
shmid_ds ds;
ds.shm_perm.uid = geteuid() == 0 ? st.st_uid : geteuid();
ds.shm_perm.gid = st.st_gid;
ds.shm_perm.mode = st.st_mode;
if (shmctl(shmid, IPC_SET, &ds) == -1)
{
error(status_vector, "shmctl", errno);
return false;
}
}
return true;
}
UCHAR* ISC_map_file(ISC_STATUS* status_vector, UCHAR* ISC_map_file(ISC_STATUS* status_vector,
const TEXT* filename, const TEXT* filename,
FPTR_INIT_GLOBAL_REGION init_routine, FPTR_INIT_GLOBAL_REGION init_routine,
@ -2136,8 +2157,20 @@ UCHAR* ISC_map_file(ISC_STATUS* status_vector,
/* Create the shared memory region if it doesn't already exist. */ /* Create the shared memory region if it doesn't already exist. */
struct shmid_ds buf; struct shmid_ds buf;
SLONG shmid = shmget(key, length, IPC_CREAT | PRIV); SLONG shmid = shmget(key, length, IPC_CREAT | IPC_EXCL | PRIV);
if (shmid == -1) if (shmid == -1) {
shmid = shmget(key, length, 0);
}
else
{
if (!setSharedMemoryAccessRights(status_vector, shmid))
{
fclose(fp);
return NULL;
}
}
if (shmid == -1) {
#ifdef SUPERSERVER #ifdef SUPERSERVER
if (errno == EINVAL) { if (errno == EINVAL) {
/* There are two cases when shmget() returns EINVAL error: /* There are two cases when shmget() returns EINVAL error:
@ -2164,7 +2197,7 @@ UCHAR* ISC_map_file(ISC_STATUS* status_vector,
way to get shmid is to attach to the segment with zero way to get shmid is to attach to the segment with zero
length length
*/ */
if ((shmid = shmget(key, 0, PRIV)) == -1) { if ((shmid = shmget(key, 0, 0)) == -1) {
string msg; string msg;
msg.printf("shmget(0x%x, 0, PRIV)", key); msg.printf("shmget(0x%x, 0, PRIV)", key);
error(status_vector, msg.c_str(), errno); error(status_vector, msg.c_str(), errno);
@ -2193,16 +2226,22 @@ UCHAR* ISC_map_file(ISC_STATUS* status_vector,
fclose(fp); fclose(fp);
return NULL; return NULL;
} }
if (!setSharedMemoryAccessRights(status_vector, shmid))
{
fclose(fp);
return NULL;
}
} }
else /* if errno != EINVAL) */ else /* if errno != EINVAL) */
#endif /* SUPERSERVER */ #endif /* SUPERSERVER */
{ {
string msg; string msg;
msg.printf("shmget(0x%x, %d, IPC_CREAT | PRIV)", key, length); msg.printf("shmget(0x%x, %d, 0)", key, length);
error(status_vector, msg.c_str(), errno); error(status_vector, msg.c_str(), errno);
fclose(fp); fclose(fp);
return NULL; return NULL;
} }
}
#ifdef SUPERSERVER #ifdef SUPERSERVER
/* If we are here there are two possibilities: /* If we are here there are two possibilities:
@ -2242,10 +2281,15 @@ UCHAR* ISC_map_file(ISC_STATUS* status_vector,
fclose(fp); fclose(fp);
return NULL; return NULL;
} }
if (!setSharedMemoryAccessRights(status_vector, shmid))
{
fclose(fp);
return NULL;
}
} }
else { else {
length = buf.shm_segsz; length = buf.shm_segsz;
if ((shmid = shmget(key, length, PRIV)) == -1) { if ((shmid = shmget(key, length, 0)) == -1) {
string msg; string msg;
msg.printf("shmget(0x%x, %d, PRIV)", key, length); msg.printf("shmget(0x%x, %d, PRIV)", key, length);
error(status_vector, msg.c_str(), errno); error(status_vector, msg.c_str(), errno);
@ -2268,7 +2312,7 @@ UCHAR* ISC_map_file(ISC_STATUS* status_vector,
/* Now remap with the new-found length */ /* Now remap with the new-found length */
if ((shmid = shmget(key, length, PRIV)) == -1) { if ((shmid = shmget(key, length, 0)) == -1) {
string msg; string msg;
msg.printf("shmget(0x%x, %d, PRIV)", key, length); msg.printf("shmget(0x%x, %d, PRIV)", key, length);
error(status_vector, msg.c_str(), errno); error(status_vector, msg.c_str(), errno);