Commit Graph

718 Commits (1565b01e215c74e5c5f83f3ecde1ed682637dc5a)

Author SHA1 Message Date
Erik Larsson 1565b01e21 mft.c: Fix broken free MFT records accounting during bitmap extension.
When the bitmap needs extending, 'vol->free_mft_records' is incremented
by 8*8=64 records. This is due to the bitmap's initialized area being
extended 8 bytes at a time.
However the way 'vol->free_mft_records' is being initialized is that all
the bits that are currently allocated to the MFT bitmap are already
taken into account at initialization time. This leads to a value for
'vol->free_mft_records' that is larger than the actual available number
of MFT records.

For example if there are 20 used MFT records and the bitmap has a 4096
byte allocation where 16 bytes are initialized, the number of free MFT
records are ((8 * 16) - 20) + (8 * (4096 - 16)) = 32748 records
available.
If we now expand the bitmap by 8 initialized bytes, we'd be adding 64
MFT entries according to the logic in the function
'ntfs_mft_bitmap_extend_initialized'.
However we are expanding it within the bounds of the existing allocation
where there is (4096 - 16) bytes free, so they shouldn't be added at all
at this stage.

The result is that our internal accounting is that we have 32748 + 64 =
32812 available MFT records, but in reality we will have 32748 records
available all the time until we expand the allocation beyond 4096 bytes.

Fixed by incrementing 'vol->free_mft_records' when the allocation is
expanded, not when the initialized size is.
2023-05-09 11:25:10 +03:00
Erik Larsson 241ddb3860 index.c: Fix crash when a reparse tag cannot be found in the index.
When 'remove_reparse_index', called by 'ntfs_delete_reparse_index',
fails to look up a reparse key in the index, it leaves the
'ntfs_index_context' without a populated 'INDEX_BLOCK *ib' field.

This causes 'remove_reparse_index' to fail but the index entry is then
marked dirty unconditionally in 'ntfs_index_entry_mark_dirty', called by
'ntfs_delete_reparse_index', even though 'ib' may be NULL.

The following 'ntfs_index_ctx_put' call then starts to write out the
dirty 'INDEX_BLOCK', which causes a crash.

Fixed by only marking the index block dirty in if it's non-NULL.

Thanks to Stephen Greenham <sg@solarisfire.com> for reporting this issue
and providing debug information.
2023-05-03 10:44:57 +02:00
Erik Larsson 01b9bddc0c attrib.c: Fix errno not being set on NULL character in attribute name.
This is an error condition as we jump to err_out, but there's no errno
value set to accompany it. Fixed by setting EIO.
2023-05-03 09:57:34 +02:00
Jean-Pierre André 76c3a799a9 Avoided merging runlists with no runs
Runlists with no runs are tolerated though not expected. However merging
such runlists is problematic as there is no significant vcn to examine.
So avoid merging them, and just return the other runlist.
2022-09-14 08:31:31 +02:00
Jean-Pierre André 18bfc67611 Rejected zero-sized runs
A zero-size run is the universal way to indentify the end of a runlist,
so we must reject zero-sized runs when decompressing a runlist. A
zero-size data run is an error, and a zero-size hole is simply ignored.
2022-09-14 08:29:58 +02:00
Jean-Pierre André bce5734a75 Fixed operation on little endian data
Forcing an even usa_of, in a recent security patch, must be made on cpu
endian data.
2022-05-10 10:40:17 +02:00
Jean-Pierre André 5ce8941bf4 Made sure there is no null character in an attribute name (bis)
When copying an attribute name which contains a null, it is truncated
and this may lead to accessing non-allocated bytes when relying on the
expected name length. Such (illegal) names must therefore be rejected.
2021-11-05 08:41:20 +01:00
Jean-Pierre André 6efc1305c1 Made sure the client log data does not overflow from restart page
Strengthen the consistency check of the length of restart pages, and
check that log client records are within such a restart page.
2021-10-20 09:53:28 +02:00
Jean-Pierre André 60717a846d Avoided allocating and reading an attribute beyond its full size
Before reading a full attribute value for internal use, its expected
length has been checked to be < 0x40000. However the allocated size
in the runlist may be much bigger as a consequence of a bug or malice.
To prevent malloc'ing excessive size, restrict the size of the last
run to read to the needed length.
2021-09-21 10:56:06 +02:00
Jean-Pierre André 838b6e35b4 Made sure there is no null character in an attribute name
When copying an attribute name which contains a null, it is truncated
and this may lead to accessing non-allocated bytes when relying on the
expected name length. Such names must therefore be rejected.
2021-09-21 10:54:50 +02:00
Jean-Pierre André a8818cf779 Used a default usn when the former one cannot be retrieved
When creating a new MFT record, the former seq_no and usn are retrieved
to avoid the new one to be mistaken for the former one.
This may not be possible when the record is used for the first time
or after some bad error. In such situation use default values.
2021-09-21 10:53:16 +02:00
Jean-Pierre André 399ba862c9 Updated the urls present in ntfs-3g code
The urls now point at Github
2021-08-30 09:27:33 +02:00
Jean-Pierre André 1261e6b60a Fixed the detection of the end of attribute list
The recent detection of a truncated attribute list entry overlooked the
normal detection of the end of list. Moreover the check for name
overflow is to be done later and not needed at this stage.
2021-07-26 08:49:45 +02:00
Jean-Pierre André a337c4c1eb Renamed ntfs_index_entry_consistent() as ntfs_index_entry_inconsistent()
The original name was error prone when checking the condition.
2021-07-19 09:23:23 +02:00
Jean-Pierre André 45141516d7 Renamed ntfs_attr_consistent() as ntfs_attr_inconsistent()
The original name was error prone while checking the condition.
2021-07-19 09:23:23 +02:00
Jean-Pierre André bb4456d339 Redesigned the INDEX_ROOT consistency checks
By ordering the values from smallest to biggest, there is less chance
to be caught by an arithmetic overflow.
2021-07-19 09:23:23 +02:00
Jean-Pierre André 81725f6a54 Made sure the requested compression block size is supported
Compressed files can only be opened if NTFS version < 3.0, and
the only supported compression block size is 16 clusters long.
2021-07-19 09:23:23 +02:00
Jean-Pierre André 4462f82580 Reset the resident attribute offset when appending from none
When there is no resident attribute value, its offset is unsafe, so better
to recompute it when appending data.
2021-07-19 09:23:23 +02:00
Jean-Pierre André 0911ef206d Rejected negative data length in an attribute list
The negative data length of an attribute list is an indication of a
probable corruption and must be rejected.
2021-07-19 09:23:23 +02:00
Jean-Pierre André b95b4ba1a5 Rejected negative data length in readall()
The negative data length of an attribute is an indication of a probable
corruption and must be rejected.
2021-07-19 09:23:23 +02:00
Jean-Pierre André e70d10d848 Added a check of the minimal length of some attributes
The minimal lengths of STANDARD_ATTRIBUTE and OBJECT_ID were not
checked and could lead to out-of-buffer access.
2021-07-19 09:23:23 +02:00
Jean-Pierre André 2bf5077804 Checked consistency of index blocks
Improved existing consistency checks of index blocks and grouped them
into a specific function.
2021-07-19 09:23:23 +02:00
Jean-Pierre André 55e7326350 Avoided endless recursions when allocating the main bitmap
Allocating clusters to the main bitmap may imply updating the bitmap
itself within a cluster not yet allocated. This can turn into endless
recursions and has to be rejected. Currently the bitmap is assumed
to be fully allocated.
2021-07-19 09:23:23 +02:00
Jean-Pierre André 61134117c1 Fixed checking the end of attrdef data
Incomplete attribute definitions have to be rejected.
2021-07-19 09:23:23 +02:00
Jean-Pierre André f85ce6ff2e Skipped errors for bad free clusters before they are computed
The count of free clusters may be updated while mounting before it
has been initialized, which may lead to irrelevant error messages.
Moreover the count is not computed at all in some ntfsprogs utilities.
So set up a flags to avoid outputting irrelevant errors.
2021-07-19 09:23:23 +02:00
Jean-Pierre André 67f959df96 Fixed the computation of the end of index entry
The end of an index entry is related to its full length, not to the
length of the key. Added an error message in an overflow case.
2021-07-12 08:31:18 +02:00
Jean-Pierre André 5c002438f2 Checked that indexes do not exceed the index block size
Make sure the used part of an index block fits into the allocated buffer.
Note : a negative size may cause overflow on 32-bit cpus.
(contributed by Rakesh Pandit)
2021-07-12 08:31:18 +02:00
Jean-Pierre André 20d700841b Shown in log the inode of directory read error
The unreadable directory record was poorly identified
2021-07-12 08:31:18 +02:00
Jean-Pierre André f30b52490f Restricted the attribute definition table size to 24 bits
The standard size is 2560 bytes. It can be extended for specific purposes,
but its former limit to 32 bits was unreasonable. Anyway ntfs-3g is
not committed to support non-standard situations.
2021-07-12 08:31:18 +02:00
Jean-Pierre André af1bc0f5ec Hardened the check of locations of MFT and MFTMirr
The MFT and MFTMirr may not be negative or overlap the boot sector.
2021-07-12 08:31:18 +02:00
Jean-Pierre André 7f45544ed7 Added and grouped generic attribute checks
Checked that attributes are [non-]resident when they have to be, and
grouped consistency checks on each of them in a dedicated function.
Consequenly request the checks where needed and remove existing index
checks.
2021-07-12 08:31:18 +02:00
Jean-Pierre André 436fe09f87 Checked consistency of index entries
Make sure the data and key in indexes do not overflow from index entries
2021-07-12 08:31:18 +02:00
Jean-Pierre André 32e858a87a Aborted mounting when cannot access standard information of MFT
The standard information of the MFT must be its first attribute in the
base record. If it is not accessible initially, we end up searching it
in an extent before the MFT struct is ready for that.
2021-07-12 08:31:18 +02:00
Jean-Pierre André 68ead9b8fc Checked expanded attribute does not overflow the space in use
When resizing an attribute, make sure the trailing data is moved in
the correct direction.
2021-07-12 08:31:18 +02:00
Jean-Pierre André 84478c4012 Checked attributes layout against length actually used
The end of the attributes must be checked against the space in use
rather than the allocated space. This contributes to the safety of
subsequent attribute resizing.
2021-07-12 08:31:18 +02:00
Jean-Pierre André ec7bf54377 Checked the MFTMirr lcn to be consistent with the boot sector
The lcn of the initial run of the MFTMirr data must be the same as
described in the boot sector. As a consequence this cannot be a hole.
2021-07-12 08:31:18 +02:00
Jean-Pierre André 4e094563d4 Checked $MFT data to be ready before fetching an extent 2021-07-12 08:31:18 +02:00
Jean-Pierre André e2e625065d Checked the layout of attribute list entries
Make sure the attribute list has at least one element, and that each of
them has the minimal size and does not overflow out of the end of list.
2021-07-12 08:31:17 +02:00
Jean-Pierre André 4e09b252af Checked name limits while looking up for an external attribute
Make sure the name of an attribute does not overflow beyond the
attribute length.
2021-07-12 08:31:17 +02:00
Jean-Pierre André d86c34390d Added a consistency check of attribute size in readall()
Make sure the attribute designated to readall() has a reasonable size.
Apart from a bitmap, in a valid call, the attribute size is limited to
65536, and bitmaps are limited by the number of cluster.
2021-05-17 15:39:52 +03:00
Jean-Pierre André 37a330ea8c Improved the consistency checks of standard information
Make sure the standard information attribute has a valid size.
2021-05-17 15:39:13 +03:00
Jean-Pierre André 218d93b37b Cleaned the tail of compressed buffer before writing a full cluster
When appending to an existing compressed file, the last block must first
be decompressed. When doing so, clear the tail of buffer in order to not
leak unrelated data.
2021-05-17 15:38:20 +03:00
Jean-Pierre André 02e199978b Fixed initialization of a Posix ACL
A posix ACL record was not fully initialized.
2021-05-17 15:37:45 +03:00
Jean-Pierre André 8adc9c6184 Hardened consistency checks of MFT records
Check the record is properly aligned and does not flow beyond the
expected end of the record.
2021-05-17 15:35:02 +03:00
Jean-Pierre André bb0168e56b Rejected updates of uninitialized MFTMirr
Reject updating uninitialized MFTMirr because it leads to endless
recursions as updating the MFTMirr entry of MFT implies further updating
MFTMirr.
This is probably specific to ntfsfix, as mounts are normally rejected
when MFTMirr does not match MFT.
2021-05-17 15:33:41 +03:00
Jean-Pierre André 3f3b771cb0 Added consistency checks of index root
The index root must lie within the space allocated for it.
2021-05-17 15:33:04 +03:00
Jean-Pierre André 00a5478625 Added check of layout of attributes
Make sure the attributes fully lie within the MFT record, and make sure
their variable components (name, runlist, value) fully lie within the
attribute.
2021-05-17 15:31:52 +03:00
Jean-Pierre André 1258474e07 Improved check of upcase table length
The consistency check on the upcase size was laxist.
The unicode points are limited to 1048575 (20 bits), but there is no
provision to support code points beyond 65535.
In the current implementation the upcase size must be even and <= 131072.
2021-05-17 15:30:17 +03:00
Jean-Pierre André 2ede3b67ee Improved checking of the location of MFT
Terminate cleanly when the MFT is improperly located, by making sure it
is located within the volume and that it contains its own data.
2021-03-05 10:21:36 +01:00
Jean-Pierre André 094f9b3f2d Fixed a minor endianness ajustment bug
The endianness ajustment was the wrong one though it did the correct thing.
2021-01-26 10:06:18 +01:00