You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[clang][analyzer] Improved documentation for TaintPropagation Checker
The usage of the taint analysis is described through a command injection attack example.
It is explained how to make a variable sanitized through configuration.
Differential Revision: https://reviews.llvm.org/D145229
Taint analysis identifies untrusted sources of information (taint sources), rules as to how the untrusted data flows along the execution path (propagation rules), and points of execution where the use of tainted data is risky (taints sinks).
2370
+
Taint analysis identifies potential security vulnerabilities where the
2371
+
attacker can inject malicious data to the program to execute an attack
If you are validating your inputs instead of sanitizing them, or don't want to
2457
+
mention each sanitizing function in our configuration,
2458
+
you can use a more generic approach.
2459
+
2460
+
Introduce a generic no-op `csa_mark_sanitized(..)` function to
2461
+
tell the Clang Static Analyzer
2462
+
that the variable is safe to be used on that analysis path.
2463
+
2464
+
.. code-block:: c
2413
2465
2414
-
The user can configure taint sources, sinks, and propagation rules by providing a configuration file via checker option ``alpha.security.taint.TaintPropagation:Config``.
2466
+
// Marking sanitized variables safe.
2467
+
// No vulnerability anymore, no warning.
2415
2468
2416
-
External taint configuration is in `YAML <http://llvm.org/docs/YamlIO.html#introduction-to-yaml>`_ format. The taint-related options defined in the config file extend but do not override the built-in sources, rules, sinks.
2417
-
The format of the external taint configuration file is not stable, and could change without any notice even in a non-backward compatible way.
2469
+
// User csa_mark_sanitize function is for the analyzer only
2470
+
#ifdef __clang_analyzer__
2471
+
void csa_mark_sanitized(const void *);
2472
+
#endif
2473
+
2474
+
int main(int argc, char** argv) {
2475
+
char cmd[2048] = "/bin/cat ";
2476
+
char filename[1024];
2477
+
printf("Filename:");
2478
+
scanf (" %1023[^\n]", filename);
2479
+
if (access(filename,F_OK)){// Verifying user input
2480
+
printf("File does not exist\n");
2481
+
return -1;
2482
+
}
2483
+
#ifdef __clang_analyzer__
2484
+
csa_mark_sanitized(filename); // Indicating to CSA that filename variable is safe to be used after this point
2485
+
#endif
2486
+
strcat(cmd, filename);
2487
+
system(cmd); // No warning
2488
+
}
2489
+
2490
+
Similarly to the previous example, you need to
2491
+
define a `Filter` function in a `YAML` configuration file
2492
+
and add the `csa_mark_sanitized` function.
2493
+
2494
+
.. code-block:: YAML
2495
+
2496
+
Filters:
2497
+
- Name: csa_mark_sanitized
2498
+
Args: [0]
2499
+
2500
+
Then calling `csa_mark_sanitized(X)` will tell the analyzer that `X` is safe to
2501
+
be used after this point, because its contents are verified. It is the
2502
+
responisibility of the programmer to ensure that this verification was indeed
2503
+
correct. Please note that `csa_mark_sanitized` function is only declared and
2504
+
used during Clang Static Analysis and skipped in (production) builds.
2505
+
2506
+
Further examples of injection vulnerabilities this checker can find.
2507
+
2508
+
.. code-block:: c
2509
+
2510
+
void test() {
2511
+
char x = getchar(); // 'x' marked as tainted
2512
+
system(&x); // warn: untrusted data is passed to a system call
2513
+
}
2514
+
2515
+
// note: compiler internally checks if the second param to
2516
+
// sprintf is a string literal or not.
2517
+
// Use -Wno-format-security to suppress compiler warning.
2518
+
void test() {
2519
+
char s[10], buf[10];
2520
+
fscanf(stdin, "%s", s); // 's' marked as tainted
2521
+
2522
+
sprintf(buf, s); // warn: untrusted data used as a format string
2523
+
}
2524
+
2525
+
void test() {
2526
+
size_t ts;
2527
+
scanf("%zd", &ts); // 'ts' marked as tainted
2528
+
int *p = (int *)malloc(ts * sizeof(int));
2529
+
// warn: untrusted data used as buffer size
2530
+
}
2531
+
2532
+
There are built-in sources, propagations and sinks even if no external taint
For a more detailed description of configuration options, please see the :doc:`user-docs/TaintAnalysisConfiguration`. For an example see :ref:`clangsa-taint-configuration-example`.
2596
+
* The taintedness property is not propagated through function calls which are
2597
+
unknown (or too complex) to the analyzer, unless there is a specific
2598
+
propagation rule built-in to the checker or given in the YAML configuration
2599
+
file. This causes potential true positive findings to be lost.
0 commit comments