Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : SMB2 read test suite
5 :
6 : Copyright (C) Andrew Tridgell 2008
7 :
8 : This program is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3 of the License, or
11 : (at your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program. If not, see <http://www.gnu.org/licenses/>.
20 : */
21 :
22 : #include "includes.h"
23 : #include "libcli/smb2/smb2.h"
24 : #include "libcli/smb2/smb2_calls.h"
25 : #include <tevent.h>
26 :
27 : #include "torture/torture.h"
28 : #include "torture/smb2/proto.h"
29 : #include "../libcli/smb/smbXcli_base.h"
30 : #include "librpc/gen_ndr/ndr_ioctl.h"
31 :
32 :
33 : #define CHECK_STATUS(_status, _expected) \
34 : torture_assert_ntstatus_equal_goto(torture, _status, _expected, \
35 : ret, done, "Incorrect status")
36 :
37 : #define CHECK_VALUE(v, correct) \
38 : torture_assert_int_equal_goto(torture, v, correct, \
39 : ret, done, "Incorrect value")
40 :
41 : #define FNAME "smb2_readtest.dat"
42 : #define DNAME "smb2_readtest.dir"
43 :
44 1 : static bool test_read_eof(struct torture_context *torture, struct smb2_tree *tree)
45 : {
46 1 : bool ret = true;
47 : NTSTATUS status;
48 : struct smb2_handle h;
49 : uint8_t buf[64*1024];
50 : struct smb2_read rd;
51 1 : TALLOC_CTX *tmp_ctx = talloc_new(tree);
52 :
53 1 : ZERO_STRUCT(buf);
54 :
55 1 : smb2_util_unlink(tree, FNAME);
56 :
57 1 : status = torture_smb2_testfile(tree, FNAME, &h);
58 1 : CHECK_STATUS(status, NT_STATUS_OK);
59 :
60 1 : ZERO_STRUCT(rd);
61 1 : rd.in.file.handle = h;
62 1 : rd.in.length = 5;
63 1 : rd.in.offset = 0;
64 1 : status = smb2_read(tree, tree, &rd);
65 1 : CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
66 :
67 1 : status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
68 1 : CHECK_STATUS(status, NT_STATUS_OK);
69 :
70 1 : ZERO_STRUCT(rd);
71 1 : rd.in.file.handle = h;
72 1 : rd.in.length = 10;
73 1 : rd.in.offset = 0;
74 1 : rd.in.min_count = 1;
75 :
76 1 : status = smb2_read(tree, tmp_ctx, &rd);
77 1 : CHECK_STATUS(status, NT_STATUS_OK);
78 1 : CHECK_VALUE(rd.out.data.length, 10);
79 :
80 1 : rd.in.min_count = 0;
81 1 : rd.in.length = 10;
82 1 : rd.in.offset = sizeof(buf);
83 1 : status = smb2_read(tree, tmp_ctx, &rd);
84 1 : CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
85 :
86 1 : rd.in.min_count = 0;
87 1 : rd.in.length = 0;
88 1 : rd.in.offset = sizeof(buf);
89 1 : status = smb2_read(tree, tmp_ctx, &rd);
90 1 : CHECK_STATUS(status, NT_STATUS_OK);
91 1 : CHECK_VALUE(rd.out.data.length, 0);
92 :
93 1 : rd.in.min_count = 1;
94 1 : rd.in.length = 0;
95 1 : rd.in.offset = sizeof(buf);
96 1 : status = smb2_read(tree, tmp_ctx, &rd);
97 1 : CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
98 :
99 1 : rd.in.min_count = 0;
100 1 : rd.in.length = 2;
101 1 : rd.in.offset = sizeof(buf) - 1;
102 1 : status = smb2_read(tree, tmp_ctx, &rd);
103 1 : CHECK_STATUS(status, NT_STATUS_OK);
104 1 : CHECK_VALUE(rd.out.data.length, 1);
105 :
106 1 : rd.in.min_count = 2;
107 1 : rd.in.length = 1;
108 1 : rd.in.offset = sizeof(buf) - 1;
109 1 : status = smb2_read(tree, tmp_ctx, &rd);
110 1 : CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
111 :
112 1 : rd.in.min_count = 0x10000;
113 1 : rd.in.length = 1;
114 1 : rd.in.offset = 0;
115 1 : status = smb2_read(tree, tmp_ctx, &rd);
116 1 : CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
117 :
118 1 : rd.in.min_count = 0x10000 - 2;
119 1 : rd.in.length = 1;
120 1 : rd.in.offset = 0;
121 1 : status = smb2_read(tree, tmp_ctx, &rd);
122 1 : CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
123 :
124 1 : rd.in.min_count = 10;
125 1 : rd.in.length = 5;
126 1 : rd.in.offset = 0;
127 1 : status = smb2_read(tree, tmp_ctx, &rd);
128 1 : CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
129 :
130 1 : done:
131 1 : talloc_free(tmp_ctx);
132 1 : return ret;
133 : }
134 :
135 :
136 1 : static bool test_read_position(struct torture_context *torture, struct smb2_tree *tree)
137 : {
138 1 : bool ret = true;
139 : NTSTATUS status;
140 : struct smb2_handle h;
141 : uint8_t buf[64*1024];
142 : struct smb2_read rd;
143 1 : TALLOC_CTX *tmp_ctx = talloc_new(tree);
144 : union smb_fileinfo info;
145 :
146 1 : ZERO_STRUCT(buf);
147 :
148 1 : smb2_util_unlink(tree, FNAME);
149 :
150 1 : status = torture_smb2_testfile(tree, FNAME, &h);
151 1 : CHECK_STATUS(status, NT_STATUS_OK);
152 :
153 1 : status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
154 1 : CHECK_STATUS(status, NT_STATUS_OK);
155 :
156 1 : ZERO_STRUCT(rd);
157 1 : rd.in.file.handle = h;
158 1 : rd.in.length = 10;
159 1 : rd.in.offset = 0;
160 1 : rd.in.min_count = 1;
161 :
162 1 : status = smb2_read(tree, tmp_ctx, &rd);
163 1 : CHECK_STATUS(status, NT_STATUS_OK);
164 1 : CHECK_VALUE(rd.out.data.length, 10);
165 :
166 1 : info.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION;
167 1 : info.generic.in.file.handle = h;
168 :
169 1 : status = smb2_getinfo_file(tree, tmp_ctx, &info);
170 1 : CHECK_STATUS(status, NT_STATUS_OK);
171 1 : if (torture_setting_bool(torture, "windows", false)) {
172 0 : CHECK_VALUE(info.all_info2.out.position, 0);
173 : } else {
174 1 : CHECK_VALUE(info.all_info2.out.position, 10);
175 : }
176 :
177 :
178 2 : done:
179 1 : talloc_free(tmp_ctx);
180 1 : return ret;
181 : }
182 :
183 1 : static bool test_read_dir(struct torture_context *torture, struct smb2_tree *tree)
184 : {
185 1 : bool ret = true;
186 : NTSTATUS status;
187 : struct smb2_handle h;
188 : struct smb2_read rd;
189 1 : TALLOC_CTX *tmp_ctx = talloc_new(tree);
190 :
191 1 : status = torture_smb2_testdir(tree, DNAME, &h);
192 1 : if (!NT_STATUS_IS_OK(status)) {
193 0 : printf(__location__ " Unable to create test directory '%s' - %s\n", DNAME, nt_errstr(status));
194 0 : return false;
195 : }
196 :
197 1 : ZERO_STRUCT(rd);
198 1 : rd.in.file.handle = h;
199 1 : rd.in.length = 10;
200 1 : rd.in.offset = 0;
201 1 : rd.in.min_count = 1;
202 :
203 1 : status = smb2_read(tree, tmp_ctx, &rd);
204 1 : CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
205 :
206 1 : rd.in.min_count = 11;
207 1 : status = smb2_read(tree, tmp_ctx, &rd);
208 1 : CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
209 :
210 1 : rd.in.length = 0;
211 1 : rd.in.min_count = 2592;
212 1 : status = smb2_read(tree, tmp_ctx, &rd);
213 1 : if (torture_setting_bool(torture, "windows", false)) {
214 0 : CHECK_STATUS(status, NT_STATUS_END_OF_FILE);
215 : } else {
216 1 : CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
217 : }
218 :
219 1 : rd.in.length = 0;
220 1 : rd.in.min_count = 0;
221 1 : rd.in.channel = 0;
222 1 : status = smb2_read(tree, tmp_ctx, &rd);
223 1 : if (torture_setting_bool(torture, "windows", false)) {
224 0 : CHECK_STATUS(status, NT_STATUS_OK);
225 : } else {
226 1 : CHECK_STATUS(status, NT_STATUS_INVALID_DEVICE_REQUEST);
227 : }
228 :
229 1 : done:
230 1 : talloc_free(tmp_ctx);
231 1 : return ret;
232 : }
233 :
234 1 : static bool test_read_access(struct torture_context *torture,
235 : struct smb2_tree *tree)
236 : {
237 1 : bool ret = true;
238 : NTSTATUS status;
239 : struct smb2_handle h;
240 : uint8_t buf[64 * 1024];
241 : struct smb2_read rd;
242 1 : TALLOC_CTX *tmp_ctx = talloc_new(tree);
243 :
244 1 : ZERO_STRUCT(buf);
245 :
246 : /* create a file */
247 1 : smb2_util_unlink(tree, FNAME);
248 :
249 1 : status = torture_smb2_testfile(tree, FNAME, &h);
250 1 : CHECK_STATUS(status, NT_STATUS_OK);
251 :
252 1 : status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
253 1 : CHECK_STATUS(status, NT_STATUS_OK);
254 :
255 1 : status = smb2_util_close(tree, h);
256 1 : CHECK_STATUS(status, NT_STATUS_OK);
257 :
258 : /* open w/ READ access - success */
259 1 : status = torture_smb2_testfile_access(
260 : tree, FNAME, &h, SEC_FILE_READ_ATTRIBUTE | SEC_FILE_READ_DATA);
261 1 : CHECK_STATUS(status, NT_STATUS_OK);
262 :
263 1 : ZERO_STRUCT(rd);
264 1 : rd.in.file.handle = h;
265 1 : rd.in.length = 5;
266 1 : rd.in.offset = 0;
267 1 : status = smb2_read(tree, tree, &rd);
268 1 : CHECK_STATUS(status, NT_STATUS_OK);
269 :
270 1 : status = smb2_util_close(tree, h);
271 1 : CHECK_STATUS(status, NT_STATUS_OK);
272 :
273 : /* open w/ EXECUTE access - success */
274 1 : status = torture_smb2_testfile_access(
275 : tree, FNAME, &h, SEC_FILE_READ_ATTRIBUTE | SEC_FILE_EXECUTE);
276 1 : CHECK_STATUS(status, NT_STATUS_OK);
277 :
278 1 : ZERO_STRUCT(rd);
279 1 : rd.in.file.handle = h;
280 1 : rd.in.length = 5;
281 1 : rd.in.offset = 0;
282 1 : status = smb2_read(tree, tree, &rd);
283 1 : CHECK_STATUS(status, NT_STATUS_OK);
284 :
285 0 : status = smb2_util_close(tree, h);
286 0 : CHECK_STATUS(status, NT_STATUS_OK);
287 :
288 : /* open without READ or EXECUTE access - access denied */
289 0 : status = torture_smb2_testfile_access(tree, FNAME, &h,
290 : SEC_FILE_READ_ATTRIBUTE);
291 0 : CHECK_STATUS(status, NT_STATUS_OK);
292 :
293 0 : ZERO_STRUCT(rd);
294 0 : rd.in.file.handle = h;
295 0 : rd.in.length = 5;
296 0 : rd.in.offset = 0;
297 0 : status = smb2_read(tree, tree, &rd);
298 0 : CHECK_STATUS(status, NT_STATUS_ACCESS_DENIED);
299 :
300 0 : status = smb2_util_close(tree, h);
301 0 : CHECK_STATUS(status, NT_STATUS_OK);
302 :
303 1 : done:
304 1 : talloc_free(tmp_ctx);
305 1 : return ret;
306 : }
307 :
308 : /*
309 : basic regression test for BUG 14607
310 : https://bugzilla.samba.org/show_bug.cgi?id=14607
311 : */
312 1 : static bool test_read_bug14607(struct torture_context *torture,
313 : struct smb2_tree *tree)
314 : {
315 1 : bool ret = true;
316 : NTSTATUS status;
317 : struct smb2_handle h;
318 : uint8_t buf[64 * 1024];
319 : struct smb2_read rd;
320 : uint32_t timeout_msec;
321 1 : DATA_BLOB out_input_buffer = data_blob_null;
322 1 : DATA_BLOB out_output_buffer = data_blob_null;
323 1 : TALLOC_CTX *tmp_ctx = talloc_new(tree);
324 1 : uint8_t *data = NULL;
325 1 : uint32_t data_length = 0;
326 :
327 1 : memset(buf, 0x1f, ARRAY_SIZE(buf));
328 :
329 : /* create a file */
330 1 : smb2_util_unlink(tree, FNAME);
331 :
332 1 : status = torture_smb2_testfile(tree, FNAME, &h);
333 1 : CHECK_STATUS(status, NT_STATUS_OK);
334 :
335 1 : status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
336 1 : CHECK_STATUS(status, NT_STATUS_OK);
337 :
338 1 : ZERO_STRUCT(rd);
339 1 : rd.in.file.handle = h;
340 1 : rd.in.length = ARRAY_SIZE(buf);
341 1 : rd.in.offset = 0;
342 1 : status = smb2_read(tree, tree, &rd);
343 1 : CHECK_STATUS(status, NT_STATUS_OK);
344 1 : CHECK_VALUE(rd.out.data.length, ARRAY_SIZE(buf));
345 1 : torture_assert_mem_equal_goto(torture, rd.out.data.data,
346 : buf, ARRAY_SIZE(buf),
347 : ret, done,
348 : "Invalid content smb2_read");
349 :
350 1 : timeout_msec = tree->session->transport->options.request_timeout * 1000;
351 :
352 3 : status = smb2cli_read(tree->session->transport->conn,
353 : timeout_msec,
354 1 : tree->session->smbXcli,
355 : tree->smbXcli,
356 : rd.in.length,
357 : rd.in.offset,
358 : h.data[0],
359 : h.data[1],
360 1 : rd.in.min_count,
361 1 : rd.in.remaining,
362 : tmp_ctx,
363 : &data, &data_length);
364 1 : CHECK_STATUS(status, NT_STATUS_OK);
365 1 : CHECK_VALUE(data_length, ARRAY_SIZE(buf));
366 1 : torture_assert_mem_equal_goto(torture, data,
367 : buf, ARRAY_SIZE(buf),
368 : ret, done,
369 : "Invalid content smb2cli_read");
370 :
371 2 : status = smb2cli_ioctl(tree->session->transport->conn,
372 : timeout_msec,
373 1 : tree->session->smbXcli,
374 : tree->smbXcli,
375 : UINT64_MAX, /* in_fid_persistent */
376 : UINT64_MAX, /* in_fid_volatile */
377 : FSCTL_SMBTORTURE_GLOBAL_READ_RESPONSE_BODY_PADDING8,
378 : 0, /* in_max_input_length */
379 : NULL, /* in_input_buffer */
380 : 1, /* in_max_output_length */
381 : NULL, /* in_output_buffer */
382 : SMB2_IOCTL_FLAG_IS_FSCTL,
383 : tmp_ctx,
384 : &out_input_buffer,
385 : &out_output_buffer);
386 2 : if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED) ||
387 2 : NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
388 2 : NT_STATUS_EQUAL(status, NT_STATUS_FS_DRIVER_REQUIRED) ||
389 1 : NT_STATUS_EQUAL(status, NT_STATUS_INVALID_DEVICE_REQUEST))
390 : {
391 1 : torture_comment(torture,
392 : "FSCTL_SMBTORTURE_GLOBAL_READ_RESPONSE_BODY_PADDING8: %s\n",
393 : nt_errstr(status));
394 1 : torture_skip(torture, "server doesn't support FSCTL_SMBTORTURE_GLOBAL_READ_RESPONSE_BODY_PADDING8\n");
395 : }
396 0 : torture_assert_ntstatus_ok(torture, status, "FSCTL_SMBTORTURE_GLOBAL_READ_RESPONSE_BODY_PADDING8");
397 :
398 0 : torture_assert_int_equal(torture, out_output_buffer.length, 0,
399 : "output length");
400 :
401 0 : ZERO_STRUCT(rd);
402 0 : rd.in.file.handle = h;
403 0 : rd.in.length = ARRAY_SIZE(buf);
404 0 : rd.in.offset = 0;
405 0 : status = smb2_read(tree, tree, &rd);
406 0 : CHECK_STATUS(status, NT_STATUS_OK);
407 0 : CHECK_VALUE(rd.out.data.length, ARRAY_SIZE(buf));
408 0 : torture_assert_mem_equal_goto(torture, rd.out.data.data,
409 : buf, ARRAY_SIZE(buf),
410 : ret, done,
411 : "Invalid content after padding smb2_read");
412 :
413 0 : status = smb2cli_read(tree->session->transport->conn,
414 : timeout_msec,
415 0 : tree->session->smbXcli,
416 : tree->smbXcli,
417 : rd.in.length,
418 : rd.in.offset,
419 : h.data[0],
420 : h.data[1],
421 0 : rd.in.min_count,
422 0 : rd.in.remaining,
423 : tmp_ctx,
424 : &data, &data_length);
425 0 : CHECK_STATUS(status, NT_STATUS_OK);
426 0 : CHECK_VALUE(data_length, ARRAY_SIZE(buf));
427 0 : torture_assert_mem_equal_goto(torture, data,
428 : buf, ARRAY_SIZE(buf),
429 : ret, done,
430 : "Invalid content after padding smb2cli_read");
431 :
432 0 : status = smb2_util_close(tree, h);
433 0 : CHECK_STATUS(status, NT_STATUS_OK);
434 :
435 0 : done:
436 0 : talloc_free(tmp_ctx);
437 0 : return ret;
438 : }
439 :
440 : /*
441 : basic testing of SMB2 read
442 : */
443 964 : struct torture_suite *torture_smb2_read_init(TALLOC_CTX *ctx)
444 : {
445 964 : struct torture_suite *suite = torture_suite_create(ctx, "read");
446 :
447 964 : torture_suite_add_1smb2_test(suite, "eof", test_read_eof);
448 964 : torture_suite_add_1smb2_test(suite, "position", test_read_position);
449 964 : torture_suite_add_1smb2_test(suite, "dir", test_read_dir);
450 964 : torture_suite_add_1smb2_test(suite, "access", test_read_access);
451 964 : torture_suite_add_1smb2_test(suite, "bug14607",
452 : test_read_bug14607);
453 :
454 964 : suite->description = talloc_strdup(suite, "SMB2-READ tests");
455 :
456 964 : return suite;
457 : }
458 :
459 0 : static bool test_aio_cancel(struct torture_context *tctx,
460 : struct smb2_tree *tree)
461 : {
462 : struct smb2_handle h;
463 : uint8_t buf[64 * 1024];
464 : struct smb2_read r;
465 0 : struct smb2_request *req = NULL;
466 : int rc;
467 : NTSTATUS status;
468 0 : bool ret = true;
469 :
470 0 : ZERO_STRUCT(buf);
471 :
472 0 : smb2_util_unlink(tree, FNAME);
473 :
474 0 : status = torture_smb2_testfile(tree, FNAME, &h);
475 0 : torture_assert_ntstatus_ok_goto(
476 : tctx,
477 : status,
478 : ret,
479 : done,
480 : "torture_smb2_testfile failed\n");
481 :
482 0 : status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf));
483 0 : torture_assert_ntstatus_ok_goto(
484 : tctx,
485 : status,
486 : ret,
487 : done,
488 : "smb2_util_write failed\n");
489 :
490 0 : status = smb2_util_close(tree, h);
491 0 : torture_assert_ntstatus_ok_goto(
492 : tctx,
493 : status,
494 : ret,
495 : done,
496 : "smb2_util_close failed\n");
497 :
498 0 : status = torture_smb2_testfile_access(
499 : tree, FNAME, &h, SEC_RIGHTS_FILE_ALL);
500 0 : torture_assert_ntstatus_ok_goto(
501 : tctx,
502 : status,
503 : ret,
504 : done,
505 : "torture_smb2_testfile_access failed\n");
506 :
507 0 : r = (struct smb2_read) {
508 : .in.file.handle = h,
509 : .in.length = 1,
510 : .in.offset = 0,
511 : .in.min_count = 1,
512 : };
513 :
514 0 : req = smb2_read_send(tree, &r);
515 0 : torture_assert_goto(
516 : tctx,
517 : req != NULL,
518 : ret,
519 : done,
520 : "smb2_read_send failed\n");
521 :
522 0 : while (!req->cancel.can_cancel) {
523 0 : rc = tevent_loop_once(tctx->ev);
524 0 : torture_assert_goto(
525 : tctx,
526 : rc == 0,
527 : ret,
528 : done,
529 : "tevent_loop_once failed\n");
530 : }
531 :
532 0 : status = smb2_cancel(req);
533 0 : torture_assert_ntstatus_ok_goto(
534 : tctx,
535 : status,
536 : ret,
537 : done,
538 : "smb2_cancel failed\n");
539 :
540 0 : status = smb2_read_recv(req, tree, &r);
541 0 : torture_assert_ntstatus_ok_goto(
542 : tctx,
543 : status,
544 : ret,
545 : done,
546 : "smb2_read_recv failed\n");
547 :
548 0 : status = smb2_util_close(tree, h);
549 0 : torture_assert_ntstatus_ok_goto(
550 : tctx,
551 : status,
552 : ret,
553 : done,
554 : "smb2_util_close failed\n");
555 :
556 0 : done:
557 0 : smb2_util_unlink(tree, FNAME);
558 0 : return ret;
559 : }
560 :
561 : /*
562 : * aio testing against share with VFS module "delay_inject"
563 : */
564 964 : struct torture_suite *torture_smb2_aio_delay_init(TALLOC_CTX *ctx)
565 : {
566 964 : struct torture_suite *suite = torture_suite_create(ctx, "aio_delay");
567 :
568 964 : torture_suite_add_1smb2_test(suite, "aio_cancel", test_aio_cancel);
569 :
570 964 : suite->description = talloc_strdup(suite, "SMB2 delayed aio tests");
571 :
572 964 : return suite;
573 : }
|