aboutsummaryrefslogtreecommitdiffstats
path: root/meta/recipes-extended/lighttpd/lighttpd/0001-mod_cgi-buffers-data-without-bound.patch
blob: a9df1744a7d72bd37b71dec664d453b4174fbcfc (plain)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
From e6ccbab5d42b110ac4f6ce1f72cb1e9ccbe4400a Mon Sep 17 00:00:00 2001
From: Li xin <lixin.fnst@cn.fujitsu.com>
Date: Tue, 16 Jun 2015 19:02:38 +0900
Subject: [PATCH] mod_cgi buffers data without bound so fix it

Upstream-Status: Submitted [http://redmine.lighttpd.net/issues/1264]

Signed-off-by: Li Xin <lixin.fnst@cn.fujitsu.com>

Update context for 1.4.36.

Signed-off-by: Kai Kang <kai.kang@windriver.com>
---
 doc/config/lighttpd.conf |   8 ++
 src/mod_cgi.c            | 188 ++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 187 insertions(+), 9 deletions(-)

diff --git a/doc/config/lighttpd.conf b/doc/config/lighttpd.conf
index 60b0ae1..9c101a7 100644
--- a/doc/config/lighttpd.conf
+++ b/doc/config/lighttpd.conf
@@ -375,6 +375,14 @@ server.upload-dirs = ( "/var/tmp" )
 ##
 #######################################################################
 
+#######################################################################
+##
+##
+## maximum bytes in send_raw before backing off [KByte]
+##  cgi.high-waterlevel        = 10240
+## minimum bytes in send_raw to disable backoff [KByte]
+##  cgi.low-waterlevel         = 5120
+#######################################################################
 
 #######################################################################
 ##
diff --git a/src/mod_cgi.c b/src/mod_cgi.c
index 01b1877..7c67eb5 100644
--- a/src/mod_cgi.c
+++ b/src/mod_cgi.c
@@ -38,6 +38,10 @@
 
 #include "version.h"
 
+/* for output logs */
+char msgbuf[2048];
+
+
 enum {EOL_UNSET, EOL_N, EOL_RN};
 
 typedef struct {
@@ -53,9 +57,19 @@ typedef struct {
 	size_t size;
 } buffer_pid_t;
 
+struct handler_ctx;
+
+typedef struct {
+	struct handler_ctx **hctx;
+	size_t used;
+	size_t size;
+} buffer_ctx_t;
+
 typedef struct {
 	array *cgi;
 	unsigned short execute_x_only;
+	unsigned int high_waterlevel; /* maximum bytes in send_raw before backing off */
+	unsigned int low_waterlevel;  /* minimum bytes in send_raw to disable backoff */
 } plugin_config;
 
 typedef struct {
@@ -68,9 +82,11 @@ typedef struct {
 	plugin_config **config_storage;
 
 	plugin_config conf;
+
+	buffer_ctx_t cgi_ctx;
 } plugin_data;
 
-typedef struct {
+typedef struct handler_ctx {
 	pid_t pid;
 	int fd;
 	int fde_ndx; /* index into the fd-event buffer */
@@ -78,11 +94,16 @@ typedef struct {
 	connection *remote_conn;  /* dumb pointer */
 	plugin_data *plugin_data; /* dumb pointer */
 
+	int throttling;        /* 1=waiting for send_raw buffer to drain */
+	off_t high_waterlevel; /* maximum bytes in send_raw before backing off */
+	off_t low_waterlevel;  /* minimum bytes in send_raw to disable backoff */
+	off_t bytes_in_buffer;
+
 	buffer *response;
 	buffer *response_header;
 } handler_ctx;
 
-static handler_ctx * cgi_handler_ctx_init(void) {
+static handler_ctx * cgi_handler_ctx_init(plugin_data *p) {
 	handler_ctx *hctx = calloc(1, sizeof(*hctx));
 
 	force_assert(hctx);
@@ -90,13 +111,26 @@ static handler_ctx * cgi_handler_ctx_init(void) {
 	hctx->response = buffer_init();
 	hctx->response_header = buffer_init();
 
+	hctx->throttling = 0;
+	hctx->high_waterlevel = (off_t)p->conf.high_waterlevel * 1024;
+	hctx->low_waterlevel  = (off_t)p->conf.low_waterlevel  * 1024;
+	if (hctx->low_waterlevel >= hctx->high_waterlevel) {
+	    hctx->low_waterlevel = hctx->high_waterlevel * 3 / 4; /* 75% */
+	}
+	hctx->bytes_in_buffer = 0;
+
 	return hctx;
 }
 
-static void cgi_handler_ctx_free(handler_ctx *hctx) {
+static void cgi_handler_ctx_free(server *srv, handler_ctx *hctx) {
 	buffer_free(hctx->response);
 	buffer_free(hctx->response_header);
 
+	/* to avoid confusion */
+	if (hctx->throttling) {
+	    log_error_write(srv, __FILE__, __LINE__, "s", "unthrottled");
+	}
+
 	free(hctx);
 }
 
@@ -154,6 +188,8 @@ SETDEFAULTS_FUNC(mod_fastcgi_set_defaults) {
 	config_values_t cv[] = {
 		{ "cgi.assign",                  NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION },       /* 0 */
 		{ "cgi.execute-x-only",          NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION },     /* 1 */
+		{ "cgi.high-waterlevel",         NULL, T_CONFIG_INT,   T_CONFIG_SCOPE_CONNECTION },       /* 2 */
+		{ "cgi.low-waterlevel",          NULL, T_CONFIG_INT,   T_CONFIG_SCOPE_CONNECTION },       /* 3 */
 		{ NULL,                          NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET}
 	};
 
@@ -169,9 +205,13 @@ SETDEFAULTS_FUNC(mod_fastcgi_set_defaults) {
 
 		s->cgi    = array_init();
 		s->execute_x_only = 0;
+		s->high_waterlevel = 0; /* 0 == disabled */
+		s->low_waterlevel  = 0;
 
 		cv[0].destination = s->cgi;
 		cv[1].destination = &(s->execute_x_only);
+		cv[2].destination = &(s->high_waterlevel);
+		cv[3].destination = &(s->low_waterlevel);
 
 		p->config_storage[i] = s;
 
@@ -184,6 +224,51 @@ SETDEFAULTS_FUNC(mod_fastcgi_set_defaults) {
 }
 
 
+static void cgi_recount_bytes_in_buffer(handler_ctx *hctx)
+{
+	chunkqueue *cq = hctx->remote_conn->write_queue;
+	hctx->bytes_in_buffer = chunkqueue_length(cq) - chunkqueue_written(cq);
+}
+
+
+static void cgi_throttling_control(server *srv, handler_ctx *hctx)
+{
+	cgi_recount_bytes_in_buffer(hctx);
+
+#ifdef DEBUG
+	sprintf(msgbuf, "throttling=%d, chars=%llu, high=%llu, low=%llu",
+		hctx->throttling, hctx->bytes_in_buffer,
+		hctx->high_waterlevel, hctx->low_waterlevel);
+	log_error_write(srv, __FILE__, __LINE__, "ss",
+			"(debug) throttling control,", msgbuf);
+#endif
+
+	if (hctx->throttling) {
+		sprintf(msgbuf, "throttling; chars in queue=%llu,"
+			" low-waterlevel=%llu, high-waterlevel=%llu",
+			hctx->bytes_in_buffer,
+			hctx->low_waterlevel, hctx->high_waterlevel);
+		log_error_write(srv, __FILE__, __LINE__, "s", msgbuf);
+		if (hctx->bytes_in_buffer <= hctx->low_waterlevel) {
+			fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
+			hctx->throttling = 0;
+			log_error_write(srv, __FILE__, __LINE__, "s", "unthrottled");
+		}
+	} else {
+		if (hctx->high_waterlevel != 0 &&
+			hctx->high_waterlevel <= hctx->bytes_in_buffer) {
+			fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
+			hctx->throttling = 1;
+			sprintf(msgbuf, "throttled; chars in queue=%llu,"
+				" low-waterlevel=%llu, high-waterlevel=%llu",
+				hctx->bytes_in_buffer,
+				hctx->low_waterlevel, hctx->high_waterlevel);
+			log_error_write(srv, __FILE__, __LINE__, "s", msgbuf);
+		}
+	}
+}
+
+
 static int cgi_pid_add(server *srv, plugin_data *p, pid_t pid) {
 	int m = -1;
 	size_t i;
@@ -230,6 +315,39 @@ static int cgi_pid_del(server *srv, plugin_data *p, pid_t pid) {
 	return 0;
 }
 
+
+static void cgi_ctx_add(plugin_data *p, handler_ctx *hctx) {
+	buffer_ctx_t *r = &(p->cgi_ctx);
+
+	if (r->size == 0) {
+		r->size = 16;
+		r->hctx = malloc(sizeof(*r->hctx) * r->size);
+	} else if (r->used == r->size) {
+		r->size += 16;
+		r->hctx = realloc(r->hctx, sizeof(*r->hctx) * r->size);
+	}
+
+	r->hctx[r->used++] = hctx;
+}
+
+static void cgi_ctx_del(plugin_data *p, handler_ctx *hctx) {
+	size_t i;
+	buffer_ctx_t *r = &(p->cgi_ctx);
+
+	for (i = 0; i < r->used; i++) {
+		if (r->hctx[i] == hctx) break;
+	}
+
+	if (i != r->used) {
+		/* found */
+
+		if (i != r->used - 1) {
+			r->hctx[i] = r->hctx[r->used - 1];
+		}
+		r->used--;
+	}
+}
+
 static int cgi_response_parse(server *srv, connection *con, plugin_data *p, buffer *in) {
 	char *ns;
 	const char *s;
@@ -380,6 +498,14 @@ static int cgi_demux_response(server *srv, handler_ctx *hctx) {
 
 		buffer_commit(hctx->response, n);
 
+#ifdef DEBUG
+		sprintf(msgbuf, "n=%d, bytes_out=%llu, bytes_in=%llu", n,
+			(unsigned long long)con->write_queue->bytes_out,
+			(unsigned long long)con->write_queue->bytes_in);
+		log_error_write(srv, __FILE__, __LINE__, "ss",
+				"(debug) read,", msgbuf);
+#endif
+
 		/* split header from body */
 
 		if (con->file_started == 0) {
@@ -503,7 +629,20 @@ static int cgi_demux_response(server *srv, handler_ctx *hctx) {
 			}
 		} else {
 			http_chunk_append_buffer(srv, con, hctx->response);
+#ifdef DEBUG
+			sprintf(msgbuf, "n=%d, bytes_out=%llu, bytes_in=%llu, limit=%llu", n,
+				(unsigned long long)con->write_queue->bytes_out,
+				(unsigned long long)con->write_queue->bytes_in,
+				(unsigned long long)hctx->high_waterlevel);
+			log_error_write(srv, __FILE__, __LINE__,
+					"ss", "(debug) append,", msgbuf);
+#endif
 			joblist_append(srv, con);
+
+			cgi_throttling_control(srv, hctx);
+			if (hctx->throttling) {
+				return FDEVENT_HANDLED_NOT_FINISHED;
+			}
 		}
 
 #if 0
@@ -553,8 +692,9 @@ static handler_t cgi_connection_close(server *srv, handler_ctx *hctx) {
 	con->plugin_ctx[p->id] = NULL;
 
 	/* is this a good idea ? */
-	cgi_handler_ctx_free(hctx);
-
+	cgi_ctx_del(p, hctx);
+	cgi_handler_ctx_free(srv, hctx);
+	
 	/* if waitpid hasn't been called by response.c yet, do it here */
 	if (pid) {
 		/* check if the CGI-script is already gone */
@@ -1105,7 +1245,8 @@ static int cgi_create_env(server *srv, connection *con, plugin_data *p, buffer *
 		con->mode = p->id;
 		buffer_reset(con->physical.path);
 
-		hctx = cgi_handler_ctx_init();
+		hctx = cgi_handler_ctx_init(p);
+		cgi_ctx_add(p, hctx);
 
 		hctx->remote_conn = con;
 		hctx->plugin_data = p;
@@ -1114,6 +1255,11 @@ static int cgi_create_env(server *srv, connection *con, plugin_data *p, buffer *
 		hctx->fde_ndx = -1;
 
 		con->plugin_ctx[p->id] = hctx;
+#ifdef DEBUG
+		sprintf(msgbuf, "hctx=%p, con=%p", (void*)hctx, (void*)con);
+		log_error_write(srv, __FILE__, __LINE__, "ss",
+				"(debug) hctx generated, ", msgbuf);
+#endif
 
 		fdevent_register(srv->ev, hctx->fd, cgi_handle_fdevent, hctx);
 		fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
@@ -1128,7 +1274,8 @@ static int cgi_create_env(server *srv, connection *con, plugin_data *p, buffer *
 
 			close(hctx->fd);
 
-			cgi_handler_ctx_free(hctx);
+			cgi_ctx_del(p, hctx);
+			cgi_handler_ctx_free(srv, hctx);
 
 			con->plugin_ctx[p->id] = NULL;
 
@@ -1153,6 +1300,8 @@ static int mod_cgi_patch_connection(server *srv, connection *con, plugin_data *p
 
 	PATCH(cgi);
 	PATCH(execute_x_only);
+	PATCH(high_waterlevel);
+	PATCH(low_waterlevel);
 
 	/* skip the first, the global context */
 	for (i = 1; i < srv->config_context->used; i++) {
@@ -1170,6 +1319,10 @@ static int mod_cgi_patch_connection(server *srv, connection *con, plugin_data *p
 				PATCH(cgi);
 			} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("cgi.execute-x-only"))) {
 				PATCH(execute_x_only);
+			} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("cgi.high-waterlevel"))) {
+				PATCH(high_waterlevel);
+			} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("cgi.low-waterlevel"))) {
+				PATCH(low_waterlevel);
 			}
 		}
 	}
@@ -1222,6 +1375,21 @@ URIHANDLER_FUNC(cgi_is_handled) {
 TRIGGER_FUNC(cgi_trigger) {
 	plugin_data *p = p_d;
 	size_t ndx;
+
+	for (ndx = 0; ndx < p->cgi_ctx.used; ndx++) {
+		handler_ctx *hctx = p->cgi_ctx.hctx[ndx];
+#ifdef DEBUG
+		connection *con = hctx->remote_conn;
+	
+		sprintf(msgbuf, "hctx=%p, con=%p, bytes_in_buffer=%llu",
+			(void*)hctx, (void*)con,
+			(unsigned long long)hctx->bytes_in_buffer);
+		log_error_write(srv, __FILE__, __LINE__, "ss",
+				"(debug) found using ctx,", msgbuf);
+#endif
+		cgi_throttling_control(srv, hctx);
+	}
+
 	/* the trigger handle only cares about lonely PID which we have to wait for */
 #ifndef __WIN32
 
@@ -1330,7 +1498,8 @@ SUBREQUEST_FUNC(mod_cgi_handle_subrequest) {
 			log_error_write(srv, __FILE__, __LINE__, "sds", "cgi close failed ", hctx->fd, strerror(errno));
 		}
 
-		cgi_handler_ctx_free(hctx);
+		cgi_ctx_del(p, hctx);
+		cgi_handler_ctx_free(srv, hctx);
 
 		con->plugin_ctx[p->id] = NULL;
 
@@ -1362,7 +1531,8 @@ SUBREQUEST_FUNC(mod_cgi_handle_subrequest) {
 			log_error_write(srv, __FILE__, __LINE__, "sds", "cgi close failed ", hctx->fd, strerror(errno));
 		}
 
-		cgi_handler_ctx_free(hctx);
+		cgi_ctx_del(p, hctx);
+		cgi_handler_ctx_free(srv, hctx);
 
 		con->plugin_ctx[p->id] = NULL;
 		return HANDLER_FINISHED;