@@ -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+
11821217security.SetgidSetuidOrder (C)
11831218""""""""""""""""""""""""""""""
11841219When 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
29172917alpha.security .ReturnPtrRange (C)
0 commit comments