From d8e7609abbda70ea5daae8de40749e7377bf99c2 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Mon, 3 Sep 2012 20:15:50 +0200 Subject: bugfix: specifying json root in a template lead to abort MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thanks to Miloslav Trmač for alerting us of this problem. --- runtime/msg.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/runtime/msg.c b/runtime/msg.c index a89a01d2..afc79042 100644 --- a/runtime/msg.c +++ b/runtime/msg.c @@ -2420,16 +2420,20 @@ getCEEPropVal(msg_t *pM, es_str_t *propName, uchar **pRes, int *buflen, unsigned if(*pbMustBeFreed) free(*pRes); *pRes = NULL; -dbgprintf("AAAA: enter getCEEProp\n"); +dbgprintf("AAAA: enter getCEEPropVal\n"); // TODO: mutex? if(pM->json == NULL) goto finalize_it; - name = (uchar*)es_str2cstr(propName, NULL); + if(!es_strbufcmp(propName, (uchar*)"!", 1)) { + field = pM->json; + } else { + name = (uchar*)es_str2cstr(propName, NULL); dbgprintf("AAAA: name to search '%s'\n", name); - leaf = jsonPathGetLeaf(name, ustrlen(name)); + leaf = jsonPathGetLeaf(name, ustrlen(name)); dbgprintf("AAAA: leaf '%s'\n", leaf); - CHKiRet(jsonPathFindParent(pM, name, leaf, &parent, 1)); - field = json_object_object_get(parent, (char*)leaf); + CHKiRet(jsonPathFindParent(pM, name, leaf, &parent, 1)); + field = json_object_object_get(parent, (char*)leaf); + } *pRes = (uchar*) strdup(json_object_get_string(field)); dbgprintf("AAAA: json_object_get_string() returns '%s'\n", *pRes); *buflen = (int) ustrlen(*pRes); @@ -2462,6 +2466,10 @@ dbgprintf("AAAA: enter getCEEPropJSON\n"); ABORT_FINALIZE(RS_RET_NOT_FOUND); } + if(!es_strbufcmp(propName, (uchar*)"!", 1)) { + *pjson = pM->json; + FINALIZE; + } name = (uchar*)es_str2cstr(propName, NULL); dbgprintf("AAAA: name to search '%s'\n", name); leaf = jsonPathGetLeaf(name, ustrlen(name)); -- cgit v1.2.3 From b38fa5219f2f8ae3b6366ab911d9662f9340f70d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Mon, 3 Sep 2012 17:53:56 +0200 Subject: Set value of a mandatory JSON field to null MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miloslav Trmač --- template.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/template.c b/template.c index 6bcca42b..74a87c99 100644 --- a/template.c +++ b/template.c @@ -307,14 +307,9 @@ rsRetVal tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjso } else { DBGPRINTF("tplToJSON: error %d looking up property\n", localRet); -#if 0 /* TODO: as it looks, there currently is no way to define Null field values in json-c... - we need to think how we will handle that. - */ if(pTpe->data.field.options.bMandatory) { - jsonf = json_object_new(json_type_null); //json_object_new_null(); - json_object_object_add(json, (char*)pTpe->fieldName, jsonf); + json_object_object_add(json, (char*)pTpe->fieldName, NULL); } -#endif } } else { pVal = (uchar*) MsgGetProp(pMsg, pTpe, pTpe->data.field.propid, -- cgit v1.2.3 From 080825632bd32e6baae1afde00df307fd4237ee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Mon, 3 Sep 2012 18:44:17 +0200 Subject: Free a temporary object for the "name" property MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes a memory leak. Another option is to use cstrConstructFromESStr, but that would change semantics of NUL handling. Signed-off-by: Miloslav Trmač --- template.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/template.c b/template.c index 74a87c99..5d933619 100644 --- a/template.c +++ b/template.c @@ -1328,9 +1328,12 @@ createPropertyTpe(struct template *pTpl, struct cnfobj *o) if(!pvals[i].bUsed) continue; if(!strcmp(pblkProperty.descr[i].name, "name")) { - rsCStrConstructFromszStr(&name, - (uchar*)es_str2cstr(pvals[i].val.d.estr, NULL)); + char *tmp; + + tmp = es_str2cstr(pvals[i].val.d.estr, NULL); + rsCStrConstructFromszStr(&name, (uchar*)tmp); cstrFinalize(name); + free(tmp); } else if(!strcmp(pblkProperty.descr[i].name, "droplastlf")) { droplastlf = pvals[i].val.d.n; } else if(!strcmp(pblkProperty.descr[i].name, "mandatory")) { -- cgit v1.2.3 From d8f00298183f7a65f9a5ab9f10b851e2c2c6746c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Mon, 3 Sep 2012 18:46:08 +0200 Subject: Fix a memory leak in "outname" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In all cases, the variable already contains a newly allocated copy. Signed-off-by: Miloslav Trmač --- template.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template.c b/template.c index 5d933619..074e12b5 100644 --- a/template.c +++ b/template.c @@ -1540,7 +1540,7 @@ createPropertyTpe(struct template *pTpl, struct cnfobj *o) pTpe->data.field.options.bSecPathReplace = 1; break; } - pTpe->fieldName = ustrdup(outname); + pTpe->fieldName = outname; if(outname != NULL) pTpe->lenFieldName = ustrlen(outname); pTpe->data.field.eDateFormat = datefmt; -- cgit v1.2.3 From c27f5446c982c4caebdefe5cb8108d0a869b59a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Mon, 3 Sep 2012 18:57:13 +0200 Subject: Don't overload length as error code indicator in getTrusted* MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the return value to signal errors instead of overloading *lenProp == 0. Signed-off-by: Miloslav Trmač --- plugins/imuxsock/imuxsock.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/plugins/imuxsock/imuxsock.c b/plugins/imuxsock/imuxsock.c index 7eb3496f..9d273a3d 100644 --- a/plugins/imuxsock/imuxsock.c +++ b/plugins/imuxsock/imuxsock.c @@ -641,14 +641,12 @@ getTrustedProp(struct ucred *cred, char *propName, uchar *buf, size_t lenBuf, in if((fd = open(namebuf, O_RDONLY)) == -1) { DBGPRINTF("error reading '%s'\n", namebuf); - *lenProp = 0; - FINALIZE; + ABORT_FINALIZE(RS_RET_ERR); } if((lenRead = read(fd, buf, lenBuf - 1)) == -1) { DBGPRINTF("error reading file data for '%s'\n", namebuf); - *lenProp = 0; close(fd); - FINALIZE; + ABORT_FINALIZE(RS_RET_ERR); } /* we strip after the first \n */ @@ -684,8 +682,7 @@ getTrustedExe(struct ucred *cred, uchar *buf, size_t lenBuf, int* lenProp) if((lenRead = readlink(namebuf, (char*)buf, lenBuf - 1)) == -1) { DBGPRINTF("error reading link '%s'\n", namebuf); - *lenProp = 0; - FINALIZE; + ABORT_FINALIZE(RS_RET_ERR); } buf[lenRead] = '\0'; @@ -839,20 +836,17 @@ SubmitMsg(uchar *pRcv, int lenRcv, lstn_t *pLstn, struct ucred *cred, struct tim memcpy(pmsgbuf+toffs, propBuf, lenProp); toffs = toffs + lenProp; - getTrustedProp(cred, "comm", propBuf, sizeof(propBuf), &lenProp); - if(lenProp) { + if(getTrustedProp(cred, "comm", propBuf, sizeof(propBuf), &lenProp) == RS_RET_OK) { memcpy(pmsgbuf+toffs, " _COMM=", 7); memcpy(pmsgbuf+toffs+7, propBuf, lenProp); toffs = toffs + 7 + lenProp; } - getTrustedExe(cred, propBuf, sizeof(propBuf), &lenProp); - if(lenProp) { + if(getTrustedExe(cred, propBuf, sizeof(propBuf), &lenProp) == RS_RET_OK) { memcpy(pmsgbuf+toffs, " _EXE=", 6); memcpy(pmsgbuf+toffs+6, propBuf, lenProp); toffs = toffs + 6 + lenProp; } - getTrustedProp(cred, "cmdline", propBuf, sizeof(propBuf), &lenProp); - if(lenProp) { + if(getTrustedProp(cred, "cmdline", propBuf, sizeof(propBuf), &lenProp) == RS_RET_OK) { memcpy(pmsgbuf+toffs, " _CMDLINE=", 9); toffs = toffs + 9 + copyescaped(pmsgbuf+toffs+9, propBuf, lenProp); -- cgit v1.2.3 From 2e9d740103c93cbe0cd084d7f3c70ff02e53a637 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Mon, 3 Sep 2012 18:59:59 +0200 Subject: Don't store uninitialized data in trusted JSON properties MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add missing error handling. Signed-off-by: Miloslav Trmač --- plugins/imuxsock/imuxsock.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/plugins/imuxsock/imuxsock.c b/plugins/imuxsock/imuxsock.c index 9d273a3d..61ce857c 100644 --- a/plugins/imuxsock/imuxsock.c +++ b/plugins/imuxsock/imuxsock.c @@ -817,15 +817,18 @@ SubmitMsg(uchar *pRcv, int lenRcv, lstn_t *pLstn, struct ucred *cred, struct tim json_object_object_add(json, "uid", jval); jval = json_object_new_int(cred->gid); json_object_object_add(json, "gid", jval); - getTrustedProp(cred, "comm", propBuf, sizeof(propBuf), &lenProp); - jval = json_object_new_string((char*)propBuf); - json_object_object_add(json, "appname", jval); - getTrustedExe(cred, propBuf, sizeof(propBuf), &lenProp); - jval = json_object_new_string((char*)propBuf); - json_object_object_add(json, "exe", jval); - getTrustedProp(cred, "cmdline", propBuf, sizeof(propBuf), &lenProp); - jval = json_object_new_string((char*)propBuf); - json_object_object_add(json, "cmd", jval); + if(getTrustedProp(cred, "comm", propBuf, sizeof(propBuf), &lenProp) == RS_RET_OK) { + jval = json_object_new_string((char*)propBuf); + json_object_object_add(json, "appname", jval); + } + if(getTrustedExe(cred, propBuf, sizeof(propBuf), &lenProp) == RS_RET_OK) { + jval = json_object_new_string((char*)propBuf); + json_object_object_add(json, "exe", jval); + } + if(getTrustedProp(cred, "cmdline", propBuf, sizeof(propBuf), &lenProp) == RS_RET_OK) { + jval = json_object_new_string((char*)propBuf); + json_object_object_add(json, "cmd", jval); + } } else { memcpy(pmsgbuf, pRcv, lenRcv); memcpy(pmsgbuf+lenRcv, " @[", 3); -- cgit v1.2.3 From 396743e19aed8494c68c3a41ce140e884d4a71af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Mon, 3 Sep 2012 19:22:18 +0200 Subject: Free configuration objects after use MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miloslav Trmač --- grammar/rainerscript.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/grammar/rainerscript.c b/grammar/rainerscript.c index 33630a76..56a6376d 100644 --- a/grammar/rainerscript.c +++ b/grammar/rainerscript.c @@ -139,7 +139,7 @@ objlstDestruct(struct objlst *lst) while(lst != NULL) { toDel = lst; lst = lst->next; - // TODO: delete object + cnfobjDestruct(toDel->obj); free(toDel); } } @@ -648,6 +648,7 @@ cnfobjDestruct(struct cnfobj *o) { if(o != NULL) { nvlstDestruct(o->nvlst); + objlstDestruct(o->subobjs); free(o); } } -- cgit v1.2.3 From 64599e94126341f3d19703ea85f6403cbf80a5fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Mon, 3 Sep 2012 19:23:06 +0200 Subject: Don't leak property name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miloslav Trmač --- template.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/template.c b/template.c index 074e12b5..f5260191 100644 --- a/template.c +++ b/template.c @@ -1297,7 +1297,7 @@ static rsRetVal createPropertyTpe(struct template *pTpl, struct cnfobj *o) { struct templateEntry *pTpe; - cstr_t *name; + cstr_t *name = NULL; uchar *outname = NULL; int i; int droplastlf = 0; @@ -1582,6 +1582,8 @@ createPropertyTpe(struct template *pTpl, struct cnfobj *o) } finalize_it: + if(name != NULL) + rsCStrDestruct(&name); RETiRet; } -- cgit v1.2.3 From 18ff8d54dde1d0c8603d5f06800ce3d55eb86fb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Mon, 3 Sep 2012 19:23:18 +0200 Subject: Don't leak configuration value arrays MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miloslav Trmač --- template.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/template.c b/template.c index f5260191..7940ab24 100644 --- a/template.c +++ b/template.c @@ -1582,6 +1582,8 @@ createPropertyTpe(struct template *pTpl, struct cnfobj *o) } finalize_it: + if(pvals != NULL) + cnfparamvalsDestruct(pvals, &pblkProperty); if(name != NULL) rsCStrDestruct(&name); RETiRet; @@ -1767,6 +1769,8 @@ tplProcessCnf(struct cnfobj *o) pTpl->optFormatEscape = JSON_ESCAPE; finalize_it: + if(pvals != NULL) + cnfparamvalsDestruct(pvals, &pblk); if(iRet != RS_RET_OK) { if(pTpl != NULL) { /* we simply make the template defunct in this case by setting -- cgit v1.2.3 From ddea340e665a657bc6be88176130d1221c0dec3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Mon, 3 Sep 2012 19:39:43 +0200 Subject: Fix reference counting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When adding a JSON object to the template output, make sure to increment its reference count. Signed-off-by: Miloslav Trmač --- template.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template.c b/template.c index 7940ab24..3cef586c 100644 --- a/template.c +++ b/template.c @@ -303,7 +303,7 @@ rsRetVal tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjso if(pTpe->data.field.propid == PROP_CEE) { localRet = msgGetCEEPropJSON(pMsg, pTpe->data.field.propName, &jsonf); if(localRet == RS_RET_OK) { - json_object_object_add(json, (char*)pTpe->fieldName, jsonf); + json_object_object_add(json, (char*)pTpe->fieldName, json_object_get(jsonf)); } else { DBGPRINTF("tplToJSON: error %d looking up property\n", localRet); -- cgit v1.2.3