From b5be30f5b3f223d1966d501778733ddd35cab818 Mon Sep 17 00:00:00 2001 From: Dongwoo Lee Date: Wed, 2 Aug 2023 14:53:23 +0900 Subject: [PATCH] resource-manager: Fix checking attribute and control id validation In order to prevent misbehavior during code genenration or optimization for builtin function clzll of gcc due to undefined operation, this fixes condition for checking attribute and control id of resources properly. Change-Id: Icef57b1ec27c54afdaf1ef93c94a069c4d8855be Signed-off-by: Dongwoo Lee --- src/resource-manager/resource-manager.c | 74 +++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 17 deletions(-) diff --git a/src/resource-manager/resource-manager.c b/src/resource-manager/resource-manager.c index a9674ec..95d5bd3 100644 --- a/src/resource-manager/resource-manager.c +++ b/src/resource-manager/resource-manager.c @@ -398,11 +398,19 @@ int syscommon_resman_set_resource_control(int resource_id, u_int64_t ctrl_id, const void *data) { const struct syscommon_resman_resource_control *ctrl; - int ctrl_index = RESOURCE_CTRL_INDEX(ctrl_id); + int ctrl_index; int ret; - struct syscommon_resman_resource *resource = find_resource(resource_id); + struct syscommon_resman_resource *resource; + + if (ctrl_id == 0) + return -EINVAL; - if (!resource || ctrl_index < 0 || ctrl_index >= resource->num_ctrls) + resource = find_resource(resource_id); + if (!resource) + return -EINVAL; + + ctrl_index = RESOURCE_CTRL_INDEX(ctrl_id); + if (ctrl_index >= resource->num_ctrls) return -EINVAL; ctrl = &resource->ctrls[ctrl_index]; @@ -420,10 +428,18 @@ const char * syscommon_resman_get_resource_control_name(int resource_id, u_int64_t ctrl_id) { const struct syscommon_resman_resource_control *ctrl; - int ctrl_index = RESOURCE_CTRL_INDEX(ctrl_id); - struct syscommon_resman_resource *resource = find_resource(resource_id); + int ctrl_index; + struct syscommon_resman_resource *resource; - if (!resource || ctrl_index < 0 || ctrl_index >= resource->num_ctrls) + if (ctrl_id == 0) + return NULL; + + resource = find_resource(resource_id); + if (!resource) + return NULL; + + ctrl_index = RESOURCE_CTRL_INDEX(ctrl_id); + if (ctrl_index >= resource->num_ctrls) return NULL; ctrl = &resource->ctrls[ctrl_index]; @@ -434,12 +450,16 @@ syscommon_resman_get_resource_control_name(int resource_id, u_int64_t ctrl_id) static int update_resource_attr(struct syscommon_resman_resource *resource, u_int64_t attr_id) { - int attr_index = RESOURCE_ATTR_INDEX(attr_id); + int attr_index; const struct syscommon_resman_resource_attribute *attr = NULL; struct syscommon_resman_resource_attribute_value *attr_value = NULL; int ret; - if (!resource || attr_index < 0 || attr_index >= resource->num_attrs) + if (!resource || attr_id == 0) + return -EINVAL; + + attr_index = RESOURCE_ATTR_INDEX(attr_id); + if (attr_index >= resource->num_attrs) return -EINVAL; attr = &resource->attrs[attr_index]; @@ -459,12 +479,16 @@ update_resource_attr(struct syscommon_resman_resource *resource, u_int64_t attr_ static int monitor_update_resource_attr(struct syscommon_resman_resource *resource, u_int64_t attr_id) { - int attr_index = RESOURCE_ATTR_INDEX(attr_id); + int attr_index; const struct syscommon_resman_resource_attribute *attr = NULL; struct syscommon_resman_resource_attribute_value *attr_value = NULL; int ret; - if (!resource || attr_index < 0 || attr_index >= resource->num_attrs) + if (!resource || attr_id == 0) + return -EINVAL; + + attr_index = RESOURCE_ATTR_INDEX(attr_id); + if (attr_index >= resource->num_attrs) return -EINVAL; attr = &resource->attrs[attr_index]; @@ -513,9 +537,13 @@ syscommon_resman_monitor_update_resource_attrs(int resource_id) static const struct syscommon_resman_resource_attribute * get_resource_attr(struct syscommon_resman_resource *resource, u_int64_t attr_id) { - int attr_index = RESOURCE_ATTR_INDEX(attr_id); + int attr_index; + + if (!resource || attr_id == 0) + return NULL; - if (!resource || attr_index < 0 || attr_index >= resource->num_attrs) + attr_index = RESOURCE_ATTR_INDEX(attr_id); + if (attr_index >= resource->num_attrs) return NULL; return &resource->attrs[attr_index]; @@ -529,13 +557,16 @@ syscommon_resman_get_resource_attr(int resource_id, u_int64_t attr_id) return get_resource_attr(resource, attr_id); } - static struct syscommon_resman_resource_attribute_value * get_resource_attr_value(struct syscommon_resman_resource *resource, u_int64_t attr_id) { - int attr_index = RESOURCE_ATTR_INDEX(attr_id); + int attr_index; + + if (!resource || attr_id == 0) + return NULL; - if (!resource || attr_index < 0 || attr_index >= resource->num_attrs) + attr_index = RESOURCE_ATTR_INDEX(attr_id); + if (attr_index >= resource->num_attrs) return NULL; return &resource->attrs_value[attr_index]; @@ -574,12 +605,21 @@ static int select_attribute_is_supported_ops(int instance_type, static int is_resource_attr_supported(struct syscommon_resman_resource *resource, u_int64_t attr_id, bool *supported) { const struct syscommon_resman_resource_attribute *attr = NULL; - int attr_index = RESOURCE_ATTR_INDEX(attr_id); + int attr_index; int ret; bool is_supported = false; struct attribute_is_supported_ops ops = { 0 , }; - if (!resource || attr_index < 0 || attr_index >= resource->num_attrs) { + if (!supported) + return -EINVAL; + + if (!resource || attr_id == 0) { + *supported = false; + return -EINVAL; + } + + attr_index = RESOURCE_ATTR_INDEX(attr_id); + if (attr_index >= resource->num_attrs) { *supported = false; return -EINVAL; } -- 2.7.4