Fix SECURITY_DESCRIPTOR Control bitfield ordering#299
Open
arpitjain099 wants to merge 1 commit into
Open
Conversation
The Control field of a SECURITY_DESCRIPTOR is an unsigned 16-bit value transmitted little-endian (MS-DTYP 2.4.6). The bit1 fields in the control struct were ordered so that each flag mapped to the wrong bit of that word. For a self-relative descriptor with Control 0x9404 (bytes 04 94), this made dacl_present and self_relative parse as 0 even though the DACL is present, which is what RubySMB::Client#get_key_security_descriptor returned in rapid7#289. MS-DTYP numbers the Control bits most-significant-first, so the low byte (read first) holds SS..OD and the high byte (read second) holds SR..DC. Declare the bit1 fields most-significant-first within each byte, low byte then high byte, so every flag maps to its MS-DTYP bit value (for example self_relative is 0x8000 and dacl_present is 0x0004). The existing per-flag expectations asserted the previous incorrect masks; they are corrected to the MS-DTYP values. A regression test parses the self-relative descriptor from the issue and checks the decoded flags. Fixes rapid7#289 Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
RubySMB::Field::SecurityDescriptorparsed the MS-DTYPControlfield with the wrong bit ordering, so the decoded flags did not match the bytes on the wire. For the self-relative descriptor reported in #289 (Controlword0x9404, little-endian bytes04 94),dacl_presentandself_relativeboth decoded as0even though the DACL is present and the descriptor is self-relative. That is whyClient#get_key_security_descriptorreturned a structure withdacl_present: 0and an emptydacldespite a validoffset_dacl.Fixes #289.
Root cause
The
Controlfield is an unsigned 16-bit value transmitted little-endian (MS-DTYP 2.4.6 SECURITY_DESCRIPTOR). MS-DTYP numbers theControlbits most-significant-first:As a value of the 16-bit word that is
SR=0x8000,RM=0x4000,PS=0x2000,PD=0x1000,SI=0x0800,DI=0x0400,SC=0x0200,DC=0x0100,SS=0x0080,DT=0x0040,SD=0x0020,SP=0x0010,DD=0x0008,DP=0x0004,GD=0x0002,OD=0x0001.The low byte (the first byte read) carries
SS..ODand the high byte (the second byte read) carriesSR..DC. The previousbit1declarations did not follow that layout, so each flag landed on the wrong bit. Decoding0x9404with the old code producedsacl_protected,owner_defaulted,dacl_defaultedandsacl_defaultedinstead of the correctself_relative,dacl_protected,dacl_auto_inheritedanddacl_present.Fix
Declare the
bit1fields most-significant-first within each byte, low byte first and then high byte, so every flag maps to its MS-DTYP bit value. Decoding0x9404now yields exactlyself_relative(0x8000),dacl_protected(0x1000),dacl_auto_inherited(0x0400) anddacl_present(0x0004).The existing per-flag examples in the spec asserted the previous incorrect masks (for example
self_relativewas checked against0x0001anddacl_presentagainst0x2000); they are corrected to the MS-DTYP values. A regression test parses the self-relative descriptor from the issue and checks the decoded flags and the DACL offset.Note: this changes the on-the-wire bit mapping of a BinData definition, which
CONTRIBUTING.mddocuments as a change that warrants a minor version bump.Verification Steps
bundle installrake specbundle exec rspec spec/lib/ruby_smb/field/security_descriptor.rbManual scenario