Add critical log level
[lttng-ust.git] / src / common / getenv.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2017 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 */
6
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include <sys/types.h>
12 #include <pthread.h>
13 #include <urcu/system.h>
14 #include "common/logging.h"
15 #include "common/macros.h"
16 #include "common/getenv.h"
17
18 enum lttng_env_secure {
19 LTTNG_ENV_SECURE,
20 LTTNG_ENV_NOT_SECURE,
21 };
22
23 struct lttng_env {
24 const char *key;
25 enum lttng_env_secure secure;
26 char *value;
27 };
28
29 /* lttng_ust_getenv_init_mutex provides mutual exclusion for initialization. */
30 static pthread_mutex_t lttng_ust_getenv_init_mutex = PTHREAD_MUTEX_INITIALIZER;
31 static int lttng_ust_getenv_is_init = 0;
32
33 static struct lttng_env lttng_env[] = {
34 /*
35 * LTTNG_UST_DEBUG and LTTNG_UST_ABORT_ON_CRITICAL are used directly by
36 * the internal logging, because they need to be already set for ERR()
37 * used in lttng_ust_getenv_init().
38 */
39 { "LTTNG_UST_DEBUG", LTTNG_ENV_NOT_SECURE, NULL, },
40 { "LTTNG_UST_ABORT_ON_CRITICAL", LTTNG_ENV_NOT_SECURE, NULL, },
41
42 /* Env. var. which can be used in setuid/setgid executables. */
43 { "LTTNG_UST_WITHOUT_BADDR_STATEDUMP", LTTNG_ENV_NOT_SECURE, NULL, },
44 { "LTTNG_UST_REGISTER_TIMEOUT", LTTNG_ENV_NOT_SECURE, NULL, },
45
46 /* Env. var. which are not fetched in setuid/setgid executables. */
47 { "LTTNG_UST_CLOCK_PLUGIN", LTTNG_ENV_SECURE, NULL, },
48 { "LTTNG_UST_GETCPU_PLUGIN", LTTNG_ENV_SECURE, NULL, },
49 { "LTTNG_UST_ALLOW_BLOCKING", LTTNG_ENV_SECURE, NULL, },
50 { "HOME", LTTNG_ENV_SECURE, NULL, },
51 { "LTTNG_HOME", LTTNG_ENV_SECURE, NULL, },
52 };
53
54 static
55 int lttng_is_setuid_setgid(void)
56 {
57 return geteuid() != getuid() || getegid() != getgid();
58 }
59
60 /*
61 * Wrapper over getenv that will only return the values of whitelisted
62 * environment variables when the current process is setuid and/or setgid.
63 */
64 char *lttng_ust_getenv(const char *name)
65 {
66 size_t i;
67 struct lttng_env *e;
68 bool found = false;
69
70 /*
71 * Perform lazy initialization of lttng_ust_getenv for early use
72 * by library constructors.
73 */
74 lttng_ust_getenv_init();
75
76 for (i = 0; i < LTTNG_ARRAY_SIZE(lttng_env); i++) {
77 e = &lttng_env[i];
78
79 if (strcmp(e->key, name) == 0) {
80 found = true;
81 break;
82 }
83 }
84 if (!found) {
85 return NULL;
86 }
87 return e->value;
88 }
89
90 void lttng_ust_getenv_init(void)
91 {
92 size_t i;
93
94 /*
95 * Return early if the init has already completed.
96 */
97 if (CMM_LOAD_SHARED(lttng_ust_getenv_is_init)) {
98 /*
99 * Load lttng_ust_getenv_is_init before reading environment cache.
100 */
101 cmm_smp_rmb();
102 return;
103 }
104
105 pthread_mutex_lock(&lttng_ust_getenv_init_mutex);
106
107 /*
108 * Check again if the init has completed in another thread now that we
109 * have acquired the mutex.
110 */
111 if (lttng_ust_getenv_is_init)
112 goto end_init;
113
114 for (i = 0; i < LTTNG_ARRAY_SIZE(lttng_env); i++) {
115 struct lttng_env *e = &lttng_env[i];
116
117 if (e->secure == LTTNG_ENV_SECURE && lttng_is_setuid_setgid()) {
118 ERR("Getting environment variable '%s' from setuid/setgid binary refused for security reasons.",
119 e->key);
120 continue;
121 }
122 e->value = getenv(e->key);
123 }
124
125 /*
126 * Store environment cache before setting lttng_ust_getenv_is_init to 1.
127 */
128 cmm_smp_wmb();
129 CMM_STORE_SHARED(lttng_ust_getenv_is_init, 1);
130 end_init:
131 pthread_mutex_unlock(&lttng_ust_getenv_init_mutex);
132 }
This page took 0.035883 seconds and 4 git commands to generate.