Get netdata execution path early to avoid user permission issues (#9339)
* Get netdata execution path early to avoid user permission issues
Markos Fountoulakis committed
Jun 16, 2020 at 19:34 UTC
c4fd4aa07c2abb18f5839e1b910294c39c3e30be
4 files changed
+28
-14
daemon/daemon.c
+21
@@ -5,6 +5,27 @@
5
6
char pidfile[FILENAME_MAX + 1] = "";
7
char claimingdirectory[FILENAME_MAX + 1];
8
+char exepath[FILENAME_MAX + 1];
9
+
10
+void get_netdata_execution_path(void)
11
+{
12
+ int ret;
13
+ size_t exepath_size = 0;
14
+ struct passwd *passwd = NULL;
15
+ char *user = NULL;
16
+
17
+ passwd = getpwuid(getuid());
18
+ user = (passwd && passwd->pw_name) ? passwd->pw_name : "";
19
+
20
+ exepath_size = sizeof(exepath) - 1;
21
+ ret = uv_exepath(exepath, &exepath_size);
22
+ if (0 != ret) {
23
+ error("uv_exepath(\"%s\", %u) (user: %s) failed (%s).", exepath, (unsigned)exepath_size, user,
24
+ uv_strerror(ret));
25
+ fatal("Cannot start netdata without getting execution path.");
26
+ }
27
+ exepath[exepath_size] = '\0';
28
+}
29
30
static void chown_open_file(int fd, uid_t uid, gid_t gid) {
31
if(fd == -1) return;
daemon/daemon.h
+3
-1
@@ -10,7 +10,9 @@ extern int become_daemon(int dont_fork, const char *user);
10
extern void netdata_cleanup_and_exit(int i);
11
extern void send_statistics(const char *action, const char *action_result, const char *action_data);
12
13
-extern char pidfile[];
13
+extern void get_netdata_execution_path(void);
14
15
+extern char pidfile[];
16
+extern char exepath[];
17
18
#endif /* NETDATA_DAEMON_H */
daemon/main.c
+3
@@ -1272,6 +1272,9 @@ int main(int argc, char **argv) {
1272
// files using relative filenames
1273
if(chdir(netdata_configured_user_config_dir) == -1)
1274
fatal("Cannot cd to '%s'", netdata_configured_user_config_dir);
1275
+
1276
+ // Get execution path before switching user to avoid permission issues
1277
+ get_netdata_execution_path();
1278
}
1279
1280
{
spawn/spawn.c
+1
-13
@@ -190,8 +190,6 @@ struct spawn_cmd_info *spawn_get_unprocessed_cmd(void)
190
int create_spawn_server(uv_loop_t *loop, uv_pipe_t *spawn_channel, uv_process_t *process)
191
{
192
uv_process_options_t options = {0};
193
- size_t exepath_size;
194
- char exepath[FILENAME_MAX];
193
char *args[3];
194
int ret;
195
#define SPAWN_SERVER_DESCRIPTORS (3)
@@ -202,15 +200,6 @@ int create_spawn_server(uv_loop_t *loop, uv_pipe_t *spawn_channel, uv_process_t
200
passwd = getpwuid(getuid());
201
user = (passwd && passwd->pw_name) ? passwd->pw_name : "";
202
205
- exepath_size = sizeof(exepath);
206
- ret = uv_exepath(exepath, &exepath_size);
207
- if (0 != ret) {
208
- error("uv_exepath(\"%s\", %u) (user: %s) failed (%s).", exepath, (unsigned)exepath_size, user,
209
- uv_strerror(ret));
210
- fatal("Cannot start netdata without the spawn server.");
211
- }
212
-
213
- exepath[exepath_size] = '\0';
203
args[0] = exepath;
204
args[1] = SPAWN_SERVER_COMMAND_LINE_ARGUMENT;
205
args[2] = NULL;
@@ -231,8 +220,7 @@ int create_spawn_server(uv_loop_t *loop, uv_pipe_t *spawn_channel, uv_process_t
220
221
ret = uv_spawn(loop, process, &options); /* execute the netdata binary again as the netdata user */
222
if (0 != ret) {
234
- error("uv_spawn (process: \"%s\", %u) (user: %s) failed (%s).", exepath, (unsigned)exepath_size, user,
235
- uv_strerror(ret));
223
+ error("uv_spawn (process: \"%s\") (user: %s) failed (%s).", exepath, user, uv_strerror(ret));
224
fatal("Cannot start netdata without the spawn server.");
225
}
226