xref: /PHP-7.3/sapi/fpm/fpm/fpm_unix.c (revision 1c850bfc)
1 	/* (c) 2007,2008 Andrei Nigmatulin */
2 
3 #include "fpm_config.h"
4 
5 #include <string.h>
6 #include <sys/time.h>
7 #include <sys/resource.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <pwd.h>
12 #include <grp.h>
13 
14 #ifdef HAVE_PRCTL
15 #include <sys/prctl.h>
16 #endif
17 
18 #ifdef HAVE_APPARMOR
19 #include <sys/apparmor.h>
20 #endif
21 
22 #ifdef HAVE_SYS_ACL_H
23 #include <sys/acl.h>
24 #endif
25 
26 #include "fpm.h"
27 #include "fpm_conf.h"
28 #include "fpm_cleanup.h"
29 #include "fpm_clock.h"
30 #include "fpm_stdio.h"
31 #include "fpm_unix.h"
32 #include "fpm_signals.h"
33 #include "zlog.h"
34 
35 size_t fpm_pagesize;
36 
fpm_unix_resolve_socket_premissions(struct fpm_worker_pool_s * wp)37 int fpm_unix_resolve_socket_premissions(struct fpm_worker_pool_s *wp) /* {{{ */
38 {
39 	struct fpm_worker_pool_config_s *c = wp->config;
40 #ifdef HAVE_FPM_ACL
41 	int n;
42 
43 	/* uninitialized */
44 	wp->socket_acl  = NULL;
45 #endif
46 	wp->socket_uid = -1;
47 	wp->socket_gid = -1;
48 	wp->socket_mode = 0660;
49 
50 	if (!c) {
51 		return 0;
52 	}
53 
54 	if (c->listen_mode && *c->listen_mode) {
55 		wp->socket_mode = strtoul(c->listen_mode, 0, 8);
56 	}
57 
58 #ifdef HAVE_FPM_ACL
59 	/* count the users and groups configured */
60 	n = 0;
61 	if (c->listen_acl_users && *c->listen_acl_users) {
62 		char *p;
63 		n++;
64 		for (p=strchr(c->listen_acl_users, ',') ; p ; p=strchr(p+1, ',')) {
65 			n++;
66 		}
67 	}
68 	if (c->listen_acl_groups && *c->listen_acl_groups) {
69 		char *p;
70 		n++;
71 		for (p=strchr(c->listen_acl_groups, ',') ; p ; p=strchr(p+1, ',')) {
72 			n++;
73 		}
74 	}
75 	/* if ACL configured */
76 	if (n) {
77 		acl_t acl;
78 		acl_entry_t entry;
79 		acl_permset_t perm;
80 		char *tmp, *p, *end;
81 
82 		acl = acl_init(n);
83 		if (!acl) {
84 			zlog(ZLOG_SYSERROR, "[pool %s] cannot allocate ACL", wp->config->name);
85 			return -1;
86 		}
87 		/* Create USER ACL */
88 		if (c->listen_acl_users && *c->listen_acl_users) {
89 			struct passwd *pwd;
90 
91 			tmp = estrdup(c->listen_acl_users);
92 			for (p=tmp ; p ; p=end) {
93 				if ((end = strchr(p, ','))) {
94 					*end++ = 0;
95 				}
96 				pwd = getpwnam(p);
97 				if (pwd) {
98 					zlog(ZLOG_DEBUG, "[pool %s] user '%s' have uid=%d", wp->config->name, p, pwd->pw_uid);
99 				} else {
100 					zlog(ZLOG_SYSERROR, "[pool %s] cannot get uid for user '%s'", wp->config->name, p);
101 					acl_free(acl);
102 					efree(tmp);
103 					return -1;
104 				}
105 				if (0 > acl_create_entry(&acl, &entry) ||
106 					0 > acl_set_tag_type(entry, ACL_USER) ||
107 					0 > acl_set_qualifier(entry, &pwd->pw_uid) ||
108 					0 > acl_get_permset(entry, &perm) ||
109 					0 > acl_clear_perms (perm) ||
110 					0 > acl_add_perm (perm, ACL_READ) ||
111 					0 > acl_add_perm (perm, ACL_WRITE)) {
112 					zlog(ZLOG_SYSERROR, "[pool %s] cannot create ACL for user '%s'", wp->config->name, p);
113 					acl_free(acl);
114 					efree(tmp);
115 					return -1;
116 				}
117 			}
118 			efree(tmp);
119 		}
120 		/* Create GROUP ACL */
121 		if (c->listen_acl_groups && *c->listen_acl_groups) {
122 			struct group *grp;
123 
124 			tmp = estrdup(c->listen_acl_groups);
125 			for (p=tmp ; p ; p=end) {
126 				if ((end = strchr(p, ','))) {
127 					*end++ = 0;
128 				}
129 				grp = getgrnam(p);
130 				if (grp) {
131 					zlog(ZLOG_DEBUG, "[pool %s] group '%s' have gid=%d", wp->config->name, p, grp->gr_gid);
132 				} else {
133 					zlog(ZLOG_SYSERROR, "[pool %s] cannot get gid for group '%s'", wp->config->name, p);
134 					acl_free(acl);
135 					efree(tmp);
136 					return -1;
137 				}
138 				if (0 > acl_create_entry(&acl, &entry) ||
139 					0 > acl_set_tag_type(entry, ACL_GROUP) ||
140 					0 > acl_set_qualifier(entry, &grp->gr_gid) ||
141 					0 > acl_get_permset(entry, &perm) ||
142 					0 > acl_clear_perms (perm) ||
143 					0 > acl_add_perm (perm, ACL_READ) ||
144 					0 > acl_add_perm (perm, ACL_WRITE)) {
145 					zlog(ZLOG_SYSERROR, "[pool %s] cannot create ACL for group '%s'", wp->config->name, p);
146 					acl_free(acl);
147 					efree(tmp);
148 					return -1;
149 				}
150 			}
151 			efree(tmp);
152 		}
153 		if (c->listen_owner && *c->listen_owner) {
154 			zlog(ZLOG_WARNING, "[pool %s] ACL set, listen.owner = '%s' is ignored", wp->config->name, c->listen_owner);
155 		}
156 		if (c->listen_group && *c->listen_group) {
157 			zlog(ZLOG_WARNING, "[pool %s] ACL set, listen.group = '%s' is ignored", wp->config->name, c->listen_group);
158 		}
159 		wp->socket_acl  = acl;
160 		return 0;
161 	}
162 	/* When listen.users and listen.groups not configured, continue with standard right */
163 #endif
164 
165 	if (c->listen_owner && *c->listen_owner) {
166 		struct passwd *pwd;
167 
168 		pwd = getpwnam(c->listen_owner);
169 		if (!pwd) {
170 			zlog(ZLOG_SYSERROR, "[pool %s] cannot get uid for user '%s'", wp->config->name, c->listen_owner);
171 			return -1;
172 		}
173 
174 		wp->socket_uid = pwd->pw_uid;
175 		wp->socket_gid = pwd->pw_gid;
176 	}
177 
178 	if (c->listen_group && *c->listen_group) {
179 		struct group *grp;
180 
181 		grp = getgrnam(c->listen_group);
182 		if (!grp) {
183 			zlog(ZLOG_SYSERROR, "[pool %s] cannot get gid for group '%s'", wp->config->name, c->listen_group);
184 			return -1;
185 		}
186 		wp->socket_gid = grp->gr_gid;
187 	}
188 
189 	return 0;
190 }
191 /* }}} */
192 
fpm_unix_set_socket_premissions(struct fpm_worker_pool_s * wp,const char * path)193 int fpm_unix_set_socket_premissions(struct fpm_worker_pool_s *wp, const char *path) /* {{{ */
194 {
195 #ifdef HAVE_FPM_ACL
196 	if (wp->socket_acl) {
197 		acl_t aclfile, aclconf;
198 		acl_entry_t entryfile, entryconf;
199 		int i;
200 
201 		/* Read the socket ACL */
202 		aclconf = wp->socket_acl;
203 		aclfile = acl_get_file (path, ACL_TYPE_ACCESS);
204 		if (!aclfile) {
205 			zlog(ZLOG_SYSERROR, "[pool %s] failed to read the ACL of the socket '%s'", wp->config->name, path);
206 			return -1;
207 		}
208 		/* Copy the new ACL entry from config */
209 		for (i=ACL_FIRST_ENTRY ; acl_get_entry(aclconf, i, &entryconf) ; i=ACL_NEXT_ENTRY) {
210 			if (0 > acl_create_entry (&aclfile, &entryfile) ||
211 			    0 > acl_copy_entry(entryfile, entryconf)) {
212 				zlog(ZLOG_SYSERROR, "[pool %s] failed to add entry to the ACL of the socket '%s'", wp->config->name, path);
213 				acl_free(aclfile);
214 				return -1;
215 			}
216 		}
217 		/* Write the socket ACL */
218 		if (0 > acl_calc_mask (&aclfile) ||
219 			0 > acl_valid (aclfile) ||
220 			0 > acl_set_file (path, ACL_TYPE_ACCESS, aclfile)) {
221 			zlog(ZLOG_SYSERROR, "[pool %s] failed to write the ACL of the socket '%s'", wp->config->name, path);
222 			acl_free(aclfile);
223 			return -1;
224 		} else {
225 			zlog(ZLOG_DEBUG, "[pool %s] ACL of the socket '%s' is set", wp->config->name, path);
226 		}
227 
228 		acl_free(aclfile);
229 		return 0;
230 	}
231 	/* When listen.users and listen.groups not configured, continue with standard right */
232 #endif
233 
234 	if (wp->socket_uid != -1 || wp->socket_gid != -1) {
235 		if (0 > chown(path, wp->socket_uid, wp->socket_gid)) {
236 			zlog(ZLOG_SYSERROR, "[pool %s] failed to chown() the socket '%s'", wp->config->name, wp->config->listen_address);
237 			return -1;
238 		}
239 	}
240 	return 0;
241 }
242 /* }}} */
243 
fpm_unix_free_socket_premissions(struct fpm_worker_pool_s * wp)244 int fpm_unix_free_socket_premissions(struct fpm_worker_pool_s *wp) /* {{{ */
245 {
246 #ifdef HAVE_FPM_ACL
247 	if (wp->socket_acl) {
248 		return acl_free(wp->socket_acl);
249 	}
250 #endif
251 	return 0;
252 }
253 /* }}} */
254 
fpm_unix_conf_wp(struct fpm_worker_pool_s * wp)255 static int fpm_unix_conf_wp(struct fpm_worker_pool_s *wp) /* {{{ */
256 {
257 	struct passwd *pwd;
258 	int is_root = !geteuid();
259 
260 	if (is_root) {
261 		if (wp->config->user && *wp->config->user) {
262 			if (strlen(wp->config->user) == strspn(wp->config->user, "0123456789")) {
263 				wp->set_uid = strtoul(wp->config->user, 0, 10);
264 			} else {
265 				struct passwd *pwd;
266 
267 				pwd = getpwnam(wp->config->user);
268 				if (!pwd) {
269 					zlog(ZLOG_ERROR, "[pool %s] cannot get uid for user '%s'", wp->config->name, wp->config->user);
270 					return -1;
271 				}
272 
273 				wp->set_uid = pwd->pw_uid;
274 				wp->set_gid = pwd->pw_gid;
275 
276 				wp->user = strdup(pwd->pw_name);
277 				wp->home = strdup(pwd->pw_dir);
278 			}
279 		}
280 
281 		if (wp->config->group && *wp->config->group) {
282 			if (strlen(wp->config->group) == strspn(wp->config->group, "0123456789")) {
283 				wp->set_gid = strtoul(wp->config->group, 0, 10);
284 			} else {
285 				struct group *grp;
286 
287 				grp = getgrnam(wp->config->group);
288 				if (!grp) {
289 					zlog(ZLOG_ERROR, "[pool %s] cannot get gid for group '%s'", wp->config->name, wp->config->group);
290 					return -1;
291 				}
292 				wp->set_gid = grp->gr_gid;
293 			}
294 		}
295 
296 		if (!fpm_globals.run_as_root) {
297 			if (wp->set_uid == 0 || wp->set_gid == 0) {
298 				zlog(ZLOG_ERROR, "[pool %s] please specify user and group other than root", wp->config->name);
299 				return -1;
300 			}
301 		}
302 	} else { /* not root */
303 		if (wp->config->user && *wp->config->user) {
304 			zlog(ZLOG_NOTICE, "[pool %s] 'user' directive is ignored when FPM is not running as root", wp->config->name);
305 		}
306 		if (wp->config->group && *wp->config->group) {
307 			zlog(ZLOG_NOTICE, "[pool %s] 'group' directive is ignored when FPM is not running as root", wp->config->name);
308 		}
309 		if (wp->config->chroot && *wp->config->chroot) {
310 			zlog(ZLOG_NOTICE, "[pool %s] 'chroot' directive is ignored when FPM is not running as root", wp->config->name);
311 		}
312 		if (wp->config->process_priority != 64) {
313 			zlog(ZLOG_NOTICE, "[pool %s] 'process.priority' directive is ignored when FPM is not running as root", wp->config->name);
314 		}
315 
316 		/* set up HOME and USER anyway */
317 		pwd = getpwuid(getuid());
318 		if (pwd) {
319 			wp->user = strdup(pwd->pw_name);
320 			wp->home = strdup(pwd->pw_dir);
321 		}
322 	}
323 	return 0;
324 }
325 /* }}} */
326 
fpm_unix_init_child(struct fpm_worker_pool_s * wp)327 int fpm_unix_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
328 {
329 	int is_root = !geteuid();
330 	int made_chroot = 0;
331 
332 	if (wp->config->rlimit_files) {
333 		struct rlimit r;
334 
335 		r.rlim_max = r.rlim_cur = (rlim_t) wp->config->rlimit_files;
336 
337 		if (0 > setrlimit(RLIMIT_NOFILE, &r)) {
338 			zlog(ZLOG_SYSERROR, "[pool %s] failed to set rlimit_files for this pool. Please check your system limits or decrease rlimit_files. setrlimit(RLIMIT_NOFILE, %d)", wp->config->name, wp->config->rlimit_files);
339 		}
340 	}
341 
342 	if (wp->config->rlimit_core) {
343 		struct rlimit r;
344 
345 		r.rlim_max = r.rlim_cur = wp->config->rlimit_core == -1 ? (rlim_t) RLIM_INFINITY : (rlim_t) wp->config->rlimit_core;
346 
347 		if (0 > setrlimit(RLIMIT_CORE, &r)) {
348 			zlog(ZLOG_SYSERROR, "[pool %s] failed to set rlimit_core for this pool. Please check your system limits or decrease rlimit_core. setrlimit(RLIMIT_CORE, %d)", wp->config->name, wp->config->rlimit_core);
349 		}
350 	}
351 
352 	if (is_root && wp->config->chroot && *wp->config->chroot) {
353 		if (0 > chroot(wp->config->chroot)) {
354 			zlog(ZLOG_SYSERROR, "[pool %s] failed to chroot(%s)",  wp->config->name, wp->config->chroot);
355 			return -1;
356 		}
357 		made_chroot = 1;
358 	}
359 
360 	if (wp->config->chdir && *wp->config->chdir) {
361 		if (0 > chdir(wp->config->chdir)) {
362 			zlog(ZLOG_SYSERROR, "[pool %s] failed to chdir(%s)", wp->config->name, wp->config->chdir);
363 			return -1;
364 		}
365 	} else if (made_chroot) {
366 		if (0 > chdir("/")) {
367 			zlog(ZLOG_WARNING, "[pool %s] failed to chdir(/)", wp->config->name);
368 		}
369 	}
370 
371 	if (is_root) {
372 
373 		if (wp->config->process_priority != 64) {
374 			if (setpriority(PRIO_PROCESS, 0, wp->config->process_priority) < 0) {
375 				zlog(ZLOG_SYSERROR, "[pool %s] Unable to set priority for this new process", wp->config->name);
376 				return -1;
377 			}
378 		}
379 
380 		if (wp->set_gid) {
381 			if (0 > setgid(wp->set_gid)) {
382 				zlog(ZLOG_SYSERROR, "[pool %s] failed to setgid(%d)", wp->config->name, wp->set_gid);
383 				return -1;
384 			}
385 		}
386 		if (wp->set_uid) {
387 			if (0 > initgroups(wp->config->user, wp->set_gid)) {
388 				zlog(ZLOG_SYSERROR, "[pool %s] failed to initgroups(%s, %d)", wp->config->name, wp->config->user, wp->set_gid);
389 				return -1;
390 			}
391 			if (0 > setuid(wp->set_uid)) {
392 				zlog(ZLOG_SYSERROR, "[pool %s] failed to setuid(%d)", wp->config->name, wp->set_uid);
393 				return -1;
394 			}
395 		}
396 	}
397 
398 #ifdef HAVE_PRCTL
399 	if (wp->config->process_dumpable && 0 > prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)) {
400 		zlog(ZLOG_SYSERROR, "[pool %s] failed to prctl(PR_SET_DUMPABLE)", wp->config->name);
401 	}
402 #endif
403 
404 	if (0 > fpm_clock_init()) {
405 		return -1;
406 	}
407 
408 #ifdef HAVE_APPARMOR
409 	if (wp->config->apparmor_hat) {
410 		char *con, *new_con;
411 
412 		if (aa_getcon(&con, NULL) == -1) {
413 			zlog(ZLOG_SYSERROR, "[pool %s] failed to query apparmor confinement. Please check if \"/proc/*/attr/current\" is read and writeable.", wp->config->name);
414 			return -1;
415 		}
416 
417 		new_con = malloc(strlen(con) + strlen(wp->config->apparmor_hat) + 3); // // + 0 Byte
418 		if (!new_con) {
419 			zlog(ZLOG_SYSERROR, "[pool %s] failed to allocate memory for apparmor hat change.", wp->config->name);
420 			return -1;
421 		}
422 
423 		if (0 > sprintf(new_con, "%s//%s", con, wp->config->apparmor_hat)) {
424 			zlog(ZLOG_SYSERROR, "[pool %s] failed to construct apparmor confinement.", wp->config->name);
425 			return -1;
426 		}
427 
428 		if (0 > aa_change_profile(new_con)) {
429 			zlog(ZLOG_SYSERROR, "[pool %s] failed to change to new confinement (%s). Please check if \"/proc/*/attr/current\" is read and writeable and \"change_profile -> %s//*\" is allowed.", wp->config->name, new_con, con);
430 			return -1;
431 		}
432 
433 		free(con);
434 		free(new_con);
435 	}
436 #endif
437 
438 	return 0;
439 }
440 /* }}} */
441 
fpm_unix_init_main()442 int fpm_unix_init_main() /* {{{ */
443 {
444 	struct fpm_worker_pool_s *wp;
445 	int is_root = !geteuid();
446 
447 	if (fpm_global_config.rlimit_files) {
448 		struct rlimit r;
449 
450 		r.rlim_max = r.rlim_cur = (rlim_t) fpm_global_config.rlimit_files;
451 
452 		if (0 > setrlimit(RLIMIT_NOFILE, &r)) {
453 			zlog(ZLOG_SYSERROR, "failed to set rlimit_core for this pool. Please check your system limits or decrease rlimit_files. setrlimit(RLIMIT_NOFILE, %d)", fpm_global_config.rlimit_files);
454 			return -1;
455 		}
456 	}
457 
458 	if (fpm_global_config.rlimit_core) {
459 		struct rlimit r;
460 
461 		r.rlim_max = r.rlim_cur = fpm_global_config.rlimit_core == -1 ? (rlim_t) RLIM_INFINITY : (rlim_t) fpm_global_config.rlimit_core;
462 
463 		if (0 > setrlimit(RLIMIT_CORE, &r)) {
464 			zlog(ZLOG_SYSERROR, "failed to set rlimit_core for this pool. Please check your system limits or decrease rlimit_core. setrlimit(RLIMIT_CORE, %d)", fpm_global_config.rlimit_core);
465 			return -1;
466 		}
467 	}
468 
469 	fpm_pagesize = getpagesize();
470 	if (fpm_global_config.daemonize) {
471 		/*
472 		 * If daemonize, the calling process will die soon
473 		 * and the master process continues to initialize itself.
474 		 *
475 		 * The parent process has then to wait for the master
476 		 * process to initialize to return a consistent exit
477 		 * value. For this pupose, the master process will
478 		 * send \"1\" into the pipe if everything went well
479 		 * and \"0\" otherwise.
480 		 */
481 
482 
483 		struct timeval tv;
484 		fd_set rfds;
485 		int ret;
486 
487 		if (pipe(fpm_globals.send_config_pipe) == -1) {
488 			zlog(ZLOG_SYSERROR, "failed to create pipe");
489 			return -1;
490 		}
491 
492 		/* then fork */
493 		pid_t pid = fork();
494 		switch (pid) {
495 
496 			case -1 : /* error */
497 				zlog(ZLOG_SYSERROR, "failed to daemonize");
498 				return -1;
499 
500 			case 0 : /* children */
501 				close(fpm_globals.send_config_pipe[0]); /* close the read side of the pipe */
502 				break;
503 
504 			default : /* parent */
505 				close(fpm_globals.send_config_pipe[1]); /* close the write side of the pipe */
506 
507 				/*
508 				 * wait for 10s before exiting with error
509 				 * the child is supposed to send 1 or 0 into the pipe to tell the parent
510 				 * how it goes for it
511 				 */
512 				FD_ZERO(&rfds);
513 				FD_SET(fpm_globals.send_config_pipe[0], &rfds);
514 
515 				tv.tv_sec = 10;
516 				tv.tv_usec = 0;
517 
518 				zlog(ZLOG_DEBUG, "The calling process is waiting for the master process to ping via fd=%d", fpm_globals.send_config_pipe[0]);
519 				ret = select(fpm_globals.send_config_pipe[0] + 1, &rfds, NULL, NULL, &tv);
520 				if (ret == -1) {
521 					zlog(ZLOG_SYSERROR, "failed to select");
522 					exit(FPM_EXIT_SOFTWARE);
523 				}
524 				if (ret) { /* data available */
525 					int readval;
526 					ret = read(fpm_globals.send_config_pipe[0], &readval, sizeof(readval));
527 					if (ret == -1) {
528 						zlog(ZLOG_SYSERROR, "failed to read from pipe");
529 						exit(FPM_EXIT_SOFTWARE);
530 					}
531 
532 					if (ret == 0) {
533 						zlog(ZLOG_ERROR, "no data have been read from pipe");
534 						exit(FPM_EXIT_SOFTWARE);
535 					} else {
536 						if (readval == 1) {
537 							zlog(ZLOG_DEBUG, "I received a valid acknowledge from the master process, I can exit without error");
538 							fpm_cleanups_run(FPM_CLEANUP_PARENT_EXIT);
539 							exit(FPM_EXIT_OK);
540 						} else {
541 							zlog(ZLOG_DEBUG, "The master process returned an error !");
542 							exit(FPM_EXIT_SOFTWARE);
543 						}
544 					}
545 				} else { /* no date sent ! */
546 					zlog(ZLOG_ERROR, "the master process didn't send back its status (via the pipe to the calling process)");
547 				  exit(FPM_EXIT_SOFTWARE);
548 				}
549 				exit(FPM_EXIT_SOFTWARE);
550 		}
551 	}
552 
553 	/* continue as a child */
554 	setsid();
555 	if (0 > fpm_clock_init()) {
556 		return -1;
557 	}
558 
559 	if (fpm_global_config.process_priority != 64) {
560 		if (is_root) {
561 			if (setpriority(PRIO_PROCESS, 0, fpm_global_config.process_priority) < 0) {
562 				zlog(ZLOG_SYSERROR, "Unable to set priority for the master process");
563 				return -1;
564 			}
565 		} else {
566 			zlog(ZLOG_NOTICE, "'process.priority' directive is ignored when FPM is not running as root");
567 		}
568 	}
569 
570 	fpm_globals.parent_pid = getpid();
571 	for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
572 		if (0 > fpm_unix_conf_wp(wp)) {
573 			return -1;
574 		}
575 	}
576 
577 	return 0;
578 }
579 /* }}} */
580