e1000 -- Linux's, unmodified -- probes, opens, gets an address by DHCP and
carries ping traffic in a SchOS process. The kernel was driving that NIC and
gave it up, which makes it the first real handover as well as the second
driver.
None of what it needed was a missing feature. Every one was a call that
succeeded, returned zero and did nothing:
_find_next_bit "bit 0 is set" -- for_each_cpu never ended
netif_carrier_off carrier already up -- TCTL.EN never set
__x86_indirect_thunk_r8 the jump discarded -- 255 rx buffers, none made
dma_map_page_attrs bus address 0 -- NIC aimed at physical 0
so the rule from memcpy is now general: a stub returning zero is unsafe
wherever zero is a meaningful answer.
The loader grew ko_fixup_percpu(). The stack guard turned out to be one
per-CPU variable among several; `current` cannot be defused because the driver
dereferences it. Taking the %gs off instead leaves the same opcode reading the
same width from storage the host supplies -- a per-CPU variable in a
single-threaded host is just a variable. Eight bytes become seven and a nop,
and the displacement is not always last, so there is now just enough of an x86
decoder in ko.c to know how long the instruction is.
ldls/kpi/thunk.c is fifteen lines of assembly and the highest-value file here:
retpolines are control flow, and a stub cannot stand in for a jump.
The kernel side:
- ioperm.c, the TSS I/O permission bitmap. A driver's outl is an instruction,
not a call, and e1000's reset path picks it deliberately. IOPL=3 would hand
over the PIC and the disk with it, so it is per-port. Writing it found that
the I/O map base had been written at offset 64 rather than 102, leaving the
CPU checking ports 0-831 against the TSS's own RSP0.
- claim masks the device's IRQ line: 450,000 a second, otherwise.
- net_detach(), the missing half of net_attach_late(). An interface left in
the stack keeps its route and sends every frame to a driver that refuses
it, which looks exactly like a userspace driver that does not work.
- e1000_release() gives up the interface, not just the hardware.
8139too still passes 4/4; ldls-check still passes 27; four CPUs, no panic.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
145 lines
4 KiB
C
145 lines
4 KiB
C
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
// The string and memory primitives. See ldls/kpi/kpi.h for the provenance rule.
|
|
//
|
|
// These are the least interesting functions in the layer and the most
|
|
// dangerous to leave out, because a stub for one of them does not crash --
|
|
// it silently does nothing, and the caller carries on with a buffer full of
|
|
// whatever was there before.
|
|
//
|
|
// That is not hypothetical. `memcpy` was a logging stub while the receive path
|
|
// was being brought up: the driver copied each frame into its skb, the copy
|
|
// did not happen, and the stack was handed the right *length* of zeroes. Every
|
|
// counter said packets were arriving and DHCP never completed. A crash would
|
|
// have been easier to find.
|
|
|
|
#include "types.h"
|
|
#include "kpi.h"
|
|
|
|
void *
|
|
kpi_memcpy(void *d, const void *s, unsigned long n)
|
|
{
|
|
unsigned char *a = (unsigned char *)d;
|
|
const unsigned char *b = (const unsigned char *)s;
|
|
while (n--) *a++ = *b++;
|
|
return d;
|
|
}
|
|
|
|
void *
|
|
kpi_memmove(void *d, const void *s, unsigned long n)
|
|
{
|
|
unsigned char *a = (unsigned char *)d;
|
|
const unsigned char *b = (const unsigned char *)s;
|
|
|
|
if (a == b || n == 0)
|
|
return d;
|
|
if (a < b) {
|
|
while (n--) *a++ = *b++;
|
|
} else {
|
|
a += n; b += n;
|
|
while (n--) *--a = *--b;
|
|
}
|
|
return d;
|
|
}
|
|
|
|
void *
|
|
kpi_memset(void *d, int c, unsigned long n)
|
|
{
|
|
unsigned char *a = (unsigned char *)d;
|
|
while (n--) *a++ = (unsigned char)c;
|
|
return d;
|
|
}
|
|
|
|
int
|
|
kpi_memcmp(const void *x, const void *y, unsigned long n)
|
|
{
|
|
const unsigned char *a = x, *b = y;
|
|
while (n--) { if (*a != *b) return (int)*a - (int)*b; a++; b++; }
|
|
return 0;
|
|
}
|
|
|
|
// A null string is survivable, and reported.
|
|
//
|
|
// A hosted driver reads names out of structures this host only partly fills
|
|
// in, and the first one it found was `pci_name(pdev)` -- a pointer into a
|
|
// `struct device` that had never been given one. Faulting inside strnlen tells
|
|
// you nothing about which field was missing; answering zero and saying so
|
|
// leaves the driver running and names the problem.
|
|
static long kpi_nullstr;
|
|
|
|
static int
|
|
str_ok(const char *s, const char *who)
|
|
{
|
|
if (s)
|
|
return 1;
|
|
if (++kpi_nullstr <= 4) {
|
|
kpi_log(" kpi: "); kpi_log(who);
|
|
kpi_log(" on a null string -- some field of a struct this host fills in "
|
|
"only partly\n");
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
unsigned long
|
|
kpi_strlen(const char *s)
|
|
{
|
|
unsigned long n = 0;
|
|
if (!str_ok(s, "strlen")) return 0;
|
|
while (s[n]) n++;
|
|
return n;
|
|
}
|
|
|
|
unsigned long
|
|
kpi_strnlen(const char *s, unsigned long max)
|
|
{
|
|
unsigned long n = 0;
|
|
if (!str_ok(s, "strnlen")) return 0;
|
|
while (n < max && s[n]) n++;
|
|
return n;
|
|
}
|
|
|
|
// sized_strscpy(dest, src, count) -- a bounded copy that always terminates and
|
|
// reports the length copied, or -E2BIG when it did not fit.
|
|
long
|
|
kpi_sized_strscpy(char *d, const char *s, unsigned long n)
|
|
{
|
|
unsigned long i = 0;
|
|
|
|
if (n == 0)
|
|
return -7; /* -E2BIG */
|
|
if (!str_ok(s, "strscpy")) { d[0] = 0; return 0; }
|
|
while (i + 1 < n && s[i]) { d[i] = s[i]; i++; }
|
|
d[i] = 0;
|
|
return s[i] ? -7 : (long)i;
|
|
}
|
|
|
|
// The io variants. On this platform a BAR is ordinary memory once mapped, so
|
|
// these differ from their plain counterparts only in that the compiler must
|
|
// not reorder or elide them -- which is what the volatile is for.
|
|
void
|
|
kpi_memcpy_fromio(void *d, const void *src, unsigned long n)
|
|
{
|
|
unsigned char *a = (unsigned char *)d;
|
|
const volatile unsigned char *b = (const volatile unsigned char *)src;
|
|
while (n--) *a++ = *b++;
|
|
}
|
|
|
|
void
|
|
kpi_memcpy_toio(void *dst, const void *s, unsigned long n)
|
|
{
|
|
volatile unsigned char *a = (volatile unsigned char *)dst;
|
|
const unsigned char *b = (const unsigned char *)s;
|
|
while (n--) *a++ = *b++;
|
|
}
|
|
|
|
// __fortify_panic is what a driver's bounds-checked string helpers call when
|
|
// they detect an overflow. Reaching it means the driver found a bug in itself,
|
|
// and the honest thing is to say so rather than return.
|
|
void
|
|
kpi_fortify_panic(void)
|
|
{
|
|
kpi_log("kpi: the driver called __fortify_panic -- it detected an "
|
|
"overflow in itself\n");
|
|
for (;;)
|
|
;
|
|
}
|