#include <stdint.h>
#include <sys/stat.h>
#include <sys/mman.h>
+#include <sys/types.h>
+#include <grp.h>
/*-----------------------------------------------------------------------------
* MACROS
#define SHM_MAX_ID INT32_MAX
#define SHM_NAME_TEMPLATE "/teec_shm%d"
+
#define SHM_FILE_MODE 0660
/*-----------------------------------------------------------------------------
* Globals
return retSize;
}
+/*
+ * === FUNCTION ======================================================================
+ * Name: set_shm_permissions
+ * Description: Set group and permissions of the shm file underneath shared memory
+ * Parameters: fd_shm - shm file FD
+ * shm_name - shm file name (for logging)
+ * Return: TEEC return value indicating success of failure
+ * =====================================================================================
+ */
+static int32_t set_shm_permissions(int fd_shm, const char *shm_name) {
+ int res;
+ struct group *tee_group = NULL;
+
+ res = fchmod(fd_shm, SHM_FILE_MODE);
+ if (res == -1) {
+ LOGE(TEEC_LIB,
+ "Cannot change permission of the %s shared memory file, error: %s",
+ shm_name, strerror(errno));
+ return TEEC_ERROR_GENERIC;
+ }
+
+ errno = 0;
+ tee_group = getgrnam(TEE_USER_GROUP_NAME);
+ if (!tee_group) {
+ if (!errno) {
+ LOGE(TEEC_LIB,
+ "Failed to get TEE group: group %s does not exist",
+ TEE_USER_GROUP_NAME);
+ } else {
+ LOGE(TEEC_LIB,
+ "Failed to set TEE group, error: %s",
+ strerror(errno));
+ }
+ return TEEC_ERROR_GENERIC;
+ }
+ res = fchown(fd_shm, -1, tee_group->gr_gid);
+ if (res == -1) {
+ LOGE(TEEC_LIB,
+ "Failed to set TEE group of the %s shared memory file, error: %s",
+ shm_name, strerror(errno));
+ return errno == EPERM ? TEEC_ERROR_ACCESS_DENIED : TEEC_ERROR_GENERIC;
+ }
+ return TEEC_SUCCESS;
+}
+
/*
* === FUNCTION ======================================================================
* Name: allocateSharedMemory
goto exit;
}
- res = fchmod(fd_shm, SHM_FILE_MODE);
- if (res == -1) {
- LOGE(TEEC_LIB,
- "Cannot change permission of the %s shared memory file, error: %s",
- shm_name, strerror(errno));
- tee_result = TEEC_ERROR_GENERIC;
+ tee_result = set_shm_permissions(fd_shm, shm_name);
+ if (tee_result != TEEC_SUCCESS)
goto cleanup_shm;
- }
size = alignSize(size);
if (ftruncate(fd_shm, size) == -1) {