Skip to content

Commit bc3baa9

Browse files
authored
[clang][analyzer] Move PutenvStackArrayChecker out of alpha package (#93980)
Checker alpha.security.PutenvStackArray is moved to security.PutenvStackArray.
1 parent fe56f19 commit bc3baa9

File tree

3 files changed

+41
-41
lines changed

3 files changed

+41
-41
lines changed

clang/docs/analyzer/checkers.rst

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,41 @@ security.insecureAPI.DeprecatedOrUnsafeBufferHandling (C)
11791179
strncpy(buf, "a", 1); // warn
11801180
}
11811181
1182+
.. _security-putenv-stack-array:
1183+
1184+
security.PutenvStackArray (C)
1185+
"""""""""""""""""""""""""""""
1186+
Finds calls to the ``putenv`` function which pass a pointer to a stack-allocated
1187+
(automatic) array as the argument. Function ``putenv`` does not copy the passed
1188+
string, only a pointer to the data is stored and this data can be read even by
1189+
other threads. Content of a stack-allocated array is likely to be overwritten
1190+
after exiting from the function.
1191+
1192+
The problem can be solved by using a static array variable or dynamically
1193+
allocated memory. Even better is to avoid using ``putenv`` (it has other
1194+
problems related to memory leaks) and use ``setenv`` instead.
1195+
1196+
The check corresponds to CERT rule
1197+
`POS34-C. Do not call putenv() with a pointer to an automatic variable as the argument
1198+
<https://wiki.sei.cmu.edu/confluence/display/c/POS34-C.+Do+not+call+putenv%28%29+with+a+pointer+to+an+automatic+variable+as+the+argument>`_.
1199+
1200+
.. code-block:: c
1201+
1202+
int f() {
1203+
char env[] = "NAME=value";
1204+
return putenv(env); // putenv function should not be called with stack-allocated string
1205+
}
1206+
1207+
There is one case where the checker can report a false positive. This is when
1208+
the stack-allocated array is used at `putenv` in a function or code branch that
1209+
does not return (process is terminated on all execution paths).
1210+
1211+
Another special case is if the `putenv` is called from function `main`. Here
1212+
the stack is deallocated at the end of the program and it should be no problem
1213+
to use the stack-allocated string (a multi-threaded program may require more
1214+
attention). The checker does not warn for cases when stack space of `main` is
1215+
used at the `putenv` call.
1216+
11821217
security.SetgidSetuidOrder (C)
11831218
""""""""""""""""""""""""""""""
11841219
When dropping user-level and group-level privileges in a program by using
@@ -2877,41 +2912,6 @@ Warn on mmap() calls that are both writable and executable.
28772912
// code
28782913
}
28792914
2880-
.. _alpha-security-putenv-stack-array:
2881-
2882-
alpha.security.PutenvStackArray (C)
2883-
"""""""""""""""""""""""""""""""""""
2884-
Finds calls to the ``putenv`` function which pass a pointer to a stack-allocated
2885-
(automatic) array as the argument. Function ``putenv`` does not copy the passed
2886-
string, only a pointer to the data is stored and this data can be read even by
2887-
other threads. Content of a stack-allocated array is likely to be overwritten
2888-
after returning from the parent function.
2889-
2890-
The problem can be solved by using a static array variable or dynamically
2891-
allocated memory. Even better is to avoid using ``putenv`` (it has other
2892-
problems related to memory leaks) and use ``setenv`` instead.
2893-
2894-
The check corresponds to CERT rule
2895-
`POS34-C. Do not call putenv() with a pointer to an automatic variable as the argument
2896-
<https://wiki.sei.cmu.edu/confluence/display/c/POS34-C.+Do+not+call+putenv%28%29+with+a+pointer+to+an+automatic+variable+as+the+argument>`_.
2897-
2898-
.. code-block:: c
2899-
2900-
int f() {
2901-
char env[] = "NAME=value";
2902-
return putenv(env); // putenv function should not be called with stack-allocated string
2903-
}
2904-
2905-
There is one case where the checker can report a false positive. This is when
2906-
the stack-allocated array is used at `putenv` in a function or code branch that
2907-
does not return (calls `fork` or `exec` like function).
2908-
2909-
Another special case is if the `putenv` is called from function `main`. Here
2910-
the stack is deallocated at the end of the program and it should be no problem
2911-
to use the stack-allocated string (a multi-threaded program may require more
2912-
attention). The checker does not warn for cases when stack space of `main` is
2913-
used at the `putenv` call.
2914-
29152915
.. _alpha-security-ReturnPtrRange:
29162916
29172917
alpha.security.ReturnPtrRange (C)

clang/include/clang/StaticAnalyzer/Checkers/Checkers.td

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,11 @@ def FloatLoopCounter : Checker<"FloatLoopCounter">,
10111011
Dependencies<[SecuritySyntaxChecker]>,
10121012
Documentation<HasDocumentation>;
10131013

1014+
def PutenvStackArray : Checker<"PutenvStackArray">,
1015+
HelpText<"Finds calls to the function 'putenv' which pass a pointer to "
1016+
"an automatic (stack-allocated) array as the argument.">,
1017+
Documentation<HasDocumentation>;
1018+
10141019
def SetgidSetuidOrderChecker : Checker<"SetgidSetuidOrder">,
10151020
HelpText<"Warn on possible reversed order of 'setgid(getgid()))' and "
10161021
"'setuid(getuid())' (CERT: POS36-C)">,
@@ -1065,11 +1070,6 @@ def MmapWriteExecChecker : Checker<"MmapWriteExec">,
10651070
]>,
10661071
Documentation<HasDocumentation>;
10671072

1068-
def PutenvStackArray : Checker<"PutenvStackArray">,
1069-
HelpText<"Finds calls to the function 'putenv' which pass a pointer to "
1070-
"an automatic (stack-allocated) array as the argument.">,
1071-
Documentation<HasDocumentation>;
1072-
10731073
def ReturnPointerRangeChecker : Checker<"ReturnPtrRange">,
10741074
HelpText<"Check for an out-of-bound pointer being returned to callers">,
10751075
Documentation<HasDocumentation>;

clang/test/Analysis/putenv-stack-array.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %clang_analyze_cc1 \
2-
// RUN: -analyzer-checker=alpha.security.PutenvStackArray \
2+
// RUN: -analyzer-checker=security.PutenvStackArray \
33
// RUN: -verify %s
44

55
#include "Inputs/system-header-simulator.h"

0 commit comments

Comments
 (0)