libc: Use secure_getenv(3) where appropriate

No functional change intended.

Reviewed by:	mjg, imp, kib
Differential Revision:	https://reviews.freebsd.org/D39278
This commit is contained in:
Mark Johnston 2023-03-27 08:55:01 -04:00
parent ad2f2ee015
commit 68ca8363c7
16 changed files with 25 additions and 43 deletions

View File

@ -391,11 +391,10 @@ tmp(void)
{ {
sigset_t set, oset; sigset_t set, oset;
int fd, len; int fd, len;
char *envtmp = NULL; char *envtmp;
char path[MAXPATHLEN]; char path[MAXPATHLEN];
if (issetugid() == 0) envtmp = secure_getenv("TMPDIR");
envtmp = getenv("TMPDIR");
len = snprintf(path, len = snprintf(path,
sizeof(path), "%s/bt.XXXXXXXXXX", envtmp ? envtmp : "/tmp"); sizeof(path), "%s/bt.XXXXXXXXXX", envtmp ? envtmp : "/tmp");
if (len < 0 || len >= (int)sizeof(path)) { if (len < 0 || len >= (int)sizeof(path)) {

View File

@ -855,11 +855,10 @@ open_temp(HTAB *hashp)
{ {
sigset_t set, oset; sigset_t set, oset;
int len; int len;
char *envtmp = NULL; char *envtmp;
char path[MAXPATHLEN]; char path[MAXPATHLEN];
if (issetugid() == 0) envtmp = secure_getenv("TMPDIR");
envtmp = getenv("TMPDIR");
len = snprintf(path, len = snprintf(path,
sizeof(path), "%s/_hash.XXXXXX", envtmp ? envtmp : "/tmp"); sizeof(path), "%s/_hash.XXXXXX", envtmp ? envtmp : "/tmp");
if (len < 0 || len >= (int)sizeof(path)) { if (len < 0 || len >= (int)sizeof(path)) {

View File

@ -259,12 +259,8 @@ setfsent(void)
LineNo = 0; LineNo = 0;
return (1); return (1);
} }
if (fsp_set == 0) { if (fsp_set == 0)
if (issetugid()) setfstab(secure_getenv("PATH_FSTAB"));
setfstab(NULL);
else
setfstab(getenv("PATH_FSTAB"));
}
if ((_fs_fp = fopen(path_fstab, "re")) != NULL) { if ((_fs_fp = fopen(path_fstab, "re")) != NULL) {
LineNo = 0; LineNo = 0;
return (1); return (1);

View File

@ -422,8 +422,7 @@ globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob11_t *pglob)
* we're not running setuid or setgid) and then trying * we're not running setuid or setgid) and then trying
* the password file * the password file
*/ */
if (issetugid() != 0 || if ((h = secure_getenv("HOME")) == NULL) {
(h = getenv("HOME")) == NULL) {
if (((h = getlogin()) != NULL && if (((h = getlogin()) != NULL &&
(pwd = getpwnam(h)) != NULL) || (pwd = getpwnam(h)) != NULL) ||
(pwd = getpwuid(getuid())) != NULL) (pwd = getpwuid(getuid())) != NULL)

View File

@ -453,8 +453,7 @@ globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
* we're not running setuid or setgid) and then trying * we're not running setuid or setgid) and then trying
* the password file * the password file
*/ */
if (issetugid() != 0 || if ((h = secure_getenv("HOME")) == NULL) {
(h = getenv("HOME")) == NULL) {
if (((h = getlogin()) != NULL && if (((h = getlogin()) != NULL &&
(pwd = getpwnam(h)) != NULL) || (pwd = getpwnam(h)) != NULL) ||
(pwd = getpwuid(getuid())) != NULL) (pwd = getpwuid(getuid())) != NULL)

View File

@ -81,8 +81,9 @@ init_cache(void)
_CITRUS_HASH_INIT(&shared_pool, CI_HASH_SIZE); _CITRUS_HASH_INIT(&shared_pool, CI_HASH_SIZE);
TAILQ_INIT(&shared_unused); TAILQ_INIT(&shared_unused);
shared_max_reuse = -1; shared_max_reuse = -1;
if (!issetugid() && getenv(CI_ENV_MAX_REUSE)) if (secure_getenv(CI_ENV_MAX_REUSE) != NULL)
shared_max_reuse = atoi(getenv(CI_ENV_MAX_REUSE)); shared_max_reuse =
atoi(secure_getenv(CI_ENV_MAX_REUSE));
if (shared_max_reuse < 0) if (shared_max_reuse < 0)
shared_max_reuse = CI_INITIAL_MAX_REUSE; shared_max_reuse = CI_INITIAL_MAX_REUSE;
isinit = true; isinit = true;

View File

@ -282,8 +282,8 @@ _citrus_load_module(_citrus_module_t *rhandle, const char *encname)
int maj, min; int maj, min;
if (_pathI18nModule == NULL) { if (_pathI18nModule == NULL) {
p = getenv("PATH_I18NMODULE"); p = secure_getenv("PATH_I18NMODULE");
if (p != NULL && !issetugid()) { if (p != NULL) {
_pathI18nModule = strdup(p); _pathI18nModule = strdup(p);
if (_pathI18nModule == NULL) if (_pathI18nModule == NULL)
return (ENOMEM); return (ENOMEM);

View File

@ -312,9 +312,9 @@ int
__detect_path_locale(void) __detect_path_locale(void)
{ {
if (_PathLocale == NULL) { if (_PathLocale == NULL) {
char *p = getenv("PATH_LOCALE"); char *p = secure_getenv("PATH_LOCALE");
if (p != NULL && !issetugid()) { if (p != NULL) {
if (strlen(p) + 1/*"/"*/ + ENCODING_LEN + if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
1/*"/"*/ + CATEGORY_LEN >= PATH_MAX) 1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
return (ENAMETOOLONG); return (ENAMETOOLONG);

View File

@ -92,10 +92,7 @@ hesiod_init(context)
ctx = malloc(sizeof(struct hesiod_p)); ctx = malloc(sizeof(struct hesiod_p));
if (ctx) { if (ctx) {
*context = ctx; *context = ctx;
if (!issetugid()) configname = secure_getenv("HESIOD_CONFIG");
configname = getenv("HESIOD_CONFIG");
else
configname = NULL;
if (!configname) if (!configname)
configname = _PATH_HESIOD_CONF; configname = _PATH_HESIOD_CONF;
if (read_config_file(ctx, configname) >= 0) { if (read_config_file(ctx, configname) >= 0) {
@ -103,10 +100,7 @@ hesiod_init(context)
* The default rhs can be overridden by an * The default rhs can be overridden by an
* environment variable. * environment variable.
*/ */
if (!issetugid()) p = secure_getenv("HES_DOMAIN");
p = getenv("HES_DOMAIN");
else
p = NULL;
if (p) { if (p) {
if (ctx->rhs) if (ctx->rhs)
free(ctx->rhs); free(ctx->rhs);

View File

@ -97,7 +97,7 @@ rcmd_af(char **ahost, int rport, const char *locuser, const char *remuser,
static char canonnamebuf[MAXDNAME]; /* is it proper here? */ static char canonnamebuf[MAXDNAME]; /* is it proper here? */
/* call rcmdsh() with specified remote shell if appropriate. */ /* call rcmdsh() with specified remote shell if appropriate. */
if (!issetugid() && (p = getenv("RSH"))) { if ((p = secure_getenv("RSH")) != NULL) {
struct servent *sp = getservbyname("shell", "tcp"); struct servent *sp = getservbyname("shell", "tcp");
if (sp && sp->s_port == rport) if (sp && sp->s_port == rport)

View File

@ -196,7 +196,7 @@ __catopen_l(const char *name, int type, locale_t locale)
pcode = cptr; pcode = cptr;
} }
if ((nlspath = getenv("NLSPATH")) == NULL || issetugid()) if ((nlspath = secure_getenv("NLSPATH")) == NULL)
nlspath = _DEFAULT_NLS_PATH; nlspath = _DEFAULT_NLS_PATH;
if ((base = cptr = strdup(nlspath)) == NULL) { if ((base = cptr = strdup(nlspath)) == NULL) {

View File

@ -177,9 +177,8 @@ mac_init_internal(int ignore_errors)
LIST_INIT(&label_default_head); LIST_INIT(&label_default_head);
if (!issetugid() && getenv("MAC_CONFFILE") != NULL) filename = secure_getenv("MAC_CONFFILE");
filename = getenv("MAC_CONFFILE"); if (filename == NULL)
else
filename = MAC_CONFFILE; filename = MAC_CONFFILE;
file = fopen(filename, "re"); file = fopen(filename, "re");
if (file == NULL) if (file == NULL)

View File

@ -277,7 +277,7 @@ __res_vinit(res_state statp, int preinit) {
#endif /* SOLARIS2 */ #endif /* SOLARIS2 */
/* Allow user to override the local domain definition */ /* Allow user to override the local domain definition */
if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) { if ((cp = secure_getenv("LOCALDOMAIN")) != NULL) {
(void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1); (void)strncpy(statp->defdname, cp, sizeof(statp->defdname) - 1);
statp->defdname[sizeof(statp->defdname) - 1] = '\0'; statp->defdname[sizeof(statp->defdname) - 1] = '\0';
haveenv++; haveenv++;

View File

@ -457,9 +457,7 @@ res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) {
if (statp->options & RES_NOALIASES) if (statp->options & RES_NOALIASES)
return (NULL); return (NULL);
if (issetugid()) file = secure_getenv("HOSTALIASES");
return (NULL);
file = getenv("HOSTALIASES");
if (file == NULL || (fp = fopen(file, "re")) == NULL) if (file == NULL || (fp = fopen(file, "re")) == NULL)
return (NULL); return (NULL);
setbuf(fp, NULL); setbuf(fp, NULL);

View File

@ -60,7 +60,7 @@ tempnam(const char *dir, const char *pfx)
if (!pfx) if (!pfx)
pfx = "tmp."; pfx = "tmp.";
if (issetugid() == 0 && (f = getenv("TMPDIR"))) { if ((f = secure_getenv("TMPDIR")) != NULL) {
(void)snprintf(name, MAXPATHLEN, "%s%s%sXXXXXX", f, (void)snprintf(name, MAXPATHLEN, "%s%s%sXXXXXX", f,
*(f + strlen(f) - 1) == '/'? "": "/", pfx); *(f + strlen(f) - 1) == '/'? "": "/", pfx);
if ((f = _mktemp(name))) if ((f = _mktemp(name)))

View File

@ -60,9 +60,7 @@ tmpfile(void)
char *buf; char *buf;
const char *tmpdir; const char *tmpdir;
tmpdir = NULL; tmpdir = secure_getenv("TMPDIR");
if (issetugid() == 0)
tmpdir = getenv("TMPDIR");
if (tmpdir == NULL) if (tmpdir == NULL)
tmpdir = _PATH_TMP; tmpdir = _PATH_TMP;