getenv(3)
๐ getenv(3) โ Library Functions Manual ๐
okay bestie, say hi to getenv(3) โ this is the libc call that goes digging through your process's environment list to pull out an environment variable for you. And it brought a security-conscious bestie along too: secure_getenv. Both of them live in the Standard C library (libc, -lc). no cap ๐
โจ NAME โจ
getenv, secure_getenv โ get an environment variable ๐ธ
๐ LIBRARY ๐
Standard C library (libc, -lc)
๐ฆ SYNOPSIS ๐ฆ
#include <stdlib.h>
char *getenv(const char *name);
char *secure_getenv(const char *name);
๐ Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
secure_getenv():_GNU_SOURCE
๐ DESCRIPTION ๐
so getenv() literally scrolls the environment list looking for the variable called name, and hands you back a pointer to the matching value string. it's giving personal assistant energy ๐ซถ
secure_getenv() is exactly the same as getenv() except for one icon behaviour: it returns NULL in any case where "secure execution" is required. dramatic? yes. necessary? also yes ๐ฅ
Secure execution is required if any one of these was true when the program run by the calling process was loaded:
- the process's effective user ID did not match its real user ID, or the process's effective group ID did not match its real group ID (typically this is the result of executing a set-user-ID or set-group-ID program);
- the effective capability bit was set on the executable file; or
- the process has a nonempty permitted capability set.
Secure execution may also be required if it got triggered by some Linux security modules. ๐
The whole point of secure_getenv() is to live in general-purpose libraries so you avoid the vulnerabilities that happen when set-user-ID or set-group-ID programs accidentally trust the environment. like, imagine trusting the environment. couldn't be her ๐
๐ RETURN VALUE ๐
getenv() returns a pointer to the value in the environment โ or NULL if there's no match. that's it, that's the whole return policy bestie ๐
๐ง ATTRIBUTES ๐ง
for an explanation of the terms used in this section, see attributes(7). (no tables here, we're keeping it cute and listy โญ)
- Interface:
getenv(),secure_getenv() - Attribute: Thread safety
- Value:
MT-Safe env
โญ STANDARDS โญ
getenv()โ C11, POSIX.1-2008.secure_getenv()โ POSIX.1-2024.
๐ HISTORY ๐
getenv()โ POSIX.1-2001, C89, C99, SVr4, 4.3BSD.secure_getenv()โ POSIX.1-2024, glibc 2.17.
look at her, she's been around. veteran queen energy ๐
๐ NOTES ๐
The strings in the environment list are shaped like name=value. that's the whole format, no frills ๐
As typically implemented, getenv() returns a pointer to a string inside the environment list. so bestieโฆ do not modify that string. like, at all. modifying it changes the environment of the process. it's not a little oopsie, it's a whole situation ๐ญ
The implementation of getenv() is not required to be reentrant. the string your return value points to may be statically allocated, and it can get modified by a subsequent call to getenv(), putenv(3), setenv(3), or unsetenv(3). so if you're holding onto that pointer, protect your peace and copy it ๐ซถ
โ ๏ธ warning bestie: that pointer is not yours forever. it's borrowed, and someone else's call can rewrite it out from under you.
The "secure execution" mode of secure_getenv() is controlled by the AT_SECURE flag contained in the auxiliary vector passed from the kernel to user space. iykyk ๐ง
๐ธ SEE ALSO ๐ธ
clearenv(3), getauxval(3), putenv(3), setenv(3), unsetenv(3), capabilities(7), environ(7)
that's the whole page, and it's iconic. this one's from Linux man-pages 6.20, dated 2026-02-08 โ go check your env, stay safe, and never trust the environment in a setuid program. periodt ๐โจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- Copyright 2007-2012, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms