diff --git a/src/qosmngr.c b/src/qosmngr.c index b167ffcd394295094d5d4ab715bdb76721c743a8..86be094574f811a8c6582b048b44e807af8bebe1 100644 --- a/src/qosmngr.c +++ b/src/qosmngr.c @@ -21,6 +21,7 @@ * 02110-1301 USA */ +#include <libubox/blobmsg_json.h> #include <libubus.h> #include <uci.h> @@ -36,7 +37,13 @@ #define MIN_INDEX 48 #define MAX_INDEX 57 -int test_flag = 0; +/* Used for fetching keys from ubus json reqest */ +#define SEPERATOR 44 +#define QUOTE 34 + +/* Used to validate requested parameters */ +#define PARAM1 "ifname" +#define PARAM2 "qid" static int init_flag = 1; static struct qos_stats **q_stat = {0}; @@ -302,6 +309,61 @@ static int get_stats_for_all_intf(struct blob_buf *b, struct qos_stats *stats, v return ret; } +/** + * validate_keys function to validate requested json keys + * @param rea_json parameter pointer to char string containing json request + * retrun integer value 0 on success and -1 on failure + */ +static int validate_keys(char *req_json) +{ + int i; + int ret = 0; + + int len = strlen(req_json); + + for (i = 0; i < len; i++) { + char key[IFNAMSIZ] = {0}; + if (req_json[i] == QUOTE) { + int j = 0; + i++; + while (req_json[i] != QUOTE) { + key[j] = req_json[i]; + j++; + i++; + } + i++; + while ((req_json[i] != SEPERATOR) && (i < len)) + i++; + + if (!(!strncmp(key, PARAM1, strlen(PARAM1)) || + !strncmp(key, PARAM2, strlen(PARAM2)))) { + syslog(LOG_ERR, "ERROR :: unknown parameter : %s\n", key); + return -1; + } + } + } + + return ret; +} + +/** + * validate_request function to validate requested blob message + * @param msg parameter pointer to blob_attr containing blob request + * retrun integer value 0 on success and -1 on failure + */ +static int validate_request(struct blob_attr *msg) +{ + int ret = 0; + char *json_blob = NULL; + + if (msg) { + json_blob = blobmsg_format_json(msg, true); + ret = validate_keys(json_blob); + } + + return ret; +} + /** * qosmngr_get_stats function callback on ubus method queue_stats * @param ctx input parameter pointer to ubus context @@ -315,7 +377,7 @@ int qosmngr_get_stats(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { - int ret; + int ret = 0; int qid = QOS_QUEUE_ANY; char ifname[IFNAMSIZ] = {0}; @@ -323,6 +385,13 @@ int qosmngr_get_stats(struct ubus_context *ctx, struct ubus_object *obj, struct blob_buf b = {0}; struct qos_stats stats = {0}; + /* Validate requested parameters, i.e., ifname and qid */ + ret = validate_request(msg); + if (ret) { + syslog(LOG_ERR, "validate_request failed : ret %d\n", ret); + return UBUS_STATUS_INVALID_ARGUMENT; + } + /* These are for the blobbuf array elements */ void *d = NULL, *dd = NULL;