summaryrefslogtreecommitdiffstats
path: root/tests/ubus_list_encoding.py
blob: c0f025d2b8e6af954fb21c4a7eafa03e3e643f04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
#!/usr/bin/env python3
"""
Regression tests for the ubus JSON-RPC "list" response encoding.

Covers GHSA-788h-w5m4-j5xv: uhttpd's list callback opened one nesting level
per signature method of every object returned by a lookup and closed the
returned cookie without checking it. A JSON-RPC "list" request naming enough
object paths made the accumulated response blob approach the 24 bit length
limit of libubox blobs (BLOB_ATTR_LEN_MASK). Appending more data then fails,
blobmsg_open_nested() returns NULL and blobmsg_close_table(buf, NULL) treats
that cookie as an offset into the buffer ((char *)buf->buf - BLOB_COOKIE),
corrupting the blob header and eventually arbitrary heap. On the reported
target this was shaped into unauthenticated remote code execution.

The tests run uhttpd against a throwaway ubusd holding synthetic objects with
large signatures and check that:

  1. an unauthenticated verbose list still works,
  2. a single wildcard lookup over JSON-RPC still works,
  3. a request repeating the same lookup path is encoded once instead of
     once per repetition (the request from the advisory),
  4. a request whose result would outgrow the response budget is answered
     with a bounded JSON-RPC error and uhttpd survives it,
  5. more object paths than allowed are rejected as invalid params,
  6. the "call" and batched request paths still produce well formed answers,
  7. normal list requests are still served after all of the above,
  8. an unparameterized GET list which no longer fits is refused with a
     bounded HTTP 500 instead of being truncated.

Usage: ubus_list_encoding.py /path/to/uhttpd [-v] [--keep] [--objects N]
       [--oversize-objects N]

Needs ubusd plus a C compiler with libubus/libubox headers to build the fake
object provider. The test is skipped (exit code 0) when those are missing.
"""

import argparse
import json
import os
import shutil
import socket
import subprocess
import sys
import tempfile
import time

PROVIDER_SRC = r"""
/* Registers synthetic ubus objects with large signatures, so that a single
 * wildcard lookup already returns a sizeable verbose list blob. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <libubox/blobmsg.h>
#include <libubox/uloop.h>
#include <libubus.h>

static struct ubus_context *ctx;

int main(int argc, char **argv)
{
	const char *sock = argc > 1 ? argv[1] : NULL;
	int nobj = argc > 2 ? atoi(argv[2]) : 60;
	int nmeth = argc > 3 ? atoi(argv[3]) : 30;
	int nargs = argc > 4 ? atoi(argv[4]) : 15;
	struct ubus_object *objs;
	struct ubus_object_type *types;
	struct ubus_method *meths;
	struct blobmsg_policy *pols;
	char name[64], meth[64], arg[32];
	int i, m, a;

	if (!sock) {
		fprintf(stderr, "usage: %s <ubus-socket> [objects] [methods] [args]\n", argv[0]);
		return 1;
	}

	objs = calloc(nobj, sizeof(*objs));
	types = calloc(nobj, sizeof(*types));
	meths = calloc(nobj * nmeth, sizeof(*meths));
	pols = calloc(nobj * nmeth * nargs, sizeof(*pols));
	if (!objs || !types || !meths || !pols)
		return 1;

	uloop_init();

	ctx = ubus_connect(sock);
	if (!ctx) {
		fprintf(stderr, "ubus_connect(%s) failed\n", sock);
		return 1;
	}

	for (i = 0; i < nobj; i++) {
		struct ubus_object_type *t = &types[i];

		snprintf(name, sizeof(name), "probe%03d", i);

		t->name = strdup(name);
		t->methods = &meths[i * nmeth];
		t->n_methods = nmeth;

		for (m = 0; m < nmeth; m++) {
			struct ubus_method *mm = &meths[i * nmeth + m];
			struct blobmsg_policy *pp = &pols[(i * nmeth + m) * nargs];

			snprintf(meth, sizeof(meth), "method%02d", m);

			mm->name = strdup(meth);
			mm->mask = 0;
			mm->policy = pp;
			mm->n_policy = nargs;

			for (a = 0; a < nargs; a++) {
				snprintf(arg, sizeof(arg), "arg%02d", a);
				pp[a].name = strdup(arg);
				pp[a].type = BLOBMSG_TYPE_STRING;
			}
		}

		objs[i].name = t->name;
		objs[i].type = t;

		if (ubus_add_object(ctx, &objs[i])) {
			fprintf(stderr, "ubus_add_object(%s) failed\n", name);
			return 1;
		}
	}

	ubus_add_uloop(ctx);
	uloop_run();

	uloop_done();
	return 0;
}
"""

# libubox bounds blob lengths with BLOB_ATTR_LEN_MASK (0x00ffffff == 16 MiB)
BLOB_LIMIT = 0x00ffffff

# uhttpd's UH_UBUS_MAX_POST_SIZE
UH_UBUS_MAX_POST_SIZE = 65536

# how many lookup paths the fixed build accepts in one "list" request
UH_UBUS_MAX_LIST_PATHS = 512

# how many lookup paths the fixed build rejects as invalid params
OVER_MANY_PATHS = UH_UBUS_MAX_LIST_PATHS + 200


class HttpError(Exception):
    pass


def free_port():
    s = socket.socket()
    s.bind(("127.0.0.1", 0))
    port = s.getsockname()[1]
    s.close()
    return port


def find_ubusd():
    env = os.environ.get("UBUSD")
    if env and os.path.exists(env):
        return env
    for name in ("ubusd", "/usr/sbin/ubusd", "/usr/local/sbin/ubusd", "/sbin/ubusd"):
        path = shutil.which(name) or (name if os.path.exists(name) else None)
        if path:
            return path
    return None


def build_provider(tmpdir, verbose):
    src = os.path.join(tmpdir, "provider.c")
    exe = os.path.join(tmpdir, "provider")

    with open(src, "w") as f:
        f.write(PROVIDER_SRC)

    compilers = [c for c in (os.environ.get("CC"), "cc", "gcc", "clang") if c]
    flagsets = [os.environ.get("UBUS_CFLAGS", "").split(), [], ["-I/usr/local/include"]]
    ldflags = [os.environ.get("UBUS_LDFLAGS", "").split(), [], ["-L/usr/local/lib"]]

    for cc in dict.fromkeys(compilers):
        for cflags in flagsets:
            for ldfs in ldflags:
                cmd = [cc, "-std=gnu99", "-o", exe, src] + cflags + ["-lubus", "-lubox"] + ldfs
                if verbose:
                    print("  try: %s" % " ".join(cmd))
                try:
                    p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                except (FileNotFoundError, PermissionError):
                    continue
                if p.returncode == 0:
                    return exe
                if verbose:
                    print("  %s" % p.stderr.decode(errors="replace").strip())

    return None


def decode_chunked(body):
    out = bytearray()
    while True:
        nl = body.find(b"\r\n")
        if nl < 0:
            break
        try:
            size = int(body[:nl].split(b";")[0], 16)
        except ValueError:
            break
        if size == 0:
            break
        out += body[nl + 2:nl + 2 + size]
        body = body[nl + 2 + size + 2:]
    return bytes(out)


class Client:
    def __init__(self, port):
        self.port = port

    def request(self, method, path, body=None):
        raw = ("%s %s HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n" % (method, path)).encode()

        if body is not None:
            body = body.encode()
            raw += b"Content-Type: application/json\r\nContent-Length: %d\r\n" % len(body)

        raw += b"\r\n"

        sock = socket.create_connection(("127.0.0.1", self.port), timeout=10)
        try:
            sock.sendall(raw + (body or b""))
            sock.settimeout(120)
            data = b""
            while True:
                try:
                    chunk = sock.recv(65536)
                except socket.timeout:
                    raise HttpError("timed out reading response")
                if not chunk:
                    break
                data += chunk
                if data.endswith(b"\r\n0\r\n\r\n"):
                    break
        finally:
            sock.close()

        head, _, rest = data.partition(b"\r\n\r\n")
        status = head.split(b"\r\n", 1)[0].decode(errors="replace")

        headers = {}
        for line in head.split(b"\r\n")[1:]:
            key, _, val = line.partition(b":")
            headers[key.decode().lower()] = val.decode().strip()

        payload = decode_chunked(rest) if headers.get("transfer-encoding", "").lower() == "chunked" else rest

        return status, payload


class Server:
    def __init__(self, uhttpd, tmpdir):
        self.uhttpd = uhttpd
        self.tmpdir = tmpdir
        self.sock = os.path.join(tmpdir, "ubus.sock")
        self.port = free_port()
        self.log = open(os.path.join(tmpdir, "uhttpd.log"), "wb")
        self.procs = []

    def start(self, ubusd, provider, objects):
        self.procs.append(subprocess.Popen([ubusd, "-s", self.sock], cwd=self.tmpdir))

        for _ in range(200):
            if os.path.exists(self.sock):
                break
            time.sleep(0.05)
        else:
            raise RuntimeError("ubusd did not create its socket")

        self.procs.append(subprocess.Popen([provider, self.sock, str(objects)], cwd=self.tmpdir))

        env = dict(os.environ)
        env["LD_LIBRARY_PATH"] = os.path.dirname(os.path.abspath(self.uhttpd)) + os.pathsep + env.get("LD_LIBRARY_PATH", "")

        self.procs.append(subprocess.Popen(
            [self.uhttpd, "-f", "-p", "127.0.0.1:%d" % self.port, "-h", self.tmpdir,
             "-u", "/ubus", "-U", self.sock, "-t", "10", "-T", "10"],
            cwd=self.tmpdir, env=env, stdout=self.log, stderr=self.log))

    def alive(self):
        return all(p.poll() is None for p in self.procs)

    def dead_with(self):
        return [(i, p.returncode) for i, p in enumerate(self.procs) if p.returncode is not None]

    def uhttpd_output(self, limit=8):
        try:
            self.log.flush()
            with open(self.log.name, "rb") as f:
                lines = f.read().splitlines()[-limit:]
            return b" / ".join(lines).decode(errors="replace")
        except Exception:
            return ""

    def stop(self):
        for proc in reversed(self.procs):
            if proc.poll() is None:
                proc.terminate()
        for proc in self.procs:
            try:
                proc.wait(timeout=5)
            except subprocess.TimeoutExpired:
                proc.kill()
                proc.wait()
        self.log.close()

    def wait_for_objects(self, client, wanted, timeout=60):
        deadline = time.time() + timeout
        while time.time() < deadline:
            try:
                _, body = client.request("GET", "/ubus/list")
                names = json.loads(body.decode(errors="replace"))
                if isinstance(names, dict) and all(w in names for w in wanted):
                    return sorted(names)
            except Exception:
                pass
            time.sleep(0.2)
        return None


def list_body(paths):
    return json.dumps({"jsonrpc": "2.0", "id": 1, "method": "list", "params": list(paths)})


def describe(body, paths):
    if paths is not None:
        return "list with %d paths" % len(paths)
    return body[:120]


def jsonrpc_error_code(payload):
    try:
        obj = json.loads(payload.decode(errors="replace"))
    except Exception:
        return None

    if isinstance(obj, dict):
        err = obj.get("error")
        if isinstance(err, dict):
            return err.get("code")
    return None


def overflow_paths(names, per_object, budget=2 * 1024 * 1024, max_paths=UH_UBUS_MAX_LIST_PATHS):
    """A set of distinct lookup paths whose combined result exceeds budget.

    Every entry is a different string, so none of them can be collapsed as a
    duplicate, and the broadest wildcards come first because they contribute
    the most response bytes per request byte.
    """
    candidates = []
    seen = set()

    for name in names:
        for i in range(1, len(name) + 1):
            path = name[:i] + "*"
            if path not in seen:
                seen.add(path)
                candidates.append(path)

    candidates.sort(key=lambda path: -sum(1 for n in names if n.startswith(path[:-1])))

    paths = []
    total = 0

    for path in candidates:
        if total > budget or len(paths) >= max_paths:
            break
        paths.append(path)
        total += per_object * sum(1 for n in names if n.startswith(path[:-1]))

    return paths, total


def run_oversize_get_case(uhttpd, ubusd, provider, tmpdir, objects, failures):
    """An unparameterized list which no longer fits has to be refused.

    Runs a second, independent uhttpd instance against an ubusd holding enough
    objects to push a single full list past the response budget.
    """
    sub = os.path.join(tmpdir, "oversize")
    os.makedirs(sub, exist_ok=True)

    server = Server(uhttpd, sub)
    try:
        server.start(ubusd, provider, objects)
        client = Client(server.port)
        last = "probe%03d" % (objects - 1)

        deadline = time.time() + 60
        while time.time() < deadline:
            try:
                status, _ = client.request("GET", "/ubus/list/" + last)
                if "200" in status:
                    break
            except (OSError, HttpError):
                pass
            time.sleep(0.2)
        else:
            failures.append("case9: ubus objects never showed up (%s)" % server.uhttpd_output())
            return

        status, body = client.request("GET", "/ubus/list")
        try:
            code = json.loads(body.decode(errors="replace")).get("code")
        except Exception:
            code = None

        if "500" in status and code == -EFBIG and server.alive():
            print("ok   case9: GET /ubus/list over %d objects -> %s, code %s, %d bytes"
                  % (objects, status, code, len(body)))
        else:
            failures.append("case9: GET /ubus/list over %d objects -> %s, code %s, "
                            "%d bytes (alive=%s)" % (objects, status, code, len(body), server.alive()))

        status, body = client.request("GET", "/ubus/list/probe000")
        if "200" in status and b"method00" in body and server.alive():
            print("ok   case10: GET /ubus/list/probe000 -> %s after the refused list" % status)
        else:
            failures.append("case10: GET /ubus/list/probe000 -> %s (%d bytes)" % (status, len(body)))
    finally:
        server.stop()


EFBIG = 27


def run_oversize_get_case(uhttpd, ubusd, provider, tmpdir, objects, failures):
    """An unparameterized list which no longer fits has to be refused.

    Runs a second, independent uhttpd instance against a throwaway ubusd
    holding enough objects to push a single full list past the response budget.
    """
    sub = os.path.join(tmpdir, "oversize")
    os.makedirs(sub, exist_ok=True)

    server = Server(uhttpd, sub)
    try:
        server.start(ubusd, provider, objects)
        client = Client(server.port)
        last = "probe%03d" % (objects - 1)

        deadline = time.time() + 60
        while time.time() < deadline:
            try:
                status, _ = client.request("GET", "/ubus/list/" + last)
                if "200" in status:
                    break
            except (OSError, HttpError):
                pass
            time.sleep(0.2)
        else:
            failures.append("case9: ubus objects never showed up (%s)" % server.uhttpd_output())
            return

        try:
            status, body = client.request("GET", "/ubus/list")
        except (OSError, HttpError) as e:
            status, body = "no response (%s)" % e, b""

        try:
            code = json.loads(body.decode(errors="replace")).get("code")
        except Exception:
            code = None

        if "500" in status and code == -EFBIG and server.alive():
            print("ok   case9: GET /ubus/list of %d objects -> %s, code %s, %d bytes"
                  % (objects, status, code, len(body)))
        else:
            failures.append("case9: GET /ubus/list of %d objects -> %s, code %s, "
                            "%d bytes (alive=%s)" % (objects, status, code, len(body), server.alive()))

        status, body = client.request("GET", "/ubus/list/probe000")
        if "200" in status and b"method00" in body and server.alive():
            print("ok   case10: GET /ubus/list/probe000 -> %s after the refused list" % status)
        else:
            failures.append("case10: GET /ubus/list/probe000 -> %s (%d bytes)" % (status, len(body)))
    finally:
        server.stop()


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("uhttpd", help="path to the uhttpd binary")
    ap.add_argument("-v", "--verbose", action="store_true")
    ap.add_argument("--keep", action="store_true", help="keep the scratch directory")
    ap.add_argument("--objects", type=int, default=60, help="number of fake ubus objects")
    ap.add_argument("--oversize-objects", type=int, default=300,
                    help="object count of the oversize GET list case, 0 to skip")
    args = ap.parse_args()

    uhttpd = os.path.abspath(args.uhttpd)
    if not os.path.isfile(uhttpd):
        print("FAIL: no such binary: %s" % uhttpd)
        return 2

    ubusd = find_ubusd()
    if not ubusd:
        print("SKIP: no ubusd binary found (set UBUSD=/path/to/ubusd)")
        return 0

    tmpdir = tempfile.mkdtemp(prefix="uhttpd-ubus-list-")

    provider = build_provider(tmpdir, args.verbose)
    if not provider:
        print("SKIP: could not build the ubus object provider (needs libubus + libubox headers)")
        if not args.keep:
            shutil.rmtree(tmpdir, ignore_errors=True)
        return 0

    objects = ["probe%03d" % i for i in range(args.objects)]
    failures = []
    server = Server(uhttpd, tmpdir)

    def call(name, body, expect_codes, paths=None):
        """POST a JSON-RPC list request and check that the answer stays bounded."""
        if isinstance(expect_codes, int):
            expect_codes = (expect_codes,)
        try:
            status, payload = client.request("POST", "/ubus", body)
            code = jsonrpc_error_code(payload)
        except (OSError, HttpError) as e:
            status, payload, code = "no response (%s)" % e, b"", None

        if not server.alive():
            failures.append("%s: %s killed uhttpd (%s, exited with %s)"
                            % (name, body[:120], status, server.dead_with()))
            return status, payload, code

        if code in expect_codes:
            print("ok   %s: %s -> %s, server alive" % (name, describe(body, paths), code))
        elif code is not None:
            failures.append("%s: %s -> unexpected error %s" % (name, describe(body, paths), code))
        else:
            failures.append("%s: %s -> %s, %d bytes, no bounded error (alive=%s)"
                            % (name, describe(body, paths), status, len(payload), server.alive()))

        return status, payload, code

    try:
        server.start(ubusd, provider, args.objects)
        client = Client(server.port)

        names = server.wait_for_objects(client, objects)
        if names is None:
            print("FAIL: fake ubus objects never showed up (%s)" % server.uhttpd_output())
            return 2

        # 1. unauthenticated verbose list
        status, body = client.request("GET", "/ubus/list")
        if "200" in status and b"probe000" in body and server.alive():
            print("ok   case1: GET /ubus/list -> %s, %d bytes" % (status, len(body)))
        else:
            failures.append("case1: GET /ubus/list -> %s (%d bytes)" % (status, len(body)))

        # 2. a single wildcard lookup over JSON-RPC, without any session id
        status, body = client.request("POST", "/ubus", list_body(["*"]))
        result = None
        try:
            result = json.loads(body.decode(errors="replace")).get("result")
        except Exception:
            pass

        if isinstance(result, dict) and all(w in result for w in objects) and server.alive():
            single = len(body)
            print("ok   case2: list(['*']) -> %s, %d objects, %d bytes" % (status, len(result), single))
        else:
            single = 1
            failures.append("case2: list(['*']) -> %s, incomplete result (%s)"
                            % (status, body[:120].decode(errors="replace")))

        # 3. the request from the advisory: the same path over and over again.
        #    Repeating it must not encode the same objects over and over.
        reps = (BLOB_LIMIT + 4 * 1024 * 1024) // max(single, 1) + 10
        reps = min(reps, (UH_UBUS_MAX_POST_SIZE - 64) // 4)

        status, body = client.request("POST", "/ubus", list_body(["*"] * reps))
        duplicates = body.count(b'"probe000"')
        if not server.alive():
            failures.append("case3: list(['*']*%d) killed uhttpd (exited with %s)"
                            % (reps, server.dead_with()))
        elif duplicates > 1:
            failures.append("case3: list(['*']*%d) encoded 'probe000' %d times" % (reps, duplicates))
        else:
            print("ok   case3: list(['*']*%d) -> %s, each object encoded once, %d bytes"
                  % (reps, status, len(body)))

        # 4. distinct paths whose combined result exceeds the response budget
        per_object = max(single // max(len(objects), 1), 1)
        paths, expected = overflow_paths(names, per_object, budget=3 * 1024 * 1024)
        body = list_body(paths)

        if len(paths) < 2 or expected <= 3 * 1024 * 1024:
            failures.append("case4: cannot build an over-large result from %d lookup paths "
                            "(estimated %d bytes)" % (len(paths), expected))
        elif len(body) > UH_UBUS_MAX_POST_SIZE:
            failures.append("case4: request would be %d bytes, above the post limit" % len(body))
        else:
            print("     case4: %d distinct paths, result should be about %d bytes" % (len(paths), expected))
            call("case4", body, (-32603, -32602), paths)

        # 5. more lookup paths than a single request may name
        paths = [objects[i % len(objects)] for i in range(OVER_MANY_PATHS)]
        call("case5", list_body(paths), -32602, paths)

        # 6. error responses of the "call" path are still well formed
        call_body = json.dumps({"jsonrpc": "2.0", "id": 1, "method": "call",
                               "params": ["0" * 32, "nosuchobject", "test", {}]})
        try:
            status, body = client.request("POST", "/ubus", call_body)
            code = jsonrpc_error_code(body)
        except (OSError, HttpError) as e:
            status, body, code = "no response (%s)" % e, b"", None

        if code in (-32000, -32001, -32002) and server.alive():
            print("ok   case6: call() on a missing object -> %s" % code)
        else:
            failures.append("case6: call() on a missing object -> %s (%s)"
                            % (status, body[:120].decode(errors="replace")))

        # 7. batched requests are still answered with a JSON array
        batch = json.dumps([
            {"jsonrpc": "2.0", "id": 1, "method": "list", "params": [objects[2]]},
            {"jsonrpc": "2.0", "id": 2, "method": "call",
             "params": ["0" * 32, "nosuchobject", "test", {}]},
        ])
        try:
            status, body = client.request("POST", "/ubus", batch)
            parsed = json.loads(body.decode(errors="replace"))
            ok = isinstance(parsed, list) and len(parsed) == 2 and server.alive()
        except (OSError, HttpError, ValueError) as e:
            status, body, ok = "no response (%s)" % e, b"", False

        if ok:
            print("ok   case7: batched list+call -> %s, 2 results" % status)
        else:
            failures.append("case7: batched list+call -> %s (%s)"
                            % (status, body[:120].decode(errors="replace")))

        # 8. normal list requests are still served after the abuse above
        try:
            status, body = client.request("POST", "/ubus", list_body([objects[1]]))
            ok = "200" in status and b"method00" in body and server.alive()
        except (OSError, HttpError) as e:
            status, body, ok = "no response (%s)" % e, b"", False

        if ok:
            print("ok   case8: list(['%s']) -> %s after the abuse cases" % (objects[1], status))
        else:
            failures.append("case8: normal list after abuse -> %s (%d bytes), alive=%s"
                            % (status, len(body), server.alive()))
    finally:
        server.stop()

    if args.oversize_objects:
        run_oversize_get_case(uhttpd, ubusd, provider, tmpdir, args.oversize_objects, failures)

    if not args.keep:
        shutil.rmtree(tmpdir, ignore_errors=True)
    else:
        print("\nscratch directory kept: %s" % tmpdir)

    if failures:
        print()
        for f in failures:
            print("FAIL: %s" % f)
        return 1

    print("all ubus list encoding cases passed")
    return 0


if __name__ == "__main__":
    sys.exit(main())