LCOV - code coverage report
Current view: top level - source4/torture/smb2 - mangle.c (source / functions) Hit Total Coverage
Test: coverage report for master 6248eab5 Lines: 78 104 75.0 %
Date: 2021-08-25 13:27:56 Functions: 3 3 100.0 %

          Line data    Source code
       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          40 : 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          40 :         struct smb2_create io = {0};
      48             : 
      49          40 :         total++;
      50             : 
      51          40 :         io.in.fname = name;
      52          40 :         io.in.desired_access = SEC_FILE_READ_DATA | SEC_FILE_WRITE_DATA |
      53             :                                SEC_FILE_EXECUTE;
      54          40 :         io.in.create_disposition = NTCREATEX_DISP_CREATE;
      55          40 :         io.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
      56             :                              NTCREATEX_SHARE_ACCESS_WRITE |
      57             :                              NTCREATEX_SHARE_ACCESS_DELETE;
      58          40 :         io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
      59          40 :         status = smb2_create(tree, tree, &io);
      60          40 :         if (!NT_STATUS_IS_OK(status)) {
      61           0 :                 torture_comment(tctx, "open of %s failed (%s)\n", name,
      62             :                                 nt_errstr(status));
      63           0 :                 return false;
      64             :         }
      65          40 :         fnum = io.out.file.handle;
      66             : 
      67          40 :         status = smb2_util_close(tree, fnum);
      68          40 :         if (NT_STATUS_IS_ERR(status)) {
      69           0 :                 torture_comment(tctx, "close of %s failed (%s)\n", name,
      70             :                                 nt_errstr(status));
      71           0 :                 return false;
      72             :         }
      73             : 
      74             :         /* get the short name */
      75          40 :         status = smb2_qpathinfo_alt_name(tctx, tree, name, &shortname);
      76          40 :         if (!NT_STATUS_IS_OK(status)) {
      77           0 :                 torture_comment(tctx, "query altname of %s failed (%s)\n",
      78             :                                 name, nt_errstr(status));
      79           0 :                 return false;
      80             :         }
      81             : 
      82          40 :         name2 = talloc_asprintf(tctx, "mangle_test\\%s", shortname);
      83          40 :         status = smb2_util_unlink(tree, name2);
      84          40 :         if (NT_STATUS_IS_ERR(status)) {
      85           0 :                 torture_comment(tctx, "unlink of %s  (%s) failed (%s)\n",
      86             :                        name2, name, nt_errstr(status));
      87           0 :                 return false;
      88             :         }
      89             : 
      90             :         /* recreate by short name */
      91          40 :         io = (struct smb2_create){0};
      92          40 :         io.in.fname = name2;
      93          40 :         io.in.desired_access = SEC_FILE_READ_DATA | SEC_FILE_WRITE_DATA |
      94             :                                SEC_FILE_EXECUTE;
      95          40 :         io.in.create_disposition = NTCREATEX_DISP_CREATE;
      96          40 :         io.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
      97             :                              NTCREATEX_SHARE_ACCESS_WRITE |
      98             :                              NTCREATEX_SHARE_ACCESS_DELETE;
      99          40 :         io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
     100          40 :         status = smb2_create(tree, tree, &io);
     101          40 :         if (!NT_STATUS_IS_OK(status)) {
     102           0 :                 torture_comment(tctx, "open2 of %s failed (%s)\n", name2,
     103             :                                 nt_errstr(status));
     104           0 :                 return false;
     105             :         }
     106          40 :         fnum = io.out.file.handle;
     107             : 
     108          40 :         status = smb2_util_close(tree, fnum);
     109          40 :         if (NT_STATUS_IS_ERR(status)) {
     110           0 :                 torture_comment(tctx, "close of %s failed (%s)\n", name,
     111             :                                 nt_errstr(status));
     112           0 :                 return false;
     113             :         }
     114             : 
     115             :         /* and unlink by long name */
     116          40 :         status = smb2_util_unlink(tree, name);
     117          40 :         if (NT_STATUS_IS_ERR(status)) {
     118           0 :                 torture_comment(tctx, "unlink2 of %s  (%s) failed (%s)\n",
     119             :                                 name, name2, nt_errstr(status));
     120           0 :                 failures++;
     121           0 :                 smb2_util_unlink(tree, name2);
     122           0 :                 return true;
     123             :         }
     124             : 
     125             :         /* see if the short name is already in the tdb */
     126          40 :         data = tdb_fetch_bystring(tdb, shortname);
     127          40 :         if (data.dptr) {
     128             :                 /* maybe its a duplicate long name? */
     129           0 :                 if (strcasecmp(name, (const char *)data.dptr) != 0) {
     130             :                         /* we have a collision */
     131           0 :                         collisions++;
     132           0 :                         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           0 :                 free(data.dptr);
     138             :         } else {
     139             :                 TDB_DATA namedata;
     140             :                 /* store it for later */
     141          40 :                 namedata.dptr = discard_const_p(uint8_t, name);
     142          40 :                 namedata.dsize = strlen(name)+1;
     143          40 :                 tdb_store_bystring(tdb, shortname, namedata, TDB_REPLACE);
     144             :         }
     145             : 
     146          40 :         return true;
     147             : }
     148             : 
     149             : 
     150          40 : static char *gen_name(struct torture_context *tctx)
     151             : {
     152          40 :         const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~...";
     153          40 :         unsigned int max_idx = strlen(chars);
     154             :         unsigned int len;
     155             :         int i;
     156          40 :         char *p = NULL;
     157          40 :         char *name = NULL;
     158             : 
     159          40 :         name = talloc_strdup(tctx, "mangle_test\\");
     160          40 :         if (!name) {
     161           0 :                 return NULL;
     162             :         }
     163             : 
     164          40 :         len = 1 + random() % NAME_LENGTH;
     165             : 
     166          40 :         name = talloc_realloc(tctx, name, char, strlen(name) + len + 6);
     167          40 :         if (!name) {
     168           0 :                 return NULL;
     169             :         }
     170          40 :         p = name + strlen(name);
     171             : 
     172         442 :         for (i=0;i<len;i++) {
     173         402 :                 p[i] = chars[random() % max_idx];
     174             :         }
     175             : 
     176          40 :         p[i] = 0;
     177             : 
     178          40 :         if (ISDOT(p) || ISDOTDOT(p)) {
     179           0 :                 p[0] = '_';
     180             :         }
     181             : 
     182             :         /* have a high probability of a common lead char */
     183          40 :         if (random() % 2 == 0) {
     184          21 :                 p[0] = 'A';
     185             :         }
     186             : 
     187             :         /* and a medium probability of a common lead string */
     188          40 :         if ((len > 5) && (random() % 10 == 0)) {
     189           1 :                 strlcpy(p, "ABCDE", 6);
     190             :         }
     191             : 
     192             :         /* and a high probability of a good extension length */
     193          40 :         if (random() % 2 == 0) {
     194          22 :                 char *s = strrchr(p, '.');
     195          22 :                 if (s) {
     196          10 :                         s[4] = 0;
     197             :                 }
     198             :         }
     199             : 
     200          40 :         return name;
     201             : }
     202             : 
     203             : 
     204           4 : 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           4 :         tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0);
     214           4 :         torture_assert(torture, tdb, "ERROR: Failed to open tdb\n");
     215             : 
     216           4 :         ok = smb2_util_setup_dir(torture, tree, "mangle_test");
     217           4 :         torture_assert(torture, ok, "smb2_util_setup_dir failed\n");
     218             : 
     219          44 :         for (i=0;i<torture_numops;i++) {
     220             :                 char *name;
     221             : 
     222          40 :                 name = gen_name(torture);
     223          40 :                 torture_assert(torture, name, "Name allocation failed\n");
     224             : 
     225          40 :                 ok = test_one(torture, tree, name);
     226          40 :                 torture_assert(torture, ok, talloc_asprintf(torture,
     227             :                                "Mangle names failed with %s", name));
     228          40 :                 if (total && total % 100 == 0) {
     229           0 :                         if (torture_setting_bool(torture, "progress", true)) {
     230           0 :                                 torture_comment(torture,
     231             :                                        "collisions %u/%u  - %.2f%%   (%u failures)\r",
     232           0 :                                        collisions, total, (100.0*collisions) / total, failures);
     233             :                         }
     234             :                 }
     235             :         }
     236             : 
     237           4 :         smb2_util_unlink(tree, "mangle_test\\*");
     238           4 :         status = smb2_util_rmdir(tree, "mangle_test");
     239           4 :         torture_assert_ntstatus_ok(torture, status,
     240             :                                    "ERROR: Failed to remove directory\n");
     241             : 
     242           8 :         torture_comment(torture,
     243             :                         "\nTotal collisions %u/%u  - %.2f%%   (%u failures)\n",
     244           4 :                         collisions, total, (100.0*collisions) / total, failures);
     245             : 
     246           4 :         return (failures == 0);
     247             : }

Generated by: LCOV version 1.13