| 1 |
/* |
| 2 |
Unix SMB/CIFS implementation. |
| 3 |
SMB torture tester - mangling test |
| 4 |
Copyright (C) Andrew Tridgell 2002 |
| 5 |
Copyright (C) David Mulder 2019 |
| 6 |
|
| 7 |
This program is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "includes.h" |
| 22 |
#include "system/filesys.h" |
| 23 |
#include "system/dir.h" |
| 24 |
#include <tdb.h> |
| 25 |
#include "../lib/util/util_tdb.h" |
| 26 |
#include "libcli/smb2/smb2.h" |
| 27 |
#include "libcli/smb2/smb2_calls.h" |
| 28 |
#include "torture/util.h" |
| 29 |
#include "torture/smb2/proto.h" |
| 30 |
|
| 31 |
#undef strcasecmp |
| 32 |
|
| 33 |
static TDB_CONTEXT *tdb; |
| 34 |
|
| 35 |
#define NAME_LENGTH 20 |
| 36 |
|
| 37 |
static unsigned int total, collisions, failures; |
| 38 |
|
| 39 |
static bool test_one(struct torture_context *tctx, struct smb2_tree *tree, |
| 40 |
const char *name) |
| 41 |
{ |
| 42 |
struct smb2_handle fnum; |
| 43 |
const char *shortname; |
| 44 |
const char *name2; |
| 45 |
NTSTATUS status; |
| 46 |
TDB_DATA data; |
| 47 |
struct smb2_create io = {0}; |
| 48 |
|
| 49 |
total++; |
| 50 |
|
| 51 |
io.in.fname = name; |
| 52 |
io.in.desired_access = SEC_FILE_READ_DATA | SEC_FILE_WRITE_DATA | |
| 53 |
SEC_FILE_EXECUTE; |
| 54 |
io.in.create_disposition = NTCREATEX_DISP_CREATE; |
| 55 |
io.in.share_access = NTCREATEX_SHARE_ACCESS_READ | |
| 56 |
NTCREATEX_SHARE_ACCESS_WRITE | |
| 57 |
NTCREATEX_SHARE_ACCESS_DELETE; |
| 58 |
io.in.file_attributes = FILE_ATTRIBUTE_NORMAL; |
| 59 |
status = smb2_create(tree, tree, &io); |
| 60 |
if (!NT_STATUS_IS_OK(status)) { |
| 61 |
torture_comment(tctx, "open of %s failed (%s)\n", name, |
| 62 |
nt_errstr(status)); |
| 63 |
return false; |
| 64 |
} |
| 65 |
fnum = io.out.file.handle; |
| 66 |
|
| 67 |
status = smb2_util_close(tree, fnum); |
| 68 |
if (NT_STATUS_IS_ERR(status)) { |
| 69 |
torture_comment(tctx, "close of %s failed (%s)\n", name, |
| 70 |
nt_errstr(status)); |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
/* get the short name */ |
| 75 |
status = smb2_qpathinfo_alt_name(tctx, tree, name, &shortname); |
| 76 |
if (!NT_STATUS_IS_OK(status)) { |
| 77 |
torture_comment(tctx, "query altname of %s failed (%s)\n", |
| 78 |
name, nt_errstr(status)); |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
name2 = talloc_asprintf(tctx, "mangle_test\\%s", shortname); |
| 83 |
status = smb2_util_unlink(tree, name2); |
| 84 |
if (NT_STATUS_IS_ERR(status)) { |
| 85 |
torture_comment(tctx, "unlink of %s (%s) failed (%s)\n", |
| 86 |
name2, name, nt_errstr(status)); |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
/* recreate by short name */ |
| 91 |
io = (struct smb2_create){0}; |
| 92 |
io.in.fname = name2; |
| 93 |
io.in.desired_access = SEC_FILE_READ_DATA | SEC_FILE_WRITE_DATA | |
| 94 |
SEC_FILE_EXECUTE; |
| 95 |
io.in.create_disposition = NTCREATEX_DISP_CREATE; |
| 96 |
io.in.share_access = NTCREATEX_SHARE_ACCESS_READ | |
| 97 |
NTCREATEX_SHARE_ACCESS_WRITE | |
| 98 |
NTCREATEX_SHARE_ACCESS_DELETE; |
| 99 |
io.in.file_attributes = FILE_ATTRIBUTE_NORMAL; |
| 100 |
status = smb2_create(tree, tree, &io); |
| 101 |
if (!NT_STATUS_IS_OK(status)) { |
| 102 |
torture_comment(tctx, "open2 of %s failed (%s)\n", name2, |
| 103 |
nt_errstr(status)); |
| 104 |
return false; |
| 105 |
} |
| 106 |
fnum = io.out.file.handle; |
| 107 |
|
| 108 |
status = smb2_util_close(tree, fnum); |
| 109 |
if (NT_STATUS_IS_ERR(status)) { |
| 110 |
torture_comment(tctx, "close of %s failed (%s)\n", name, |
| 111 |
nt_errstr(status)); |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
/* and unlink by long name */ |
| 116 |
status = smb2_util_unlink(tree, name); |
| 117 |
if (NT_STATUS_IS_ERR(status)) { |
| 118 |
torture_comment(tctx, "unlink2 of %s (%s) failed (%s)\n", |
| 119 |
name, name2, nt_errstr(status)); |
| 120 |
failures++; |
| 121 |
smb2_util_unlink(tree, name2); |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
/* see if the short name is already in the tdb */ |
| 126 |
data = tdb_fetch_bystring(tdb, shortname); |
| 127 |
if (data.dptr) { |
| 128 |
/* maybe its a duplicate long name? */ |
| 129 |
if (strcasecmp(name, (const char *)data.dptr) != 0) { |
| 130 |
/* we have a collision */ |
| 131 |
collisions++; |
| 132 |
torture_comment(tctx, "Collision between %s and %s" |
| 133 |
" -> %s (coll/tot: %u/%u)\n", |
| 134 |
name, data.dptr, shortname, collisions, |
| 135 |
total); |
| 136 |
} |
| 137 |
free(data.dptr); |
| 138 |
} else { |
| 139 |
TDB_DATA namedata; |
| 140 |
/* store it for later */ |
| 141 |
namedata.dptr = discard_const_p(uint8_t, name); |
| 142 |
namedata.dsize = strlen(name)+1; |
| 143 |
tdb_store_bystring(tdb, shortname, namedata, TDB_REPLACE); |
| 144 |
} |
| 145 |
|
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
static char *gen_name(struct torture_context *tctx) |
| 151 |
{ |
| 152 |
const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~..."; |
| 153 |
unsigned int max_idx = strlen(chars); |
| 154 |
unsigned int len; |
| 155 |
int i; |
| 156 |
char *p = NULL; |
| 157 |
char *name = NULL; |
| 158 |
|
| 159 |
name = talloc_strdup(tctx, "mangle_test\\"); |
| 160 |
if (!name) { |
| 161 |
return NULL; |
| 162 |
} |
| 163 |
|
| 164 |
len = 1 + random() % NAME_LENGTH; |
| 165 |
|
| 166 |
name = talloc_realloc(tctx, name, char, strlen(name) + len + 6); |
| 167 |
if (!name) { |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
p = name + strlen(name); |
| 171 |
|
| 172 |
for (i=0;i<len;i++) { |
| 173 |
p[i] = chars[random() % max_idx]; |
| 174 |
} |
| 175 |
|
| 176 |
p[i] = 0; |
| 177 |
|
| 178 |
if (ISDOT(p) || ISDOTDOT(p)) { |
| 179 |
p[0] = '_'; |
| 180 |
} |
| 181 |
|
| 182 |
/* have a high probability of a common lead char */ |
| 183 |
if (random() % 2 == 0) { |
| 184 |
p[0] = 'A'; |
| 185 |
} |
| 186 |
|
| 187 |
/* and a medium probability of a common lead string */ |
| 188 |
if ((len > 5) && (random() % 10 == 0)) { |
| 189 |
strlcpy(p, "ABCDE", 6); |
| 190 |
} |
| 191 |
|
| 192 |
/* and a high probability of a good extension length */ |
| 193 |
if (random() % 2 == 0) { |
| 194 |
char *s = strrchr(p, '.'); |
| 195 |
if (s) { |
| 196 |
s[4] = 0; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return name; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
static bool torture_smb2_mangle(struct torture_context *torture, |
| 205 |
struct smb2_tree *tree) |
| 206 |
{ |
| 207 |
extern int torture_numops; |
| 208 |
int i; |
| 209 |
bool ok; |
| 210 |
NTSTATUS status; |
| 211 |
|
| 212 |
/* we will use an internal tdb to store the names we have used */ |
| 213 |
tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0); |
| 214 |
torture_assert(torture, tdb, "ERROR: Failed to open tdb\n"); |
| 215 |
|
| 216 |
ok = smb2_util_setup_dir(torture, tree, "mangle_test"); |
| 217 |
torture_assert(torture, ok, "smb2_util_setup_dir failed\n"); |
| 218 |
|
| 219 |
for (i=0;i<torture_numops;i++) { |
| 220 |
char *name; |
| 221 |
|
| 222 |
name = gen_name(torture); |
| 223 |
torture_assert(torture, name, "Name allocation failed\n"); |
| 224 |
|
| 225 |
ok = test_one(torture, tree, name); |
| 226 |
torture_assert(torture, ok, talloc_asprintf(torture, |
| 227 |
"Mangle names failed with %s", name)); |
| 228 |
if (total && total % 100 == 0) { |
| 229 |
if (torture_setting_bool(torture, "progress", true)) { |
| 230 |
torture_comment(torture, |
| 231 |
"collisions %u/%u - %.2f%% (%u failures)\r", |
| 232 |
collisions, total, (100.0*collisions) / total, failures); |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
smb2_util_unlink(tree, "mangle_test\\*"); |
| 238 |
status = smb2_util_rmdir(tree, "mangle_test"); |
| 239 |
torture_assert_ntstatus_ok(torture, status, |
| 240 |
"ERROR: Failed to remove directory\n"); |
| 241 |
|
| 242 |
torture_comment(torture, |
| 243 |
"\nTotal collisions %u/%u - %.2f%% (%u failures)\n", |
| 244 |
collisions, total, (100.0*collisions) / total, failures); |
| 245 |
|
| 246 |
return (failures == 0); |
| 247 |
} |
| 248 |
|
| 249 |
static bool test_mangled_mask(struct torture_context *tctx, |
| 250 |
struct smb2_tree *tree) |
| 251 |
{ |
| 252 |
bool ret = true; |
| 253 |
const char *dname = "single_find_with_mangled_name"; |
| 254 |
const char *fname = "single_find_with_mangled_name\\verylongfilename"; |
| 255 |
const char *shortname = NULL; |
| 256 |
NTSTATUS status; |
| 257 |
struct smb2_handle dh = {{0}}; |
| 258 |
struct smb2_handle fh = {{0}}; |
| 259 |
struct smb2_find f; |
| 260 |
union smb_search_data *d; |
| 261 |
unsigned count, i; |
| 262 |
|
| 263 |
torture_comment(tctx, "Checking find with mangled search mask\n"); |
| 264 |
|
| 265 |
smb2_deltree(tree, dname); |
| 266 |
|
| 267 |
/* Create dname and fname */ |
| 268 |
status = torture_smb2_testdir(tree, dname, &dh); |
| 269 |
torture_assert_ntstatus_ok_goto(tctx, status, ret, done, |
| 270 |
"torture_smb2_testdir failed"); |
| 271 |
|
| 272 |
status = torture_smb2_testfile(tree, fname, &fh); |
| 273 |
smb2_util_close(tree, fh); |
| 274 |
|
| 275 |
smb2_util_close(tree, dh); |
| 276 |
|
| 277 |
/* Update the directory handle, that readdir() can see the testfile */ |
| 278 |
status = torture_smb2_testdir(tree, dname, &dh); |
| 279 |
torture_assert_ntstatus_ok_goto(tctx, status, ret, done, |
| 280 |
"torture_smb2_testdir failed"); |
| 281 |
|
| 282 |
ZERO_STRUCT(f); |
| 283 |
f.in.file.handle = dh; |
| 284 |
f.in.pattern = "*"; |
| 285 |
f.in.max_response_size = 0x1000; |
| 286 |
f.in.level = SMB2_FIND_BOTH_DIRECTORY_INFO; |
| 287 |
|
| 288 |
status = smb2_find_level(tree, tree, &f, &count, &d); |
| 289 |
torture_assert_ntstatus_ok_goto(tctx, status, ret, done, |
| 290 |
"smb2_find_level failed\n"); |
| 291 |
smb2_util_close(tree, dh); |
| 292 |
ZERO_STRUCT(dh); |
| 293 |
|
| 294 |
for (i = 0; i < count; i++) { |
| 295 |
const char *found = d[i].both_directory_info.name.s; |
| 296 |
|
| 297 |
if (!strcmp(found, ".") || !strcmp(found, "..")) { |
| 298 |
continue; |
| 299 |
} |
| 300 |
|
| 301 |
torture_assert_str_equal_goto(tctx, found, "verylongfilename", ret, done, |
| 302 |
"Bad filename\n"); |
| 303 |
shortname = d[i].both_directory_info.short_name.s; |
| 304 |
break; |
| 305 |
} |
| 306 |
|
| 307 |
torture_assert_not_null_goto(tctx, shortname, ret, done, |
| 308 |
"shortname is NULL\n"); |
| 309 |
|
| 310 |
torture_comment(tctx, "Got shortname: %s\n", shortname); |
| 311 |
|
| 312 |
status = torture_smb2_testdir(tree, dname, &dh); |
| 313 |
|
| 314 |
ZERO_STRUCT(f); |
| 315 |
f.in.file.handle = dh; |
| 316 |
f.in.continue_flags = SMB2_CONTINUE_FLAG_SINGLE; |
| 317 |
f.in.pattern = shortname; |
| 318 |
f.in.max_response_size = 0x1000; |
| 319 |
f.in.level = SMB2_FIND_BOTH_DIRECTORY_INFO; |
| 320 |
|
| 321 |
status = smb2_find_level(tree, tree, &f, &count, &d); |
| 322 |
torture_assert_ntstatus_ok_goto(tctx, status, ret, done, |
| 323 |
"smb2_find_level failed\n"); |
| 324 |
smb2_util_close(tree, dh); |
| 325 |
ZERO_STRUCT(dh); |
| 326 |
|
| 327 |
done: |
| 328 |
if (!smb2_util_handle_empty(fh)) { |
| 329 |
smb2_util_close(tree, fh); |
| 330 |
} |
| 331 |
if (!smb2_util_handle_empty(dh)) { |
| 332 |
smb2_util_close(tree, dh); |
| 333 |
} |
| 334 |
smb2_deltree(tree, dname); |
| 335 |
return ret; |
| 336 |
|
| 337 |
} |
| 338 |
|
| 339 |
struct torture_suite *torture_smb2_name_mangling_init(TALLOC_CTX *ctx) |
| 340 |
{ |
| 341 |
struct torture_suite *suite = NULL; |
| 342 |
|
| 343 |
suite = torture_suite_create(ctx, "name-mangling"); |
| 344 |
suite->description = talloc_strdup(suite, "SMB2 name mangling tests"); |
| 345 |
|
| 346 |
torture_suite_add_1smb2_test(suite, "mangle", torture_smb2_mangle); |
| 347 |
torture_suite_add_1smb2_test(suite, "mangled-mask", test_mangled_mask); |
| 348 |
return suite; |
| 349 |
} |