@@ -36,11 +36,14 @@ func (ntsd *NtSecurityDescriptor) Unmarshal(marshalledData []byte) (int, error)
3636 ntsd .RawBytesSize = 0
3737
3838 // Unmarshal the header
39- rawBytesSize , err := ntsd .Header .Unmarshal (marshalledData )
39+ _ , err := ntsd .Header .Unmarshal (marshalledData )
4040 if err != nil {
4141 return 0 , err
4242 }
43- ntsd .RawBytesSize += uint32 (rawBytesSize )
43+ // Track the maximum extent (offset + component size) across all components,
44+ // since components are placed at specific offsets within the buffer rather
45+ // than sequentially.
46+ ntsd .RawBytesSize = 20 // header size
4447
4548 // Unmarshal Owner if present
4649 if ntsd .Header .OffsetOwner != 0 {
@@ -52,7 +55,9 @@ func (ntsd *NtSecurityDescriptor) Unmarshal(marshalledData []byte) (int, error)
5255 if err != nil {
5356 return 0 , fmt .Errorf ("failed to unmarshal Owner: %w" , err )
5457 }
55- ntsd .RawBytesSize += uint32 (rawBytesSize )
58+ if end := ntsd .Header .OffsetOwner + uint32 (rawBytesSize ); end > ntsd .RawBytesSize {
59+ ntsd .RawBytesSize = end
60+ }
5661 } else {
5762 return 0 , fmt .Errorf ("failed to unmarshal Owner: offset is out of bounds OffsetOwner=%d, RawBytesSize=%d" , ntsd .Header .OffsetOwner , ntsd .RawBytesSize )
5863 }
@@ -68,7 +73,9 @@ func (ntsd *NtSecurityDescriptor) Unmarshal(marshalledData []byte) (int, error)
6873 if err != nil {
6974 return 0 , fmt .Errorf ("failed to unmarshal Group: %w" , err )
7075 }
71- ntsd .RawBytesSize += uint32 (rawBytesSize )
76+ if end := ntsd .Header .OffsetGroup + uint32 (rawBytesSize ); end > ntsd .RawBytesSize {
77+ ntsd .RawBytesSize = end
78+ }
7279 } else {
7380 return 0 , fmt .Errorf ("failed to unmarshal Group: offset is out of bounds OffsetGroup=%d, RawBytesSize=%d" , ntsd .Header .OffsetGroup , ntsd .RawBytesSize )
7481 }
@@ -84,7 +91,9 @@ func (ntsd *NtSecurityDescriptor) Unmarshal(marshalledData []byte) (int, error)
8491 if err != nil {
8592 return 0 , fmt .Errorf ("failed to unmarshal DACL: %w" , err )
8693 }
87- ntsd .RawBytesSize += uint32 (rawBytesSize )
94+ if end := ntsd .Header .OffsetDacl + uint32 (rawBytesSize ); end > ntsd .RawBytesSize {
95+ ntsd .RawBytesSize = end
96+ }
8897 } else {
8998 return 0 , fmt .Errorf ("failed to unmarshal DACL: offset is out of bounds OffsetDacl=%d, RawBytesSize=%d" , ntsd .Header .OffsetDacl , ntsd .RawBytesSize )
9099 }
@@ -100,7 +109,9 @@ func (ntsd *NtSecurityDescriptor) Unmarshal(marshalledData []byte) (int, error)
100109 if err != nil {
101110 return 0 , fmt .Errorf ("failed to unmarshal SACL: %w" , err )
102111 }
103- ntsd .RawBytesSize += uint32 (rawBytesSize )
112+ if end := ntsd .Header .OffsetSacl + uint32 (rawBytesSize ); end > ntsd .RawBytesSize {
113+ ntsd .RawBytesSize = end
114+ }
104115 } else {
105116 return 0 , fmt .Errorf ("failed to unmarshal SACL: offset is out of bounds OffsetSacl=%d, RawBytesSize=%d" , ntsd .Header .OffsetSacl , ntsd .RawBytesSize )
106117 }
0 commit comments