Debian Codegraph @

source4/torture/smb2/secleak.c

raw
1 /*
2 Unix SMB/CIFS implementation.
3
4 find security related memory leaks
5
6 Copyright (C) Andrew Tridgell 2004
7 Copyright (C) David Mulder 2020
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/raw/raw_proto.h"
26 #include "libcli/libcli.h"
27 #include "torture/util.h"
28 #include "system/time.h"
29 #include "libcli/smb_composite/smb_composite.h"
30 #include "auth/credentials/credentials.h"
31 #include "param/param.h"
32 #include "libcli/smb2/smb2.h"
33 #include "libcli/smb2/smb2_calls.h"
34 #include "torture/smb2/proto.h"
35 #include "../libcli/smb/smbXcli_base.h"
36
37 static bool try_failed_login(struct torture_context *tctx, struct smb2_tree *tree)
38 {
39 NTSTATUS status;
40 struct cli_credentials *credentials = NULL;
41 uint32_t sessid = 0;
42 struct smb2_session *session = NULL;
43 bool result = true;
44
45 session = smb2_session_init(tree->session->transport,
46 tctx->lp_ctx,
47 lpcfg_gensec_settings(tctx, tctx->lp_ctx),
48 tctx);
49 torture_assert(tctx, session, "Session initialization failed");
50
51 sessid = smb2cli_session_current_id(tree->session->smbXcli);
52 credentials = cli_credentials_init(session);
53 torture_assert_goto(tctx, credentials, result, done,
54 "Credential allocation failed");
55
56 cli_credentials_set_conf(credentials, tctx->lp_ctx);
57 cli_credentials_set_domain(credentials, "INVALID-DOMAIN", CRED_SPECIFIED);
58 cli_credentials_set_username(credentials, "INVALID-USERNAME", CRED_SPECIFIED);
59 cli_credentials_set_password(credentials, "INVALID-PASSWORD", CRED_SPECIFIED);
60
61 status = smb2_session_setup_spnego(session, credentials, sessid);
62 torture_assert_ntstatus_equal_goto(tctx, status,
63 NT_STATUS_LOGON_FAILURE, result, done,
64 "Allowed session setup with invalid credentials?!\n");
65
66 done:
67 /* smb2_session_init() steals the transport, and if we don't steal it
68 * back before freeing session, then we segfault on the next iteration
69 * because the transport pointer in the tree is now invalid.
70 */
71 tree->session->transport = talloc_steal(tree->session, session->transport);
72 talloc_free(session);
73
74 return result;
75 }
76
77 bool torture_smb2_sec_leak(struct torture_context *tctx, struct smb2_tree *tree)
78 {
79 time_t t1 = time_mono(NULL);
80 int timelimit = torture_setting_int(tctx, "timelimit", 20);
81 bool result;
82
83 while (time_mono(NULL) < t1+timelimit) {
84 result = try_failed_login(tctx, tree);
85 torture_assert(tctx, result,
86 "Invalid credentials should have failed");
87
88 talloc_report(NULL, stdout);
89 }
90
91 return true;
92 }