forked from rnagy/nginx_chroot_patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nginx-1.12.0-chroot.patch
254 lines (239 loc) · 8 KB
/
nginx-1.12.0-chroot.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
diff --git a/src/core/nginx.c b/src/core/nginx.c
index cdc067e..197b935 100644
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -174,7 +174,7 @@ ngx_module_t ngx_core_module = {
static ngx_uint_t ngx_show_help;
static ngx_uint_t ngx_show_version;
static ngx_uint_t ngx_show_configure;
-static u_char *ngx_prefix;
+u_char *ngx_prefix;
static u_char *ngx_conf_file;
static u_char *ngx_conf_params;
static char *ngx_signal;
@@ -403,6 +403,8 @@ ngx_show_version_info()
")" NGX_LINEFEED
" -g directives : set global directives out of configuration "
"file" NGX_LINEFEED NGX_LINEFEED
+ " -u : disable chroot(2) "
+ "file" NGX_LINEFEED NGX_LINEFEED
);
}
@@ -806,6 +808,10 @@ ngx_get_options(int argc, char *const *argv)
ngx_log_stderr(0, "invalid option: \"-s %s\"", ngx_signal);
return NGX_ERROR;
+ case 'u':
+ ngx_chrooted = 0;
+ break;
+
default:
ngx_log_stderr(0, "invalid option: \"%c\"", *(p - 1));
return NGX_ERROR;
diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c
index f103266..5a01f63 100644
--- a/src/core/ngx_cycle.c
+++ b/src/core/ngx_cycle.c
@@ -1105,6 +1105,10 @@ ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user)
i = 0;
}
+ if ((ngx_process == NGX_PROCESS_WORKER) && ngx_chrooted) {
+ ngx_strip_chroot(&file[i].name);
+ }
+
if (file[i].name.len == 0) {
continue;
}
diff --git a/src/core/ngx_file.c b/src/core/ngx_file.c
index 3ebd73d..9f79ce2 100644
--- a/src/core/ngx_file.c
+++ b/src/core/ngx_file.c
@@ -563,6 +563,7 @@ ngx_add_path(ngx_conf_t *cf, ngx_path_t **slot)
ngx_int_t
ngx_create_paths(ngx_cycle_t *cycle, ngx_uid_t user)
{
+ u_char *prefix;
ngx_err_t err;
ngx_uint_t i;
ngx_path_t **path;
@@ -570,6 +571,21 @@ ngx_create_paths(ngx_cycle_t *cycle, ngx_uid_t user)
path = cycle->paths.elts;
for (i = 0; i < cycle->paths.nelts; i++) {
+ if (ngx_chrooted) {
+ if (ngx_prefix)
+ prefix = ngx_prefix;
+ else
+ prefix = NGX_PREFIX;
+ if (chdir(prefix) == -1) {
+ ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
+ "chdir(\"%s\") failed", prefix);
+ return NGX_ERROR;
+ }
+ ngx_strip_chroot(&path[i]->name);
+ path[i]->name.data++;
+ path[i]->name.len--;
+ }
+
if (ngx_create_dir(path[i]->name.data, 0700) == NGX_FILE_ERROR) {
err = ngx_errno;
if (err != NGX_EEXIST) {
diff --git a/src/core/ngx_string.c b/src/core/ngx_string.c
index cf665a4..83ec16f 100644
--- a/src/core/ngx_string.c
+++ b/src/core/ngx_string.c
@@ -1974,3 +1974,27 @@ ngx_memcpy(void *dst, const void *src, size_t n)
}
#endif
+
+void
+ngx_strip_chroot(ngx_str_t *path)
+{
+ int plen;
+ u_char *prefix;
+
+ if (ngx_prefix)
+ prefix = ngx_prefix;
+ else
+ prefix = NGX_PREFIX;
+
+ if (prefix[strlen(prefix) - 1] == '/')
+ plen = strlen(prefix) - 1;
+ else
+ plen = strlen(prefix);
+
+ if (!ngx_strncmp(path->data, prefix, strlen(prefix))) {
+ char *x, *buf = malloc(path->len);
+ x = ngx_cpystrn(buf, path->data + plen, path->len);
+ path->len = (x - buf);
+ path->data = buf;
+ }
+}
diff --git a/src/core/ngx_string.h b/src/core/ngx_string.h
index 7363bd2..4b2c520 100644
--- a/src/core/ngx_string.h
+++ b/src/core/ngx_string.h
@@ -230,5 +230,6 @@ void ngx_sort(void *base, size_t n, size_t size,
#define ngx_value_helper(n) #n
#define ngx_value(n) ngx_value_helper(n)
+void ngx_strip_chroot(ngx_str_t *root);
#endif /* _NGX_STRING_H_INCLUDED_ */
diff --git a/src/http/modules/ngx_http_auth_basic_module.c b/src/http/modules/ngx_http_auth_basic_module.c
index 8ec5329..38892c1 100644
--- a/src/http/modules/ngx_http_auth_basic_module.c
+++ b/src/http/modules/ngx_http_auth_basic_module.c
@@ -453,6 +453,10 @@ ngx_http_auth_basic_user_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
+ if (ngx_chrooted) {
+ ngx_strip_chroot(&value[1]);
+ }
+
ccv.cf = cf;
ccv.value = &value[1];
ccv.complex_value = &alcf->user_file;
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index df7e8d4..6c58e31 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -4464,6 +4464,10 @@ ngx_http_core_root(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
value = cf->args->elts;
+ if (ngx_chrooted && value[1].data != NULL) {
+ ngx_strip_chroot(&value[1]);
+ }
+
if (ngx_strstr(value[1].data, "$document_root")
|| ngx_strstr(value[1].data, "${document_root}"))
{
diff --git a/src/os/unix/ngx_process.h b/src/os/unix/ngx_process.h
index 7b5e8c0..14f7b89 100644
--- a/src/os/unix/ngx_process.h
+++ b/src/os/unix/ngx_process.h
@@ -77,6 +77,7 @@ void ngx_debug_point(void);
extern int ngx_argc;
extern char **ngx_argv;
extern char **ngx_os_argv;
+extern u_char *ngx_prefix;
extern ngx_pid_t ngx_pid;
extern ngx_socket_t ngx_channel;
diff --git a/src/os/unix/ngx_process_cycle.c b/src/os/unix/ngx_process_cycle.c
index 9be6376..30778be 100644
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -45,6 +45,7 @@ sig_atomic_t ngx_reopen;
sig_atomic_t ngx_change_binary;
ngx_pid_t ngx_new_binary;
ngx_uint_t ngx_inherited;
+ngx_uint_t ngx_chrooted = 1;
ngx_uint_t ngx_daemonized;
sig_atomic_t ngx_noaccept;
@@ -787,6 +788,8 @@ ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker)
ngx_int_t n;
ngx_uint_t i;
ngx_cpuset_t *cpu_affinity;
+ struct passwd *pw;
+ struct stat stb;
struct rlimit rlmt;
ngx_core_conf_t *ccf;
ngx_listening_t *ls;
@@ -828,6 +831,53 @@ ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker)
}
if (geteuid() == 0) {
+ char *prefix;
+
+ if (!ngx_chrooted) {
+ goto nochroot;
+ }
+
+ if ((pw = getpwnam(ccf->username)) == NULL) {
+ ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
+ "getpwnam(%s) failed", ccf->username);
+ /* fatal */
+ exit(2);
+ }
+
+ if (ngx_prefix)
+ prefix = (char *)ngx_prefix;
+ else
+ prefix = pw->pw_dir;
+
+ if (stat(prefix, &stb) == -1) {
+ ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
+ "stat(%s) failed", prefix);
+ /* fatal */
+ exit(2);
+ }
+
+ if (stb.st_uid != 0 || (stb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
+ ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
+ "bad privsep dir permissions on %s", prefix);
+ /* fatal */
+ exit(2);
+ }
+
+ if (chroot(prefix) == -1) {
+ ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
+ "chroot(%s) failed", prefix);
+ /* fatal */
+ exit(2);
+ }
+
+ if (chdir("/") == -1) {
+ ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
+ "chdir(\"/\") failed");
+ /* fatal */
+ exit(2);
+ }
+
+nochroot:
if (setgid(ccf->group) == -1) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
"setgid(%d) failed", ccf->group);
diff --git a/src/os/unix/ngx_process_cycle.h b/src/os/unix/ngx_process_cycle.h
index 69495d5..19ebe5c 100644
--- a/src/os/unix/ngx_process_cycle.h
+++ b/src/os/unix/ngx_process_cycle.h
@@ -43,6 +43,7 @@ extern ngx_uint_t ngx_worker;
extern ngx_pid_t ngx_pid;
extern ngx_pid_t ngx_new_binary;
extern ngx_uint_t ngx_inherited;
+extern ngx_uint_t ngx_chrooted;
extern ngx_uint_t ngx_daemonized;
extern ngx_uint_t ngx_exiting;