Skip to content

Commit b2d0e6f

Browse files
committed
Request: new request session flag to mark those files opened by FDT
This patch aims to fix a potential DDoS problem that can be caused in the server quering repetitive non-existent resources. When serving a static file, the core use Vhost FDT mechanism, but if it sends a static error page it does a direct open(2). When closing the resources for the same request it was just calling mk_vhost_close() which did not clear properly the file descriptor. This patch adds a new field on the struct session_request called 'fd_is_fdt', which contains MK_TRUE or MK_FALSE depending of how fd_file was opened. Thanks to Matthew Daley <mattd@bugfuzz.com> for report and troubleshoot this problem. Signed-off-by: Eduardo Silva <eduardo@monkey.io>
1 parent f9d0f8d commit b2d0e6f

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

Diff for: include/monkey/mk_request.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,18 @@ struct session_request
159159
long port;
160160
/*------------*/
161161

162-
/* file descriptors */
162+
/*
163+
* Static file file descriptor: the following twp fields represents an
164+
* opened file in the file system and a flag saying which mechanism
165+
* was used to open it.
166+
*
167+
* - fd_file : common file descriptor
168+
* - fd_is_fdt: set to MK_TRUE if fd_file was opened using Vhost FDT, or
169+
* MK_FALSE for the opposite case.
170+
*/
163171
int fd_file;
172+
int fd_is_fdt;
173+
164174

165175
int headers_len;
166176

Diff for: src/mk_request.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ static inline void mk_request_init(struct session_request *request)
9999
void mk_request_free(struct session_request *sr)
100100
{
101101
if (sr->fd_file > 0) {
102-
mk_vhost_close(sr);
102+
if (sr->fd_is_fdt == MK_TRUE) {
103+
mk_vhost_close(sr);
104+
}
105+
else {
106+
close(sr->fd_file);
107+
}
103108
}
104109

105110
if (sr->headers.location) {
@@ -841,7 +846,8 @@ int mk_request_error(int http_status, struct client_session *cs,
841846
break;
842847
}
843848

844-
sr->fd_file = fd;
849+
sr->fd_file = fd;
850+
sr->fd_is_fdt = MK_FALSE;
845851
sr->bytes_to_send = finfo.size;
846852
sr->headers.content_length = finfo.size;
847853
sr->headers.real_length = finfo.size;

Diff for: src/mk_vhost.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ static inline int mk_vhost_fdt_open(int id, unsigned int hash,
220220

221221
sr->vhost_fdt_id = id;
222222
sr->vhost_fdt_hash = hash;
223+
sr->fd_is_fdt = MK_TRUE;
223224

224225
return fd;
225226
}
@@ -262,7 +263,6 @@ static inline int mk_vhost_fdt_close(struct session_request *sr)
262263
return 0;
263264
}
264265
}
265-
266266
return close(sr->fd_file);
267267
}
268268

0 commit comments

Comments
 (0)