44ccfacbe3a3d4b300f80d48651c77837a4b571e galt Tue Apr 26 11:12:02 2022 -0700 SQL INJECTION Prevention Version 2 - this improves our methods by making subclauses of SQL that get passed around be both easy and correct to use. The way that was achieved was by getting rid of the obscure and not well used functions sqlSafefFrag and sqlDyStringPrintfFrag and replacing them with the plain versions of those functions, since these are not needed anymore. The new version checks for NOSQLINJ in unquoted %-s which is used to include SQL clauses, and will give an error the NOSQLINJ clause is not present, and this will automatically require the correct behavior by developers. sqlDyStringPrint is a very useful function, however because it was not enforced, users could use various other dyString functions and they operated without any awareness or checking for SQL correct use. Now those dyString functions are prohibited and it will produce an error if you try to use a dyString function on a SQL string, which is simply detected by the presence of the NOSQLINJ prefix. diff --git src/parasol/para/para.c src/parasol/para/para.c index 8639a4b..325c2eb 100644 --- src/parasol/para/para.c +++ src/parasol/para/para.c @@ -759,31 +759,31 @@ line = trimSpaces(line); if (line == NULL || line[0] == '#' || line[0] == 0) continue; ++db->jobCount; job = jobFromLine(lf, line); slAddHead(&db->jobList, job); } lineFileClose(&lf); slReverse(&db->jobList); return db; } void sendSetPriorityMessage(int priority) /* Tell hub to change priority on batch */ { -struct dyString *dy = newDyString(1024); +struct dyString *dy = dyStringNew(1024); char *result; if ((priority < 1) || (priority > MAX_PRIORITY)) errAbort("Priority %d out of range, should be 1 to %d",priority,MAX_PRIORITY); dyStringPrintf(dy, "setPriority %s %s %d", getUser(), resultsName, priority); result = hubSingleLineQuery(dy->string); dyStringFree(&dy); if (result == NULL || sameString(result, "0")) errAbort("Couldn't set priority for %s\n", batchDir); freez(&result); verbose(1, "Told hub to set priority %d\n",priority); } void paraPriority(char *val) /* Tell hub to change priority on batch */ @@ -800,31 +800,31 @@ } void checkPrioritySetting() /* see if we can and need to set the priority */ { if (optionVal("pri",NULL)!=NULL) paraPriority(optionVal("pri","medium")); if (optionVal("priority",NULL)!=NULL) paraPriority(optionVal("priority","medium")); } void sendSetMaxJobMessage(int maxJob) /* Tell hub to change maxJob on batch */ { -struct dyString *dy = newDyString(1024); +struct dyString *dy = dyStringNew(1024); char *result; if (maxJob <-1) errAbort("maxJob %d out of range, should be >=-1", maxJob); dyStringPrintf(dy, "setMaxJob %s %s %d", getUser(), resultsName, maxJob); result = hubSingleLineQuery(dy->string); dyStringFree(&dy); if (result == NULL || sameString(result, "-2")) errAbort("Couldn't set maxJob %d for %s", maxJob, batchDir); freez(&result); verbose(1, "Told hub to set maxJob %d\n",maxJob); } void paraMaxJob(char *val) /* Tell hub to change maxJob on batch */ { @@ -949,31 +949,31 @@ { struct hash *hash = newHash(max(12,digitsBaseTwo(db->jobCount)-3)); struct job *job; struct submission *sub; int queueSize = 0; long killSeconds = killTime*60; long warnSeconds = warnTime*60; long duration; time_t now = time(NULL); long queryTime = clock1000(); /* Get job list from paraHub. */ -struct dyString *dy = newDyString(1024); +struct dyString *dy = dyStringNew(1024); dyStringPrintf(dy, "pstat2 %s %s", getUser(), resultsName); struct slName *lineList = hubMultilineQuery(dy->string), *lineEl; dyStringFree(&dy); now = time(NULL); /* need to refresh this after we get the info */ verbose(2, "pstat2 time: %.2f seconds\n", (clock1000() - queryTime) / 1000.0); long hashTime = clock1000(); verbose(2, "submission hash size: %d\n", hash->size); /* Make hash of submissions based on id and clear flags. */ for (job = db->jobList; job != NULL; job = job->next) { for (sub = job->submissionList; sub != NULL; sub = sub->next) @@ -1119,31 +1119,31 @@ } double subRealTime(struct submission *sub) /* Get real time in seconds for job. */ { /* note sub->*Time are unsigned, so we need to convert to double * before subtracting or time moving backwards is not detected */ return ((double)sub->endTime) - ((double)sub->startTime); } void showSickNodes(boolean showSummary) /* Tell hub to show sick nodes on batch. */ { int count = 0; -struct dyString *dy = newDyString(1024); +struct dyString *dy = dyStringNew(1024); dyStringPrintf(dy, "showSickNodes %s %s", getUser(), resultsName); struct slName *lineList = hubMultilineQuery(dy->string), *lineEl; for (lineEl = lineList; lineEl != NULL; lineEl = lineEl->next) { ++count; char *line = lineEl->name; /* In show summary mode, only print the last line, * which contains the totals. Only print this one * if there's more than one line (the total is greater than zero). */ if (!showSummary || !(lineEl->next || count == 1)) printf("%s\n", line); } slFreeList(&lineList); dyStringFree(&dy); } @@ -1278,31 +1278,31 @@ verbose(2, "paraCycle time: %.2f seconds\n", (clock1000() - time) / 1000.0); return db; } void paraPush(char *batch) /* Push forward batch one time. */ { struct jobDb *db = paraCycle(batch); jobDbFree(&db); } void clearSickNodes() /* Tell hub to clear sick nodes on batch */ { -struct dyString *dy = newDyString(1024); +struct dyString *dy = dyStringNew(1024); char *result; dyStringPrintf(dy, "clearSickNodes %s %s", getUser(), resultsName); result = hubSingleLineQuery(dy->string); dyStringFree(&dy); if (!sameString(result, "0")) errAbort("Couldn't clear sick nodes for %s", batchDir); freez(&result); verbose(1, "Told hub to clear sick nodes\n"); } void paraShove(char *batch) /* Push batch of jobs and keep pushing until it's finished, polling * parasol every 5 minutes. */ { @@ -1659,83 +1659,83 @@ int duration = time(NULL) - startTime; if (duration > (minTime * 60)) { runningReport(job, sub); ++runCount; } } } printf("total %s running: %d\n", !minTimeS ? "jobs" : "hippos", runCount); } void sendChillMessage() /* Tell hub to chill out on job */ { -struct dyString *dy = newDyString(1024); +struct dyString *dy = dyStringNew(1024); char *result; dyStringPrintf(dy, "chill %s %s", getUser(), resultsName); result = hubSingleLineQuery(dy->string); dyStringFree(&dy); if (result == NULL || !sameString(result, "ok")) errAbort("Couldn't chill %s\n", batchDir); freez(&result); verbose(1, "Told hub to chill out\n"); } void paraResetCounts() /* Send msg to hub to reset done and crashed counts on batch */ { -struct dyString *dy = newDyString(1024); +struct dyString *dy = dyStringNew(1024); char *result; dyStringPrintf(dy, "resetCounts %s %s", getUser(), resultsName); result = hubSingleLineQuery(dy->string); dyStringFree(&dy); if (result == NULL || sameString(result, "-2")) errAbort("Couldn't reset done and crashed counts on batch %s\n", batchDir); freez(&result); verbose(1, "Told hub to reset done and crashed counts on batch %s\n", batchDir); } void freeBatch() /* Send msg to hub to reset done and crashed counts on batch */ { -struct dyString *dy = newDyString(1024); +struct dyString *dy = dyStringNew(1024); char *result; dyStringPrintf(dy, "freeBatch %s %s", getUser(), resultsName); result = hubSingleLineQuery(dy->string); dyStringFree(&dy); verbose(1, "Told hub to free all batch-related resources\n"); if (result == NULL) errAbort("result == NULL"); if (sameOk(result, "-3")) errAbort("User not found."); if (sameOk(result, "-2")) errAbort("Batch not found."); if (sameOk(result, "-1")) warn("Unable to free batch. Jobs are queued or running."); if (sameOk(result, "0")) verbose(1, "Batch freed.\n"); freez(&result); } void flushResults() /* Send msg to hub to flush results file */ { -struct dyString *dy = newDyString(1024); +struct dyString *dy = dyStringNew(1024); char *result; dyStringPrintf(dy, "flushResults %s %s", getUser(), resultsName); result = hubSingleLineQuery(dy->string); dyStringFree(&dy); verbose(1, "Told hub to flush the results file\n"); if (result == NULL) errAbort("result == NULL"); if (sameOk(result, "-3")) errAbort("User not found."); if (sameOk(result, "-2")) errAbort("Batch not found."); if (sameOk(result, "-1")) warn("Flushed results. Some jobs are still queued or running."); if (sameOk(result, "0")) verbose(1, "Flushed results.\n");