summaryrefslogtreecommitdiffstats
path: root/src/nspawn/nspawn-oci.c
blob: 1db69b45128414ebf1dfdd52ba4973cdc06bc232 (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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <linux/oom.h>
#if HAVE_SECCOMP
#include <seccomp.h>
#endif

#include "bus-util.h"
#include "cap-list.h"
#include "cpu-set-util.h"
#include "env-util.h"
#include "format-util.h"
#include "fs-util.h"
#include "hostname-util.h"
#include "json.h"
#include "missing_sched.h"
#include "nspawn-oci.h"
#include "path-util.h"
#include "rlimit-util.h"
#if HAVE_SECCOMP
#include "seccomp-util.h"
#endif
#include "stat-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "user-util.h"

/* TODO:
 * OCI runtime tool implementation
 * hooks
 *
 * Spec issues:
 *
 * How is RLIM_INFINITY supposed to be encoded?
 * configured effective caps is bullshit, as execv() corrupts it anyway
 * pipes bind mounted is *very* different from pipes newly created, comments regarding bind mount or not are bogus
 * annotation values structured? or string?
 * configurable file system namespace path, but then also root path? wtf?
 * apply sysctl inside of the container? or outside?
 * how is unlimited pids tasks limit to be encoded?
 * what are the defaults for caps if not specified?
 * what are the default uid/gid mappings if one is missing but the other set, or when user ns is on but no namespace configured
 * the source field of "mounts" is really weird, as it cannot realistically be relative to the bundle, since we never know if that's what the fs wants
 * spec contradicts itself on the mount "type" field, as the example uses "bind" as type, but it's not listed in /proc/filesystem, and is something made up by /bin/mount
 * if type of mount is left out, what shall be assumed? "bind"?
 * readonly mounts is entirely redundant?
 * should escaping be applied when joining mount options with ","?
 * devices cgroup support is bogus, "allow" and "deny" on the kernel level is about adding/removing entries, not about access
 * spec needs to say that "rwm" devices cgroup combination can't be the empty string
 * cgrouspv1 crap: kernel, kernelTCP, swapiness, disableOOMKiller, swap, devices, leafWeight
 * general: it shouldn't leak lower level abstractions this obviously
 * unmanagable cgroups stuff: realtimeRuntime/realtimePeriod
 * needs to say what happense when some option is not specified, i.e. which defaults apply
 * no architecture? no personality?
 * seccomp example and logic is simply broken: there's no constant "SCMP_ACT_ERRNO".
 * spec should say what to do with unknown props
 * /bin/mount regarding NFS and FUSE required?
 * what does terminal=false mean?
 * sysctl inside or outside? allow-listing?
 * swapiness typo -> swappiness
 *
 * Unsupported:
 *
 * apparmorProfile
 * selinuxLabel + mountLabel
 * hugepageLimits
 * network
 * rdma
 * intelRdt
 * swappiness, disableOOMKiller, kernel, kernelTCP, leafWeight (because it's dead, cgroupsv2 can't do it and hence systemd neither)
 *
 * Non-slice cgroup paths
 * Propagation that is not slave + shared
 * more than one uid/gid mapping, mappings with a container base != 0, or non-matching uid/gid mappings
 * device cgroups access = false items that are not catchall
 * device cgroups matches where minor is specified, but major isn't. similar where major is specified but char/block is not. also, any match that only has a type set that has less than "rwm" set. also, any entry that has none of rwm set.
 *
 */

static int oci_unexpected(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                        "Unexpected OCI element '%s' of type '%s'.", name, json_variant_type_to_string(json_variant_type(v)));
}

static int oci_unsupported(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
                        "Unsupported OCI element '%s' of type '%s'.", name, json_variant_type_to_string(json_variant_type(v)));
}

static int oci_terminal(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        Settings *s = userdata;

        /* If not specified, or set to true, we'll default to either an interactive or a read-only
         * console. If specified as false, we'll forcibly move to "pipe" mode though. */
        s->console_mode = json_variant_boolean(v) ? _CONSOLE_MODE_INVALID : CONSOLE_PIPE;
        return 0;
}

static int oci_console_dimension(const char *name, JsonVariant *variant, JsonDispatchFlags flags, void *userdata) {
        unsigned *u = userdata;
        uintmax_t k;

        assert(u);

        k = json_variant_unsigned(variant);
        if (k == 0)
                return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE),
                                "Console size field '%s' is too small.", strna(name));
        if (k > USHRT_MAX) /* TIOCSWINSZ's struct winsize uses "unsigned short" for width and height */
                return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE),
                                "Console size field '%s' is too large.", strna(name));

        *u = (unsigned) k;
        return 0;
}

static int oci_console_size(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {

        static const JsonDispatch table[] = {
                { "height", JSON_VARIANT_UNSIGNED, oci_console_dimension, offsetof(Settings, console_height), JSON_MANDATORY },
                { "width",  JSON_VARIANT_UNSIGNED, oci_console_dimension, offsetof(Settings, console_width),  JSON_MANDATORY },
                {}
        };

        return json_dispatch(v, table, oci_unexpected, flags, userdata);
}

static int oci_absolute_path(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        char **p = userdata;
        const char *n;

        assert(p);

        n = json_variant_string(v);

        if (!path_is_absolute(n))
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                "Path in JSON field '%s' is not absolute: %s", strna(name), n);

        return free_and_strdup_warn(p, n);
}

static int oci_env(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        char ***l = userdata;
        JsonVariant *e;
        int r;

        assert(l);

        JSON_VARIANT_ARRAY_FOREACH(e, v) {
                const char *n;

                if (!json_variant_is_string(e))
                        return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Environment array contains non-string.");

                assert_se(n = json_variant_string(e));

                if (!env_assignment_is_valid(n))
                        return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Environment assignment not valid: %s", n);

                r = strv_extend(l, n);
                if (r < 0)
                        return log_oom();
        }

        return 0;
}

static int oci_args(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        _cleanup_strv_free_ char **l = NULL;
        char ***value = userdata;
        int r;

        assert(value);

        r = json_variant_strv(v, &l);
        if (r < 0)
                return json_log(v, flags, r, "Cannot parse arguments as list of strings: %m");

        if (strv_isempty(l))
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                "Argument list empty, refusing.");

        if (isempty(l[0]))
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                "Executable name is empty, refusing.");

        return strv_free_and_replace(*value, l);
}

static int oci_rlimit_type(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        const char *z;
        int t, *type = userdata;

        assert_se(type);

        z = startswith(json_variant_string(v), "RLIMIT_");
        if (!z)
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                "rlimit entry's name does not begin with 'RLIMIT_', refusing: %s",
                                json_variant_string(v));

        t = rlimit_from_string(z);
        if (t < 0)
                return json_log(v, flags, t,
                                "rlimit name unknown: %s", json_variant_string(v));

        *type = t;
        return 0;
}

static int oci_rlimit_value(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        rlim_t z, *value = userdata;

        assert(value);

        if (json_variant_is_negative(v))
                z = RLIM_INFINITY;
        else {
                if (!json_variant_is_unsigned(v))
                        return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
                                        "rlimits limit not unsigned, refusing.");

                z = (rlim_t) json_variant_unsigned(v);

                if ((uintmax_t) z != json_variant_unsigned(v))
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "rlimits limit out of range, refusing.");
        }

        *value = z;
        return 0;
}

static int oci_rlimits(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {

        Settings *s = userdata;
        JsonVariant *e;
        int r;

        assert(s);

        JSON_VARIANT_ARRAY_FOREACH(e, v) {

                struct rlimit_data {
                        int type;
                        rlim_t soft;
                        rlim_t hard;
                } data = {
                        .type = -1,
                        .soft = RLIM_INFINITY,
                        .hard = RLIM_INFINITY,
                };

                static const JsonDispatch table[] = {
                        { "soft", JSON_VARIANT_NUMBER, oci_rlimit_value, offsetof(struct rlimit_data, soft), JSON_MANDATORY },
                        { "hard", JSON_VARIANT_NUMBER, oci_rlimit_value, offsetof(struct rlimit_data, hard), JSON_MANDATORY },
                        { "type", JSON_VARIANT_STRING, oci_rlimit_type,  offsetof(struct rlimit_data, type), JSON_MANDATORY },
                        {}
                };

                r = json_dispatch(e, table, oci_unexpected, flags, &data);
                if (r < 0)
                        return r;

                assert(data.type >= 0);
                assert(data.type < _RLIMIT_MAX);

                if (s->rlimit[data.type])
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "rlimits array contains duplicate entry, refusing.");

                s->rlimit[data.type] = new(struct rlimit, 1);
                if (!s->rlimit[data.type])
                        return log_oom();

                *s->rlimit[data.type] = (struct rlimit) {
                        .rlim_cur = data.soft,
                        .rlim_max = data.hard,
                };

        }
        return 0;
}

static int oci_capability_array(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        uint64_t *mask = userdata, m = 0;
        JsonVariant *e;

        JSON_VARIANT_ARRAY_FOREACH(e, v) {
                const char *n;
                int cap;

                if (!json_variant_is_string(e))
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Entry in capabilities array is not a string.");

                assert_se(n = json_variant_string(e));

                cap = capability_from_name(n);
                if (cap < 0)
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Unknown capability: %s", n);

                m |= UINT64_C(1) << cap;
        }

        if (*mask == UINT64_MAX)
                *mask = m;
        else
                *mask |= m;

        return 0;
}

static int oci_capabilities(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {

        static const JsonDispatch table[] = {
                { "effective",   JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, effective)   },
                { "bounding",    JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, bounding)    },
                { "inheritable", JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, inheritable) },
                { "permitted",   JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, permitted)   },
                { "ambient",     JSON_VARIANT_ARRAY, oci_capability_array, offsetof(CapabilityQuintet, ambient)     },
                {}
        };

        Settings *s = userdata;
        int r;

        assert(s);

        r = json_dispatch(v, table, oci_unexpected, flags, &s->full_capabilities);
        if (r < 0)
                return r;

        if (s->full_capabilities.bounding != UINT64_MAX) {
                s->capability = s->full_capabilities.bounding;
                s->drop_capability = ~s->full_capabilities.bounding;
        }

        return 0;
}

static int oci_oom_score_adj(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        Settings *s = userdata;
        intmax_t k;

        assert(s);

        k = json_variant_integer(v);
        if (k < OOM_SCORE_ADJ_MIN || k > OOM_SCORE_ADJ_MAX)
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                "oomScoreAdj value out of range: %ji", k);

        s->oom_score_adjust = (int) k;
        s->oom_score_adjust_set = true;

        return 0;
}

static int oci_uid_gid(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        uid_t *uid = userdata, u;
        uintmax_t k;

        assert(uid);
        assert_cc(sizeof(uid_t) == sizeof(gid_t));

        k = json_variant_unsigned(v);
        u = (uid_t) k;
        if ((uintmax_t) u != k)
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                "UID/GID out of range: %ji", k);

        if (!uid_is_valid(u))
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                "UID/GID is not valid: " UID_FMT, u);

        *uid = u;
        return 0;
}

static int oci_supplementary_gids(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        Settings *s = userdata;
        JsonVariant *e;
        int r;

        assert(s);

        JSON_VARIANT_ARRAY_FOREACH(e, v) {
                gid_t gid, *a;

                if (!json_variant_is_unsigned(e))
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Supplementary GID entry is not a UID.");

                r = oci_uid_gid(name, e, flags, &gid);
                if (r < 0)
                        return r;

                a = reallocarray(s->supplementary_gids, s->n_supplementary_gids + 1, sizeof(gid_t));
                if (!a)
                        return log_oom();

                s->supplementary_gids = a;
                s->supplementary_gids[s->n_supplementary_gids++] = gid;
        }

        return 0;
}

static int oci_user(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        static const JsonDispatch table[] = {
                { "uid",            JSON_VARIANT_UNSIGNED, oci_uid_gid,            offsetof(Settings, uid), JSON_MANDATORY },
                { "gid",            JSON_VARIANT_UNSIGNED, oci_uid_gid,            offsetof(Settings, gid), JSON_MANDATORY },
                { "additionalGids", JSON_VARIANT_ARRAY,    oci_supplementary_gids, 0,                       0              },
                {}
        };

        return json_dispatch(v, table, oci_unexpected, flags, userdata);
}

static int oci_process(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {

        static const JsonDispatch table[] = {
                { "terminal",        JSON_VARIANT_BOOLEAN, oci_terminal,          0,                                     0               },
                { "consoleSize",     JSON_VARIANT_OBJECT,  oci_console_size,      0,                                     0               },
                { "cwd",             JSON_VARIANT_STRING,  oci_absolute_path,     offsetof(Settings, working_directory), 0               },
                { "env",             JSON_VARIANT_ARRAY,   oci_env,               offsetof(Settings, environment),       0               },
                { "args",            JSON_VARIANT_ARRAY,   oci_args,              offsetof(Settings, parameters),        0               },
                { "rlimits",         JSON_VARIANT_ARRAY,   oci_rlimits,           0,                                     0               },
                { "apparmorProfile", JSON_VARIANT_STRING,  oci_unsupported,       0,                                     JSON_PERMISSIVE },
                { "capabilities",    JSON_VARIANT_OBJECT,  oci_capabilities,      0,                                     0               },
                { "noNewPrivileges", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(Settings, no_new_privileges), 0               },
                { "oomScoreAdj",     JSON_VARIANT_INTEGER, oci_oom_score_adj,     0,                                     0               },
                { "selinuxLabel",    JSON_VARIANT_STRING,  oci_unsupported,       0,                                     JSON_PERMISSIVE },
                { "user",            JSON_VARIANT_OBJECT,  oci_user,              0,                                     0               },
                {}
        };

        return json_dispatch(v, table, oci_unexpected, flags, userdata);
}

static int oci_root(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        Settings *s = userdata;
        int r;

        static const JsonDispatch table[] = {
                { "path",     JSON_VARIANT_STRING,  json_dispatch_string,  offsetof(Settings, root)      },
                { "readonly", JSON_VARIANT_BOOLEAN, json_dispatch_boolean, offsetof(Settings, read_only) },
                {}
        };

        r = json_dispatch(v, table, oci_unexpected, flags, s);
        if (r < 0)
                return r;

        if (s->root && !path_is_absolute(s->root)) {
                char *joined;

                joined = path_join(s->bundle, s->root);
                if (!joined)
                        return log_oom();

                free_and_replace(s->root, joined);
        }

        return 0;
}

static int oci_hostname(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        Settings *s = userdata;
        const char *n;

        assert(s);

        assert_se(n = json_variant_string(v));

        if (!hostname_is_valid(n, 0))
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                "Hostname string is not a valid hostname: %s", n);

        return free_and_strdup_warn(&s->hostname, n);
}

static bool oci_exclude_mount(const char *path) {

        /* Returns "true" for all mounts we insist to mount on our own, and hence ignore the OCI data. */

        if (PATH_IN_SET(path,
                        "/dev",
                        "/dev/mqueue",
                        "/dev/pts",
                        "/dev/shm",
                        "/proc",
                        "/proc/acpi",
                        "/proc/apm",
                        "/proc/asound",
                        "/proc/bus",
                        "/proc/fs",
                        "/proc/irq",
                        "/proc/kallsyms",
                        "/proc/kcore",
                        "/proc/keys",
                        "/proc/scsi",
                        "/proc/sys",
                        "/proc/sys/net",
                        "/proc/sysrq-trigger",
                        "/proc/timer_list",
                        "/run",
                        "/sys",
                        "/sys",
                        "/sys/fs/selinux",
                        "/tmp"))
                return true;

        /* Similar, skip the whole /sys/fs/cgroups subtree */
        if (path_startswith(path, "/sys/fs/cgroup"))
                return true;

        return false;
}

typedef struct oci_mount_data {
        char *destination;
        char *source;
        char *type;
        char **options;
} oci_mount_data;

static void cleanup_oci_mount_data(oci_mount_data *data) {
        free(data->destination);
        free(data->source);
        strv_free(data->options);
        free(data->type);
}

static int oci_mounts(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        Settings *s = userdata;
        JsonVariant *e;
        int r;

        assert(s);

        JSON_VARIANT_ARRAY_FOREACH(e, v) {
                static const JsonDispatch table[] = {
                        { "destination", JSON_VARIANT_STRING, oci_absolute_path,    offsetof(oci_mount_data, destination), JSON_MANDATORY },
                        { "source",      JSON_VARIANT_STRING, json_dispatch_string, offsetof(oci_mount_data, source),      0              },
                        { "options",     JSON_VARIANT_ARRAY,  json_dispatch_strv,   offsetof(oci_mount_data, options),     0,             },
                        { "type",        JSON_VARIANT_STRING, json_dispatch_string, offsetof(oci_mount_data, type),        0              },
                        {}
                };

                _cleanup_free_ char *joined_options = NULL;
                CustomMount *m;
                _cleanup_(cleanup_oci_mount_data) oci_mount_data data = {};

                r = json_dispatch(e, table, oci_unexpected, flags, &data);
                if (r < 0)
                        return r;

                if (!path_is_absolute(data.destination))
                        return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Mount destination not an absolute path: %s", data.destination);

                if (oci_exclude_mount(data.destination))
                        continue;

                if (data.options) {
                        joined_options = strv_join(data.options, ",");
                        if (!joined_options)
                                return log_oom();
                }

                if (!data.type || streq(data.type, "bind")) {
                        if (data.source && !path_is_absolute(data.source)) {
                                char *joined;

                                joined = path_join(s->bundle, data.source);
                                if (!joined)
                                        return log_oom();

                                free_and_replace(data.source, joined);
                        }

                        data.type = mfree(data.type);

                        m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_BIND);
                } else
                        m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_ARBITRARY);
                if (!m)
                        return log_oom();

                m->destination = TAKE_PTR(data.destination);
                m->source = TAKE_PTR(data.source);
                m->options = TAKE_PTR(joined_options);
                m->type_argument = TAKE_PTR(data.type);
        }

        return 0;
}

static int oci_namespace_type(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        unsigned long *nsflags = userdata;
        const char *n;

        assert(nsflags);
        assert_se(n = json_variant_string(v));

        /* We don't use namespace_flags_from_string() here, as the OCI spec uses slightly different names than the
         * kernel here. */
        if (streq(n, "pid"))
                *nsflags = CLONE_NEWPID;
        else if (streq(n, "network"))
                *nsflags = CLONE_NEWNET;
        else if (streq(n, "mount"))
                *nsflags = CLONE_NEWNS;
        else if (streq(n, "ipc"))
                *nsflags = CLONE_NEWIPC;
        else if (streq(n, "uts"))
                *nsflags = CLONE_NEWUTS;
        else if (streq(n, "user"))
                *nsflags = CLONE_NEWUSER;
        else if (streq(n, "cgroup"))
                *nsflags = CLONE_NEWCGROUP;
        else
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                "Unknown cgroup type, refusing: %s", n);

        return 0;
}

static int oci_namespaces(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        Settings *s = userdata;
        unsigned long n = 0;
        JsonVariant *e;
        int r;

        assert_se(s);

        JSON_VARIANT_ARRAY_FOREACH(e, v) {

                struct namespace_data {
                        unsigned long type;
                        char *path;
                } data = {};

                static const JsonDispatch table[] = {
                        { "type", JSON_VARIANT_STRING, oci_namespace_type, offsetof(struct namespace_data, type), JSON_MANDATORY },
                        { "path", JSON_VARIANT_STRING, oci_absolute_path,  offsetof(struct namespace_data, path), 0              },
                        {}
                };

                r = json_dispatch(e, table, oci_unexpected, flags, &data);
                if (r < 0) {
                        free(data.path);
                        return r;
                }

                if (data.path) {
                        if (data.type != CLONE_NEWNET) {
                                free(data.path);
                                return json_log(e, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
                                                "Specifying namespace path for non-network namespace is not supported.");
                        }

                        if (s->network_namespace_path) {
                                free(data.path);
                                return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
                                                "Network namespace path specified more than once, refusing.");
                        }

                        free(s->network_namespace_path);
                        s->network_namespace_path = data.path;
                }

                if (FLAGS_SET(n, data.type))
                        return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Duplicate namespace specification, refusing.");

                n |= data.type;
        }

        if (!FLAGS_SET(n, CLONE_NEWNS))
                return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
                                "Containers without file system namespace aren't supported.");

        s->private_network = FLAGS_SET(n, CLONE_NEWNET);
        s->userns_mode = FLAGS_SET(n, CLONE_NEWUSER) ? USER_NAMESPACE_FIXED : USER_NAMESPACE_NO;
        s->use_cgns = FLAGS_SET(n, CLONE_NEWCGROUP);

        s->clone_ns_flags = n & (CLONE_NEWIPC|CLONE_NEWPID|CLONE_NEWUTS);

        return 0;
}

static int oci_uid_gid_range(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        uid_t *uid = userdata, u;
        uintmax_t k;

        assert(uid);
        assert_cc(sizeof(uid_t) == sizeof(gid_t));

        /* This is very much like oci_uid_gid(), except the checks are a bit different, as this is a UID range rather
         * than a specific UID, and hence UID_INVALID has no special significance. OTOH a range of zero makes no
         * sense. */

        k = json_variant_unsigned(v);
        u = (uid_t) k;
        if ((uintmax_t) u != k)
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
                                "UID/GID out of range: %ji", k);
        if (u == 0)
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
                                "UID/GID range can't be zero.");

        *uid = u;
        return 0;
}

static int oci_uid_gid_mappings(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        struct mapping_data {
                uid_t host_id;
                uid_t container_id;
                uid_t range;
        } data = {
                .host_id = UID_INVALID,
                .container_id = UID_INVALID,
                .range = 0,
        };

        static const JsonDispatch table[] = {
                { "containerID", JSON_VARIANT_UNSIGNED, oci_uid_gid,       offsetof(struct mapping_data, container_id), JSON_MANDATORY },
                { "hostID",      JSON_VARIANT_UNSIGNED, oci_uid_gid,       offsetof(struct mapping_data, host_id),      JSON_MANDATORY },
                { "size",        JSON_VARIANT_UNSIGNED, oci_uid_gid_range, offsetof(struct mapping_data, range),        JSON_MANDATORY },
                {}
        };

        Settings *s = userdata;
        JsonVariant *e;
        int r;

        assert(s);

        if (json_variant_elements(v) == 0)
                return 0;

        if (json_variant_elements(v) > 1)
                return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
                                "UID/GID mappings with more than one entry are not supported.");

        assert_se(e = json_variant_by_index(v, 0));

        r = json_dispatch(e, table, oci_unexpected, flags, &data);
        if (r < 0)
                return r;

        if (data.host_id + data.range < data.host_id ||
            data.container_id + data.range < data.container_id)
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                "UID/GID range goes beyond UID/GID validity range, refusing.");

        if (data.container_id != 0)
                return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
                                "UID/GID mappings with a non-zero container base are not supported.");

        if (data.range < 0x10000)
                json_log(v, flags|JSON_WARNING, 0,
                         "UID/GID mapping with less than 65536 UID/GIDS set up, you are looking for trouble.");

        if (s->uid_range != UID_INVALID &&
            (s->uid_shift != data.host_id || s->uid_range != data.range))
                return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
                                "Non-matching UID and GID mappings are not supported.");

        s->uid_shift = data.host_id;
        s->uid_range = data.range;

        return 0;
}

static int oci_device_type(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        mode_t *mode = userdata;
        const char *t;

        assert(mode);
        assert_se(t = json_variant_string(v));

        if (STR_IN_SET(t, "c", "u"))
                *mode = (*mode & ~S_IFMT) | S_IFCHR;
        else if (streq(t, "b"))
                *mode = (*mode & ~S_IFMT) | S_IFBLK;
        else if (streq(t, "p"))
                *mode = (*mode & ~S_IFMT) | S_IFIFO;
        else
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                "Unknown device type: %s", t);

        return 0;
}

static int oci_device_major(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        unsigned *u = userdata;
        uintmax_t k;

        assert_se(u);

        k = json_variant_unsigned(v);
        if (!DEVICE_MAJOR_VALID(k))
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
                                "Device major %ji out of range.", k);

        *u = (unsigned) k;
        return 0;
}

static int oci_device_minor(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        unsigned *u = userdata;
        uintmax_t k;

        assert_se(u);

        k = json_variant_unsigned(v);
        if (!DEVICE_MINOR_VALID(k))
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
                                "Device minor %ji out of range.", k);

        *u = (unsigned) k;
        return 0;
}

static int oci_device_file_mode(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        mode_t *mode = userdata, m;
        uintmax_t k;

        assert(mode);

        k = json_variant_unsigned(v);
        m = (mode_t) k;

        if ((m & ~07777) != 0 || (uintmax_t) m != k)
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
                                "fileMode out of range, refusing.");

        *mode = m;
        return 0;
}

static int oci_devices(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        Settings *s = userdata;
        JsonVariant *e;
        int r;

        assert(s);

        JSON_VARIANT_ARRAY_FOREACH(e, v) {

                static const JsonDispatch table[] = {
                        { "type",     JSON_VARIANT_STRING,   oci_device_type,      offsetof(DeviceNode, mode),  JSON_MANDATORY },
                        { "path",     JSON_VARIANT_STRING,   oci_absolute_path,    offsetof(DeviceNode, path),  JSON_MANDATORY },
                        { "major",    JSON_VARIANT_UNSIGNED, oci_device_major,     offsetof(DeviceNode, major), 0              },
                        { "minor",    JSON_VARIANT_UNSIGNED, oci_device_minor,     offsetof(DeviceNode, minor), 0              },
                        { "fileMode", JSON_VARIANT_UNSIGNED, oci_device_file_mode, offsetof(DeviceNode, mode),  0              },
                        { "uid",      JSON_VARIANT_UNSIGNED, oci_uid_gid,          offsetof(DeviceNode, uid),   0              },
                        { "gid",      JSON_VARIANT_UNSIGNED, oci_uid_gid,          offsetof(DeviceNode, gid),   0              },
                        {}
                };

                DeviceNode *node, *nodes;

                nodes = reallocarray(s->extra_nodes, s->n_extra_nodes + 1, sizeof(DeviceNode));
                if (!nodes)
                        return log_oom();

                s->extra_nodes = nodes;

                node = nodes + s->n_extra_nodes;
                *node = (DeviceNode) {
                        .uid = UID_INVALID,
                        .gid = GID_INVALID,
                        .major = UINT_MAX,
                        .minor = UINT_MAX,
                        .mode = 0644,
                };

                r = json_dispatch(e, table, oci_unexpected, flags, node);
                if (r < 0)
                        goto fail_element;

                if (S_ISCHR(node->mode) || S_ISBLK(node->mode)) {
                        _cleanup_free_ char *path = NULL;

                        if (node->major == UINT_MAX || node->minor == UINT_MAX) {
                                r = json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
                                             "Major/minor required when device node is device node");
                                goto fail_element;
                        }

                        /* Suppress a couple of implicit device nodes */
                        r = device_path_make_canonical(node->mode, makedev(node->major, node->minor), &path);
                        if (r < 0)
                                json_log(e, flags|JSON_DEBUG, 0, "Failed to resolve device node %u:%u, ignoring: %m", node->major, node->minor);
                        else {
                                if (PATH_IN_SET(path,
                                                "/dev/null",
                                                "/dev/zero",
                                                "/dev/full",
                                                "/dev/random",
                                                "/dev/urandom",
                                                "/dev/tty",
                                                "/dev/net/tun",
                                                "/dev/ptmx",
                                                "/dev/pts/ptmx",
                                                "/dev/console")) {

                                        json_log(e, flags|JSON_DEBUG, 0, "Ignoring devices item for device '%s', as it is implicitly created anyway.", path);
                                        free(node->path);
                                        continue;
                                }
                        }
                }

                s->n_extra_nodes++;
                continue;

        fail_element:
                free(node->path);
                return r;
        }

        return 0;
}

static int oci_cgroups_path(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        _cleanup_free_ char *slice = NULL, *backwards = NULL;
        Settings *s = userdata;
        const char *p;
        int r;

        assert(s);

        assert_se(p = json_variant_string(v));

        r = cg_path_get_slice(p, &slice);
        if (r < 0)
                return json_log(v, flags, r, "Couldn't derive slice unit name from path '%s': %m", p);

        r = cg_slice_to_path(slice, &backwards);
        if (r < 0)
                return json_log(v, flags, r, "Couldn't convert slice unit name '%s' back to path: %m", slice);

        if (!path_equal(backwards, p))
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                "Control group path '%s' does not refer to slice unit, refusing.", p);

        free_and_replace(s->slice, slice);
        return 0;
}

static int oci_cgroup_device_type(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        mode_t *mode = userdata;
        const char *n;

        assert_se(n = json_variant_string(v));

        if (streq(n, "c"))
                *mode = S_IFCHR;
        else if (streq(n, "b"))
                *mode = S_IFBLK;
        else
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                "Control group device type unknown: %s", n);

        return 0;
}

struct device_data {
        bool allow;
        bool r;
        bool w;
        bool m;
        mode_t type;
        unsigned major;
        unsigned minor;
};

static int oci_cgroup_device_access(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        struct device_data *d = userdata;
        bool r = false, w = false, m = false;
        const char *s;
        size_t i;

        assert_se(s = json_variant_string(v));

        for (i = 0; s[i]; i++)
                if (s[i] == 'r')
                        r = true;
                else if (s[i] == 'w')
                        w = true;
                else if (s[i] == 'm')
                        m = true;
                else
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Unknown device access character '%c'.", s[i]);

        d->r = r;
        d->w = w;
        d->m = m;

        return 0;
}

static int oci_cgroup_devices(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {

        _cleanup_free_ struct device_data *list = NULL;
        Settings *s = userdata;
        size_t n_list = 0, i;
        bool noop = false;
        JsonVariant *e;
        int r;

        assert(s);

        JSON_VARIANT_ARRAY_FOREACH(e, v) {

                struct device_data data = {
                        .major = UINT_MAX,
                        .minor = UINT_MAX,
                }, *a;

                static const JsonDispatch table[] = {
                        { "allow",  JSON_VARIANT_BOOLEAN,  json_dispatch_boolean,    offsetof(struct device_data, allow), JSON_MANDATORY },
                        { "type",   JSON_VARIANT_STRING,   oci_cgroup_device_type,   offsetof(struct device_data, type),  0              },
                        { "major",  JSON_VARIANT_UNSIGNED, oci_device_major,         offsetof(struct device_data, major), 0              },
                        { "minor",  JSON_VARIANT_UNSIGNED, oci_device_minor,         offsetof(struct device_data, minor), 0              },
                        { "access", JSON_VARIANT_STRING,   oci_cgroup_device_access, 0,                                   0              },
                        {}
                };

                r = json_dispatch(e, table, oci_unexpected, flags, &data);
                if (r < 0)
                        return r;

                if (!data.allow) {
                        /* The fact that OCI allows 'deny' entries makes really no sense, as 'allow'
                         * vs. 'deny' for the devices cgroup controller is really not about allow-listing and
                         * deny-listing but about adding and removing entries from the allow list. Since we
                         * always start out with an empty allow list we hence ignore the whole thing, as
                         * removing entries which don't exist make no sense. We'll log about this, since this
                         * is really borked in the spec, with one exception: the entry that's supposed to
                         * drop the kernel's default we ignore silently */

                        if (!data.r || !data.w || !data.m || data.type != 0 || data.major != UINT_MAX || data.minor != UINT_MAX)
                                json_log(v, flags|JSON_WARNING, 0, "Devices cgroup allow list with arbitrary 'allow' entries not supported, ignoring.");

                        /* We ignore the 'deny' entry as for us that's implied */
                        continue;
                }

                if (!data.r && !data.w && !data.m) {
                        json_log(v, flags|LOG_WARNING, 0, "Device cgroup allow list entry with no effect found, ignoring.");
                        continue;
                }

                if (data.minor != UINT_MAX && data.major == UINT_MAX)
                        return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
                                        "Device cgroup allow list entries with minors but no majors not supported.");

                if (data.major != UINT_MAX && data.type == 0)
                        return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
                                        "Device cgroup allow list entries with majors but no device node type not supported.");

                if (data.type == 0) {
                        if (data.r && data.w && data.m) /* a catchall allow list entry means we are looking at a noop */
                                noop = true;
                        else
                                return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP),
                                                "Device cgroup allow list entries with no type not supported.");
                }

                a = reallocarray(list, n_list + 1, sizeof(struct device_data));
                if (!a)
                        return log_oom();

                list = a;
                list[n_list++] = data;
        }

        if (noop)
                return 0;

        r = settings_allocate_properties(s);
        if (r < 0)
                return r;

        r = sd_bus_message_open_container(s->properties, 'r', "sv");
        if (r < 0)
                return bus_log_create_error(r);

        r = sd_bus_message_append(s->properties, "s", "DeviceAllow");
        if (r < 0)
                return bus_log_create_error(r);

        r = sd_bus_message_open_container(s->properties, 'v', "a(ss)");
        if (r < 0)
                return bus_log_create_error(r);

        r = sd_bus_message_open_container(s->properties, 'a', "(ss)");
        if (r < 0)
                return bus_log_create_error(r);

        for (i = 0; i < n_list; i++) {
                _cleanup_free_ char *pattern = NULL;
                char access[4];
                size_t n = 0;

                if (list[i].minor == UINT_MAX) {
                        const char *t;

                        if (list[i].type == S_IFBLK)
                                t = "block";
                        else {
                                assert(list[i].type == S_IFCHR);
                                t = "char";
                        }

                        if (list[i].major == UINT_MAX) {
                                pattern = strjoin(t, "-*");
                                if (!pattern)
                                        return log_oom();
                        } else {
                                if (asprintf(&pattern, "%s-%u", t, list[i].major) < 0)
                                        return log_oom();
                        }

                } else {
                        assert(list[i].major != UINT_MAX); /* If a minor is specified, then a major also needs to be specified */

                        r = device_path_make_major_minor(list[i].type, makedev(list[i].major, list[i].minor), &pattern);
                        if (r < 0)
                                return log_oom();
                }

                if (list[i].r)
                        access[n++] = 'r';
                if (list[i].w)
                        access[n++] = 'w';
                if (list[i].m)
                        access[n++] = 'm';
                access[n] = 0;

                assert(n > 0);

                r = sd_bus_message_append(s->properties, "(ss)", pattern, access);
                if (r < 0)
                        return bus_log_create_error(r);
        }

        r = sd_bus_message_close_container(s->properties);
        if (r < 0)
                return bus_log_create_error(r);

        r = sd_bus_message_close_container(s->properties);
        if (r < 0)
                return bus_log_create_error(r);

        r = sd_bus_message_close_container(s->properties);
        if (r < 0)
                return bus_log_create_error(r);

        return 0;
}

static int oci_cgroup_memory_limit(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        uint64_t *m = userdata;
        uintmax_t k;

        assert(m);

        if (json_variant_is_negative(v)) {
                *m = UINT64_MAX;
                return 0;
        }

        if (!json_variant_is_unsigned(v))
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                "Memory limit is not an unsigned integer");

        k = json_variant_unsigned(v);
        if (k >= UINT64_MAX)
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
                                "Memory limit too large: %ji", k);

        *m = (uint64_t) k;
        return 0;
}

static int oci_cgroup_memory(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {

        struct memory_data {
                uint64_t limit;
                uint64_t reservation;
                uint64_t swap;
        } data = {
                .limit = UINT64_MAX,
                .reservation = UINT64_MAX,
                .swap = UINT64_MAX,
        };

        static const JsonDispatch table[] = {
                { "limit",            JSON_VARIANT_NUMBER, oci_cgroup_memory_limit, offsetof(struct memory_data, limit),       0               },
                { "reservation",      JSON_VARIANT_NUMBER, oci_cgroup_memory_limit, offsetof(struct memory_data, reservation), 0               },
                { "swap",             JSON_VARIANT_NUMBER, oci_cgroup_memory_limit, offsetof(struct memory_data, swap),        0               },
                { "kernel",           JSON_VARIANT_NUMBER, oci_unsupported,         0,                                         JSON_PERMISSIVE },
                { "kernelTCP",        JSON_VARIANT_NUMBER, oci_unsupported,         0,                                         JSON_PERMISSIVE },
                { "swapiness",        JSON_VARIANT_NUMBER, oci_unsupported,         0,                                         JSON_PERMISSIVE },
                { "disableOOMKiller", JSON_VARIANT_NUMBER, oci_unsupported,         0,                                         JSON_PERMISSIVE },
                {}
        };

        Settings *s = userdata;
        int r;

        r = json_dispatch(v, table, oci_unexpected, flags, &data);
        if (r < 0)
                return r;

        if (data.swap != UINT64_MAX) {
                if (data.limit == UINT64_MAX)
                        json_log(v, flags|LOG_WARNING, 0, "swap limit without memory limit is not supported, ignoring.");
                else if (data.swap < data.limit)
                        json_log(v, flags|LOG_WARNING, 0, "swap limit is below memory limit, ignoring.");
                else {
                        r = settings_allocate_properties(s);
                        if (r < 0)
                                return r;

                        r = sd_bus_message_append(s->properties, "(sv)", "MemorySwapMax", "t", data.swap - data.limit);
                        if (r < 0)
                                return bus_log_create_error(r);
                }
        }

        if (data.limit != UINT64_MAX) {
                r = settings_allocate_properties(s);
                if (r < 0)
                        return r;

                r = sd_bus_message_append(s->properties, "(sv)", "MemoryMax", "t", data.limit);
                if (r < 0)
                        return bus_log_create_error(r);
        }

        if (data.reservation != UINT64_MAX) {
                r = settings_allocate_properties(s);
                if (r < 0)
                        return r;

                r = sd_bus_message_append(s->properties, "(sv)", "MemoryLow", "t", data.reservation);
                if (r < 0)
                        return bus_log_create_error(r);
        }

        return 0;
}

struct cpu_data {
        uint64_t shares;
        uint64_t quota;
        uint64_t period;
        CPUSet cpu_set;
};

static int oci_cgroup_cpu_shares(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        uint64_t *u = userdata;
        uintmax_t k;

        assert(u);

        k = json_variant_unsigned(v);
        if (k < CGROUP_CPU_SHARES_MIN || k > CGROUP_CPU_SHARES_MAX)
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
                                "shares value out of range.");

        *u = (uint64_t) k;
        return 0;
}

static int oci_cgroup_cpu_quota(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        uint64_t *u = userdata;
        uintmax_t k;

        assert(u);

        k = json_variant_unsigned(v);
        if (k <= 0 || k >= UINT64_MAX)
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
                                "period/quota value out of range.");

        *u = (uint64_t) k;
        return 0;
}

static int oci_cgroup_cpu_cpus(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        struct cpu_data *data = userdata;
        CPUSet set;
        const char *n;
        int r;

        assert(data);

        assert_se(n = json_variant_string(v));

        r = parse_cpu_set(n, &set);
        if (r < 0)
                return json_log(v, flags, r, "Failed to parse CPU set specification: %s", n);

        cpu_set_reset(&data->cpu_set);
        data->cpu_set = set;

        return 0;
}

static int oci_cgroup_cpu(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {

        static const JsonDispatch table[] = {
                { "shares",          JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_shares, offsetof(struct cpu_data, shares), 0 },
                { "quota",           JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_quota,  offsetof(struct cpu_data, quota),  0 },
                { "period",          JSON_VARIANT_UNSIGNED, oci_cgroup_cpu_quota,  offsetof(struct cpu_data, period), 0 },
                { "realtimeRuntime", JSON_VARIANT_UNSIGNED, oci_unsupported,       0,                                 0 },
                { "realtimePeriod",  JSON_VARIANT_UNSIGNED, oci_unsupported,       0,                                 0 },
                { "cpus",            JSON_VARIANT_STRING,   oci_cgroup_cpu_cpus,   0,                                 0 },
                { "mems",            JSON_VARIANT_STRING,   oci_unsupported,       0,                                 0 },
                {}
        };

        struct cpu_data data = {
                .shares = UINT64_MAX,
                .quota = UINT64_MAX,
                .period = UINT64_MAX,
        };

        Settings *s = userdata;
        int r;

        r = json_dispatch(v, table, oci_unexpected, flags, &data);
        if (r < 0) {
                cpu_set_reset(&data.cpu_set);
                return r;
        }

        cpu_set_reset(&s->cpu_set);
        s->cpu_set = data.cpu_set;

        if (data.shares != UINT64_MAX) {
                r = settings_allocate_properties(s);
                if (r < 0)
                        return r;

                r = sd_bus_message_append(s->properties, "(sv)", "CPUShares", "t", data.shares);
                if (r < 0)
                        return bus_log_create_error(r);
        }

        if (data.quota != UINT64_MAX && data.period != UINT64_MAX) {
                r = settings_allocate_properties(s);
                if (r < 0)
                        return r;

                r = sd_bus_message_append(s->properties, "(sv)", "CPUQuotaPerSecUSec", "t", (uint64_t) (data.quota * USEC_PER_SEC / data.period));
                if (r < 0)
                        return bus_log_create_error(r);

        } else if ((data.quota != UINT64_MAX) != (data.period != UINT64_MAX))
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                "CPU quota and period not used together.");

        return 0;
}

static int oci_cgroup_block_io_weight(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        Settings *s = userdata;
        uintmax_t k;
        int r;

        assert(s);

        k = json_variant_unsigned(v);
        if (k < CGROUP_BLKIO_WEIGHT_MIN || k > CGROUP_BLKIO_WEIGHT_MAX)
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
                                "Block I/O weight out of range.");

        r = settings_allocate_properties(s);
        if (r < 0)
                return r;

        r = sd_bus_message_append(s->properties, "(sv)", "BlockIOWeight", "t", (uint64_t) k);
        if (r < 0)
                return bus_log_create_error(r);

        return 0;
}

static int oci_cgroup_block_io_weight_device(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        Settings *s = userdata;
        JsonVariant *e;
        int r;

        assert(s);

        JSON_VARIANT_ARRAY_FOREACH(e, v) {
                struct device_data {
                        unsigned major;
                        unsigned minor;
                        uintmax_t weight;
                } data = {
                        .major = UINT_MAX,
                        .minor = UINT_MAX,
                        .weight = UINTMAX_MAX,
                };

                static const JsonDispatch table[] =  {
                        { "major",      JSON_VARIANT_UNSIGNED, oci_device_major,       offsetof(struct device_data, major),  JSON_MANDATORY  },
                        { "minor",      JSON_VARIANT_UNSIGNED, oci_device_minor,       offsetof(struct device_data, minor),  JSON_MANDATORY  },
                        { "weight",     JSON_VARIANT_UNSIGNED, json_dispatch_uintmax,  offsetof(struct device_data, weight), 0               },
                        { "leafWeight", JSON_VARIANT_INTEGER,  oci_unsupported,        0,                                    JSON_PERMISSIVE },
                        {}
                };

                _cleanup_free_ char *path = NULL;

                r = json_dispatch(e, table, oci_unexpected, flags, &data);
                if (r < 0)
                        return r;

                if (data.weight == UINTMAX_MAX)
                        continue;

                if (data.weight < CGROUP_BLKIO_WEIGHT_MIN || data.weight > CGROUP_BLKIO_WEIGHT_MAX)
                        return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
                                        "Block I/O device weight out of range.");

                r = device_path_make_major_minor(S_IFBLK, makedev(data.major, data.minor), &path);
                if (r < 0)
                        return json_log(v, flags, r, "Failed to build device path: %m");

                r = settings_allocate_properties(s);
                if (r < 0)
                        return r;

                r = sd_bus_message_append(s->properties, "(sv)", "BlockIODeviceWeight", "a(st)", 1, path, (uint64_t) data.weight);
                if (r < 0)
                        return bus_log_create_error(r);
        }

        return 0;
}

static int oci_cgroup_block_io_throttle(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        Settings *s = userdata;
        const char *pname;
        JsonVariant *e;
        int r;

        assert(s);

        pname = streq(name, "throttleReadBpsDevice")  ? "IOReadBandwidthMax" :
                streq(name, "throttleWriteBpsDevice") ? "IOWriteBandwidthMax" :
                streq(name, "throttleReadIOPSDevice") ? "IOReadIOPSMax" :
                                                        "IOWriteIOPSMax";

        JSON_VARIANT_ARRAY_FOREACH(e, v) {
                struct device_data {
                        unsigned major;
                        unsigned minor;
                        uintmax_t rate;
                } data = {
                        .major = UINT_MAX,
                        .minor = UINT_MAX,
                };

                static const JsonDispatch table[] = {
                        { "major", JSON_VARIANT_UNSIGNED, oci_device_major,       offsetof(struct device_data, major), JSON_MANDATORY },
                        { "minor", JSON_VARIANT_UNSIGNED, oci_device_minor,       offsetof(struct device_data, minor), JSON_MANDATORY },
                        { "rate",  JSON_VARIANT_UNSIGNED, json_dispatch_uintmax,  offsetof(struct device_data, rate),  JSON_MANDATORY },
                        {}
                };

                _cleanup_free_ char *path = NULL;

                r = json_dispatch(e, table, oci_unexpected, flags, &data);
                if (r < 0)
                        return r;

                if (data.rate >= UINT64_MAX)
                        return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
                                        "Block I/O device rate out of range.");

                r = device_path_make_major_minor(S_IFBLK, makedev(data.major, data.minor), &path);
                if (r < 0)
                        return json_log(v, flags, r, "Failed to build device path: %m");

                r = settings_allocate_properties(s);
                if (r < 0)
                        return r;

                r = sd_bus_message_append(s->properties, "(sv)", pname, "a(st)", 1, path, (uint64_t) data.rate);
                if (r < 0)
                        return bus_log_create_error(r);
        }

        return 0;
}

static int oci_cgroup_block_io(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {

        static const JsonDispatch table[] = {
                { "weight",                  JSON_VARIANT_UNSIGNED, oci_cgroup_block_io_weight,        0, 0               },
                { "leafWeight",              JSON_VARIANT_UNSIGNED, oci_unsupported,                   0, JSON_PERMISSIVE },
                { "weightDevice",            JSON_VARIANT_ARRAY,    oci_cgroup_block_io_weight_device, 0, 0               },
                { "throttleReadBpsDevice",   JSON_VARIANT_ARRAY,    oci_cgroup_block_io_throttle,      0, 0               },
                { "throttleWriteBpsDevice",  JSON_VARIANT_ARRAY,    oci_cgroup_block_io_throttle,      0, 0               },
                { "throttleReadIOPSDevice",  JSON_VARIANT_ARRAY,    oci_cgroup_block_io_throttle,      0, 0               },
                { "throttleWriteIOPSDevice", JSON_VARIANT_ARRAY,    oci_cgroup_block_io_throttle,      0, 0               },
                {}
        };

        return json_dispatch(v, table, oci_unexpected, flags, userdata);
}

static int oci_cgroup_pids(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {

        static const JsonDispatch table[] = {
                { "limit", JSON_VARIANT_NUMBER, json_dispatch_variant, 0, JSON_MANDATORY },
                {}
        };

        _cleanup_(json_variant_unrefp) JsonVariant *k = NULL;
        Settings *s = userdata;
        uint64_t m;
        int r;

        assert(s);

        r = json_dispatch(v, table, oci_unexpected, flags, &k);
        if (r < 0)
                return r;

        if (json_variant_is_negative(k))
                m = UINT64_MAX;
        else {
                if (!json_variant_is_unsigned(k))
                        return json_log(k, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "pids limit not unsigned integer, refusing.");

                m = (uint64_t) json_variant_unsigned(k);

                if ((uintmax_t) m != json_variant_unsigned(k))
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "pids limit out of range, refusing.");
        }

        r = settings_allocate_properties(s);
        if (r < 0)
                return r;

        r = sd_bus_message_append(s->properties, "(sv)", "TasksMax", "t", m);
        if (r < 0)
                return bus_log_create_error(r);

        return 0;
}

static int oci_resources(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {

        static const JsonDispatch table[] = {
                { "devices",        JSON_VARIANT_ARRAY,  oci_cgroup_devices,  0, 0 },
                { "memory",         JSON_VARIANT_OBJECT, oci_cgroup_memory,   0, 0 },
                { "cpu",            JSON_VARIANT_OBJECT, oci_cgroup_cpu,      0, 0 },
                { "blockIO",        JSON_VARIANT_OBJECT, oci_cgroup_block_io, 0, 0 },
                { "hugepageLimits", JSON_VARIANT_ARRAY,  oci_unsupported,     0, 0 },
                { "network",        JSON_VARIANT_OBJECT, oci_unsupported,     0, 0 },
                { "pids",           JSON_VARIANT_OBJECT, oci_cgroup_pids,     0, 0 },
                { "rdma",           JSON_VARIANT_OBJECT, oci_unsupported,     0, 0 },
                {}
        };

        return json_dispatch(v, table, oci_unexpected, flags, userdata);
}

static bool sysctl_key_valid(const char *s) {
        bool dot = true;

        /* Note that we are a bit stricter here than in systemd-sysctl, as that inherited semantics from the old sysctl
         * tool, which were really weird (as it swaps / and . in both ways) */

        if (isempty(s))
                return false;

        for (; *s; s++) {

                if (*s <= ' ' || *s >= 127)
                        return false;
                if (*s == '/')
                        return false;
                if (*s == '.') {

                        if (dot) /* Don't allow two dots next to each other (or at the beginning) */
                                return false;

                        dot = true;
                } else
                        dot = false;
        }

        if (dot) /* don't allow a dot at the end */
                return false;

        return true;
}

static int oci_sysctl(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        Settings *s = userdata;
        JsonVariant *w;
        const char *k;
        int r;

        assert(s);

        JSON_VARIANT_OBJECT_FOREACH(k, w, v) {
                const char *m;

                if (!json_variant_is_string(w))
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "sysctl parameter is not a string, refusing.");

                assert_se(m = json_variant_string(w));

                if (sysctl_key_valid(k))
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "sysctl key invalid, refusing: %s", k);

                r = strv_extend_strv(&s->sysctl, STRV_MAKE(k, m), false);
                if (r < 0)
                        return log_oom();
        }

        return 0;
}

#if HAVE_SECCOMP
static int oci_seccomp_action_from_string(const char *name, uint32_t *ret) {

        static const struct {
                const char *name;
                uint32_t action;
        } table[] = {
                { "SCMP_ACT_ALLOW",         SCMP_ACT_ALLOW        },
                { "SCMP_ACT_ERRNO",         SCMP_ACT_ERRNO(EPERM) }, /* the OCI spec doesn't document the error, but it appears EPERM is supposed to be used */
                { "SCMP_ACT_KILL",          SCMP_ACT_KILL         },
#ifdef SCMP_ACT_KILL_PROCESS
                { "SCMP_ACT_KILL_PROCESS",  SCMP_ACT_KILL_PROCESS },
#endif
#ifdef SCMP_ACT_KILL_THREAD
                { "SCMP_ACT_KILL_THREAD",   SCMP_ACT_KILL_THREAD  },
#endif
#ifdef SCMP_ACT_LOG
                { "SCMP_ACT_LOG",           SCMP_ACT_LOG          },
#endif
                { "SCMP_ACT_TRAP",          SCMP_ACT_TRAP         },

                /* We don't support SCMP_ACT_TRACE because that requires a tracer, and that doesn't really make sense
                 * here */
        };

        size_t i;

        for (i = 0; i < ELEMENTSOF(table); i++)
                if (streq_ptr(name, table[i].name)) {
                        *ret = table[i].action;
                        return 0;
                }

        return -EINVAL;
}

static int oci_seccomp_arch_from_string(const char *name, uint32_t *ret) {

        static const struct {
                const char *name;
                uint32_t arch;
        } table[] = {
                { "SCMP_ARCH_AARCH64",     SCMP_ARCH_AARCH64     },
                { "SCMP_ARCH_ARM",         SCMP_ARCH_ARM         },
                { "SCMP_ARCH_MIPS",        SCMP_ARCH_MIPS        },
                { "SCMP_ARCH_MIPS64",      SCMP_ARCH_MIPS64      },
                { "SCMP_ARCH_MIPS64N32",   SCMP_ARCH_MIPS64N32   },
                { "SCMP_ARCH_MIPSEL",      SCMP_ARCH_MIPSEL      },
                { "SCMP_ARCH_MIPSEL64",    SCMP_ARCH_MIPSEL64    },
                { "SCMP_ARCH_MIPSEL64N32", SCMP_ARCH_MIPSEL64N32 },
                { "SCMP_ARCH_NATIVE",      SCMP_ARCH_NATIVE      },
#ifdef SCMP_ARCH_PARISC
                { "SCMP_ARCH_PARISC",      SCMP_ARCH_PARISC      },
#endif
#ifdef SCMP_ARCH_PARISC64
                { "SCMP_ARCH_PARISC64",    SCMP_ARCH_PARISC64    },
#endif
                { "SCMP_ARCH_PPC",         SCMP_ARCH_PPC         },
                { "SCMP_ARCH_PPC64",       SCMP_ARCH_PPC64       },
                { "SCMP_ARCH_PPC64LE",     SCMP_ARCH_PPC64LE     },
#ifdef SCMP_ARCH_RISCV64
                { "SCMP_ARCH_RISCV64",     SCMP_ARCH_RISCV64     },
#endif
                { "SCMP_ARCH_S390",        SCMP_ARCH_S390        },
                { "SCMP_ARCH_S390X",       SCMP_ARCH_S390X       },
                { "SCMP_ARCH_X32",         SCMP_ARCH_X32         },
                { "SCMP_ARCH_X86",         SCMP_ARCH_X86         },
                { "SCMP_ARCH_X86_64",      SCMP_ARCH_X86_64      },
        };

        size_t i;

        for (i = 0; i < ELEMENTSOF(table); i++)
                if (streq_ptr(table[i].name, name)) {
                        *ret = table[i].arch;
                        return 0;
                }

        return -EINVAL;
}

static int oci_seccomp_compare_from_string(const char *name, enum scmp_compare *ret) {

        static const struct {
                const char *name;
                enum scmp_compare op;
        } table[] = {
                { "SCMP_CMP_NE",        SCMP_CMP_NE        },
                { "SCMP_CMP_LT",        SCMP_CMP_LT        },
                { "SCMP_CMP_LE",        SCMP_CMP_LE        },
                { "SCMP_CMP_EQ",        SCMP_CMP_EQ        },
                { "SCMP_CMP_GE",        SCMP_CMP_GE        },
                { "SCMP_CMP_GT",        SCMP_CMP_GT        },
                { "SCMP_CMP_MASKED_EQ", SCMP_CMP_MASKED_EQ },
        };

        size_t i;

        for (i = 0; i < ELEMENTSOF(table); i++)
                if (streq_ptr(table[i].name, name)) {
                        *ret = table[i].op;
                        return 0;
                }

        return -EINVAL;
}

static int oci_seccomp_archs(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        scmp_filter_ctx *sc = userdata;
        JsonVariant *e;
        int r;

        assert(sc);

        JSON_VARIANT_ARRAY_FOREACH(e, v) {
                uint32_t a;

                if (!json_variant_is_string(e))
                        return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Architecture entry is not a string");

                r = oci_seccomp_arch_from_string(json_variant_string(e), &a);
                if (r < 0)
                        return json_log(e, flags, r, "Unknown architecture: %s", json_variant_string(e));

                r = seccomp_arch_add(sc, a);
                if (r == -EEXIST)
                        continue;
                if (r < 0)
                        return json_log(e, flags, r, "Failed to add architecture to seccomp filter: %m");
        }

        return 0;
}

struct syscall_rule {
        char **names;
        uint32_t action;
        struct scmp_arg_cmp *arguments;
        size_t n_arguments;
};

static void syscall_rule_free(struct syscall_rule *rule) {
        assert(rule);

        strv_free(rule->names);
        free(rule->arguments);
};

static int oci_seccomp_action(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        uint32_t *action = userdata;
        int r;

        assert(action);

        r = oci_seccomp_action_from_string(json_variant_string(v), action);
        if (r < 0)
                return json_log(v, flags, r, "Unknown system call action '%s': %m", json_variant_string(v));

        return 0;
}

static int oci_seccomp_op(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        enum scmp_compare *op = userdata;
        int r;

        assert(op);

        r = oci_seccomp_compare_from_string(json_variant_string(v), op);
        if (r < 0)
                return json_log(v, flags, r, "Unknown seccomp operator '%s': %m", json_variant_string(v));

        return 0;
}

static int oci_seccomp_args(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        struct syscall_rule *rule = userdata;
        JsonVariant *e;
        int r;

        assert(rule);

        JSON_VARIANT_ARRAY_FOREACH(e, v) {
                static const struct JsonDispatch table[] = {
                        { "index",    JSON_VARIANT_UNSIGNED, json_dispatch_uint32, offsetof(struct scmp_arg_cmp, arg),     JSON_MANDATORY },
                        { "value",    JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct scmp_arg_cmp, datum_a), JSON_MANDATORY },
                        { "valueTwo", JSON_VARIANT_UNSIGNED, json_dispatch_uint64, offsetof(struct scmp_arg_cmp, datum_b), 0              },
                        { "op",       JSON_VARIANT_STRING,   oci_seccomp_op,       offsetof(struct scmp_arg_cmp, op),      JSON_MANDATORY },
                        {},
                };

                struct scmp_arg_cmp *a, *p;
                int expected;

                a = reallocarray(rule->arguments, rule->n_arguments + 1, sizeof(struct syscall_rule));
                if (!a)
                        return log_oom();

                rule->arguments = a;
                p = rule->arguments + rule->n_arguments;

                *p = (struct scmp_arg_cmp) {
                        .arg = 0,
                        .datum_a = 0,
                        .datum_b = 0,
                        .op = 0,
                };

                r = json_dispatch(e, table, oci_unexpected, flags, p);
                if (r < 0)
                        return r;

                expected = p->op == SCMP_CMP_MASKED_EQ ? 4 : 3;
                if (r != expected)
                        json_log(e, flags|JSON_WARNING, 0, "Wrong number of system call arguments for JSON data data, ignoring.");

                /* Note that we are a bit sloppy here and do not insist that SCMP_CMP_MASKED_EQ gets two datum values,
                 * and the other only one. That's because buildah for example by default calls things with
                 * SCMP_CMP_MASKED_EQ but only one argument. We use 0 when the value is not specified. */

                rule->n_arguments++;
        }

        return 0;
}

static int oci_seccomp_syscalls(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        scmp_filter_ctx *sc = userdata;
        JsonVariant *e;
        int r;

        assert(sc);

        JSON_VARIANT_ARRAY_FOREACH(e, v) {
                static const JsonDispatch table[] = {
                        { "names",  JSON_VARIANT_ARRAY,  json_dispatch_strv, offsetof(struct syscall_rule, names),  JSON_MANDATORY },
                        { "action", JSON_VARIANT_STRING, oci_seccomp_action, offsetof(struct syscall_rule, action), JSON_MANDATORY },
                        { "args",   JSON_VARIANT_ARRAY,  oci_seccomp_args,   0,                                     0              },
                };
                struct syscall_rule rule = {
                        .action = UINT32_MAX,
                };
                char **i;

                r = json_dispatch(e, table, oci_unexpected, flags, &rule);
                if (r < 0)
                        goto fail_rule;

                if (strv_isempty(rule.names)) {
                        json_log(e, flags, 0, "System call name list is empty.");
                        r = -EINVAL;
                        goto fail_rule;
                }

                STRV_FOREACH(i, rule.names) {
                        int nr;

                        nr = seccomp_syscall_resolve_name(*i);
                        if (nr == __NR_SCMP_ERROR) {
                                log_debug("Unknown syscall %s, skipping.", *i);
                                continue;
                        }

                        r = seccomp_rule_add_array(sc, rule.action, nr, rule.n_arguments, rule.arguments);
                        if (r < 0)
                                goto fail_rule;
                }

                syscall_rule_free(&rule);
                continue;

        fail_rule:
                syscall_rule_free(&rule);
                return r;
        }

        return 0;
}
#endif

static int oci_seccomp(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {

#if HAVE_SECCOMP
        static const JsonDispatch table[] = {
                { "defaultAction", JSON_VARIANT_STRING, NULL,                 0, JSON_MANDATORY },
                { "architectures", JSON_VARIANT_ARRAY,  oci_seccomp_archs,    0, 0              },
                { "syscalls",      JSON_VARIANT_ARRAY,  oci_seccomp_syscalls, 0, 0              },
                {}
        };

        _cleanup_(seccomp_releasep) scmp_filter_ctx sc = NULL;
        Settings *s = userdata;
        JsonVariant *def;
        uint32_t d;
        int r;

        assert(s);

        def = json_variant_by_key(v, "defaultAction");
        if (!def)
                return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "defaultAction element missing.");

        if (!json_variant_is_string(def))
                return json_log(def, flags, SYNTHETIC_ERRNO(EINVAL), "defaultAction is not a string.");

        r = oci_seccomp_action_from_string(json_variant_string(def), &d);
        if (r < 0)
                return json_log(def, flags, r, "Unknown default action: %s", json_variant_string(def));

        sc = seccomp_init(d);
        if (!sc)
                return json_log(v, flags, SYNTHETIC_ERRNO(ENOMEM), "Couldn't allocate seccomp object.");

        r = json_dispatch(v, table, oci_unexpected, flags, sc);
        if (r < 0)
                return r;

        seccomp_release(s->seccomp);
        s->seccomp = TAKE_PTR(sc);
        return 0;
#else
        return json_log(v, flags, SYNTHETIC_ERRNO(EOPNOTSUPP), "libseccomp support not enabled, can't parse seccomp object.");
#endif
}

static int oci_rootfs_propagation(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        const char *s;

        s = json_variant_string(v);

        if (streq(s, "shared"))
                return 0;

        json_log(v, flags|JSON_DEBUG, 0, "Ignoring rootfsPropagation setting '%s'.", s);
        return 0;
}

static int oci_masked_paths(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        Settings *s = userdata;
        JsonVariant *e;

        assert(s);

        JSON_VARIANT_ARRAY_FOREACH(e, v) {
                _cleanup_free_ char *destination = NULL;
                CustomMount *m;
                const char *p;

                if (!json_variant_is_string(e))
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Path is not a string, refusing.");

                assert_se(p = json_variant_string(e));

                if (!path_is_absolute(p))
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Path is not not absolute, refusing: %s", p);

                if (oci_exclude_mount(p))
                        continue;

                destination = strdup(p);
                if (!destination)
                        return log_oom();

                m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_INACCESSIBLE);
                if (!m)
                        return log_oom();

                m->destination = TAKE_PTR(destination);

                /* The spec doesn't say this, but apparently pre-existing implementations are lenient towards
                 * non-existing paths to mask. Let's hence be too. */
                m->graceful = true;
        }

        return 0;
}

static int oci_readonly_paths(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        Settings *s = userdata;
        JsonVariant *e;

        assert(s);

        JSON_VARIANT_ARRAY_FOREACH(e, v) {
                _cleanup_free_ char *source = NULL, *destination = NULL;
                CustomMount *m;
                const char *p;

                if (!json_variant_is_string(e))
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Path is not a string, refusing.");

                assert_se(p = json_variant_string(e));

                if (!path_is_absolute(p))
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Path is not not absolute, refusing: %s", p);

                if (oci_exclude_mount(p))
                        continue;

                source = strjoin("+", p);
                if (!source)
                        return log_oom();

                destination = strdup(p);
                if (!destination)
                        return log_oom();

                m = custom_mount_add(&s->custom_mounts, &s->n_custom_mounts, CUSTOM_MOUNT_BIND);
                if (!m)
                        return log_oom();

                m->source = TAKE_PTR(source);
                m->destination = TAKE_PTR(destination);
                m->read_only = true;
        }

        return 0;
}

static int oci_linux(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {

        static const JsonDispatch table[] = {
                { "namespaces",        JSON_VARIANT_ARRAY,  oci_namespaces,         0, 0               },
                { "uidMappings",       JSON_VARIANT_ARRAY,  oci_uid_gid_mappings,   0, 0               },
                { "gidMappings",       JSON_VARIANT_ARRAY,  oci_uid_gid_mappings,   0, 0               },
                { "devices",           JSON_VARIANT_ARRAY,  oci_devices,            0, 0               },
                { "cgroupsPath",       JSON_VARIANT_STRING, oci_cgroups_path,       0, 0               },
                { "resources",         JSON_VARIANT_OBJECT, oci_resources,          0, 0               },
                { "intelRdt",          JSON_VARIANT_OBJECT, oci_unsupported,        0, JSON_PERMISSIVE },
                { "sysctl",            JSON_VARIANT_OBJECT, oci_sysctl,             0, 0               },
                { "seccomp",           JSON_VARIANT_OBJECT, oci_seccomp,            0, 0               },
                { "rootfsPropagation", JSON_VARIANT_STRING, oci_rootfs_propagation, 0, 0               },
                { "maskedPaths",       JSON_VARIANT_ARRAY,  oci_masked_paths,       0, 0               },
                { "readonlyPaths",     JSON_VARIANT_ARRAY,  oci_readonly_paths,     0, 0               },
                { "mountLabel",        JSON_VARIANT_STRING, oci_unsupported,        0, JSON_PERMISSIVE },
                {}
        };

        return json_dispatch(v, table, oci_unexpected, flags, userdata);
}

static int oci_hook_timeout(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        usec_t *u = userdata;
        uintmax_t k;

        k = json_variant_unsigned(v);
        if (k == 0 || k > (UINT64_MAX-1)/USEC_PER_SEC)
                return json_log(v, flags, SYNTHETIC_ERRNO(ERANGE),
                                "Hook timeout value out of range.");

        *u = k * USEC_PER_SEC;
        return 0;
}

static int oci_hooks_array(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        Settings *s = userdata;
        JsonVariant *e;
        int r;

        assert(s);

        JSON_VARIANT_ARRAY_FOREACH(e, v) {

                static const JsonDispatch table[] = {
                        { "path",    JSON_VARIANT_STRING,   oci_absolute_path, offsetof(OciHook, path),    JSON_MANDATORY },
                        { "args",    JSON_VARIANT_ARRAY,    oci_args,          offsetof(OciHook, args),    0              },
                        { "env",     JSON_VARIANT_ARRAY,    oci_env,           offsetof(OciHook, env),     0              },
                        { "timeout", JSON_VARIANT_UNSIGNED, oci_hook_timeout,  offsetof(OciHook, timeout), 0              },
                        {}
                };

                OciHook *a, **array, *new_item;
                size_t *n_array;

                if (streq(name, "prestart")) {
                        array = &s->oci_hooks_prestart;
                        n_array = &s->n_oci_hooks_prestart;
                } else if (streq(name, "poststart")) {
                        array = &s->oci_hooks_poststart;
                        n_array = &s->n_oci_hooks_poststart;
                } else {
                        assert(streq(name, "poststop"));
                        array = &s->oci_hooks_poststop;
                        n_array = &s->n_oci_hooks_poststop;
                }

                a = reallocarray(*array, *n_array + 1, sizeof(OciHook));
                if (!a)
                        return log_oom();

                *array = a;
                new_item = a + *n_array;

                *new_item = (OciHook) {
                        .timeout = USEC_INFINITY,
                };

                r = json_dispatch(e, table, oci_unexpected, flags, userdata);
                if (r < 0) {
                        free(new_item->path);
                        strv_free(new_item->args);
                        strv_free(new_item->env);
                        return r;
                }

                (*n_array) ++;
        }

        return 0;
}

static int oci_hooks(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {

        static const JsonDispatch table[] = {
                { "prestart",  JSON_VARIANT_OBJECT, oci_hooks_array, 0, 0 },
                { "poststart", JSON_VARIANT_OBJECT, oci_hooks_array, 0, 0 },
                { "poststop",  JSON_VARIANT_OBJECT, oci_hooks_array, 0, 0 },
                {}
        };

        return json_dispatch(v, table, oci_unexpected, flags, userdata);
}

static int oci_annotations(const char *name, JsonVariant *v, JsonDispatchFlags flags, void *userdata) {
        JsonVariant *w;
        const char *k;

        JSON_VARIANT_OBJECT_FOREACH(k, w, v) {

                if (isempty(k))
                        return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Annotation with empty key, refusing.");

                if (!json_variant_is_string(w))
                        return json_log(w, flags, SYNTHETIC_ERRNO(EINVAL),
                                        "Annotation has non-string value, refusing.");

                json_log(w, flags|JSON_DEBUG, 0, "Ignoring annotation '%s' with value '%s'.", k, json_variant_string(w));
        }

        return 0;
}

int oci_load(FILE *f, const char *bundle, Settings **ret) {

        static const JsonDispatch table[] = {
                { "ociVersion",  JSON_VARIANT_STRING, NULL,            0, JSON_MANDATORY },
                { "process",     JSON_VARIANT_OBJECT, oci_process,     0, 0 },
                { "root",        JSON_VARIANT_OBJECT, oci_root,        0, 0 },
                { "hostname",    JSON_VARIANT_STRING, oci_hostname,    0, 0 },
                { "mounts",      JSON_VARIANT_ARRAY,  oci_mounts,      0, 0 },
                { "linux",       JSON_VARIANT_OBJECT, oci_linux,       0, 0 },
                { "hooks",       JSON_VARIANT_OBJECT, oci_hooks,       0, 0 },
                { "annotations", JSON_VARIANT_OBJECT, oci_annotations, 0, 0 },
                {}
        };

        _cleanup_(json_variant_unrefp) JsonVariant *oci = NULL;
        _cleanup_(settings_freep) Settings *s = NULL;
        unsigned line = 0, column = 0;
        JsonVariant *v;
        const char *path;
        int r;

        assert_se(bundle);

        path = strjoina(bundle, "/config.json");

        r = json_parse_file(f, path, 0, &oci, &line, &column);
        if (r < 0) {
                if (line != 0 && column != 0)
                        return log_error_errno(r, "Failed to parse '%s' at %u:%u: %m", path, line, column);
                else
                        return log_error_errno(r, "Failed to parse '%s': %m", path);
        }

        v = json_variant_by_key(oci, "ociVersion");
        if (!v)
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "JSON file '%s' is not an OCI bundle configuration file. Refusing.",
                                       path);
        if (!streq_ptr(json_variant_string(v), "1.0.0"))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "OCI bundle version not supported: %s",
                                       strna(json_variant_string(v)));

        // {
        //         _cleanup_free_ char *formatted = NULL;
        //         assert_se(json_variant_format(oci, JSON_FORMAT_PRETTY|JSON_FORMAT_COLOR, &formatted) >= 0);
        //         fputs(formatted, stdout);
        // }

        s = settings_new();
        if (!s)
                return log_oom();

        s->start_mode = START_PID1;
        s->resolv_conf = RESOLV_CONF_OFF;
        s->link_journal = LINK_NO;
        s->timezone = TIMEZONE_OFF;

        s->bundle = strdup(bundle);
        if (!s->bundle)
                return log_oom();

        r = json_dispatch(oci, table, oci_unexpected, 0, s);
        if (r < 0)
                return r;

        if (s->properties) {
                r = sd_bus_message_seal(s->properties, 0, 0);
                if (r < 0)
                        return log_error_errno(r, "Cannot seal properties bus message: %m");
        }

        *ret = TAKE_PTR(s);
        return 0;
}