From f66612f62e43b752cb7461da429efd26d1c47296 Mon Sep 17 00:00:00 2001 From: Steven Danneman Date: Wed, 18 Nov 2009 16:35:03 -0800 Subject: [PATCH] s4/torture: port SMBv1 RAW-LOCK tests to SMBv2 RAW-LOCK ported as: RAW-LOCK-LOCK, RAW-LOCK-LOCKX -> SMB2-LOCK-LOCK RAW-PIDHIGH -> removed, no longer relevant RAW-ASYNC -> SMB2-LOCK-ASYNC, SMB2-LOCK-CANCEL, SMB2-LOCK-CANCEL-TDIS, SMB2-LOCK-CANCEL-LOGOFF RAW-ERRORCODE -> SMB2-LOCK-ERRORCODE RAW-CHANGETYPE -> removed, no longer relevant RAW-ZEROBYTELOCKS -> SMB2-LOCK->ZEROBYTELENGTH RAW-UNLOCK -> SMB2-LOCK-UNLOCK RAW-MULTIPLE_UNLOCK -> SMB2-LOCK-MULTIPLE-UNLOCK RAW-STACKING -> SMB2-LOCK-STACKING BASE-LOCK ported as: BASE-LOCK-LOCK1 -> SMB2-LOCK-ERRORCODE, timeout is no longer relevant BASE-LOCK-LOCK2 -> SMB2-LOCK-CONTEND, SMB2-LOCK-LOCK, SMB2-LOCK-CONTEXT BASE-LOCK-LOCK3 -> SMB2-LOCK-RANGE BASE-LOCK-LOCK4 -> SMB2-LOCK-OVERLAP BASE-LOCK-LOCK5 -> SMB2-LOCK-STACKING BASE-LOCK-LOCK6 -> SMB2-LOCK-CANCEL, change_locktype no longer relevant BASE-LOCK-LOCK7 -> SMB2-LOCK-RW-SHARED, SMB2-LOCK-RW-EXCLUSIVE --- source4/torture/smb2/lock.c | 2042 ++++++++++++++++++++++++++++++++++- 1 file changed, 2017 insertions(+), 25 deletions(-) diff --git a/source4/torture/smb2/lock.c b/source4/torture/smb2/lock.c index 67dd76836b9..6bba7bd0e47 100644 --- a/source4/torture/smb2/lock.c +++ b/source4/torture/smb2/lock.c @@ -1,20 +1,20 @@ -/* +/* Unix SMB/CIFS implementation. SMB2 lock test suite Copyright (C) Stefan Metzmacher 2006 - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program. If not, see . */ @@ -25,23 +25,50 @@ #include "torture/torture.h" #include "torture/smb2/proto.h" +#include "torture/util.h" - -#define TARGET_SUPPORTS_INVALID_LOCK_RANGE(_tctx) \ - (torture_setting_bool(_tctx, "invalid_lock_range_support", true)) -#define TARGET_IS_W2K8(_tctx) (torture_setting_bool(_tctx, "w2k8", false)) +#include "lib/events/events.h" +#include "param/param.h" #define CHECK_STATUS(status, correct) do { \ const char *_cmt = "(" __location__ ")"; \ - torture_assert_ntstatus_equal_goto(torture,status,correct,ret,done,_cmt); \ -} while (0) + torture_assert_ntstatus_equal_goto(torture,status,correct, \ + ret,done,_cmt); \ + } while (0) + +#define CHECK_STATUS_CMT(status, correct, cmt) do { \ + torture_assert_ntstatus_equal_goto(torture,status,correct, \ + ret,done,cmt); \ + } while (0) + +#define CHECK_STATUS_CONT(status, correct) do { \ + if (!NT_STATUS_EQUAL(status, correct)) { \ + torture_result(torture, TORTURE_FAIL, \ + "(%s) Incorrect status %s - should be %s\n", \ + __location__, nt_errstr(status), nt_errstr(correct)); \ + ret = false; \ + }} while (0) #define CHECK_VALUE(v, correct) do { \ const char *_cmt = "(" __location__ ")"; \ torture_assert_int_equal_goto(torture,v,correct,ret,done,_cmt); \ -} while (0) + } while (0) + +#define BASEDIR "testlock" -static bool test_valid_request(struct torture_context *torture, struct smb2_tree *tree) +#define TARGET_SUPPORTS_INVALID_LOCK_RANGE(_tctx) \ + (torture_setting_bool(_tctx, "invalid_lock_range_support", true)) +#define TARGET_IS_W2K8(_tctx) (torture_setting_bool(_tctx, "w2k8", false)) + +#define WAIT_FOR_ASYNC_RESPONSE(req) \ + while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) { \ + if (event_loop_once(req->transport->socket->event.ctx) != 0) { \ + break; \ + } \ + } + +static bool test_valid_request(struct torture_context *torture, + struct smb2_tree *tree) { bool ret = true; NTSTATUS status; @@ -368,7 +395,7 @@ static bool test_lock_read_write(struct torture_context *torture, cr.in.desired_access = SEC_RIGHTS_FILE_ALL; cr.in.file_attributes = FILE_ATTRIBUTE_NORMAL; cr.in.create_disposition = NTCREATEX_DISP_OPEN_IF; - cr.in.share_access = + cr.in.share_access = NTCREATEX_SHARE_ACCESS_DELETE| NTCREATEX_SHARE_ACCESS_READ| NTCREATEX_SHARE_ACCESS_WRITE; @@ -531,8 +558,8 @@ static bool test_lock_auto_unlock(struct torture_context *torture, if (TARGET_IS_W2K8(torture)) { CHECK_STATUS(status, NT_STATUS_OK); torture_warning(torture, "Target has \"pretty please\" bug. " - "Every other contending lock request " - "succeeds."); + "A contending lock request on the same handle " + "unlocks the lock.\n"); } else { CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); } @@ -544,21 +571,1986 @@ done: return ret; } +/* + test different lock ranges and see if different handles conflict +*/ +static bool test_lock(struct torture_context *torture, + struct smb2_tree *tree) +{ + NTSTATUS status; + bool ret = true; + struct smb2_handle h, h2; + uint8_t buf[200]; + struct smb2_lock lck; + struct smb2_lock_element el[2]; + + const char *fname = BASEDIR "\\async.txt"; -/* basic testing of SMB2 locking + status = torture_smb2_testdir(tree, BASEDIR, &h); + CHECK_STATUS(status, NT_STATUS_OK); + smb2_util_close(tree, h); + + status = torture_smb2_testfile(tree, fname, &h); + CHECK_STATUS(status, NT_STATUS_OK); + + ZERO_STRUCT(buf); + status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf)); + CHECK_STATUS(status, NT_STATUS_OK); + + status = torture_smb2_testfile(tree, fname, &h2); + CHECK_STATUS(status, NT_STATUS_OK); + + lck.in.locks = el; + + lck.in.lock_count = 0x0001; + lck.in.lock_sequence = 0x00000000; + lck.in.file.handle = h; + el[0].reserved = 0x00000000; + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + + torture_comment(torture, "Trying 0/0 lock\n"); + el[0].offset = 0x0000000000000000; + el[0].length = 0x0000000000000000; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + lck.in.file.handle = h2; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, "Trying 0/1 lock\n"); + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + el[0].offset = 0x0000000000000000; + el[0].length = 0x0000000000000001; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + lck.in.file.handle = h2; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + + torture_comment(torture, "Trying 0xEEFFFFF lock\n"); + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + el[0].offset = 0xEEFFFFFF; + el[0].length = 4000; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + lck.in.file.handle = h2; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + + torture_comment(torture, "Trying 0xEF00000 lock\n"); + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + el[0].offset = 0xEF000000; + el[0].length = 4000; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + lck.in.file.handle = h2; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + + torture_comment(torture, "Trying (2^63 - 1)/1\n"); + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + el[0].offset = 1; + el[0].offset <<= 63; + el[0].offset--; + el[0].length = 1; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + lck.in.file.handle = h2; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + + torture_comment(torture, "Trying 2^63/1\n"); + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + el[0].offset = 1; + el[0].offset <<= 63; + el[0].length = 1; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + lck.in.file.handle = h2; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + + torture_comment(torture, "Trying max/0 lock\n"); + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + el[0].offset = ~0; + el[0].length = 0; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + lck.in.file.handle = h2; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + lck.in.file.handle = h; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + + torture_comment(torture, "Trying max/1 lock\n"); + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + el[0].offset = ~0; + el[0].length = 1; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + lck.in.file.handle = h2; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + + torture_comment(torture, "Trying max/2 lock\n"); + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + el[0].offset = ~0; + el[0].length = 2; + status = smb2_lock(tree, &lck); + if (TARGET_SUPPORTS_INVALID_LOCK_RANGE(torture)) { + CHECK_STATUS(status, NT_STATUS_INVALID_LOCK_RANGE); + } else { + CHECK_STATUS(status, NT_STATUS_OK); + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + } + + torture_comment(torture, "Trying wrong handle unlock\n"); + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + el[0].offset = 10001; + el[0].length = 40002; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + lck.in.file.handle = h2; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + lck.in.file.handle = h; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + +done: + smb2_util_close(tree, h2); + smb2_util_close(tree, h); + smb2_deltree(tree, BASEDIR); + return ret; +} + +/* + test SMB2 LOCK async operation */ -struct torture_suite *torture_smb2_lock_init(void) +static bool test_async(struct torture_context *torture, + struct smb2_tree *tree) { - struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "LOCK"); + NTSTATUS status; + bool ret = true; + struct smb2_handle h, h2; + uint8_t buf[200]; + struct smb2_lock lck; + struct smb2_lock_element el[2]; + struct smb2_request *req = NULL; - torture_suite_add_1smb2_test(suite, "VALID-REQUEST", test_valid_request); - torture_suite_add_1smb2_test(suite, "RW-NONE", test_lock_rw_none); - torture_suite_add_1smb2_test(suite, "RW-SHARED", test_lock_rw_shared); - torture_suite_add_1smb2_test(suite, "RW-EXCLUSIVE", test_lock_rw_exclusive); - torture_suite_add_1smb2_test(suite, "AUTO-UNLOCK", test_lock_auto_unlock); + const char *fname = BASEDIR "\\async.txt"; - suite->description = talloc_strdup(suite, "SMB2-LOCK tests"); + status = torture_smb2_testdir(tree, BASEDIR, &h); + CHECK_STATUS(status, NT_STATUS_OK); + smb2_util_close(tree, h); - return suite; + status = torture_smb2_testfile(tree, fname, &h); + CHECK_STATUS(status, NT_STATUS_OK); + + ZERO_STRUCT(buf); + status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf)); + CHECK_STATUS(status, NT_STATUS_OK); + + status = torture_smb2_testfile(tree, fname, &h2); + CHECK_STATUS(status, NT_STATUS_OK); + + lck.in.locks = el; + + lck.in.lock_count = 0x0001; + lck.in.lock_sequence = 0x00000000; + lck.in.file.handle = h; + el[0].offset = 100; + el[0].length = 50; + el[0].reserved = 0x00000000; + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE; + + torture_comment(torture, " Acquire first lock\n"); + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, " Second lock should pend on first\n"); + lck.in.file.handle = h2; + req = smb2_lock_send(tree, &lck); + WAIT_FOR_ASYNC_RESPONSE(req); + + torture_comment(torture, " Unlock first lock\n"); + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, " Second lock should now succeed\n"); + lck.in.file.handle = h2; + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE; + status = smb2_lock_recv(req, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + +done: + smb2_util_close(tree, h2); + smb2_util_close(tree, h); + smb2_deltree(tree, BASEDIR); + return ret; } +/* + test SMB2 LOCK Cancel operation +*/ +static bool test_cancel(struct torture_context *torture, + struct smb2_tree *tree) +{ + NTSTATUS status; + bool ret = true; + struct smb2_handle h, h2; + uint8_t buf[200]; + struct smb2_lock lck; + struct smb2_lock_element el[2]; + struct smb2_request *req = NULL; + + const char *fname = BASEDIR "\\cancel.txt"; + + status = torture_smb2_testdir(tree, BASEDIR, &h); + CHECK_STATUS(status, NT_STATUS_OK); + smb2_util_close(tree, h); + + status = torture_smb2_testfile(tree, fname, &h); + CHECK_STATUS(status, NT_STATUS_OK); + + ZERO_STRUCT(buf); + status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf)); + CHECK_STATUS(status, NT_STATUS_OK); + + status = torture_smb2_testfile(tree, fname, &h2); + CHECK_STATUS(status, NT_STATUS_OK); + + lck.in.locks = el; + + lck.in.lock_count = 0x0001; + lck.in.lock_sequence = 0x00000000; + lck.in.file.handle = h; + el[0].offset = 100; + el[0].length = 10; + el[0].reserved = 0x00000000; + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE; + + torture_comment(torture, "Testing basic cancel\n"); + + torture_comment(torture, " Acquire first lock\n"); + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, " Second lock should pend on first\n"); + lck.in.file.handle = h2; + req = smb2_lock_send(tree, &lck); + WAIT_FOR_ASYNC_RESPONSE(req); + + torture_comment(torture, " Cancel the second lock\n"); + smb2_cancel(req); + lck.in.file.handle = h2; + status = smb2_lock_recv(req, &lck); + CHECK_STATUS(status, NT_STATUS_CANCELLED); + + torture_comment(torture, " Unlock first lock\n"); + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + + torture_comment(torture, "Testing cancel by unlock\n"); + + torture_comment(torture, " Acquire first lock\n"); + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, " Second lock should pend on first\n"); + lck.in.file.handle = h2; + req = smb2_lock_send(tree, &lck); + WAIT_FOR_ASYNC_RESPONSE(req); + + torture_comment(torture, " Attempt to unlock pending second lock\n"); + lck.in.file.handle = h2; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + + torture_comment(torture, " Now cancel the second lock\n"); + smb2_cancel(req); + lck.in.file.handle = h2; + status = smb2_lock_recv(req, &lck); + CHECK_STATUS(status, NT_STATUS_CANCELLED); + + torture_comment(torture, " Unlock first lock\n"); + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + + torture_comment(torture, "Testing cancel by close\n"); + + torture_comment(torture, " Acquire first lock\n"); + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, " Second lock should pend on first\n"); + lck.in.file.handle = h2; + req = smb2_lock_send(tree, &lck); + WAIT_FOR_ASYNC_RESPONSE(req); + + torture_comment(torture, " Close the second lock handle\n"); + smb2_util_close(tree, h2); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, " Check pending lock reply\n"); + status = smb2_lock_recv(req, &lck); + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + + torture_comment(torture, " Unlock first lock\n"); + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + +done: + smb2_util_close(tree, h2); + smb2_util_close(tree, h); + smb2_deltree(tree, BASEDIR); + return ret; +} + +/* + test SMB2 LOCK Cancel by tree disconnect +*/ +static bool test_cancel_tdis(struct torture_context *torture, + struct smb2_tree *tree) +{ + NTSTATUS status; + bool ret = true; + struct smb2_handle h, h2; + uint8_t buf[200]; + struct smb2_lock lck; + struct smb2_lock_element el[2]; + struct smb2_request *req = NULL; + + const char *fname = BASEDIR "\\cancel_tdis.txt"; + + status = torture_smb2_testdir(tree, BASEDIR, &h); + CHECK_STATUS(status, NT_STATUS_OK); + smb2_util_close(tree, h); + + status = torture_smb2_testfile(tree, fname, &h); + CHECK_STATUS(status, NT_STATUS_OK); + + ZERO_STRUCT(buf); + status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf)); + CHECK_STATUS(status, NT_STATUS_OK); + + status = torture_smb2_testfile(tree, fname, &h2); + CHECK_STATUS(status, NT_STATUS_OK); + + lck.in.locks = el; + + lck.in.lock_count = 0x0001; + lck.in.lock_sequence = 0x00000000; + lck.in.file.handle = h; + el[0].offset = 100; + el[0].length = 10; + el[0].reserved = 0x00000000; + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE; + + torture_comment(torture, "Testing cancel by tree disconnect\n"); + + status = torture_smb2_testfile(tree, fname, &h); + CHECK_STATUS(status, NT_STATUS_OK); + + status = torture_smb2_testfile(tree, fname, &h2); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, " Acquire first lock\n"); + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, " Second lock should pend on first\n"); + lck.in.file.handle = h2; + req = smb2_lock_send(tree, &lck); + WAIT_FOR_ASYNC_RESPONSE(req); + + torture_comment(torture, " Disconnect the tree\n"); + smb2_tdis(tree); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, " Check pending lock reply\n"); + status = smb2_lock_recv(req, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, " Attempt to unlock first lock\n"); + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_FILE_CLOSED); + +done: + smb2_util_close(tree, h2); + smb2_util_close(tree, h); + smb2_deltree(tree, BASEDIR); + return ret; +} + +/* + test SMB2 LOCK Cancel by user logoff +*/ +static bool test_cancel_logoff(struct torture_context *torture, + struct smb2_tree *tree) +{ + NTSTATUS status; + bool ret = true; + struct smb2_handle h, h2; + uint8_t buf[200]; + struct smb2_lock lck; + struct smb2_lock_element el[2]; + struct smb2_request *req = NULL; + + const char *fname = BASEDIR "\\cancel_tdis.txt"; + + status = torture_smb2_testdir(tree, BASEDIR, &h); + CHECK_STATUS(status, NT_STATUS_OK); + smb2_util_close(tree, h); + + status = torture_smb2_testfile(tree, fname, &h); + CHECK_STATUS(status, NT_STATUS_OK); + + ZERO_STRUCT(buf); + status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf)); + CHECK_STATUS(status, NT_STATUS_OK); + + status = torture_smb2_testfile(tree, fname, &h2); + CHECK_STATUS(status, NT_STATUS_OK); + + lck.in.locks = el; + + lck.in.lock_count = 0x0001; + lck.in.lock_sequence = 0x00000000; + lck.in.file.handle = h; + el[0].offset = 100; + el[0].length = 10; + el[0].reserved = 0x00000000; + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE; + + torture_comment(torture, "Testing cancel by ulogoff\n"); + + torture_comment(torture, " Acquire first lock\n"); + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, " Second lock should pend on first\n"); + lck.in.file.handle = h2; + req = smb2_lock_send(tree, &lck); + WAIT_FOR_ASYNC_RESPONSE(req); + + torture_comment(torture, " Logoff user\n"); + smb2_logoff(tree->session); + + torture_comment(torture, " Check pending lock reply\n"); + status = smb2_lock_recv(req, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, " Attempt to unlock first lock\n"); + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_FILE_CLOSED); + +done: + smb2_util_close(tree, h2); + smb2_util_close(tree, h); + smb2_deltree(tree, BASEDIR); + return ret; +} + +/* + * Test NT_STATUS_LOCK_NOT_GRANTED vs. NT_STATUS_FILE_LOCK_CONFLICT + * + * The SMBv1 protocol returns a different error code on lock acquisition + * failure depending on a number of parameters, including what error code + * was returned to the previous failure. + * + * SMBv2 has cleaned up these semantics and should always return + * NT_STATUS_LOCK_NOT_GRANTED to failed lock requests, and + * NT_STATUS_FILE_LOCK_CONFLICT to failed read/write requests due to a lock + * being held on that range. +*/ +static bool test_errorcode(struct torture_context *torture, + struct smb2_tree *tree) +{ + NTSTATUS status; + bool ret = true; + struct smb2_handle h, h2; + uint8_t buf[200]; + struct smb2_lock lck; + struct smb2_lock_element el[2]; + + const char *fname = BASEDIR "\\errorcode.txt"; + + status = torture_smb2_testdir(tree, BASEDIR, &h); + CHECK_STATUS(status, NT_STATUS_OK); + smb2_util_close(tree, h); + + status = torture_smb2_testfile(tree, fname, &h); + CHECK_STATUS(status, NT_STATUS_OK); + + ZERO_STRUCT(buf); + status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf)); + CHECK_STATUS(status, NT_STATUS_OK); + + status = torture_smb2_testfile(tree, fname, &h2); + CHECK_STATUS(status, NT_STATUS_OK); + + lck.in.locks = el; + + lck.in.lock_count = 0x0001; + lck.in.lock_sequence = 0x00000000; + lck.in.file.handle = h; + el[0].offset = 100; + el[0].length = 10; + el[0].reserved = 0x00000000; + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + + torture_comment(torture, "Testing LOCK_NOT_GRANTED vs. " + "FILE_LOCK_CONFLICT\n"); + + if (TARGET_IS_W2K8(torture)) { + torture_result(torture, TORTURE_SKIP, + "Target has \"pretty please\" bug. A contending lock " + "request on the same handle unlocks the lock."); + goto done; + } + + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* Demonstrate that the first conflicting lock on each handle gives + * LOCK_NOT_GRANTED. */ + lck.in.file.handle = h; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + lck.in.file.handle = h2; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + /* Demonstrate that each following conflict also gives + * LOCK_NOT_GRANTED */ + lck.in.file.handle = h; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + lck.in.file.handle = h2; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + lck.in.file.handle = h; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + lck.in.file.handle = h2; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + /* Demonstrate that the smbpid doesn't matter */ + tree->session->pid++; + lck.in.file.handle = h; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + lck.in.file.handle = h2; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + tree->session->pid--; + + /* Demonstrate that a 0-byte lock inside the locked range still + * gives the same error. */ + + el[0].offset = 102; + el[0].length = 0; + lck.in.file.handle = h; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + lck.in.file.handle = h2; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + /* Demonstrate that a lock inside the locked range still gives the + * same error. */ + + el[0].offset = 102; + el[0].length = 5; + lck.in.file.handle = h; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + lck.in.file.handle = h2; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + +done: + smb2_util_close(tree, h2); + smb2_util_close(tree, h); + smb2_deltree(tree, BASEDIR); + return ret; +} + +/** + * Tests zero byte locks. + */ + +struct double_lock_test { + struct smb2_lock_element lock1; + struct smb2_lock_element lock2; + NTSTATUS status; +}; + +static struct double_lock_test zero_byte_tests[] = { + /* {offset, count, reserved, flags}, + * {offset, count, reserved, flags}, + * status */ + + /** First, takes a zero byte lock at offset 10. Then: + * - Taking 0 byte lock at 10 should succeed. + * - Taking 1 byte locks at 9,10,11 should succeed. + * - Taking 2 byte lock at 9 should fail. + * - Taking 2 byte lock at 10 should succeed. + * - Taking 3 byte lock at 9 should fail. + */ + {{10, 0, 0, 0}, {10, 0, 0, 0}, NT_STATUS_OK}, + {{10, 0, 0, 0}, {9, 1, 0, 0}, NT_STATUS_OK}, + {{10, 0, 0, 0}, {10, 1, 0, 0}, NT_STATUS_OK}, + {{10, 0, 0, 0}, {11, 1, 0, 0}, NT_STATUS_OK}, + {{10, 0, 0, 0}, {9, 2, 0, 0}, NT_STATUS_LOCK_NOT_GRANTED}, + {{10, 0, 0, 0}, {10, 2, 0, 0}, NT_STATUS_OK}, + {{10, 0, 0, 0}, {9, 3, 0, 0}, NT_STATUS_LOCK_NOT_GRANTED}, + + /** Same, but opposite order. */ + {{10, 0, 0, 0}, {10, 0, 0, 0}, NT_STATUS_OK}, + {{9, 1, 0, 0}, {10, 0, 0, 0}, NT_STATUS_OK}, + {{10, 1, 0, 0}, {10, 0, 0, 0}, NT_STATUS_OK}, + {{11, 1, 0, 0}, {10, 0, 0, 0}, NT_STATUS_OK}, + {{9, 2, 0, 0}, {10, 0, 0, 0}, NT_STATUS_LOCK_NOT_GRANTED}, + {{10, 2, 0, 0}, {10, 0, 0, 0}, NT_STATUS_OK}, + {{9, 3, 0, 0}, {10, 0, 0, 0}, NT_STATUS_LOCK_NOT_GRANTED}, + + /** Zero zero case. */ + {{0, 0, 0, 0}, {0, 0, 0, 0}, NT_STATUS_OK}, +}; + +static bool test_zerobytelength(struct torture_context *torture, + struct smb2_tree *tree) +{ + NTSTATUS status; + bool ret = true; + struct smb2_handle h, h2; + uint8_t buf[200]; + struct smb2_lock lck; + int i; + + const char *fname = BASEDIR "\\zero.txt"; + + torture_comment(torture, "Testing zero length byte range locks:\n"); + + status = torture_smb2_testdir(tree, BASEDIR, &h); + CHECK_STATUS(status, NT_STATUS_OK); + smb2_util_close(tree, h); + + status = torture_smb2_testfile(tree, fname, &h); + CHECK_STATUS(status, NT_STATUS_OK); + + ZERO_STRUCT(buf); + status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf)); + CHECK_STATUS(status, NT_STATUS_OK); + + status = torture_smb2_testfile(tree, fname, &h2); + CHECK_STATUS(status, NT_STATUS_OK); + + /* Setup initial parameters */ + lck.in.lock_count = 0x0001; + lck.in.lock_sequence = 0x00000000; + lck.in.file.handle = h; + + /* Try every combination of locks in zero_byte_tests, using the same + * handle for both locks. The first lock is assumed to succeed. The + * second lock may contend, depending on the expected status. */ + for (i = 0; i < ARRAY_SIZE(zero_byte_tests); i++) { + torture_comment(torture, + " ... {%llu, %llu} + {%llu, %llu} = %s\n", + zero_byte_tests[i].lock1.offset, + zero_byte_tests[i].lock1.length, + zero_byte_tests[i].lock2.offset, + zero_byte_tests[i].lock2.length, + nt_errstr(zero_byte_tests[i].status)); + + /* Lock both locks. */ + lck.in.locks = &zero_byte_tests[i].lock1; + lck.in.locks[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + lck.in.locks = &zero_byte_tests[i].lock2; + lck.in.locks[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS_CONT(status, zero_byte_tests[i].status); + + /* Unlock both locks in reverse order. */ + lck.in.locks[0].flags = SMB2_LOCK_FLAG_UNLOCK; + if (NT_STATUS_EQUAL(status, NT_STATUS_OK)) { + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + } + + lck.in.locks = &zero_byte_tests[i].lock1; + lck.in.locks[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + } + + /* Try every combination of locks in zero_byte_tests, using two + * different handles. */ + for (i = 0; i < ARRAY_SIZE(zero_byte_tests); i++) { + torture_comment(torture, + " ... {%llu, %llu} + {%llu, %llu} = %s\n", + zero_byte_tests[i].lock1.offset, + zero_byte_tests[i].lock1.length, + zero_byte_tests[i].lock2.offset, + zero_byte_tests[i].lock2.length, + nt_errstr(zero_byte_tests[i].status)); + + /* Lock both locks. */ + lck.in.file.handle = h; + lck.in.locks = &zero_byte_tests[i].lock1; + lck.in.locks[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + lck.in.file.handle = h2; + lck.in.locks = &zero_byte_tests[i].lock2; + lck.in.locks[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS_CONT(status, zero_byte_tests[i].status); + + /* Unlock both locks in reverse order. */ + lck.in.file.handle = h2; + lck.in.locks[0].flags = SMB2_LOCK_FLAG_UNLOCK; + if (NT_STATUS_EQUAL(status, NT_STATUS_OK)) { + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + } + + lck.in.file.handle = h; + lck.in.locks = &zero_byte_tests[i].lock1; + lck.in.locks[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + } + +done: + smb2_util_close(tree, h2); + smb2_util_close(tree, h); + smb2_deltree(tree, BASEDIR); + return ret; +} + +static bool test_unlock(struct torture_context *torture, + struct smb2_tree *tree) +{ + NTSTATUS status; + bool ret = true; + struct smb2_handle h, h2; + uint8_t buf[200]; + struct smb2_lock lck; + struct smb2_lock_element el1[1]; + struct smb2_lock_element el2[1]; + + const char *fname = BASEDIR "\\unlock.txt"; + + status = torture_smb2_testdir(tree, BASEDIR, &h); + CHECK_STATUS(status, NT_STATUS_OK); + smb2_util_close(tree, h); + + status = torture_smb2_testfile(tree, fname, &h); + CHECK_STATUS(status, NT_STATUS_OK); + + ZERO_STRUCT(buf); + status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf)); + CHECK_STATUS(status, NT_STATUS_OK); + + status = torture_smb2_testfile(tree, fname, &h2); + CHECK_STATUS(status, NT_STATUS_OK); + + /* Setup initial parameters */ + lck.in.locks = el1; + lck.in.lock_count = 0x0001; + lck.in.lock_sequence = 0x00000000; + el1[0].offset = 0; + el1[0].length = 10; + el1[0].reserved = 0x00000000; + + /* Take exclusive lock, then unlock it with a shared-unlock call. */ + + torture_comment(torture, "Testing unlock exclusive with shared\n"); + + torture_comment(torture, " taking exclusive lock.\n"); + lck.in.file.handle = h; + el1[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, " try to unlock the exclusive with a shared " + "unlock call.\n"); + el1[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER); + + torture_comment(torture, " try shared lock on h2, to test the " + "unlock.\n"); + lck.in.file.handle = h2; + el1[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + torture_comment(torture, " unlock the exclusive lock\n"); + lck.in.file.handle = h; + el1[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* Unlock a shared lock with an exclusive-unlock call. */ + + torture_comment(torture, "Testing unlock shared with exclusive\n"); + + torture_comment(torture, " taking shared lock.\n"); + lck.in.file.handle = h; + el1[0].flags = SMB2_LOCK_FLAG_SHARED; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, " try to unlock the shared with an exclusive " + "unlock call.\n"); + el1[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER); + + torture_comment(torture, " try exclusive lock on h2, to test the " + "unlock.\n"); + lck.in.file.handle = h2; + el1[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + torture_comment(torture, " unlock the exclusive lock\n"); + lck.in.file.handle = h; + el1[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* Test unlocking of stacked 0-byte locks. SMB2 0-byte lock behavior + * should be the same as >0-byte behavior. Exclusive locks should be + * unlocked before shared. */ + + torture_comment(torture, "Test unlocking stacked 0-byte locks\n"); + + lck.in.locks = el1; + lck.in.file.handle = h; + el1[0].offset = 10; + el1[0].length = 0; + el1[0].reserved = 0x00000000; + el2[0].offset = 5; + el2[0].length = 10; + el2[0].reserved = 0x00000000; + + /* lock 0-byte exclusive */ + el1[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* lock 0-byte shared */ + el1[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* test contention */ + lck.in.locks = el2; + lck.in.file.handle = h2; + el2[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + lck.in.locks = el2; + lck.in.file.handle = h2; + el2[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + /* unlock */ + lck.in.locks = el1; + lck.in.file.handle = h; + el1[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* test - can we take a shared lock? */ + lck.in.locks = el2; + lck.in.file.handle = h2; + el2[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + el2[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* cleanup */ + lck.in.locks = el1; + lck.in.file.handle = h; + el1[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* Test unlocking of stacked exclusive, shared locks. Exclusive + * should be unlocked before any shared. */ + + torture_comment(torture, "Test unlocking stacked exclusive/shared " + "locks\n"); + + lck.in.locks = el1; + lck.in.file.handle = h; + el1[0].offset = 10; + el1[0].length = 10; + el1[0].reserved = 0x00000000; + el2[0].offset = 5; + el2[0].length = 10; + el2[0].reserved = 0x00000000; + + /* lock exclusive */ + el1[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* lock shared */ + el1[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* test contention */ + lck.in.locks = el2; + lck.in.file.handle = h2; + el2[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + lck.in.locks = el2; + lck.in.file.handle = h2; + el2[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + /* unlock */ + lck.in.locks = el1; + lck.in.file.handle = h; + el1[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* test - can we take a shared lock? */ + lck.in.locks = el2; + lck.in.file.handle = h2; + el2[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + el2[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* cleanup */ + lck.in.locks = el1; + lck.in.file.handle = h; + el1[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + +done: + smb2_util_close(tree, h2); + smb2_util_close(tree, h); + smb2_deltree(tree, BASEDIR); + return ret; +} + +static bool test_multiple_unlock(struct torture_context *torture, + struct smb2_tree *tree) +{ + NTSTATUS status; + bool ret = true; + struct smb2_handle h; + uint8_t buf[200]; + struct smb2_lock lck; + struct smb2_lock_element el[2]; + struct smb2_lock_element el0, el1; + + const char *fname = BASEDIR "\\unlock_multiple.txt"; + + status = torture_smb2_testdir(tree, BASEDIR, &h); + CHECK_STATUS(status, NT_STATUS_OK); + smb2_util_close(tree, h); + + status = torture_smb2_testfile(tree, fname, &h); + CHECK_STATUS(status, NT_STATUS_OK); + + ZERO_STRUCT(buf); + status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf)); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, "Testing multiple unlocks:\n"); + + /* Setup initial parameters */ + lck.in.lock_count = 0x0002; + lck.in.lock_sequence = 0x00000000; + lck.in.file.handle = h; + el0.offset = 0; + el0.length = 10; + el0.reserved = 0x00000000; + el1.offset = 10; + el1.length = 10; + el1.reserved = 0x00000000; + el[0] = el0; + el[1] = el1; + + /* Test1: Acquire second lock, but not first. */ + torture_comment(torture, " unlock 2 locks, first one not locked. " + "Expect no locks unlocked. \n"); + + lck.in.lock_count = 0x0001; + el1.flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + lck.in.locks = &el1; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* Try to unlock both locks */ + lck.in.lock_count = 0x0002; + el0.flags = SMB2_LOCK_FLAG_UNLOCK; + el1.flags = SMB2_LOCK_FLAG_UNLOCK; + el[0] = el0; + el[1] = el1; + lck.in.locks = el; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + + /* Second lock should not be unlocked. */ + lck.in.lock_count = 0x0001; + el1.flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + lck.in.locks = &el1; + status = smb2_lock(tree, &lck); + if (TARGET_IS_W2K8(torture)) { + CHECK_STATUS(status, NT_STATUS_OK); + torture_warning(torture, "Target has \"pretty please\" bug. " + "A contending lock request on the same handle " + "unlocks the lock.\n"); + } else { + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + } + + /* cleanup */ + lck.in.lock_count = 0x0001; + el1.flags = SMB2_LOCK_FLAG_UNLOCK; + lck.in.locks = &el1; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* Test2: Acquire first lock, but not second. */ + torture_comment(torture, " unlock 2 locks, second one not locked. " + "Expect first lock unlocked.\n"); + + lck.in.lock_count = 0x0001; + el0.flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + lck.in.locks = &el0; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* Try to unlock both locks */ + lck.in.lock_count = 0x0002; + el0.flags = SMB2_LOCK_FLAG_UNLOCK; + el1.flags = SMB2_LOCK_FLAG_UNLOCK; + el[0] = el0; + el[1] = el1; + lck.in.locks = el; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + + /* First lock should be unlocked. */ + lck.in.lock_count = 0x0001; + el0.flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + lck.in.locks = &el0; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* cleanup */ + lck.in.lock_count = 0x0001; + el0.flags = SMB2_LOCK_FLAG_UNLOCK; + lck.in.locks = &el0; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + +done: + smb2_util_close(tree, h); + smb2_deltree(tree, BASEDIR); + return ret; +} + +/** + * Test lock stacking + * - some tests ported from BASE-LOCK-LOCK5 + */ +static bool test_stacking(struct torture_context *torture, + struct smb2_tree *tree) +{ + NTSTATUS status; + bool ret = true; + struct smb2_handle h, h2; + uint8_t buf[200]; + struct smb2_lock lck; + struct smb2_lock_element el[1]; + + const char *fname = BASEDIR "\\stacking.txt"; + + status = torture_smb2_testdir(tree, BASEDIR, &h); + CHECK_STATUS(status, NT_STATUS_OK); + smb2_util_close(tree, h); + + status = torture_smb2_testfile(tree, fname, &h); + CHECK_STATUS(status, NT_STATUS_OK); + + ZERO_STRUCT(buf); + status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf)); + CHECK_STATUS(status, NT_STATUS_OK); + + status = torture_smb2_testfile(tree, fname, &h2); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, "Testing lock stacking:\n"); + + /* Setup initial parameters */ + lck.in.locks = el; + lck.in.lock_count = 0x0001; + lck.in.lock_sequence = 0x00000000; + lck.in.file.handle = h; + el[0].offset = 0; + el[0].length = 10; + el[0].reserved = 0x00000000; + + /* Try to take a shared lock, then a shared lock on same handle */ + torture_comment(torture, " stacking a shared on top of a shared" + "lock succeeds.\n"); + + el[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + el[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* cleanup */ + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + + /* Try to take an exclusive lock, then a shared lock on same handle */ + torture_comment(torture, " stacking a shared on top of an exclusive " + "lock succeeds.\n"); + + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + el[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + el[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* stacking a shared from a different handle should fail */ + lck.in.file.handle = h2; + el[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + /* cleanup */ + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* ensure the 4th unlock fails */ + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + + /* ensure a second handle can now take an exclusive lock */ + lck.in.file.handle = h2; + el[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* Try to take an exclusive lock, then a shared lock on a + * different handle */ + torture_comment(torture, " stacking a shared on top of an exclusive " + "lock with different handles fails.\n"); + + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + lck.in.file.handle = h2; + el[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + /* cleanup */ + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + /* Try to take a shared lock, then stack an exclusive with same + * handle. */ + torture_comment(torture, " stacking an exclusive on top of a shared " + "lock fails.\n"); + + el[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + /* cleanup */ + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + if (TARGET_IS_W2K8(torture)) { + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + torture_warning(torture, "Target has \"pretty please\" bug. " + "A contending lock request on the same handle " + "unlocks the lock.\n"); + } else { + CHECK_STATUS(status, NT_STATUS_OK); + } + + /* Prove that two exclusive locks do not stack on the same handle. */ + torture_comment(torture, " two exclusive locks do not stack.\n"); + + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + /* cleanup */ + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + if (TARGET_IS_W2K8(torture)) { + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + torture_warning(torture, "Target has \"pretty please\" bug. " + "A contending lock request on the same handle " + "unlocks the lock.\n"); + } else { + CHECK_STATUS(status, NT_STATUS_OK); + } + +done: + smb2_util_close(tree, h2); + smb2_util_close(tree, h); + smb2_deltree(tree, BASEDIR); + return ret; +} + +/** + * Test lock contention + * - shared lock should contend with exclusive lock on different handle + */ +static bool test_contend(struct torture_context *torture, + struct smb2_tree *tree) +{ + NTSTATUS status; + bool ret = true; + struct smb2_handle h, h2; + uint8_t buf[200]; + struct smb2_lock lck; + struct smb2_lock_element el[1]; + + const char *fname = BASEDIR "\\contend.txt"; + + status = torture_smb2_testdir(tree, BASEDIR, &h); + CHECK_STATUS(status, NT_STATUS_OK); + smb2_util_close(tree, h); + + status = torture_smb2_testfile(tree, fname, &h); + CHECK_STATUS(status, NT_STATUS_OK); + + ZERO_STRUCT(buf); + status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf)); + CHECK_STATUS(status, NT_STATUS_OK); + + status = torture_smb2_testfile(tree, fname, &h2); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, "Testing lock contention:\n"); + + /* Setup initial parameters */ + lck.in.locks = el; + lck.in.lock_count = 0x0001; + lck.in.lock_sequence = 0x00000000; + lck.in.file.handle = h; + el[0].offset = 0; + el[0].length = 10; + el[0].reserved = 0x00000000; + + /* Take an exclusive lock, then a shared lock on different handle */ + torture_comment(torture, " shared should contend on exclusive on " + "different handle.\n"); + + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + lck.in.file.handle = h2; + el[0].flags = SMB2_LOCK_FLAG_SHARED | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED); + + /* cleanup */ + lck.in.file.handle = h; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + +done: + smb2_util_close(tree, h2); + smb2_util_close(tree, h); + smb2_deltree(tree, BASEDIR); + return ret; +} + +/** + * Test locker context + * - test that pid does not affect the locker context + */ +static bool test_context(struct torture_context *torture, + struct smb2_tree *tree) +{ + NTSTATUS status; + bool ret = true; + struct smb2_handle h, h2; + uint8_t buf[200]; + struct smb2_lock lck; + struct smb2_lock_element el[1]; + + const char *fname = BASEDIR "\\context.txt"; + + status = torture_smb2_testdir(tree, BASEDIR, &h); + CHECK_STATUS(status, NT_STATUS_OK); + smb2_util_close(tree, h); + + status = torture_smb2_testfile(tree, fname, &h); + CHECK_STATUS(status, NT_STATUS_OK); + + ZERO_STRUCT(buf); + status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf)); + CHECK_STATUS(status, NT_STATUS_OK); + + status = torture_smb2_testfile(tree, fname, &h2); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, "Testing locker context:\n"); + + /* Setup initial parameters */ + lck.in.locks = el; + lck.in.lock_count = 0x0001; + lck.in.lock_sequence = 0x00000000; + lck.in.file.handle = h; + el[0].offset = 0; + el[0].length = 10; + el[0].reserved = 0x00000000; + + /* Take an exclusive lock, then try to unlock with a different pid, + * same handle. This shows that the pid doesn't affect the locker + * context in SMB2. */ + torture_comment(torture, " pid shouldn't affect locker context\n"); + + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + tree->session->pid++; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_OK); + + tree->session->pid--; + el[0].flags = SMB2_LOCK_FLAG_UNLOCK; + status = smb2_lock(tree, &lck); + CHECK_STATUS(status, NT_STATUS_RANGE_NOT_LOCKED); + +done: + smb2_util_close(tree, h2); + smb2_util_close(tree, h); + smb2_deltree(tree, BASEDIR); + return ret; +} + +/** + * Test as much of the potential lock range as possible + * - test ported from BASE-LOCK-LOCK3 + */ +static bool test_range(struct torture_context *torture, + struct smb2_tree *tree) +{ + NTSTATUS status; + bool ret = true; + struct smb2_handle h, h2; + uint8_t buf[200]; + struct smb2_lock lck; + struct smb2_lock_element el[1]; + uint64_t offset, i; + extern int torture_numops; + + const char *fname = BASEDIR "\\range.txt"; + +#define NEXT_OFFSET offset += (~(uint64_t)0) / torture_numops + + status = torture_smb2_testdir(tree, BASEDIR, &h); + CHECK_STATUS(status, NT_STATUS_OK); + smb2_util_close(tree, h); + + status = torture_smb2_testfile(tree, fname, &h); + CHECK_STATUS(status, NT_STATUS_OK); + + ZERO_STRUCT(buf); + status = smb2_util_write(tree, h, buf, 0, ARRAY_SIZE(buf)); + CHECK_STATUS(status, NT_STATUS_OK); + + status = torture_smb2_testfile(tree, fname, &h2); + CHECK_STATUS(status, NT_STATUS_OK); + + torture_comment(torture, "Testing locks spread across the 64-bit " + "offset range\n"); + + if (TARGET_IS_W2K8(torture)) { + torture_result(torture, TORTURE_SKIP, + "Target has \"pretty please\" bug. A contending lock " + "request on the same handle unlocks the lock."); + goto done; + } + + /* Setup initial parameters */ + lck.in.locks = el; + lck.in.lock_count = 0x0001; + lck.in.lock_sequence = 0x00000000; + lck.in.file.handle = h; + el[0].offset = 0; + el[0].length = 1; + el[0].reserved = 0x00000000; + el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE | + SMB2_LOCK_FLAG_FAIL_IMMEDIATELY; + + torture_comment(torture, " establishing %d locks\n", torture_numops); + + for (offset=i=0; idescription = talloc_strdup(suite, "SMB2-LOCK tests"); + + return suite; +} -- 2.34.1