#include "libcli/smb2/smb2_calls.h"
#include "libcli/smb_composite/smb_composite.h"
#include "libcli/resolve/resolve.h"
+#include "libcli/smb/smbXcli_base.h"
#include "lib/cmdline/popt_common.h"
#include "lib/events/events.h"
#include "torture/torture.h"
#include "torture/smb2/proto.h"
+#include "lib/util/sys_rw.h"
+
#define CHECK_RANGE(v, min, max) do { \
if ((v) < (min) || (v) > (max)) { \
torture_result(tctx, TORTURE_FAIL, "(%s): wrong value for %s " \
NTSTATUS status;
const char *host = torture_setting_string(tctx, "host", NULL);
const char *share = torture_setting_string(tctx, "share", NULL);
- struct cli_credentials *credentials = cmdline_credentials;
struct smbcli_options options;
lpcfg_smbcli_options(tctx->lp_ctx, &options);
status = smb2_connect(tctx, host,
lpcfg_smb_ports(tctx->lp_ctx), share,
lpcfg_resolve_context(tctx->lp_ctx),
- credentials, tree, tctx->ev, &options,
+ popt_get_cmdline_credentials(),
+ tree, tctx->ev, &options,
lpcfg_socket_options(tctx->lp_ctx),
lpcfg_gensec_settings(tctx, tctx->lp_ctx));
if (!NT_STATUS_IS_OK(status)) {
return true;
}
-/*
- Timer handler function notifies the registering function that time is up
-*/
-static void timeout_cb(struct tevent_context *ev,
- struct tevent_timer *te,
- struct timeval current_time,
- void *private_data)
-{
- bool *timesup = (bool *)private_data;
- *timesup = true;
- return;
-}
-
-/*
- Wait a short period of time to receive a single oplock break request
-*/
-static void torture_wait_for_oplock_break(struct torture_context *tctx)
-{
- TALLOC_CTX *tmp_ctx = talloc_new(NULL);
- struct tevent_timer *te = NULL;
- struct timeval ne;
- bool timesup = false;
- int old_count = break_info.count;
-
- /* Wait .1 seconds for an oplock break */
- ne = tevent_timeval_current_ofs(0, 100000);
-
- if ((te = tevent_add_timer(tctx->ev, tmp_ctx, ne, timeout_cb, ×up))
- == NULL)
- {
- torture_comment(tctx, "Failed to wait for an oplock break. "
- "test results may not be accurate.");
- goto done;
- }
-
- while (!timesup && break_info.count < old_count + 1) {
- if (tevent_loop_once(tctx->ev) != 0) {
- torture_comment(tctx, "Failed to wait for an oplock "
- "break. test results may not be "
- "accurate.");
- goto done;
- }
- }
-
-done:
- /* We don't know if the timed event fired and was freed, we received
- * our oplock break, or some other event triggered the loop. Thus,
- * we create a tmp_ctx to be able to safely free/remove the timed
- * event in all 3 cases. */
- talloc_free(tmp_ctx);
-
- return;
-}
-
static bool test_smb2_oplock_exclusive1(struct torture_context *tctx,
struct smb2_tree *tree1,
struct smb2_tree *tree2)
SEC_FILE_WRITE_ATTRIBUTE |
SEC_STD_SYNCHRONIZE;
io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
+ io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
status = smb2_create(tree1, tctx, &(io.smb2));
torture_assert_ntstatus_ok(tctx, status, "Error opening the file");
h1 = io.smb2.out.file.handle;
+ CHECK_VAL(io.smb2.out.create_action, FILE_WAS_CREATED);
CHECK_VAL(io.smb2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
torture_comment(tctx, "Subsequent normal open should break oplock on "
io.smb2.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
+ io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
status = smb2_create(tree2, tctx, &(io.smb2));
torture_assert_ntstatus_ok(tctx, status, "Incorrect status");
h2 = io.smb2.out.file.handle;
CHECK_VAL(break_info.handle.data[0], h1.data[0]);
CHECK_VAL(break_info.failures, 0);
CHECK_VAL(break_info.level, SMB2_OPLOCK_LEVEL_II);
+ CHECK_VAL(io.smb2.out.create_action, FILE_WAS_OPENED);
CHECK_VAL(io.smb2.out.oplock_level, SMB2_OPLOCK_LEVEL_II);
smb2_util_close(tree2, h2);
state->done = true;
}
-struct torture_suite *torture_smb2_oplocks_init(void)
+static bool test_smb2_oplock_levelII502(struct torture_context *tctx,
+ struct smb2_tree *tree1,
+ struct smb2_tree *tree2)
+
+{
+ const char *fname = BASEDIR "\\test_levelII502.dat";
+ NTSTATUS status;
+ union smb_open io;
+ struct smb2_close closeio;
+ struct smb2_handle h;
+
+ status = torture_smb2_testdir(tree1, BASEDIR, &h);
+ torture_assert_ntstatus_ok(tctx, status, "Error creating directory");
+
+ /* cleanup */
+ smb2_util_unlink(tree1, fname);
+
+ /*
+ base ntcreatex parms
+ */
+ ZERO_STRUCT(io.smb2);
+ io.generic.level = RAW_OPEN_SMB2;
+ io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
+ io.smb2.in.alloc_size = 0;
+ io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+ io.smb2.in.create_options = 0;
+ io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+ io.smb2.in.security_flags = 0;
+ io.smb2.in.fname = fname;
+
+ torture_comment(
+ tctx,
+ "LEVELII502: Open a stale LEVEL2 oplock with OVERWRITE");
+
+ io.smb2.in.desired_access = SEC_RIGHTS_FILE_READ |
+ SEC_RIGHTS_FILE_WRITE;
+ io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
+ NTCREATEX_SHARE_ACCESS_WRITE;
+ io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
+ io.smb2.in.oplock_level = SMB2_OPLOCK_LEVEL_II;
+ status = smb2_create(tree1, tctx, &(io.smb2));
+ torture_assert_ntstatus_ok(tctx, status, "Error opening the file");
+ torture_assert(tctx,
+ io.smb2.out.oplock_level==SMB2_OPLOCK_LEVEL_II,
+ "Did not get LEVEL_II oplock\n");
+
+ status = smbXcli_conn_samba_suicide(
+ tree1->session->transport->conn, 93);
+ torture_assert_ntstatus_ok(tctx, status, "suicide failed");
+
+ sleep(1);
+
+ io.smb2.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
+ io.smb2.in.create_disposition = NTCREATEX_DISP_OVERWRITE;
+
+ status = smb2_create(tree2, tctx, &(io.smb2));
+ torture_assert_ntstatus_ok(tctx, status, "Error opening the file");
+ torture_assert(tctx,
+ io.smb2.out.oplock_level==SMB2_OPLOCK_LEVEL_BATCH,
+ "Did not get BATCH oplock\n");
+
+ closeio = (struct smb2_close) {
+ .in.file.handle = io.smb2.out.file.handle,
+ };
+ status = smb2_close(tree2, &closeio);
+ torture_assert_ntstatus_equal(
+ tctx, status, NT_STATUS_OK, "close failed");
+
+ return true;
+}
+
+struct torture_suite *torture_smb2_oplocks_init(TALLOC_CTX *ctx)
{
struct torture_suite *suite =
- torture_suite_create(talloc_autofree_context(), "oplock");
+ torture_suite_create(ctx, "oplock");
torture_suite_add_2smb2_test(suite, "exclusive1", test_smb2_oplock_exclusive1);
torture_suite_add_2smb2_test(suite, "exclusive2", test_smb2_oplock_exclusive2);
torture_suite_add_1smb2_test(suite, "levelii500", test_smb2_oplock_levelII500);
torture_suite_add_2smb2_test(suite, "levelii501",
test_smb2_oplock_levelII501);
+ torture_suite_add_2smb2_test(suite, "levelii502",
+ test_smb2_oplock_levelII502);
suite->description = talloc_strdup(suite, "SMB2-OPLOCK tests");
return suite;
uint32_t share_access;
struct smb2_handle handle;
} hold_info[] = {
- { BASEDIR "\\notshared_close", true,
- NTCREATEX_SHARE_ACCESS_NONE, },
- { BASEDIR "\\notshared_noclose", false,
- NTCREATEX_SHARE_ACCESS_NONE, },
- { BASEDIR "\\shared_close", true,
- NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE, },
- { BASEDIR "\\shared_noclose", false,
- NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE, },
+ {
+ .fname = BASEDIR "\\notshared_close",
+ .close_on_break = true,
+ .share_access = NTCREATEX_SHARE_ACCESS_NONE,
+ },
+ {
+ .fname = BASEDIR "\\notshared_noclose",
+ .close_on_break = false,
+ .share_access = NTCREATEX_SHARE_ACCESS_NONE,
+ },
+ {
+ .fname = BASEDIR "\\shared_close",
+ .close_on_break = true,
+ .share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE,
+ },
+ {
+ .fname = BASEDIR "\\shared_noclose",
+ .close_on_break = false,
+ .share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE,
+ },
};
static bool torture_oplock_handler_hold(struct smb2_transport *transport,
return ret;
}
-struct torture_suite *torture_smb2_kernel_oplocks_init(void)
+/**
+ * 1. 1st client opens file with oplock
+ * 2. 2nd client opens file
+ *
+ * Verify 2 triggers an oplock break
+ **/
+static bool test_smb2_kernel_oplocks3(struct torture_context *tctx,
+ struct smb2_tree *tree,
+ struct smb2_tree *tree2)
+{
+ const char *fname = "test_kernel_oplock3.dat";
+ NTSTATUS status;
+ bool ret = true;
+ struct smb2_create create;
+ struct smb2_handle h1 = {{0}}, h2 = {{0}};
+
+ smb2_util_unlink(tree, fname);
+ status = torture_smb2_testfile(tree, fname, &h1);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+ "Error creating testfile\n");
+ smb2_util_close(tree, h1);
+ ZERO_STRUCT(h1);
+
+ tree->session->transport->oplock.handler = torture_oplock_handler;
+ tree->session->transport->oplock.private_data = tree;
+ ZERO_STRUCT(break_info);
+
+ /* 1 */
+ ZERO_STRUCT(create);
+ create.in.desired_access = SEC_RIGHTS_FILE_ALL;
+ create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+ create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+ create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+ create.in.fname = fname;
+ create.in.oplock_level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
+
+ status = smb2_create(tree, tctx, &create);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+ h1 = create.out.file.handle;
+
+ torture_assert_goto(tctx, create.out.oplock_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE, ret, done,
+ "Oplock level is not SMB2_OPLOCK_LEVEL_EXCLUSIVE\n");
+
+ /* 2 */
+ ZERO_STRUCT(create);
+ create.in.desired_access = SEC_RIGHTS_FILE_READ;
+ create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+ create.in.create_disposition = NTCREATEX_DISP_OPEN;
+ create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+ create.in.fname = fname;
+
+ status = smb2_create(tree2, tctx, &create);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+ h2 = create.out.file.handle;
+
+ torture_wait_for_oplock_break(tctx);
+ torture_assert_goto(tctx, break_info.count == 1, ret, done, "Expected 1 oplock break\n");
+
+done:
+ if (!smb2_util_handle_empty(h1)) {
+ smb2_util_close(tree, h1);
+ }
+ if (!smb2_util_handle_empty(h2)) {
+ smb2_util_close(tree, h2);
+ }
+ smb2_util_unlink(tree, fname);
+ return ret;
+}
+
+/**
+ * 1) create testfile with stream
+ * 2) open file r/w with batch oplock, sharing read/delete
+ * 3) open stream on file for reading
+ *
+ * Verify 3) doesn't trigger an oplock break
+ **/
+static bool test_smb2_kernel_oplocks4(struct torture_context *tctx,
+ struct smb2_tree *tree)
+{
+ const char *fname = "test_kernel_oplock4.dat";
+ const char *sname = "test_kernel_oplock4.dat:foo";
+ NTSTATUS status;
+ bool ret = true;
+ struct smb2_create create;
+ struct smb2_handle h1 = {{0}}, h2 = {{0}};
+
+ tree->session->transport->oplock.handler = torture_oplock_handler;
+ tree->session->transport->oplock.private_data = tree;
+ ZERO_STRUCT(break_info);
+ smb2_util_unlink(tree, fname);
+
+ /* 1 */
+ status = torture_smb2_testfile(tree, fname, &h1);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+ "Error creating testfile\n");
+ smb2_util_close(tree, h1);
+ ZERO_STRUCT(h1);
+
+ ZERO_STRUCT(create);
+ create.in.desired_access = SEC_RIGHTS_FILE_READ;
+ create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+ create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+ create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+ create.in.fname = sname;
+
+ status = smb2_create(tree, tctx, &create);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+ h1 = create.out.file.handle;
+ smb2_util_close(tree, h1);
+ ZERO_STRUCT(h1);
+
+ /* 2 */
+ ZERO_STRUCT(create);
+ create.in.desired_access = SEC_RIGHTS_FILE_ALL;
+ create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ create.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE;
+ create.in.create_disposition = NTCREATEX_DISP_OPEN;
+ create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+ create.in.fname = fname;
+ create.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
+
+ status = smb2_create(tree, tctx, &create);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+ h1 = create.out.file.handle;
+
+ torture_assert_goto(tctx, create.out.oplock_level == SMB2_OPLOCK_LEVEL_BATCH, ret, done,
+ "Oplock level is not SMB2_OPLOCK_LEVEL_BATCH\n");
+
+ ZERO_STRUCT(create);
+ create.in.desired_access = SEC_RIGHTS_FILE_READ;
+ create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ create.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE;
+ create.in.create_disposition = NTCREATEX_DISP_OPEN;
+ create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+ create.in.fname = sname;
+
+ status = smb2_create(tree, tctx, &create);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+ h2 = create.out.file.handle;
+
+ torture_wait_for_oplock_break(tctx);
+ if (break_info.count != 0) {
+ torture_warning(tctx, "Stream open caused oplock break\n");
+ }
+
+done:
+ if (!smb2_util_handle_empty(h1)) {
+ smb2_util_close(tree, h1);
+ }
+ if (!smb2_util_handle_empty(h2)) {
+ smb2_util_close(tree, h2);
+ }
+ smb2_util_unlink(tree, fname);
+ return ret;
+}
+
+/**
+ * 1) create testfile with stream
+ * 2) open stream r/w with batch oplock -> batch oplock granted
+ * 3) open stream r/o with batch oplock
+ *
+ * Verify 3) does trigger an oplock break
+ **/
+static bool test_smb2_kernel_oplocks5(struct torture_context *tctx,
+ struct smb2_tree *tree)
+{
+ const char *fname = "test_kernel_oplock4.dat";
+ const char *sname = "test_kernel_oplock4.dat:foo";
+ NTSTATUS status;
+ bool ret = true;
+ struct smb2_create create;
+ struct smb2_handle h1 = {{0}}, h2 = {{0}};
+
+ tree->session->transport->oplock.handler = torture_oplock_handler;
+ tree->session->transport->oplock.private_data = tree;
+ ZERO_STRUCT(break_info);
+ smb2_util_unlink(tree, fname);
+
+ /* 1 */
+ status = torture_smb2_testfile(tree, fname, &h1);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+ "Error creating testfile\n");
+ smb2_util_close(tree, h1);
+ ZERO_STRUCT(h1);
+
+ ZERO_STRUCT(create);
+ create.in.desired_access = SEC_RIGHTS_FILE_READ;
+ create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+ create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+ create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+ create.in.fname = sname;
+
+ status = smb2_create(tree, tctx, &create);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+ h1 = create.out.file.handle;
+ smb2_util_close(tree, h1);
+ ZERO_STRUCT(h1);
+
+ /* 2 */
+ ZERO_STRUCT(create);
+ create.in.desired_access = SEC_RIGHTS_FILE_ALL;
+ create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+ create.in.create_disposition = NTCREATEX_DISP_OPEN;
+ create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+ create.in.fname = sname;
+ create.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
+
+ status = smb2_create(tree, tctx, &create);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+ h1 = create.out.file.handle;
+
+ torture_assert_goto(tctx, create.out.oplock_level == SMB2_OPLOCK_LEVEL_BATCH, ret, done,
+ "Oplock level is not SMB2_OPLOCK_LEVEL_BATCH\n");
+
+ ZERO_STRUCT(create);
+ create.in.desired_access = SEC_RIGHTS_FILE_READ;
+ create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+ create.in.create_disposition = NTCREATEX_DISP_OPEN;
+ create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+ create.in.fname = sname;
+ create.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
+
+ status = smb2_create(tree, tctx, &create);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+ h2 = create.out.file.handle;
+
+ torture_assert_goto(tctx, create.out.oplock_level == SMB2_OPLOCK_LEVEL_NONE, ret, done,
+ "Oplock level is not SMB2_OPLOCK_LEVEL_NONE\n");
+
+ torture_wait_for_oplock_break(tctx);
+ if (break_info.count != 1) {
+ torture_warning(tctx, "Stream open didn't cause oplock break\n");
+ }
+
+done:
+ if (!smb2_util_handle_empty(h1)) {
+ smb2_util_close(tree, h1);
+ }
+ if (!smb2_util_handle_empty(h2)) {
+ smb2_util_close(tree, h2);
+ }
+ smb2_util_unlink(tree, fname);
+ return ret;
+}
+
+/**
+ * 1) create testfile with stream
+ * 2) 1st client opens stream r/w with batch oplock -> batch oplock granted
+ * 3) 2nd client opens stream r/o with batch oplock
+ *
+ * Verify 3) does trigger an oplock break
+ **/
+static bool test_smb2_kernel_oplocks6(struct torture_context *tctx,
+ struct smb2_tree *tree,
+ struct smb2_tree *tree2)
+{
+ const char *fname = "test_kernel_oplock6.dat";
+ const char *sname = "test_kernel_oplock6.dat:foo";
+ NTSTATUS status;
+ bool ret = true;
+ struct smb2_create create;
+ struct smb2_handle h1 = {{0}}, h2 = {{0}};
+
+ smb2_util_unlink(tree, fname);
+ status = torture_smb2_testfile(tree, fname, &h1);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+ "Error creating testfile\n");
+ smb2_util_close(tree, h1);
+ ZERO_STRUCT(h1);
+
+ tree->session->transport->oplock.handler = torture_oplock_handler;
+ tree->session->transport->oplock.private_data = tree;
+ ZERO_STRUCT(break_info);
+
+ /* 1 */
+ ZERO_STRUCT(create);
+ create.in.desired_access = SEC_RIGHTS_FILE_READ;
+ create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+ create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+ create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+ create.in.fname = sname;
+
+ status = smb2_create(tree, tctx, &create);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+ h1 = create.out.file.handle;
+ smb2_util_close(tree, h1);
+ ZERO_STRUCT(h1);
+
+ /* 2 */
+ ZERO_STRUCT(create);
+ create.in.desired_access = SEC_RIGHTS_FILE_ALL;
+ create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+ create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+ create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+ create.in.fname = fname;
+ create.in.oplock_level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
+
+ status = smb2_create(tree, tctx, &create);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+ h1 = create.out.file.handle;
+
+ torture_assert_goto(tctx, create.out.oplock_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE, ret, done,
+ "Oplock level is not SMB2_OPLOCK_LEVEL_EXCLUSIVE\n");
+
+ /* 3 */
+ ZERO_STRUCT(create);
+ create.in.desired_access = SEC_RIGHTS_FILE_READ;
+ create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+ create.in.create_disposition = NTCREATEX_DISP_OPEN;
+ create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+ create.in.fname = fname;
+
+ status = smb2_create(tree2, tctx, &create);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+ h2 = create.out.file.handle;
+
+ torture_assert_goto(tctx, create.out.oplock_level == SMB2_OPLOCK_LEVEL_NONE, ret, done,
+ "Oplock level is not SMB2_OPLOCK_LEVEL_NONE\n");
+
+ torture_wait_for_oplock_break(tctx);
+ torture_assert_goto(tctx, break_info.count == 1, ret, done, "Expected 1 oplock break\n");
+
+done:
+ if (!smb2_util_handle_empty(h1)) {
+ smb2_util_close(tree, h1);
+ }
+ if (!smb2_util_handle_empty(h2)) {
+ smb2_util_close(tree, h2);
+ }
+ smb2_util_unlink(tree, fname);
+ return ret;
+}
+
+/**
+ * Recreate regression test from bug:
+ *
+ * https://bugzilla.samba.org/show_bug.cgi?id=13058
+ *
+ * 1. smbd-1 opens the file and sets the oplock
+ * 2. smbd-2 tries to open the file. open() fails(EAGAIN) and open is deferred.
+ * 3. smbd-1 sends oplock break request to the client.
+ * 4. smbd-1 closes the file.
+ * 5. smbd-1 opens the file and sets the oplock.
+ * 6. smbd-2 calls defer_open_done(), and should re-break the oplock.
+ **/
+
+static bool test_smb2_kernel_oplocks7(struct torture_context *tctx,
+ struct smb2_tree *tree,
+ struct smb2_tree *tree2)
+{
+ const char *fname = "test_kernel_oplock7.dat";
+ NTSTATUS status;
+ bool ret = true;
+ struct smb2_create create;
+ struct smb2_handle h1 = {{0}}, h2 = {{0}};
+ struct smb2_create create_2;
+ struct smb2_create io;
+ struct smb2_request *req;
+
+ smb2_util_unlink(tree, fname);
+ status = torture_smb2_testfile(tree, fname, &h1);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+ "Error creating testfile\n");
+ smb2_util_close(tree, h1);
+ ZERO_STRUCT(h1);
+
+ /* Close the open file on break. */
+ tree->session->transport->oplock.handler = torture_oplock_handler_close;
+ tree->session->transport->oplock.private_data = tree;
+ ZERO_STRUCT(break_info);
+
+ /* 1 - open file with oplock */
+ ZERO_STRUCT(create);
+ create.in.desired_access = SEC_RIGHTS_FILE_ALL;
+ create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+ create.in.create_disposition = NTCREATEX_DISP_OPEN;
+ create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+ create.in.fname = fname;
+ create.in.oplock_level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
+
+ status = smb2_create(tree, tctx, &create);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+ "Error opening the file\n");
+ CHECK_VAL(create.out.oplock_level, SMB2_OPLOCK_LEVEL_EXCLUSIVE);
+
+ /* 2 - open file to break oplock */
+ ZERO_STRUCT(create_2);
+ create_2.in.desired_access = SEC_RIGHTS_FILE_ALL;
+ create_2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ create_2.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+ create_2.in.create_disposition = NTCREATEX_DISP_OPEN;
+ create_2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+ create_2.in.fname = fname;
+ create_2.in.oplock_level = SMB2_OPLOCK_LEVEL_NONE;
+
+ /* Open on tree2 - should cause a break on tree */
+ req = smb2_create_send(tree2, &create_2);
+ torture_assert(tctx, req != NULL, "smb2_create_send");
+
+ /* The oplock break handler should close the file. */
+ /* Steps 3 & 4. */
+ torture_wait_for_oplock_break(tctx);
+
+ tree->session->transport->oplock.handler = torture_oplock_handler;
+
+ /*
+ * 5 - re-open on tree. NB. There is a race here
+ * depending on which smbd goes first. We either get
+ * an oplock level of SMB2_OPLOCK_LEVEL_EXCLUSIVE if
+ * the close and re-open on tree is processed first, or
+ * SMB2_OPLOCK_LEVEL_NONE if the pending create on
+ * tree2 is processed first.
+ */
+ status = smb2_create(tree, tctx, &create);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+ "Error opening the file\n");
+
+ h1 = create.out.file.handle;
+ if (create.out.oplock_level != SMB2_OPLOCK_LEVEL_EXCLUSIVE &&
+ create.out.oplock_level != SMB2_OPLOCK_LEVEL_NONE) {
+ torture_result(tctx,
+ TORTURE_FAIL,
+ "(%s): wrong value for oplock got 0x%x\n",
+ __location__,
+ (unsigned int)create.out.oplock_level);
+ ret = false;
+ goto done;
+
+ }
+
+ /* 6 - retrieve the second open. */
+ status = smb2_create_recv(req, tctx, &io);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+ "Error opening the file\n");
+ h2 = io.out.file.handle;
+ CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_NONE);
+
+done:
+ if (!smb2_util_handle_empty(h1)) {
+ smb2_util_close(tree, h1);
+ }
+ if (!smb2_util_handle_empty(h2)) {
+ smb2_util_close(tree2, h2);
+ }
+ smb2_util_unlink(tree, fname);
+ return ret;
+}
+
+#ifdef HAVE_KERNEL_OPLOCKS_LINUX
+
+#ifndef F_SETLEASE
+#define F_SETLEASE 1024
+#endif
+
+#ifndef RT_SIGNAL_LEASE
+#define RT_SIGNAL_LEASE (SIGRTMIN+1)
+#endif
+
+#ifndef F_SETSIG
+#define F_SETSIG 10
+#endif
+
+static int got_break;
+
+/*
+ * Signal handler.
+ */
+
+static void got_rt_break(int sig)
+{
+ got_break = 1;
+}
+
+static int got_alarm;
+
+/*
+ * Signal handler.
+ */
+
+static void got_alarm_fn(int sig)
+{
+ got_alarm = 1;
+}
+
+/*
+ * Child process function.
+ */
+
+static int do_child_process(int pipefd, const char *name)
+{
+ int ret = 0;
+ int fd = -1;
+ char c = 0;
+ struct sigaction act;
+ sigset_t set;
+ sigset_t empty_set;
+
+ /* Block RT_SIGNAL_LEASE and SIGALRM. */
+ sigemptyset(&set);
+ sigemptyset(&empty_set);
+ sigaddset(&set, RT_SIGNAL_LEASE);
+ sigaddset(&set, SIGALRM);
+ ret = sigprocmask(SIG_SETMASK, &set, NULL);
+ if (ret == -1) {
+ return 11;
+ }
+
+ /* Set up a signal handler for RT_SIGNAL_LEASE. */
+ ZERO_STRUCT(act);
+ act.sa_handler = got_rt_break;
+ ret = sigaction(RT_SIGNAL_LEASE, &act, NULL);
+ if (ret == -1) {
+ return 1;
+ }
+ /* Set up a signal handler for SIGALRM. */
+ ZERO_STRUCT(act);
+ act.sa_handler = got_alarm_fn;
+ ret = sigaction(SIGALRM, &act, NULL);
+ if (ret == -1) {
+ return 1;
+ }
+ /* Open the passed in file and get a kernel oplock. */
+ fd = open(name, O_RDWR, 0666);
+ if (fd == -1) {
+ return 2;
+ }
+
+ ret = fcntl(fd, F_SETSIG, RT_SIGNAL_LEASE);
+ if (ret == -1) {
+ close(fd);
+ return 3;
+ }
+
+ ret = fcntl(fd, F_SETLEASE, F_WRLCK);
+ if (ret == -1) {
+ close(fd);
+ return 4;
+ }
+
+ /* Tell the parent we're ready. */
+ ret = sys_write(pipefd, &c, 1);
+ if (ret != 1) {
+ close(fd);
+ return 5;
+ }
+
+ /* Ensure the pause doesn't hang forever. */
+ alarm(5);
+
+ /* Wait for RT_SIGNAL_LEASE or SIGALRM. */
+ ret = sigsuspend(&empty_set);
+ if (ret != -1 || errno != EINTR) {
+ close(fd);
+ return 6;
+ }
+
+ if (got_alarm == 1) {
+ close(fd);
+ return 10;
+ }
+
+ if (got_break != 1) {
+ close(fd);
+ return 7;
+ }
+
+ /* Cancel any pending alarm. */
+ alarm(0);
+
+ /* Force the server to wait for 3 seconds. */
+ sleep(3);
+
+ /* Remove our lease. */
+ ret = fcntl(fd, F_SETLEASE, F_UNLCK);
+ if (ret == -1) {
+ close(fd);
+ return 8;
+ }
+
+ ret = close(fd);
+ if (ret == -1) {
+ return 9;
+ }
+
+ /* All is well. */
+ return 0;
+}
+
+static bool wait_for_child_oplock(struct torture_context *tctx,
+ const char *localdir,
+ const char *fname)
+{
+ int fds[2];
+ int ret;
+ pid_t pid;
+ char *name = talloc_asprintf(tctx,
+ "%s/%s",
+ localdir,
+ fname);
+
+ torture_assert(tctx, name != NULL, "talloc failed");
+
+ ret = pipe(fds);
+ torture_assert(tctx, ret != -1, "pipe failed");
+
+ pid = fork();
+ torture_assert(tctx, pid != (pid_t)-1, "fork failed");
+
+ if (pid != (pid_t)0) {
+ char c;
+ /* Parent. */
+ TALLOC_FREE(name);
+ close(fds[1]);
+ ret = sys_read(fds[0], &c, 1);
+ torture_assert(tctx, ret == 1, "read failed");
+ return true;
+ }
+
+ /* Child process. */
+ close(fds[0]);
+ ret = do_child_process(fds[1], name);
+ _exit(ret);
+ /* Notreached. */
+}
+#else
+static bool wait_for_child_oplock(struct torture_context *tctx,
+ const char *localdir,
+ const char *fname)
+{
+ return false;
+}
+#endif
+
+static void child_sig_term_handler(struct tevent_context *ev,
+ struct tevent_signal *se,
+ int signum,
+ int count,
+ void *siginfo,
+ void *private_data)
+{
+ int *pstatus = (int *)private_data;
+ int status = 0;
+
+ wait(&status);
+ if (WIFEXITED(status)) {
+ *pstatus = WEXITSTATUS(status);
+ } else {
+ *pstatus = status;
+ }
+}
+
+/*
+ * Deal with a non-smbd process holding a kernel oplock.
+ */
+
+static bool test_smb2_kernel_oplocks8(struct torture_context *tctx,
+ struct smb2_tree *tree)
+{
+ const char *fname = "test_kernel_oplock8.dat";
+ const char *fname1 = "tmp_test_kernel_oplock8.dat";
+ NTSTATUS status;
+ bool ret = true;
+ struct smb2_create io;
+ struct smb2_request *req = NULL;
+ struct smb2_handle h1 = {{0}};
+ struct smb2_handle h2 = {{0}};
+ const char *localdir = torture_setting_string(tctx, "localdir", NULL);
+ struct tevent_signal *se = NULL;
+ int child_exit_code = -1;
+ time_t start;
+ time_t end;
+
+#ifndef HAVE_KERNEL_OPLOCKS_LINUX
+ torture_skip(tctx, "Need kernel oplocks for test");
+#endif
+
+ if (localdir == NULL) {
+ torture_skip(tctx, "Need localdir for test");
+ }
+
+ smb2_util_unlink(tree, fname);
+ smb2_util_unlink(tree, fname1);
+ status = torture_smb2_testfile(tree, fname, &h1);
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+ "Error creating testfile\n");
+ smb2_util_close(tree, h1);
+ ZERO_STRUCT(h1);
+
+ se = tevent_add_signal(tctx->ev,
+ tctx,
+ SIGCHLD,
+ 0,
+ child_sig_term_handler,
+ &child_exit_code);
+ torture_assert(tctx, se != NULL, "tevent_add_signal failed\n");
+
+ /* Take the oplock locally in a sub-process. */
+ ret = wait_for_child_oplock(tctx, localdir, fname);
+ torture_assert_goto(tctx, ret, ret, done,
+ "Wait for child process failed.\n");
+
+ /*
+ * Now try and open. This should block for 3 seconds.
+ * while the child process is still alive.
+ */
+
+ ZERO_STRUCT(io);
+ io.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
+ io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+ io.in.create_disposition = NTCREATEX_DISP_OPEN;
+ io.in.share_access =
+ NTCREATEX_SHARE_ACCESS_DELETE|
+ NTCREATEX_SHARE_ACCESS_READ|
+ NTCREATEX_SHARE_ACCESS_WRITE;
+ io.in.create_options = 0;
+ io.in.fname = fname;
+
+ req = smb2_create_send(tree, &io);
+ torture_assert_goto(tctx, req != NULL,
+ ret, done, "smb2_create_send");
+
+ /* Ensure while the open is blocked the smbd is
+ still serving other requests. */
+ io.in.fname = fname1;
+ io.in.create_disposition = NTCREATEX_DISP_CREATE;
+
+ /* Time the start -> end of the request. */
+ start = time(NULL);
+ status = smb2_create(tree, tctx, &io);
+ end = time(NULL);
+
+ /* Should succeed. */
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+ "Error opening the second file\n");
+ h1 = io.out.file.handle;
+
+ /* in less than 2 seconds. Otherwise the server blocks. */
+ torture_assert_goto(tctx, end - start < 2,
+ ret, done, "server was blocked !");
+
+ /* Pick up the return for the initial blocking open. */
+ status = smb2_create_recv(req, tctx, &io);
+
+ /* Which should also have succeeded. */
+ torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+ "Error opening the file\n");
+ h2 = io.out.file.handle;
+
+ /* Wait for the exit code from the child. */
+ while (child_exit_code == -1) {
+ int rval = tevent_loop_once(tctx->ev);
+ torture_assert_goto(tctx, rval == 0, ret,
+ done, "tevent_loop_once error\n");
+ }
+
+ torture_assert_int_equal_goto(tctx, child_exit_code, 0,
+ ret, done, "Bad child exit code");
+
+done:
+ if (!smb2_util_handle_empty(h1)) {
+ smb2_util_close(tree, h1);
+ }
+ if (!smb2_util_handle_empty(h2)) {
+ smb2_util_close(tree, h2);
+ }
+ smb2_util_unlink(tree, fname);
+ smb2_util_unlink(tree, fname1);
+ return ret;
+}
+
+struct torture_suite *torture_smb2_kernel_oplocks_init(TALLOC_CTX *ctx)
{
struct torture_suite *suite =
- torture_suite_create(talloc_autofree_context(), "kernel-oplocks");
+ torture_suite_create(ctx, "kernel-oplocks");
torture_suite_add_1smb2_test(suite, "kernel_oplocks1", test_smb2_kernel_oplocks1);
torture_suite_add_1smb2_test(suite, "kernel_oplocks2", test_smb2_kernel_oplocks2);
+ torture_suite_add_2smb2_test(suite, "kernel_oplocks3", test_smb2_kernel_oplocks3);
+ torture_suite_add_1smb2_test(suite, "kernel_oplocks4", test_smb2_kernel_oplocks4);
+ torture_suite_add_1smb2_test(suite, "kernel_oplocks5", test_smb2_kernel_oplocks5);
+ torture_suite_add_2smb2_test(suite, "kernel_oplocks6", test_smb2_kernel_oplocks6);
+ torture_suite_add_2smb2_test(suite, "kernel_oplocks7", test_smb2_kernel_oplocks7);
+ torture_suite_add_1smb2_test(suite, "kernel_oplocks8", test_smb2_kernel_oplocks8);
suite->description = talloc_strdup(suite, "SMB2-KERNEL-OPLOCK tests");