Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
7.5 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. querysec.c
  5. Abstract:
  6. This module contains the routines which implement the
  7. NtQuerySection service.
  8. Author:
  9. Lou Perazzoli (loup) 22-May-1989
  10. Landy Wang (landyw) 02-Jun-1997
  11. Revision History:
  12. --*/
  13. #include "mi.h"
  14. #ifdef ALLOC_PRAGMA
  15. #pragma alloc_text(PAGE,NtQuerySection)
  16. #endif
  17. NTSTATUS
  18. NtQuerySection(
  19. IN HANDLE SectionHandle,
  20. IN SECTION_INFORMATION_CLASS SectionInformationClass,
  21. OUT PVOID SectionInformation,
  22. IN SIZE_T SectionInformationLength,
  23. OUT PSIZE_T ReturnLength OPTIONAL
  24. )
  25. /*++
  26. Routine Description:
  27. This function provides the capability to determine the base address,
  28. size, granted access, and allocation of an opened section object.
  29. Arguments:
  30. SectionHandle - Supplies an open handle to a section object.
  31. SectionInformationClass - The section information class about
  32. which to retrieve information.
  33. SectionInformation - A pointer to a buffer that receives the
  34. specified information. The format and content of the
  35. buffer depend on the specified section class.
  36. SectionInformation Format by Information Class:
  37. SectionBasicInformation - Data type is PSECTION_BASIC_INFORMATION.
  38. SECTION_BASIC_INFORMATION Structure
  39. PVOID BaseAddress - The base virtual address of the
  40. section if the section is based.
  41. LARGE_INTEGER MaximumSize - The maximum size of the section in
  42. bytes.
  43. ULONG AllocationAttributes - The allocation attributes flags.
  44. AllocationAttributes Flags
  45. SEC_BASED - The section is a based section.
  46. SEC_FILE - The section is backed by a data file.
  47. SEC_RESERVE - All pages of the section were initially
  48. set to the reserved state.
  49. SEC_COMMIT - All pages of the section were initially
  50. to the committed state.
  51. SEC_IMAGE - The section was mapped as an executable image file.
  52. SECTION_IMAGE_INFORMATION
  53. SectionInformationLength - Specifies the length in bytes of the
  54. section information buffer.
  55. ReturnLength - An optional pointer which, if specified, receives the
  56. number of bytes placed in the section information buffer.
  57. Return Value:
  58. NTSTATUS.
  59. --*/
  60. {
  61. NTSTATUS Status;
  62. PSECTION Section;
  63. KPROCESSOR_MODE PreviousMode;
  64. PAGED_CODE();
  65. //
  66. // Get previous processor mode and probe output argument if necessary.
  67. //
  68. PreviousMode = KeGetPreviousMode();
  69. if (PreviousMode != KernelMode) {
  70. //
  71. // Check arguments.
  72. //
  73. try {
  74. ProbeForWrite(SectionInformation,
  75. SectionInformationLength,
  76. sizeof(ULONG));
  77. if (ARGUMENT_PRESENT (ReturnLength)) {
  78. ProbeForWriteUlong_ptr (ReturnLength);
  79. }
  80. } except (EXCEPTION_EXECUTE_HANDLER) {
  81. //
  82. // If an exception occurs during the probe or capture
  83. // of the initial values, then handle the exception and
  84. // return the exception code as the status value.
  85. //
  86. return GetExceptionCode();
  87. }
  88. }
  89. //
  90. // Check argument validity.
  91. //
  92. if ((SectionInformationClass != SectionBasicInformation) &&
  93. (SectionInformationClass != SectionImageInformation)) {
  94. return STATUS_INVALID_INFO_CLASS;
  95. }
  96. if (SectionInformationClass == SectionBasicInformation) {
  97. if (SectionInformationLength < (ULONG)sizeof(SECTION_BASIC_INFORMATION)) {
  98. return STATUS_INFO_LENGTH_MISMATCH;
  99. }
  100. }
  101. else {
  102. if (SectionInformationLength < (ULONG)sizeof(SECTION_IMAGE_INFORMATION)) {
  103. return STATUS_INFO_LENGTH_MISMATCH;
  104. }
  105. }
  106. //
  107. // Reference section object by handle for READ access, get the information
  108. // from the section object, dereference the section
  109. // object, fill in information structure, optionally return the length of
  110. // the information structure, and return service status.
  111. //
  112. Status = ObReferenceObjectByHandle (SectionHandle,
  113. SECTION_QUERY,
  114. MmSectionObjectType,
  115. PreviousMode,
  116. (PVOID *)&Section,
  117. NULL);
  118. if (NT_SUCCESS(Status)) {
  119. try {
  120. if (SectionInformationClass == SectionBasicInformation) {
  121. ((PSECTION_BASIC_INFORMATION)SectionInformation)->BaseAddress =
  122. (PVOID)Section->Address.StartingVpn;
  123. ((PSECTION_BASIC_INFORMATION)SectionInformation)->MaximumSize =
  124. Section->SizeOfSection;
  125. ((PSECTION_BASIC_INFORMATION)SectionInformation)->AllocationAttributes =
  126. 0;
  127. if (Section->u.Flags.Image) {
  128. ((PSECTION_BASIC_INFORMATION)SectionInformation)->AllocationAttributes =
  129. SEC_IMAGE;
  130. }
  131. if (Section->u.Flags.Based) {
  132. ((PSECTION_BASIC_INFORMATION)SectionInformation)->AllocationAttributes |=
  133. SEC_BASED;
  134. }
  135. if (Section->u.Flags.File) {
  136. ((PSECTION_BASIC_INFORMATION)SectionInformation)->AllocationAttributes |=
  137. SEC_FILE;
  138. }
  139. if (Section->u.Flags.NoCache) {
  140. ((PSECTION_BASIC_INFORMATION)SectionInformation)->AllocationAttributes |=
  141. SEC_NOCACHE;
  142. }
  143. if (Section->u.Flags.Reserve) {
  144. ((PSECTION_BASIC_INFORMATION)SectionInformation)->AllocationAttributes |=
  145. SEC_RESERVE;
  146. }
  147. if (Section->u.Flags.Commit) {
  148. ((PSECTION_BASIC_INFORMATION)SectionInformation)->AllocationAttributes |=
  149. SEC_COMMIT;
  150. }
  151. if (Section->Segment->ControlArea->u.Flags.GlobalMemory) {
  152. ((PSECTION_BASIC_INFORMATION)SectionInformation)->AllocationAttributes |=
  153. SEC_GLOBAL;
  154. }
  155. if (ARGUMENT_PRESENT(ReturnLength)) {
  156. *ReturnLength = sizeof(SECTION_BASIC_INFORMATION);
  157. }
  158. }
  159. else {
  160. if (Section->u.Flags.Image == 0) {
  161. Status = STATUS_SECTION_NOT_IMAGE;
  162. }
  163. else {
  164. *((PSECTION_IMAGE_INFORMATION)SectionInformation) =
  165. *Section->Segment->u2.ImageInformation;
  166. if (ARGUMENT_PRESENT(ReturnLength)) {
  167. *ReturnLength = sizeof(SECTION_IMAGE_INFORMATION);
  168. }
  169. }
  170. }
  171. } except (EXCEPTION_EXECUTE_HANDLER) {
  172. Status = GetExceptionCode ();
  173. }
  174. ObDereferenceObject ((PVOID)Section);
  175. }
  176. return Status;
  177. }