]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/hfs/hfs_endian.c
xnu-2782.1.97.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_endian.c
index 41251934fce9c1e556e4f4ecd250f7c11cee2577..50fb1ddd9a509d3d76d7ad5e4439149225a7ab0f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
+ * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  * The direction parameter must be kSwapBTNodeBigToHost or kSwapBTNodeHostToBig.
  * The kSwapBTNodeHeaderRecordOnly "direction" is not valid for these routines.
  */
-static int hfs_swap_HFSPlusBTInternalNode (BlockDescriptor *src, HFSCatalogNodeID fileID, enum HFSBTSwapDirection direction);
-static int hfs_swap_HFSBTInternalNode (BlockDescriptor *src, HFSCatalogNodeID fileID, enum HFSBTSwapDirection direction);
+int hfs_swap_HFSPlusBTInternalNode (BlockDescriptor *src, HFSCatalogNodeID fileID, enum HFSBTSwapDirection direction);
+void hfs_swap_HFSPlusForkData (HFSPlusForkData *src);
+
+#if CONFIG_HFS_STD
+int hfs_swap_HFSBTInternalNode (BlockDescriptor *src, HFSCatalogNodeID fileID, enum HFSBTSwapDirection direction);
+#endif
 
 /*
  * hfs_swap_HFSPlusForkData
  */
-static void
+void
 hfs_swap_HFSPlusForkData (
     HFSPlusForkData *src
 )
@@ -83,22 +87,23 @@ int
 hfs_swap_BTNode (
     BlockDescriptor *src,
     vnode_t vp,
-    enum HFSBTSwapDirection direction
+    enum HFSBTSwapDirection direction,
+    u_int8_t allow_empty_node
 )
 {
     BTNodeDescriptor *srcDesc = src->buffer;
     u_int16_t *srcOffs = NULL;
        BTreeControlBlockPtr btcb = (BTreeControlBlockPtr)VTOF(vp)->fcbBTCBPtr;
-    u_int32_t i;
+    u_int16_t i; /* index to match srcDesc->numRecords */
     int error = 0;
 
 #ifdef ENDIAN_DEBUG
     if (direction == kSwapBTNodeBigToHost) {
-        printf ("BE -> Native Swap\n");
+        printf ("hfs: BE -> Native Swap\n");
     } else if (direction == kSwapBTNodeHostToBig) {
-        printf ("Native -> BE Swap\n");
+        printf ("hfs: Native -> BE Swap\n");
     } else if (direction == kSwapBTNodeHeaderRecordOnly) {
-        printf ("Not swapping descriptors\n");
+        printf ("hfs: Not swapping descriptors\n");
     } else {
         panic ("hfs_swap_BTNode: This is impossible");
     }
@@ -115,7 +120,9 @@ hfs_swap_BTNode (
        /*
         * When first opening a BTree, we have to read the header node before the
         * control block is initialized.  In this case, totalNodes will be zero,
-        * so skip the bounds checking.
+        * so skip the bounds checking. Also, we should ignore the header node when
+                * checking for invalid forwards and backwards links, since the header node's
+                * links can point back to itself legitimately.
         */
        if (btcb->totalNodes != 0) {
                        if (srcDesc->fLink >= btcb->totalNodes) {
@@ -128,6 +135,21 @@ hfs_swap_BTNode (
                                error = fsBTInvalidHeaderErr;
                                goto fail;
                        }
+                       
+                       if ((src->blockNum != 0) && (srcDesc->fLink == (u_int32_t) src->blockNum)) {
+                               printf("hfs_swap_BTNode: invalid forward link (0x%08x == 0x%08x)\n",
+                                               srcDesc->fLink, (u_int32_t) src->blockNum);
+                               error = fsBTInvalidHeaderErr;
+                               goto fail;
+                       }
+                       if ((src->blockNum != 0) && (srcDesc->bLink == (u_int32_t) src->blockNum)) {
+                               printf("hfs_swap_BTNode: invalid backward link (0x%08x == 0x%08x)\n",
+                                               srcDesc->bLink, (u_int32_t) src->blockNum);
+                               error = fsBTInvalidHeaderErr;
+                               goto fail;
+                       }
+
+
                }
                
                /* 
@@ -142,7 +164,7 @@ hfs_swap_BTNode (
                /*
                 * Check srcDesc->height.  Don't swap it because it's only one byte.
                 */
-               if (srcDesc->height > btcb->treeDepth) {
+               if (srcDesc->height > kMaxTreeDepth) {
                        printf("hfs_swap_BTNode: invalid node height (%d)\n", srcDesc->height);
                        error = fsBTInvalidHeaderErr;
                        goto fail;
@@ -177,9 +199,13 @@ hfs_swap_BTNode (
              * Sanity check: must be even, and within the node itself.
              *
              * We may be called to swap an unused node, which contains all zeroes.
-             * This is why we allow the record offset to be zero.
+                        * Unused nodes are expected only when allow_empty_node is true.
+                        * If it is false and record offset is zero, return error.
              */
-            if ((srcOffs[i] & 1) || (srcOffs[i] < sizeof(BTNodeDescriptor) && srcOffs[i] != 0) || (srcOffs[i] >= src->blockSize)) {
+            if ((srcOffs[i] & 1) || (
+                           (allow_empty_node == false) && (srcOffs[i] == 0)) ||
+                               (srcOffs[i] < sizeof(BTNodeDescriptor) && srcOffs[i] != 0) || 
+                               (srcOffs[i] >= src->blockSize)) { 
                printf("hfs_swap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc->numRecords-i-1, srcOffs[i]);
                error = fsBTInvalidHeaderErr;
                goto fail;
@@ -206,9 +232,12 @@ hfs_swap_BTNode (
 
         if (VTOVCB(vp)->vcbSigWord == kHFSPlusSigWord) {
             error = hfs_swap_HFSPlusBTInternalNode (src, VTOC(vp)->c_fileid, direction);
-        } else {
+        } 
+#if CONFIG_HFS_STD
+               else {
             error = hfs_swap_HFSBTInternalNode (src, VTOC(vp)->c_fileid, direction);
         }
+#endif
         
         if (error) goto fail;
         
@@ -249,17 +278,34 @@ hfs_swap_BTNode (
     if (direction == kSwapBTNodeHostToBig) {
                /*
                 * Sanity check and swap the forward and backward links.
+                * Ignore the header node since its forward and backwards links can legitimately
+                * point to itself.
                 */
                if (srcDesc->fLink >= btcb->totalNodes) {
                        panic("hfs_UNswap_BTNode: invalid forward link (0x%08X)\n", srcDesc->fLink);
                        error = fsBTInvalidHeaderErr;
                        goto fail;
                }
+               if ((src->blockNum != 0) && (srcDesc->fLink == (u_int32_t) src->blockNum)) {
+                       panic ("hfs_UNswap_BTNode: invalid forward link (0x%08x == 0x%08x)\n", 
+                                       srcDesc->fLink, (u_int32_t) src->blockNum);
+                       error = fsBTInvalidHeaderErr;
+                       goto fail;
+               }
+               
                if (srcDesc->bLink >= btcb->totalNodes) {
                        panic("hfs_UNswap_BTNode: invalid backward link (0x%08X)\n", srcDesc->bLink);
                        error = fsBTInvalidHeaderErr;
                        goto fail;
                }
+               if ((src->blockNum != 0) && (srcDesc->bLink == (u_int32_t) src->blockNum)) {
+                       panic ("hfs_UNswap_BTNode: invalid backward link (0x%08x == 0x%08x)\n", 
+                                       srcDesc->bLink, (u_int32_t) src->blockNum);
+                       error = fsBTInvalidHeaderErr;
+                       goto fail;
+               }
+
+
         srcDesc->fLink         = SWAP_BE32 (srcDesc->fLink);
         srcDesc->bLink         = SWAP_BE32 (srcDesc->bLink);
     
@@ -275,7 +321,7 @@ hfs_swap_BTNode (
                /* 
                 * Check srcDesc->height.  Don't swap it because it's only one byte.
                 */
-               if (srcDesc->height > btcb->treeDepth) {
+               if (srcDesc->height > kMaxTreeDepth) {
                        panic("hfs_UNswap_BTNode: invalid node height (%d)\n", srcDesc->height);
                        error = fsBTInvalidHeaderErr;
                        goto fail;
@@ -306,9 +352,15 @@ hfs_swap_BTNode (
              * Sanity check: must be even, and within the node itself.
              *
              * We may be called to swap an unused node, which contains all zeroes.
+                * This can happen when the last record from a node gets deleted.
              * This is why we allow the record offset to be zero.
+                * Unused nodes are expected only when allow_empty_node is true 
+                * (the caller should set it to true for kSwapBTNodeBigToHost). 
              */
-            if ((srcOffs[i] & 1) || (srcOffs[i] < sizeof(BTNodeDescriptor) && srcOffs[i] != 0) || (srcOffs[i] >= src->blockSize)) {
+            if ((srcOffs[i] & 1) || 
+                           ((allow_empty_node == false) && (srcOffs[i] == 0)) ||
+                               (srcOffs[i] < sizeof(BTNodeDescriptor) && srcOffs[i] != 0) || 
+                               (srcOffs[i] >= src->blockSize)) {
                panic("hfs_UNswap_BTNode: record #%d invalid offset (0x%04X)\n", srcDesc->numRecords-i-1, srcOffs[i]);
                error = fsBTInvalidHeaderErr;
                goto fail;
@@ -336,15 +388,15 @@ fail:
                /*
                 * Log some useful information about where the corrupt node is.
                 */
-               printf("node=%lld fileID=%u volume=%s device=%s\n", src->blockNum, VTOC(vp)->c_fileid,
+               printf("hfs: node=%lld fileID=%u volume=%s device=%s\n", src->blockNum, VTOC(vp)->c_fileid,
                        VTOVCB(vp)->vcbVN, vfs_statfs(vnode_mount(vp))->f_mntfromname);
-               hfs_mark_volume_inconsistent(VTOVCB(vp));
+               hfs_mark_inconsistent(VTOVCB(vp), HFS_INCONSISTENCY_DETECTED);
        }
        
     return (error);
 }
 
-static int
+int
 hfs_swap_HFSPlusBTInternalNode (
     BlockDescriptor *src,
     HFSCatalogNodeID fileID,
@@ -445,7 +497,7 @@ hfs_swap_HFSPlusBTInternalNode (
              * to be sure the current record doesn't overflow into the next
              * record.
              */
-                       nextRecord = (char *)src->buffer + srcOffs[i-1];
+                       nextRecord = (char *)src->buffer + (uintptr_t)(srcOffs[i-1]);
 
                        /*
                         * Make sure we can safely dereference the keyLength and parentID fields. 
@@ -880,7 +932,8 @@ hfs_swap_HFSPlusBTInternalNode (
     return (0);
 }
 
-static int
+#if CONFIG_HFS_STD
+int
 hfs_swap_HFSBTInternalNode (
     BlockDescriptor *src,
     HFSCatalogNodeID fileID,
@@ -1170,3 +1223,5 @@ hfs_swap_HFSBTInternalNode (
 
     return (0);
 }
+#endif
+