summaryrefslogtreecommitdiff
path: root/libcli/smb/test_smb1cli_session.c
blob: 6a526c96b6143dcfd14e3b208ce531f696bc7bd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>

#include "replace.h"
#include <talloc.h>
#include "libcli/util/ntstatus.h"
#include "smb_constants.h"
#include "smb_util.h"

static const uint8_t smb1_session_setup_bytes[] = {
	0xA1, 0x82, 0x01, 0x02, 0x30, 0x81, 0xFF, 0xA0,
	0x03, 0x0A, 0x01, 0x01, 0xA1, 0x0C, 0x06, 0x0A,
	0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02,
	0x02, 0x0A, 0xA2, 0x81, 0xE9, 0x04, 0x81, 0xE6,
	0x4E, 0x54, 0x4C, 0x4D, 0x53, 0x53, 0x50, 0x00,
	0x02, 0x00, 0x00, 0x00, 0x16, 0x00, 0x16, 0x00,
	0x38, 0x00, 0x00, 0x00, 0x15, 0x82, 0x89, 0x62,
	0xF6, 0x65, 0xAB, 0x23, 0x47, 0xBC, 0x4D, 0x21,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x98, 0x00, 0x98, 0x00, 0x4E, 0x00, 0x00, 0x00,
	0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F,
	0x53, 0x00, 0x41, 0x00, 0x4D, 0x00, 0x42, 0x00,
	0x41, 0x00, 0x44, 0x00, 0x4F, 0x00, 0x4D, 0x00,
	0x41, 0x00, 0x49, 0x00, 0x4E, 0x00, 0x02, 0x00,
	0x16, 0x00, 0x53, 0x00, 0x41, 0x00, 0x4D, 0x00,
	0x42, 0x00, 0x41, 0x00, 0x44, 0x00, 0x4F, 0x00,
	0x4D, 0x00, 0x41, 0x00, 0x49, 0x00, 0x4E, 0x00,
	0x01, 0x00, 0x0E, 0x00, 0x4C, 0x00, 0x4F, 0x00,
	0x43, 0x00, 0x41, 0x00, 0x4C, 0x00, 0x44, 0x00,
	0x43, 0x00, 0x04, 0x00, 0x22, 0x00, 0x73, 0x00,
	0x61, 0x00, 0x6D, 0x00, 0x62, 0x00, 0x61, 0x00,
	0x2E, 0x00, 0x65, 0x00, 0x78, 0x00, 0x61, 0x00,
	0x6D, 0x00, 0x70, 0x00, 0x6C, 0x00, 0x65, 0x00,
	0x2E, 0x00, 0x63, 0x00, 0x6F, 0x00, 0x6D, 0x00,
	0x03, 0x00, 0x32, 0x00, 0x6C, 0x00, 0x6F, 0x00,
	0x63, 0x00, 0x61, 0x00, 0x6C, 0x00, 0x64, 0x00,
	0x63, 0x00, 0x2E, 0x00, 0x73, 0x00, 0x61, 0x00,
	0x6D, 0x00, 0x62, 0x00, 0x61, 0x00, 0x2E, 0x00,
	0x65, 0x00, 0x78, 0x00, 0x61, 0x00, 0x6D, 0x00,
	0x70, 0x00, 0x6C, 0x00, 0x65, 0x00, 0x2E, 0x00,
	0x63, 0x00, 0x6F, 0x00, 0x6D, 0x00, 0x07, 0x00,
	0x08, 0x00, 0x0C, 0x40, 0xA3, 0xC3, 0x5B, 0xE0,
	0xD2, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55,
	0x00, 0x6E, 0x00, 0x69, 0x00, 0x78, 0x00, 0x00,
	0x00, 0x53, 0x00, 0x61, 0x00, 0x6D, 0x00, 0x62,
	0x00, 0x61, 0x00, 0x20, 0x00, 0x34, 0x00, 0x2E,
	0x00, 0x37, 0x00, 0x2E, 0x00, 0x30, 0x00, 0x70,
	0x00, 0x72, 0x00, 0x65, 0x00, 0x31, 0x00, 0x2D,
	0x00, 0x44, 0x00, 0x45, 0x00, 0x56, 0x00, 0x45,
	0x00, 0x4C, 0x00, 0x4F, 0x00, 0x50, 0x00, 0x45,
	0x00, 0x52, 0x00, 0x42, 0x00, 0x55, 0x00, 0x49,
	0x00, 0x4C, 0x00, 0x44, 0x00, 0x00, 0x00, 0x53,
	0x00, 0x41, 0x00, 0x4D, 0x00, 0x42, 0x00, 0x41,
	0x00, 0x44, 0x00, 0x4F, 0x00, 0x4D, 0x00, 0x41,
	0x00, 0x49, 0x00, 0x4E, 0x00, 0x00, 0x00
};

static void test_smb_bytes_pull_str(void **state)
{
	NTSTATUS status;
	const uint8_t *bytes = smb1_session_setup_bytes;
	const size_t num_bytes = sizeof(smb1_session_setup_bytes);
	const uint8_t *p = NULL;
	size_t ret = 0;
	size_t out_security_blob_length = 262;
	bool use_unicode = true;
	char *str = NULL;

	p = bytes;
	p += out_security_blob_length;

	status = smb_bytes_pull_str(NULL, &str, use_unicode,
				    bytes, num_bytes,
				    p, &ret);
	assert_true(NT_STATUS_IS_OK(status));
	assert_string_equal(str, "Unix");
	assert_int_equal(ret, 0x0b);
	TALLOC_FREE(str);

	p += ret;
	status = smb_bytes_pull_str(NULL, &str, use_unicode,
				    bytes, num_bytes,
				    p, &ret);
	assert_true(NT_STATUS_IS_OK(status));
	assert_string_equal(str, "Samba 4.7.0pre1-DEVELOPERBUILD");
	assert_int_equal(ret, 0x3e);
	TALLOC_FREE(str);

	p += ret;
	status = smb_bytes_pull_str(NULL, &str, use_unicode,
				    bytes, num_bytes,
				    p, &ret);
	assert_true(NT_STATUS_IS_OK(status));
	assert_string_equal(str, "SAMBADOMAIN");
	assert_int_equal(ret, 0x18);
	TALLOC_FREE(str);

	p += ret;
	status = smb_bytes_pull_str(NULL, &str, use_unicode,
				    bytes, num_bytes,
				    p, &ret);
	assert_true(NT_STATUS_IS_OK(status));
	assert_string_equal(str, "");
	assert_int_equal(ret, 0x00);
	TALLOC_FREE(str);
}

static void test_smb_bytes_pull_str_no_unicode(void **state)
{
	NTSTATUS status;
	const uint8_t *bytes = smb1_session_setup_bytes;
	const size_t num_bytes = sizeof(smb1_session_setup_bytes);
	const uint8_t *p = NULL;
	size_t ret = 0;
	size_t out_security_blob_length = 262;
	bool use_unicode = false;
	char *str = NULL;

	p = bytes;
	p += out_security_blob_length;

	status = smb_bytes_pull_str(NULL, &str, use_unicode,
				    bytes, num_bytes,
				    p, &ret);
	assert_true(NT_STATUS_IS_OK(status));
	assert_string_equal(str, "");
	assert_int_equal(ret, 0x01);
	TALLOC_FREE(str);
}

static void test_smb_bytes_pull_str_wrong_offset(void **state)
{
	NTSTATUS status;
	const uint8_t *bytes = smb1_session_setup_bytes;
	const size_t num_bytes = sizeof(smb1_session_setup_bytes);
	const uint8_t *p = NULL;
	size_t ret = 0;
	size_t out_security_blob_length = 261;
	bool use_unicode = true;
	char *str = NULL;

	bytes += 1;
	p = bytes;
	p += out_security_blob_length;

	status = smb_bytes_pull_str(NULL, &str, use_unicode,
				    bytes, num_bytes,
				    p, &ret);
	assert_true(NT_STATUS_IS_OK(status));

	assert_string_equal(str, "\xE5\x94\x80\xE6\xB8\x80\xE6\xA4\x80\xE7\xA0\x80");
	assert_int_equal(ret, 0x0a);
	TALLOC_FREE(str);
}

static void test_smb_bytes_pull_str_invalid_offset(void **state)
{
	NTSTATUS status;
	const uint8_t *bytes = smb1_session_setup_bytes;
	const size_t num_bytes = sizeof(smb1_session_setup_bytes);
	const uint8_t *p = NULL;
	size_t ret = 0;
	bool use_unicode = true;
	char *str = NULL;
	intptr_t bytes_address = (intptr_t)bytes;

	/* Warning: array subscript is below array bounds */
	p = (const uint8_t *)(bytes_address - 1);
	status = smb_bytes_pull_str(NULL, &str, use_unicode,
				    bytes, num_bytes,
				    p, &ret);
	assert_int_equal(NT_STATUS_V(status),
			 NT_STATUS_V(NT_STATUS_INTERNAL_ERROR));

	p = bytes + num_bytes;
	status = smb_bytes_pull_str(NULL, &str, use_unicode,
				    bytes, num_bytes,
				    p, &ret);
	assert_true(NT_STATUS_IS_OK(status));
	assert_string_equal(str, "");
	assert_int_equal(ret, 0x00);
	TALLOC_FREE(str);

	p = bytes + num_bytes - 1;
	status = smb_bytes_pull_str(NULL, &str, use_unicode,
				    bytes, num_bytes,
				    p, &ret);
	assert_true(NT_STATUS_IS_OK(status));
	assert_string_equal(str, "");
	assert_int_equal(ret, 0x01);
	TALLOC_FREE(str);

	/* Warning: array subscript is above array bounds */
	p = (const uint8_t *)(bytes_address + num_bytes + 1);
	status = smb_bytes_pull_str(NULL, &str, use_unicode,
				    bytes, num_bytes,
				    p, &ret);
	assert_int_equal(NT_STATUS_V(status),
			 NT_STATUS_V(NT_STATUS_BUFFER_TOO_SMALL));
}

int main(void)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(test_smb_bytes_pull_str),
		cmocka_unit_test(test_smb_bytes_pull_str_no_unicode),
		cmocka_unit_test(test_smb_bytes_pull_str_wrong_offset),
		cmocka_unit_test(test_smb_bytes_pull_str_invalid_offset),
	};

	cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
	return cmocka_run_group_tests(tests, NULL, NULL);
}