aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / x86 / swap_disk_read.h
| 1 | /* swap_disk_read.h -- bounds-checked read of the QEMU test-disk slot object. |
| 2 | * |
| 3 | * Header only so a hosted test can drive it with a mocked sector reader. The includer |
| 4 | * supplies memcmp(), memcpy(), ck_puts() and ck_printf(); the kernel gets them from |
| 5 | * ck_internal.h. */ |
| 6 | #ifndef AIENOS_CK_SWAP_DISK_READ_H |
| 7 | #define AIENOS_CK_SWAP_DISK_READ_H |
| 8 | #include <stdint.h> |
| 9 | #include <stddef.h> |
| 10 | #include "../../boot/slot_format.h" |
| 11 | |
| 12 | #define CK_SWAP_SECTOR 512u |
| 13 | |
| 14 | /* Reads `count` sectors from `lba` into `buf`; 0 on success. */ |
| 15 | typedef int (*ck_swap_sector_reader)(uint64_t lba, unsigned count, uint8_t *buf); |
| 16 | |
| 17 | /* Sectors needed for the whole slot object (manifest unit + image) when the image is |
| 18 | * img_len bytes and the buffer holds cap bytes. Sets *total to the object length and |
| 19 | * *nsectors to the sector count. Returns 0 only if the sector-rounded read fits in cap: |
| 20 | * the disk reads whole sectors, so the rounded length is what lands in the buffer. |
| 21 | * Nothing here can overflow: img_len is compared against cap - CK_SLOT_UNIT before it |
| 22 | * is added to anything. */ |
| 23 | static inline int ck_swap_disk_extent(size_t cap, uint64_t img_len, size_t *total, |
| 24 | unsigned *nsectors) |
| 25 | { |
| 26 | if (cap < CK_SLOT_UNIT) |
| 27 | return -1; |
| 28 | if (img_len > (uint64_t)(cap - CK_SLOT_UNIT)) |
| 29 | return -1; |
| 30 | size_t t = (size_t)CK_SLOT_UNIT + (size_t)img_len; |
| 31 | size_t sectors = t / CK_SWAP_SECTOR + ((t % CK_SWAP_SECTOR) != 0); |
| 32 | if (sectors == 0 || sectors > (size_t)~0u) |
| 33 | return -1; |
| 34 | if (sectors > cap / CK_SWAP_SECTOR) /* sectors * 512 > cap, without the multiply */ |
| 35 | return -1; |
| 36 | *total = t; |
| 37 | *nsectors = (unsigned)sectors; |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | /* Reads the raw slot object at lba. Returns 0 and the object length on success; no |
| 42 | * sector is read into buf past cap bytes. The disk content is untrusted. */ |
| 43 | static inline int ck_swap_disk_load(ck_swap_sector_reader rd, uint64_t lba, uint8_t *buf, |
| 44 | size_t cap, size_t *len_out) |
| 45 | { |
| 46 | static uint8_t header[CK_SWAP_SECTOR]; |
| 47 | *len_out = 0; |
| 48 | if (rd(lba, 1, header) != 0) { |
| 49 | ck_puts("swap_inbox: test disk not readable\n"); |
| 50 | return -1; |
| 51 | } |
| 52 | if (memcmp(header, CK_SLOT_MANIFEST_MAGIC, 8) != 0) { |
| 53 | ck_puts("swap_inbox: no slot object at the test LBA\n"); |
| 54 | return -1; |
| 55 | } |
| 56 | uint64_t img_len64; |
| 57 | memcpy(&img_len64, header + 48, 8); /* manifest.image_len (u64 at offset 48) */ |
| 58 | size_t total; |
| 59 | unsigned nsectors; |
| 60 | if (ck_swap_disk_extent(cap, img_len64, &total, &nsectors) != 0) { |
| 61 | ck_puts("swap_inbox: slot too large\n"); |
| 62 | return -1; |
| 63 | } |
| 64 | if (rd(lba, nsectors, buf) != 0) { |
| 65 | ck_puts("swap_inbox: read failed\n"); |
| 66 | return -1; |
| 67 | } |
| 68 | *len_out = total; |
| 69 | ck_printf("swap: inbox at LBA %u img_len=%llu total=%zu\n", (unsigned)lba, |
| 70 | (unsigned long long)img_len64, total); |
| 71 | return 0; |
| 72 | } |
| 73 | #endif |