...
Non-Compliant Code Example
Command line arguments are read from the command line and stored in process memory. The function Wiki Markup main()
, called at program startup, is typically declared as follows when the program accepts command line arguments:
Code Block |
---|
int main(int argc, char *argv[]) { /* ... */ }
|
Wiki Markup |
---|
from the command line and stored in process memory. Command line arguments are passed to {{main()}} as pointers to null-terminated byte strings in the array members {{argv\[0\]}} through {{argv\[argc-1\]}}. |
...
. |
...
|
...
|
...
Wiki Markup |
---|
If the value of {{argc}} is greater than zero, the string pointed to by {{argv\[0\]}} represents the program name. If the value of {{argc}} is greater than one, the strings pointed to by {{argv\[1\]}} through {{argv\[argc-1\]}} represent the program parameters. In the following definition for {{main()}} the array members {{argv\[0\]}} through {{argv\[argc-1\]}} inclusive contain pointers to null-terminated byte strings. |
...
...
int main(int argc, char *argv[]) { /* ... */ }
Wiki Markup |
---|
IfThe theparameters value of {{argc}} is greater than zero,and {{argv}} and the stringstrings pointed to by the {{argv\[0\]}} representsarray are not modifiable by the program name. If the value of argc is greater than one, the strings pointed to by {{argv\[1\]}} through {{argv\[argc-1\]}} represent the program parameters. |
Wiki Markup |
The, and retain their last-stored values between program startup and program termination. This requires that a copy of these parameters be made before the strings can be modified. Vulnerabilities can occur when inadequate space is allocated to copy a command line argument. In this example, the contents of {{argv\[0\]}} can be manipulated by an attacker to cause a buffer overflow in the following program: |
Code Block |
---|
int main(int argc, char *argv[]) { ... char prog_name[128]; strcpy(prog_name, argv[0]); ... } |
...