Commit | Line | Data |
---|---|---|
6f626d28 MD |
1 | /* |
2 | * Copyright (C) 2017 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
3 | * | |
4 | * This library is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU Lesser General Public | |
6 | * License as published by the Free Software Foundation; only | |
7 | * version 2.1 of the License. | |
8 | * | |
9 | * This library is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * Lesser General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Lesser General Public | |
15 | * License along with this library; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | ||
19 | #include <stdlib.h> | |
20 | #include <unistd.h> | |
21 | #include <stdbool.h> | |
b4051ad8 | 22 | #include <stddef.h> |
6f626d28 MD |
23 | #include <sys/types.h> |
24 | #include <usterr-signal-safe.h> | |
25 | #include <helper.h> | |
26 | #include "getenv.h" | |
27 | ||
28 | enum lttng_env_secure { | |
29 | LTTNG_ENV_SECURE, | |
30 | LTTNG_ENV_NOT_SECURE, | |
31 | }; | |
32 | ||
33 | struct lttng_env { | |
34 | const char *key; | |
35 | enum lttng_env_secure secure; | |
36 | char *value; | |
37 | }; | |
38 | ||
39 | static struct lttng_env lttng_env[] = { | |
40 | /* | |
41 | * LTTNG_UST_DEBUG is used directly by snprintf, because it | |
42 | * needs to be already set for ERR() used in | |
43 | * lttng_ust_getenv_init(). | |
44 | */ | |
45 | { "LTTNG_UST_DEBUG", LTTNG_ENV_NOT_SECURE, NULL, }, | |
46 | ||
47 | /* Env. var. which can be used in setuid/setgid executables. */ | |
48 | { "LTTNG_UST_WITHOUT_BADDR_STATEDUMP", LTTNG_ENV_NOT_SECURE, NULL, }, | |
49 | { "LTTNG_UST_REGISTER_TIMEOUT", LTTNG_ENV_NOT_SECURE, NULL, }, | |
50 | ||
51 | /* Env. var. which are not fetched in setuid/setgid executables. */ | |
52 | { "LTTNG_UST_CLOCK_PLUGIN", LTTNG_ENV_SECURE, NULL, }, | |
53 | { "LTTNG_UST_GETCPU_PLUGIN", LTTNG_ENV_SECURE, NULL, }, | |
b2c5f61a | 54 | { "LTTNG_UST_ALLOW_BLOCKING", LTTNG_ENV_SECURE, NULL, }, |
6f626d28 MD |
55 | { "HOME", LTTNG_ENV_SECURE, NULL, }, |
56 | { "LTTNG_HOME", LTTNG_ENV_SECURE, NULL, }, | |
57 | }; | |
58 | ||
59 | static | |
60 | int lttng_is_setuid_setgid(void) | |
61 | { | |
62 | return geteuid() != getuid() || getegid() != getgid(); | |
63 | } | |
64 | ||
65 | char *lttng_getenv(const char *name) | |
66 | { | |
67 | size_t i; | |
68 | struct lttng_env *e; | |
69 | bool found = false; | |
70 | ||
71 | for (i = 0; i < LTTNG_ARRAY_SIZE(lttng_env); i++) { | |
72 | e = <tng_env[i]; | |
73 | ||
74 | if (strcmp(e->key, name) == 0) { | |
75 | found = true; | |
76 | break; | |
77 | } | |
78 | } | |
79 | if (!found) { | |
80 | return NULL; | |
81 | } | |
82 | return e->value; | |
83 | } | |
84 | ||
85 | void lttng_ust_getenv_init(void) | |
86 | { | |
87 | size_t i; | |
88 | ||
89 | for (i = 0; i < LTTNG_ARRAY_SIZE(lttng_env); i++) { | |
90 | struct lttng_env *e = <tng_env[i]; | |
91 | ||
92 | if (e->secure == LTTNG_ENV_SECURE && lttng_is_setuid_setgid()) { | |
93 | ERR("Getting environment variable '%s' from setuid/setgid binary refused for security reasons.", | |
94 | e->key); | |
95 | continue; | |
96 | } | |
97 | e->value = getenv(e->key); | |
98 | } | |
99 | } |