Skip to content

Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.

C

APFS: the iOS 16 cryptexes as Apple ships them -- sealed trees, the extent tree, LZBITMAP

vcvkk3ca6320159af
diff --git a/tools/apfs/apfs-tool.c b/tools/apfs/apfs-tool.c
index 05937ee..cbe793a 100644
--- a/tools/apfs/apfs-tool.c
+++ b/tools/apfs/apfs-tool.c
@@ -367,10 +367,13 @@ cmd_stat(const char *path, const char *where)
ino.alloced_size);
printf("flags internal 0x%" PRIx64 " bsd 0x%x%s\n", ino.internal_flags,
ino.bsd_flags, ino.is_sparse ? " sparse" : "");
+ memset(&cf, 0, sizeof(cf));
+ cf.held = APFS_NO_PIECE;
if (apfs_cfile_init(&vol, &ino, &cf) == 0 && cf.compressed) {
printf("decmpfs type %u uncompressed %" PRIu64 " %s\n", cf.type, cf.size,
cf.in_rsrc ? "in the resource fork" : "in the attribute");
}
+ apfs_cfile_fini(&cf);
apfs_xattr_list(&vol, id, xattr_print_cb, NULL);
apfs_nx_close(&nx);
return 0;
@@ -407,6 +410,14 @@ cmd_cat(const char *path, const char *where)
}
memset(&cf, 0, sizeof(cf));
cf.held = APFS_NO_PIECE;
+ {
+ uint64_t size = 0;
+
+ /* (a compressed file's size is in its decmpfs header, not its inode) */
+ if (apfs_inode_size(&vol, &ino, &size) == 0) {
+ ino.size = size;
+ }
+ }
while (off < ino.size) {
size_t got = 0;
diff --git a/tools/apfs/build-tool.sh b/tools/apfs/build-tool.sh
index ad5b8dd..6ebee6e 100755
--- a/tools/apfs/build-tool.sh
+++ b/tools/apfs/build-tool.sh
@@ -40,5 +40,5 @@ else
echo "build-tool.sh: no bsd/vfs/vfs_unicode.c: names other than plain ASCII are found by reading the directory" >&2
fi
cc -std=c11 -D_DEFAULT_SOURCE -O2 -Wall -Wextra -Wno-unused-parameter -I"$SRC" -I"$HERE" \
- $TOOL_CFLAGS -o "$OUT" "$HERE/apfs-tool.c" "$SRC/apfs_lib.c" "$OBJ/apfs_deflate.o" $UNI -lz -pthread
+ $TOOL_CFLAGS -o "$OUT" "$HERE/apfs-tool.c" "$SRC/apfs_lib.c" "$SRC/apfs_zbitmap.c" "$OBJ/apfs_deflate.o" $UNI -lz -lcrypto -pthread
echo "build-tool.sh: $OUT (from $SRC)"
diff --git a/xnu/files/bsd/miscfs/apfs/apfs_format.h b/xnu/files/bsd/miscfs/apfs/apfs_format.h
index 391df14..973046d 100644
--- a/xnu/files/bsd/miscfs/apfs/apfs_format.h
+++ b/xnu/files/bsd/miscfs/apfs/apfs_format.h
@@ -748,6 +748,9 @@ struct apfs_decmpfs_disk_header {
#define APFS_DECMPFS_TYPE_RAW_RSRC 10 /* uncompressed, bytes in the resource fork */
#define APFS_DECMPFS_TYPE_LZFSE_ATTR 11
#define APFS_DECMPFS_TYPE_LZFSE_RSRC 12
+#define APFS_DECMPFS_TYPE_LZBITMAP_ATTR 13 /* LZBITMAP, bytes in the attribute */
+#define APFS_DECMPFS_TYPE_LZBITMAP_RSRC 14 /* LZBITMAP, 64 KB pieces in the resource fork behind
+ a table of n+1 little-endian offsets */
/* the pieces a resource-fork-compressed file is cut into */
#define APFS_DECMPFS_BLOCK_SIZE 65536
diff --git a/xnu/files/bsd/miscfs/apfs/apfs_lib.c b/xnu/files/bsd/miscfs/apfs/apfs_lib.c
index d7aab02..57df8fb 100644
--- a/xnu/files/bsd/miscfs/apfs/apfs_lib.c
+++ b/xnu/files/bsd/miscfs/apfs/apfs_lib.c
@@ -26,6 +26,12 @@
#include <zlib.h>
#endif
#include "libdeflate/libdeflate.h"
+#include "libzbitmap/libzbitmap.h"
+#ifdef KERNEL
+#include <libkern/crypto/sha2.h>
+#else
+#include <openssl/sha.h>
+#endif
/* The pieces a resource-fork-compressed file is cut into. */
#define APFS_RSRC_PIECE APFS_DECMPFS_BLOCK_SIZE
@@ -640,32 +646,139 @@ tree_node_addr(struct apfs_btree *tree, uint64_t oid, uint64_t *addr);
* ENOENT, which a caller scanning for records reads as "there are no more" -- the records
* of the object quietly go missing instead of the read failing.
*/
+/*
+ * A sealed volume (the iOS cryptexes) keeps its file-system tree in nodes without an object
+ * header -- no Fletcher checksum -- and each parent keeps the SHA-256 of each child's whole
+ * block instead. Checked once per block the cache brings in (b->checked, which for such a
+ * block means "its hash was right"); `len` is 32 on the way down, 8 when a cursor comes back
+ * to a node it had left (apfs_cursor.hash8). The root has no parent: Apple checks it against
+ * the volume's root hash, which is kept outside the image (the IPSW's .root_hash), and
+ * nothing here has that.
+ */
+static void
+apfs_sha256(const uint8_t *data, size_t len, uint8_t out[32])
+{
+#ifdef KERNEL
+ SHA256_CTX c;
+
+ SHA256_Init(&c);
+ SHA256_Update(&c, data, len);
+ SHA256_Final(out, &c);
+#else
+ SHA256(data, len, out);
+#endif
+}
+
static int
-node_at(struct apfs_btree *tree, uint64_t addr, const uint8_t **blk, struct node *n)
+node_hash_check(struct apfs_nx *nx, uint64_t addr, const uint8_t *blk, const uint8_t *hash, size_t len)
{
- int error = nx_block(tree->nx, addr, blk, true);
+ struct apfs_buf *b = cache_find(nx, addr);
+ uint8_t d[32];
+
+ if (b != NULL && b->checked) {
+ return 0;
+ }
+ apfs_sha256(blk, nx->block_size, d);
+ if (memcmp(d, hash, len) != 0) {
+ apfs_log("block %llu: not the node its parent names (SHA-256)\n", (unsigned long long)addr);
+ return EIO;
+ }
+ if (b != NULL) {
+ b->checked = true;
+ }
+ return 0;
+}
+
+/* hash8: for a sealed tree's node, the first 8 bytes of its SHA-256 (0 for the root: none) */
+static int
+node_at_h(struct apfs_btree *tree, uint64_t addr, uint64_t hash8, const uint8_t **blk, struct node *n)
+{
+ bool noheader = (tree->flags & APFS_BTREE_NOHEADER) != 0;
+ int error = nx_block(tree->nx, addr, blk, !noheader);
if (error) {
return error;
}
- if (obj_type(*blk) != APFS_OBJECT_TYPE_BTREE &&
+ if (noheader) {
+ uint8_t h[8];
+
+ if ((le16(*blk + 32) & APFS_BTNODE_NOHEADER) == 0) {
+ return EIO;
+ }
+ if (addr != tree->root) {
+ if (hash8 == 0) {
+ return EIO; /* (a node below the root always comes with its parent's hash) */
+ }
+ memcpy(h, &hash8, 8);
+ error = node_hash_check(tree->nx, addr, *blk, h, 8);
+ if (error) {
+ return error;
+ }
+ }
+ } else if (obj_type(*blk) != APFS_OBJECT_TYPE_BTREE &&
obj_type(*blk) != APFS_OBJECT_TYPE_BTREE_NODE) {
return EIO;
}
return node_parse(tree, *blk, n);
}
-/* The block address of child i of a non-leaf node. */
static int
-node_child_addr(struct apfs_btree *tree, const struct node *n, uint32_t i, uint64_t *addr)
+node_at(struct apfs_btree *tree, uint64_t addr, const uint8_t **blk, struct node *n)
+{
+ return node_at_h(tree, addr, 0, blk, n);
+}
+
+/*
+ * The block address of child i of a non-leaf node. In a sealed (hashed) tree the value also
+ * holds the child's SHA-256, and the id in it is an offset from the root's id -- apfsck says
+ * so as well; Apple's reference does not. The child's block is checked against the hash
+ * here, and the first 8 bytes of it given back for a cursor to keep (hash8, may be NULL).
+ */
+static int
+node_child_addr(struct apfs_btree *tree, const struct node *n, uint32_t i, uint64_t *addr,
+ uint64_t *hash8)
{
+ const uint8_t *k, *v, *blk;
+ uint16_t kl, vl;
+ uint8_t hash[32];
uint64_t oid;
- int error = node_child(tree, n, i, &oid);
+ int error;
+ if ((tree->flags & APFS_BTREE_HASHED) == 0) {
+ error = node_child(tree, n, i, &oid);
+ if (error) {
+ return error;
+ }
+ if (hash8 != NULL) {
+ *hash8 = 0;
+ }
+ return tree_node_addr(tree, oid, addr);
+ }
+ error = node_entry(tree, n, i, &k, &kl, &v, &vl);
+ if (error) {
+ return error;
+ }
+ if (v == NULL || vl != 8 + sizeof(hash)) {
+ apfs_log("a sealed tree's node with a %u-byte child entry (only SHA-256 is known here)\n", vl);
+ return ENOTSUP;
+ }
+ oid = le64(v) + tree->root_oid;
+ memcpy(hash, v + 8, sizeof(hash));
+ error = tree_node_addr(tree, oid, addr);
if (error) {
return error;
}
- return tree_node_addr(tree, oid, addr);
+ error = nx_block(tree->nx, *addr, &blk, (tree->flags & APFS_BTREE_NOHEADER) == 0);
+ if (error) {
+ return error;
+ }
+ if (tree->flags & APFS_BTREE_NOHEADER) {
+ error = node_hash_check(tree->nx, *addr, blk, hash, sizeof(hash));
+ }
+ if (error == 0 && hash8 != NULL) {
+ memcpy(hash8, hash, 8);
+ }
+ return error;
}
/* ------------------------------------------------------------------ object maps */
@@ -706,19 +819,29 @@ btree_open(struct apfs_nx *nx, struct apfs_omap *omap, uint64_t root_oid,
return error;
}
}
- error = nx_block(nx, addr, &blk, true);
- if (error) {
- return error;
- }
- if (obj_type(blk) != APFS_OBJECT_TYPE_BTREE) {
- apfs_log("block %llu is not a b-tree root\n", (unsigned long long)addr);
- return EIO;
- }
if (bs < 56 + sizeof(struct apfs_btree_info)) {
return EIO;
}
+ error = nx_block(nx, addr, &blk, false);
+ if (error) {
+ return error;
+ }
info = blk + bs - sizeof(struct apfs_btree_info);
+ if (le64(blk) == 0 && le64(blk + 8) == 0 && le32(blk + 24) == 0 &&
+ (le16(blk + 32) & APFS_BTNODE_NOHEADER) && (le32(info) & APFS_BTREE_NOHEADER)) {
+ /* a sealed tree's root: no header, no checksum (see node_hash_check) */
+ } else {
+ error = nx_block(nx, addr, &blk, true);
+ if (error) {
+ return error;
+ }
+ if (obj_type(blk) != APFS_OBJECT_TYPE_BTREE) {
+ apfs_log("block %llu is not a b-tree root\n", (unsigned long long)addr);
+ return EIO;
+ }
+ }
tree->root = addr;
+ tree->root_oid = root_oid;
tree->flags = le32(info);
tree->key_size = le32(info + 8);
tree->val_size = le32(info + 12);
@@ -1050,7 +1173,8 @@ int
apfs_vol_open(struct apfs_nx *nx, uint32_t index, struct apfs_vol *vol)
{
const uint8_t *blk;
- uint64_t addr, omap_oid, root_oid;
+ uint64_t addr, omap_oid, root_oid, fext_oid;
+ uint32_t fext_type;
int error;
if (index >= APFS_NX_MAX_FILE_SYSTEMS || nx->fs_oid[index] == 0) {
@@ -1084,6 +1208,8 @@ apfs_vol_open(struct apfs_nx *nx, uint32_t index, struct apfs_vol *vol)
memcpy(vol->name, blk + 704, APFS_VOLNAME_LEN);
vol->name[APFS_VOLNAME_LEN - 1] = '\0';
vol->role = le16(blk + 964);
+ fext_oid = le64(blk + 0x408); /* apfs_fext_tree_oid, apfs_fext_tree_type */
+ fext_type = le32(blk + 0x410);
vol->hashed_names = (vol->incompat & (APFS_INCOMPAT_CASE_INSENSITIVE |
APFS_INCOMPAT_NORMALIZATION_INSENSITIVE)) != 0;
@@ -1103,7 +1229,23 @@ apfs_vol_open(struct apfs_nx *nx, uint32_t index, struct apfs_vol *vol)
if (error) {
return error;
}
- return btree_open(nx, &vol->omap, root_oid, &vol->tree);
+ error = btree_open(nx, &vol->omap, root_oid, &vol->tree);
+ if (error) {
+ return error;
+ }
+ if (fext_oid != 0) {
+ if ((fext_type & APFS_OBJ_STORAGETYPE_MASK) != APFS_OBJ_PHYSICAL) {
+ apfs_log("volume %u (%s): an extent tree that is not physical (0x%x)\n", index, vol->name,
+ fext_type);
+ return ENOTSUP;
+ }
+ error = btree_open(nx, NULL, fext_oid, &vol->fext);
+ if (error) {
+ return error;
+ }
+ vol->has_fext = true;
+ }
+ return 0;
}
/* ------------------------------------------------------------------ records */
@@ -1200,12 +1342,20 @@ seek_to(struct apfs_btree *tree, const struct seek_target *t, struct apfs_cursor
*/
pick = (i > 0) ? i - 1 : 0;
cur->index[cur->depth] = pick;
- error = node_child_addr(tree, &n, pick, &addr);
- if (error) {
- return error;
+ {
+ uint64_t h8 = 0;
+
+ error = node_child_addr(tree, &n, pick, &addr, &h8);
+ if (error) {
+ return error;
+ }
+ if (cur->depth + 1 >= APFS_MAX_LEVELS) {
+ return EIO;
+ }
+ cur->depth++;
+ cur->hash8[cur->depth] = h8;
+ error = node_at_h(tree, addr, h8, &blk, &n);
}
- cur->depth++;
- error = node_at(tree, addr, &blk, &n);
if (error) {
return error;
}
@@ -1237,7 +1387,7 @@ apfs_cursor_next(struct apfs_cursor *cur, struct apfs_rec *rec)
return ENOENT;
}
for (;;) {
- error = node_at(tree, cur->node[cur->depth], &blk, &n);
+ error = node_at_h(tree, cur->node[cur->depth], cur->hash8[cur->depth], &blk, &n);
if (error) {
return error;
}
@@ -1271,7 +1421,7 @@ apfs_cursor_next(struct apfs_cursor *cur, struct apfs_rec *rec)
return ENOENT;
}
cur->depth--;
- error = node_at(tree, cur->node[cur->depth], &blk, &n);
+ error = node_at_h(tree, cur->node[cur->depth], cur->hash8[cur->depth], &blk, &n);
if (error) {
return error;
}
@@ -1287,7 +1437,9 @@ apfs_cursor_next(struct apfs_cursor *cur, struct apfs_rec *rec)
for (;;) {
uint64_t child;
- error = node_child_addr(tree, &n, cur->index[cur->depth], &child);
+ uint64_t h8 = 0;
+
+ error = node_child_addr(tree, &n, cur->index[cur->depth], &child, &h8);
if (error) {
return error;
}
@@ -1297,7 +1449,8 @@ apfs_cursor_next(struct apfs_cursor *cur, struct apfs_rec *rec)
cur->depth++;
cur->node[cur->depth] = child;
cur->index[cur->depth] = 0;
- error = node_at(tree, child, &blk, &n);
+ cur->hash8[cur->depth] = h8;
+ error = node_at_h(tree, child, h8, &blk, &n);
if (error) {
return error;
}
@@ -2018,12 +2171,138 @@ apfs_xattr_read(struct apfs_vol *vol, uint64_t obj_id, const char *name, void *b
/* ------------------------------------------------------------------ contents */
+/*
+ * The extents of one stream, in order: the file-system tree's records of it, or on a sealed
+ * volume its entries in the extent tree (key: stream id and logical offset; value: length
+ * with flags, and the first block). ext_begin(..., off, ...) starts at the extent that may
+ * reach `off`, or earlier.
+ */
+struct ext_iter {
+ struct apfs_cursor cur;
+ uint64_t id;
+ bool fext;
+};
+
+static int
+fext_seek(struct apfs_btree *tree, uint64_t id, uint64_t off, struct apfs_cursor *cur)
+{
+ const uint8_t *blk;
+ struct node n;
+ uint64_t addr = tree->root;
+ int error;
+
+ memset(cur, 0, sizeof(*cur));
+ cur->tree = tree;
+ error = node_at(tree, addr, &blk, &n);
+ if (error) {
+ return error;
+ }
+ for (;;) {
+ uint32_t lo = 0, hi = n.nkeys, pick;
+
+ if (cur->depth >= APFS_MAX_LEVELS) {
+ return EIO;
+ }
+ cur->node[cur->depth] = addr;
+ if (n.nkeys == 0) {
+ cur->done = true;
+ return 0;
+ }
+ /* the last entry at or before (id, off), by halving */
+ while (lo < hi) {
+ uint32_t mid = lo + (hi - lo) / 2;
+ const uint8_t *k, *v;
+ uint16_t kl, vl;
+
+ error = node_entry(tree, &n, mid, &k, &kl, &v, &vl);
+ if (error) {
+ return error;
+ }
+ if (kl < 16) {
+ return EIO;
+ }
+ if (le64(k) < id || (le64(k) == id && le64(k + 8) <= off)) {
+ lo = mid + 1;
+ } else {
+ hi = mid;
+ }
+ }
+ pick = lo > 0 ? lo - 1 : 0;
+ cur->index[cur->depth] = pick;
+ if (n.leaf) {
+ return 0;
+ }
+ error = node_child_addr(tree, &n, pick, &addr, NULL);
+ if (error) {
+ return error;
+ }
+ if (cur->depth + 1 >= APFS_MAX_LEVELS) {
+ return EIO;
+ }
+ cur->depth++;
+ error = node_at(tree, addr, &blk, &n);
+ if (error) {
+ return error;
+ }
+ }
+}
+
+static int
+ext_begin(struct apfs_vol *vol, uint64_t id, uint64_t off, struct ext_iter *it)
+{
+ it->id = id;
+ it->fext = vol->has_fext;
+ if (it->fext) {
+ return fext_seek(&vol->fext, id, off, &it->cur);
+ }
+ return apfs_seek(&vol->tree, id, APFS_TYPE_FILE_EXTENT, &it->cur);
+}
+
+/* The next extent; ENOENT after the last. */
+static int
+ext_next(struct ext_iter *it, uint64_t *logical, uint64_t *length, uint64_t *phys)
+{
+ struct apfs_rec rec;
+ int error;
+
+ for (;;) {
+ error = apfs_cursor_next(&it->cur, &rec);
+ if (error) {
+ return error;
+ }
+ if (it->fext) {
+ uint64_t id;
+
+ if (rec.key_len < 16 || rec.val == NULL || rec.val_len < 16) {
+ return EIO;
+ }
+ id = le64(rec.key);
+ if (id < it->id) {
+ continue; /* (the seek may land on the stream before) */
+ }
+ if (id > it->id) {
+ return ENOENT;
+ }
+ } else {
+ if (rec.obj_id != it->id || rec.type != APFS_TYPE_FILE_EXTENT) {
+ return ENOENT;
+ }
+ if (rec.key_len < 16 || rec.val == NULL || rec.val_len < 24) {
+ return EIO;
+ }
+ }
+ *logical = le64(rec.key + 8);
+ *length = le64(rec.val) & APFS_J_FILE_EXTENT_LEN_MASK;
+ *phys = le64(rec.val + 8);
+ return 0;
+ }
+}
+
int
apfs_read_stream(struct apfs_vol *vol, uint64_t stream_id, uint64_t stream_size,
uint64_t off, void *buf, size_t len, size_t *out)
{
- struct apfs_cursor cur;
- struct apfs_rec rec;
+ struct ext_iter it;
uint8_t *dst = buf;
uint32_t bs = vol->nx->block_size;
uint64_t end;
@@ -2040,29 +2319,20 @@ apfs_read_stream(struct apfs_vol *vol, uint64_t stream_id, uint64_t stream_size,
/* holes read as zeros, so start from zeros and fill in what the extents cover */
memset(dst, 0, len);
- error = apfs_seek(&vol->tree, stream_id, APFS_TYPE_FILE_EXTENT, &cur);
+ error = ext_begin(vol, stream_id, off, &it);
if (error) {
return error;
}
for (;;) {
uint64_t logical, length, phys;
- error = apfs_cursor_next(&cur, &rec);
+ error = ext_next(&it, &logical, &length, &phys);
if (error == ENOENT) {
break;
}
if (error) {
return error;
}
- if (rec.obj_id != stream_id || rec.type != APFS_TYPE_FILE_EXTENT) {
- break;
- }
- if (rec.key_len < 16 || rec.val == NULL || rec.val_len < 24) {
- return EIO;
- }
- logical = le64(rec.key + 8);
- length = le64(rec.val) & APFS_J_FILE_EXTENT_LEN_MASK;
- phys = le64(rec.val + 8);
if (length == 0 || (length % bs) != 0) {
return EIO;
}
@@ -2140,37 +2410,27 @@ static int
stream_plan(struct apfs_vol *vol, uint64_t stream_id, uint64_t off, uint64_t len,
struct apfs_run *runs, uint32_t max, uint32_t *nruns, uint64_t *covered)
{
- struct apfs_cursor cur;
- struct apfs_rec rec;
+ struct ext_iter it;
uint32_t bs = vol->nx->block_size;
uint64_t end = off + len;
int error;
*nruns = 0;
*covered = len;
- error = apfs_seek(&vol->tree, stream_id, APFS_TYPE_FILE_EXTENT, &cur);
+ error = ext_begin(vol, stream_id, off, &it);
if (error) {
return error;
}
for (;;) {
uint64_t logical, length, phys, from, to, inner;
- error = apfs_cursor_next(&cur, &rec);
+ error = ext_next(&it, &logical, &length, &phys);
if (error == ENOENT) {
break;
}
if (error) {
return error;
}
- if (rec.obj_id != stream_id || rec.type != APFS_TYPE_FILE_EXTENT) {
- break;
- }
- if (rec.key_len < 16 || rec.val == NULL || rec.val_len < 24) {
- return EIO;
- }
- logical = le64(rec.key + 8);
- length = le64(rec.val) & APFS_J_FILE_EXTENT_LEN_MASK;
- phys = le64(rec.val + 8);
if (length == 0 || (length % bs) != 0) {
return EIO;
}
@@ -2324,10 +2584,12 @@ cfile_header(struct apfs_vol *vol, const struct apfs_inode *ino, bool *compresse
switch (*type) {
case APFS_DECMPFS_TYPE_ZLIB_ATTR:
case APFS_DECMPFS_TYPE_RAW_ATTR:
+ case APFS_DECMPFS_TYPE_LZBITMAP_ATTR:
*in_rsrc = false;
return 0;
case APFS_DECMPFS_TYPE_ZLIB_RSRC:
case APFS_DECMPFS_TYPE_RAW_RSRC:
+ case APFS_DECMPFS_TYPE_LZBITMAP_RSRC:
*in_rsrc = true;
return 0;
default:
@@ -2368,6 +2630,57 @@ apfs_cfile_fini(struct apfs_cfile *cf)
* the data offset is 256 in every file here, so the table lands at 260, but it has to be
* read rather than assumed.
*/
+/*
+ * The table of an LZBITMAP resource fork (type 14): no resource-fork header, but straight
+ * from its start n+1 little-endian offsets, piece i lying between the i-th and the next.
+ */
+static int
+cfile_read_offsets(struct apfs_vol *vol, const struct apfs_inode *ino, struct apfs_cfile *cf)
+{
+ uint32_t count = (uint32_t)((cf->size + APFS_DECMPFS_BLOCK_SIZE - 1) / APFS_DECMPFS_BLOCK_SIZE);
+ size_t tbl_len = ((size_t)count + 1) * 4, got = 0;
+ uint8_t *tbl;
+ uint32_t i;
+ int error;
+
+ if (count == 0 || tbl_len > cf->rsrc_size) {
+ apfs_log("inode %llu: an LZBITMAP resource fork of %llu bytes for %llu bytes of file\n",
+ (unsigned long long)ino->obj_id, (unsigned long long)cf->rsrc_size,
+ (unsigned long long)cf->size);
+ return EIO;
+ }
+ tbl = apfs_alloc(tbl_len);
+ cf->piece_off = apfs_alloc((size_t)count * sizeof(uint32_t));
+ cf->piece_len = apfs_alloc((size_t)count * sizeof(uint32_t));
+ if (tbl == NULL || cf->piece_off == NULL || cf->piece_len == NULL) {
+ error = ENOMEM;
+ goto out;
+ }
+ cf->piece_count = count;
+ cf->table_base = 0;
+ error = apfs_read_stream(vol, cf->rsrc_id, cf->rsrc_size, 0, tbl, tbl_len, &got);
+ if (error == 0 && got < tbl_len) {
+ error = EIO;
+ }
+ for (i = 0; error == 0 && i < count; i++) {
+ uint32_t a = le32(tbl + 4 * i), b = le32(tbl + 4 * (i + 1));
+
+ if (b <= a || b > cf->rsrc_size) {
+ apfs_log("inode %llu: LZBITMAP piece %u from %u to %u of a %llu-byte resource fork\n",
+ (unsigned long long)ino->obj_id, i, a, b, (unsigned long long)cf->rsrc_size);
+ error = EIO;
+ break;
+ }
+ cf->piece_off[i] = a;
+ cf->piece_len[i] = b - a;
+ }
+out:
+ if (tbl != NULL) {
+ apfs_free(tbl, tbl_len);
+ }
+ return error;
+}
+
static int
cfile_read_table(struct apfs_vol *vol, const struct apfs_inode *ino,
struct apfs_cfile *cf)
@@ -2388,6 +2701,9 @@ cfile_read_table(struct apfs_vol *vol, const struct apfs_inode *ino,
}
cf->rsrc_id = xa.stream_id;
cf->rsrc_size = xa.stream_size;
+ if (cf->type == APFS_DECMPFS_TYPE_LZBITMAP_RSRC) {
+ return cfile_read_offsets(vol, ino, cf);
+ }
error = apfs_read_stream(vol, cf->rsrc_id, cf->rsrc_size, 0, head, sizeof(head),
&got);
@@ -2717,6 +3033,44 @@ apfs_inflate_counted(struct apfs_nx *nx, const uint8_t *src, size_t src_len, uin
* after it, which is how the marker's length was settled; a payload that is already
* exactly the right length is taken whole.
*/
+/*
+ * One piece of a compressed file by the file's method: zlib (types 3 and 4) as above;
+ * LZBITMAP (13 and 14) -- what the iOS 16 cryptexes use -- by libzbitmap (apfs_zbitmap.c):
+ * a piece that starts with 'Z' is a stream of it, one whose first byte has its low four bits
+ * set is stored after that byte.
+ */
+static int
+apfs_decode_piece(uint32_t type, struct libdeflate_decompressor *d, const uint8_t *src,
+ size_t src_len, uint8_t *dst, size_t dst_len, size_t *out, bool *fell_back)
+{
+ if (type != APFS_DECMPFS_TYPE_LZBITMAP_ATTR && type != APFS_DECMPFS_TYPE_LZBITMAP_RSRC) {
+ return apfs_inflate_fast(d, src, src_len, dst, dst_len, out, fell_back);
+ }
+ if (src_len == 0) {
+ return EIO;
+ }
+ if (src[0] == 'Z') {
+ size_t n = 0;
+ int r = zbm_decompress(dst, dst_len, src, src_len, &n);
+
+ if (r < 0 || n > dst_len) {
+ apfs_log("an LZBITMAP piece of %zu bytes for %zu: %d (%zu out)\n", src_len, dst_len, r, n);
+ return EIO;
+ }
+ *out = n;
+ return 0;
+ }
+ if ((src[0] & 0x0F) == 0x0F) {
+ size_t n = src_len - 1 < dst_len ? src_len - 1 : dst_len;
+
+ memcpy(dst, src + 1, n);
+ *out = n;
+ return 0;
+ }
+ apfs_log("an LZBITMAP piece of %zu bytes that starts with %02x\n", src_len, src[0]);
+ return EIO;
+}
+
static int
decmpfs_attr(struct apfs_nx *nx, uint32_t type, const uint8_t *src, size_t src_len,
size_t want, uint8_t *dst, size_t *out)
@@ -2740,9 +3094,10 @@ decmpfs_attr(struct apfs_nx *nx, uint32_t type, const uint8_t *src, size_t src_l
*out = n;
return 0;
}
- case APFS_DECMPFS_TYPE_ZLIB_ATTR: {
+ case APFS_DECMPFS_TYPE_ZLIB_ATTR:
+ case APFS_DECMPFS_TYPE_LZBITMAP_ATTR: {
bool fb = false;
- int error = apfs_inflate_fast(NULL, src, src_len, dst, want, out, &fb);
+ int error = apfs_decode_piece(type, NULL, src, src_len, dst, want, out, &fb);
if (fb && nx != NULL) {
nx->inflate_fallbacks++;
@@ -2876,6 +3231,7 @@ struct apfs_piece_job {
uint8_t *dst; /* the caller's, `want` bytes */
size_t want;
bool ra; /* read ahead, not asked for: its failure fails nothing */
+ uint32_t type; /* the file's decmpfs type: how the piece is decoded */
int error;
const uint8_t *src; /* its compressed bytes, already read with its neighbours; or NULL */
};
@@ -3012,7 +3368,7 @@ batch_one(void *arg, uint32_t item, uint32_t worker)
j->error = error;
return j->ra ? 0 : error;
}
- error = apfs_inflate_fast(sc->dec, src, j->plen, j->dst, j->want, &got,
+ error = apfs_decode_piece(j->type, sc->dec, src, j->plen, j->dst, j->want, &got,
&fell_back);
b->w[worker].inflate_ticks += apfs_ticks() - t1;
b->w[worker].out += got;
@@ -3296,6 +3652,7 @@ read_cfile_rsrc(struct apfs_vol *vol, const struct apfs_inode *ino,
j->dst = buf + *out;
j->want = want;
j->ra = false;
+ j->type = cf->type;
*out += want;
if (nbatch == APFS_BATCH) {
error = batch_run(vol, sc, nbatch);
@@ -3354,6 +3711,7 @@ read_cfile_rsrc(struct apfs_vol *vol, const struct apfs_inode *ino,
j->dst = cf->ra_data + (size_t)(k - i - 1) * APFS_DECMPFS_BLOCK_SIZE;
j->want = kwant;
j->ra = true;
+ j->type = cf->type;
cf->ra_len[k - i - 1] = (uint32_t)kwant;
nra++;
}
@@ -3398,7 +3756,7 @@ read_cfile_rsrc(struct apfs_vol *vol, const struct apfs_inode *ino,
}
t1 = apfs_ticks();
if (error == 0 && !raw_type) {
- error = apfs_inflate_fast(sc->dec, src, plen, dst, want, &got_len,
+ error = apfs_decode_piece(cf->type, sc->dec, src, plen, dst, want, &got_len,
&fell_back);
}
nx_relock(vol->nx);
diff --git a/xnu/files/bsd/miscfs/apfs/apfs_lib.h b/xnu/files/bsd/miscfs/apfs/apfs_lib.h
index 0240ea6..3784ec6 100644
--- a/xnu/files/bsd/miscfs/apfs/apfs_lib.h
+++ b/xnu/files/bsd/miscfs/apfs/apfs_lib.h
@@ -140,6 +140,7 @@ struct apfs_btree {
struct apfs_nx *nx;
struct apfs_omap *omap; /* NULL for a tree whose node ids are block addresses */
uint64_t root; /* block address of the root node */
+ uint64_t root_oid; /* its object id: a sealed tree names its nodes from it */
uint32_t flags; /* bt_flags out of the root's btree_info */
uint32_t key_size; /* nonzero only for fixed-size keys */
uint32_t val_size;
@@ -287,6 +288,12 @@ struct apfs_vol {
* fit the key and the record is refused.
*/
bool hashed_names;
+ /*
+ * A sealed volume (the iOS 16 cryptexes) keeps its files' extents in a tree of their own,
+ * keyed by stream and offset, rather than among the file-system tree's records.
+ */
+ struct apfs_btree fext;
+ bool has_fext;
uint64_t num_files;
uint64_t num_dirs;
uint64_t num_symlinks;
@@ -319,6 +326,8 @@ struct apfs_cursor {
uint64_t node[APFS_MAX_LEVELS]; /* the block address at each level,
* never an object identifier */
uint32_t index[APFS_MAX_LEVELS];
+ uint64_t hash8[APFS_MAX_LEVELS]; /* a sealed tree's nodes: the first 8 bytes of the
+ * SHA-256 the parent keeps of each (0 for the root) */
bool done;
};
diff --git a/xnu/files/bsd/miscfs/apfs/apfs_zbitmap.c b/xnu/files/bsd/miscfs/apfs/apfs_zbitmap.c
new file mode 100644
index 0000000..1a8619f
--- /dev/null
+++ b/xnu/files/bsd/miscfs/apfs/apfs_zbitmap.c
@@ -0,0 +1,12 @@
+/*
+ * LZBITMAP, Apple's method for the files of the iOS 16 cryptexes (decmpfs types 13 and 14:
+ * the OS cryptex of the IPSW keeps its dyld shared cache this way), so that a cryptex image
+ * can be mounted as Apple ships it. The same file is built into the kernel and into
+ * tools/apfs/apfs-tool.
+ *
+ * The decoder is libzbitmap (https://github.com/eafer/libzbitmap) as shipped with the
+ * linux-apfs-rw driver, Copyright (C) 2022 Corellium LLC, Ernesto A. Fernandez, under
+ * "GPL-2.0+ OR MIT" -- taken here under the MIT licence; only its includes are changed
+ * (libzbitmap/).
+ */
+#include "libzbitmap/libzbitmap.c"
diff --git a/xnu/files/bsd/miscfs/apfs/libzbitmap/libzbitmap.c b/xnu/files/bsd/miscfs/apfs/libzbitmap/libzbitmap.c
new file mode 100644
index 0000000..f9c0815
--- /dev/null
+++ b/xnu/files/bsd/miscfs/apfs/libzbitmap/libzbitmap.c
@@ -0,0 +1,448 @@
+// SPDX-License-Identifier: GPL-2.0+ OR MIT (taken here under MIT)
+/*
+ * Copyright (C) 2022 Corellium LLC
+ *
+ * Author: Ernesto A. Fernández <[email protected]>
+ *
+ * Ported from libzbitmap (https://github.com/eafer/libzbitmap). Only the
+ * decompression code is included.
+ */
+
+/* (the port: errno and memcmp/memcpy from where the kernel and the host tool each have them) */
+#ifdef KERNEL
+#include <sys/errno.h>
+#include <string.h>
+#else
+#include <errno.h>
+#include <string.h>
+#endif
+#include "libzbitmap.h"
+
+#define ZBM_MAGIC "ZBM\x09"
+#define ZBM_MAGIC_SZ 4
+
+#define ZBM_MAX_DECMP_CHUNK_SIZE 0x8000
+#define ZBM_MAX_DECMP_CHUNK_SIZE_BITS 15
+
+struct uint24 {
+ uint8_t low;
+ uint8_t mid;
+ uint8_t hig;
+};
+
+/* This header is shared by both compressed and decompressed chunks */
+struct zbm_chunk_hdr {
+ struct uint24 len; /* Length of the chunk */
+ struct uint24 decmp_len; /* Length of the chunk after decompression */
+};
+
+/* The full header for compressed chunks */
+struct zbm_cmp_chunk_hdr {
+ /* Shared with decompressed chunks */
+ struct zbm_chunk_hdr hdr;
+
+ /* Offset for each of the three metadata areas */
+ struct uint24 meta_off_1;
+ struct uint24 meta_off_2;
+ struct uint24 meta_off_3;
+};
+
+/* Pointer to a half-byte */
+struct nybl_ptr {
+ uint8_t *addr; /* Address of the byte */
+ int nibble; /* Which of the two nibbles? */
+};
+
+/* 0-2 and 0xf are not real bitmap indexes */
+#define ZBM_BITMAP_COUNT (16 - 1 - 3)
+#define ZBM_BITMAP_BASE 3
+#define ZBM_BITMAP_BYTECNT 17
+#define ZBM_MAX_PERIOD_BYTECNT 2
+
+struct zbm_bmap {
+ uint8_t bitmap; /* The bitmap */
+ uint8_t period_bytecnt; /* Read this many bytes to get the new period */
+};
+
+struct zbm_state {
+ /* Updated during a chunk read */
+ uint8_t *dest; /* Write the next byte here */
+ size_t dest_left; /* Room left in destination buffer */
+ uint32_t written; /* Bytes written so far for current chunk */
+ uint16_t period; /* Repetition period for decompression, in bytes */
+
+ /* Updated right before a chunk read */
+ const uint8_t *src_end; /* End of current chunk */
+ uint32_t len; /* Length of the chunk */
+ uint32_t decmp_len; /* Expected chunk length after decompression */
+
+ /* Updated after a chunk read */
+ const uint8_t *src; /* Start of buffer, or current chunk if any */
+ size_t src_left; /* Room left in the source buffer */
+ size_t prewritten; /* Bytes written for previous chunks */
+
+ /* Current position in data and metadata areas for this chunk */
+ const uint8_t *data;
+ const uint8_t *meta_1;
+ const uint8_t *meta_2;
+ struct nybl_ptr meta_3;
+
+ /* Array of bitmaps for the current chunk */
+ struct zbm_bmap bitmaps[ZBM_BITMAP_COUNT];
+};
+
+static int zbm_check_magic(struct zbm_state *state)
+{
+ if(state->src_left < ZBM_MAGIC_SZ)
+ return -EINVAL;
+
+ if(memcmp(state->src, ZBM_MAGIC, ZBM_MAGIC_SZ))
+ return -EINVAL;
+
+ state->src += ZBM_MAGIC_SZ;
+ state->src_left -= ZBM_MAGIC_SZ;
+ return 0;
+}
+
+static uint32_t zbm_u24_to_u32(struct uint24 n)
+{
+ uint32_t res;
+
+ res = n.hig;
+ res <<= 8;
+ res += n.mid;
+ res <<= 8;
+ res += n.low;
+ return res;
+}
+
+/* Some chunks just have regular uncompressed data, but with a header */
+static int zbm_chunk_is_uncompressed(struct zbm_state *state)
+{
+ return state->len == state->decmp_len + sizeof(struct zbm_chunk_hdr);
+}
+
+static int zbm_handle_uncompressed_chunk(struct zbm_state *state)
+{
+ state->meta_1 = state->meta_2 = NULL;
+ state->meta_3.addr = NULL;
+ state->meta_3.nibble = 0;
+ state->data = state->src + sizeof(struct zbm_chunk_hdr);
+ memcpy(state->dest, state->data, state->decmp_len);
+
+ state->dest += state->decmp_len;
+ state->dest_left -= state->decmp_len;
+ state->written = state->decmp_len;
+ return 0;
+}
+
+static int zbm_read_nibble(struct nybl_ptr *nybl, const uint8_t *limit, uint8_t *result)
+{
+ if(nybl->addr >= limit)
+ return -EINVAL;
+
+ if(nybl->nibble == 0) {
+ *result = *nybl->addr & 0xf;
+ nybl->nibble = 1;
+ } else {
+ *result = (*nybl->addr >> 4) & 0xf;
+ nybl->nibble = 0;
+ ++nybl->addr;
+ }
+ return 0;
+}
+
+static void zbm_rewind_nibble(struct nybl_ptr *nybl)
+{
+ if(nybl->nibble == 0) {
+ nybl->nibble = 1;
+ --nybl->addr;
+ } else {
+ nybl->nibble = 0;
+ }
+}
+
+static int zbm_apply_bitmap(struct zbm_state *state, struct zbm_bmap *bitmap)
+{
+ int i;
+
+ /* The periods are stored in the first metadata area */
+ if(bitmap->period_bytecnt) {
+ state->period = 0;
+ for(i = 0; i < bitmap->period_bytecnt; ++i) {
+ if(state->meta_1 >= state->src_end)
+ return -EINVAL;
+ state->period |= *state->meta_1 << i * 8;
+ ++state->meta_1;
+ }
+ }
+ if(state->period == 0)
+ return -EINVAL;
+
+ for(i = 0; i < 8; ++i) {
+ if(state->written == state->decmp_len)
+ break;
+ if(bitmap->bitmap & 1 << i) {
+ if(state->data >= state->src_end)
+ return -EINVAL;
+ *state->dest = *state->data;
+ ++state->data;
+ } else {
+ if(state->prewritten + state->written < state->period)
+ return -EINVAL;
+ *state->dest = *(state->dest - state->period);
+ }
+ ++state->dest;
+ --state->dest_left;
+ ++state->written;
+ }
+
+ return 0;
+}
+
+static int zbm_apply_bitmap_number(struct zbm_state *state, uint8_t bmp_num)
+{
+ struct zbm_bmap next = {0};
+
+ /* Not a valid bitmap number (it signals a repetition) */
+ if(bmp_num == 0xf)
+ return -EINVAL;
+
+ /* An actual index in the bitmap array */
+ if(bmp_num > ZBM_MAX_PERIOD_BYTECNT)
+ return zbm_apply_bitmap(state, &state->bitmaps[bmp_num - ZBM_BITMAP_BASE]);
+
+ /* For < 2, use the next bitmap in the second metadata area */
+ if(state->meta_2 >= state->src_end)
+ return -EINVAL;
+ next.bitmap = *state->meta_2;
+ next.period_bytecnt = bmp_num;
+ ++state->meta_2;
+ return zbm_apply_bitmap(state, &next);
+}
+
+/* Find out how many times we need to repeat the current bitmap operation */
+static int zbm_read_repetition_count(struct zbm_state *state, uint16_t *repeat)
+{
+ uint8_t nibble;
+ uint16_t total;
+ int err;
+
+ /* Don't confuse the trailing bitmaps with a repetition count */
+ if(state->decmp_len - state->written <= 8) {
+ *repeat = 1;
+ return 0;
+ }
+
+ err = zbm_read_nibble(&state->meta_3, state->src_end, &nibble);
+ if(err)
+ return err;
+
+ if(nibble != 0xf) {
+ /* No repetition count: the previous bitmap number gets applied once */
+ zbm_rewind_nibble(&state->meta_3);
+ *repeat = 1;
+ return 0;
+ }
+
+ /*
+ * Under this scheme, repeating a bitmap number 3 times wouldn't save any
+ * space, so the repetition count starts from 4.
+ */
+ total = 4;
+ while(nibble == 0xf) {
+ err = zbm_read_nibble(&state->meta_3, state->src_end, &nibble);
+ if(err)
+ return err;
+ total += nibble;
+ if(total < nibble)
+ return -EINVAL;
+ }
+
+ *repeat = total;
+ return 0;
+}
+
+static int zbm_decompress_single_bitmap(struct zbm_state *state)
+{
+ uint8_t bmp_num;
+ uint16_t repeat;
+ int i;
+ int err;
+
+ /* The current nibble is the offset of the next bitmap to apply */
+ err = zbm_read_nibble(&state->meta_3, state->src_end, &bmp_num);
+ if(err)
+ return err;
+
+ err = zbm_read_repetition_count(state, &repeat);
+ if(err)
+ return err;
+
+ for(i = 0; i < repeat; ++i) {
+ err = zbm_apply_bitmap_number(state, bmp_num);
+ if(err)
+ return err;
+ }
+ return 0;
+}
+
+/* Pointer to a bit */
+struct bit_ptr {
+ uint8_t *addr; /* Address of the byte */
+ int offset; /* Bit number */
+};
+
+/* This function does not perform boundary checks, the caller must do it */
+static int zbm_read_single_bit(struct bit_ptr *bit)
+{
+ int res = *bit->addr >> bit->offset & 1;
+
+ ++bit->offset;
+ if(bit->offset != 8)
+ return res;
+ bit->offset = 0;
+ ++bit->addr;
+ return res;
+}
+
+static int zbm_read_single_bitmap(struct bit_ptr *bit, const uint8_t *limit, struct zbm_bmap *result)
+{
+ int i;
+
+ result->bitmap = 0;
+ result->period_bytecnt = 0;
+
+ /* The bitmap itself */
+ for(i = 0; i < 8; ++i) {
+ if(bit->addr >= limit)
+ return -EINVAL;
+ result->bitmap |= zbm_read_single_bit(bit) << i;
+ }
+
+ /*
+ * The two trailing bits tell us how many bytes to read for the next
+ * repetition period
+ */
+ for(i = 0; i < 2; ++i) {
+ if(bit->addr >= limit)
+ return -EINVAL;
+ result->period_bytecnt |= zbm_read_single_bit(bit) << i;
+ }
+
+ return 0;
+}
+
+static int zbm_read_bitmaps(struct zbm_state *state)
+{
+ struct bit_ptr bmap = {0};
+ int err, i;
+
+ if(state->len < ZBM_BITMAP_BYTECNT)
+ return -EINVAL;
+
+ bmap.addr = (uint8_t *)state->src_end - ZBM_BITMAP_BYTECNT;
+ bmap.offset = 0;
+
+ for(i = 0; i < ZBM_BITMAP_COUNT; ++i) {
+ err = zbm_read_single_bitmap(&bmap, state->src_end, &state->bitmaps[i]);
+ if(err)
+ return err;
+ if(state->bitmaps[i].period_bytecnt > ZBM_MAX_PERIOD_BYTECNT)
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static int zbm_handle_compressed_chunk(struct zbm_state *state)
+{
+ const struct zbm_cmp_chunk_hdr *hdr = NULL;
+ uint32_t meta_off_1, meta_off_2, meta_off_3;
+ int err;
+
+ state->written = 0;
+ state->period = 8;
+
+ if(state->len < sizeof(*hdr))
+ return -EINVAL;
+ hdr = (struct zbm_cmp_chunk_hdr *)state->src;
+ state->data = state->src + sizeof(*hdr);
+
+ meta_off_1 = zbm_u24_to_u32(hdr->meta_off_1);
+ meta_off_2 = zbm_u24_to_u32(hdr->meta_off_2);
+ meta_off_3 = zbm_u24_to_u32(hdr->meta_off_3);
+ if(meta_off_1 >= state->len || meta_off_2 >= state->len || meta_off_3 >= state->len)
+ return -EINVAL;
+ state->meta_1 = state->src + meta_off_1;
+ state->meta_2 = state->src + meta_off_2;
+ state->meta_3.addr = (uint8_t *)state->src + meta_off_3;
+ state->meta_3.nibble = 0;
+
+ err = zbm_read_bitmaps(state);
+ if(err)
+ return err;
+
+ while(state->written < state->decmp_len) {
+ err = zbm_decompress_single_bitmap(state);
+ if(err)
+ return err;
+ }
+
+ return 0;
+}
+
+static int zbm_handle_chunk(struct zbm_state *state)
+{
+ const struct zbm_chunk_hdr *decmp_hdr = NULL;
+
+ if(state->src_left < sizeof(*decmp_hdr))
+ return -EINVAL;
+ decmp_hdr = (struct zbm_chunk_hdr *)state->src;
+
+ state->len = zbm_u24_to_u32(decmp_hdr->len);
+ if(state->len > state->src_left)
+ return -EINVAL;
+ state->src_end = state->src + state->len;
+
+ state->decmp_len = zbm_u24_to_u32(decmp_hdr->decmp_len);
+ if(state->decmp_len > ZBM_MAX_DECMP_CHUNK_SIZE)
+ return -EINVAL;
+ if(!state->dest) /* We just wanted the length, so we are done */
+ return 0;
+ if(state->decmp_len > state->dest_left)
+ return -ERANGE;
+
+ if(zbm_chunk_is_uncompressed(state))
+ return zbm_handle_uncompressed_chunk(state);
+
+ return zbm_handle_compressed_chunk(state);
+}
+
+int zbm_decompress(void *dest, size_t dest_size, const void *src, size_t src_size, size_t *out_len)
+{
+ struct zbm_state state = {0};
+ int err;
+
+ state.src = src;
+ state.src_left = src_size;
+ state.dest = dest;
+ state.dest_left = dest_size;
+ state.prewritten = 0;
+
+ err = zbm_check_magic(&state);
+ if(err)
+ return err;
+
+ /* The final chunk has zero decompressed length */
+ do {
+ err = zbm_handle_chunk(&state);
+ if(err)
+ return err;
+ state.src += state.len;
+ state.src_left -= state.len;
+ state.prewritten += state.decmp_len;
+ } while(state.decmp_len != 0);
+
+ *out_len = state.prewritten;
+ return 0;
+}
diff --git a/xnu/files/bsd/miscfs/apfs/libzbitmap/libzbitmap.h b/xnu/files/bsd/miscfs/apfs/libzbitmap/libzbitmap.h
new file mode 100644
index 0000000..89bed59
--- /dev/null
+++ b/xnu/files/bsd/miscfs/apfs/libzbitmap/libzbitmap.h
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR MIT (taken here under MIT) */
+/*
+ * Copyright (c) 2022 Corellium LLC
+ *
+ * Author: Ernesto A. Fernández <[email protected]>
+ *
+ * Ported from libzbitmap (https://github.com/eafer/libzbitmap). Only the
+ * decompression code is included.
+ */
+
+#ifndef _LIBZBITMAP_H
+#define _LIBZBITMAP_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+/**
+ * zbm_decompress - Decompress an LZBITMAP buffer
+ * @dest: destination buffer (may be NULL)
+ * @dest_size: size of the destination buffer
+ * @src: source buffer
+ * @src_size: size of the source buffer
+ * @out_len: on return, the length of the decompressed output
+ *
+ * May be called with a NULL destination buffer to retrieve the expected length
+ * of the decompressed data. Returns 0 on success, or a negative error code in
+ * case of failure.
+ */
+int zbm_decompress(void *dest, size_t dest_size, const void *src, size_t src_size, size_t *out_len);
+
+#endif /* _LIBZBITMAP_H */
diff --git a/xnu/redwood.patch b/xnu/redwood.patch
index d4ad229..c5a993a 100644
--- a/xnu/redwood.patch
+++ b/xnu/redwood.patch
@@ -62,7 +62,7 @@ index a4d25040f38f..aad72993d139 100644
kern_debug.o_INCFLAGS += -I$(SRCROOT)/osfmk
diff --git a/bsd/conf/files b/bsd/conf/files
-index 44274016a4cf..14810c85d6dd 100644
+index 44274016a4cf..47198fd9ec89 100644
--- a/bsd/conf/files
+++ b/bsd/conf/files
@@ -109,6 +109,36 @@ bsd/dev/munge.c optional config_requires_u32_munging
@@ -102,11 +102,12 @@ index 44274016a4cf..14810c85d6dd 100644
bsd/dev/dev_perfmon.c optional config_perfmon
bsd/libkern/crc32.c standard
-@@ -155,6 +185,14 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs
+@@ -155,6 +185,15 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs
bsd/miscfs/mockfs/mockfs_fsnode.c optional mockfs
bsd/miscfs/mockfs/mockfs_vfsops.c optional mockfs
bsd/miscfs/mockfs/mockfs_vnops.c optional mockfs
+bsd/miscfs/apfs/apfs_deflate.c optional apfs
++bsd/miscfs/apfs/apfs_zbitmap.c optional apfs
+bsd/miscfs/apfs/apfs_lib.c optional apfs
+bsd/miscfs/apfs/apfs_vfsops.c optional apfs
+bsd/miscfs/apfs/apfs_vnops.c optional apfs
@@ -117,7 +118,7 @@ index 44274016a4cf..14810c85d6dd 100644
bsd/miscfs/routefs/routefs_ops.c optional routefs
-@@ -361,6 +399,7 @@ bsd/security/audit/audit_syscalls.c standard
+@@ -361,6 +400,7 @@ bsd/security/audit/audit_syscalls.c standard
bsd/security/audit/audit_worker.c optional config_audit
bsd/pthread/pthread_shims.c standard
@@ -125,7 +126,7 @@ index 44274016a4cf..14810c85d6dd 100644
bsd/pthread/pthread_priority.c standard
bsd/pthread/pthread_workqueue.c standard
-@@ -383,6 +422,7 @@ bsd/kern/kern_debug.c standard
+@@ -383,6 +423,7 @@ bsd/kern/kern_debug.c standard
bsd/kern/kern_symfile.c standard
bsd/kern/kern_descrip.c standard
bsd/kern/kern_guarded.c standard