Skip to content

XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)

C

memdev: the disk size in bytes is 64-bit (mdSize << 12 overflowed 32 bits: a 16G host disk read as empty, a 6G one as 2G)

Heyang Zhou17c24a779f34
diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patch
index a7a8d0f..85c4ccf 100644
--- a/xnu-patches/0001-linux64-stage-hook.patch
+++ b/xnu-patches/0001-linux64-stage-hook.patch
@@ -1,5 +1,5 @@
diff --git a/bsd/dev/memdev.c b/bsd/dev/memdev.c
-index d24535d..0b13601 100644
+index d24535d..f8cd02d 100644
--- a/bsd/dev/memdev.c
+++ b/bsd/dev/memdev.c
@@ -95,6 +95,7 @@
@@ -91,6 +91,40 @@ index d24535d..0b13601 100644
mdata = ((addr64_t)mdev[devid].mdBase << 12) + uio->uio_offset; /* Point to the area in "file" */
saveflag = uio->uio_segflg; /* Remember what the request is */
+@@ -242,10 +303,10 @@ mdevrw(dev_t dev, struct uio *uio, __unused int ioflag)
+ }
+ }
+
+- if (uio->uio_offset > (mdev[devid].mdSize << 12)) {
++ if (uio->uio_offset > ((uint64_t)mdev[devid].mdSize << 12)) {
+ count = 0;
+ } else {
+- count = imin(uio_resid(uio), (mdev[devid].mdSize << 12) - uio->uio_offset);
++ count = imin(uio_resid(uio), ((uint64_t)mdev[devid].mdSize << 12) - uio->uio_offset);
+ }
+
+ status = uiomove64(mdata, count, uio); /* Move the data */
+@@ -280,16 +341,16 @@ mdevstrategy(struct buf *bp)
+ * we just return with resid == count.
+ */
+
+- if (blkoff >= (mdev[devid].mdSize << 12)) { /* Are they trying to read/write at/after end? */
+- if (blkoff != (mdev[devid].mdSize << 12)) { /* Are we trying to read after EOF? */
++ if (blkoff >= ((uint64_t)mdev[devid].mdSize << 12)) { /* Are they trying to read/write at/after end? */
++ if (blkoff != ((uint64_t)mdev[devid].mdSize << 12)) { /* Are we trying to read after EOF? */
+ buf_seterror(bp, EINVAL); /* Yeah, this is an error */
+ }
+ buf_biodone(bp); /* Return */
+ return;
+ }
+
+- if ((blkoff + buf_count(bp)) > (mdev[devid].mdSize << 12)) { /* Will this read go past end? */
+- buf_setcount(bp, (uint32_t)((mdev[devid].mdSize << 12) - blkoff)); /* Yes, trim to max */
++ if ((blkoff + buf_count(bp)) > ((uint64_t)mdev[devid].mdSize << 12)) { /* Will this read go past end? */
++ buf_setcount(bp, (uint32_t)(((uint64_t)mdev[devid].mdSize << 12) - blkoff)); /* Yes, trim to max */
+ }
+ /*
+ * make sure the buffer's data area is
@@ -299,6 +360,18 @@ mdevstrategy(struct buf *bp)
panic("ramstrategy: buf_map failed");
}
@@ -110,6 +144,15 @@ index d24535d..0b13601 100644
fvaddr = (mdev[devid].mdBase << 12) + blkoff; /* Point to offset into ram disk */
if (buf_flags(bp) & B_READ) { /* Is this a read? */
+@@ -434,7 +507,7 @@ mdevioctl(dev_t dev, u_long cmd, caddr_t data, __unused int flag,
+ if (!(mdev[devid].mdFlags & mdInited)) {
+ return ENXIO;
+ }
+- *o = ((mdev[devid].mdSize << 12) + mdev[devid].mdSecsize - 1) / mdev[devid].mdSecsize;
++ *o = (((uint64_t)mdev[devid].mdSize << 12) + mdev[devid].mdSecsize - 1) / mdev[devid].mdSecsize;
+ break;
+
+ /*
@@ -708,6 +781,37 @@ mdevadd(int devid, uint64_t base, unsigned int size, int phys)
}