Reading an environment variable using getenv() will not cause a buffer overflow.
On Linux, inherited environment variables and their values ββare stored in the process address space by the kernel during exec() . The getenv() function simply returns a pointer to this existing data. Since it does not copy any data, the buffer does not exist, and there can be no buffer overflow.
If you try to pass too many environment variables into a new process, exec() will signal an E2BIG error.
Security concerns
Actually, there are no problems with buffer overflows with environment variables.
A security issue is that you do not have to trust the contents of the environment. If your program is running setuid (or setgid, etc.), then the environment is an attack vector. The user can set PATH or LD_PRELOAD or other variables in malicious ways.
However, you rarely have to write setuid programs. This is good because there are so many reasons why it is difficult to make setuid programs safe.
source share