手表平台使用cJsonCreateNum接口异常,整改flags保存接口

Signed-off-by: ohos_vae <liwei331@huawei.com>
This commit is contained in:
ohos_vae 2021-07-21 19:19:51 +08:00
parent 650338d51f
commit 00ec2c6a54
10 changed files with 141 additions and 100 deletions

View File

@ -118,6 +118,22 @@ int GrantPermission(const char *identifier, const char *permName);
*/
int RevokePermission(const char *identifier, const char *permName);
/**
* @brief Update a specified runtime permission flags.
*
* @param uid Indicates the user ID of the application. The range is [0, INT_MAX].
* @param permissionName Indicates the pointer to the permission name.
* @param flags Indicates the permission flags of the permission.
* @return Returns <b>0</b> if the permission flags is successfully updated; returns an error code defined in
* {@link PmsErrorCode} otherwise.
*
* @since 1.0
* @version 1.0
*
*/
int UpdatePermissionFlags(const char *identifier, const char *permissionName, const int flags);
/**
* @brief Grants a specified runtime permission to an application.
*
@ -152,21 +168,6 @@ int GrantRuntimePermission(int uid, const char *permissionName);
*/
int RevokeRuntimePermission(int uid, const char *permissionName);
/**
* @brief Update a specified runtime permission flags.
*
* @param uid Indicates the user ID of the application. The range is [0, INT_MAX].
* @param permissionName Indicates the pointer to the permission name.
* @param flags Indicates the permission flags of the permission.
* @return Returns <b>0</b> if the permission flags is successfully updated; returns an error code defined in
* {@link PmsErrorCode} otherwise.
*
* @since 1.0
* @version 1.0
*
*/
int UpdateRuntimePermissionFlags(int uid, const char *permissionName, const int flags);
#ifdef __cplusplus
#if __cplusplus
}

View File

@ -93,11 +93,6 @@ enum PmsFlagDef {
* Permission flag mask: Indicates the valid flag definition.
*/
PMS_FLAG_VALID_MASK = 0x2,
/**
* Permission flag: Indicates the value of flag not modify.
*/
PMS_FLAG_NOT_MODIFY = 0xffffffff,
};
/**

View File

@ -20,7 +20,7 @@
int PermissionIsGranted(const TList *list, int uid, const char *permission);
int ModifyPermission(TNode *node, const char *permission, const PermissionSaved *perms);
int ModifyPermission(TNode *node, const char *permission, const enum IsGranted granted);
void AddTask(TList *list, TNode *node);
@ -30,4 +30,8 @@ TNode *GetTaskWithUid(TList *list, int uid);
TNode *GetTaskWithPkgName(TList *list, const char *pkgName);
int PmsAtoI(char *input);
char *PmsItoA(int num, char *output, int len);
#endif // PERM_OPERATE_H

View File

@ -48,12 +48,12 @@ int GrantPermission(const char *identifier, const char *permName);
int RevokePermission(const char *identifier, const char *permName);
int UpdatePermissionFlags(const char *identifier, const char *permissionName, const int flags);
int GrantRuntimePermission(int uid, const char *permissionName);
int RevokeRuntimePermission(int uid, const char *permissionName);
int UpdateRuntimePermissionFlags(int uid, const char *permissionName, const int flags);
int GetDevUdid(unsigned char *udid, int size);
#ifdef __cplusplus

View File

@ -35,7 +35,7 @@ typedef struct PmsInnerApi {
int (*GrantRuntimePermission)(int uid, const char *permissionName);
int (*RevokeRuntimePermission)(int uid, const char *permissionName);
int (*GetDevUdid)(unsigned char *udid, int size);
int (*UpdateRuntimePermissionFlags)(int uid, const char *permissionName, int flags);
int (*UpdatePermissionFlags)(const char *identifier, const char *permissionName, int flags);
} PmsInnerApi;
typedef struct PmsInner {

View File

@ -14,13 +14,15 @@
*/
#include "perm_operate.h"
#include <string.h>
#include "hal_pms.h"
#define RET_OK 0
#define RET_NOK (-1)
#define VAL_NEN (16)
#define POSITIVE (1)
#define NEGATIVE (-1)
#define DECIMAL (10)
int PermissionIsGranted(const TList *list, int uid, const char *permission)
{
@ -40,17 +42,14 @@ int PermissionIsGranted(const TList *list, int uid, const char *permission)
return RET_NOK;
}
int ModifyPermission(TNode *node, const char *permission, const PermissionSaved *perms)
int ModifyPermission(TNode *node, const char *permission, const enum IsGranted granted)
{
if (node == NULL || permission == NULL || perms == NULL) {
if (node == NULL || permission == NULL) {
return RET_NOK;
}
for (int i = 0; i < node->permNum; i++) {
if (strcmp(node->permList[i].name, permission) == 0) {
node->permList[i].granted = perms->granted;
if (perms->flags != PMS_FLAG_NOT_MODIFY) {
node->permList[i].flags = perms->flags;
}
node->permList[i].granted = granted;
return RET_OK;
}
}
@ -109,4 +108,56 @@ TNode *GetTaskWithPkgName(TList *list, const char *pkgName)
cur = cur->next;
}
return NULL;
}
}
int PmsAtoI(char *input)
{
if (input == NULL) {
return RET_NOK;
}
while ((*input) == ' ') {
input++;
}
int sign = ((*input == '-') ? NEGATIVE : POSITIVE);
if ((*input == '+') || (*input == '-')) {
input++;
}
int num = 0;
while (*input != '\0') {
num = (num * DECIMAL) + (*input - '0');
input++;
}
return (num * sign);
}
char *PmsItoA(int num, char *output, int len)
{
char temp[VAL_NEN] = {0};
int sign = num;
int i = 0, j = 0;
if (sign < 0) {
num = -num;
}
do {
temp[i++] = num % DECIMAL + '0';
num /= DECIMAL;
} while (num > 0);
if (sign < 0) {
temp[i++] = '-';
}
temp[i--] = '\0';
for (j = 0; i >= 0; j++, i--) {
output[j] = temp[i];
}
output[j] = '\0';
return output;
}

View File

@ -26,8 +26,8 @@
#include "hal_pms.h"
#include "perm_operate.h"
#define P_NAME_MAXLEN 32
#define BUFF_SIZE 1024
#define BUFF_SIZE_16 (16)
#define BUFF_SIZE_1024 (1024)
#define FIELD_PERMISSION "permissions"
#define FIELD_NAME "name"
#define FIELD_DESC "desc"
@ -43,7 +43,7 @@ static struct TaskList g_taskList = {
static char *ConcatString(const char *s1, const char *s2)
{
unsigned int allocSize = strlen(s1) + strlen(s2) + 1;
if (allocSize > BUFF_SIZE) {
if (allocSize > BUFF_SIZE_1024) {
return NULL;
}
char *rst = (char *) HalMalloc(allocSize);
@ -164,7 +164,7 @@ static int ParseNewPermissionsItem(const cJSON *object, PermissionSaved *perms)
{
cJSON *itemFlags = cJSON_GetObjectItem(object, FIELD_FLAGS);
if (itemFlags != NULL) {
perms->flags = (enum PmsFlagDef)itemFlags->valuedouble;
perms->flags = PmsAtoI(itemFlags->valuestring);
} else {
perms->flags = PMS_FLAG_DEFAULT;
}
@ -238,6 +238,7 @@ static int SavePermissions(const char *identifier, const PermissionSaved *permis
cJSON *root = NULL;
cJSON *array = NULL;
char *path = NULL;
char buf[BUFF_SIZE_16] = {0};
root = cJSON_CreateObject();
if (root == NULL) {
return PERM_ERRORCODE_MALLOC_FAIL;
@ -264,7 +265,7 @@ static int SavePermissions(const char *identifier, const PermissionSaved *permis
cJSON_AddItemToObject(object, FIELD_NAME, cJSON_CreateString(permissions[i].name));
cJSON_AddItemToObject(object, FIELD_DESC, cJSON_CreateString(permissions[i].desc));
cJSON_AddItemToObject(object, FIELD_IS_GRANTED, cJSON_CreateBool(permissions[i].granted));
cJSON_AddItemToObject(object, FIELD_FLAGS, cJSON_CreateNumber(permissions[i].flags));
cJSON_AddItemToObject(object, FIELD_FLAGS, cJSON_CreateString(PmsItoA(permissions[i].flags, buf, BUFF_SIZE_16)));
cJSON_AddItemToArray(array, object);
}
cJSON_AddItemToObject(root, FIELD_PERMISSION, array);
@ -585,7 +586,7 @@ int CheckPermissionStat(int uid, const char *permissionName)
return ret;
}
static int OnPermissionFileSync(const char *identifier, const char *permName, const PermissionSaved *perms)
static int OnPermissionFileSync(const char *identifier, const char *permName, const enum IsGranted granted)
{
int retCode = PERM_ERRORCODE_SUCCESS;
bool isSave = false;
@ -597,12 +598,32 @@ static int OnPermissionFileSync(const char *identifier, const char *permName, co
}
for (int i = 0; i < permNum; i++) {
if (strcmp(permissions[i].name, permName) == 0) {
isSave = permissions[i].granted ^ perms->granted;
permissions[i].granted = perms->granted;
if (perms->flags != PMS_FLAG_NOT_MODIFY) {
isSave |= permissions[i].flags ^ perms->flags;
permissions[i].flags = perms->flags;
}
isSave = permissions[i].granted ^ granted;
permissions[i].granted = granted;
break;
}
}
if (isSave) {
retCode = SavePermissions(identifier, permissions, permNum);
}
HalFree(permissions);
return retCode;
}
static int OnPermissionFlagsFileSync(const char *identifier, const char *permName, const int flags)
{
int retCode = PERM_ERRORCODE_SUCCESS;
bool isSave = false;
PermissionSaved *permissions = NULL;
int permNum = 0;
int ret = QueryPermission(identifier, &permissions, &permNum);
if (ret != PERM_ERRORCODE_SUCCESS) {
return ret;
}
for (int i = 0; i < permNum; i++) {
if (strcmp(permissions[i].name, permName) == 0) {
isSave = permissions[i].flags ^ flags;
permissions[i].flags = flags;
break;
}
}
@ -619,14 +640,10 @@ int GrantPermission(const char *identifier, const char *permName)
return PERM_ERRORCODE_INVALID_PARAMS;
}
int ret = PERM_ERRORCODE_SUCCESS;
PermissionSaved perms = {
.granted = GRANTED,
.flags = PMS_FLAG_NOT_MODIFY,
};
HalMutexLock();
TNode *node = GetTaskWithPkgName(&g_taskList, identifier);
if (node != NULL) {
ret = ModifyPermission(node, permName, &perms);
ret = ModifyPermission(node, permName, GRANTED);
}
HalMutexUnlock();
@ -634,7 +651,7 @@ int GrantPermission(const char *identifier, const char *permName)
return PERM_ERRORCODE_PERM_NOT_EXIST;
}
return OnPermissionFileSync(identifier, permName, &perms);
return OnPermissionFileSync(identifier, permName, GRANTED);
}
int RevokePermission(const char *identifier, const char *permName)
@ -643,14 +660,10 @@ int RevokePermission(const char *identifier, const char *permName)
return PERM_ERRORCODE_INVALID_PARAMS;
}
int ret = PERM_ERRORCODE_SUCCESS;
PermissionSaved perms = {
.granted = NOT_GRANTED,
.flags = PMS_FLAG_NOT_MODIFY,
};
HalMutexLock();
TNode *node = GetTaskWithPkgName(&g_taskList, identifier);
if (node != NULL) {
ret = ModifyPermission(node, permName, &perms);
ret = ModifyPermission(node, permName, NOT_GRANTED);
}
HalMutexUnlock();
@ -658,7 +671,7 @@ int RevokePermission(const char *identifier, const char *permName)
return PERM_ERRORCODE_PERM_NOT_EXIST;
}
return OnPermissionFileSync(identifier, permName, &perms);
return OnPermissionFileSync(identifier, permName, NOT_GRANTED);
}
int GrantRuntimePermission(int uid, const char *permissionName)
@ -666,10 +679,6 @@ int GrantRuntimePermission(int uid, const char *permissionName)
if (permissionName == NULL) {
return PERM_ERRORCODE_INVALID_PARAMS;
}
PermissionSaved perms = {
.granted = GRANTED,
.flags = PMS_FLAG_NOT_MODIFY,
};
HalMutexLock();
TNode *node = GetTaskWithUid(&g_taskList, uid);
@ -678,13 +687,13 @@ int GrantRuntimePermission(int uid, const char *permissionName)
return PERM_ERRORCODE_TASKID_NOT_EXIST;
}
int ret = ModifyPermission(node, permissionName, &perms);
int ret = ModifyPermission(node, permissionName, GRANTED);
HalMutexUnlock();
if (ret < 0) {
return PERM_ERRORCODE_PERM_NOT_EXIST;
}
return OnPermissionFileSync(node->pkgName, permissionName, &perms);
return OnPermissionFileSync(node->pkgName, permissionName, GRANTED);
}
int RevokeRuntimePermission(int uid, const char *permissionName)
@ -692,10 +701,6 @@ int RevokeRuntimePermission(int uid, const char *permissionName)
if (permissionName == NULL) {
return PERM_ERRORCODE_INVALID_PARAMS;
}
PermissionSaved perms = {
.granted = NOT_GRANTED,
.flags = PMS_FLAG_NOT_MODIFY,
};
HalMutexLock();
TNode *node = GetTaskWithUid(&g_taskList, uid);
@ -704,13 +709,13 @@ int RevokeRuntimePermission(int uid, const char *permissionName)
return PERM_ERRORCODE_TASKID_NOT_EXIST;
}
int ret = ModifyPermission(node, permissionName, &perms);
int ret = ModifyPermission(node, permissionName, NOT_GRANTED);
HalMutexUnlock();
if (ret < 0) {
return PERM_ERRORCODE_PERM_NOT_EXIST;
}
return OnPermissionFileSync(node->pkgName, permissionName, &perms);
return OnPermissionFileSync(node->pkgName, permissionName, NOT_GRANTED);
}
int GetDevUdid(unsigned char *udid, int size)
@ -723,27 +728,11 @@ int GetDevUdid(unsigned char *udid, int size)
return HalGetDevUdid(udid, size);
}
int UpdateRuntimePermissionFlags(int uid, const char *permissionName, const int flags)
int UpdatePermissionFlags(const char *identifier, const char *permissionName, const int flags)
{
if ((permissionName == NULL) || !IsValidFlags(flags)) {
if ((identifier == NULL) || (permissionName == NULL) || !IsValidFlags(flags)) {
return PERM_ERRORCODE_INVALID_PARAMS;
}
PermissionSaved perms = {
.flags = flags,
};
}
HalMutexLock();
TNode *node = GetTaskWithUid(&g_taskList, uid);
if (node == NULL) {
HalMutexUnlock();
return PERM_ERRORCODE_TASKID_NOT_EXIST;
}
perms.granted = (enum IsGranted)PermissionIsGranted(&g_taskList, uid, permissionName);
int ret = ModifyPermission(node, permissionName, &perms);
HalMutexUnlock();
if (ret < 0) {
return PERM_ERRORCODE_PERM_NOT_EXIST;
}
return OnPermissionFileSync(node->pkgName, permissionName, &perms);
return OnPermissionFlagsFileSync(identifier, permissionName, flags);
}

View File

@ -43,7 +43,7 @@ static PmsInner g_permlite = {
.GrantRuntimePermission = GrantRuntimePermission,
.RevokeRuntimePermission = RevokeRuntimePermission,
.GetDevUdid = GetDevUdid,
.UpdateRuntimePermissionFlags = UpdateRuntimePermissionFlags,
.UpdatePermissionFlags = UpdatePermissionFlags,
DEFAULT_IUNKNOWN_ENTRY_END,
.identity = {-1, -1, NULL},
};

View File

@ -40,7 +40,7 @@ typedef struct InnerPermLiteApi {
int (*GrantRuntimePermission)(int uid, const char *permissionName);
int (*RevokeRuntimePermission)(int uid, const char *permissionName);
int (*GetDevUdid)(unsigned char *udid, int size);
int (*UpdateRuntimePermissionFlags)(int uid, const char *permissionName, const int flags);
int (*UpdatePermissionFlags)(const char *identifier, const char *permissionName, const int flags);
} InnerPermLiteApi;
typedef struct InnerPermLite {
@ -79,7 +79,7 @@ static InnerPermLite g_permlite = {
.GrantRuntimePermission = GrantRuntimePermission,
.RevokeRuntimePermission = RevokeRuntimePermission,
.GetDevUdid = GetDevUdid,
.UpdateRuntimePermissionFlags = UpdateRuntimePermissionFlags,
.UpdatePermissionFlags = UpdatePermissionFlags,
IPROXY_END,
.identity = {-1, -1, NULL},
};
@ -212,12 +212,13 @@ static void ReplyUpdatePermissionFlags(const void *origin, IpcIo *req, IpcIo *re
uid_t callingUid = GetCallingUid(origin);
HILOG_INFO(HILOG_MODULE_APP, "Enter ID_UPDATE_PERMS_FLAGS, [callerPid: %d][callerUid: %u]", callingPid, callingUid);
size_t permLen = 0;
int64_t uid = IpcIoPopInt64(req);
size_t idLen = 0;
char *identifier = (char *)IpcIoPopString(req, &idLen);
char *permName = (char *)IpcIoPopString(req, &permLen);
int32_t flags = IpcIoPopInt32(req);
int32_t ret = api->UpdateRuntimePermissionFlags(uid, permName, flags);
HILOG_INFO(HILOG_MODULE_APP, "update runtime permission flags, [uid: %lld][perm: %s][flags:%d][ret: %d]", uid,
permName, flags, ret);
int32_t ret = api->UpdatePermissionFlags(identifier, permName, flags);
HILOG_INFO(HILOG_MODULE_APP, "update runtime permission flags, [identifier: %s][perm: %s][flags:%d][ret: %d]",
identifier, permName, flags, ret);
IpcIoPushInt32(reply, ret);
}

View File

@ -70,7 +70,7 @@ typedef struct InnerClientApi {
int (*GrantRuntimePermission)(int uid, const char *permissionName);
int (*RevokeRuntimePermission)(int uid, const char *permissionName);
int (*RequestDevUdid)(unsigned char *udid, int size);
int (*UpdateRuntimePermissionFlags)(int uid, const char *permissionName, int flags);
int (*UpdatePermissionFlags)(const char *identifier, const char *permissionName, int flags);
} InnerClientApi;
typedef struct ClientInnerEntry {
@ -166,7 +166,7 @@ void *CreatInnerClient(const char *service, const char *feature, uint32 size)
entry->iUnknown.GrantRuntimePermission = GrantRuntimePermission;
entry->iUnknown.RevokeRuntimePermission = RevokeRuntimePermission;
entry->iUnknown.RequestDevUdid = RequestDevUdid;
entry->iUnknown.UpdateRuntimePermissionFlags = UpdateRuntimePermissionFlags;
entry->iUnknown.UpdatePermissionFlags = UpdatePermissionFlags;
return client;
}
@ -473,7 +473,7 @@ int RequestDevUdid(unsigned char *udid, int size)
return ret.result;
}
int UpdateRuntimePermissionFlags(int uid, const char *permissionName, const int flags)
int UpdatePermissionFlags(const char *identifier, const char *permissionName, const int flags)
{
InnerClientApi *proxy = GetInnerClientApi();
if (proxy == NULL) {
@ -482,7 +482,7 @@ int UpdateRuntimePermissionFlags(int uid, const char *permissionName, const int
IpcIo request;
char data[MAX_DATA_LEN];
IpcIoInit(&request, data, MAX_DATA_LEN, 0);
IpcIoPushInt64(&request, uid);
IpcIoPushString(&request, identifier);
IpcIoPushString(&request, permissionName);
IpcIoPushInt32(&request, flags);
int32_t ret;