-
Notifications
You must be signed in to change notification settings - Fork 3
/
nginx-1.9-chroot.patch
248 lines (231 loc) · 7.13 KB
/
nginx-1.9-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
diff --git a/src/core/nginx.c b/src/core/nginx.c
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -185,11 +185,11 @@
ngx_uint_t ngx_max_module;
+u_char *ngx_prefix;
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;
static u_char *ngx_conf_file;
static u_char *ngx_conf_params;
static char *ngx_signal;
@@ -244,6 +244,8 @@
NGX_CONF_PATH ")" NGX_LINEFEED
" -g directives : set global directives out of configuration "
"file" NGX_LINEFEED NGX_LINEFEED
+ " -u : disable chroot(2) "
+ "file" NGX_LINEFEED NGX_LINEFEED
);
}
@@ -772,6 +774,10 @@
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
--- a/src/core/ngx_cycle.c
+++ b/src/core/ngx_cycle.c
@@ -1067,6 +1067,10 @@
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
--- a/src/core/ngx_file.c
+++ b/src/core/ngx_file.c
@@ -563,6 +563,7 @@
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 @@
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
--- a/src/core/ngx_string.c
+++ b/src/core/ngx_string.c
@@ -1903,3 +1903,27 @@
}
#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
--- a/src/core/ngx_string.h
+++ b/src/core/ngx_string.h
@@ -229,5 +229,6 @@
#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
--- 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_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
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -4465,6 +4465,10 @@
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
--- a/src/os/unix/ngx_process.h
+++ b/src/os/unix/ngx_process.h
@@ -77,6 +77,7 @@
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
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -49,6 +49,7 @@
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;
@@ -855,6 +856,8 @@
uint64_t cpu_affinity;
ngx_int_t n;
ngx_uint_t i;
+ struct passwd *pw;
+ struct stat stb;
struct rlimit rlmt;
ngx_core_conf_t *ccf;
ngx_listening_t *ls;
@@ -909,6 +912,53 @@
#endif
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
--- a/src/os/unix/ngx_process_cycle.h
+++ b/src/os/unix/ngx_process_cycle.h
@@ -42,6 +42,7 @@
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_threaded;
extern ngx_uint_t ngx_exiting;