~ps10gel/ubuntu/xenial/nginx/add-with-http_slice_module

« back to all changes in this revision

Viewing changes to debian/modules/nginx-lua/t/099-c-api.t

  • Committer: ps10gel at gmail
  • Author(s): Christos Trochalakis, Christos Trochalakis
  • Date: 2016-05-31 19:01:25 UTC
  • Revision ID: ps10gel@gmail.com-20160531190125-o9xy03d29nhzti33
Tags: 1.10.1-1
[ Christos Trochalakis ]
* New upstream release (1.10.1)
  Fixes CVE-2016-4450
  NULL pointer dereference while writing client request body.
  (Closes: #825960)

* debian/control:
  + Generate dbgsym packages only if dh_strip supports it.
    Thanks Faidon Liambotis for the hint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim:set ft= ts=4 sw=4 et fdm=marker:
 
2
use Test::Nginx::Socket::Lua;
 
3
 
 
4
#worker_connections(1014);
 
5
#master_process_enabled(1);
 
6
log_level('warn');
 
7
 
 
8
repeat_each(3);
 
9
 
 
10
plan tests => repeat_each() * (blocks() * 3);
 
11
 
 
12
#no_diff();
 
13
no_long_string();
 
14
#master_on();
 
15
#workers(2);
 
16
 
 
17
run_tests();
 
18
 
 
19
__DATA__
 
20
 
 
21
=== TEST 1: find zone
 
22
--- http_config
 
23
    lua_shared_dict dogs 1m;
 
24
--- config
 
25
    location = /test {
 
26
        content_by_lua '
 
27
            local ffi = require "ffi"
 
28
 
 
29
            ffi.cdef[[
 
30
                void *ngx_http_lua_find_zone(char *data, size_t len);
 
31
            ]]
 
32
 
 
33
            local buf = ffi.new("char[?]", 4)
 
34
            ffi.copy(buf, "foo", 3)
 
35
            local zone = ffi.C.ngx_http_lua_find_zone(buf, 3)
 
36
            ngx.say("foo zone: ", tonumber(ffi.cast("long", zone)) ~= 0 and "defined" or "undef")
 
37
 
 
38
            ffi.copy(buf, "dogs", 4)
 
39
            zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
 
40
            ngx.say("dogs zone: ", tonumber(ffi.cast("long", zone)) ~= 0 and "defined" or "undef")
 
41
        ';
 
42
    }
 
43
--- request
 
44
GET /test
 
45
--- response_body
 
46
foo zone: undef
 
47
dogs zone: defined
 
48
--- no_error_log
 
49
[error]
 
50
 
 
51
 
 
52
 
 
53
=== TEST 2: number typed value
 
54
--- http_config
 
55
    lua_shared_dict dogs 1m;
 
56
--- config
 
57
    location = /test {
 
58
        content_by_lua '
 
59
            local ffi = require "ffi"
 
60
 
 
61
            ffi.cdef[[
 
62
                typedef struct {
 
63
                    size_t  len;
 
64
                    char   *data;
 
65
                } ngx_str_t;
 
66
 
 
67
                typedef struct {
 
68
                    uint8_t         type;
 
69
 
 
70
                    union {
 
71
                        int         b; /* boolean */
 
72
                        double      n; /* number */
 
73
                        ngx_str_t   s; /* string */
 
74
                    } value;
 
75
 
 
76
                } ngx_http_lua_value_t;
 
77
 
 
78
                void *ngx_http_lua_find_zone(char *data, size_t len);
 
79
                intptr_t ngx_http_lua_shared_dict_get(void *zone, char *kdata, size_t klen, ngx_http_lua_value_t *val);
 
80
            ]]
 
81
 
 
82
            local dogs = ngx.shared.dogs
 
83
            dogs:set("foo", 1234567)
 
84
            dogs:set("bar", 3.14159)
 
85
 
 
86
            local buf = ffi.new("char[?]", 4)
 
87
 
 
88
            ffi.copy(buf, "dogs", 4)
 
89
            zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
 
90
 
 
91
            local val = ffi.new("ngx_http_lua_value_t[?]", 1)
 
92
 
 
93
            ffi.copy(buf, "foo", 3)
 
94
            local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
 
95
            ngx.say("foo: rc=", tonumber(rc),
 
96
                ", type=", val[0].type,
 
97
                ", val=", tonumber(val[0].value.n))
 
98
 
 
99
            ffi.copy(buf, "bar", 3)
 
100
            local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
 
101
            ngx.say("bar: rc=", tonumber(rc),
 
102
                ", type=", val[0].type,
 
103
                ", val=", tonumber(val[0].value.n))
 
104
        ';
 
105
    }
 
106
--- request
 
107
GET /test
 
108
--- response_body
 
109
foo: rc=0, type=3, val=1234567
 
110
bar: rc=0, type=3, val=3.14159
 
111
--- no_error_log
 
112
[error]
 
113
 
 
114
 
 
115
 
 
116
=== TEST 3: boolean typed value
 
117
--- http_config
 
118
    lua_shared_dict dogs 1m;
 
119
--- config
 
120
    location = /test {
 
121
        content_by_lua '
 
122
            local ffi = require "ffi"
 
123
 
 
124
            ffi.cdef[[
 
125
                typedef struct {
 
126
                    size_t  len;
 
127
                    char   *data;
 
128
                } ngx_str_t;
 
129
 
 
130
                typedef struct {
 
131
                    uint8_t         type;
 
132
 
 
133
                    union {
 
134
                        int         b; /* boolean */
 
135
                        double      n; /* number */
 
136
                        ngx_str_t   s; /* string */
 
137
                    } value;
 
138
 
 
139
                } ngx_http_lua_value_t;
 
140
 
 
141
                void *ngx_http_lua_find_zone(char *data, size_t len);
 
142
                intptr_t ngx_http_lua_shared_dict_get(void *zone, char *kdata, size_t klen, ngx_http_lua_value_t *val);
 
143
            ]]
 
144
 
 
145
            local dogs = ngx.shared.dogs
 
146
            dogs:set("foo", true)
 
147
            dogs:set("bar", false)
 
148
 
 
149
            local buf = ffi.new("char[?]", 4)
 
150
 
 
151
            ffi.copy(buf, "dogs", 4)
 
152
            zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
 
153
 
 
154
            local val = ffi.new("ngx_http_lua_value_t[?]", 1)
 
155
 
 
156
            ffi.copy(buf, "foo", 3)
 
157
            local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
 
158
            ngx.say("foo: rc=", tonumber(rc),
 
159
                ", type=", tonumber(val[0].type),
 
160
                ", val=", tonumber(val[0].value.b))
 
161
 
 
162
            local val = ffi.new("ngx_http_lua_value_t[?]", 1)
 
163
            ffi.copy(buf, "bar", 3)
 
164
            local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
 
165
            ngx.say("bar: rc=", tonumber(rc),
 
166
                ", type=", tonumber(val[0].type),
 
167
                ", val=", tonumber(val[0].value.b))
 
168
        ';
 
169
    }
 
170
--- request
 
171
GET /test
 
172
--- response_body
 
173
foo: rc=0, type=1, val=1
 
174
bar: rc=0, type=1, val=0
 
175
--- no_error_log
 
176
[error]
 
177
 
 
178
 
 
179
 
 
180
=== TEST 4: key not found
 
181
--- http_config
 
182
    lua_shared_dict dogs 1m;
 
183
--- config
 
184
    location = /test {
 
185
        content_by_lua '
 
186
            local ffi = require "ffi"
 
187
 
 
188
            ffi.cdef[[
 
189
                typedef struct {
 
190
                    size_t  len;
 
191
                    char   *data;
 
192
                } ngx_str_t;
 
193
 
 
194
                typedef struct {
 
195
                    uint8_t         type;
 
196
 
 
197
                    union {
 
198
                        int         b; /* boolean */
 
199
                        double      n; /* number */
 
200
                        ngx_str_t   s; /* string */
 
201
                    } value;
 
202
 
 
203
                } ngx_http_lua_value_t;
 
204
 
 
205
                void *ngx_http_lua_find_zone(char *data, size_t len);
 
206
                intptr_t ngx_http_lua_shared_dict_get(void *zone, char *kdata, size_t klen, ngx_http_lua_value_t *val);
 
207
            ]]
 
208
 
 
209
            local dogs = ngx.shared.dogs
 
210
            dogs:flush_all()
 
211
 
 
212
            local buf = ffi.new("char[?]", 4)
 
213
 
 
214
            ffi.copy(buf, "dogs", 4)
 
215
            zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
 
216
 
 
217
            local val = ffi.new("ngx_http_lua_value_t[?]", 1)
 
218
 
 
219
            ffi.copy(buf, "foo", 3)
 
220
            local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
 
221
            ngx.say("foo: rc=", tonumber(rc))
 
222
 
 
223
            local val = ffi.new("ngx_http_lua_value_t[?]", 1)
 
224
            ffi.copy(buf, "bar", 3)
 
225
            local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
 
226
            ngx.say("bar: rc=", tonumber(rc))
 
227
        ';
 
228
    }
 
229
--- request
 
230
GET /test
 
231
--- response_body
 
232
foo: rc=-5
 
233
bar: rc=-5
 
234
--- no_error_log
 
235
[error]
 
236
 
 
237
 
 
238
 
 
239
=== TEST 5: string typed value
 
240
--- http_config
 
241
    lua_shared_dict dogs 1m;
 
242
--- config
 
243
    location = /test {
 
244
        content_by_lua '
 
245
            local ffi = require "ffi"
 
246
 
 
247
            ffi.cdef[[
 
248
                typedef struct {
 
249
                    size_t  len;
 
250
                    char   *data;
 
251
                } ngx_str_t;
 
252
 
 
253
                typedef struct {
 
254
                    uint8_t         type;
 
255
 
 
256
                    union {
 
257
                        int         b; /* boolean */
 
258
                        double      n; /* number */
 
259
                        ngx_str_t   s; /* string */
 
260
                    } value;
 
261
 
 
262
                } ngx_http_lua_value_t;
 
263
 
 
264
                void *ngx_http_lua_find_zone(char *data, size_t len);
 
265
                intptr_t ngx_http_lua_shared_dict_get(void *zone, char *kdata, size_t klen, ngx_http_lua_value_t *val);
 
266
            ]]
 
267
 
 
268
            local dogs = ngx.shared.dogs
 
269
            dogs:set("foo", "hello world")
 
270
            dogs:set("bar", "")
 
271
 
 
272
            local buf = ffi.new("char[?]", 4)
 
273
 
 
274
            ffi.copy(buf, "dogs", 4)
 
275
            zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
 
276
 
 
277
            local s = ffi.new("char[?]", 20)
 
278
 
 
279
            local val = ffi.new("ngx_http_lua_value_t[?]", 1)
 
280
            val[0].value.s.len = 20
 
281
            val[0].value.s.data = s
 
282
 
 
283
            ffi.copy(buf, "foo", 3)
 
284
            local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
 
285
            ngx.say("foo: rc=", tonumber(rc),
 
286
                ", type=", tonumber(val[0].type),
 
287
                ", val=", ffi.string(val[0].value.s.data, val[0].value.s.len),
 
288
                ", len=", tonumber(val[0].value.s.len))
 
289
 
 
290
            local val = ffi.new("ngx_http_lua_value_t[?]", 1)
 
291
            val[0].value.s.len = 20
 
292
            val[0].value.s.data = s
 
293
 
 
294
            ffi.copy(buf, "bar", 3)
 
295
            local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
 
296
            ngx.say("bar: rc=", tonumber(rc),
 
297
                ", type=", tonumber(val[0].type),
 
298
                ", val=", ffi.string(val[0].value.s.data, val[0].value.s.len),
 
299
                ", len=", tonumber(val[0].value.s.len))
 
300
        ';
 
301
    }
 
302
--- request
 
303
GET /test
 
304
--- response_body
 
305
foo: rc=0, type=4, val=hello world, len=11
 
306
bar: rc=0, type=4, val=, len=0
 
307
--- no_error_log
 
308
[error]
 
309
 
 
310
 
 
311
 
 
312
=== TEST 6: nil typed value
 
313
--- http_config
 
314
    lua_shared_dict dogs 1m;
 
315
--- config
 
316
    location = /test {
 
317
        content_by_lua '
 
318
            local ffi = require "ffi"
 
319
 
 
320
            ffi.cdef[[
 
321
                typedef struct {
 
322
                    size_t  len;
 
323
                    char   *data;
 
324
                } ngx_str_t;
 
325
 
 
326
                typedef struct {
 
327
                    uint8_t         type;
 
328
 
 
329
                    union {
 
330
                        int         b; /* boolean */
 
331
                        double      n; /* number */
 
332
                        ngx_str_t   s; /* string */
 
333
                    } value;
 
334
 
 
335
                } ngx_http_lua_value_t;
 
336
 
 
337
                void *ngx_http_lua_find_zone(char *data, size_t len);
 
338
                intptr_t ngx_http_lua_shared_dict_get(void *zone, char *kdata, size_t klen, ngx_http_lua_value_t *val);
 
339
            ]]
 
340
 
 
341
            local dogs = ngx.shared.dogs
 
342
            dogs:set("foo", nil)
 
343
 
 
344
            local buf = ffi.new("char[?]", 4)
 
345
 
 
346
            ffi.copy(buf, "dogs", 4)
 
347
            zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
 
348
 
 
349
            local val = ffi.new("ngx_http_lua_value_t[?]", 1)
 
350
 
 
351
            ffi.copy(buf, "foo", 3)
 
352
            local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
 
353
            ngx.say("foo: rc=", tonumber(rc))
 
354
        ';
 
355
    }
 
356
--- request
 
357
GET /test
 
358
--- response_body
 
359
foo: rc=-5
 
360
--- no_error_log
 
361
[error]
 
362
 
 
363
 
 
364
 
 
365
=== TEST 7: find zone (multiple zones)
 
366
--- http_config
 
367
    lua_shared_dict dogs 1m;
 
368
    lua_shared_dict cats 1m;
 
369
--- config
 
370
    location = /test {
 
371
        content_by_lua '
 
372
            local ffi = require "ffi"
 
373
 
 
374
            ffi.cdef[[
 
375
                void *ngx_http_lua_find_zone(char *data, size_t len);
 
376
            ]]
 
377
 
 
378
            local buf = ffi.new("char[?]", 4)
 
379
            ffi.copy(buf, "cats", 4)
 
380
            local zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
 
381
            local cats = tostring(zone)
 
382
 
 
383
            ffi.copy(buf, "dogs", 4)
 
384
            zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
 
385
            local dogs = tostring(zone)
 
386
 
 
387
            ngx.say("dogs == cats ? ", dogs == cats)
 
388
            -- ngx.say("dogs: ", dogs)
 
389
            -- ngx.say("cats ", cats)
 
390
        ';
 
391
    }
 
392
--- request
 
393
GET /test
 
394
--- response_body
 
395
dogs == cats ? false
 
396
--- no_error_log
 
397
[error]