Esempio n. 1
0
 public static extern NtStatus NtSetInformationFile(
     SafeKernelObjectHandle FileHandle,
     [Out] IoStatus IoStatusBlock,
     SafeBuffer FileInformation,
     int Length,
     FileInformationClass FileInformationClass
     );
Esempio n. 2
0
        /// <summary>
        /// Wait for the result to complete. This could be waiting on an event
        /// or the file handle.
        /// </summary>
        /// <returns>Returns true if the wait completed successfully.</returns>
        /// <remarks>If true is returned then status and information can be read out.</remarks>
        internal bool WaitForComplete()
        {
            if (_result != null)
            {
                return(true);
            }

            NtStatus status;

            if (_event != null)
            {
                status = _event.Wait(NtWaitTimeout.Infinite).ToNtException();
            }
            else
            {
                status = _object.Wait(NtWaitTimeout.Infinite).ToNtException();
            }

            if (status == NtStatus.STATUS_SUCCESS)
            {
                _result = _io_status.Result;
                return(true);
            }

            return(false);
        }
Esempio n. 3
0
 public static extern NtStatus NtOpenFile(
     out SafeKernelObjectHandle FileHandle,
     FileAccessRights DesiredAccess,
     ObjectAttributes ObjAttr,
     [Out] IoStatus IoStatusBlock,
     FileShareMode ShareAccess,
     FileOpenOptions OpenOptions);
Esempio n. 4
0
        /// <summary>
        /// Send an Device IO Control code to the file driver
        /// </summary>
        /// <param name="control_code">The control code</param>
        /// <param name="input_buffer">Input buffer can be null</param>
        /// <param name="output_buffer">Output buffer can be null</param>
        /// <exception cref="NtException">Thrown on error.</exception>
        public void DeviceIoControl(int control_code, SafeBuffer input_buffer, SafeBuffer output_buffer)
        {
            IoStatus status = new IoStatus();

            NtSystemCalls.NtDeviceIoControlFile(Handle, SafeKernelObjectHandle.Null, IntPtr.Zero, IntPtr.Zero, status,
                                                control_code, GetSafePointer(input_buffer), GetSafeLength(input_buffer), GetSafePointer(output_buffer), GetSafeLength(output_buffer)).ToNtException();
        }
 public static extern NtStatus NtRemoveIoCompletion(
     SafeKernelObjectHandle IoCompletionHandle,
     out IntPtr KeyContext,
     out IntPtr ApcContext,
     [In, Out] IoStatus IoStatusBlock,
     LargeInteger Timeout
     );
Esempio n. 6
0
 /// <summary>
 /// Reset the file result so it can be reused.
 /// </summary>
 internal void Reset()
 {
     _result = null;
     if (_event != null)
     {
         _event.Clear();
     }
 }
Esempio n. 7
0
        /// <summary>
        /// Open a file
        /// </summary>
        /// <param name="obj_attributes">The object attributes</param>
        /// <param name="DesiredAccess">The desired access for the file handle</param>
        /// <param name="ShareAccess">The file share access</param>
        /// <param name="OpenOptions">File open options</param>
        /// <returns>The opened file</returns>
        /// <exception cref="NtException">Thrown on error.</exception>
        public static NtFile Open(ObjectAttributes obj_attributes, FileAccessRights DesiredAccess, FileShareMode ShareAccess, FileOpenOptions OpenOptions)
        {
            SafeKernelObjectHandle handle;
            IoStatus iostatus = new IoStatus();

            NtSystemCalls.NtOpenFile(out handle, DesiredAccess, obj_attributes, iostatus, ShareAccess, OpenOptions).ToNtException();
            return(new NtFile(handle));
        }
        /// <summary>
        /// Remove a queued status from the queue.
        /// </summary>
        /// <param name="timeout">An optional timeout.</param>
        /// <param name="throw_on_error">True to throw on error.</param>
        /// <returns>The completion result.</returns>
        /// <exception cref="NtException">Thrown on error or timeout.</exception>
        public NtResult <FileIoCompletionResult> Remove(NtWaitTimeout timeout, bool throw_on_error)
        {
            var io_status = new IoStatus();

            return(NtSystemCalls.NtRemoveIoCompletion(Handle, out IntPtr key_context,
                                                      out IntPtr apc_context, io_status, timeout.ToLargeInteger())
                   .CreateResult(throw_on_error, () => new FileIoCompletionResult(key_context, apc_context, io_status)));
        }
 internal NtNamedPipeFileBase(SafeKernelObjectHandle handle, IoStatus io_status)
     : base(handle, io_status)
 {
     _pipe_information = new Lazy <NtResult <FilePipeInformation> >(()
                                                                    => Query(FileInformationClass.FilePipeInformation, new FilePipeInformation(), false));
     _pipe_local_information = new Lazy <NtResult <FilePipeLocalInformation> >(()
                                                                               => Query(FileInformationClass.FilePipeLocalInformation, new FilePipeLocalInformation(), false));
 }
 /// <summary>
 /// Cancel the pending IO operation.
 /// </summary>
 internal void Cancel()
 {
     if (_object is NtFile)
     {
         IoStatus io_status = new IoStatus();
         NtSystemCalls.NtCancelIoFileEx(_object.Handle,
                                        _io_status, io_status).ToNtException();
     }
 }
Esempio n. 11
0
 public static extern NtStatus NtCreateMailslotFile(
     out SafeKernelObjectHandle FileHandle,
     FileAccessRights DesiredAccess,
     [In] ObjectAttributes ObjectAttributes,
     [Out] IoStatus IoStatusBlock,
     FileOpenOptions CreateOptions,
     int MailslotQuota,
     int MaximumMessageSize,
     LargeInteger ReadTimeout
     );
 internal FileIoCompletionResult(FileIoCompletionInformation result)
 {
     KeyContext    = result.KeyContext;
     ApcContext    = result.ApcContext;
     IoStatusBlock = new IoStatus()
     {
         Information = result.IoStatusBlock.Information,
         Pointer     = result.IoStatusBlock.Pointer
     };
 }
        /// <summary>
        /// Remove multiple queued status from the queue.
        /// </summary>
        /// <param name="max_count">Maximum number of status to remove.</param>
        /// <param name="timeout">An optional timeout.</param>
        /// <param name="alertable">Indicate whether the wait is alertable.</param>
        /// <param name="throw_on_error">True to throw on error.</param>
        /// <returns>Array of completion results. Length can be &lt;= max_count.</returns>
        public NtResult <FileIoCompletionResult[]> Remove(int max_count, NtWaitTimeout timeout, bool alertable, bool throw_on_error)
        {
            IoStatus io_status = new IoStatus();

            FileIoCompletionInformation[] result = new FileIoCompletionInformation[max_count];

            return(NtSystemCalls.NtRemoveIoCompletionEx(Handle, result, max_count,
                                                        out int result_count, timeout.ToLargeInteger(), alertable).CreateResult(throw_on_error, () =>
                                                                                                                                result.Take(result_count).Select(r => new FileIoCompletionResult(r)).ToArray()));
        }
Esempio n. 14
0
 /// <summary>
 /// Cancel the pending IO operation.
 /// </summary>
 /// <param name="throw_on_error">True to throw on error.</param>
 /// <returns>The NT status code.</returns>
 internal NtStatus Cancel(bool throw_on_error)
 {
     if (_object is NtFile)
     {
         IoStatus io_status = new IoStatus();
         return(NtSystemCalls.NtCancelIoFileEx(_object.Handle,
                                               _io_status, io_status).ToNtException(throw_on_error));
     }
     return(NtStatus.STATUS_SUCCESS);
 }
Esempio n. 15
0
 internal NtAsyncResult(NtObject @object)
 {
     _object = @object;
     if (!_object.CanSynchronize)
     {
         _event = NtEvent.Create(null,
                                 EventType.SynchronizationEvent, false);
     }
     _io_status = new SafeIoStatusBuffer();
     _result    = null;
 }
Esempio n. 16
0
 public static extern NtStatus NtQueryEaFile(
     SafeKernelObjectHandle FileHandle,
     [Out] IoStatus IoStatusBlock,
     [Out] byte[] Buffer,
     int Length,
     bool ReturnSingleEntry,
     SafeBuffer EaList,
     int EaListLength,
     [In] OptionalInt32 EaIndex,
     bool RestartScan
     );
Esempio n. 17
0
        /// <summary>
        /// Create a new file
        /// </summary>
        /// <param name="obj_attributes">The object attributes</param>
        /// <param name="desired_access">Desired access for the file</param>
        /// <param name="file_attributes">Attributes for the file</param>
        /// <param name="share_access">Share access for the file</param>
        /// <param name="open_options">Open options for file</param>
        /// <param name="disposition">Disposition when opening the file</param>
        /// <param name="ea_buffer">Extended Attributes buffer</param>
        /// <returns>The created/opened file object.</returns>
        public static NtFile Create(ObjectAttributes obj_attributes, FileAccessRights desired_access, FileAttributes file_attributes, FileShareMode share_access,
                                    FileOpenOptions open_options, FileDisposition disposition, EaBuffer ea_buffer)
        {
            SafeKernelObjectHandle handle;
            IoStatus iostatus = new IoStatus();

            byte[] buffer = ea_buffer != null?ea_buffer.ToByteArray() : null;

            NtSystemCalls.NtCreateFile(out handle, desired_access, obj_attributes, iostatus, null, FileAttributes.Normal,
                                       share_access, disposition, open_options, buffer, buffer != null ? buffer.Length : 0).ToNtException();
            return(new NtFile(handle));
        }
Esempio n. 18
0
 public static extern NtStatus NtFsControlFile(
     SafeKernelObjectHandle FileHandle,
     SafeKernelObjectHandle Event,
     IntPtr ApcRoutine,
     IntPtr ApcContext,
     [Out] IoStatus IoStatusBlock,
     int FSControlCode,
     IntPtr InputBuffer,
     int InputBufferLength,
     IntPtr OutputBuffer,
     int OutputBufferLength
     );
Esempio n. 19
0
 public static extern NtStatus NtCreateFile(
     out SafeKernelObjectHandle FileHandle,
     FileAccessRights DesiredAccess,
     ObjectAttributes ObjAttr,
     [Out] IoStatus IoStatusBlock,
     LargeInteger AllocationSize,
     FileAttributes FileAttributes,
     FileShareMode ShareAccess,
     FileDisposition CreateDisposition,
     FileOpenOptions CreateOptions,
     byte[] EaBuffer,
     int EaLength);
        /// <summary>
        /// Remove a queued status from the queue.
        /// </summary>
        /// <param name="timeout">An optional timeout.</param>
        /// <returns>The completion result.</returns>
        /// <exception cref="NtException">Thrown on error or timeout.</exception>
        public FileIoCompletionResult Remove(NtWaitTimeout timeout)
        {
            IntPtr   key_context;
            IntPtr   apc_context;
            IoStatus io_status = new IoStatus();
            NtStatus status    = NtSystemCalls.NtRemoveIoCompletion(Handle, out key_context, out apc_context, io_status, timeout.ToLargeInteger()).ToNtException();

            if (status != NtStatus.STATUS_SUCCESS)
            {
                throw new NtException(status);
            }
            return(new FileIoCompletionResult(key_context, apc_context, io_status));
        }
Esempio n. 21
0
        /// <summary>
        /// Delete the file. Must have been opened with DELETE access.
        /// </summary>
        /// <exception cref="NtException">Thrown on error.</exception>
        public void Delete()
        {
            IoStatus iostatus = new IoStatus();

            using (var deletefile = new FileDispositionInformation()
            {
                DeleteFile = true
            }.ToBuffer())
            {
                NtSystemCalls.NtSetInformationFile(Handle, iostatus, deletefile,
                                                   deletefile.Length, FileInformationClass.FileDispositionInformation).ToNtException();
            }
        }
Esempio n. 22
0
        /// <summary>
        /// Open a file by its object ID
        /// </summary>
        /// <param name="volume">A handle to the volume on which the file resides.</param>
        /// <param name="id">The object ID as a binary string</param>
        /// <param name="DesiredAccess">The desired access for the file</param>
        /// <param name="ShareAccess">File share access</param>
        /// <param name="OpenOptions">Open options.</param>
        /// <returns>The opened file object</returns>
        /// <exception cref="NtException">Thrown on error.</exception>
        public static NtFile OpenFileById(NtFile volume, string id,
                                          FileAccessRights DesiredAccess, FileShareMode ShareAccess, FileOpenOptions OpenOptions)
        {
            StringBuilder name_builder = new StringBuilder();

            using (ObjectAttributes obja = new ObjectAttributes(id, AttributeFlags.CaseInsensitive, volume, null, null))
            {
                SafeKernelObjectHandle handle;
                IoStatus iostatus = new IoStatus();
                NtSystemCalls.NtOpenFile(out handle, DesiredAccess, obja,
                                         iostatus, ShareAccess, OpenOptions | FileOpenOptions.OpenByFileId).ToNtException();
                return(new NtFile(handle));
            }
        }
Esempio n. 23
0
 internal NtStatus CompleteCall(NtStatus status)
 {
     if (status == NtStatus.STATUS_PENDING)
     {
         if (WaitForComplete())
         {
             status = _io_status.Result.Status;
         }
     }
     else if (status.IsSuccess())
     {
         _result = _io_status.Result;
     }
     return(status);
 }
        /// <summary>
        /// Remove multiple queued status from the queue.
        /// </summary>
        /// <param name="max_count">Maximum number of status to remove.</param>
        /// <param name="timeout">An optional timeout.</param>
        /// <param name="alertable">Indicate whether the wait is alertable.</param>
        /// <returns>Array of completion results. Length can be &lt;= max_count. If timeout then returns an empty array.</returns>
        public FileIoCompletionResult[] Remove(int max_count, NtWaitTimeout timeout, bool alertable)
        {
            IoStatus io_status = new IoStatus();

            FileIoCompletionInformation[] result = new FileIoCompletionInformation[max_count];
            int result_count = 0;

            NtStatus status = NtSystemCalls.NtRemoveIoCompletionEx(Handle, result, max_count,
                                                                   out result_count, timeout.ToLargeInteger(), alertable).ToNtException();

            if (status == NtStatus.STATUS_SUCCESS)
            {
                return(result.Take(result_count).Select(r => new FileIoCompletionResult(r)).ToArray());
            }
            return(new FileIoCompletionResult[0]);
        }
Esempio n. 25
0
 public static extern NtStatus NtCreateNamedPipeFile(
     out SafeKernelObjectHandle FileHandle,
     FileAccessRights DesiredAccess,
     [In] ObjectAttributes ObjectAttributes,
     [Out] IoStatus IoStatusBlock,
     FileShareMode ShareAccess,
     FileDisposition CreateDisposition,
     FileOpenOptions CreateOptions,
     NamedPipeType NamedPipeType,
     NamedPipeReadMode ReadMode,
     NamedPipeCompletionMode CompletionMode,
     int MaximumInstances,
     int InboundQuota,
     int OutboundQuota,
     LargeInteger DefaultTimeout
     );
Esempio n. 26
0
        /// <summary>
        /// Create a new hardlink to this file.
        /// </summary>
        /// <param name="linkname">The target NT path.</param>
        /// <param name="root">The root directory if linkname is relative</param>
        /// <exception cref="NtException">Thrown on error.</exception>
        public void CreateHardlink(string linkname, NtFile root)
        {
            FileLinkRenameInformation link = new FileLinkRenameInformation();

            link.ReplaceIfExists = true;
            link.RootDirectory   = root != null?root.Handle.DangerousGetHandle() : IntPtr.Zero;

            char[] chars = linkname.ToCharArray();
            link.FileNameLength = chars.Length * 2;
            using (var buffer = link.ToBuffer(link.FileNameLength, true))
            {
                IoStatus iostatus = new IoStatus();
                buffer.Data.WriteArray(0, chars, 0, chars.Length);
                NtSystemCalls.NtSetInformationFile(Handle, iostatus, buffer,
                                                   buffer.Length, FileInformationClass.FileLinkInformation).ToNtException();
            }
        }
Esempio n. 27
0
 private string TryGetName()
 {
     using (SafeStructureInOutBuffer <FileNameInformation> buffer = new SafeStructureInOutBuffer <FileNameInformation>(32 * 1024, true))
     {
         try
         {
             IoStatus status = new IoStatus();
             NtSystemCalls.NtQueryInformationFile(Handle, status, buffer, buffer.Length, FileInformationClass.FileNameInformation).ToNtException();
             char[] result = new char[buffer.Result.NameLength / 2];
             buffer.Data.ReadArray(0, result, 0, result.Length);
             return(new string(result));
         }
         catch (NtException)
         {
             return(String.Empty);
         }
     }
 }
Esempio n. 28
0
        /// <summary>
        /// Wait for the result to complete asynchronously. This could be waiting on an event
        /// or the file handle.
        /// </summary>
        /// <param name="token">Cancellation token.</param>
        /// <returns>Returns true if the wait completed successfully.</returns>
        /// <remarks>If true is returned then status and information can be read out.</remarks>
        internal async Task <bool> WaitForCompleteAsync(CancellationToken token)
        {
            if (_result != null)
            {
                return(true);
            }

            bool success;

            using (NtWaitHandle wait_handle = _event?.DuplicateAsWaitHandle() ?? _object.DuplicateAsWaitHandle()) {
                success = await wait_handle.WaitAsync(Timeout.Infinite, token);
            }

            if (success)
            {
                _result = _io_status.Result;
                return(true);
            }

            return(false);
        }
Esempio n. 29
0
 internal async Task <NtStatus> CompleteCallAsync(NtStatus status, CancellationToken token)
 {
     try {
         if (status == NtStatus.STATUS_PENDING)
         {
             if (await WaitForCompleteAsync(token))
             {
                 return(_result.Status);
             }
         }
         else if (status.IsSuccess())
         {
             _result = _io_status.Result;
         }
         return(status);
     } catch (TaskCanceledException) {
         // Cancel and then rethrow.
         Cancel();
         throw;
     }
 }
Esempio n. 30
0
 public static extern NtStatus NtFlushVirtualMemory(
     SafeKernelObjectHandle ProcessHandle,
     ref IntPtr BaseAddress,
     ref IntPtr RegionSize,
     out IoStatus IoStatus
     );