security: check size boundaries for getenv() returns
getenv() return values cannot be trusted, because an attacker might set them. For instance, we shouldn't expect, that getenv() returns a value of some sane size. Another problem is that getenv() returns a pointer to one of `char **environ` members, which might change upon next setenv(). Introduce a wrapper, getenv_safe(), which returns the value only when it fits in a buffer of a specified size, and copies the value onto the buffer. Use this wrapper everywhere in our code. Below's a slightly decorated output of `grep -rwn getenv ./src --include *.c --include *.h --include *.cc --include *.cpp --include *.hpp --exclude *.lua.c` as of 2022-10-14. `-` marks invalid occurences (comments, for example), `*` marks the places that are already guarded before this patch, `X` mars the places guarded in this patch, and `^` marks places fixed in the next commit: NO_WRAP ``` * ./src/lib/core/coio_file.c:509: const char *tmpdir = getenv("TMPDIR"); X ./src/lib/core/errinj.c:75: const char *env_value = getenv(inj->name); - ./src/proc_title.c:202: * that might try to hang onto a getenv() result.) - ./src/proc_title.c:241: * is mandatory to flush internal libc caches on getenv/setenv X ./src/systemd.c:54: sd_unix_path = getenv("NOTIFY_SOCKET"); * ./src/box/module_cache.c:300: const char *tmpdir = getenv("TMPDIR"); X ./src/box/sql/os_unix.c:1441: azDirs[0] = getenv("SQL_TMPDIR"); X ./src/box/sql/os_unix.c:1446: azDirs[1] = getenv("TMPDIR"); * ./src/box/lua/console.c:394: const char *envvar = getenv("TT_CONSOLE_HIDE_SHOW_PROMPT"); ^ ./src/box/lua/console.lua:771: local home_dir = os.getenv('HOME') ^ ./src/box/lua/load_cfg.lua:1007: local raw_value = os.getenv(env_var_name) X ./src/lua/init.c:575: const char *path = getenv(envname); X ./src/lua/init.c:592: const char *home = getenv("HOME"); * ./src/find_path.c:77: snprintf(buf, sizeof(buf) - 1, "%s", getenv("_")); ``` NO_WRAP Part-of #7797 NO_DOC=security
Showing
- changelogs/unreleased/gh-7797-untrusted-input-validation.md 4 additions, 0 deletionschangelogs/unreleased/gh-7797-untrusted-input-validation.md
- src/box/lua/console.c 3 additions, 1 deletionsrc/box/lua/console.c
- src/box/module_cache.c 6 additions, 5 deletionssrc/box/module_cache.c
- src/box/sql/os_unix.c 4 additions, 2 deletionssrc/box/sql/os_unix.c
- src/find_path.c 2 additions, 1 deletionsrc/find_path.c
- src/lib/core/coio_file.c 8 additions, 6 deletionssrc/lib/core/coio_file.c
- src/lib/core/coio_file.h 3 additions, 1 deletionsrc/lib/core/coio_file.h
- src/lib/core/errinj.c 7 additions, 2 deletionssrc/lib/core/errinj.c
- src/lib/core/util.c 41 additions, 0 deletionssrc/lib/core/util.c
- src/lua/init.c 6 additions, 4 deletionssrc/lua/init.c
- src/main.cc 1 addition, 1 deletionsrc/main.cc
- src/systemd.c 3 additions, 1 deletionsrc/systemd.c
- src/trivia/util.h 27 additions, 1 deletionsrc/trivia/util.h
- test/unit/CMakeLists.txt 7 additions, 2 deletionstest/unit/CMakeLists.txt
- test/unit/getenv_safe.c 55 additions, 0 deletionstest/unit/getenv_safe.c
Loading