From: Aymen T <atijari00@gmail.com>
To: mkp@kernel.org, James.Bottomley@HansenPartnership.com
Cc: linux-scsi@vger.kernel.org
Subject: scsi: scsi_report_lun_scan() retry length wraps to 0..7 for a list length near 2^32
Date: Sat, 21 Nov 2026 19:17:05 +0100 [thread overview]
Message-ID: <6785b8f2-e089-4e02-848d-6c3327f67750@gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 1659 bytes --]
Hi Martin, James,
When the REPORT LUNS reply says the list is longer than the buffer,
scsi_report_lun_scan() retries with a bigger one:
if (get_unaligned_be32(lun_data->scsi_lun) +
sizeof(struct scsi_lun) > length) {
length = get_unaligned_be32(lun_data->scsi_lun) +
sizeof(struct scsi_lun);
kfree(lun_data);
goto retry;
}
The comparison is done in 64 bits but length is an unsigned int, so a
list length of 0xfffffff8 gives length = 0. kmalloc(0) returns
ZERO_SIZE_PTR, the command is sent with no buffer, and the next
get_unaligned_be32(lun_data->scsi_lun) reads address 0x10. List
lengths 0xfffffff9..0xffffffff give length 1..7; in my runs the scan
retried about 34 times and then gave up.
On mainline (65e4fd942f72, 7.4.0-rc3) with KASAN, in QEMU, with a
dummy_hcd/raw_gadget UAS device whose first REPORT LUNS reply starts
with ff ff ff f8:
Oops: general protection fault, probably for non-canonical address
0xdffffc0000000002: 0000 [#1] SMP KASAN
KASAN: null-ptr-deref in range [0x0000000000000010-0x0000000000000017]
Workqueue: async async_run_entry_fn
RIP: 0010:__scsi_scan_target+0x322/0x720
scsi_scan_host_selected+0x2c0/0x530
do_scan_async+0x1db/0x5d0
async_run_entry_fn+0x74/0x330
Fix: compute the new length in a u64 and refuse it if it is above a
sane cap (or doesn't fit in unsigned int), and check that the buffer
holds at least the 8-byte header before reading it.
Attached: rg.c, the raw_gadget device (gcc -O1 -static -pthread -o rg
rg.c; ./rg uas-rl-f8.cfg with dummy_hcd and raw_gadget loaded), and the
scenario file.
Thanks,
Aymen
[-- Attachment #2: rg.c --]
[-- Type: text/plain, Size: 24822 bytes --]
// rg.c - raw_gadget responder used as a configurable test device.
// It presents a USB mass-storage device (BOT or high-speed UAS) to the dummy_hcd host in the same
// (QEMU) guest kernel, answering with the exact bytes named in a scenario file, so that host-side
// USB/SCSI parsing can be run on deliberate boundary values.
// No randomisation or mutation: every byte comes from the scenario file or the fixed defaults below.
//
// usage: rg <scenario-file> (key=value lines; '#' comments; hex strings are plain hex digits)
// build: gcc -O1 -static -pthread -o rg rg.c
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <linux/usb/ch9.h>
#include <linux/usb/raw_gadget.h>
/* ---------------- scenario file ---------------- */
#define MAXKV 256
static char *kv_k[MAXKV], *kv_v[MAXKV];
static int nkv;
static void load_scn(const char *path)
{
FILE *f = fopen(path, "r");
char *line = NULL;
size_t cap = 0;
ssize_t n;
if (!f) { perror(path); exit(2); }
while ((n = getline(&line, &cap, f)) > 0) {
char *eq;
while (n > 0 && (line[n-1] == '\n' || line[n-1] == '\r')) line[--n] = 0;
if (!n || line[0] == '#') continue;
eq = strchr(line, '=');
if (!eq) continue;
*eq = 0;
if (nkv < MAXKV) { kv_k[nkv] = strdup(line); kv_v[nkv] = strdup(eq + 1); nkv++; }
}
fclose(f);
}
static const char *get(const char *k)
{
for (int i = 0; i < nkv; i++) if (!strcmp(kv_k[i], k)) return kv_v[i];
return NULL;
}
static const char *geti(const char *k, int eng)
{
char nm[64];
const char *v;
if (eng > 0) { snprintf(nm, sizeof nm, "e%d.%s", eng, k); v = get(nm); if (v) return v; }
return get(k);
}
static long num(const char *k, long def)
{
const char *v = get(k);
return v ? strtol(v, NULL, 0) : def;
}
static long numi(const char *k, int eng, long def)
{
const char *v = geti(k, eng);
return v ? strtol(v, NULL, 0) : def;
}
static int hexval(int c)
{
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
/* hex string -> bytes. '*N' after a byte repeats it N times ("ab*3" -> ab ab ab), spaces/colons ignored */
static int unhex(const char *s, uint8_t *out, int max)
{
int n = 0;
while (*s) {
int h, l;
if (*s == ' ' || *s == ':' || *s == '_') { s++; continue; }
h = hexval(s[0]);
l = s[1] ? hexval(s[1]) : -1;
if (h < 0 || l < 0) { fprintf(stderr, "bad hex at '%s'\n", s); exit(2); }
s += 2;
if (*s == '*') {
int cnt = strtol(s + 1, (char **)&s, 10);
for (int i = 0; i < cnt && n < max; i++) out[n++] = (h << 4) | l;
} else if (n < max) out[n++] = (h << 4) | l;
}
return n;
}
static int ascii_pad(const char *s, uint8_t *out, int width)
{
int l = s ? strlen(s) : 0;
memset(out, ' ', width);
memcpy(out, s, l > width ? width : l);
return width;
}
/* ---------------- device description ---------------- */
static int fd;
static int speed_cfg = USB_SPEED_HIGH;
static uint8_t devdesc[18];
static uint8_t *cfgA[8], *cfgB[8];
static int cfgAlen[8], cfgBlen[8], ncfg;
static uint8_t *bosblob; static int boslen;
static uint8_t *strdesc[256]; static int strdesclen[256];
static int curcfg;
static void put_u16(uint8_t *p, int v) { p[0] = v & 0xff; p[1] = v >> 8; }
static void make_string(int idx, const char *ascii)
{
int l = strlen(ascii);
uint8_t *b = calloc(1, 2 + 2 * l);
b[0] = 2 + 2 * l; b[1] = 3;
for (int i = 0; i < l; i++) b[2 + 2 * i] = ascii[i];
strdesc[idx] = b; strdesclen[idx] = 2 + 2 * l;
}
static int nbot = 1, use_uas;
static const int bot_in[2] = { 0x81, 0x86 }, bot_out[2] = { 0x02, 0x07 };
static const int uas_cmd_out = 0x01, uas_status_in = 0x82;
static const int uas_data_in = 0x83, uas_data_out = 0x04;
static uint8_t *default_cfg(int *len)
{
int ni = nbot, tot = 9 + ni * (9 + 14);
uint8_t *c = calloc(1, tot), *p;
int maxp = speed_cfg == USB_SPEED_FULL ? 64 : 512;
c[0] = 9; c[1] = 2; put_u16(c + 2, tot); c[4] = ni; c[5] = 1; c[6] = 0; c[7] = 0x80; c[8] = 50;
p = c + 9;
for (int i = 0; i < ni; i++) {
p[0] = 9; p[1] = 4; p[2] = i; p[3] = 0; p[4] = 2;
p[5] = numi("ifc", i, 8); p[6] = numi("ifs", i, 6); p[7] = numi("ifp", i, 0x50); p[8] = 0;
p += 9;
p[0] = 7; p[1] = 5; p[2] = bot_in[i]; p[3] = 2; put_u16(p + 4, maxp); p[6] = 0; p += 7;
p[0] = 7; p[1] = 5; p[2] = bot_out[i]; p[3] = 2; put_u16(p + 4, maxp); p[6] = 0; p += 7;
}
*len = tot;
return c;
}
static uint8_t *uas_cfg(int *len)
{
static const uint8_t ep_addr[4] = { 0x01, 0x82, 0x83, 0x04 };
int tot = 9 + 9 + 4 * (7 + 4);
int maxp = speed_cfg == USB_SPEED_FULL ? 64 : 512;
uint8_t *c = calloc(1, tot), *p = c;
p[0] = 9; p[1] = 2; put_u16(p + 2, tot); p[4] = 1;
p[5] = 1; p[7] = 0x80; p[8] = 50; p += 9;
p[0] = 9; p[1] = 4; p[2] = 0; p[3] = 0; p[4] = 4;
p[5] = 8; p[6] = 6; p[7] = 0x62; p += 9;
for (int i = 0; i < 4; i++) {
p[0] = 7; p[1] = 5; p[2] = ep_addr[i]; p[3] = 2;
put_u16(p + 4, maxp); p += 7;
p[0] = 4; p[1] = 0x24; p[2] = i + 1; p += 4;
}
*len = tot;
return c;
}
static void build_device(void)
{
const char *v;
char nm[32];
nbot = num("nbot", 1);
use_uas = num("uas", 0);
if (use_uas)
nbot = 0;
v = get("speed");
if (v && !strcmp(v, "full")) speed_cfg = USB_SPEED_FULL;
if (v && !strcmp(v, "super")) speed_cfg = USB_SPEED_SUPER;
if ((v = get("rawdev"))) { memset(devdesc, 0, 18); unhex(v, devdesc, 18); }
else {
devdesc[0] = 18; devdesc[1] = 1;
put_u16(devdesc + 2, num("bcdusb", 0x0200));
devdesc[4] = num("dclass", 0); devdesc[5] = num("dsub", 0); devdesc[6] = num("dproto", 0);
devdesc[7] = num("ep0", speed_cfg == USB_SPEED_SUPER ? 9 : 64);
put_u16(devdesc + 8, num("vid", 0x1234)); put_u16(devdesc + 10, num("pid", 0x5678));
put_u16(devdesc + 12, num("bcd", 0x0100));
devdesc[14] = get("mfg") ? 1 : 0; devdesc[15] = get("prod") ? 2 : 0; devdesc[16] = get("ser") ? 3 : 0;
devdesc[17] = num("ncfg", 1);
}
ncfg = devdesc[17] > 8 ? 8 : devdesc[17];
if (get("ncfg_serve")) ncfg = num("ncfg_serve", 1);
for (int i = 0; i < ncfg; i++) {
uint8_t buf[70000];
snprintf(nm, sizeof nm, "rawcfg%d", i);
if ((v = get(nm))) {
cfgAlen[i] = unhex(v, buf, sizeof buf);
cfgA[i] = malloc(cfgAlen[i]); memcpy(cfgA[i], buf, cfgAlen[i]);
} else if (i == 0) {
cfgA[0] = use_uas ? uas_cfg(&cfgAlen[0]) : default_cfg(&cfgAlen[0]);
} else {
cfgA[i] = use_uas ? uas_cfg(&cfgAlen[i]) : default_cfg(&cfgAlen[i]);
cfgA[i][5] = i + 1;
}
snprintf(nm, sizeof nm, "rawcfg%db", i);
if ((v = get(nm))) {
cfgBlen[i] = unhex(v, buf, sizeof buf);
cfgB[i] = malloc(cfgBlen[i]); memcpy(cfgB[i], buf, cfgBlen[i]);
} else { cfgB[i] = cfgA[i]; cfgBlen[i] = cfgAlen[i]; }
}
if ((v = get("bos"))) { uint8_t buf[70000]; boslen = unhex(v, buf, sizeof buf); bosblob = malloc(boslen); memcpy(bosblob, buf, boslen); }
if ((v = get("mfg"))) make_string(1, v);
if ((v = get("prod"))) make_string(2, v);
if ((v = get("ser"))) make_string(3, v);
for (int i = 1; i < 256; i++) {
uint8_t buf[600];
snprintf(nm, sizeof nm, "rawstr%d", i);
if ((v = get(nm))) { int l = unhex(v, buf, sizeof buf); strdesc[i] = malloc(l); memcpy(strdesc[i], buf, l); strdesclen[i] = l; }
}
}
/* ---------------- raw gadget glue ---------------- */
struct io { struct usb_raw_ep_io h; uint8_t data[66000]; };
static int ep0_write(const void *d, int len)
{
static struct io io;
io.h.ep = 0; io.h.flags = 0; io.h.length = len; memcpy(io.data, d, len);
return ioctl(fd, USB_RAW_IOCTL_EP0_WRITE, &io);
}
static int ep0_ack(void)
{
static struct io io;
io.h.ep = 0; io.h.flags = 0; io.h.length = 0;
return ioctl(fd, USB_RAW_IOCTL_EP0_READ, &io);
}
static int ep_write(int h, const void *d, int len, int zero)
{
struct io *io = calloc(1, sizeof *io);
int r;
io->h.ep = h; io->h.flags = zero ? USB_RAW_IO_FLAGS_ZERO : 0; io->h.length = len; memcpy(io->data, d, len);
r = ioctl(fd, USB_RAW_IOCTL_EP_WRITE, io);
free(io);
return r;
}
static int ep_read(int h, void *d, int len)
{
struct io *io = calloc(1, sizeof *io);
int r;
io->h.ep = h; io->h.flags = 0; io->h.length = len;
r = ioctl(fd, USB_RAW_IOCTL_EP_READ, io);
if (r > 0) memcpy(d, io->data, r);
free(io);
return r;
}
static int eph[256];
static int ep_enable(int addr, int maxp)
{
struct usb_endpoint_descriptor d = { .bLength = 7, .bDescriptorType = 5, .bEndpointAddress = addr, .bmAttributes = 2, .wMaxPacketSize = maxp };
int h = ioctl(fd, USB_RAW_IOCTL_EP_ENABLE, &d);
if (h >= 0) eph[addr & 0xff] = h;
return h;
}
/* ---------------- SCSI model (per transport engine) ---------------- */
struct bot {
int idx, ep_in, ep_out, maxp;
uint8_t sense[96]; int senselen;
uint64_t blocks; uint32_t bsz;
int fail_left[4]; int fail_op[4]; uint8_t fail_sense[4][96]; int fail_senselen[4]; int fail_status[4];
int nlun, rl_count;
};
struct resp {
uint8_t data[65536]; int dlen; int status; int resid_set; uint32_t resid; int stall_in; int babble;
};
static void log_hex(const char *tag, const uint8_t *p, int n)
{
fprintf(stderr, "rg: %s", tag);
for (int i = 0; i < n && i < 64; i++) fprintf(stderr, " %02x", p[i]);
if (n > 64) fprintf(stderr, " ...(%d)", n);
fprintf(stderr, "\n");
}
static void set_sense_fixed(struct bot *b, int key, int asc, int ascq)
{
memset(b->sense, 0, sizeof b->sense);
b->sense[0] = 0x70; b->sense[2] = key; b->sense[7] = 10; b->sense[12] = asc; b->sense[13] = ascq;
b->senselen = 18;
}
static int hexparam(const char *k, int eng, uint8_t *out, int max)
{
const char *v = geti(k, eng);
return v ? unhex(v, out, max) : -1;
}
static void put_alloc(struct resp *r, const uint8_t *src, int len, int alloc)
{
/* device answers with at most the allocation length (honest device); 'babble' ignores it */
int n = (!r->babble && len > alloc) ? alloc : len;
memcpy(r->data, src, n);
r->dlen = n;
}
static void scsi_cmd(struct bot *b, const uint8_t *cdb, struct resp *r)
{
int op = cdb[0], eng = b->idx;
uint8_t tmp[70000];
int n;
memset(r, 0, sizeof *r);
r->babble = numi("babble", eng, 0);
for (int i = 0; i < 4; i++)
if (b->fail_left[i] > 0 && b->fail_op[i] == op) {
b->fail_left[i]--;
memcpy(b->sense, b->fail_sense[i], 96); b->senselen = b->fail_senselen[i];
r->status = b->fail_status[i];
fprintf(stderr, "rg: injected failure op=%02x status=%d sense_len=%d\n", op, r->status, b->senselen);
if (numi("fail_stall_in", eng, 0)) r->stall_in = 1;
return;
}
switch (op) {
case 0x00: case 0x1e: case 0x35: case 0x1b: case 0x2f: case 0x2a: case 0x8a: case 0x2e: return;
case 0x12: {
int alloc = cdb[3] << 8 | cdb[4];
if (cdb[1] & 1) {
char k[16];
snprintf(k, sizeof k, "vpd%02x", cdb[2]);
n = hexparam(k, eng, tmp, sizeof tmp);
if (n < 0) { set_sense_fixed(b, 5, 0x24, 0); r->status = 1; return; }
put_alloc(r, tmp, n, alloc);
} else {
uint8_t std[36] = { 0x00, 0x80, 0x06, 0x02, 0x1f };
n = hexparam("inq", eng, tmp, sizeof tmp);
if (n < 0) {
memcpy(tmp, std, 36);
ascii_pad(geti("vendor", eng) ? geti("vendor", eng) : "KSLICE", tmp + 8, 8);
ascii_pad(geti("model", eng) ? geti("model", eng) : "Test Disk", tmp + 16, 16);
ascii_pad("1.00", tmp + 32, 4);
n = 36;
}
put_alloc(r, tmp, n, alloc);
}
return;
}
case 0x25: {
n = hexparam("cap10", eng, tmp, 8);
if (n < 0) {
uint64_t last = b->blocks - 1;
tmp[0] = last >> 24; tmp[1] = last >> 16; tmp[2] = last >> 8; tmp[3] = last;
tmp[4] = b->bsz >> 24; tmp[5] = b->bsz >> 16; tmp[6] = b->bsz >> 8; tmp[7] = b->bsz;
n = 8;
}
put_alloc(r, tmp, n, 8);
return;
}
case 0x9e: {
int alloc = cdb[10] << 24 | cdb[11] << 16 | cdb[12] << 8 | cdb[13];
if ((cdb[1] & 0x1f) != 0x10) { set_sense_fixed(b, 5, 0x20, 0); r->status = 1; return; }
n = hexparam("cap16", eng, tmp, 64);
if (n < 0) {
uint64_t last = b->blocks - 1;
memset(tmp, 0, 32);
for (int i = 0; i < 8; i++) tmp[i] = last >> (56 - 8 * i);
tmp[8] = b->bsz >> 24; tmp[9] = b->bsz >> 16; tmp[10] = b->bsz >> 8; tmp[11] = b->bsz;
n = 32;
}
put_alloc(r, tmp, n, alloc);
return;
}
case 0x1a: {
int alloc = cdb[4];
n = hexparam("ms6", eng, tmp, 256);
if (n < 0) { tmp[0] = 3; tmp[1] = 0; tmp[2] = 0; tmp[3] = 0; n = 4; }
put_alloc(r, tmp, n, alloc);
return;
}
case 0x5a: {
int alloc = cdb[7] << 8 | cdb[8];
n = hexparam("ms10", eng, tmp, 512);
if (n < 0) { memset(tmp, 0, 8); tmp[1] = 6; n = 8; }
put_alloc(r, tmp, n, alloc);
return;
}
case 0x03: {
int alloc = cdb[4];
if (!b->senselen) set_sense_fixed(b, 0, 0, 0);
n = b->senselen;
put_alloc(r, b->sense, n, alloc);
if (alloc >= n) b->senselen = 0;
return;
}
case 0xa0: {
char k[24];
int alloc = cdb[6] << 24 | cdb[7] << 16 | cdb[8] << 8 | cdb[9];
int limit = numi("max_rl_commands", eng, 0);
if (limit && b->rl_count >= limit) {
fprintf(stderr, "rg: REPORT LUNS ceiling fired after %d commands\n",
b->rl_count);
fflush(stderr);
exit(77);
}
snprintf(k, sizeof k, "rl%d", b->rl_count++);
n = hexparam(k, eng, tmp, sizeof tmp);
if (n < 0)
n = hexparam("rl", eng, tmp, sizeof tmp);
if (n < 0) { set_sense_fixed(b, 5, 0x20, 0); r->status = 1; return; }
put_alloc(r, tmp, n, alloc);
return;
}
case 0xf0: {
/* ums-realtek vendor command: 0x09 reads a 16-byte reader status. */
if (cdb[1] != 0x09) {
set_sense_fixed(b, 5, 0x20, 0);
r->status = 1;
return;
}
n = hexparam("realtek_status", eng, tmp, 16);
if (n < 0) {
static const uint8_t status[16] = {
0x0b, 0xda, 0x01, 0x38, 0, 0, 0,
0x58, 0x88, 0, 0, 0, 0, 0, 0, 0
};
memcpy(tmp, status, sizeof status);
n = sizeof status;
}
put_alloc(r, tmp, n, 16);
return;
}
case 0x28: case 0x88: case 0xa8: {
uint64_t lba; uint32_t cnt; uint32_t len;
if (op == 0x28) { lba = (uint32_t)(cdb[2] << 24 | cdb[3] << 16 | cdb[4] << 8 | cdb[5]); cnt = cdb[7] << 8 | cdb[8]; }
else if (op == 0xa8) { lba = (uint32_t)(cdb[2] << 24 | cdb[3] << 16 | cdb[4] << 8 | cdb[5]); cnt = cdb[6] << 24 | cdb[7] << 16 | cdb[8] << 8 | cdb[9]; }
else { lba = 0; for (int i = 0; i < 8; i++) lba = lba << 8 | cdb[2 + i]; cnt = cdb[10] << 24 | cdb[11] << 16 | cdb[12] << 8 | cdb[13]; }
len = cnt * b->bsz;
if (len > sizeof r->data) len = sizeof r->data;
memset(r->data, 0, len);
if (lba == 0) { n = hexparam("lba0", eng, tmp, 4096); if (n > 0 && n <= (int)len) memcpy(r->data, tmp, n); }
r->dlen = len;
return;
}
default:
fprintf(stderr, "rg: unsupported opcode %02x -> ILLEGAL REQUEST\n", op);
set_sense_fixed(b, 5, 0x20, 0); r->status = 1;
return;
}
}
static void *bot_thread(void *arg)
{
struct bot *b = arg;
uint8_t cbw[512], csw[13];
struct resp *r = malloc(sizeof *r);
fprintf(stderr, "rg: BOT engine %d running ep_in=%d ep_out=%d\n", b->idx, b->ep_in, b->ep_out);
for (;;) {
int n = ep_read(b->ep_out, cbw, 512);
uint32_t tag, dlen;
int in, sent;
if (n < 0) { fprintf(stderr, "rg: engine %d ep_read CBW err %d (%s)\n", b->idx, n, strerror(errno)); return NULL; }
if (n != 31 || memcmp(cbw, "USBC", 4)) { fprintf(stderr, "rg: engine %d bad CBW len %d\n", b->idx, n); log_hex("cbw", cbw, n); continue; }
tag = cbw[4] | cbw[5] << 8 | cbw[6] << 16 | (uint32_t)cbw[7] << 24;
dlen = cbw[8] | cbw[9] << 8 | cbw[10] << 16 | (uint32_t)cbw[11] << 24;
in = cbw[12] & 0x80;
fprintf(stderr, "rg: eng%d CBW tag=%u dlen=%u dir=%s lun=%d cdblen=%d", b->idx, tag, dlen, in ? "IN" : "OUT", cbw[13], cbw[14]);
log_hex(" cdb", cbw + 15, cbw[14]);
scsi_cmd(b, cbw + 15, r);
sent = 0;
if (in && dlen > 0 && r->dlen == 0) r->stall_in = 1; /* Hi > Di: stall the data-in pipe, then CSW */
if (in) {
if (r->stall_in) {
ioctl(fd, USB_RAW_IOCTL_EP_SET_HALT, b->ep_in);
} else if (r->dlen > 0 && dlen > 0) {
int len = r->dlen;
if (!r->babble && len > (int)dlen) len = dlen;
sent = len;
if (ep_write(b->ep_in, r->data, len, (len % b->maxp == 0) && len < (int)dlen) < 0)
fprintf(stderr, "rg: eng%d data-in write failed (%s)\n", b->idx, strerror(errno));
}
} else if (dlen > 0) {
uint32_t got = 0;
uint8_t *junk = malloc(65536);
while (got < dlen) {
int k = ep_read(b->ep_out, junk, 65536 < dlen - got ? 65536 : dlen - got);
if (k <= 0) break;
got += k;
}
free(junk);
sent = got;
}
memcpy(csw, "USBS", 4);
csw[4] = tag; csw[5] = tag >> 8; csw[6] = tag >> 16; csw[7] = tag >> 24;
{
uint32_t resid = r->resid_set ? r->resid : (dlen > (uint32_t)sent ? dlen - sent : 0);
const char *rs = geti("csw_resid", b->idx);
if (rs && r->status == 0 && dlen) resid = strtoul(rs, NULL, 0);
csw[8] = resid; csw[9] = resid >> 8; csw[10] = resid >> 16; csw[11] = resid >> 24;
}
csw[12] = r->status;
if (ep_write(b->ep_in, csw, 13, 0) < 0)
fprintf(stderr, "rg: eng%d CSW write failed (%s)\n", b->idx, strerror(errno));
}
}
struct uas_dev {
struct bot model;
int cmd_out, status_in, data_in, data_out, maxp;
};
static void *uas_thread(void *arg)
{
struct uas_dev *u = arg;
uint8_t cmd[512], status[16 + 96];
struct resp *r = malloc(sizeof *r);
fprintf(stderr,
"rg: UAS engine running cmd=%d status=%d data-in=%d data-out=%d\n",
u->cmd_out, u->status_in, u->data_in, u->data_out);
for (;;) {
int n = ep_read(u->cmd_out, cmd, sizeof cmd);
int status_len = 16;
if (n < 0) {
fprintf(stderr, "rg: UAS command read failed %d (%s)\n",
n, strerror(errno));
return NULL;
}
if (n < 32 || cmd[0] != 0x01) {
fprintf(stderr, "rg: bad UAS command IU len=%d id=%02x\n",
n, n > 0 ? cmd[0] : 0);
log_hex("uas command", cmd, n);
continue;
}
fprintf(stderr, "rg: UAS tag=%u lun-byte1=%u",
cmd[2] << 8 | cmd[3], cmd[9]);
log_hex(" cdb", cmd + 16, n - 16);
scsi_cmd(&u->model, cmd + 16, r);
if (!r->status && r->dlen > 0) {
uint8_t ready[4] = { 0x06, 0, cmd[2], cmd[3] };
fprintf(stderr, "rg: UAS READ READY tag=%u\n",
cmd[2] << 8 | cmd[3]);
if (ep_write(u->status_in, ready, sizeof ready, 0) < 0)
fprintf(stderr, "rg: UAS read-ready write failed (%s)\n",
strerror(errno));
if (ep_write(u->data_in, r->data, r->dlen,
(r->dlen % u->maxp) == 0) < 0)
fprintf(stderr, "rg: UAS data-in write failed (%s)\n",
strerror(errno));
}
memset(status, 0, sizeof status);
status[0] = 0x03;
status[2] = cmd[2];
status[3] = cmd[3];
if (r->status) {
int slen = u->model.senselen;
if (slen > 96)
slen = 96;
status[6] = 0x02;
status[14] = slen >> 8;
status[15] = slen;
memcpy(status + 16, u->model.sense, slen);
status_len += slen;
u->model.senselen = 0;
}
if (ep_write(u->status_in, status, status_len, 0) < 0)
fprintf(stderr, "rg: UAS status write failed (%s)\n",
strerror(errno));
}
}
static void start_engines(void)
{
static int started;
if (started) return;
started = 1;
if (use_uas) {
struct uas_dev *u = calloc(1, sizeof *u);
pthread_t t;
int maxp = speed_cfg == USB_SPEED_FULL ? 64 : 512;
u->model.idx = 0;
u->model.blocks = numi("blocks", 0, 131072);
u->model.bsz = numi("bsz", 0, 512);
u->maxp = maxp;
u->cmd_out = ep_enable(uas_cmd_out, maxp);
u->status_in = ep_enable(uas_status_in, maxp);
u->data_in = ep_enable(uas_data_in, maxp);
u->data_out = ep_enable(uas_data_out, maxp);
if (u->cmd_out < 0 || u->status_in < 0 ||
u->data_in < 0 || u->data_out < 0) {
fprintf(stderr,
"rg: UAS ep_enable failed cmd=%d status=%d in=%d out=%d\n",
u->cmd_out, u->status_in, u->data_in, u->data_out);
return;
}
pthread_create(&t, NULL, uas_thread, u);
return;
}
for (int i = 0; i < nbot; i++) {
struct bot *b = calloc(1, sizeof *b);
pthread_t t;
const char *v;
b->idx = i; b->ep_in = ep_enable(bot_in[i], speed_cfg == USB_SPEED_FULL ? 64 : 512);
b->ep_out = ep_enable(bot_out[i], speed_cfg == USB_SPEED_FULL ? 64 : 512);
b->maxp = speed_cfg == USB_SPEED_FULL ? 64 : 512;
if (b->ep_in < 0 || b->ep_out < 0) { fprintf(stderr, "rg: ep_enable failed in=%d out=%d\n", b->ep_in, b->ep_out); continue; }
b->blocks = numi("blocks", i, 131072); b->bsz = numi("bsz", i, 512);
for (int j = 0; j < 4; j++) {
char k[16];
snprintf(k, sizeof k, "fail%d", j);
if ((v = geti(k, i))) {
/* fail<j>=<opcode hex>:<count>:<status>:<sense hex> */
char *s = strdup(v), *a, *c, *st, *se;
a = strtok(s, ":"); c = strtok(NULL, ":"); st = strtok(NULL, ":"); se = strtok(NULL, "");
b->fail_op[j] = strtol(a, NULL, 16); b->fail_left[j] = strtol(c, NULL, 0); b->fail_status[j] = strtol(st, NULL, 0);
b->fail_senselen[j] = se ? unhex(se, b->fail_sense[j], 96) : 0;
}
}
pthread_create(&t, NULL, bot_thread, b);
}
}
/* ---------------- ep0 ---------------- */
static void handle_control(struct usb_ctrlrequest *c)
{
int wl = c->wLength, type = c->bRequestType, req = c->bRequest, v = c->wValue, idx = c->wIndex;
fprintf(stderr, "rg: ctrl bmRT=%02x bReq=%02x wValue=%04x wIndex=%04x wLength=%d\n", type, req, v, idx, wl);
if ((type & 0x60) == 0x00 && req == USB_REQ_GET_DESCRIPTOR) {
int dt = v >> 8, di = v & 0xff;
const uint8_t *p = NULL; int l = 0;
if (dt == USB_DT_DEVICE) { p = devdesc; l = 18; }
else if (dt == USB_DT_CONFIG && di < ncfg) {
if (wl <= 9) { p = cfgA[di]; l = cfgAlen[di]; } else { p = cfgB[di]; l = cfgBlen[di]; }
} else if (dt == USB_DT_BOS && bosblob) { p = bosblob; l = boslen; }
else if (dt == USB_DT_STRING) {
static uint8_t lang[4] = { 4, 3, 0x09, 0x04 };
if (di == 0 && !strdesc[0]) { p = lang; l = 4; }
else if (strdesc[di]) { p = strdesc[di]; l = strdesclen[di]; }
} else if (dt == USB_DT_DEVICE_QUALIFIER && speed_cfg == USB_SPEED_HIGH) {
static uint8_t q[10] = { 10, 6, 0, 2, 0, 0, 0, 64, 1, 0 };
p = q; l = 10;
}
if (!p) { fprintf(stderr, "rg: -> STALL (no descriptor type %d idx %d)\n", dt, di); ioctl(fd, USB_RAW_IOCTL_EP0_STALL); return; }
if (l > wl) l = wl;
ep0_write(p, l);
return;
}
if ((type & 0x60) == 0x00 && req == USB_REQ_SET_CONFIGURATION) {
curcfg = v & 0xff;
fprintf(stderr, "rg: SET_CONFIGURATION %d\n", curcfg);
if (curcfg) {
ioctl(fd, USB_RAW_IOCTL_VBUS_DRAW, 0x32);
start_engines();
ioctl(fd, USB_RAW_IOCTL_CONFIGURE, 0);
}
ep0_ack();
return;
}
if ((type & 0x60) == 0x00 && req == USB_REQ_SET_INTERFACE) { ep0_ack(); return; }
if ((type & 0x60) == 0x00 && req == USB_REQ_GET_INTERFACE) { uint8_t z = 0; ep0_write(&z, 1); return; }
if ((type & 0x60) == 0x00 && req == USB_REQ_GET_STATUS) { uint8_t z[2] = { 0, 0 }; ep0_write(z, wl < 2 ? wl : 2); return; }
if ((type & 0x60) == 0x00 && req == USB_REQ_CLEAR_FEATURE && (type & 0x1f) == 2 && v == 0) { /* ENDPOINT_HALT */
if (eph[idx & 0xff] >= 0) ioctl(fd, USB_RAW_IOCTL_EP_CLEAR_HALT, eph[idx & 0xff]);
ep0_ack();
return;
}
if ((type & 0x60) == 0x00 && (req == USB_REQ_CLEAR_FEATURE || req == USB_REQ_SET_FEATURE)) { ep0_ack(); return; }
if ((type & 0x60) == 0x00 && req == 0x30 /* SET_SEL */) { struct io *io = calloc(1, sizeof *io); io->h.length = wl < 6 ? wl : 6; ioctl(fd, USB_RAW_IOCTL_EP0_READ, io); free(io); return; }
if ((type & 0x60) == 0x00 && req == 0x31 /* SET_ISOCH_DELAY */) { ep0_ack(); return; }
if ((type & 0x60) == 0x20 && req == 0xfe && (type & 0x80)) { /* GET MAX LUN */
uint8_t m = numi("maxlun", idx < 2 ? idx : 0, 0);
const char *raw = get("maxlun_len");
int l = raw ? strtol(raw, NULL, 0) : 1;
if (l == 0) { ep0_write(&m, 0); return; }
if (num("maxlun_stall", 0)) { ioctl(fd, USB_RAW_IOCTL_EP0_STALL); return; }
ep0_write(&m, wl < 1 ? wl : 1);
return;
}
if ((type & 0x60) == 0x20 && req == 0xff) { ep0_ack(); return; }
fprintf(stderr, "rg: -> STALL (unhandled)\n");
ioctl(fd, USB_RAW_IOCTL_EP0_STALL);
}
int main(int argc, char **argv)
{
struct usb_raw_init init;
struct { struct usb_raw_event e; uint8_t d[256]; } ev;
if (argc < 2) { fprintf(stderr, "usage: rg scenario-file\n"); return 2; }
memset(eph, -1, sizeof eph);
load_scn(argv[1]);
build_device();
fd = open("/dev/raw-gadget", O_RDWR);
if (fd < 0) { perror("/dev/raw-gadget"); return 1; }
memset(&init, 0, sizeof init);
strcpy((char *)init.driver_name, get("udc_driver") ? get("udc_driver") : "dummy_udc");
strcpy((char *)init.device_name, get("udc_device") ? get("udc_device") : "dummy_udc.0");
init.speed = speed_cfg;
if (ioctl(fd, USB_RAW_IOCTL_INIT, &init) < 0) { perror("INIT"); return 1; }
if (ioctl(fd, USB_RAW_IOCTL_RUN, 0) < 0) { perror("RUN"); return 1; }
fprintf(stderr, "rg: running, scenario %s\n", argv[1]);
for (;;) {
memset(&ev, 0, sizeof ev);
ev.e.length = sizeof ev.d;
if (ioctl(fd, USB_RAW_IOCTL_EVENT_FETCH, &ev) < 0) { perror("EVENT_FETCH"); return 1; }
if (ev.e.type == USB_RAW_EVENT_CONNECT) { fprintf(stderr, "rg: CONNECT\n"); continue; }
if (ev.e.type == USB_RAW_EVENT_CONTROL) handle_control((struct usb_ctrlrequest *)ev.d);
else fprintf(stderr, "rg: event type %d\n", ev.e.type);
}
}
[-- Attachment #3: uas-rl-f8.cfg --]
[-- Type: text/plain, Size: 440 bytes --]
# K-1: high-speed UAS; first REPORT LUNS wraps allocation to zero.
uas=1
vid=0x1234
pid=0xa0f8
bcd=0x0100
mfg=KSLICE-mfg
prod=UAS REPORT LUNS F8
ser=UASRLF8
# SPC-4 direct-access, removable, 36-byte standard INQUIRY.
inq=00 80 06 02 1f 00 00 00 4b 53 4c 49 43 45 20 20 55 41 53 20 52 4c 20 46 38 20 20 20 20 20 20 20 31 2e 30 30
# First reply causes (L + 8) modulo 2^32 == 0; second GOOD reply has no data.
rl0=ff ff ff f8 00 00 00 00
rl1=
reply other threads:[~2026-11-21 18:17 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6785b8f2-e089-4e02-848d-6c3327f67750@gmail.com \
--to=atijari00@gmail.com \
--cc=James.Bottomley@HansenPartnership.com \
--cc=linux-scsi@vger.kernel.org \
--cc=mkp@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.