1 /*++
2 
3 Copyright (c) Microsoft. All rights reserved.
4 
5 Module Name:
6 
7     FxValidateFunctions.h
8 
9 Abstract:
10 
11     Inline functions which validate external WDF data structures
12 
13 Author:
14 
15 
16 
17 Environment:
18 
19     Both kernel and user mode
20 
21 Revision History:
22 
23         Made it mode agnostic
24 
25         Moved request options validation to FxRequestValidateFunctions.hpp
26         in kmdf\inc\private
27 
28         When request is merged FxRequestValidateFunctions.hpp can be moved to
29         shared directory
30 
31 --*/
32 
33 #ifndef _FXVALIDATEFUNCTIONS_H_
34 #define _FXVALIDATEFUNCTIONS_H_
35 
36 extern "C" {
37 
38 #if defined(EVENT_TRACING)
39 #include "FxValidateFunctions.hpp.tmh"
40 #endif
41 
42 }
43 
44 enum FX_VALIDATE_FUNCTIONS_FLAGS {
45     FX_VALIDATE_OPTION_NONE_SPECIFIED = 0x00000000,
46     FX_VALIDATE_OPTION_PARENT_NOT_ALLOWED = 0x00000001,
47     FX_VALIDATE_OPTION_EXECUTION_LEVEL_ALLOWED = 0x00000002,
48     FX_VALIDATE_OPTION_SYNCHRONIZATION_SCOPE_ALLOWED = 0x00000004,
49     FX_VALIDATE_OPTION_ATTRIBUTES_REQUIRED = 0x00000008,
50 
51     // not used directly, use FX_VALIDATE_OPTION_PARENT_REQUIRED instead
52     FX_VALIDATE_OPTION_PARENT_REQUIRED_FLAG = 0x00000010,
53 
54     // if a parent is required, the attributes themselves are requried
55     FX_VALIDATE_OPTION_PARENT_REQUIRED = FX_VALIDATE_OPTION_PARENT_REQUIRED_FLAG |
56                                          FX_VALIDATE_OPTION_ATTRIBUTES_REQUIRED,
57 };
58 
59 _Must_inspect_result_
60 NTSTATUS
61 FxValidateObjectAttributes(
62     __in PFX_DRIVER_GLOBALS FxDriverGlobals,
63     __in PWDF_OBJECT_ATTRIBUTES Attributes,
64     __in ULONG Flags = FX_VALIDATE_OPTION_NONE_SPECIFIED
65     );
66 
67 _Must_inspect_result_
68 NTSTATUS
69 __inline
FxValidateObjectAttributesForParentHandle(__in PFX_DRIVER_GLOBALS FxDriverGlobals,__in PWDF_OBJECT_ATTRIBUTES Attributes,__in ULONG Flags=FX_VALIDATE_OPTION_NONE_SPECIFIED)70 FxValidateObjectAttributesForParentHandle(
71     __in PFX_DRIVER_GLOBALS FxDriverGlobals,
72     __in PWDF_OBJECT_ATTRIBUTES Attributes,
73     __in ULONG Flags = FX_VALIDATE_OPTION_NONE_SPECIFIED
74     )
75 {
76     if (Attributes == NULL) {
77         if (Flags & FX_VALIDATE_OPTION_PARENT_REQUIRED) {
78             DoTraceLevelMessage(
79                 FxDriverGlobals, TRACE_LEVEL_ERROR, TRACINGDEVICE,
80                 "WDF_OBJECT_ATTRIBUTES required, %!STATUS!",
81                 (ULONG) STATUS_WDF_PARENT_NOT_SPECIFIED);
82         }
83         return STATUS_WDF_PARENT_NOT_SPECIFIED;
84     }
85 
86     if (Attributes->Size != sizeof(WDF_OBJECT_ATTRIBUTES)) {
87         //
88         // Size is wrong, bail out
89         //
90         DoTraceLevelMessage(FxDriverGlobals, TRACE_LEVEL_ERROR, TRACINGAPIERROR,
91                             "Attributes %p Size incorrect, expected %d, got %d, %!STATUS!",
92                             Attributes, sizeof(WDF_OBJECT_ATTRIBUTES),
93                             Attributes->Size, STATUS_INFO_LENGTH_MISMATCH);
94 
95         return STATUS_INFO_LENGTH_MISMATCH;
96     }
97 
98     if (Attributes->ParentObject == NULL) {
99         if (Flags & FX_VALIDATE_OPTION_PARENT_REQUIRED) {
100             DoTraceLevelMessage(
101                 FxDriverGlobals, TRACE_LEVEL_ERROR, TRACINGDEVICE,
102                 "ParentObject required in WDF_OBJECT_ATTRIBUTES %p, %!STATUS!",
103                 Attributes, STATUS_WDF_PARENT_NOT_SPECIFIED);
104         }
105         return STATUS_WDF_PARENT_NOT_SPECIFIED;
106     }
107 
108     return STATUS_SUCCESS;
109 }
110 
111 _Must_inspect_result_
112 NTSTATUS
113 __inline
FxValidateUnicodeString(__in PFX_DRIVER_GLOBALS FxDriverGlobals,__in PCUNICODE_STRING String)114 FxValidateUnicodeString(
115     __in PFX_DRIVER_GLOBALS FxDriverGlobals,
116     __in PCUNICODE_STRING String
117     )
118 {
119     NTSTATUS status;
120 
121     status = STATUS_INVALID_PARAMETER;
122 
123     if (String->Length & 1) {
124         DoTraceLevelMessage(
125             FxDriverGlobals, TRACE_LEVEL_ERROR, TRACINGAPIERROR,
126             "UNICODE_STRING %p, Length %d is odd, %!STATUS!",
127             String, String->Length, status);
128 
129         return status;
130     }
131 
132     if (String->MaximumLength & 1) {
133         DoTraceLevelMessage(
134             FxDriverGlobals, TRACE_LEVEL_ERROR, TRACINGAPIERROR,
135             "UNICODE_STRING %p, MaximumLength %d is odd, %!STATUS!",
136             String, String->MaximumLength, status);
137 
138         return status;
139     }
140 
141     if (String->MaximumLength > 0 && String->Buffer == NULL) {
142         DoTraceLevelMessage(
143             FxDriverGlobals, TRACE_LEVEL_ERROR, TRACINGAPIERROR,
144             "UNICODE_STRING %p, MaximumLength %d > 0, Buffer is NULL, %!STATUS!",
145             String, String->MaximumLength, status);
146 
147         return status;
148     }
149 
150     if (String->Length > String->MaximumLength) {
151         DoTraceLevelMessage(
152             FxDriverGlobals, TRACE_LEVEL_ERROR, TRACINGAPIERROR,
153             "UNICODE_STRING %p, Length %d > MaximumLength %d, %!STATUS!",
154             String, String->Length, String->MaximumLength, status);
155 
156         return status;
157     }
158 
159     return STATUS_SUCCESS;
160 }
161 
162 #endif // _FXVALIDATEFUNCTIONS_H_
163