Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * mst.c - NTFS multi sector transfer protection handling code. Part of the
0004  *     Linux-NTFS project.
0005  *
0006  * Copyright (c) 2001-2004 Anton Altaparmakov
0007  */
0008 
0009 #include "ntfs.h"
0010 
0011 /**
0012  * post_read_mst_fixup - deprotect multi sector transfer protected data
0013  * @b:      pointer to the data to deprotect
0014  * @size:   size in bytes of @b
0015  *
0016  * Perform the necessary post read multi sector transfer fixup and detect the
0017  * presence of incomplete multi sector transfers. - In that case, overwrite the
0018  * magic of the ntfs record header being processed with "BAAD" (in memory only!)
0019  * and abort processing.
0020  *
0021  * Return 0 on success and -EINVAL on error ("BAAD" magic will be present).
0022  *
0023  * NOTE: We consider the absence / invalidity of an update sequence array to
0024  * mean that the structure is not protected at all and hence doesn't need to
0025  * be fixed up. Thus, we return success and not failure in this case. This is
0026  * in contrast to pre_write_mst_fixup(), see below.
0027  */
0028 int post_read_mst_fixup(NTFS_RECORD *b, const u32 size)
0029 {
0030     u16 usa_ofs, usa_count, usn;
0031     u16 *usa_pos, *data_pos;
0032 
0033     /* Setup the variables. */
0034     usa_ofs = le16_to_cpu(b->usa_ofs);
0035     /* Decrement usa_count to get number of fixups. */
0036     usa_count = le16_to_cpu(b->usa_count) - 1;
0037     /* Size and alignment checks. */
0038     if ( size & (NTFS_BLOCK_SIZE - 1)   ||
0039          usa_ofs & 1            ||
0040          usa_ofs + (usa_count * 2) > size   ||
0041          (size >> NTFS_BLOCK_SIZE_BITS) != usa_count)
0042         return 0;
0043     /* Position of usn in update sequence array. */
0044     usa_pos = (u16*)b + usa_ofs/sizeof(u16);
0045     /*
0046      * The update sequence number which has to be equal to each of the
0047      * u16 values before they are fixed up. Note no need to care for
0048      * endianness since we are comparing and moving data for on disk
0049      * structures which means the data is consistent. - If it is
0050      * consistenty the wrong endianness it doesn't make any difference.
0051      */
0052     usn = *usa_pos;
0053     /*
0054      * Position in protected data of first u16 that needs fixing up.
0055      */
0056     data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
0057     /*
0058      * Check for incomplete multi sector transfer(s).
0059      */
0060     while (usa_count--) {
0061         if (*data_pos != usn) {
0062             /*
0063              * Incomplete multi sector transfer detected! )-:
0064              * Set the magic to "BAAD" and return failure.
0065              * Note that magic_BAAD is already converted to le32.
0066              */
0067             b->magic = magic_BAAD;
0068             return -EINVAL;
0069         }
0070         data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
0071     }
0072     /* Re-setup the variables. */
0073     usa_count = le16_to_cpu(b->usa_count) - 1;
0074     data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1;
0075     /* Fixup all sectors. */
0076     while (usa_count--) {
0077         /*
0078          * Increment position in usa and restore original data from
0079          * the usa into the data buffer.
0080          */
0081         *data_pos = *(++usa_pos);
0082         /* Increment position in data as well. */
0083         data_pos += NTFS_BLOCK_SIZE/sizeof(u16);
0084     }
0085     return 0;
0086 }
0087 
0088 /**
0089  * pre_write_mst_fixup - apply multi sector transfer protection
0090  * @b:      pointer to the data to protect
0091  * @size:   size in bytes of @b
0092  *
0093  * Perform the necessary pre write multi sector transfer fixup on the data
0094  * pointer to by @b of @size.
0095  *
0096  * Return 0 if fixup applied (success) or -EINVAL if no fixup was performed
0097  * (assumed not needed). This is in contrast to post_read_mst_fixup() above.
0098  *
0099  * NOTE: We consider the absence / invalidity of an update sequence array to
0100  * mean that the structure is not subject to protection and hence doesn't need
0101  * to be fixed up. This means that you have to create a valid update sequence
0102  * array header in the ntfs record before calling this function, otherwise it
0103  * will fail (the header needs to contain the position of the update sequence
0104  * array together with the number of elements in the array). You also need to
0105  * initialise the update sequence number before calling this function
0106  * otherwise a random word will be used (whatever was in the record at that
0107  * position at that time).
0108  */
0109 int pre_write_mst_fixup(NTFS_RECORD *b, const u32 size)
0110 {
0111     le16 *usa_pos, *data_pos;
0112     u16 usa_ofs, usa_count, usn;
0113     le16 le_usn;
0114 
0115     /* Sanity check + only fixup if it makes sense. */
0116     if (!b || ntfs_is_baad_record(b->magic) ||
0117             ntfs_is_hole_record(b->magic))
0118         return -EINVAL;
0119     /* Setup the variables. */
0120     usa_ofs = le16_to_cpu(b->usa_ofs);
0121     /* Decrement usa_count to get number of fixups. */
0122     usa_count = le16_to_cpu(b->usa_count) - 1;
0123     /* Size and alignment checks. */
0124     if ( size & (NTFS_BLOCK_SIZE - 1)   ||
0125          usa_ofs & 1            ||
0126          usa_ofs + (usa_count * 2) > size   ||
0127          (size >> NTFS_BLOCK_SIZE_BITS) != usa_count)
0128         return -EINVAL;
0129     /* Position of usn in update sequence array. */
0130     usa_pos = (le16*)((u8*)b + usa_ofs);
0131     /*
0132      * Cyclically increment the update sequence number
0133      * (skipping 0 and -1, i.e. 0xffff).
0134      */
0135     usn = le16_to_cpup(usa_pos) + 1;
0136     if (usn == 0xffff || !usn)
0137         usn = 1;
0138     le_usn = cpu_to_le16(usn);
0139     *usa_pos = le_usn;
0140     /* Position in data of first u16 that needs fixing up. */
0141     data_pos = (le16*)b + NTFS_BLOCK_SIZE/sizeof(le16) - 1;
0142     /* Fixup all sectors. */
0143     while (usa_count--) {
0144         /*
0145          * Increment the position in the usa and save the
0146          * original data from the data buffer into the usa.
0147          */
0148         *(++usa_pos) = *data_pos;
0149         /* Apply fixup to data. */
0150         *data_pos = le_usn;
0151         /* Increment position in data as well. */
0152         data_pos += NTFS_BLOCK_SIZE/sizeof(le16);
0153     }
0154     return 0;
0155 }
0156 
0157 /**
0158  * post_write_mst_fixup - fast deprotect multi sector transfer protected data
0159  * @b:      pointer to the data to deprotect
0160  *
0161  * Perform the necessary post write multi sector transfer fixup, not checking
0162  * for any errors, because we assume we have just used pre_write_mst_fixup(),
0163  * thus the data will be fine or we would never have gotten here.
0164  */
0165 void post_write_mst_fixup(NTFS_RECORD *b)
0166 {
0167     le16 *usa_pos, *data_pos;
0168 
0169     u16 usa_ofs = le16_to_cpu(b->usa_ofs);
0170     u16 usa_count = le16_to_cpu(b->usa_count) - 1;
0171 
0172     /* Position of usn in update sequence array. */
0173     usa_pos = (le16*)b + usa_ofs/sizeof(le16);
0174 
0175     /* Position in protected data of first u16 that needs fixing up. */
0176     data_pos = (le16*)b + NTFS_BLOCK_SIZE/sizeof(le16) - 1;
0177 
0178     /* Fixup all sectors. */
0179     while (usa_count--) {
0180         /*
0181          * Increment position in usa and restore original data from
0182          * the usa into the data buffer.
0183          */
0184         *data_pos = *(++usa_pos);
0185 
0186         /* Increment position in data as well. */
0187         data_pos += NTFS_BLOCK_SIZE/sizeof(le16);
0188     }
0189 }