X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Fcompat%2Fgetenv.h;fp=src%2Fcommon%2Fcompat%2Fgetenv.h;h=23a6dfeb0c4da9da3c37aa33c3893cd9eb1f7c46;hb=e8fa9fb0539ec3d734f6d0ab91220b4538b2ea30;hp=0000000000000000000000000000000000000000;hpb=d07ceecd2f35ccf383c7529a18dcc9b9c4b8cb17;p=lttng-tools.git diff --git a/src/common/compat/getenv.h b/src/common/compat/getenv.h new file mode 100644 index 000000000..23a6dfeb0 --- /dev/null +++ b/src/common/compat/getenv.h @@ -0,0 +1,43 @@ +#ifndef _COMPAT_GETENV_H +#define _COMPAT_GETENV_H + +/* + * Copyright (C) 2015 - Mathieu Desnoyers + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2 only, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include + +static inline +int lttng_is_setuid_setgid(void) +{ + return geteuid() != getuid() || getegid() != getgid(); +} + +static inline +char *lttng_secure_getenv(const char *name) +{ + if (lttng_is_setuid_setgid()) { + WARN("Getting environment variable '%s' from setuid/setgid binary refused for security reasons.", + name); + return NULL; + } + return getenv(name); +} + +#endif /* _COMPAT_GETENV_H */