Skip to content

Commit 669b71f

Browse files
committed
Fix #2563: ddtrace overrides php error log permissions
We now only change log permissions on log creation. Signed-off-by: Bob Weinand <[email protected]>
1 parent 91e74a8 commit 669b71f

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

ext/logging.c

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,22 @@ _Atomic(uintmax_t) dd_error_log_fd_rotated = 0;
3737

3838
void ddtrace_log_minit(void) {
3939
if (ZSTR_LEN(get_global_DD_TRACE_LOG_FILE())) {
40-
int fd = VCWD_OPEN_MODE(ZSTR_VAL(get_global_DD_TRACE_LOG_FILE()), O_CREAT | O_RDWR | O_APPEND, 0666);
41-
if (fd >= 0) {
40+
int fd = VCWD_OPEN_MODE(ZSTR_VAL(get_global_DD_TRACE_LOG_FILE()), O_RDWR | O_APPEND, 0666);
41+
if (fd < 0) {
42+
// Retry with CREAT to only apply fchmod() on CREAT
43+
fd = VCWD_OPEN_MODE(ZSTR_VAL(get_global_DD_TRACE_LOG_FILE()), O_CREAT | O_RDWR | O_APPEND, 0666);
44+
if (fd < 0) {
45+
return;
46+
}
4247
#ifndef _WIN32
4348
fchmod(fd, 0666); // ignore umask
4449
#endif
45-
atomic_store(&ddtrace_error_log_fd, fd);
46-
47-
time_t now;
48-
time(&now);
49-
atomic_store(&dd_error_log_fd_rotated, (uintmax_t) now);
5050
}
51+
atomic_store(&ddtrace_error_log_fd, fd);
52+
53+
time_t now;
54+
time(&now);
55+
atomic_store(&dd_error_log_fd_rotated, (uintmax_t) now);
5156
}
5257

5358
// no need to call dd_log_set_level here, ddtrace_config_minit() inits the debug config
@@ -62,10 +67,17 @@ void ddtrace_log_rinit(char *error_log) {
6267
return;
6368
}
6469

65-
int desired = VCWD_OPEN_MODE(error_log, O_CREAT | O_RDWR | O_APPEND, 0666);
70+
int desired = VCWD_OPEN_MODE(error_log, O_RDWR | O_APPEND, 0666);
71+
if (desired < 0) {
72+
// Retry with CREAT to only apply fchmod() on CREAT
73+
desired = VCWD_OPEN_MODE(error_log, O_CREAT | O_RDWR | O_APPEND, 0666);
74+
6675
#ifndef _WIN32
67-
fchmod(desired, 0666); // ignore umask
76+
if (desired >= 0) {
77+
fchmod(desired, 0666); // ignore umask
78+
}
6879
#endif
80+
}
6981

7082
time_t now;
7183
time(&now);
@@ -129,10 +141,14 @@ int ddtrace_log_with_time(int fd, const char *msg, int msg_len) {
129141
if (last_check < (uintmax_t)now - 60) { // 1x/min
130142
char pathbuf[MAXPATHLEN];
131143
if (ddtrace_get_fd_path(fd, pathbuf) >= 0) {
132-
int new_fd = VCWD_OPEN_MODE(pathbuf, O_CREAT | O_RDWR | O_APPEND, 0666);
144+
int new_fd = VCWD_OPEN_MODE(pathbuf, O_RDWR | O_APPEND, 0666);
145+
if (new_fd < 0) {
146+
// Retry with CREAT to only apply fchmod() on CREAT
147+
new_fd = VCWD_OPEN_MODE(pathbuf, O_CREAT | O_RDWR | O_APPEND, 0666);
133148
#ifndef _WIN32
134-
fchmod(new_fd, 0666); // ignore umask
149+
fchmod(new_fd, 0666); // ignore umask
135150
#endif
151+
}
136152
dup2(new_fd, fd); // atomic replace
137153
close(new_fd);
138154
}

0 commit comments

Comments
 (0)