@@ -62,6 +62,7 @@ DWORD SharedMemoryException::GetErrorCode() const
62
62
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63
63
// SharedMemoryHelpers
64
64
65
+ const mode_t SharedMemoryHelpers::PermissionsMask_CurrentUser_ReadWriteExecute = S_IRUSR | S_IWUSR | S_IXUSR;
65
66
const mode_t SharedMemoryHelpers::PermissionsMask_AllUsers_ReadWrite =
66
67
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
67
68
const mode_t SharedMemoryHelpers::PermissionsMask_AllUsers_ReadWriteExecute =
@@ -92,10 +93,16 @@ SIZE_T SharedMemoryHelpers::AlignUp(SIZE_T value, SIZE_T alignment)
92
93
return AlignDown (value + (alignment - 1 ), alignment);
93
94
}
94
95
95
- bool SharedMemoryHelpers::EnsureDirectoryExists (const char *path, bool isGlobalLockAcquired, bool createIfNotExist)
96
+ bool SharedMemoryHelpers::EnsureDirectoryExists (
97
+ const char *path,
98
+ bool isGlobalLockAcquired,
99
+ bool createIfNotExist,
100
+ bool isSystemDirectory)
96
101
{
97
102
_ASSERTE (path != nullptr );
103
+ _ASSERTE (!(isSystemDirectory && createIfNotExist)); // should not create or change permissions on system directories
98
104
_ASSERTE (SharedMemoryManager::IsCreationDeletionProcessLockAcquired ());
105
+ _ASSERTE (!isGlobalLockAcquired || SharedMemoryManager::IsCreationDeletionFileLockAcquired ());
99
106
100
107
// Check if the path already exists
101
108
struct stat statInfo;
@@ -155,7 +162,24 @@ bool SharedMemoryHelpers::EnsureDirectoryExists(const char *path, bool isGlobalL
155
162
throw SharedMemoryException (static_cast <DWORD>(SharedMemoryError::IO));
156
163
}
157
164
158
- // Check the directory's permissions and try to update them
165
+ if (isSystemDirectory)
166
+ {
167
+ // For system directories (such as SHARED_MEMORY_TEMP_DIRECTORY_PATH), require sufficient permissions only for the
168
+ // current user. For instance, "docker run --mount ..." to mount /tmp to some directory on the host mounts the
169
+ // destination directory with the same permissions as the source directory, which may not include some permissions for
170
+ // other users. In the docker container, other user permissions are typically not relevant and relaxing the permissions
171
+ // requirement allows for that scenario to work without having to work around it by first giving sufficient permissions
172
+ // for all users.
173
+ if ((statInfo.st_mode & PermissionsMask_CurrentUser_ReadWriteExecute) == PermissionsMask_CurrentUser_ReadWriteExecute)
174
+ {
175
+ return true ;
176
+ }
177
+ throw SharedMemoryException (static_cast <DWORD>(SharedMemoryError::IO));
178
+ }
179
+
180
+ // For non-system directories (such as SHARED_MEMORY_RUNTIME_TEMP_DIRECTORY_PATH), require sufficient permissions for all
181
+ // users and try to update them if requested to create the directory, so that shared memory files may be shared by all
182
+ // processes on the system.
159
183
if ((statInfo.st_mode & PermissionsMask_AllUsers_ReadWriteExecute) == PermissionsMask_AllUsers_ReadWriteExecute)
160
184
{
161
185
return true ;
@@ -214,6 +238,8 @@ int SharedMemoryHelpers::CreateOrOpenFile(LPCSTR path, bool createIfNotExist, bo
214
238
{
215
239
_ASSERTE (path != nullptr );
216
240
_ASSERTE (path[0 ] != ' \0 ' );
241
+ _ASSERTE (SharedMemoryManager::IsCreationDeletionProcessLockAcquired ());
242
+ _ASSERTE (!createIfNotExist || SharedMemoryManager::IsCreationDeletionFileLockAcquired ());
217
243
218
244
// Try to open the file
219
245
int openFlags = O_RDWR;
@@ -1032,7 +1058,8 @@ void SharedMemoryManager::AcquireCreationDeletionFileLock()
1032
1058
if (!SharedMemoryHelpers::EnsureDirectoryExists (
1033
1059
SHARED_MEMORY_TEMP_DIRECTORY_PATH,
1034
1060
false /* isGlobalLockAcquired */ ,
1035
- false /* createIfNotExist */ ))
1061
+ false /* createIfNotExist */ ,
1062
+ true /* isSystemDirectory */ ))
1036
1063
{
1037
1064
throw SharedMemoryException (static_cast <DWORD>(SharedMemoryError::IO));
1038
1065
}
0 commit comments