Mercurial > hg > nginx
annotate src/core/ngx_crc32.h @ 6359:dac6eda40475 stable-1.8
Resolver: fixed use-after-free memory accesses with CNAME.
When several requests were waiting for a response, then after getting
a CNAME response only the last request's context had the name updated.
Contexts of other requests had the wrong name. This name was used by
ngx_resolve_name_done() to find the node to remove the request context
from. When the name was wrong, the request could not be properly
cancelled, its context was freed but stayed linked to the node's waiting
list. This happened e.g. when the first request was aborted or timed
out before the resolving completed. When it completed, this triggered
a use-after-free memory access by calling ctx->handler of already freed
request context. The bug manifests itself by
"could not cancel <name> resolving" alerts in error_log.
When a request was responded with a CNAME, the request context kept
the pointer to the original node's rn->u.cname. If the original node
expired before the resolving timed out or completed with an error,
this would trigger a use-after-free memory access via ctx->name in
ctx->handler().
The fix is to keep ctx->name unmodified. The name from context
is no longer used by ngx_resolve_name_done(). Instead, we now keep
the pointer to resolver node to which this request is linked.
Keeping the original name intact also improves logging.
author | Roman Arutyunyan <arut@nginx.com> |
---|---|
date | Tue, 26 Jan 2016 16:46:59 +0300 |
parents | d620f497c50f |
children |
rev | line source |
---|---|
790 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4412 | 4 * Copyright (C) Nginx, Inc. |
790 | 5 */ |
6 | |
7 | |
8 #ifndef _NGX_CRC32_H_INCLUDED_ | |
9 #define _NGX_CRC32_H_INCLUDED_ | |
10 | |
11 | |
12 #include <ngx_config.h> | |
13 #include <ngx_core.h> | |
14 | |
15 | |
793
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
16 extern uint32_t *ngx_crc32_table_short; |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
17 extern uint32_t ngx_crc32_table256[]; |
790 | 18 |
19 | |
20 static ngx_inline uint32_t | |
793
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
21 ngx_crc32_short(u_char *p, size_t len) |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
22 { |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
23 u_char c; |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
24 uint32_t crc; |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
25 |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
26 crc = 0xffffffff; |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
27 |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
28 while (len--) { |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
29 c = *p++; |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
30 crc = ngx_crc32_table_short[(crc ^ (c & 0xf)) & 0xf] ^ (crc >> 4); |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
31 crc = ngx_crc32_table_short[(crc ^ (c >> 4)) & 0xf] ^ (crc >> 4); |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
32 } |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
33 |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
34 return crc ^ 0xffffffff; |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
35 } |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
36 |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
37 |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
38 static ngx_inline uint32_t |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
39 ngx_crc32_long(u_char *p, size_t len) |
790 | 40 { |
41 uint32_t crc; | |
42 | |
43 crc = 0xffffffff; | |
44 | |
45 while (len--) { | |
793
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
46 crc = ngx_crc32_table256[(crc ^ *p++) & 0xff] ^ (crc >> 8); |
790 | 47 } |
48 | |
49 return crc ^ 0xffffffff; | |
50 } | |
51 | |
52 | |
1694
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
53 #define ngx_crc32_init(crc) \ |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
54 crc = 0xffffffff |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
55 |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
56 |
1697 | 57 static ngx_inline void |
1694
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
58 ngx_crc32_update(uint32_t *crc, u_char *p, size_t len) |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
59 { |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
60 uint32_t c; |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
61 |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
62 c = *crc; |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
63 |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
64 while (len--) { |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
65 c = ngx_crc32_table256[(c ^ *p++) & 0xff] ^ (c >> 8); |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
66 } |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
67 |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
68 *crc = c; |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
69 } |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
70 |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
71 |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
72 #define ngx_crc32_final(crc) \ |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
73 crc ^= 0xffffffff |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
74 |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
75 |
8c696afe46b3
rename ngx_crc32_init() to ngx_crc32_table_init()
Igor Sysoev <igor@sysoev.ru>
parents:
930
diff
changeset
|
76 ngx_int_t ngx_crc32_table_init(void); |
793
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
77 |
8d39da951bbd
split ngx_crc32() to short and long version
Igor Sysoev <igor@sysoev.ru>
parents:
790
diff
changeset
|
78 |
790 | 79 #endif /* _NGX_CRC32_H_INCLUDED_ */ |