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
|
#!/usr/libexec/flua
--
-- SPDX-License-Identifier: BSD-2-Clause
--
-- Copyright (c) 2024, Klara, Inc.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions
-- are met:
-- 1. Redistributions of source code must retain the above copyright
-- notice, this list of conditions and the following disclaimer.
-- 2. Redistributions in binary form must reproduce the above copyright
-- notice, this list of conditions and the following disclaimer in the
-- documentation and/or other materials provided with the distribution.
--
-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-- SUCH DAMAGE.
--
-- THEORY OF OPERATION
--
-- generate-fortify-tests.lua is intended to test fortified functions as found
-- mostly in the various headers in /usr/include/ssp. Each fortified function
-- gets three basic tests:
--
-- 1. Write just before the end of the buffer,
-- 2. Write right at the end of the buffer,
-- 3. Write just after the end of the buffer.
--
-- Each test is actually generated twice: once with a buffer on the stack, and
-- again with a buffer on the heap, to confirm that __builtin_object_size(3) can
-- deduce the buffer size in both scenarios. The tests work by setting up the
-- stack with our buffer (and some padding on either side to avoid tripping any
-- other stack or memory protection), doing any initialization as described by
-- the test definition, then calling the fortified function with the buffer as
-- outlined by the test definition.
--
-- For the 'before' and 'at' the end tests, we're ensuring that valid writes
-- that are on the verge of being invalid aren't accidentally being detected as
-- invalid.
--
-- The 'after' test is the one that actually tests the functional benefit of
-- _FORTIFY_SOURCE by violating a boundary that should trigger an abort. As
-- such, this test differs more from the other two in that it has to fork() off
-- the fortified function call so that we can monitor for a SIGABRT and
-- pass/fail the test at function end appropriately.
-- Some tests, like the FD_*() macros, may define these differently. For
-- instance, for fd sets we're varying the index we pass and not using arbitrary
-- buffers. Other tests that don't use the length in any way may physically
-- vary the buffer size for each test case when we'd typically vary the length
-- we're requesting a write for.
local includes = {
"sys/param.h",
"sys/random.h",
"sys/resource.h",
"sys/select.h",
"sys/socket.h",
"sys/time.h",
"sys/uio.h",
"sys/wait.h",
"dirent.h",
"errno.h",
"fcntl.h",
"limits.h",
"poll.h",
"signal.h",
"stdio.h",
"stdlib.h",
"string.h",
"strings.h",
"sysexits.h",
"unistd.h",
"wchar.h",
"atf-c.h",
}
local tests_added = {}
-- Some of these will need to be excluded because clang sees the wrong size when
-- an array is embedded inside a struct, we'll get something that looks more
-- like __builtin_object_size(ptr, 0) than it does the correct
-- __builtin_object_size(ptr, 1) (i.e., includes the padding after). This is
-- almost certainly a bug in llvm.
local function excludes_stack_overflow(disposition, is_heap)
return (not is_heap) and disposition > 0
end
local poll_init = [[
for (size_t i = 0; i < howmany(__bufsz, sizeof(struct pollfd)); i++) {
__stack.__buf[i].fd = -1;
}
]]
local printf_stackvars = "\tchar srcvar[__len + 10];\n"
local printf_init = [[
memset(srcvar, 'A', sizeof(srcvar) - 1);
srcvar[sizeof(srcvar) - 1] = '\0';
]]
local readv_stackvars = "\tstruct iovec iov[1];\n"
local readv_init = [[
iov[0].iov_base = __stack.__buf;
iov[0].iov_len = __len;
replace_stdin();
]]
local socket_stackvars = "\tint sock[2] = { -1, -1 };\n"
local recvfrom_sockaddr_stackvars = socket_stackvars .. [[
char data[16];
socklen_t socklen;
]]
local recvmsg_stackvars = socket_stackvars .. "\tstruct msghdr msg;\n"
local socket_init = [[
new_socket(sock);
]]
local socket_socklen_init = socket_init .. [[
socklen = __len;
]]
local stdio_init = [[
replace_stdin();
]]
local string_stackvars = "\tchar src[__len];\n"
local string_init = [[
memset(__stack.__buf, 0, __len);
memset(src, 'A', __len - 1);
src[__len - 1] = '\0';
]]
local wstring_stackvars = "\twchar_t src[__len];\n"
local wstring_init = [[
wmemset(__stack.__buf, 0, __len);
wmemset(src, 'A', __len - 1);
src[__len - 1] = '\0';
]]
-- Each test entry describes how to test a given function. We need to know how
-- to construct the buffer, we need to know the argument set we're dealing with,
-- and we need to know what we're passing to each argument. We could be passing
-- fixed values, or we could be passing the __buf under test.
--
-- definition:
-- func: name of the function under test to call
-- bufsize: size of buffer to generate, defaults to 42
-- buftype: type of buffer to generate, defaults to unsigned char[]
-- arguments: __buf, __len, or the name of a variable placed on the stack
-- exclude: a function(disposition, is_heap) that returns true if this combo
-- should be excluded.
-- stackvars: extra variables to be placed on the stack, should be a string
-- optionally formatted with tabs and newlines
-- init: extra code to inject just before the function call for initialization
-- of the buffer or any of the above-added stackvars; also a string
-- uses_len: bool-ish, necessary if arguments doesn't include either __idx or
-- or __len so that the test generator doesn't try to vary the size of the
-- buffer instead of just manipulating __idx/__len to try and induce an
-- overflow.
--
-- Most tests will just use the default bufsize/buftype, but under some
-- circumstances it's useful to use a different type (e.g., for alignment
-- requirements).
local all_tests = {
random = {
-- <sys/random.h>
{
func = "getrandom",
arguments = {
"__buf",
"__len",
"0",
},
exclude = excludes_stack_overflow,
},
},
select = {
-- <sys/select.h>
{
func = "FD_SET",
bufsize = "FD_SETSIZE",
buftype = "fd_set",
arguments = {
"__idx",
"__buf",
},
},
{
func = "FD_CLR",
bufsize = "FD_SETSIZE",
buftype = "fd_set",
arguments = {
"__idx",
"__buf",
},
},
{
func = "FD_ISSET",
bufsize = "FD_SETSIZE",
buftype = "fd_set",
arguments = {
"__idx",
"__buf",
},
},
},
socket = {
-- <sys/socket.h>
{
func = "getpeername",
buftype = "struct sockaddr",
bufsize = "sizeof(struct sockaddr)",
arguments = {
"sock[0]",
"__buf",
"&socklen",
},
exclude = excludes_stack_overflow,
stackvars = socket_stackvars .. "\tsocklen_t socklen;",
init = socket_socklen_init,
uses_len = true,
},
{
func = "getsockname",
buftype = "struct sockaddr",
bufsize = "sizeof(struct sockaddr)",
arguments = {
"sock[0]",
"__buf",
"&socklen",
},
exclude = excludes_stack_overflow,
stackvars = socket_stackvars .. "\tsocklen_t socklen;",
init = socket_socklen_init,
uses_len = true,
},
{
func = "recv",
arguments = {
"sock[0]",
"__buf",
"__len",
"0",
},
exclude = excludes_stack_overflow,
stackvars = socket_stackvars,
init = socket_init,
},
{
func = "recvfrom",
arguments = {
"sock[0]",
"__buf",
"__len",
"0",
"NULL",
"NULL",
},
exclude = excludes_stack_overflow,
stackvars = socket_stackvars,
init = socket_init,
},
{
func = "recvfrom",
variant = "sockaddr",
buftype = "struct sockaddr",
bufsize = "sizeof(struct sockaddr)",
arguments = {
"sock[0]",
"data",
"sizeof(data)",
"0",
"__buf",
"&socklen",
},
exclude = excludes_stack_overflow,
stackvars = recvfrom_sockaddr_stackvars,
init = socket_socklen_init,
uses_len = true,
},
{
func = "recvmsg",
variant = "msg_name",
buftype = "struct sockaddr",
bufsize = "sizeof(struct sockaddr)",
arguments = {
"sock[0]",
"&msg",
"0",
},
exclude = excludes_stack_overflow,
stackvars = recvmsg_stackvars,
init = [[
memset(&msg, 0, sizeof(msg));
msg.msg_name = BUF;
msg.msg_namelen = __len;
]],
uses_len = true,
},
{
func = "recvmsg",
variant = "msg_iov",
arguments = {
"sock[0]",
"&msg",
"0",
},
exclude = excludes_stack_overflow,
stackvars = recvmsg_stackvars .. "\tstruct iovec iov[2];\n",
init = [[
memset(&msg, 0, sizeof(msg));
memset(&iov[0], 0, sizeof(iov));
/*
* We position the buffer second just so that we can confirm that the
* fortification bits are traversing the iovec correctly.
*/
iov[1].iov_base = BUF;
iov[1].iov_len = __len;
msg.msg_iov = &iov[0];
msg.msg_iovlen = nitems(iov);
]],
uses_len = true,
},
{
func = "recvmsg",
variant = "msg_control",
bufsize = "CMSG_SPACE(sizeof(int))",
arguments = {
"sock[0]",
"&msg",
"0",
},
exclude = excludes_stack_overflow,
stackvars = recvmsg_stackvars,
init = [[
memset(&msg, 0, sizeof(msg));
msg.msg_control = BUF;
msg.msg_controllen = __len;
]],
uses_len = true,
},
{
func = "recvmmsg",
variant = "msgvec",
buftype = "struct mmsghdr[]",
bufsize = "2",
arguments = {
"sock[0]",
"__buf",
"__len",
"0",
"NULL",
},
stackvars = socket_stackvars,
},
{
-- We'll assume that recvmsg is covering msghdr
-- validation thoroughly enough, we'll just try tossing
-- an error in the second element of a msgvec to try and
-- make sure that each one is being validated.
func = "recvmmsg",
variant = "msghdr",
arguments = {
"sock[0]",
"&msgvec[0]",
"nitems(msgvec)",
"0",
"NULL",
},
exclude = excludes_stack_overflow,
stackvars = socket_stackvars .. "\tstruct mmsghdr msgvec[2];\n",
init = [[
memset(&msgvec[0], 0, sizeof(msgvec));
/*
* Same as above, make sure fortification isn't ignoring n > 1 elements
* of the msgvec.
*/
msgvec[1].msg_hdr.msg_control = BUF;
msgvec[1].msg_hdr.msg_controllen = __len;
]],
uses_len = true,
},
},
uio = {
-- <sys/uio.h>
{
func = "readv",
buftype = "struct iovec[]",
bufsize = 2,
arguments = {
"STDIN_FILENO",
"__buf",
"__len",
},
},
{
func = "readv",
variant = "iov",
arguments = {
"STDIN_FILENO",
"iov",
"nitems(iov)",
},
exclude = excludes_stack_overflow,
stackvars = readv_stackvars,
init = readv_init,
uses_len = true,
},
{
func = "preadv",
buftype = "struct iovec[]",
bufsize = 2,
arguments = {
"STDIN_FILENO",
"__buf",
"__len",
"0",
},
},
{
func = "preadv",
variant = "iov",
arguments = {
"STDIN_FILENO",
"iov",
"nitems(iov)",
"0",
},
exclude = excludes_stack_overflow,
stackvars = readv_stackvars,
init = readv_init,
uses_len = true,
},
},
poll = {
-- <poll.h>
{
func = "poll",
bufsize = "4",
buftype = "struct pollfd[]",
arguments = {
"__buf",
"__len",
"0",
},
init = poll_init,
},
{
func = "ppoll",
bufsize = "4",
buftype = "struct pollfd[]",
arguments = {
"__buf",
"__len",
"&tv",
"NULL",
},
stackvars = "\tstruct timespec tv = { 0 };\n",
init = poll_init,
},
},
stdio = {
-- <stdio.h>
{
func = "ctermid",
bufsize = "L_ctermid",
arguments = {
"__buf",
},
exclude = excludes_stack_overflow,
},
{
func = "ctermid_r",
bufsize = "L_ctermid",
arguments = {
"__buf",
},
exclude = excludes_stack_overflow,
},
{
func = "fread",
arguments = {
"__buf",
"__len",
"1",
"stdin",
},
exclude = excludes_stack_overflow,
init = stdio_init,
},
{
func = "fread_unlocked",
arguments = {
"__buf",
"__len",
"1",
"stdin",
},
exclude = excludes_stack_overflow,
init = stdio_init,
},
{
func = "gets_s",
arguments = {
"__buf",
"__len",
},
exclude = excludes_stack_overflow,
init = stdio_init,
},
{
func = "sprintf",
arguments = {
"__buf",
"\"%.*s\"",
"(int)__len - 1", -- - 1 for NUL terminator
"srcvar",
},
exclude = excludes_stack_overflow,
stackvars = printf_stackvars,
init = printf_init,
},
{
func = "snprintf",
arguments = {
"__buf",
"__len",
"\"%.*s\"",
"(int)__len - 1", -- - 1 for NUL terminator
"srcvar",
},
exclude = excludes_stack_overflow,
stackvars = printf_stackvars,
init = printf_init,
},
{
func = "tmpnam",
bufsize = "L_tmpnam",
arguments = {
"__buf",
},
exclude = excludes_stack_overflow,
},
{
func = "fgets",
arguments = {
"__buf",
"__len",
"fp",
},
exclude = excludes_stack_overflow,
stackvars = "\tFILE *fp;\n",
init = [[
fp = new_fp(__len);
]],
},
},
stdlib = {
-- <stdlib.h>
{
func = "arc4random_buf",
arguments = {
"__buf",
"__len",
},
exclude = excludes_stack_overflow,
},
{
func = "realpath",
bufsize = "PATH_MAX",
arguments = {
"\".\"",
"__buf",
},
exclude = excludes_stack_overflow,
},
},
string = {
-- <string.h>
{
func = "memcpy",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = "\tchar src[__len + 10];\n",
},
{
func = "mempcpy",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = "\tchar src[__len + 10];\n",
},
{
func = "memmove",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = "\tchar src[__len + 10];\n",
},
{
func = "memset",
arguments = {
"__buf",
"0",
"__len",
},
exclude = excludes_stack_overflow,
},
{
func = "memset_explicit",
arguments = {
"__buf",
"0",
"__len",
},
exclude = excludes_stack_overflow,
},
{
func = "stpcpy",
arguments = {
"__buf",
"src",
},
exclude = excludes_stack_overflow,
stackvars = string_stackvars,
init = string_init,
uses_len = true,
},
{
func = "stpncpy",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = string_stackvars,
init = string_init,
},
{
func = "strcat",
arguments = {
"__buf",
"src",
},
exclude = excludes_stack_overflow,
stackvars = string_stackvars,
init = string_init,
uses_len = true,
},
{
func = "strlcat",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = string_stackvars,
init = string_init,
},
{
func = "strncat",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = string_stackvars,
init = string_init,
},
{
func = "strcpy",
arguments = {
"__buf",
"src",
},
exclude = excludes_stack_overflow,
stackvars = string_stackvars,
init = string_init,
uses_len = true,
},
{
func = "strlcpy",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = string_stackvars,
init = string_init,
},
{
func = "strncpy",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = string_stackvars,
init = string_init,
},
},
strings = {
-- <strings.h>
{
func = "bcopy",
arguments = {
"src",
"__buf",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = "\tchar src[__len + 10];\n",
},
{
func = "bzero",
arguments = {
"__buf",
"__len",
},
exclude = excludes_stack_overflow,
},
{
func = "explicit_bzero",
arguments = {
"__buf",
"__len",
},
exclude = excludes_stack_overflow,
},
},
unistd = {
-- <unistd.h>
{
func = "getcwd",
bufsize = "8",
arguments = {
"__buf",
"__len",
},
exclude = excludes_stack_overflow,
},
{
func = "getgrouplist",
bufsize = "4",
buftype = "gid_t[]",
arguments = {
"\"root\"",
"0",
"__buf",
"&intlen",
},
exclude = excludes_stack_overflow,
stackvars = "\tint intlen = (int)__len;\n",
uses_len = true,
},
{
func = "getgroups",
bufsize = "4",
buftype = "gid_t[]",
arguments = {
"__len",
"__buf",
},
exclude = excludes_stack_overflow,
},
{
func = "getloginclass",
arguments = {
"__buf",
"__len",
},
exclude = excludes_stack_overflow,
},
{
func = "pread",
bufsize = "41",
arguments = {
"fd",
"__buf",
"__len",
"0",
},
exclude = excludes_stack_overflow,
stackvars = "\tint fd;\n",
init = [[
fd = new_tmpfile(); /* Cannot fail */
]],
},
{
func = "read",
bufsize = "41",
arguments = {
"fd",
"__buf",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = "\tint fd;\n",
init = [[
fd = new_tmpfile(); /* Cannot fail */
]],
},
{
func = "readlink",
arguments = {
"path",
"__buf",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = "\tconst char *path;\n",
init = [[
path = new_symlink(__len); /* Cannot fail */
]],
},
{
func = "readlinkat",
arguments = {
"AT_FDCWD",
"path",
"__buf",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = "\tconst char *path;\n",
init = [[
path = new_symlink(__len); /* Cannot fail */
]],
},
{
func = "getdomainname",
bufsize = "4",
arguments = {
"__buf",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = "\tchar sysdomain[256];\n",
early_init = [[
(void)getdomainname(sysdomain, __len);
if (strlen(sysdomain) <= __len)
atf_tc_skip("domain name too short for testing");
]]
},
{
func = "getentropy",
arguments = {
"__buf",
"__len",
},
exclude = excludes_stack_overflow,
},
{
func = "gethostname",
bufsize = "4",
arguments = {
"__buf",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = [[
char syshost[256];
int error;
]],
early_init = [[
error = gethostname(syshost, __len);
if (error != 0 || strlen(syshost) <= __len)
atf_tc_skip("hostname too short for testing");
]]
},
{
func = "getlogin_r",
bufsize = "MAXLOGNAME + 1",
arguments = {
"__buf",
"__len",
},
exclude = excludes_stack_overflow,
},
{
func = "ttyname_r",
arguments = {
"fd",
"__buf",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = "\tint fd;\n",
early_init = [[
fd = STDIN_FILENO;
if (!isatty(fd))
atf_tc_skip("stdin is not an fd");
]]
},
},
wchar = {
-- <wchar.h>
{
func = "wmemcpy",
buftype = "wchar_t[]",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = "\twchar_t src[__len + 10];\n",
},
{
func = "wmempcpy",
buftype = "wchar_t[]",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = "\twchar_t src[__len + 10];\n",
},
{
func = "wmemmove",
buftype = "wchar_t[]",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = "\twchar_t src[__len + 10];\n",
},
{
func = "wmemset",
buftype = "wchar_t[]",
arguments = {
"__buf",
"L'0'",
"__len",
},
exclude = excludes_stack_overflow,
},
{
func = "wcpcpy",
buftype = "wchar_t[]",
arguments = {
"__buf",
"src",
},
exclude = excludes_stack_overflow,
stackvars = wstring_stackvars,
init = wstring_init,
uses_len = true,
},
{
func = "wcpncpy",
buftype = "wchar_t[]",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = wstring_stackvars,
init = wstring_init,
},
{
func = "wcscat",
buftype = "wchar_t[]",
arguments = {
"__buf",
"src",
},
exclude = excludes_stack_overflow,
stackvars = wstring_stackvars,
init = wstring_init,
uses_len = true,
},
{
func = "wcslcat",
buftype = "wchar_t[]",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = wstring_stackvars,
init = wstring_init,
},
{
func = "wcsncat",
buftype = "wchar_t[]",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = wstring_stackvars,
init = wstring_init,
},
{
func = "wcscpy",
buftype = "wchar_t[]",
arguments = {
"__buf",
"src",
},
exclude = excludes_stack_overflow,
stackvars = wstring_stackvars,
init = wstring_init,
uses_len = true,
},
{
func = "wcslcpy",
buftype = "wchar_t[]",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = wstring_stackvars,
init = wstring_init,
},
{
func = "wcsncpy",
buftype = "wchar_t[]",
arguments = {
"__buf",
"src",
"__len",
},
exclude = excludes_stack_overflow,
stackvars = wstring_stackvars,
init = wstring_init,
},
},
}
local function write_test_boilerplate(fh, name, body)
fh:write("ATF_TC_WITHOUT_HEAD(" .. name .. ");\n")
fh:write("ATF_TC_BODY(" .. name .. ", tc)\n")
fh:write("{\n" .. body .. "\n}\n\n")
return name
end
local function generate_test_name(func, variant, disposition, heap)
local basename = func
if variant then
basename = basename .. "_" .. variant
end
if heap then
basename = basename .. "_heap"
end
if disposition < 0 then
return basename .. "_before_end"
elseif disposition == 0 then
return basename .. "_end"
else
return basename .. "_after_end"
end
end
local function array_type(buftype)
if not buftype:match("%[%]") then
return nil
end
return buftype:gsub("%[%]", "")
end
local function configurable(def, idx)
local cfgitem = def[idx]
if not cfgitem then
return nil
end
if type(cfgitem) == "function" then
return cfgitem()
end
return cfgitem
end
local function generate_stackframe(buftype, bufsize, disposition, heap, def)
local function len_offset(inverted, disposition)
-- Tests that don't use __len in their arguments may use an
-- inverted sense because we can't just specify a length that
-- would induce an access just after the end. Instead, we have
-- to manipulate the buffer size to be too short so that the
-- function under test would write one too many.
if disposition < 0 then
return ((inverted and " + ") or " - ") .. "1"
elseif disposition == 0 then
return ""
else
return ((inverted and " - ") or " + ") .. "1"
end
end
local function test_uses_len(def)
if def.uses_len then
return true
end
for _, arg in ipairs(def.arguments) do
if arg:match("__len") or arg:match("__idx") then
return true
end
end
return false
end
-- This is perhaps a little convoluted, but we toss the buffer into a
-- struct on the stack to guarantee that we have at least one valid
-- byte on either side of the buffer -- a measure to make sure that
-- we're tripping _FORTIFY_SOURCE specifically in the buffer + 1 case,
-- rather than some other stack or memory protection.
local vars = "\tstruct {\n"
vars = vars .. "\t\tuint8_t padding_l;\n"
local uses_len = test_uses_len(def)
local bufsize_offset = len_offset(not uses_len, disposition)
local buftype_elem = array_type(buftype)
local size_expr = bufsize
if not uses_len then
-- If the length isn't in use, we have to vary the buffer size
-- since the fortified function likely has some internal size
-- constraint that it's supposed to be checking.
size_expr = size_expr .. bufsize_offset
end
if not heap and buftype_elem then
-- Array type: size goes after identifier
vars = vars .. "\t\t" .. buftype_elem ..
" __buf[" .. size_expr .. "];\n"
else
local basic_type = buftype_elem or buftype
-- Heap tests obviously just put a pointer on the stack that
-- points to our new allocation, but we leave it in the padded
-- struct just to simplify our generator.
if heap then
basic_type = basic_type .. " *"
end
vars = vars .. "\t\t" .. basic_type .. " __buf;\n"
end
-- padding_r is our just-past-the-end padding that we use to make sure
-- that there's a valid portion after the buffer that isn't being
-- included in our function calls. If we didn't have it, then we'd have
-- a hard time feeling confident that an abort on the just-after tests
-- isn't maybe from some other memory or stack protection.
vars = vars .. "\t\tuint8_t padding_r;\n"
vars = vars .. "\t} __stack;\n"
-- Not all tests will use __bufsz, but some do for, e.g., clearing
-- memory..
vars = vars .. "\tconst size_t __bufsz __unused = "
if heap then
local scalar = 1
if buftype_elem then
scalar = size_expr
end
vars = vars .. "sizeof(*__stack.__buf) * (" .. scalar .. ");\n"
else
vars = vars .. "sizeof(__stack.__buf);\n"
end
vars = vars .. "\tconst size_t __len = " .. bufsize ..
bufsize_offset .. ";\n"
vars = vars .. "\tconst size_t __idx __unused = __len - 1;\n"
-- For overflow testing, we need to fork() because we're expecting the
-- test to ultimately abort()/_exit(). Then we can collect the exit
-- status and report appropriately.
if disposition > 0 then
vars = vars .. "\tpid_t __child;\n"
vars = vars .. "\tint __status;\n"
end
-- Any other stackvars defined by the test get placed after everything
-- else.
vars = vars .. (configurable(def, "stackvars") or "")
return vars
end
local function write_test(fh, func, disposition, heap, def)
local testname = generate_test_name(func, def.variant, disposition, heap)
local buftype = def.buftype or "unsigned char[]"
local bufsize = def.bufsize or 42
local body = ""
if def.exclude and def.exclude(disposition, heap) then
return
end
local function need_addr(buftype)
return not (buftype:match("%[%]") or buftype:match("%*"))
end
if heap then
body = body .. "#define BUF __stack.__buf\n"
else
body = body .. "#define BUF &__stack.__buf\n"
end
-- Setup the buffer
body = body .. generate_stackframe(buftype, bufsize, disposition, heap, def) ..
"\n"
-- Any early initialization goes before we would fork for the just-after
-- tests, because they may want to skip the test based on some criteria
-- and we can't propagate that up very easily once we're forked.
local early_init = configurable(def, "early_init")
body = body .. (early_init or "")
if early_init then
body = body .. "\n"
end
-- Fork off, iff we're testing some access past the end of the buffer.
if disposition > 0 then
body = body .. [[
__child = fork();
ATF_REQUIRE(__child >= 0);
if (__child > 0)
goto monitor;
/* Child */
disable_coredumps();
]]
end
local bufvar = "__stack.__buf"
if heap then
-- Buffer needs to be initialized because it's a heap allocation.
body = body .. "\t" .. bufvar .. " = malloc(__bufsz);\n"
end
-- Non-early init happens just after the fork in the child, not the
-- monitor. This is used to setup any other buffers we may need, for
-- instance.
local extra_init = configurable(def, "init")
body = body .. (extra_init or "")
if heap or extra_init then
body = body .. "\n"
end
-- Setup the function call with arguments as described in the test
-- definition.
body = body .. "\t" .. func .. "("
for idx, arg in ipairs(def.arguments) do
if idx > 1 then
body = body .. ", "
end
if arg == "__buf" then
if not heap and need_addr(buftype) then
body = body .. "&"
end
body = body .. bufvar
else
local argname = arg
if def.value_of then
argname = argname or def.value_of(arg)
end
body = body .. argname
end
end
body = body .. ");\n"
-- Monitor stuff follows, for OOB access.
if disposition <= 0 then
goto skip
end
body = body .. [[
_exit(EX_SOFTWARE); /* Should have aborted. */
monitor:
while (waitpid(__child, &__status, 0) != __child) {
ATF_REQUIRE_EQ(EINTR, errno);
}
if (!WIFSIGNALED(__status)) {
switch (WEXITSTATUS(__status)) {
case EX_SOFTWARE:
atf_tc_fail("FORTIFY_SOURCE failed to abort");
break;
case EX_OSERR:
atf_tc_fail("setrlimit(2) failed");
break;
default:
atf_tc_fail("child exited with status %d",
WEXITSTATUS(__status));
}
} else {
ATF_REQUIRE_EQ(SIGABRT, WTERMSIG(__status));
}
]]
::skip::
body = body .. "#undef BUF\n"
return write_test_boilerplate(fh, testname, body)
end
-- main()
local tests
local tcat = assert(arg[1], "usage: generate-fortify-tests.lua <category>")
for k, defs in pairs(all_tests) do
if k == tcat then
tests = defs
break
end
end
assert(tests, "category " .. tcat .. " not found")
local fh = io.stdout
fh:write("/* @" .. "generated" .. " by `generate-fortify-tests.lua \"" ..
tcat .. "\"` */\n\n")
fh:write("#define _FORTIFY_SOURCE 2\n")
fh:write("#define TMPFILE_SIZE (1024 * 32)\n")
fh:write("\n")
for _, inc in ipairs(includes) do
fh:write("#include <" .. inc .. ">\n")
end
fh:write([[
static FILE * __unused
new_fp(size_t __len)
{
static char fpbuf[LINE_MAX];
FILE *fp;
ATF_REQUIRE(__len <= sizeof(fpbuf));
memset(fpbuf, 'A', sizeof(fpbuf) - 1);
fpbuf[sizeof(fpbuf) - 1] = '\0';
fp = fmemopen(fpbuf, sizeof(fpbuf), "rb");
ATF_REQUIRE(fp != NULL);
return (fp);
}
/*
* Create a new symlink to use for readlink(2) style tests, we'll just use a
* random target name to have something interesting to look at.
*/
static const char * __unused
new_symlink(size_t __len)
{
static const char linkname[] = "link";
char target[MAXNAMLEN];
int error;
ATF_REQUIRE(__len <= sizeof(target));
arc4random_buf(target, sizeof(target));
error = unlink(linkname);
ATF_REQUIRE(error == 0 || errno == ENOENT);
error = symlink(target, linkname);
ATF_REQUIRE(error == 0);
return (linkname);
}
/*
* For our purposes, first descriptor will be the reader; we'll send both
* raw data and a control message over it so that the result can be used for
* any of our recv*() tests.
*/
static void __unused
new_socket(int sock[2])
{
unsigned char ctrl[CMSG_SPACE(sizeof(int))] = { 0 };
static char sockbuf[256];
ssize_t rv;
size_t total = 0;
struct msghdr hdr = { 0 };
struct cmsghdr *cmsg;
int error, fd;
error = socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
ATF_REQUIRE(error == 0);
while (total != sizeof(sockbuf)) {
rv = send(sock[1], &sockbuf[total], sizeof(sockbuf) - total, 0);
ATF_REQUIRE_MSG(rv > 0,
"expected bytes sent, got %zd with %zu left (size %zu, total %zu)",
rv, sizeof(sockbuf) - total, sizeof(sockbuf), total);
ATF_REQUIRE_MSG(total + (size_t)rv <= sizeof(sockbuf),
"%zd exceeds total %zu", rv, sizeof(sockbuf));
total += rv;
}
hdr.msg_control = ctrl;
hdr.msg_controllen = sizeof(ctrl);
cmsg = CMSG_FIRSTHDR(&hdr);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
fd = STDIN_FILENO;
memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
error = sendmsg(sock[1], &hdr, 0);
ATF_REQUIRE(error != -1);
}
/*
* Constructs a tmpfile that we can use for testing read(2) and friends.
*/
static int __unused
new_tmpfile(void)
{
char buf[1024];
ssize_t rv;
size_t written;
int fd;
fd = open("tmpfile", O_RDWR | O_CREAT | O_TRUNC, 0644);
ATF_REQUIRE(fd >= 0);
written = 0;
while (written < TMPFILE_SIZE) {
rv = write(fd, buf, sizeof(buf));
ATF_REQUIRE(rv > 0);
written += rv;
}
ATF_REQUIRE_EQ(0, lseek(fd, 0, SEEK_SET));
return (fd);
}
static void
disable_coredumps(void)
{
struct rlimit rl = { 0 };
if (setrlimit(RLIMIT_CORE, &rl) == -1)
_exit(EX_OSERR);
}
/*
* Replaces stdin with a file that we can actually read from, for tests where
* we want a FILE * or fd that we can get data from.
*/
static void __unused
replace_stdin(void)
{
int fd;
fd = new_tmpfile();
(void)dup2(fd, STDIN_FILENO);
if (fd != STDIN_FILENO)
close(fd);
}
]])
for _, def in pairs(tests) do
local func = def.func
local function write_tests(heap)
-- Dispositions here are relative to the buffer size prescribed
-- by the test definition.
local dispositions = def.dispositions or { -1, 0, 1 }
for _, disposition in ipairs(dispositions) do
tests_added[#tests_added + 1] = write_test(fh, func, disposition, heap, def)
end
end
write_tests(false)
write_tests(true)
end
fh:write("ATF_TP_ADD_TCS(tp)\n")
fh:write("{\n")
for idx = 1, #tests_added do
fh:write("\tATF_TP_ADD_TC(tp, " .. tests_added[idx] .. ");\n")
end
fh:write("\treturn (atf_no_error());\n")
fh:write("}\n")
|