prevent chowning log files if they are not regular files, #453
Costa Tsaousis committed
May 26, 2016 at 10:52 UTC
ccbf2fb69e888938a48b4e4c3a5f1238697a36a4
1 file changed
+20
-21
src/daemon.c
+20
-21
@@ -34,6 +34,22 @@ void sig_handler(int signo)
34
netdata_exit = 1;
35
}
36
37
+static void properly_chown_netdata_generated_file(int fd, uid_t uid, gid_t gid) {
38
+ if(fd == -1) return;
39
+
40
+ struct stat buf;
41
+
42
+ if(fstat(fd, &buf) == -1) {
43
+ error("Cannot fstat() fd %d", fd);
44
+ return;
45
+ }
46
+
47
+ if((buf.st_uid != uid || buf.st_gid != gid) && S_ISREG(buf.st_mode)) {
48
+ if(fchown(fd, uid, gid) == -1)
49
+ error("Cannot fchown() fd %d.", fd);
50
+ }
51
+}
52
+
53
int become_user(const char *username, int access_fd, int output_fd, int error_fd, int pid_fd)
54
{
55
struct passwd *pw = getpwnam(username);
@@ -60,27 +76,10 @@ int become_user(const char *username, int access_fd, int output_fd, int error_fd
76
else fatal("Cannot allocate memory for %d supplementary groups", ngroups);
77
}
78
63
- if(getuid() != uid || getgid() != gid) {
64
- if (access_fd != -1) {
65
- if (fchown(access_fd, uid, gid) == -1)
66
- error("Cannot set the ownership of access log file.");
67
- }
68
-
69
- if (output_fd != -1) {
70
- if (fchown(output_fd, uid, gid) == -1)
71
- error("Cannot set the ownership of output log file.");
72
- }
73
-
74
- if (error_fd != -1) {
75
- if (fchown(error_fd, uid, gid) == -1)
76
- error("Cannot set the ownership of error log file.");
77
- }
78
-
79
- if (pid_fd != -1) {
80
- if(fchown(pid_fd, uid, gid) != 0)
81
- error("Cannot set the ownership of pid file.");
82
- }
83
- }
79
+ properly_chown_netdata_generated_file(access_fd, uid, gid);
80
+ properly_chown_netdata_generated_file(output_fd, uid, gid);
81
+ properly_chown_netdata_generated_file(error_fd, uid, gid);
82
+ properly_chown_netdata_generated_file(pid_fd, uid, gid);
83
84
if(supplementary_groups && ngroups) {
85
if(setgroups(ngroups, supplementary_groups) == -1)