Skip to content

Commit ee57b4f

Browse files
committed
hid: hidparser: Fix for CORE-11538.
This commit fixes issue https://jira.reactos.org/browse/CORE-11538. There were mistakes in buffer manipulation loops. svn path=/branches/GSoC_2016/USB/; revision=72394
1 parent ada3e93 commit ee57b4f

File tree

4 files changed

+52
-37
lines changed

4 files changed

+52
-37
lines changed

drivers/hid/hidparse/hidparse.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,29 @@ HidP_GetCaps(
125125
return HidParser_GetCaps(&Parser, PreparsedData, Capabilities);
126126
}
127127

128+
NTSTATUS
129+
TranslateStatusForUpperLayer(
130+
IN HIDPARSER_STATUS Status)
131+
{
132+
//
133+
// now we are handling only this values, for others just return
134+
// status as it is.
135+
//
136+
switch (Status)
137+
{
138+
case HIDPARSER_STATUS_INSUFFICIENT_RESOURCES:
139+
return STATUS_INSUFFICIENT_RESOURCES;
140+
case HIDPARSER_STATUS_INVALID_REPORT_TYPE:
141+
return HIDP_STATUS_INVALID_REPORT_TYPE;
142+
case HIDPARSER_STATUS_BUFFER_TOO_SMALL:
143+
return STATUS_BUFFER_TOO_SMALL;
144+
case HIDPARSER_STATUS_COLLECTION_NOT_FOUND:
145+
return STATUS_NO_DATA_DETECTED;
146+
default:
147+
return Status;
148+
}
149+
}
150+
128151
NTSTATUS
129152
NTAPI
130153
HidP_GetCollectionDescription(
@@ -134,6 +157,7 @@ HidP_GetCollectionDescription(
134157
OUT PHIDP_DEVICE_DESC DeviceDescription)
135158
{
136159
HID_PARSER Parser;
160+
NTSTATUS Status;
137161

138162
//
139163
// init parser
@@ -143,7 +167,8 @@ HidP_GetCollectionDescription(
143167
//
144168
// get description;
145169
//
146-
return HidParser_GetCollectionDescription(&Parser, ReportDesc, DescLength, PoolType, DeviceDescription);
170+
Status = HidParser_GetCollectionDescription(&Parser, ReportDesc, DescLength, PoolType, DeviceDescription);
171+
return TranslateStatusForUpperLayer(Status);
147172
}
148173

149174
HIDAPI

sdk/lib/drivers/hidparser/context.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ HidParser_StoreCollection(
163163
//
164164
// store offset
165165
//
166-
TargetCollection->Offsets[Collection->NodeCount + Index] = CurrentOffset;
166+
TargetCollection->Offsets[Collection->ReportCount + Index] = CurrentOffset;
167167

168168
//
169169
// store sub collections
@@ -254,7 +254,7 @@ HidParser_SearchReportInCollection(
254254
//
255255
// get collection
256256
//
257-
SubCollection = (PHID_COLLECTION)(CollectionContext->RawData + Collection->Offsets[Collection->NodeCount + Index]);
257+
SubCollection = (PHID_COLLECTION)(CollectionContext->RawData + Collection->Offsets[Collection->ReportCount + Index]);
258258

259259
//
260260
// recursively search collection
@@ -314,7 +314,7 @@ HidParser_GetCollectionCount(
314314
//
315315
// get offset to sub collection
316316
//
317-
SubCollection = (PHID_COLLECTION)(CollectionContext->RawData + Collection->Offsets[Collection->NodeCount + Index]);
317+
SubCollection = (PHID_COLLECTION)(CollectionContext->RawData + Collection->Offsets[Collection->ReportCount + Index]);
318318

319319
//
320320
// count collection for sub nodes

sdk/lib/drivers/hidparser/hidparser.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ HidParser_GetCollectionDescription(
6868
// failed to parse report descriptor
6969
//
7070
Parser->Debug("[HIDPARSER] Failed to parse report descriptor with %x\n", ParserStatus);
71-
return TranslateHidParserStatus(ParserStatus);
71+
return ParserStatus;
7272
}
7373

7474
//
@@ -126,7 +126,9 @@ HidParser_GetCollectionDescription(
126126
//
127127
// no memory
128128
//
129-
return TranslateHidParserStatus(ParserStatus);
129+
Parser->Free(DeviceDescription->CollectionDesc);
130+
Parser->Free(DeviceDescription->ReportIDs);
131+
return ParserStatus;
130132
}
131133

132134
//
@@ -153,6 +155,13 @@ HidParser_GetCollectionDescription(
153155
// get collection usage page
154156
//
155157
ParserStatus = HidParser_GetCollectionUsagePage((PVOID)DeviceDescription->CollectionDesc[Index].PreparsedData, &DeviceDescription->CollectionDesc[Index].Usage, &DeviceDescription->CollectionDesc[Index].UsagePage);
158+
if (ParserStatus != HIDPARSER_STATUS_SUCCESS)
159+
{
160+
// collection not found
161+
Parser->Free(DeviceDescription->CollectionDesc);
162+
Parser->Free(DeviceDescription->ReportIDs);
163+
return ParserStatus;
164+
}
156165

157166
//
158167
// windows seems to prepend the report id, regardless if it is required

sdk/lib/drivers/hidparser/parser.c

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -713,30 +713,6 @@ HidParser_AddMainItem(
713713
return HIDPARSER_STATUS_SUCCESS;
714714
}
715715

716-
HIDPARSER_STATUS
717-
AllocateParserContext(
718-
IN PHID_PARSER Parser,
719-
OUT PHID_PARSER_CONTEXT *OutParserContext)
720-
{
721-
PHID_PARSER_CONTEXT ParserContext;
722-
723-
ParserContext = Parser->Alloc(sizeof(HID_PARSER_CONTEXT));
724-
if (!ParserContext)
725-
{
726-
//
727-
// failed
728-
//
729-
return HIDPARSER_STATUS_INSUFFICIENT_RESOURCES;
730-
}
731-
732-
//
733-
// store result
734-
//
735-
*OutParserContext = ParserContext;
736-
return HIDPARSER_STATUS_SUCCESS;
737-
}
738-
739-
740716
HIDPARSER_STATUS
741717
HidParser_ParseReportDescriptor(
742718
IN PHID_PARSER Parser,
@@ -760,12 +736,18 @@ HidParser_ParseReportDescriptor(
760736
PMAIN_ITEM_DATA MainItemData;
761737
PHID_PARSER_CONTEXT ParserContext;
762738

739+
CurrentOffset = ReportDescriptor;
740+
ReportEnd = ReportDescriptor + ReportLength;
741+
742+
if (ReportDescriptor >= ReportEnd)
743+
return HIDPARSER_STATUS_COLLECTION_NOT_FOUND;
744+
763745
//
764746
// allocate parser
765747
//
766-
Status = AllocateParserContext(Parser, &ParserContext);
767-
if (Status != HIDPARSER_STATUS_SUCCESS)
768-
return Status;
748+
ParserContext = Parser->Alloc(sizeof(HID_PARSER_CONTEXT));;
749+
if (!ParserContext)
750+
return HIDPARSER_STATUS_INSUFFICIENT_RESOURCES;
769751

770752

771753
//
@@ -778,6 +760,7 @@ HidParser_ParseReportDescriptor(
778760
//
779761
// no memory
780762
//
763+
Parser->Free(ParserContext);
781764
return HIDPARSER_STATUS_INSUFFICIENT_RESOURCES;
782765
}
783766

@@ -792,15 +775,14 @@ HidParser_ParseReportDescriptor(
792775
//
793776
Parser->Free(ParserContext->LocalItemState.UsageStack);
794777
ParserContext->LocalItemState.UsageStack = NULL;
778+
Parser->Free(ParserContext);
795779
return HIDPARSER_STATUS_INSUFFICIENT_RESOURCES;
796780
}
797781

798782
//
799783
// start parsing
800784
//
801785
CurrentCollection = ParserContext->RootCollection;
802-
CurrentOffset = ReportDescriptor;
803-
ReportEnd = ReportDescriptor + ReportLength;
804786

805787
do
806788
{
@@ -1230,8 +1212,7 @@ HidParser_ParseReportDescriptor(
12301212
//
12311213
CurrentOffset += CurrentItemSize + sizeof(ITEM_PREFIX);
12321214

1233-
1234-
}while(CurrentOffset < ReportEnd);
1215+
}while (CurrentOffset < ReportEnd);
12351216

12361217

12371218
//

0 commit comments

Comments
 (0)