summaryrefslogtreecommitdiffstats
path: root/src/analyze/analyze-security.c
blob: 8d94fbc5d794b744fd0fec5858340edd0cdd07e0 (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <sys/utsname.h>

#include "analyze-security.h"
#include "bus-error.h"
#include "bus-map-properties.h"
#include "bus-unit-util.h"
#include "bus-util.h"
#include "env-util.h"
#include "format-table.h"
#include "in-addr-util.h"
#include "locale-util.h"
#include "macro.h"
#include "missing_capability.h"
#include "missing_sched.h"
#include "nulstr-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "pretty-print.h"
#if HAVE_SECCOMP
#  include "seccomp-util.h"
#endif
#include "set.h"
#include "stdio-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "unit-def.h"
#include "unit-name.h"

struct security_info {
        char *id;
        char *type;
        char *load_state;
        char *fragment_path;
        bool default_dependencies;

        uint64_t ambient_capabilities;
        uint64_t capability_bounding_set;

        char *user;
        char **supplementary_groups;
        bool dynamic_user;

        bool ip_address_deny_all;
        bool ip_address_allow_localhost;
        bool ip_address_allow_other;

        bool ip_filters_custom_ingress;
        bool ip_filters_custom_egress;

        char *keyring_mode;
        char *protect_proc;
        char *proc_subset;
        bool lock_personality;
        bool memory_deny_write_execute;
        bool no_new_privileges;
        char *notify_access;
        bool protect_hostname;

        bool private_devices;
        bool private_mounts;
        bool private_network;
        bool private_tmp;
        bool private_users;

        bool protect_control_groups;
        bool protect_kernel_modules;
        bool protect_kernel_tunables;
        bool protect_kernel_logs;
        bool protect_clock;

        char *protect_home;
        char *protect_system;

        bool remove_ipc;

        bool restrict_address_family_inet;
        bool restrict_address_family_unix;
        bool restrict_address_family_netlink;
        bool restrict_address_family_packet;
        bool restrict_address_family_other;

        uint64_t restrict_namespaces;
        bool restrict_realtime;
        bool restrict_suid_sgid;

        char *root_directory;
        char *root_image;

        bool delegate;
        char *device_policy;
        bool device_allow_non_empty;

        char **system_call_architectures;

        bool system_call_filter_allow_list;
        Set *system_call_filter;

        uint32_t _umask;
};

struct security_assessor {
        const char *id;
        const char *description_good;
        const char *description_bad;
        const char *description_na;
        const char *url;
        uint64_t weight;
        uint64_t range;
        int (*assess)(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description);
        size_t offset;
        uint64_t parameter;
        bool default_dependencies_only;
};

static void security_info_free(struct security_info *i) {
        if (!i)
                return;

        free(i->id);
        free(i->type);
        free(i->load_state);
        free(i->fragment_path);

        free(i->user);

        free(i->protect_home);
        free(i->protect_system);

        free(i->root_directory);
        free(i->root_image);

        free(i->keyring_mode);
        free(i->protect_proc);
        free(i->proc_subset);
        free(i->notify_access);

        free(i->device_policy);

        strv_free(i->supplementary_groups);
        strv_free(i->system_call_architectures);

        set_free(i->system_call_filter);
}

static bool security_info_runs_privileged(const struct security_info *i)  {
        assert(i);

        if (STRPTR_IN_SET(i->user, "0", "root"))
                return true;

        if (i->dynamic_user)
                return false;

        return isempty(i->user);
}

static int assess_bool(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        const bool *b = data;

        assert(b);
        assert(ret_badness);
        assert(ret_description);

        *ret_badness = a->parameter ? *b : !*b;
        *ret_description = NULL;

        return 0;
}

static int assess_user(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        _cleanup_free_ char *d = NULL;
        uint64_t b;

        assert(ret_badness);
        assert(ret_description);

        if (streq_ptr(info->user, NOBODY_USER_NAME)) {
                d = strdup("Service runs under as '" NOBODY_USER_NAME "' user, which should not be used for services");
                b = 9;
        } else if (info->dynamic_user && !STR_IN_SET(info->user, "0", "root")) {
                d = strdup("Service runs under a transient non-root user identity");
                b = 0;
        } else if (info->user && !STR_IN_SET(info->user, "0", "root", "")) {
                d = strdup("Service runs under a static non-root user identity");
                b = 0;
        } else {
                *ret_badness = 10;
                *ret_description = NULL;
                return 0;
        }

        if (!d)
                return log_oom();

        *ret_badness = b;
        *ret_description = TAKE_PTR(d);

        return 0;
}

static int assess_protect_home(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        const char *description;
        uint64_t badness;
        char *copy;
        int r;

        assert(ret_badness);
        assert(ret_description);

        badness = 10;
        description = "Service has full access to home directories";

        r = parse_boolean(info->protect_home);
        if (r < 0) {
                if (streq_ptr(info->protect_home, "read-only")) {
                        badness = 5;
                        description = "Service has read-only access to home directories";
                } else if (streq_ptr(info->protect_home, "tmpfs")) {
                        badness = 1;
                        description = "Service has access to fake empty home directories";
                }
        } else if (r > 0) {
                badness = 0;
                description = "Service has no access to home directories";
        }

        copy = strdup(description);
        if (!copy)
                return log_oom();

        *ret_badness = badness;
        *ret_description = copy;

        return 0;
}

static int assess_protect_system(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        const char *description;
        uint64_t badness;
        char *copy;
        int r;

        assert(ret_badness);
        assert(ret_description);

        badness = 10;
        description = "Service has full access to the OS file hierarchy";

        r = parse_boolean(info->protect_system);
        if (r < 0) {
                if (streq_ptr(info->protect_system, "full")) {
                        badness = 3;
                        description = "Service has very limited write access to the OS file hierarchy";
                } else if (streq_ptr(info->protect_system, "strict")) {
                        badness = 0;
                        description = "Service has strict read-only access to the OS file hierarchy";
                }
        } else if (r > 0) {
                badness = 5;
                description = "Service has limited write access to the OS file hierarchy";
        }

        copy = strdup(description);
        if (!copy)
                return log_oom();

        *ret_badness = badness;
        *ret_description = copy;

        return 0;
}

static int assess_root_directory(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        assert(ret_badness);
        assert(ret_description);

        *ret_badness =
                empty_or_root(info->root_directory) &&
                empty_or_root(info->root_image);
        *ret_description = NULL;

        return 0;
}

static int assess_capability_bounding_set(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        assert(ret_badness);
        assert(ret_description);

        *ret_badness = !!(info->capability_bounding_set & a->parameter);
        *ret_description = NULL;

        return 0;
}

static int assess_umask(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        char *copy = NULL;
        const char *d;
        uint64_t b;

        assert(ret_badness);
        assert(ret_description);

        if (!FLAGS_SET(info->_umask, 0002)) {
                d = "Files created by service are world-writable by default";
                b = 10;
        } else if (!FLAGS_SET(info->_umask, 0004)) {
                d = "Files created by service are world-readable by default";
                b = 5;
        } else if (!FLAGS_SET(info->_umask, 0020)) {
                d = "Files created by service are group-writable by default";
                b = 2;
        } else if (!FLAGS_SET(info->_umask, 0040)) {
                d = "Files created by service are group-readable by default";
                b = 1;
        } else {
                d = "Files created by service are accessible only by service's own user by default";
                b = 0;
        }

        copy = strdup(d);
        if (!copy)
                return log_oom();

        *ret_badness = b;
        *ret_description = copy;

        return 0;
}

static int assess_keyring_mode(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        assert(ret_badness);
        assert(ret_description);

        *ret_badness = !streq_ptr(info->keyring_mode, "private");
        *ret_description = NULL;

        return 0;
}

static int assess_protect_proc(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        assert(ret_badness);
        assert(ret_description);

        if (streq_ptr(info->protect_proc, "noaccess"))
                *ret_badness = 1;
        else if (STRPTR_IN_SET(info->protect_proc, "invisible", "ptraceable"))
                *ret_badness = 0;
        else
                *ret_badness = 3;

        *ret_description = NULL;

        return 0;
}

static int assess_proc_subset(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        assert(ret_badness);
        assert(ret_description);

        *ret_badness = !streq_ptr(info->proc_subset, "pid");
        *ret_description = NULL;

        return 0;
}

static int assess_notify_access(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        assert(ret_badness);
        assert(ret_description);

        *ret_badness = streq_ptr(info->notify_access, "all");
        *ret_description = NULL;

        return 0;
}

static int assess_remove_ipc(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        assert(ret_badness);
        assert(ret_description);

        if (security_info_runs_privileged(info))
                *ret_badness = UINT64_MAX;
        else
                *ret_badness = !info->remove_ipc;

        *ret_description = NULL;
        return 0;
}

static int assess_supplementary_groups(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        assert(ret_badness);
        assert(ret_description);

        if (security_info_runs_privileged(info))
                *ret_badness = UINT64_MAX;
        else
                *ret_badness = !strv_isempty(info->supplementary_groups);

        *ret_description = NULL;
        return 0;
}

static int assess_restrict_namespaces(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        assert(ret_badness);
        assert(ret_description);

        *ret_badness = !!(info->restrict_namespaces & a->parameter);
        *ret_description = NULL;

        return 0;
}

static int assess_system_call_architectures(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        char *d;
        uint64_t b;

        assert(ret_badness);
        assert(ret_description);

        if (strv_isempty(info->system_call_architectures)) {
                b = 10;
                d = strdup("Service may execute system calls with all ABIs");
        } else if (strv_equal(info->system_call_architectures, STRV_MAKE("native"))) {
                b = 0;
                d = strdup("Service may execute system calls only with native ABI");
        } else {
                b = 8;
                d = strdup("Service may execute system calls with multiple ABIs");
        }

        if (!d)
                return log_oom();

        *ret_badness = b;
        *ret_description = d;

        return 0;
}

#if HAVE_SECCOMP

static bool syscall_names_in_filter(Set *s, bool allow_list, const SyscallFilterSet *f, const char **ret_offending_syscall) {
        const char *syscall;

        NULSTR_FOREACH(syscall, f->value) {
                int id;

                if (syscall[0] == '@') {
                        const SyscallFilterSet *g;

                        assert_se(g = syscall_filter_set_find(syscall));
                        if (syscall_names_in_filter(s, allow_list, g, ret_offending_syscall))
                                return true; /* bad! */

                        continue;
                }

                /* Let's see if the system call actually exists on this platform, before complaining */
                id = seccomp_syscall_resolve_name(syscall);
                if (id < 0)
                        continue;

                if (set_contains(s, syscall) == allow_list) {
                        log_debug("Offending syscall filter item: %s", syscall);
                        if (ret_offending_syscall)
                                *ret_offending_syscall = syscall;
                        return true; /* bad! */
                }
        }

        *ret_offending_syscall = NULL;
        return false;
}

static int assess_system_call_filter(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        assert(a);
        assert(info);
        assert(ret_badness);
        assert(ret_description);

        assert(a->parameter < _SYSCALL_FILTER_SET_MAX);
        const SyscallFilterSet *f = syscall_filter_sets + a->parameter;

        char *d = NULL;
        uint64_t b;

        if (!info->system_call_filter_allow_list && set_isempty(info->system_call_filter)) {
                d = strdup("Service does not filter system calls");
                b = 10;
        } else {
                bool bad;
                const char *offender = NULL;

                log_debug("Analyzing system call filter, checking against: %s", f->name);
                bad = syscall_names_in_filter(info->system_call_filter, info->system_call_filter_allow_list, f, &offender);
                log_debug("Result: %s", bad ? "bad" : "good");

                if (info->system_call_filter_allow_list) {
                        if (bad) {
                                (void) asprintf(&d, "System call allow list defined for service, and %s is included "
                                                "(e.g. %s is allowed)",
                                                f->name, offender);
                                b = 9;
                        } else {
                                (void) asprintf(&d, "System call allow list defined for service, and %s is not included",
                                                f->name);
                                b = 0;
                        }
                } else {
                        if (bad) {
                                (void) asprintf(&d, "System call deny list defined for service, and %s is not included "
                                                "(e.g. %s is allowed)",
                                                f->name, offender);
                                b = 10;
                        } else {
                                (void) asprintf(&d, "System call deny list defined for service, and %s is included",
                                                f->name);
                                b = 0;
                        }
                }
        }

        if (!d)
                return log_oom();

        *ret_badness = b;
        *ret_description = d;

        return 0;
}

#endif

static int assess_ip_address_allow(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        char *d = NULL;
        uint64_t b;

        assert(info);
        assert(ret_badness);
        assert(ret_description);

        if (info->ip_filters_custom_ingress || info->ip_filters_custom_egress) {
                d = strdup("Service defines custom ingress/egress IP filters with BPF programs");
                b = 0;
        } else if (!info->ip_address_deny_all) {
                d = strdup("Service does not define an IP address allow list");
                b = 10;
        } else if (info->ip_address_allow_other) {
                d = strdup("Service defines IP address allow list with non-localhost entries");
                b = 5;
        } else if (info->ip_address_allow_localhost) {
                d = strdup("Service defines IP address allow list with only localhost entries");
                b = 2;
        } else {
                d = strdup("Service blocks all IP address ranges");
                b = 0;
        }

        if (!d)
                return log_oom();

        *ret_badness = b;
        *ret_description = d;

        return 0;
}

static int assess_device_allow(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        char *d = NULL;
        uint64_t b;

        assert(info);
        assert(ret_badness);
        assert(ret_description);

        if (STRPTR_IN_SET(info->device_policy, "strict", "closed")) {

                if (info->device_allow_non_empty) {
                        d = strdup("Service has a device ACL with some special devices");
                        b = 5;
                } else {
                        d = strdup("Service has a minimal device ACL");
                        b = 0;
                }
        } else {
                d = strdup("Service has no device ACL");
                b = 10;
        }

        if (!d)
                return log_oom();

        *ret_badness = b;
        *ret_description = d;

        return 0;
}

static int assess_ambient_capabilities(
                const struct security_assessor *a,
                const struct security_info *info,
                const void *data,
                uint64_t *ret_badness,
                char **ret_description) {

        assert(ret_badness);
        assert(ret_description);

        *ret_badness = info->ambient_capabilities != 0;
        *ret_description = NULL;

        return 0;
}

static const struct security_assessor security_assessor_table[] = {
        {
                .id = "User=/DynamicUser=",
                .description_bad = "Service runs as root user",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#User=",
                .weight = 2000,
                .range = 10,
                .assess = assess_user,
        },
        {
                .id = "SupplementaryGroups=",
                .description_good = "Service has no supplementary groups",
                .description_bad = "Service runs with supplementary groups",
                .description_na = "Service runs as root, option does not matter",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SupplementaryGroups=",
                .weight = 200,
                .range = 1,
                .assess = assess_supplementary_groups,
        },
        {
                .id = "PrivateDevices=",
                .description_good = "Service has no access to hardware devices",
                .description_bad = "Service potentially has access to hardware devices",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateDevices=",
                .weight = 1000,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, private_devices),
        },
        {
                .id = "PrivateMounts=",
                .description_good = "Service cannot install system mounts",
                .description_bad = "Service may install system mounts",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateMounts=",
                .weight = 1000,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, private_mounts),
        },
        {
                .id = "PrivateNetwork=",
                .description_good = "Service has no access to the host's network",
                .description_bad = "Service has access to the host's network",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateNetwork=",
                .weight = 2500,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, private_network),
        },
        {
                .id = "PrivateTmp=",
                .description_good = "Service has no access to other software's temporary files",
                .description_bad = "Service has access to other software's temporary files",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateTmp=",
                .weight = 1000,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, private_tmp),
                .default_dependencies_only = true,
        },
        {
                .id = "PrivateUsers=",
                .description_good = "Service does not have access to other users",
                .description_bad = "Service has access to other users",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#PrivateUsers=",
                .weight = 1000,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, private_users),
        },
        {
                .id = "ProtectControlGroups=",
                .description_good = "Service cannot modify the control group file system",
                .description_bad = "Service may modify the control group file system",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectControlGroups=",
                .weight = 1000,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, protect_control_groups),
        },
        {
                .id = "ProtectKernelModules=",
                .description_good = "Service cannot load or read kernel modules",
                .description_bad = "Service may load or read kernel modules",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectKernelModules=",
                .weight = 1000,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, protect_kernel_modules),
        },
        {
                .id = "ProtectKernelTunables=",
                .description_good = "Service cannot alter kernel tunables (/proc/sys, …)",
                .description_bad = "Service may alter kernel tunables",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectKernelTunables=",
                .weight = 1000,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, protect_kernel_tunables),
        },
        {
                .id = "ProtectKernelLogs=",
                .description_good = "Service cannot read from or write to the kernel log ring buffer",
                .description_bad = "Service may read from or write to the kernel log ring buffer",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectKernelLogs=",
                .weight = 1000,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, protect_kernel_logs),
        },
        {
                .id = "ProtectClock=",
                .description_good = "Service cannot write to the hardware clock or system clock",
                .description_bad = "Service may write to the hardware clock or system clock",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectClock=",
                .weight = 1000,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, protect_clock),
        },
        {
                .id = "ProtectHome=",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectHome=",
                .weight = 1000,
                .range = 10,
                .assess = assess_protect_home,
                .default_dependencies_only = true,
        },
        {
                .id = "ProtectHostname=",
                .description_good = "Service cannot change system host/domainname",
                .description_bad = "Service may change system host/domainname",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectHostname=",
                .weight = 50,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, protect_hostname),
        },
        {
                .id = "ProtectSystem=",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectSystem=",
                .weight = 1000,
                .range = 10,
                .assess = assess_protect_system,
                .default_dependencies_only = true,
        },
        {
                .id = "RootDirectory=/RootImage=",
                .description_good = "Service has its own root directory/image",
                .description_bad = "Service runs within the host's root directory",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RootDirectory=",
                .weight = 200,
                .range = 1,
                .assess = assess_root_directory,
                .default_dependencies_only = true,
        },
        {
                .id = "LockPersonality=",
                .description_good = "Service cannot change ABI personality",
                .description_bad = "Service may change ABI personality",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#LockPersonality=",
                .weight = 100,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, lock_personality),
        },
        {
                .id = "MemoryDenyWriteExecute=",
                .description_good = "Service cannot create writable executable memory mappings",
                .description_bad = "Service may create writable executable memory mappings",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#MemoryDenyWriteExecute=",
                .weight = 100,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, memory_deny_write_execute),
        },
        {
                .id = "NoNewPrivileges=",
                .description_good = "Service processes cannot acquire new privileges",
                .description_bad = "Service processes may acquire new privileges",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#NoNewPrivileges=",
                .weight = 1000,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, no_new_privileges),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_SYS_ADMIN",
                .description_good = "Service has no administrator privileges",
                .description_bad = "Service has administrator privileges",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 1500,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = UINT64_C(1) << CAP_SYS_ADMIN,
        },
        {
                .id = "CapabilityBoundingSet=~CAP_SET(UID|GID|PCAP)",
                .description_good = "Service cannot change UID/GID identities/capabilities",
                .description_bad = "Service may change UID/GID identities/capabilities",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 1500,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_SETUID)|
                             (UINT64_C(1) << CAP_SETGID)|
                             (UINT64_C(1) << CAP_SETPCAP),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_SYS_PTRACE",
                .description_good = "Service has no ptrace() debugging abilities",
                .description_bad = "Service has ptrace() debugging abilities",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 1500,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_SYS_PTRACE),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_SYS_TIME",
                .description_good = "Service processes cannot change the system clock",
                .description_bad = "Service processes may change the system clock",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 1000,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = UINT64_C(1) << CAP_SYS_TIME,
        },
        {
                .id = "CapabilityBoundingSet=~CAP_NET_ADMIN",
                .description_good = "Service has no network configuration privileges",
                .description_bad = "Service has network configuration privileges",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 1000,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_NET_ADMIN),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_SYS_RAWIO",
                .description_good = "Service has no raw I/O access",
                .description_bad = "Service has raw I/O access",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 1000,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_SYS_RAWIO),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_SYS_MODULE",
                .description_good = "Service cannot load kernel modules",
                .description_bad = "Service may load kernel modules",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 1000,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_SYS_MODULE),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_AUDIT_*",
                .description_good = "Service has no audit subsystem access",
                .description_bad = "Service has audit subsystem access",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 500,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_AUDIT_CONTROL) |
                             (UINT64_C(1) << CAP_AUDIT_READ) |
                             (UINT64_C(1) << CAP_AUDIT_WRITE),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_SYSLOG",
                .description_good = "Service has no access to kernel logging",
                .description_bad = "Service has access to kernel logging",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 500,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_SYSLOG),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_SYS_(NICE|RESOURCE)",
                .description_good = "Service has no privileges to change resource use parameters",
                .description_bad = "Service has privileges to change resource use parameters",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 500,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_SYS_NICE) |
                             (UINT64_C(1) << CAP_SYS_RESOURCE),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_MKNOD",
                .description_good = "Service cannot create device nodes",
                .description_bad = "Service may create device nodes",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 500,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_MKNOD),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_(CHOWN|FSETID|SETFCAP)",
                .description_good = "Service cannot change file ownership/access mode/capabilities",
                .description_bad = "Service may change file ownership/access mode/capabilities unrestricted",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 1000,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_CHOWN) |
                             (UINT64_C(1) << CAP_FSETID) |
                             (UINT64_C(1) << CAP_SETFCAP),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_(DAC_*|FOWNER|IPC_OWNER)",
                .description_good = "Service cannot override UNIX file/IPC permission checks",
                .description_bad = "Service may override UNIX file/IPC permission checks",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 1000,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_DAC_OVERRIDE) |
                             (UINT64_C(1) << CAP_DAC_READ_SEARCH) |
                             (UINT64_C(1) << CAP_FOWNER) |
                             (UINT64_C(1) << CAP_IPC_OWNER),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_KILL",
                .description_good = "Service cannot send UNIX signals to arbitrary processes",
                .description_bad = "Service may send UNIX signals to arbitrary processes",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 500,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_KILL),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_NET_(BIND_SERVICE|BROADCAST|RAW)",
                .description_good = "Service has no elevated networking privileges",
                .description_bad = "Service has elevated networking privileges",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 500,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_NET_BIND_SERVICE) |
                             (UINT64_C(1) << CAP_NET_BROADCAST) |
                             (UINT64_C(1) << CAP_NET_RAW),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_SYS_BOOT",
                .description_good = "Service cannot issue reboot()",
                .description_bad = "Service may issue reboot()",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 100,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_SYS_BOOT),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_MAC_*",
                .description_good = "Service cannot adjust SMACK MAC",
                .description_bad = "Service may adjust SMACK MAC",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 100,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_MAC_ADMIN)|
                             (UINT64_C(1) << CAP_MAC_OVERRIDE),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_LINUX_IMMUTABLE",
                .description_good = "Service cannot mark files immutable",
                .description_bad = "Service may mark files immutable",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 75,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_LINUX_IMMUTABLE),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_IPC_LOCK",
                .description_good = "Service cannot lock memory into RAM",
                .description_bad = "Service may lock memory into RAM",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 50,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_IPC_LOCK),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_SYS_CHROOT",
                .description_good = "Service cannot issue chroot()",
                .description_bad = "Service may issue chroot()",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 50,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_SYS_CHROOT),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_BLOCK_SUSPEND",
                .description_good = "Service cannot establish wake locks",
                .description_bad = "Service may establish wake locks",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 25,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_BLOCK_SUSPEND),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_WAKE_ALARM",
                .description_good = "Service cannot program timers that wake up the system",
                .description_bad = "Service may program timers that wake up the system",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 25,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_WAKE_ALARM),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_LEASE",
                .description_good = "Service cannot create file leases",
                .description_bad = "Service may create file leases",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 25,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_LEASE),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_SYS_TTY_CONFIG",
                .description_good = "Service cannot issue vhangup()",
                .description_bad = "Service may issue vhangup()",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 25,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_SYS_TTY_CONFIG),
        },
        {
                .id = "CapabilityBoundingSet=~CAP_SYS_PACCT",
                .description_good = "Service cannot use acct()",
                .description_bad = "Service may use acct()",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#CapabilityBoundingSet=",
                .weight = 25,
                .range = 1,
                .assess = assess_capability_bounding_set,
                .parameter = (UINT64_C(1) << CAP_SYS_PACCT),
        },
        {
                .id = "UMask=",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#UMask=",
                .weight = 100,
                .range = 10,
                .assess = assess_umask,
        },
        {
                .id = "KeyringMode=",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#KeyringMode=",
                .description_good = "Service doesn't share key material with other services",
                .description_bad = "Service shares key material with other service",
                .weight = 1000,
                .range = 1,
                .assess = assess_keyring_mode,
        },
        {
                .id = "ProtectProc=",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProtectProc=",
                .description_good = "Service has restricted access to process tree (/proc hidepid=)",
                .description_bad = "Service has full access to process tree (/proc hidepid=)",
                .weight = 1000,
                .range = 3,
                .assess = assess_protect_proc,
        },
        {
                .id = "ProcSubset=",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#ProcSubset=",
                .description_good = "Service has no access to non-process /proc files (/proc subset=)",
                .description_bad = "Service has full access to non-process /proc files (/proc subset=)",
                .weight = 10,
                .range = 1,
                .assess = assess_proc_subset,
        },
        {
                .id = "NotifyAccess=",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#NotifyAccess=",
                .description_good = "Service child processes cannot alter service state",
                .description_bad = "Service child processes may alter service state",
                .weight = 1000,
                .range = 1,
                .assess = assess_notify_access,
        },
        {
                .id = "RemoveIPC=",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RemoveIPC=",
                .description_good = "Service user cannot leave SysV IPC objects around",
                .description_bad = "Service user may leave SysV IPC objects around",
                .description_na = "Service runs as root, option does not apply",
                .weight = 100,
                .range = 1,
                .assess = assess_remove_ipc,
                .offset = offsetof(struct security_info, remove_ipc),
        },
        {
                .id = "Delegate=",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#Delegate=",
                .description_good = "Service does not maintain its own delegated control group subtree",
                .description_bad = "Service maintains its own delegated control group subtree",
                .weight = 100,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, delegate),
                .parameter = true, /* invert! */
        },
        {
                .id = "RestrictRealtime=",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictRealtime=",
                .description_good = "Service realtime scheduling access is restricted",
                .description_bad = "Service may acquire realtime scheduling",
                .weight = 500,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, restrict_realtime),
        },
        {
                .id = "RestrictSUIDSGID=",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictSUIDSGID=",
                .description_good = "SUID/SGID file creation by service is restricted",
                .description_bad = "Service may create SUID/SGID files",
                .weight = 1000,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, restrict_suid_sgid),
        },
        {
                .id = "RestrictNamespaces=~CLONE_NEWUSER",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
                .description_good = "Service cannot create user namespaces",
                .description_bad = "Service may create user namespaces",
                .weight = 1500,
                .range = 1,
                .assess = assess_restrict_namespaces,
                .parameter = CLONE_NEWUSER,
        },
        {
                .id = "RestrictNamespaces=~CLONE_NEWNS",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
                .description_good = "Service cannot create file system namespaces",
                .description_bad = "Service may create file system namespaces",
                .weight = 500,
                .range = 1,
                .assess = assess_restrict_namespaces,
                .parameter = CLONE_NEWNS,
        },
        {
                .id = "RestrictNamespaces=~CLONE_NEWIPC",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
                .description_good = "Service cannot create IPC namespaces",
                .description_bad = "Service may create IPC namespaces",
                .weight = 500,
                .range = 1,
                .assess = assess_restrict_namespaces,
                .parameter = CLONE_NEWIPC,
        },
        {
                .id = "RestrictNamespaces=~CLONE_NEWPID",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
                .description_good = "Service cannot create process namespaces",
                .description_bad = "Service may create process namespaces",
                .weight = 500,
                .range = 1,
                .assess = assess_restrict_namespaces,
                .parameter = CLONE_NEWPID,
        },
        {
                .id = "RestrictNamespaces=~CLONE_NEWCGROUP",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
                .description_good = "Service cannot create cgroup namespaces",
                .description_bad = "Service may create cgroup namespaces",
                .weight = 500,
                .range = 1,
                .assess = assess_restrict_namespaces,
                .parameter = CLONE_NEWCGROUP,
        },
        {
                .id = "RestrictNamespaces=~CLONE_NEWNET",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
                .description_good = "Service cannot create network namespaces",
                .description_bad = "Service may create network namespaces",
                .weight = 500,
                .range = 1,
                .assess = assess_restrict_namespaces,
                .parameter = CLONE_NEWNET,
        },
        {
                .id = "RestrictNamespaces=~CLONE_NEWUTS",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictNamespaces=",
                .description_good = "Service cannot create hostname namespaces",
                .description_bad = "Service may create hostname namespaces",
                .weight = 100,
                .range = 1,
                .assess = assess_restrict_namespaces,
                .parameter = CLONE_NEWUTS,
        },
        {
                .id = "RestrictAddressFamilies=~AF_(INET|INET6)",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
                .description_good = "Service cannot allocate Internet sockets",
                .description_bad = "Service may allocate Internet sockets",
                .weight = 1500,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, restrict_address_family_inet),
        },
        {
                .id = "RestrictAddressFamilies=~AF_UNIX",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
                .description_good = "Service cannot allocate local sockets",
                .description_bad = "Service may allocate local sockets",
                .weight = 25,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, restrict_address_family_unix),
        },
        {
                .id = "RestrictAddressFamilies=~AF_NETLINK",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
                .description_good = "Service cannot allocate netlink sockets",
                .description_bad = "Service may allocate netlink sockets",
                .weight = 200,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, restrict_address_family_netlink),
        },
        {
                .id = "RestrictAddressFamilies=~AF_PACKET",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
                .description_good = "Service cannot allocate packet sockets",
                .description_bad = "Service may allocate packet sockets",
                .weight = 1000,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, restrict_address_family_packet),
        },
        {
                .id = "RestrictAddressFamilies=~…",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#RestrictAddressFamilies=",
                .description_good = "Service cannot allocate exotic sockets",
                .description_bad = "Service may allocate exotic sockets",
                .weight = 1250,
                .range = 1,
                .assess = assess_bool,
                .offset = offsetof(struct security_info, restrict_address_family_other),
        },
        {
                .id = "SystemCallArchitectures=",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallArchitectures=",
                .weight = 1000,
                .range = 10,
                .assess = assess_system_call_architectures,
        },
#if HAVE_SECCOMP
        {
                .id = "SystemCallFilter=~@swap",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
                .weight = 1000,
                .range = 10,
                .assess = assess_system_call_filter,
                .parameter = SYSCALL_FILTER_SET_SWAP,
        },
        {
                .id = "SystemCallFilter=~@obsolete",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
                .weight = 250,
                .range = 10,
                .assess = assess_system_call_filter,
                .parameter = SYSCALL_FILTER_SET_OBSOLETE,
        },
        {
                .id = "SystemCallFilter=~@clock",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
                .weight = 1000,
                .range = 10,
                .assess = assess_system_call_filter,
                .parameter = SYSCALL_FILTER_SET_CLOCK,
        },
        {
                .id = "SystemCallFilter=~@cpu-emulation",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
                .weight = 250,
                .range = 10,
                .assess = assess_system_call_filter,
                .parameter = SYSCALL_FILTER_SET_CPU_EMULATION,
        },
        {
                .id = "SystemCallFilter=~@debug",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
                .weight = 1000,
                .range = 10,
                .assess = assess_system_call_filter,
                .parameter = SYSCALL_FILTER_SET_DEBUG,
        },
        {
                .id = "SystemCallFilter=~@mount",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
                .weight = 1000,
                .range = 10,
                .assess = assess_system_call_filter,
                .parameter = SYSCALL_FILTER_SET_MOUNT,
        },
        {
                .id = "SystemCallFilter=~@module",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
                .weight = 1000,
                .range = 10,
                .assess = assess_system_call_filter,
                .parameter = SYSCALL_FILTER_SET_MODULE,
        },
        {
                .id = "SystemCallFilter=~@raw-io",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
                .weight = 1000,
                .range = 10,
                .assess = assess_system_call_filter,
                .parameter = SYSCALL_FILTER_SET_RAW_IO,
        },
        {
                .id = "SystemCallFilter=~@reboot",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
                .weight = 1000,
                .range = 10,
                .assess = assess_system_call_filter,
                .parameter = SYSCALL_FILTER_SET_REBOOT,
        },
        {
                .id = "SystemCallFilter=~@privileged",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
                .weight = 700,
                .range = 10,
                .assess = assess_system_call_filter,
                .parameter = SYSCALL_FILTER_SET_PRIVILEGED,
        },
        {
                .id = "SystemCallFilter=~@resources",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#SystemCallFilter=",
                .weight = 700,
                .range = 10,
                .assess = assess_system_call_filter,
                .parameter = SYSCALL_FILTER_SET_RESOURCES,
        },
#endif
        {
                .id = "IPAddressDeny=",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#IPAddressDeny=",
                .weight = 1000,
                .range = 10,
                .assess = assess_ip_address_allow,
        },
        {
                .id = "DeviceAllow=",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#DeviceAllow=",
                .weight = 1000,
                .range = 10,
                .assess = assess_device_allow,
        },
        {
                .id = "AmbientCapabilities=",
                .url = "https://www.freedesktop.org/software/systemd/man/systemd.exec.html#AmbientCapabilities=",
                .description_good = "Service process does not receive ambient capabilities",
                .description_bad = "Service process receives ambient capabilities",
                .weight = 500,
                .range = 1,
                .assess = assess_ambient_capabilities,
        },
};

static int assess(const struct security_info *info, Table *overview_table, AnalyzeSecurityFlags flags) {
        static const struct {
                uint64_t exposure;
                const char *name;
                const char *color;
                SpecialGlyph smiley;
        } badness_table[] = {
                { 100, "DANGEROUS", ANSI_HIGHLIGHT_RED,    SPECIAL_GLYPH_DEPRESSED_SMILEY        },
                { 90,  "UNSAFE",    ANSI_HIGHLIGHT_RED,    SPECIAL_GLYPH_UNHAPPY_SMILEY          },
                { 75,  "EXPOSED",   ANSI_HIGHLIGHT_YELLOW, SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY },
                { 50,  "MEDIUM",    NULL,                  SPECIAL_GLYPH_NEUTRAL_SMILEY          },
                { 10,  "OK",        ANSI_HIGHLIGHT_GREEN,  SPECIAL_GLYPH_SLIGHTLY_HAPPY_SMILEY   },
                { 1,   "SAFE",      ANSI_HIGHLIGHT_GREEN,  SPECIAL_GLYPH_HAPPY_SMILEY            },
                { 0,   "PERFECT",   ANSI_HIGHLIGHT_GREEN,  SPECIAL_GLYPH_ECSTATIC_SMILEY         },
        };

        uint64_t badness_sum = 0, weight_sum = 0, exposure;
        _cleanup_(table_unrefp) Table *details_table = NULL;
        size_t i;
        int r;

        if (!FLAGS_SET(flags, ANALYZE_SECURITY_SHORT)) {
                details_table = table_new(" ", "name", "description", "weight", "badness", "range", "exposure");
                if (!details_table)
                        return log_oom();

                (void) table_set_sort(details_table, (size_t) 3, (size_t) 1, (size_t) -1);
                (void) table_set_reverse(details_table, 3, true);

                if (getenv_bool("SYSTEMD_ANALYZE_DEBUG") <= 0)
                        (void) table_set_display(details_table, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 6, (size_t) -1);
        }

        for (i = 0; i < ELEMENTSOF(security_assessor_table); i++) {
                const struct security_assessor *a = security_assessor_table + i;
                _cleanup_free_ char *d = NULL;
                uint64_t badness;
                void *data;

                data = (uint8_t *) info + a->offset;

                if (a->default_dependencies_only && !info->default_dependencies) {
                        badness = UINT64_MAX;
                        d = strdup("Service runs in special boot phase, option does not apply");
                        if (!d)
                                return log_oom();
                } else {
                        r = a->assess(a, info, data, &badness, &d);
                        if (r < 0)
                                return r;
                }

                assert(a->range > 0);

                if (badness != UINT64_MAX) {
                        assert(badness <= a->range);

                        badness_sum += DIV_ROUND_UP(badness * a->weight, a->range);
                        weight_sum += a->weight;
                }

                if (details_table) {
                        const char *checkmark, *description, *color = NULL;

                        if (badness == UINT64_MAX) {
                                checkmark = " ";
                                description = a->description_na;
                                color = NULL;
                        } else if (badness == a->range) {
                                checkmark = special_glyph(SPECIAL_GLYPH_CROSS_MARK);
                                description = a->description_bad;
                                color = ansi_highlight_red();
                        } else if (badness == 0) {
                                checkmark = special_glyph(SPECIAL_GLYPH_CHECK_MARK);
                                description = a->description_good;
                                color = ansi_highlight_green();
                        } else {
                                checkmark = special_glyph(SPECIAL_GLYPH_CROSS_MARK);
                                description = NULL;
                                color = ansi_highlight_red();
                        }

                        if (d)
                                description = d;

                        r = table_add_many(details_table,
                                           TABLE_STRING, checkmark,
                                           TABLE_SET_MINIMUM_WIDTH, 1,
                                           TABLE_SET_MAXIMUM_WIDTH, 1,
                                           TABLE_SET_ELLIPSIZE_PERCENT, 0,
                                           TABLE_SET_COLOR, color,
                                           TABLE_STRING, a->id, TABLE_SET_URL, a->url,
                                           TABLE_STRING, description,
                                           TABLE_UINT64, a->weight, TABLE_SET_ALIGN_PERCENT, 100,
                                           TABLE_UINT64, badness, TABLE_SET_ALIGN_PERCENT, 100,
                                           TABLE_UINT64, a->range, TABLE_SET_ALIGN_PERCENT, 100,
                                           TABLE_EMPTY, TABLE_SET_ALIGN_PERCENT, 100);
                        if (r < 0)
                                return table_log_add_error(r);
                }
        }

        assert(weight_sum > 0);

        if (details_table) {
                size_t row;

                for (row = 1; row < table_get_rows(details_table); row++) {
                        char buf[DECIMAL_STR_MAX(uint64_t) + 1 + DECIMAL_STR_MAX(uint64_t) + 1];
                        const uint64_t *weight, *badness, *range;
                        TableCell *cell;
                        uint64_t x;

                        assert_se(weight = table_get_at(details_table, row, 3));
                        assert_se(badness = table_get_at(details_table, row, 4));
                        assert_se(range = table_get_at(details_table, row, 5));

                        if (*badness == UINT64_MAX || *badness == 0)
                                continue;

                        assert_se(cell = table_get_cell(details_table, row, 6));

                        x = DIV_ROUND_UP(DIV_ROUND_UP(*badness * *weight * 100U, *range), weight_sum);
                        xsprintf(buf, "%" PRIu64 ".%" PRIu64, x / 10, x % 10);

                        r = table_update(details_table, cell, TABLE_STRING, buf);
                        if (r < 0)
                                return log_error_errno(r, "Failed to update cell in table: %m");
                }

                r = table_print(details_table, stdout);
                if (r < 0)
                        return log_error_errno(r, "Failed to output table: %m");
        }

        exposure = DIV_ROUND_UP(badness_sum * 100U, weight_sum);

        for (i = 0; i < ELEMENTSOF(badness_table); i++)
                if (exposure >= badness_table[i].exposure)
                        break;

        assert(i < ELEMENTSOF(badness_table));

        if (details_table) {
                _cleanup_free_ char *clickable = NULL;
                const char *name;

                /* If we shall output the details table, also print the brief summary underneath */

                if (info->fragment_path) {
                        r = terminal_urlify_path(info->fragment_path, info->id, &clickable);
                        if (r < 0)
                                return log_oom();

                        name = clickable;
                } else
                        name = info->id;

                printf("\n%s %sOverall exposure level for %s%s: %s%" PRIu64 ".%" PRIu64 " %s%s %s\n",
                       special_glyph(SPECIAL_GLYPH_ARROW),
                       ansi_highlight(),
                       name,
                       ansi_normal(),
                       colors_enabled() ? strempty(badness_table[i].color) : "",
                       exposure / 10, exposure % 10,
                       badness_table[i].name,
                       ansi_normal(),
                       special_glyph(badness_table[i].smiley));
        }

        fflush(stdout);

        if (overview_table) {
                char buf[DECIMAL_STR_MAX(uint64_t) + 1 + DECIMAL_STR_MAX(uint64_t) + 1];
                _cleanup_free_ char *url = NULL;

                if (info->fragment_path) {
                        r = file_url_from_path(info->fragment_path, &url);
                        if (r < 0)
                                return log_error_errno(r, "Failed to generate URL from path: %m");
                }

                xsprintf(buf, "%" PRIu64 ".%" PRIu64, exposure / 10, exposure % 10);

                r = table_add_many(overview_table,
                                   TABLE_STRING, info->id,
                                   TABLE_SET_URL, url,
                                   TABLE_STRING, buf,
                                   TABLE_SET_ALIGN_PERCENT, 100,
                                   TABLE_STRING, badness_table[i].name,
                                   TABLE_SET_COLOR, strempty(badness_table[i].color),
                                   TABLE_STRING, special_glyph(badness_table[i].smiley));
                if (r < 0)
                        return table_log_add_error(r);
        }

        return 0;
}

static int property_read_restrict_address_families(
                sd_bus *bus,
                const char *member,
                sd_bus_message *m,
                sd_bus_error *error,
                void *userdata) {

        struct security_info *info = userdata;
        int allow_list, r;

        assert(bus);
        assert(member);
        assert(m);

        r = sd_bus_message_enter_container(m, 'r', "bas");
        if (r < 0)
                return r;

        r = sd_bus_message_read(m, "b", &allow_list);
        if (r < 0)
                return r;

        info->restrict_address_family_inet =
                info->restrict_address_family_unix =
                info->restrict_address_family_netlink =
                info->restrict_address_family_packet =
                info->restrict_address_family_other = allow_list;

        r = sd_bus_message_enter_container(m, 'a', "s");
        if (r < 0)
                return r;

        for (;;) {
                const char *name;

                r = sd_bus_message_read(m, "s", &name);
                if (r < 0)
                        return r;
                if (r == 0)
                        break;

                if (STR_IN_SET(name, "AF_INET", "AF_INET6"))
                        info->restrict_address_family_inet = !allow_list;
                else if (streq(name, "AF_UNIX"))
                        info->restrict_address_family_unix = !allow_list;
                else if (streq(name, "AF_NETLINK"))
                        info->restrict_address_family_netlink = !allow_list;
                else if (streq(name, "AF_PACKET"))
                        info->restrict_address_family_packet = !allow_list;
                else
                        info->restrict_address_family_other = !allow_list;
        }

        r = sd_bus_message_exit_container(m);
        if (r < 0)
                return r;

        return sd_bus_message_exit_container(m);
}

static int property_read_system_call_filter(
                sd_bus *bus,
                const char *member,
                sd_bus_message *m,
                sd_bus_error *error,
                void *userdata) {

        struct security_info *info = userdata;
        int allow_list, r;

        assert(bus);
        assert(member);
        assert(m);

        r = sd_bus_message_enter_container(m, 'r', "bas");
        if (r < 0)
                return r;

        r = sd_bus_message_read(m, "b", &allow_list);
        if (r < 0)
                return r;

        info->system_call_filter_allow_list = allow_list;

        r = sd_bus_message_enter_container(m, 'a', "s");
        if (r < 0)
                return r;

        for (;;) {
                const char *name;

                r = sd_bus_message_read(m, "s", &name);
                if (r < 0)
                        return r;
                if (r == 0)
                        break;

                r = set_put_strdup(&info->system_call_filter, name);
                if (r < 0)
                        return r;
        }

        r = sd_bus_message_exit_container(m);
        if (r < 0)
                return r;

        return sd_bus_message_exit_container(m);
}

static int property_read_ip_address_allow(
                sd_bus *bus,
                const char *member,
                sd_bus_message *m,
                sd_bus_error *error,
                void *userdata) {

        struct security_info *info = userdata;
        bool deny_ipv4 = false, deny_ipv6 = false;
        int r;

        assert(bus);
        assert(member);
        assert(m);

        r = sd_bus_message_enter_container(m, 'a', "(iayu)");
        if (r < 0)
                return r;

        for (;;) {
                const void *data;
                size_t size;
                int32_t family;
                uint32_t prefixlen;

                r = sd_bus_message_enter_container(m, 'r', "iayu");
                if (r < 0)
                        return r;
                if (r == 0)
                        break;

                r = sd_bus_message_read(m, "i", &family);
                if (r < 0)
                        return r;

                r = sd_bus_message_read_array(m, 'y', &data, &size);
                if (r < 0)
                        return r;

                r = sd_bus_message_read(m, "u", &prefixlen);
                if (r < 0)
                        return r;

                r = sd_bus_message_exit_container(m);
                if (r < 0)
                        return r;

                if (streq(member, "IPAddressAllow")) {
                        union in_addr_union u;

                        if (family == AF_INET && size == 4 && prefixlen == 8)
                                memcpy(&u.in, data, size);
                        else if (family == AF_INET6 && size == 16 && prefixlen == 128)
                                memcpy(&u.in6, data, size);
                        else {
                                info->ip_address_allow_other = true;
                                continue;
                        }

                        if (in_addr_is_localhost(family, &u))
                                info->ip_address_allow_localhost = true;
                        else
                                info->ip_address_allow_other = true;
                } else {
                        assert(streq(member, "IPAddressDeny"));

                        if (family == AF_INET && size == 4 && prefixlen == 0)
                                deny_ipv4 = true;
                        else if (family == AF_INET6 && size == 16 && prefixlen == 0)
                                deny_ipv6 = true;
                }
        }

        info->ip_address_deny_all = deny_ipv4 && deny_ipv6;

        return sd_bus_message_exit_container(m);
}

static int property_read_ip_filters(
                sd_bus *bus,
                const char *member,
                sd_bus_message *m,
                sd_bus_error *error,
                void *userdata) {

        struct security_info *info = userdata;
        _cleanup_(strv_freep) char **l = NULL;
        int r;

        assert(bus);
        assert(member);
        assert(m);

        r = sd_bus_message_read_strv(m, &l);
        if (r < 0)
                return r;

        if (streq(member, "IPIngressFilterPath"))
                info->ip_filters_custom_ingress = !strv_isempty(l);
        else if (streq(member, "IPEgressFilterPath"))
                info->ip_filters_custom_ingress = !strv_isempty(l);

        return 0;
}

static int property_read_device_allow(
                sd_bus *bus,
                const char *member,
                sd_bus_message *m,
                sd_bus_error *error,
                void *userdata) {

        struct security_info *info = userdata;
        size_t n = 0;
        int r;

        assert(bus);
        assert(member);
        assert(m);

        r = sd_bus_message_enter_container(m, 'a', "(ss)");
        if (r < 0)
                return r;

        for (;;) {
                const char *name, *policy;

                r = sd_bus_message_read(m, "(ss)", &name, &policy);
                if (r < 0)
                        return r;
                if (r == 0)
                        break;

                n++;
        }

        info->device_allow_non_empty = n > 0;

        return sd_bus_message_exit_container(m);
}

static int acquire_security_info(sd_bus *bus, const char *name, struct security_info *info, AnalyzeSecurityFlags flags) {

        static const struct bus_properties_map security_map[] = {
                { "AmbientCapabilities",     "t",       NULL,                                    offsetof(struct security_info, ambient_capabilities)      },
                { "CapabilityBoundingSet",   "t",       NULL,                                    offsetof(struct security_info, capability_bounding_set)   },
                { "DefaultDependencies",     "b",       NULL,                                    offsetof(struct security_info, default_dependencies)      },
                { "Delegate",                "b",       NULL,                                    offsetof(struct security_info, delegate)                  },
                { "DeviceAllow",             "a(ss)",   property_read_device_allow,              0                                                         },
                { "DevicePolicy",            "s",       NULL,                                    offsetof(struct security_info, device_policy)             },
                { "DynamicUser",             "b",       NULL,                                    offsetof(struct security_info, dynamic_user)              },
                { "FragmentPath",            "s",       NULL,                                    offsetof(struct security_info, fragment_path)             },
                { "IPAddressAllow",          "a(iayu)", property_read_ip_address_allow,          0                                                         },
                { "IPAddressDeny",           "a(iayu)", property_read_ip_address_allow,          0                                                         },
                { "IPIngressFilterPath",     "as",      property_read_ip_filters,                0                                                         },
                { "IPEgressFilterPath",      "as",      property_read_ip_filters,                0                                                         },
                { "Id",                      "s",       NULL,                                    offsetof(struct security_info, id)                        },
                { "KeyringMode",             "s",       NULL,                                    offsetof(struct security_info, keyring_mode)              },
                { "ProtectProc",             "s",       NULL,                                    offsetof(struct security_info, protect_proc)              },
                { "ProcSubset",              "s",       NULL,                                    offsetof(struct security_info, proc_subset)               },
                { "LoadState",               "s",       NULL,                                    offsetof(struct security_info, load_state)                },
                { "LockPersonality",         "b",       NULL,                                    offsetof(struct security_info, lock_personality)          },
                { "MemoryDenyWriteExecute",  "b",       NULL,                                    offsetof(struct security_info, memory_deny_write_execute) },
                { "NoNewPrivileges",         "b",       NULL,                                    offsetof(struct security_info, no_new_privileges)         },
                { "NotifyAccess",            "s",       NULL,                                    offsetof(struct security_info, notify_access)             },
                { "PrivateDevices",          "b",       NULL,                                    offsetof(struct security_info, private_devices)           },
                { "PrivateMounts",           "b",       NULL,                                    offsetof(struct security_info, private_mounts)            },
                { "PrivateNetwork",          "b",       NULL,                                    offsetof(struct security_info, private_network)           },
                { "PrivateTmp",              "b",       NULL,                                    offsetof(struct security_info, private_tmp)               },
                { "PrivateUsers",            "b",       NULL,                                    offsetof(struct security_info, private_users)             },
                { "ProtectControlGroups",    "b",       NULL,                                    offsetof(struct security_info, protect_control_groups)    },
                { "ProtectHome",             "s",       NULL,                                    offsetof(struct security_info, protect_home)              },
                { "ProtectHostname",         "b",       NULL,                                    offsetof(struct security_info, protect_hostname)          },
                { "ProtectKernelModules",    "b",       NULL,                                    offsetof(struct security_info, protect_kernel_modules)    },
                { "ProtectKernelTunables",   "b",       NULL,                                    offsetof(struct security_info, protect_kernel_tunables)   },
                { "ProtectKernelLogs",       "b",       NULL,                                    offsetof(struct security_info, protect_kernel_logs)       },
                { "ProtectClock",            "b",       NULL,                                    offsetof(struct security_info, protect_clock)             },
                { "ProtectSystem",           "s",       NULL,                                    offsetof(struct security_info, protect_system)            },
                { "RemoveIPC",               "b",       NULL,                                    offsetof(struct security_info, remove_ipc)                },
                { "RestrictAddressFamilies", "(bas)",   property_read_restrict_address_families, 0                                                         },
                { "RestrictNamespaces",      "t",       NULL,                                    offsetof(struct security_info, restrict_namespaces)       },
                { "RestrictRealtime",        "b",       NULL,                                    offsetof(struct security_info, restrict_realtime)         },
                { "RestrictSUIDSGID",        "b",       NULL,                                    offsetof(struct security_info, restrict_suid_sgid)        },
                { "RootDirectory",           "s",       NULL,                                    offsetof(struct security_info, root_directory)            },
                { "RootImage",               "s",       NULL,                                    offsetof(struct security_info, root_image)                },
                { "SupplementaryGroups",     "as",      NULL,                                    offsetof(struct security_info, supplementary_groups)      },
                { "SystemCallArchitectures", "as",      NULL,                                    offsetof(struct security_info, system_call_architectures) },
                { "SystemCallFilter",        "(as)",    property_read_system_call_filter,        0                                                         },
                { "Type",                    "s",       NULL,                                    offsetof(struct security_info, type)                      },
                { "UMask",                   "u",       NULL,                                    offsetof(struct security_info, _umask)                    },
                { "User",                    "s",       NULL,                                    offsetof(struct security_info, user)                      },
                {}
        };

        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_free_ char *path = NULL;
        int r;

        /* Note: this mangles *info on failure! */

        assert(bus);
        assert(name);
        assert(info);

        path = unit_dbus_path_from_name(name);
        if (!path)
                return log_oom();

        r = bus_map_all_properties(
                        bus,
                        "org.freedesktop.systemd1",
                        path,
                        security_map,
                        BUS_MAP_STRDUP | BUS_MAP_BOOLEAN_AS_BOOL,
                        &error,
                        NULL,
                        info);
        if (r < 0)
                return log_error_errno(r, "Failed to get unit properties: %s", bus_error_message(&error, r));

        if (!streq_ptr(info->load_state, "loaded")) {

                if (FLAGS_SET(flags, ANALYZE_SECURITY_ONLY_LOADED))
                        return -EMEDIUMTYPE;

                if (streq_ptr(info->load_state, "not-found"))
                        log_error("Unit %s not found, cannot analyze.", name);
                else if (streq_ptr(info->load_state, "masked"))
                        log_error("Unit %s is masked, cannot analyze.", name);
                else
                        log_error("Unit %s not loaded properly, cannot analyze.", name);

                return -EINVAL;
        }

        if (FLAGS_SET(flags, ANALYZE_SECURITY_ONLY_LONG_RUNNING) && streq_ptr(info->type, "oneshot"))
                return -EMEDIUMTYPE;

        if (info->private_devices ||
            info->private_tmp ||
            info->protect_control_groups ||
            info->protect_kernel_tunables ||
            info->protect_kernel_modules ||
            !streq_ptr(info->protect_home, "no") ||
            !streq_ptr(info->protect_system, "no") ||
            info->root_image)
                info->private_mounts = true;

        if (info->protect_kernel_modules)
                info->capability_bounding_set &= ~(UINT64_C(1) << CAP_SYS_MODULE);

        if (info->protect_kernel_logs)
                info->capability_bounding_set &= ~(UINT64_C(1) << CAP_SYSLOG);

        if (info->protect_clock)
                info->capability_bounding_set &= ~((UINT64_C(1) << CAP_SYS_TIME) |
                                                   (UINT64_C(1) << CAP_WAKE_ALARM));

        if (info->private_devices)
                info->capability_bounding_set &= ~((UINT64_C(1) << CAP_MKNOD) |
                                                   (UINT64_C(1) << CAP_SYS_RAWIO));

        return 0;
}

static int analyze_security_one(sd_bus *bus, const char *name, Table *overview_table, AnalyzeSecurityFlags flags) {
        _cleanup_(security_info_free) struct security_info info = {
                .default_dependencies = true,
                .capability_bounding_set = UINT64_MAX,
                .restrict_namespaces = UINT64_MAX,
                ._umask = 0002,
        };
        int r;

        assert(bus);
        assert(name);

        r = acquire_security_info(bus, name, &info, flags);
        if (r == -EMEDIUMTYPE) /* Ignore this one because not loaded or Type is oneshot */
                return 0;
        if (r < 0)
                return r;

        r = assess(&info, overview_table, flags);
        if (r < 0)
                return r;

        return 0;
}

int analyze_security(sd_bus *bus, char **units, AnalyzeSecurityFlags flags) {
        _cleanup_(table_unrefp) Table *overview_table = NULL;
        int ret = 0, r;

        assert(bus);

        if (strv_length(units) != 1) {
                overview_table = table_new("unit", "exposure", "predicate", "happy");
                if (!overview_table)
                        return log_oom();
        }

        if (strv_isempty(units)) {
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
                _cleanup_strv_free_ char **list = NULL;
                size_t allocated = 0, n = 0;
                char **i;

                r = sd_bus_call_method(
                                bus,
                                "org.freedesktop.systemd1",
                                "/org/freedesktop/systemd1",
                                "org.freedesktop.systemd1.Manager",
                                "ListUnits",
                                &error,
                                &reply,
                                NULL);
                if (r < 0)
                        return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r));

                r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
                if (r < 0)
                        return bus_log_parse_error(r);

                for (;;) {
                        UnitInfo info;
                        char *copy = NULL;

                        r = bus_parse_unit_info(reply, &info);
                        if (r < 0)
                                return bus_log_parse_error(r);
                        if (r == 0)
                                break;

                        if (!endswith(info.id, ".service"))
                                continue;

                        if (!GREEDY_REALLOC(list, allocated, n + 2))
                                return log_oom();

                        copy = strdup(info.id);
                        if (!copy)
                                return log_oom();

                        list[n++] = copy;
                        list[n] = NULL;
                }

                strv_sort(list);

                flags |= ANALYZE_SECURITY_SHORT|ANALYZE_SECURITY_ONLY_LOADED|ANALYZE_SECURITY_ONLY_LONG_RUNNING;

                STRV_FOREACH(i, list) {
                        r = analyze_security_one(bus, *i, overview_table, flags);
                        if (r < 0 && ret >= 0)
                                ret = r;
                }

        } else {
                char **i;

                STRV_FOREACH(i, units) {
                        _cleanup_free_ char *mangled = NULL, *instance = NULL;
                        const char *name;

                        if (!FLAGS_SET(flags, ANALYZE_SECURITY_SHORT) && i != units) {
                                putc('\n', stdout);
                                fflush(stdout);
                        }

                        r = unit_name_mangle(*i, 0, &mangled);
                        if (r < 0)
                                return log_error_errno(r, "Failed to mangle unit name '%s': %m", *i);

                        if (!endswith(mangled, ".service"))
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                       "Unit %s is not a service unit, refusing.",
                                                       *i);

                        if (unit_name_is_valid(mangled, UNIT_NAME_TEMPLATE)) {
                                r = unit_name_replace_instance(mangled, "test-instance", &instance);
                                if (r < 0)
                                        return log_oom();

                                name = instance;
                        } else
                                name = mangled;

                        r = analyze_security_one(bus, name, overview_table, flags);
                        if (r < 0 && ret >= 0)
                                ret = r;
                }
        }

        if (overview_table) {
                if (!FLAGS_SET(flags, ANALYZE_SECURITY_SHORT)) {
                        putc('\n', stdout);
                        fflush(stdout);
                }

                r = table_print(overview_table, stdout);
                if (r < 0)
                        return log_error_errno(r, "Failed to output table: %m");
        }

        return ret;
}