pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS


workaround for wintar zero bytes in text files bug
Posted by Anonymous on Wed 18th Dec 2024 10:13
raw | new post
modification of post by Anonymous (view diff)

  1. diff --git a/build.vc19/nfs41_driver/nfs41_driver.vcxproj b/build.vc19/nfs41_driver/nfs41_driver.vcxproj
  2. index 0affe23..9c55630 100644
  3. --- a/build.vc19/nfs41_driver/nfs41_driver.vcxproj
  4. +++ b/build.vc19/nfs41_driver/nfs41_driver.vcxproj
  5. @@ -144,7 +144,7 @@
  6.        <AdditionalOptions>/std:c17 /Zc:preprocessor- /kernel /wd4100 /wd4201 /wd5104</AdditionalOptions>
  7.      </ClCompile>
  8.      <Link>
  9. -      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib</AdditionalDependencies>
  10. +      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib;$(DDK_LIB_PATH)\tdi.lib</AdditionalDependencies>
  11.      </Link>
  12.    </ItemDefinitionGroup>
  13.    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
  14. @@ -158,7 +158,7 @@
  15.        <AdditionalOptions>/std:c17 /Zc:preprocessor- /kernel /wd4100 /wd4201 /wd5104</AdditionalOptions>
  16.      </ClCompile>
  17.      <Link>
  18. -      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib</AdditionalDependencies>
  19. +      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib;$(DDK_LIB_PATH)\tdi.lib</AdditionalDependencies>
  20.      </Link>
  21.    </ItemDefinitionGroup>
  22.    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
  23. @@ -172,7 +172,7 @@
  24.        <AdditionalOptions>/std:c17 /Zc:preprocessor- /kernel /wd4100 /wd4201 /wd5104</AdditionalOptions>
  25.      </ClCompile>
  26.      <Link>
  27. -      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib</AdditionalDependencies>
  28. +      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib;$(DDK_LIB_PATH)\tdi.lib</AdditionalDependencies>
  29.      </Link>
  30.      <DriverSign>
  31.        <FileDigestAlgorithm>sha256</FileDigestAlgorithm>
  32. @@ -189,7 +189,7 @@
  33.        <AdditionalOptions>/std:c17 /Zc:preprocessor- /kernel /wd4100 /wd4201 /wd5104</AdditionalOptions>
  34.      </ClCompile>
  35.      <Link>
  36. -      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib</AdditionalDependencies>
  37. +      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib;$(DDK_LIB_PATH)\tdi.lib</AdditionalDependencies>
  38.      </Link>
  39.      <DriverSign>
  40.        <FileDigestAlgorithm>sha256</FileDigestAlgorithm>
  41. @@ -206,7 +206,7 @@
  42.        <AdditionalOptions>/std:c17 /Zc:preprocessor- /kernel /wd4100 /wd4201 /wd5104</AdditionalOptions>
  43.      </ClCompile>
  44.      <Link>
  45. -      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib</AdditionalDependencies>
  46. +      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib;$(DDK_LIB_PATH)\tdi.lib</AdditionalDependencies>
  47.      </Link>
  48.    </ItemDefinitionGroup>
  49.    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">
  50. @@ -220,7 +220,7 @@
  51.        <AdditionalOptions>/std:c17 /Zc:preprocessor- /kernel /wd4100 /wd4201 /wd5104</AdditionalOptions>
  52.      </ClCompile>
  53.      <Link>
  54. -      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib</AdditionalDependencies>
  55. +      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib;$(DDK_LIB_PATH)\tdi.lib</AdditionalDependencies>
  56.      </Link>
  57.    </ItemDefinitionGroup>
  58.    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
  59. @@ -234,7 +234,7 @@
  60.        <AdditionalOptions>/std:c17 /Zc:preprocessor- /kernel /wd4100 /wd4201 /wd5104</AdditionalOptions>
  61.      </ClCompile>
  62.      <Link>
  63. -      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib</AdditionalDependencies>
  64. +      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib;$(DDK_LIB_PATH)\tdi.lib</AdditionalDependencies>
  65.      </Link>
  66.      <DriverSign>
  67.        <FileDigestAlgorithm>sha256</FileDigestAlgorithm>
  68. @@ -251,7 +251,7 @@
  69.        <AdditionalOptions>/std:c17 /Zc:preprocessor- /kernel /wd4100 /wd4201 /wd5104</AdditionalOptions>
  70.      </ClCompile>
  71.      <Link>
  72. -      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib</AdditionalDependencies>
  73. +      <AdditionalDependencies>$(DDK_LIB_PATH)\ntoskrnl.lib;$(DDK_LIB_PATH)\hal.lib;$(DDK_LIB_PATH)\BufferOverflowfastfailK.lib;$(DDK_LIB_PATH)\ksecdd.lib;$(DDK_LIB_PATH)\rxce.lib;$(DDK_LIB_PATH)\rdbsslib.lib;$(DDK_LIB_PATH)\tdi.lib</AdditionalDependencies>
  74.      </Link>
  75.      <DriverSign>
  76.        <FileDigestAlgorithm>sha256</FileDigestAlgorithm>
  77. diff --git a/sys/nfs41sys_driver.h b/sys/nfs41sys_driver.h
  78. index 1c39ab6..0e0107c 100644
  79. --- a/sys/nfs41sys_driver.h
  80. +++ b/sys/nfs41sys_driver.h
  81. @@ -48,8 +48,10 @@
  82.      (POOL_FLAG_UNINITIALIZED|POOL_FLAG_CACHE_ALIGNED)
  83.  
  84.  #define RxAllocatePoolWithTag(rxallocpool, numbytes, tag) \
  85. -    ExAllocatePool2(((((rxallocpool) == NonPagedPoolNx)? \
  86. -            POOL_FLAG_NON_PAGED:POOL_FLAG_NON_PAGED_EXECUTE) | \
  87. +    ExAllocatePool2((( \
  88. +            ((rxallocpool) == PagedPool)?POOL_FLAG_PAGED: \
  89. +                (((rxallocpool) == NonPagedPoolNx)? \
  90. +                    POOL_FLAG_NON_PAGED:POOL_FLAG_NON_PAGED_EXECUTE)) | \
  91.              RXALLOCATEPOOL_DEFAULT_ALLOCATEPOOL2FLAGS), \
  92.          (numbytes), (tag))
  93.  #endif /* EXALLOCATEPOOLWITHTAG_DEPRECATED */
  94. diff --git a/sys/nfs41sys_readwrite.c b/sys/nfs41sys_readwrite.c
  95. index 050819c..a70ead2 100644
  96. --- a/sys/nfs41sys_readwrite.c
  97. +++ b/sys/nfs41sys_readwrite.c
  98. @@ -58,6 +58,8 @@
  99.  #include <winerror.h>
  100.  
  101.  #include <Ntstrsafe.h>
  102. +#include <stdbool.h>
  103. +#include <tdikrnl.h>
  104.  
  105.  #include "nfs41sys_buildconfig.h"
  106.  
  107. @@ -333,7 +335,7 @@ NTSTATUS nfs41_Write(
  108.      IN OUT PRX_CONTEXT RxContext)
  109.  {
  110.      NTSTATUS status = STATUS_INSUFFICIENT_RESOURCES;
  111. -    nfs41_updowncall_entry *entry;
  112. +    nfs41_updowncall_entry *entry = NULL;
  113.      BOOLEAN async = FALSE;
  114.      PLOWIO_CONTEXT LowIoContext  = &RxContext->LowIoContext;
  115.      __notnull PMRX_SRV_OPEN SrvOpen = RxContext->pRelevantSrvOpen;
  116. @@ -344,6 +346,11 @@ NTSTATUS nfs41_Write(
  117.      __notnull PNFS41_FCB nfs41_fcb = NFS41GetFcbExtension(RxContext->pFcb);
  118.      __notnull PNFS41_FOBX nfs41_fobx = NFS41GetFobxExtension(RxContext->pFobx);
  119.      DWORD io_delay;
  120. +#if 1
  121. +    void *userbuffer_mem = NULL;
  122. +    PMDL userbuffer_mdl = NULL;
  123. +//    PVOID UserBuffer = NULL;
  124. +#endif
  125.  #ifdef ENABLE_TIMINGS
  126.      LARGE_INTEGER t1, t2;
  127.      t1 = KeQueryPerformanceCounter(NULL);
  128. @@ -362,9 +369,151 @@ NTSTATUS nfs41_Write(
  129.          pNetRootContext->nfs41d_version, SrvOpen->pAlreadyPrefixedName, &entry);
  130.      if (status) goto out;
  131.  
  132. +#if 1
  133. +    ULONG padded_readwrite_bytecount = LowIoContext->ParamsFor.ReadWrite.ByteCount + (8192) /* fixme */;
  134. +
  135. +#if 1
  136. +    userbuffer_mem = ExAllocatePool2(POOL_FLAG_PAGED, padded_readwrite_bytecount, NFS41_MM_POOLTAG_DOWN);
  137. +#else
  138. +    userbuffer_mem = RxAllocatePoolWithTag(NonPagedPoolNx,
  139. +        padded_readwrite_bytecount,
  140. +        NFS41_MM_POOLTAG_DOWN);
  141. +#endif
  142. +    if (userbuffer_mem == NULL) {
  143. +        status = STATUS_NO_MEMORY;
  144. +        goto out;
  145. +    }
  146. +
  147. +    RtlZeroMemory(userbuffer_mem, padded_readwrite_bytecount);
  148. +#if 0
  149. +    UserBuffer = RxLowIoGetBufferAddress(RxContext);
  150. +    if (UserBuffer == NULL) {
  151. +        status = STATUS_NO_MEMORY;
  152. +        goto out;
  153. +    }
  154. +#endif
  155. +    userbuffer_mdl = IoAllocateMdl(userbuffer_mem,
  156. +        padded_readwrite_bytecount,
  157. +        FALSE, FALSE, NULL);
  158. +    if (userbuffer_mdl == NULL) {
  159. +        status = STATUS_NO_MEMORY;
  160. +        goto out;
  161. +    }
  162. +
  163. +#if 1
  164. +#pragma warning( push )
  165. +/*
  166. + * C28145: "The opaque MDL structure should not be modified by a
  167. + * driver.", |MDL_MAPPING_CAN_FAIL| is the exception
  168. + */
  169. +#pragma warning (disable : 28145)
  170. +        userbuffer_mdl->MdlFlags |= MDL_MAPPING_CAN_FAIL;
  171. +#pragma warning( pop )
  172. +#endif
  173. +
  174. +    MmProbeAndLockPages(userbuffer_mdl, KernelMode, IoModifyAccess);
  175. +
  176. +#if 1
  177. +
  178. +#if 1
  179. +    PVOID Src;
  180. +
  181. +    if ((Src = MmGetSystemAddressForMdlSafe(LowIoContext->ParamsFor.ReadWrite.Buffer, NormalPagePriority)) == NULL) {
  182. +        return STATUS_INSUFFICIENT_RESOURCES;
  183. +    }
  184. +
  185. +    ULONG mdl_bytecount = MmGetMdlByteCount(LowIoContext->ParamsFor.ReadWrite.Buffer);
  186. +
  187. +    if (mdl_bytecount < LowIoContext->ParamsFor.ReadWrite.ByteCount) {
  188. +        DbgP("#### ERROR: mdl_bytecount(=%lld) < LowIoContext->ParamsFor.ReadWrite.ByteCount(=%lld)\n",
  189. +            (long long)mdl_bytecount,
  190. +            (long long)LowIoContext->ParamsFor.ReadWrite.ByteCount);
  191. +        DbgP("ReadWrite.Buffer=(Next=0x%p, Size=%ld, MdlFlags=0x%lx, "
  192. +            "Process=0x%p, MappedSystemVa=0x%p, StartVa=0x%p, "
  193. +            "ByteCount=%ld, ByteOffset=%ld)\n",
  194. +            (void *)LowIoContext->ParamsFor.ReadWrite.Buffer->Next,
  195. +            (long)LowIoContext->ParamsFor.ReadWrite.Buffer->Size,
  196. +            (long)LowIoContext->ParamsFor.ReadWrite.Buffer->MdlFlags,
  197. +            (void *)LowIoContext->ParamsFor.ReadWrite.Buffer->Process,
  198. +            (void *)LowIoContext->ParamsFor.ReadWrite.Buffer->MappedSystemVa,
  199. +            (void *)LowIoContext->ParamsFor.ReadWrite.Buffer->StartVa,
  200. +            (long)LowIoContext->ParamsFor.ReadWrite.Buffer->ByteCount,
  201. +            (long)LowIoContext->ParamsFor.ReadWrite.Buffer->ByteOffset);        
  202. +        return STATUS_INTERNAL_ERROR;
  203. +    }
  204. +
  205. +#if 1
  206. +    volatile char *cp_dest = userbuffer_mem;
  207. +    volatile char *cp_src = ((char *)Src)+LowIoContext->ParamsFor.ReadWrite.ByteOffset;
  208. +    volatile char tmpbyte;
  209. +
  210. +    long long cp_i;
  211. +
  212. +    long long cp_max = LowIoContext->ParamsFor.ReadWrite.ByteCount;
  213. +
  214. +    if (LowIoContext->ParamsFor.ReadWrite.ByteOffset)
  215. +        cp_max--;
  216. +
  217. +    for (cp_i = 0 ; cp_i < cp_max ; cp_i++) {
  218. +        tmpbyte = *cp_src++;
  219. +        
  220. +        *cp_dest++ = tmpbyte;
  221. +    }
  222. +#else
  223. +    (void)RtlCopyMemory(userbuffer_mem,
  224. +        ((char *)Src)+LowIoContext->ParamsFor.ReadWrite.ByteOffset,
  225. +        LowIoContext->ParamsFor.ReadWrite.ByteCount);
  226. +#endif
  227. +#else
  228. +    ULONG bytescopied = 0UL;
  229. +    NTSTATUS tdi_status;
  230. +
  231. +    tdi_status = TdiCopyMdlToBuffer(LowIoContext->ParamsFor.ReadWrite.Buffer,
  232. +        (ULONG)LowIoContext->ParamsFor.ReadWrite.ByteOffset /* fixme: Add assert LONGLONG2ULONG cast */,
  233. +        userbuffer_mem,
  234. +        0,
  235. +        LowIoContext->ParamsFor.ReadWrite.ByteCount+1,
  236. +        &bytescopied);
  237. +
  238. +    if (((tdi_status != STATUS_SUCCESS) && (tdi_status != STATUS_BUFFER_OVERFLOW)) ||
  239. +        (bytescopied != LowIoContext->ParamsFor.ReadWrite.ByteCount)) {
  240. +        DbgP("#### ERROR: tdi_status=0x%lx || (bytescopied(=%lld) != ReadWrite.ByteCount(=%lld))\n",
  241. +            (long)tdi_status,
  242. +            (long long)bytescopied,
  243. +            (long long)LowIoContext->ParamsFor.ReadWrite.ByteCount);
  244. +        DbgP("ReadWrite.Buffer=(Next=0x%p, Size=%ld, MdlFlags=0x%lx, "
  245. +            "Process=0x%p, MappedSystemVa=0x%p, StartVa=0x%p, "
  246. +            "ByteCount=%ld, ByteOffset=%ld)\n",
  247. +            (void *)LowIoContext->ParamsFor.ReadWrite.Buffer->Next,
  248. +            (long)LowIoContext->ParamsFor.ReadWrite.Buffer->Size,
  249. +            (long)LowIoContext->ParamsFor.ReadWrite.Buffer->MdlFlags,
  250. +            (void *)LowIoContext->ParamsFor.ReadWrite.Buffer->Process,
  251. +            (void *)LowIoContext->ParamsFor.ReadWrite.Buffer->MappedSystemVa,
  252. +            (void *)LowIoContext->ParamsFor.ReadWrite.Buffer->StartVa,
  253. +            (long)LowIoContext->ParamsFor.ReadWrite.Buffer->ByteCount,
  254. +            (long)LowIoContext->ParamsFor.ReadWrite.Buffer->ByteOffset);
  255. +    }
  256. +    if ((bytescopied == 0) && (LowIoContext->ParamsFor.ReadWrite.ByteCount > 0)) {
  257. +        DbgP("requesting STATUS_RETRY\n");
  258. +//        status = STATUS_RETRY;
  259. +//        goto out;
  260. +    }
  261. +    
  262. +#endif
  263. +#else
  264. +    (void)RtlCopyMemory(userbuffer_mem,
  265. +        ((char *)UserBuffer)+LowIoContext->ParamsFor.ReadWrite.ByteOffset,
  266. +        LowIoContext->ParamsFor.ReadWrite.ByteCount);
  267. +#endif
  268. +
  269. +    entry->u.ReadWrite.MdlAddress = userbuffer_mdl;
  270. +    entry->buf_len = LowIoContext->ParamsFor.ReadWrite.ByteCount;
  271. +    entry->u.ReadWrite.offset = 0;
  272. +#else
  273.      entry->u.ReadWrite.MdlAddress = LowIoContext->ParamsFor.ReadWrite.Buffer;
  274.      entry->buf_len = LowIoContext->ParamsFor.ReadWrite.ByteCount;
  275.      entry->u.ReadWrite.offset = LowIoContext->ParamsFor.ReadWrite.ByteOffset;
  276. +#endif
  277.  
  278.      if (FlagOn(RxContext->CurrentIrpSp->FileObject->Flags,
  279.              FO_SYNCHRONOUS_IO) == FALSE) {
  280. @@ -420,6 +569,17 @@ NTSTATUS nfs41_Write(
  281.      }
  282.      nfs41_UpcallDestroy(entry);
  283.  out:
  284. +#if 1
  285. +    if (!async) {
  286. +        if (userbuffer_mdl) {
  287. +            IoFreeMdl(userbuffer_mdl);
  288. +        }
  289. +        if (userbuffer_mem) {
  290. +            RxFreePool(userbuffer_mem);
  291. +        }
  292. +    }
  293. +#endif
  294. +
  295.  #ifdef ENABLE_TIMINGS
  296.      t2 = KeQueryPerformanceCounter(NULL);
  297.      InterlockedIncrement(&write.tops);

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at