|
| 1 | +/** |
| 2 | + * @id c/cert/detect-and-handle-standard-library-errors |
| 3 | + * @name ERR33-C: Detect and handle standard library errors |
| 4 | + * @description Detect and handle standard library errors. |
| 5 | + * @kind problem |
| 6 | + * @precision high |
| 7 | + * @problem.severity error |
| 8 | + * @tags external/cert/id/err33-c |
| 9 | + * correctness |
| 10 | + * external/cert/obligation/rule |
| 11 | + */ |
| 12 | + |
| 13 | +import cpp |
| 14 | +import codingstandards.c.cert |
| 15 | +import semmle.code.cpp.commons.NULL |
| 16 | +import codingstandards.cpp.ReadErrorsAndEOF |
| 17 | +import semmle.code.cpp.dataflow.DataFlow |
| 18 | + |
| 19 | +/** |
| 20 | + * Classifies error returning function calls based on the |
| 21 | + * type and value of the required checked |
| 22 | + */ |
| 23 | +class ExpectedErrReturn extends FunctionCall { |
| 24 | + Expr errValue; |
| 25 | + string errOperator; |
| 26 | + |
| 27 | + ExpectedErrReturn() { |
| 28 | + errOperator = ["==", "!="] and |
| 29 | + ( |
| 30 | + errValue.(Literal).getValue() = "0" and |
| 31 | + this.getTarget() |
| 32 | + .hasName([ |
| 33 | + "asctime_s", "at_quick_exit", "atexit", "ctime_s", "fgetpos", "fopen_s", "freopen_s", |
| 34 | + "fseek", "fsetpos", "mbsrtowcs_s", "mbstowcs_s", "raise", "remove", "rename", |
| 35 | + "setvbuf", "strerror_s", "strftime", "strtod", "strtof", "strtold", "timespec_get", |
| 36 | + "tmpfile_s", "tmpnam_s", "tss_get", "wcsftime", "wcsrtombs_s", "wcstod", "wcstof", |
| 37 | + "wcstold", "wcstombs_s", "wctrans", "wctype" |
| 38 | + ]) |
| 39 | + or |
| 40 | + errValue instanceof NULL and |
| 41 | + this.getTarget() |
| 42 | + .hasName([ |
| 43 | + "aligned_alloc", "bsearch_s", "bsearch", "calloc", "fgets", "fopen", "freopen", |
| 44 | + "getenv_s", "getenv", "gets_s", "gmtime_s", "gmtime", "localtime_s", "localtime", |
| 45 | + "malloc", "memchr", "realloc", "setlocale", "strchr", "strpbrk", "strrchr", "strstr", |
| 46 | + "strtok_s", "strtok", "tmpfile", "tmpnam", "wcschr", "wcspbrk", "wcsrchr", "wcsstr", |
| 47 | + "wcstok_s", "wcstok", "wmemchr" |
| 48 | + ]) |
| 49 | + or |
| 50 | + errValue = any(EOFInvocation i).getExpr() and |
| 51 | + this.getTarget() |
| 52 | + .hasName([ |
| 53 | + "fclose", "fflush", "fputs", "fputws", "fscanf_s", "fscanf", "fwscanf_s", "fwscanf", |
| 54 | + "scanf_s", "scanf", "sscanf_s", "sscanf", "swscanf_s", "swscanf", "ungetc", |
| 55 | + "vfscanf_s", "vfscanf", "vfwscanf_s", "vfwscanf", "vscanf_s", "vscanf", "vsscanf_s", |
| 56 | + "vsscanf", "vswscanf_s", "vswscanf", "vwscanf_s", "vwscanf", "wctob", "wscanf_s", |
| 57 | + "wscanf", "fgetc", "fputc", "getc", "getchar", "putc", "putchar", "puts" |
| 58 | + ]) |
| 59 | + or |
| 60 | + errValue = any(WEOFInvocation i).getExpr() and |
| 61 | + this.getTarget() |
| 62 | + .hasName([ |
| 63 | + "btowc", "fgetwc", "fputwc", "getwc", "getwchar", "putwc", "ungetwc", "putwchar" |
| 64 | + ]) |
| 65 | + or |
| 66 | + errValue = any(EnumConstantAccess i | i.toString() = "thrd_error") and |
| 67 | + this.getTarget() |
| 68 | + .hasName([ |
| 69 | + "cnd_broadcast", "cnd_init", "cnd_signal", "cnd_timedwait", "cnd_wait", "mtx_init", |
| 70 | + "mtx_lock", "mtx_timedlock", "mtx_trylock", "mtx_unlock", "thrd_create", |
| 71 | + "thrd_detach", "thrd_join", "tss_create", "tss_set" |
| 72 | + ]) |
| 73 | + or |
| 74 | + errValue = any(EnumConstantAccess i | i.toString() = "thrd_nomem") and |
| 75 | + this.getTarget().hasName(["cnd_init", "thrd_create"]) |
| 76 | + or |
| 77 | + errValue = any(EnumConstantAccess i | i.toString() = "thrd_timedout") and |
| 78 | + this.getTarget().hasName(["cnd_timedwait", "mtx_timedlock"]) |
| 79 | + or |
| 80 | + errValue = any(EnumConstantAccess i | i.toString() = "thrd_busy") and |
| 81 | + this.getTarget().hasName(["mtx_trylock"]) |
| 82 | + or |
| 83 | + errValue = any(MacroInvocation i | i.getMacroName() = "UINTMAX_MAX").getExpr() and |
| 84 | + this.getTarget().hasName(["strtoumax", "wcstoumax"]) |
| 85 | + or |
| 86 | + errValue = any(MacroInvocation i | i.getMacroName() = "ULONG_MAX").getExpr() and |
| 87 | + this.getTarget().hasName(["strtoul", "wcstoul"]) |
| 88 | + or |
| 89 | + errValue = any(MacroInvocation i | i.getMacroName() = "ULLONG_MAX").getExpr() and |
| 90 | + this.getTarget().hasName(["strtoull", "wcstoull"]) |
| 91 | + or |
| 92 | + errValue = any(MacroInvocation i | i.getMacroName() = "SIG_ERR").getExpr() and |
| 93 | + this.getTarget().hasName(["signal"]) |
| 94 | + or |
| 95 | + errValue = any(MacroInvocation i | i.getMacroName() = ["INTMAX_MAX", "INTMAX_MIN"]).getExpr() and |
| 96 | + this.getTarget().hasName(["strtoimax", "wcstoimax"]) |
| 97 | + or |
| 98 | + errValue = any(MacroInvocation i | i.getMacroName() = ["LONG_MAX", "LONG_MIN"]).getExpr() and |
| 99 | + this.getTarget().hasName(["strtol", "wcstol"]) |
| 100 | + or |
| 101 | + errValue = any(MacroInvocation i | i.getMacroName() = ["LLONG_MAX", "LLONG_MIN"]).getExpr() and |
| 102 | + this.getTarget().hasName(["strtoll", "wcstoll"]) |
| 103 | + or |
| 104 | + errValue.(UnaryMinusExpr).getOperand().(Literal).getValue() = "1" and |
| 105 | + this.getTarget() |
| 106 | + .hasName([ |
| 107 | + "c16rtomb", "c32rtomb", "clock", "ftell", "mbrtoc16", "mbrtoc32", "mbsrtowcs", |
| 108 | + "mbstowcs", "mktime", "time", "wcrtomb", "wcsrtombs", "wcstombs" |
| 109 | + ]) |
| 110 | + or |
| 111 | + errValue.(UnaryMinusExpr).getOperand().(Literal).getValue() = "1" and |
| 112 | + not this.getArgument(0) instanceof NULL and |
| 113 | + this.getTarget().hasName(["mblen", "mbrlen", "mbrtowc", "mbtowc", "wctomb_s", "wctomb"]) |
| 114 | + or |
| 115 | + errValue.getType() instanceof IntType and |
| 116 | + this.getTarget().hasName(["fread", "fwrite"]) |
| 117 | + ) |
| 118 | + or |
| 119 | + errOperator = ["<", ">="] and |
| 120 | + ( |
| 121 | + errValue.(Literal).getValue() = "0" and |
| 122 | + this.getTarget() |
| 123 | + .hasName([ |
| 124 | + "fprintf_s", "fprintf", "fwprintf_s", "fwprintf", "printf_s", "snprintf_s", |
| 125 | + "snprintf", "sprintf_s", "sprintf", "swprintf_s", "swprintf", "thrd_sleep", |
| 126 | + "vfprintf_s", "vfprintf", "vfwprintf_s", "vfwprintf", "vprintf_s", "vsnprintf_s", |
| 127 | + "vsnprintf", "vsprintf_s", "vsprintf", "vswprintf_s", "vswprintf", "vwprintf_s", |
| 128 | + "wprintf_s", "printf", "vprintf", "wprintf", "vwprintf" |
| 129 | + ]) |
| 130 | + or |
| 131 | + errValue.getType() instanceof IntType and |
| 132 | + this.getTarget().hasName(["strxfrm", "wcsxfrm"]) |
| 133 | + ) |
| 134 | + or |
| 135 | + errOperator = "NA" and |
| 136 | + ( |
| 137 | + errValue = any(Expr e) and |
| 138 | + this.getTarget() |
| 139 | + .hasName([ |
| 140 | + "kill_dependency", "memcpy", "wmemcpy", "memmove", "wmemmove", "strcpy", "wcscpy", |
| 141 | + "strncpy", "wcsncpy", "strcat", "wcscat", "strncat", "wcsncat", "memset", "wmemset" |
| 142 | + ]) |
| 143 | + ) |
| 144 | + } |
| 145 | + |
| 146 | + Expr getErrValue() { result = errValue } |
| 147 | + |
| 148 | + string getErrOperator() { result = errOperator } |
| 149 | +} |
| 150 | + |
| 151 | +// Nodes following a file write before a call to `ferror` is performed |
| 152 | +ControlFlowNode ferrorNotchecked(FileWriteFunctionCall write) { |
| 153 | + result = write |
| 154 | + or |
| 155 | + exists(ControlFlowNode mid | |
| 156 | + mid = ferrorNotchecked(write) and |
| 157 | + //do not traverse the short-circuited CFG edge |
| 158 | + not isShortCircuitedEdge(mid, result) and |
| 159 | + result = mid.getASuccessor() and |
| 160 | + //Stop recursion on call to ferror on the correct file |
| 161 | + not accessSameTarget(result.(FerrorCall).getArgument(0), write.getFileExpr()) |
| 162 | + ) |
| 163 | +} |
| 164 | + |
| 165 | +from ExpectedErrReturn err |
| 166 | +where |
| 167 | + not isExcluded(err, Contracts5Package::detectAndHandleStandardLibraryErrorsQuery()) and |
| 168 | + // calls that must be verified using the return value |
| 169 | + not exists(ComparisonOperation op | |
| 170 | + DataFlow::localExprFlow(err, op.getAnOperand()) and |
| 171 | + (err.getErrOperator() != "NA" implies op.getOperator() = err.getErrOperator()) and |
| 172 | + op.getAnOperand() = err.getErrValue() and |
| 173 | + // special case for function `realloc` where the returned pointer |
| 174 | + // should not be invalidated |
| 175 | + not ( |
| 176 | + err.getTarget().hasName("realloc") and |
| 177 | + op.getAnOperand().(VariableAccess).getTarget() = |
| 178 | + err.getArgument(0).(VariableAccess).getTarget() |
| 179 | + ) |
| 180 | + ) and |
| 181 | + // EXCEPTIONS |
| 182 | + ( |
| 183 | + // calls that can be verified using ferror() && feof() |
| 184 | + err.getTarget().hasName(["fgetc", "fgetwc", "getc", "getchar"]) |
| 185 | + implies |
| 186 | + missingFeofFerrorChecks(err) |
| 187 | + ) and |
| 188 | + ( |
| 189 | + // calls that can be verified using ferror() |
| 190 | + err.getTarget().hasName(["fputc", "putc"]) |
| 191 | + implies |
| 192 | + err.getEnclosingFunction() = ferrorNotchecked(err) |
| 193 | + ) and |
| 194 | + ( |
| 195 | + // ERR33-C-EX1: calls that can be ignored when cast to `void` |
| 196 | + err.getTarget() |
| 197 | + .hasName([ |
| 198 | + "putchar", "putwchar", "puts", "printf", "vprintf", "wprintf", "vwprintf", |
| 199 | + "kill_dependency", "memcpy", "wmemcpy", "memmove", "wmemmove", "strcpy", "wcscpy", |
| 200 | + "strncpy", "wcsncpy", "strcat", "wcscat", "strncat", "wcsncat", "memset", "wmemset" |
| 201 | + ]) |
| 202 | + implies |
| 203 | + not err.getExplicitlyConverted() instanceof VoidConversion |
| 204 | + ) |
| 205 | +select err, |
| 206 | + "Missing error detection for the call to function `" + err.getTarget() + |
| 207 | + "`. Undetected failures can lead to unexpected or undefined behavior." |
0 commit comments