- mFileName = kPrefix;
- mFileName += segmentName;
-
- // make the mds directory, just in case it doesn't exist
- mkdir("/var/db/mds", 1777);
- mkdir("/var/db/mds/messages", 0755);
-
- // make the file name
- // clean any old file away
- unlink (mFileName.c_str ());
-
- // open the file
- int segmentDescriptor = open (mFileName.c_str (), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
- if (segmentDescriptor < 0)
- {
- return;
- }
-
- // set the segment size
- ftruncate (segmentDescriptor, segmentSize);
-
- // map it into memory
- mSegment = (u_int8_t*) mmap (NULL, mSegmentSize, PROT_READ | PROT_WRITE, MAP_SHARED, segmentDescriptor, 0);
- close (segmentDescriptor);
-
- if (mSegment == (u_int8_t*) -1) // can't map the memory?
- {
- mSegment = NULL;
- unlink (mFileName.c_str());
- }
-
- mDataPtr = mDataArea = mSegment + sizeof(SegmentOffsetType);
- mDataMax = mSegment + segmentSize;;
-
- SetProducerOffset (0);
+ const mode_t perm1777 = S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO;
+ const mode_t perm0755 = S_IRWXU | (S_IRGRP | S_IXGRP) | (S_IROTH | S_IXOTH);
+ const mode_t perm0600 = (S_IRUSR | S_IWUSR);
+
+ // make the mds directory, just in case it doesn't exist
+ if (mUID == 0) {
+ makedir(SharedMemoryCommon::kMDSDirectory, perm1777);
+ makedir(SharedMemoryCommon::kMDSMessagesDirectory, perm0755);
+ } else {
+ // Assume kMDSMessagesDirectory was created first by securityd
+ std::string uidstr = std::to_string(mUID);
+ std::string upath = SharedMemoryCommon::kMDSMessagesDirectory;
+ upath += "/" + uidstr;
+ makedir(upath.c_str(), perm0755);
+ }
+ mFileName = SharedMemoryCommon::SharedMemoryFilePath(segmentName, uid);
+
+ // make the file name
+ // clean any old file away
+ unlinkfile(mFileName.c_str());
+
+ // open the file
+ secdebug("MDSPRIVACY","creating %s",mFileName.c_str ());
+ if(mUID != 0) {
+ mBackingFile = open (mFileName.c_str (), O_RDWR | O_CREAT | O_EXCL, perm0600);
+ }
+ else {
+ mBackingFile = open (mFileName.c_str (), O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+ }
+
+ if (mBackingFile < 0)
+ {
+ secdebug("MDSPRIVACY","creation of %s failed", mFileName.c_str());
+ return;
+ }
+
+ int rx = fchown(mBackingFile, uid, gid);
+ if (rx) {
+ secdebug("MDSPRIVACY","chown of %s to %d/%d failed : %d", mFileName.c_str(), uid, gid, rx);
+ }
+
+ // set the segment size
+ ftruncate (mBackingFile, segmentSize);
+
+ // map it into memory
+ mSegment = (u_int8_t*) mmap (NULL, mSegmentSize, PROT_READ | PROT_WRITE, MAP_SHARED, mBackingFile, 0);
+
+ if (mSegment == MAP_FAILED) // can't map the memory?
+ {
+ mSegment = NULL;
+ unlinkfile(mFileName.c_str());
+ } else {
+ mDataPtr = mDataArea = mSegment + sizeof(SegmentOffsetType);
+ mDataMax = mSegment + segmentSize;;
+
+ SetProducerOffset (0);
+ }