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
|
/*
* Copyright (c) 2022 Todd C. Miller <Todd.Miller@sudo.ws>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
*/
#include <config.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#if defined(HAVE_STDINT_H)
# include <stdint.h>
#elif defined(HAVE_INTTYPES_H)
# include <inttypes.h>
#endif
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if defined(HAVE_ENDIAN_H)
# include <endian.h>
#elif defined(HAVE_SYS_ENDIAN_H)
# include <sys/endian.h>
#elif defined(HAVE_MACHINE_ENDIAN_H)
# include <machine/endian.h>
#else
# include <compat/endian.h>
#endif
#include <sudo.h>
#include <sudo_exec.h>
#ifdef HAVE_PTRACE_INTERCEPT
# include <exec_intercept.h>
# include <exec_ptrace.h>
/* We need to take care when ptracing 32-bit binaries on 64-bit kernels. */
# ifdef __LP64__
# define COMPAT_FLAG 0x01
# else
# define COMPAT_FLAG 0x00
# endif
static int seccomp_trap_supported = -1;
#ifdef HAVE_PROCESS_VM_READV
static size_t page_size;
#endif
static size_t arg_max;
/* Register getters and setters. */
# ifdef SECCOMP_AUDIT_ARCH_COMPAT
static inline unsigned long
get_stack_pointer(struct sudo_ptrace_regs *regs)
{
if (regs->compat) {
return compat_reg_sp(regs->u.compat);
} else {
return reg_sp(regs->u.native);
}
}
static inline void
set_sc_retval(struct sudo_ptrace_regs *regs, int retval)
{
if (regs->compat) {
compat_reg_set_retval(regs->u.compat, retval);
} else {
reg_set_retval(regs->u.native, retval);
}
}
static inline int
get_syscallno(struct sudo_ptrace_regs *regs)
{
if (regs->compat) {
return compat_reg_syscall(regs->u.compat);
} else {
return reg_syscall(regs->u.native);
}
}
static inline void
set_syscallno(pid_t pid, struct sudo_ptrace_regs *regs, int syscallno)
{
if (regs->compat) {
compat_reg_set_syscall(regs->u.compat, syscallno);
} else {
reg_set_syscall(regs->u.native, syscallno);
}
}
static inline unsigned long
get_sc_arg1(struct sudo_ptrace_regs *regs)
{
if (regs->compat) {
return compat_reg_arg1(regs->u.compat);
} else {
return reg_arg1(regs->u.native);
}
}
static inline void
set_sc_arg1(struct sudo_ptrace_regs *regs, unsigned long addr)
{
if (regs->compat) {
compat_reg_set_arg1(regs->u.compat, addr);
} else {
reg_set_arg1(regs->u.native, addr);
}
}
static inline unsigned long
get_sc_arg2(struct sudo_ptrace_regs *regs)
{
if (regs->compat) {
return compat_reg_arg2(regs->u.compat);
} else {
return reg_arg2(regs->u.native);
}
}
static inline void
set_sc_arg2(struct sudo_ptrace_regs *regs, unsigned long addr)
{
if (regs->compat) {
compat_reg_set_arg2(regs->u.compat, addr);
} else {
reg_set_arg2(regs->u.native, addr);
}
}
static inline unsigned long
get_sc_arg3(struct sudo_ptrace_regs *regs)
{
if (regs->compat) {
return compat_reg_arg3(regs->u.compat);
} else {
return reg_arg3(regs->u.native);
}
}
# ifdef notyet
static inline void
set_sc_arg3(struct sudo_ptrace_regs *regs, unsigned long addr)
{
if (regs->compat) {
compat_reg_set_arg3(regs->u.compat, addr);
} else {
reg_set_arg3(regs->u.native, addr);
}
}
static inline unsigned long
get_sc_arg4(struct sudo_ptrace_regs *regs)
{
if (regs->compat) {
return compat_reg_arg4(regs->u.compat);
} else {
return reg_arg4(regs->u.native);
}
}
static inline void
set_sc_arg4(struct sudo_ptrace_regs *regs, unsigned long addr)
{
if (regs->compat) {
compat_reg_set_arg4(regs->u.compat, addr);
} else {
reg_set_arg4(regs->u.native, addr);
}
}
# endif /* notyet */
# else /* SECCOMP_AUDIT_ARCH_COMPAT */
static inline unsigned long
get_stack_pointer(struct sudo_ptrace_regs *regs)
{
return reg_sp(regs->u.native);
}
static inline void
set_sc_retval(struct sudo_ptrace_regs *regs, int retval)
{
reg_set_retval(regs->u.native, retval);
}
static inline int
get_syscallno(struct sudo_ptrace_regs *regs)
{
return reg_syscall(regs->u.native);
}
static inline void
set_syscallno(pid_t pid, struct sudo_ptrace_regs *regs, int syscallno)
{
reg_set_syscall(regs->u.native, syscallno);
}
static inline unsigned long
get_sc_arg1(struct sudo_ptrace_regs *regs)
{
return reg_arg1(regs->u.native);
}
static inline void
set_sc_arg1(struct sudo_ptrace_regs *regs, unsigned long addr)
{
reg_set_arg1(regs->u.native, addr);
}
static inline unsigned long
get_sc_arg2(struct sudo_ptrace_regs *regs)
{
return reg_arg2(regs->u.native);
}
static inline void
set_sc_arg2(struct sudo_ptrace_regs *regs, unsigned long addr)
{
reg_set_arg2(regs->u.native, addr);
}
static inline unsigned long
get_sc_arg3(struct sudo_ptrace_regs *regs)
{
return reg_arg3(regs->u.native);
}
# ifdef notyet
static inline void
set_sc_arg3(struct sudo_ptrace_regs *regs, unsigned long addr)
{
reg_set_arg3(regs->u.native, addr);
}
static inline unsigned long
get_sc_arg4(struct sudo_ptrace_regs *regs)
{
return reg_arg4(regs->u.native);
}
static inline void
set_sc_arg4(struct sudo_ptrace_regs *regs, unsigned long addr)
{
reg_set_arg4(regs->u.native, addr);
}
# endif /* notyet */
# endif /* SECCOMP_AUDIT_ARCH_COMPAT */
/*
* Get the registers for the given process and store in regs, which
* must be large enough. If the compat flag is set, pid is expected
* to refer to a 32-bit process and the md parameters will be filled
* in accordingly.
* Returns true on success, else false.
*/
static bool
ptrace_getregs(int pid, struct sudo_ptrace_regs *regs, int compat)
{
struct iovec iov;
debug_decl(ptrace_getregs, SUDO_DEBUG_EXEC);
iov.iov_base = ®s->u;
iov.iov_len = sizeof(regs->u);
# ifdef __mips__
/* PTRACE_GETREGSET has bugs with the MIPS o32 ABI at least. */
if (ptrace(PTRACE_GETREGS, pid, NULL, iov.iov_base) == -1)
debug_return_bool(false);
# else
if (ptrace(PTRACE_GETREGSET, pid, (void *)NT_PRSTATUS, &iov) == -1)
debug_return_bool(false);
# endif /* __mips__ */
if (compat == -1) {
# ifdef SECCOMP_AUDIT_ARCH_COMPAT
if (sizeof(regs->u.native) != sizeof(regs->u.compat)) {
/* Guess compat based on size of register struct returned. */
compat = iov.iov_len != sizeof(regs->u.native);
} else {
/* Assume a 64-bit executable will have a 64-bit stack pointer. */
compat = reg_sp(regs->u.native) < 0xffffffff;
}
# else
compat = false;
# endif /* SECCOMP_AUDIT_ARCH_COMPAT */
}
/* Machine-dependent parameters to support compat binaries. */
if (compat) {
regs->compat = true;
regs->wordsize = sizeof(int);
} else {
regs->compat = false;
regs->wordsize = sizeof(long);
}
debug_return_bool(true);
}
/*
* Set the registers, specified by regs, for the given process.
* Returns true on success, else false.
*/
static bool
ptrace_setregs(int pid, struct sudo_ptrace_regs *regs)
{
debug_decl(ptrace_setregs, SUDO_DEBUG_EXEC);
# ifdef __mips__
/* PTRACE_SETREGSET has bugs with the MIPS o32 ABI at least. */
if (ptrace(PTRACE_SETREGS, pid, NULL, ®s->u) == -1)
debug_return_bool(false);
# else
struct iovec iov;
iov.iov_base = ®s->u;
iov.iov_len = sizeof(regs->u);
if (ptrace(PTRACE_SETREGSET, pid, (void *)NT_PRSTATUS, &iov) == -1)
debug_return_bool(false);
# endif /* __mips__ */
debug_return_bool(true);
}
#ifdef HAVE_PROCESS_VM_READV
/*
* Read the string at addr and store in buf using process_vm_readv(2).
* Returns the number of bytes stored, including the NUL.
*/
static ssize_t
ptrace_readv_string(pid_t pid, unsigned long addr, char *buf, size_t bufsize)
{
const char *cp, *buf0 = buf;
struct iovec local, remote;
ssize_t nread;
debug_decl(ptrace_read_string, SUDO_DEBUG_EXEC);
/*
* Read the string via process_vm_readv(2) one page at a time.
* We could do larger reads but since we don't know the length
* of the string, going one page at a time is simplest.
*/
for (;;) {
if (bufsize == 0) {
sudo_debug_printf(SUDO_DEBUG_ERROR,
"%s: %d: out of space reading string", __func__, (int)pid);
errno = ENOSPC;
debug_return_ssize_t(-1);
}
local.iov_base = buf;
local.iov_len = bufsize;
remote.iov_base = (void *)addr;
remote.iov_len = MIN(bufsize, page_size);
nread = process_vm_readv(pid, &local, 1, &remote, 1, 0);
switch (nread) {
case -1:
sudo_debug_printf(
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"process_vm_readv(%d, [0x%lx, %zu], 1, [0x%lx, %zu], 1, 0)",
(int)pid, (unsigned long)local.iov_base, local.iov_len,
(unsigned long)remote.iov_base, remote.iov_len);
debug_return_ssize_t(-1);
case 0:
sudo_debug_printf(
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"process_vm_readv(%d, [0x%lx, %zu], 1, [0x%lx, %zu], 1, 0): %s",
(int)pid, (unsigned long)local.iov_base, local.iov_len,
(unsigned long)remote.iov_base, remote.iov_len, "premature EOF");
debug_return_ssize_t(-1);
default:
/* Check for NUL terminator in page. */
cp = memchr(buf, '\0', (size_t)nread);
if (cp != NULL)
debug_return_ssize_t((cp - buf0) + 1); /* includes NUL */
buf += nread;
bufsize -= (size_t)nread;
addr += sizeof(unsigned long);
break;
}
}
debug_return_ssize_t(-1);
}
#endif /* HAVE_PROCESS_VM_READV */
/*
* Read the string at addr and store in buf using ptrace(2).
* Returns the number of bytes stored, including the NUL.
*/
static ssize_t
ptrace_read_string(pid_t pid, unsigned long addr, char *buf, size_t bufsize)
{
const char *cp, *buf0 = buf;
unsigned long word;
size_t i;
debug_decl(ptrace_read_string, SUDO_DEBUG_EXEC);
#ifdef HAVE_PROCESS_VM_READV
ssize_t nread = ptrace_readv_string(pid, addr, buf, bufsize);
if (nread != -1 || errno != ENOSYS)
debug_return_ssize_t(nread);
#endif /* HAVE_PROCESS_VM_READV */
/*
* Read the string via ptrace(2) one (native) word at a time.
* We use the native word size even in compat mode because that
* is the unit ptrace(2) uses.
*/
for (;;) {
word = ptrace(PTRACE_PEEKDATA, pid, addr, NULL);
if (word == (unsigned long)-1) {
sudo_debug_printf(
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"ptrace(PTRACE_PEEKDATA, %d, 0x%lx, NULL)", (int)pid, addr);
debug_return_ssize_t(-1);
}
cp = (char *)&word;
for (i = 0; i < sizeof(unsigned long); i++) {
if (bufsize == 0) {
sudo_debug_printf(SUDO_DEBUG_ERROR,
"%s: %d: out of space reading string", __func__, (int)pid);
errno = ENOSPC;
debug_return_ssize_t(-1);
}
*buf = cp[i];
if (*buf++ == '\0')
debug_return_ssize_t(buf - buf0);
bufsize--;
}
addr += sizeof(unsigned long);
}
}
/*
* Expand buf by doubling its size.
* Updates bufp and bufsizep and recalculates curp and remp if non-NULL.
* Returns true on success, else false.
*/
static bool
growbuf(char **bufp, size_t *bufsizep, char **curp, size_t *remp)
{
const size_t oldsize = *bufsizep;
char *newbuf;
debug_decl(growbuf, SUDO_DEBUG_EXEC);
/* Double the size of the buffer. */
newbuf = reallocarray(*bufp, 2, oldsize);
if (newbuf == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
debug_return_bool(false);
}
if (curp != NULL)
*curp = newbuf + (*curp - *bufp);
if (remp != NULL)
*remp += oldsize;
*bufp = newbuf;
*bufsizep = 2 * oldsize;
debug_return_bool(true);
}
/*
* Build a NULL-terminated string vector from a string table.
* On success, returns number of bytes used for the vector and sets
* vecp to the start of the vector and countp to the number of elements
* (not including the NULL). The buffer is resized as needed.
* Both vecp and its elements are stored as offsets into buf, not pointers.
* However, NULL is still stored as NULL.
* Returns (size_t)-1 on failure.
*/
static ssize_t
strtab_to_vec(char *strtab, size_t strtab_len, int *countp, char ***vecp,
char **bufp, size_t *bufsizep, size_t remainder)
{
char *strend = strtab + strtab_len;
char **vec, **vp;
int count = 0;
debug_decl(strtab_to_vec, SUDO_DEBUG_EXEC);
/* Store vector in buf after string table and make it aligned. */
while (remainder < 2 * sizeof(char *)) {
if (!growbuf(bufp, bufsizep, &strtab, &remainder))
debug_return_ssize_t(-1);
strend = strtab + strtab_len;
}
vec = (char **)LONGALIGN(strend);
remainder -= (size_t)((char *)vec - strend);
/* Fill in vector with the strings we read. */
for (vp = vec; strtab < strend; ) {
while (remainder < 2 * sizeof(char *)) {
if (!growbuf(bufp, bufsizep, &strtab, &remainder))
debug_return_ssize_t(-1);
strend = strtab + strtab_len;
vec = (char **)LONGALIGN(strend);
vp = vec + count;
}
/* Store offset into buf (not a pointer) in case of realloc(). */
*vp++ = (char *)(strtab - *bufp);
remainder -= sizeof(char *);
strtab = memchr(strtab, '\0', (size_t)(strend - strtab));
if (strtab == NULL)
break;
strtab++;
count++;
}
*vp++ = NULL; /* we always leave room for NULL */
*countp = count;
*vecp = (char **)((char *)vec - *bufp);
debug_return_ssize_t((char *)vp - strend);
}
/*
* Read the string vector at addr and store it in bufp, which
* is reallocated as needed. The actual vector is returned in vecp.
* The count stored in countp does not include the terminating NULL pointer.
* The vecp and its contents are _offsets_, not pointers, in case the buffer
* gets reallocated later. The caller is responsible for converting the
* offsets into pointers based on the buffer before using.
* Returns the number of bytes in buf consumed (including NULs).
*/
static ssize_t
ptrace_read_vec(pid_t pid, struct sudo_ptrace_regs *regs, unsigned long addr,
int *countp, char ***vecp, char **bufp, size_t *bufsizep, size_t off)
{
# ifdef SECCOMP_AUDIT_ARCH_COMPAT
unsigned long next_word = (unsigned long)-1;
# endif
size_t strtab_len, remainder = *bufsizep - off;
char *strtab = *bufp + off;
unsigned long word;
ssize_t len;
debug_decl(ptrace_read_vec, SUDO_DEBUG_EXEC);
/* Treat a NULL vector as empty, thanks Linux. */
if (addr == 0) {
char **vp;
while (remainder < 2 * sizeof(char *)) {
if (!growbuf(bufp, bufsizep, &strtab, &remainder))
debug_return_ssize_t(-1);
}
vp = (char **)LONGALIGN(strtab);
*vecp = (char **)((char *)vp - *bufp);
*countp = 0;
*vp++ = NULL;
debug_return_ssize_t((char *)vp - strtab);
}
/* Fill in string table. */
do {
# ifdef SECCOMP_AUDIT_ARCH_COMPAT
if (next_word == (unsigned long)-1) {
word = ptrace(PTRACE_PEEKDATA, pid, addr, NULL);
if (regs->compat) {
/* Stash the next compat word in next_word. */
# if BYTE_ORDER == BIG_ENDIAN
next_word = word & 0xffffffffU;
word >>= 32;
# else
next_word = word >> 32;
word &= 0xffffffffU;
# endif
}
} else {
/* Use the stashed value of the next word. */
word = next_word;
next_word = (unsigned long)-1;
}
# else /* SECCOMP_AUDIT_ARCH_COMPAT */
word = ptrace(PTRACE_PEEKDATA, pid, addr, NULL);
# endif /* SECCOMP_AUDIT_ARCH_COMPAT */
switch (word) {
case -1:
sudo_warn("%s: ptrace(PTRACE_PEEKDATA, %d, 0x%lx, NULL)",
__func__, (int)pid, addr);
debug_return_ssize_t(-1);
case 0:
/* NULL terminator */
break;
default:
for (;;) {
len = ptrace_read_string(pid, word, strtab, remainder);
if (len != -1)
break;
if (errno != ENOSPC)
debug_return_ssize_t(-1);
if (!growbuf(bufp, bufsizep, &strtab, &remainder))
debug_return_ssize_t(-1);
}
strtab += len;
remainder -= (size_t)len;
addr += regs->wordsize;
continue;
}
} while (word != 0);
/* Store strings in a vector after the string table. */
strtab_len = (size_t)(strtab - (*bufp + off));
strtab = *bufp + off;
len = strtab_to_vec(strtab, strtab_len, countp, vecp, bufp, bufsizep,
remainder);
if (len == -1)
debug_return_ssize_t(-1);
debug_return_ssize_t((ssize_t)strtab_len + len);
}
#ifdef HAVE_PROCESS_VM_READV
/*
* Write the NUL-terminated string str to addr in the tracee using
* process_vm_writev(2).
* Returns the number of bytes written, including trailing NUL.
*/
static ssize_t
ptrace_writev_string(pid_t pid, unsigned long addr, const char *str0)
{
const char *str = str0;
size_t len = strlen(str) + 1;
debug_decl(ptrace_writev_string, SUDO_DEBUG_EXEC);
/*
* Write the string via process_vm_writev(2), handling partial writes.
*/
for (;;) {
struct iovec local, remote;
ssize_t nwritten;
local.iov_base = (void *)str;
local.iov_len = len;
remote.iov_base = (void *)addr;
remote.iov_len = len;
nwritten = process_vm_writev(pid, &local, 1, &remote, 1, 0);
switch (nwritten) {
case -1:
sudo_debug_printf(
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"process_vm_writev(%d, [0x%lx, %zu], 1, [0x%lx, %zu], 1, 0)",
(int)pid, (unsigned long)local.iov_base, local.iov_len,
(unsigned long)remote.iov_base, remote.iov_len);
debug_return_ssize_t(-1);
case 0:
/* Should not be possible. */
sudo_debug_printf(
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"process_vm_writev(%d, [0x%lx, %zu], 1, [0x%lx, %zu], 1, 0): %s",
(int)pid, (unsigned long)local.iov_base, local.iov_len,
(unsigned long)remote.iov_base, remote.iov_len,
"zero bytes written");
debug_return_ssize_t(-1);
default:
str += nwritten;
len -= (size_t)nwritten;
addr += (size_t)nwritten;
if (len == 0)
debug_return_ssize_t(str - str0); /* includes NUL */
break;
}
}
debug_return_ssize_t(-1);
}
#endif /* HAVE_PROCESS_VM_READV */
/*
* Write the NUL-terminated string str to addr in the tracee using ptrace(2).
* Returns the number of bytes written, including trailing NUL.
*/
static ssize_t
ptrace_write_string(pid_t pid, unsigned long addr, const char *str)
{
const char *str0 = str;
size_t i;
union {
unsigned long word;
char buf[sizeof(unsigned long)];
} u;
debug_decl(ptrace_write_string, SUDO_DEBUG_EXEC);
#ifdef HAVE_PROCESS_VM_READV
ssize_t nwritten = ptrace_writev_string(pid, addr, str);
if (nwritten != -1 || errno != ENOSYS)
debug_return_ssize_t(nwritten);
#endif /* HAVE_PROCESS_VM_READV */
/*
* Write the string via ptrace(2) one (native) word at a time.
* We use the native word size even in compat mode because that
* is the unit ptrace(2) writes in terms of.
*/
for (;;) {
for (i = 0; i < sizeof(u.buf); i++) {
if (*str == '\0') {
/* NUL-pad buf to sizeof(unsigned long). */
u.buf[i] = '\0';
continue;
}
u.buf[i] = *str++;
}
if (ptrace(PTRACE_POKEDATA, pid, addr, u.word) == -1) {
sudo_warn("%s: ptrace(PTRACE_POKEDATA, %d, 0x%lx, %.*s)",
__func__, (int)pid, addr, (int)sizeof(u.buf), u.buf);
debug_return_ssize_t(-1);
}
if ((u.word & 0xff) == 0) {
/* If the last byte we wrote is a NUL we are done. */
debug_return_ssize_t(str - str0 + 1);
}
addr += sizeof(unsigned long);
}
}
#ifdef HAVE_PROCESS_VM_READV
/*
* Write the string vector vec to addr in the tracee which must have
* sufficient space. Strings are written to strtab.
* Returns the number of bytes used in strtab (including NULs).
* process_vm_writev() version.
*/
static ssize_t
ptrace_writev_vec(pid_t pid, struct sudo_ptrace_regs *regs, char **vec,
unsigned long addr, unsigned long strtab)
{
const unsigned long addr0 = addr;
const unsigned long strtab0 = strtab;
unsigned long *addrbuf = NULL;
struct iovec *local, *remote;
struct iovec local_addrs, remote_addrs;
size_t i, j, len, off = 0;
ssize_t expected = -1, nwritten, total_written = 0;
debug_decl(ptrace_writev_vec, SUDO_DEBUG_EXEC);
/* Build up local and remote iovecs for process_vm_writev(2). */
for (len = 0; vec[len] != NULL; len++)
continue;
local = reallocarray(NULL, len, sizeof(struct iovec));
remote = reallocarray(NULL, len, sizeof(struct iovec));
j = regs->compat && (len & 1) != 0; /* pad for final NULL in compat */
addrbuf = reallocarray(NULL, len + 1 + j, regs->wordsize);
if (local == NULL || remote == NULL || addrbuf == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
goto done;
}
for (i = 0, j = 0; i < len; i++) {
unsigned long word = strtab;
/* Store remote string. */
const size_t size = strlen(vec[i]) + 1;
local[i].iov_base = vec[i];
local[i].iov_len = size;
remote[i].iov_base = (void *)strtab;
remote[i].iov_len = size;
strtab += size;
/* Store address of remote string. */
# ifdef SECCOMP_AUDIT_ARCH_COMPAT
if (regs->compat) {
/*
* For compat binaries we need to pack two 32-bit string addresses
* into a single 64-bit word. If this is the last string, NULL
* will be written as the second 32-bit address.
*/
if ((i & 1) == 1) {
/* Wrote this string address last iteration. */
continue;
}
# if BYTE_ORDER == BIG_ENDIAN
word <<= 32;
if (vec[i + 1] != NULL)
word |= strtab;
# else
if (vec[i + 1] != NULL)
word |= strtab << 32;
# endif
}
# endif
addrbuf[j++] = word;
addr += sizeof(unsigned long);
}
if (!regs->compat || (len & 1) == 0) {
addrbuf[j] = 0;
}
/* Write strings addresses to addr0 on remote. */
local_addrs.iov_base = addrbuf;
local_addrs.iov_len = (len + 1) * regs->wordsize;
remote_addrs.iov_base = (void *)addr0;
remote_addrs.iov_len = local_addrs.iov_len;
if (process_vm_writev(pid, &local_addrs, 1, &remote_addrs, 1, 0) == -1)
goto done;
/* Copy the strings to the (remote) string table. */
expected = (ssize_t)(strtab - strtab0);
for (;;) {
nwritten = process_vm_writev(pid, local + off, len - off,
remote + off, len - off, 0);
switch (nwritten) {
case -1:
sudo_debug_printf(
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"process_vm_writev(%d, 0x%lx, %zu, 0x%lx, %zu, 0)",
(int)pid, (unsigned long)local + off, len - off,
(unsigned long)remote + off, len - off);
goto done;
case 0:
sudo_debug_printf(
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"process_vm_writev(%d, 0x%lx, %zu, 0x%lx, %zu, 0): %s",
(int)pid, (unsigned long)local + off, len - off,
(unsigned long)remote + off, len - off,
"zero bytes written");
goto done;
default:
total_written += nwritten;
if (total_written >= expected)
goto done;
/* Adjust offset for partial write (doesn't cross iov boundary). */
while (off < len) {
nwritten -= (ssize_t)local[off].iov_len;
off++;
if (nwritten <= 0)
break;
}
if (off == len) {
sudo_debug_printf(
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"overflow while resuming process_vm_writev()");
goto done;
}
break;
}
}
done:
free(local);
free(remote);
free(addrbuf);
if (total_written == expected)
debug_return_ssize_t(total_written);
debug_return_ssize_t(-1);
}
#endif /* HAVE_PROCESS_VM_READV */
/*
* Write the string vector vec to addr in the tracee which must have
* sufficient space. Strings are written to strtab.
* Returns the number of bytes used in strtab (including NULs).
*/
static ssize_t
ptrace_write_vec(pid_t pid, struct sudo_ptrace_regs *regs, char **vec,
unsigned long addr, unsigned long strtab)
{
const unsigned long strtab0 = strtab;
size_t i;
debug_decl(ptrace_write_vec, SUDO_DEBUG_EXEC);
#ifdef HAVE_PROCESS_VM_READV
ssize_t nwritten = ptrace_writev_vec(pid, regs, vec, addr, strtab);
if (nwritten != -1 || errno != ENOSYS)
debug_return_ssize_t(nwritten);
#endif /* HAVE_PROCESS_VM_READV */
/* Copy string vector into tracee one word at a time. */
for (i = 0; vec[i] != NULL; i++) {
unsigned long word = strtab;
/* First write the actual string to tracee's string table. */
nwritten = ptrace_write_string(pid, strtab, vec[i]);
if (nwritten == -1)
debug_return_ssize_t(-1);
strtab += (size_t)nwritten;
# ifdef SECCOMP_AUDIT_ARCH_COMPAT
if (regs->compat) {
/*
* For compat binaries we need to pack two 32-bit string addresses
* into a single 64-bit word. If this is the last string, NULL
* will be written as the second 32-bit address.
*/
if ((i & 1) == 1) {
/* Wrote this string address last iteration. */
continue;
}
# if BYTE_ORDER == BIG_ENDIAN
word <<= 32;
if (vec[i + 1] != NULL)
word |= strtab;
# else
if (vec[i + 1] != NULL)
word |= strtab << 32;
# endif
}
# endif
/* Next write the string address to tracee at addr. */
if (ptrace(PTRACE_POKEDATA, pid, addr, word) == -1) {
sudo_warn("%s: ptrace(PTRACE_POKEDATA, %d, 0x%lx, 0x%lx)",
__func__, (int)pid, addr, word);
debug_return_ssize_t(-1);
}
addr += sizeof(unsigned long);
}
/* Finally, write the terminating NULL to tracee if needed. */
if (!regs->compat || (i & 1) == 0) {
if (ptrace(PTRACE_POKEDATA, pid, addr, NULL) == -1) {
sudo_warn("%s: ptrace(PTRACE_POKEDATA, %d, 0x%lx, NULL)",
__func__, (int)pid, addr);
debug_return_ssize_t(-1);
}
}
debug_return_ssize_t((ssize_t)(strtab - strtab0));
}
/*
* Read a link from /proc/PID and store the result in buf.
* Used to read the cwd and exe links in /proc/PID.
* Returns true on success, else false.
*/
static bool
proc_read_link(pid_t pid, const char *name, char *buf, size_t bufsize)
{
ssize_t len;
char path[PATH_MAX];
debug_decl(proc_read_link, SUDO_DEBUG_EXEC);
len = snprintf(path, sizeof(path), "/proc/%d/%s", (int)pid, name);
if (len > 0 && len < ssizeof(path)) {
len = readlink(path, buf, bufsize - 1);
if (len != -1) {
/* readlink(2) does not add the NUL for us. */
buf[len] = '\0';
debug_return_bool(true);
}
}
debug_return_bool(false);
}
/*
* Read the filename, argv and envp of the execve(2) system call.
* Returns a dynamically allocated buffer the parent is responsible for.
*/
static char *
get_execve_info(pid_t pid, struct sudo_ptrace_regs *regs, char **pathname_out,
int *argc_out, char ***argv_out, int *envc_out, char ***envp_out)
{
char *argbuf, **argv, **envp, *pathname = NULL;
unsigned long argv_addr, envp_addr, path_addr;
size_t bufsize, off = 0;
int i, argc, envc = 0;
ssize_t nread;
debug_decl(get_execve_info, SUDO_DEBUG_EXEC);
bufsize = PATH_MAX + arg_max;
argbuf = malloc(bufsize);
if (argbuf == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
goto bad;
}
/* execve(2) takes three arguments: pathname, argv, envp. */
path_addr = get_sc_arg1(regs);
argv_addr = get_sc_arg2(regs);
envp_addr = get_sc_arg3(regs);
sudo_debug_printf(SUDO_DEBUG_INFO,
"%s: %d: path 0x%lx, argv 0x%lx, envp 0x%lx", __func__,
(int)pid, path_addr, argv_addr, envp_addr);
/* Read the pathname, if not NULL. */
if (path_addr != 0) {
nread = ptrace_read_string(pid, path_addr, argbuf, bufsize);
if (nread == -1) {
sudo_debug_printf(
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"unable to read execve pathname for process %d", (int)pid);
goto bad;
}
/* Defer setting pathname until after all reallocations are done. */
off = (size_t)nread;
}
/* Read argv */
nread = ptrace_read_vec(pid, regs, argv_addr, &argc, &argv, &argbuf,
&bufsize, off);
if (nread == -1) {
sudo_debug_printf(
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"unable to read execve argv for process %d", (int)pid);
goto bad;
}
off += (size_t)nread;
if (argc == 0) {
/* Reserve an extra slot so we can store argv[0]. */
while (bufsize - off < sizeof(char *)) {
if (!growbuf(&argbuf, &bufsize, NULL, NULL))
goto bad;
}
off += sizeof(char *);
}
/* Read envp */
nread = ptrace_read_vec(pid, regs, envp_addr, &envc, &envp, &argbuf,
&bufsize, off);
if (nread == -1) {
sudo_debug_printf(
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"unable to read execve envp for process %d", (int)pid);
goto bad;
}
/* Set pathname now that argbuf has been fully allocated. */
if (path_addr != 0)
pathname = argbuf;
/* Convert offsets in argv and envp to pointers. */
argv = (char **)(argbuf + (unsigned long)argv);
for (i = 0; i < argc; i++) {
argv[i] = argbuf + (unsigned long)argv[i];
}
envp = (char **)(argbuf + (unsigned long)envp);
for (i = 0; i < envc; i++) {
envp[i] = argbuf + (unsigned long)envp[i];
}
sudo_debug_execve(SUDO_DEBUG_DIAG, pathname, argv, envp);
*pathname_out = pathname;
*argc_out = argc;
*argv_out = argv;
*envc_out = envc;
*envp_out = envp;
debug_return_ptr(argbuf);
bad:
free(argbuf);
debug_return_ptr(NULL);
}
/*
* Cause the current syscall to fail and set the error value to ecode.
*/
static bool
ptrace_fail_syscall(pid_t pid, struct sudo_ptrace_regs *regs, int ecode)
{
sigset_t chldmask;
bool ret = false;
int status;
debug_decl(ptrace_fail_syscall, SUDO_DEBUG_EXEC);
/* Cause the syscall to fail by changing its number to -1. */
set_syscallno(pid, regs, -1);
if (!ptrace_setregs(pid, regs)) {
sudo_warn(U_("unable to set registers for process %d"), (int)pid);
debug_return_bool(false);
}
/* Block SIGCHLD for the critical section (waitpid). */
sigemptyset(&chldmask);
sigaddset(&chldmask, SIGCHLD);
sigprocmask(SIG_BLOCK, &chldmask, NULL);
/* Allow the syscall to continue and change return value to ecode. */
ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
for (;;) {
if (waitpid(pid, &status, __WALL) != -1)
break;
if (errno == EINTR)
continue;
sudo_warn(U_("%s: %s"), __func__, "waitpid");
goto done;
}
if (!WIFSTOPPED(status)) {
sudo_warnx(U_("process %d exited unexpectedly"), (int)pid);
goto done;
}
set_sc_retval(regs, -ecode);
if (!ptrace_setregs(pid, regs)) {
sudo_warn(U_("unable to set registers for process %d"), (int)pid);
goto done;
}
ret = true;
done:
sigprocmask(SIG_UNBLOCK, &chldmask, NULL);
debug_return_bool(ret);
}
/*
* Check whether seccomp(2) filtering supports ptrace(2) traps.
* Only supported by Linux 4.14 and higher.
*/
static bool
have_seccomp_action(const char *action)
{
char line[LINE_MAX];
bool ret = false;
FILE *fp;
debug_decl(have_seccomp_action, SUDO_DEBUG_EXEC);
fp = fopen("/proc/sys/kernel/seccomp/actions_avail", "r");
if (fp != NULL) {
if (fgets(line, sizeof(line), fp) != NULL) {
char *cp, *last;
for ((cp = strtok_r(line, " \t\n", &last)); cp != NULL;
(cp = strtok_r(NULL, " \t\n", &last))) {
if (strcmp(cp, action) == 0) {
ret = true;
break;
}
}
}
fclose(fp);
}
debug_return_bool(ret);
}
/*
* Intercept execve(2) and execveat(2) using seccomp(2) and ptrace(2).
* If no tracer is present, execve(2) and execveat(2) will fail with ENOSYS.
* Must be called with CAP_SYS_ADMIN, before privs are dropped.
*/
bool
set_exec_filter(void)
{
struct sock_filter exec_filter[] = {
/* Load architecture value (AUDIT_ARCH_*) into the accumulator. */
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, arch)),
# ifdef SECCOMP_AUDIT_ARCH_COMPAT2
/* Match on the compat2 architecture or jump to the compat check. */
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SECCOMP_AUDIT_ARCH_COMPAT2, 0, 4),
/* Load syscall number into the accumulator. */
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)),
/* Jump to trace for compat2 execve(2)/execveat(2), else allow. */
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, COMPAT2_execve, 1, 0),
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, COMPAT2_execveat, 0, 13),
/* Trace execve(2)/execveat(2) syscalls (w/ compat flag) */
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRACE | COMPAT_FLAG),
# endif /* SECCOMP_AUDIT_ARCH_COMPAT2 */
# ifdef SECCOMP_AUDIT_ARCH_COMPAT
/* Match on the compat architecture or jump to the native arch check. */
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SECCOMP_AUDIT_ARCH_COMPAT, 0, 4),
/* Load syscall number into the accumulator. */
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)),
/* Jump to trace for compat execve(2)/execveat(2), else allow. */
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, COMPAT_execve, 1, 0),
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, COMPAT_execveat, 0, 8),
/* Trace execve(2)/execveat(2) syscalls (w/ compat flag) */
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRACE | COMPAT_FLAG),
# endif /* SECCOMP_AUDIT_ARCH_COMPAT */
/* Jump to the end unless the architecture matches. */
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SECCOMP_AUDIT_ARCH, 0, 6),
/* Load syscall number into the accumulator. */
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)),
/* Jump to trace for execve(2)/execveat(2), else allow. */
# ifdef X32_execve
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, X32_execve, 3, 0),
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, X32_execveat, 2, 0),
# else
/* No x32 support, check native system call numbers. */
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_execve, 3, 0),
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_execveat, 2, 3),
# endif /* X32_execve */
/* If no x32 support, these two instructions are never reached. */
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_execve, 1, 0),
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_execveat, 0, 1),
/* Trace execve(2)/execveat(2) syscalls */
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRACE),
/* Allow non-matching syscalls */
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW)
};
const struct sock_fprog exec_fprog = {
nitems(exec_filter),
exec_filter
};
debug_decl(set_exec_filter, SUDO_DEBUG_EXEC);
/* We must set SECCOMP_MODE_FILTER before dropping privileges. */
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &exec_fprog) == -1) {
sudo_warn("%s", U_("unable to set seccomp filter"));
debug_return_bool(false);
}
debug_return_bool(true);
}
/*
* Seize control of the specified child process which must be in
* ptrace wait. Returns true on success, false if child is already
* being traced and -1 on error.
*/
int
exec_ptrace_seize(pid_t child)
{
const long ptrace_opts = PTRACE_O_TRACESECCOMP|PTRACE_O_TRACECLONE|
PTRACE_O_TRACEFORK|PTRACE_O_TRACEVFORK|
PTRACE_O_TRACEEXEC;
int ret = -1;
int status;
debug_decl(exec_ptrace_seize, SUDO_DEBUG_EXEC);
#ifdef HAVE_PROCESS_VM_READV
page_size = (size_t)sysconf(_SC_PAGESIZE);
if (page_size == (size_t)-1)
page_size = 4096;
#endif
arg_max = (size_t)sysconf(_SC_ARG_MAX);
if (arg_max == (size_t)-1)
arg_max = 128 * 1024;
/* Seize control of the child process. */
if (ptrace(PTRACE_SEIZE, child, NULL, ptrace_opts) == -1) {
/*
* If the process is already being traced, we will get EPERM.
* We don't treat that as a fatal error since we want it to be
* possible to run sudo inside a sudo shell with intercept enabled.
*/
if (errno != EPERM) {
sudo_warn("%s: ptrace(PTRACE_SEIZE, %d, NULL, 0x%lx)",
__func__, (int)child, ptrace_opts);
goto done;
}
sudo_debug_printf(SUDO_DEBUG_WARN,
"%s: unable to trace process %d, already being traced?",
__func__, (int)child);
ret = false;
}
/* The child is suspended waiting for SIGUSR1, wake it up. */
if (kill(child, SIGUSR1) == -1) {
sudo_warn("kill(%d, SIGUSR1)", (int)child);
goto done;
}
if (!ret)
goto done;
/* Wait for the child to enter trace stop and continue it. */
for (;;) {
if (waitpid(child, &status, __WALL) != -1)
break;
if (errno == EINTR)
continue;
sudo_warn(U_("%s: %s"), __func__, "waitpid");
goto done;
}
if (!WIFSTOPPED(status)) {
sudo_warnx(U_("process %d exited unexpectedly"), (int)child);
goto done;
}
if (ptrace(PTRACE_CONT, child, NULL, (void *)SIGUSR1) == -1) {
sudo_warn("%s: ptrace(PTRACE_CONT, %d, NULL, SIGUSR1)",
__func__, (int)child);
goto done;
}
ret = true;
done:
debug_return_int(ret);
}
/*
* Compare two pathnames. If do_stat is true, fall back to stat(2)ing
* the paths for a dev/inode match if the strings don't match.
* Returns true on match, else false.
*/
static bool
pathname_matches(const char *path1, const char *path2, bool do_stat)
{
struct stat sb1, sb2;
debug_decl(pathname_matches, SUDO_DEBUG_EXEC);
sudo_debug_printf(SUDO_DEBUG_INFO, "%s: compare %s to %s", __func__,
path1 ? path1 : "(NULL)", path2 ? path2 : "(NULL)");
if (path1 == NULL || path2 == NULL)
debug_return_bool(false);
if (strcmp(path1, path2) == 0)
debug_return_bool(true);
if (do_stat && stat(path1, &sb1) == 0 && stat(path2, &sb2) == 0) {
if (sb1.st_dev == sb2.st_dev && sb1.st_ino == sb2.st_ino)
debug_return_bool(true);
}
debug_return_bool(false);
}
/*
* Open script and check for '#!' magic number followed by an interpreter.
* If present, check the interpreter against execpath, and argument string
* (if any) against argv[1].
* Returns number of argv entries to skip on success, else 0.
*/
static int
script_matches(const char *script, const char *execpath, int argc,
char * const *argv)
{
char * const *orig_argv = argv;
size_t linesize = 0;
char *interp, *interp_args, *line = NULL;
char magic[2];
int count;
FILE *fp = NULL;
ssize_t len;
debug_decl(get_interpreter, SUDO_DEBUG_EXEC);
/* Linux allows up to 4 nested interpreters. */
for (count = 0; count < 4; count++) {
if (fp != NULL)
fclose(fp);
fp = fopen(script, "r");
if (fp == NULL) {
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_ERRNO,
"%s: unable to open %s for reading", __func__, script);
goto done;
}
if (fread(magic, 1, 2, fp) != 2 || memcmp(magic, "#!", 2) != 0) {
sudo_debug_printf(SUDO_DEBUG_DEBUG, "%s: %s: not a script",
__func__, script);
goto done;
}
/* Check interpreter, skipping the shebang and trim trailing space. */
len = getdelim(&line, &linesize, '\n', fp);
if (len == -1) {
sudo_debug_printf(SUDO_DEBUG_ERROR, "%s: %s: can't get interpreter",
__func__, script);
goto done;
}
while (len > 0 && isspace((unsigned char)line[len - 1])) {
len--;
line[len] = '\0';
}
sudo_debug_printf(SUDO_DEBUG_DEBUG, "%s: %s: shebang line \"%s\"",
__func__, script, line);
/*
* Split line into interpreter and args.
* Whitespace is not supported in the interpreter path.
*/
for (interp = line; isspace((unsigned char)*interp); interp++)
continue;
interp_args = strpbrk(interp, " \t");
if (interp_args != NULL) {
*interp_args++ = '\0';
while (isspace((unsigned char)*interp_args))
interp_args++;
}
sudo_debug_printf(SUDO_DEBUG_INFO, "%s: interpreter %s, args \"%s\"",
__func__, interp, interp_args ? interp_args : "");
/* Match interpreter. */
if (!pathname_matches(execpath, interp, true)) {
/* It is possible for the interpreter to be a script too. */
if (argv > 0 && strcmp(interp, argv[1]) == 0) {
/* Interpreter args must match for *this* interpreter. */
if (interp_args == NULL ||
(argc > 1 && strcmp(interp_args, argv[2]) == 0)) {
script = interp;
argv++;
argc--;
if (interp_args != NULL) {
argv++;
argc--;
}
/* Check whether interp is itself a script. */
continue;
}
}
}
if (argc > 0 && interp_args != NULL) {
if (strcmp(interp_args, argv[1]) != 0) {
sudo_warnx(
U_("interpreter argument , expected \"%s\", got \"%s\""),
interp_args, argc > 1 ? argv[1] : "(NULL)");
goto done;
}
argv++;
}
argv++;
break;
}
done:
free(line);
if (fp != NULL)
fclose(fp);
debug_return_int((int)(argv - orig_argv));
}
static ssize_t
proc_read_vec(pid_t pid, const char *name, int *countp, char ***vecp,
char **bufp, size_t *bufsizep, size_t off)
{
size_t strtab_len, remainder = *bufsizep - off;
char path[PATH_MAX], *strtab = *bufp + off;
ssize_t len, nread;
int fd;
debug_decl(proc_read_vec, SUDO_DEBUG_EXEC);
len = snprintf(path, sizeof(path), "/proc/%d/%s", (int)pid, name);
if (len >= ssizeof(path))
debug_return_ssize_t(-1);
fd = open(path, O_RDONLY);
if (fd == -1)
debug_return_ssize_t(-1);
/* Read in strings until EOF. */
do {
nread = read(fd, strtab, remainder);
if (nread == -1) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
"%s: unable to read %s", __func__, path);
close(fd);
debug_return_ssize_t(-1);
}
strtab += nread;
remainder -= (size_t)nread;
if (remainder < sizeof(char *)) {
while (!growbuf(bufp, bufsizep, &strtab, &remainder)) {
close(fd);
debug_return_ssize_t(-1);
}
}
} while (nread != 0);
close(fd);
/* Trim off the extra NUL byte at the end of the string table. */
if (strtab - *bufp >= 2 && strtab[-1] == '\0' && strtab[-2] == '\0') {
strtab--;
remainder++;
}
/* Store strings in a vector after the string table. */
strtab_len = (size_t)(strtab - (*bufp + off));
strtab = *bufp + off;
len = strtab_to_vec(strtab, strtab_len, countp, vecp, bufp, bufsizep,
remainder);
if (len == -1)
debug_return_ssize_t(-1);
debug_return_ssize_t((ssize_t)strtab_len + len);
}
/*
* Check if the execve(2) arguments match the contents of closure.
* Returns true if they match, else false.
*/
static bool
execve_args_match(const char *pathname, int argc, char * const *argv,
int envc, char * const *envp, bool do_stat,
struct intercept_closure *closure)
{
bool ret = true;
int i;
debug_decl(execve_args_match, SUDO_DEBUG_EXEC);
if (!pathname_matches(pathname, closure->command, do_stat)) {
/* For scripts, pathname will refer to the interpreter instead. */
if (do_stat) {
int skip = script_matches(closure->command, pathname,
argc, argv);
if (skip != 0) {
/* Skip interpreter (and args) in argv. */
argv += skip;
argc -= skip;
goto check_argv;
}
}
sudo_warnx(
U_("pathname mismatch, expected \"%s\", got \"%s\""),
closure->command, pathname ? pathname : "(NULL)");
ret = false;
}
check_argv:
for (i = 0; i < argc; i++) {
if (closure->run_argv[i] == NULL) {
ret = false;
sudo_warnx(
U_("%s[%d] mismatch, expected \"%s\", got \"%s\""),
"argv", i, "(NULL)", argv[i] ? argv[i] : "(NULL)");
break;
}
if (argv[i] == NULL) {
ret = false;
sudo_warnx(
U_("%s[%d] mismatch, expected \"%s\", got \"%s\""),
"argv", i, closure->run_argv[i], "(NULL)");
break;
}
if (strcmp(argv[i], closure->run_argv[i]) != 0) {
if (i == 0) {
/* Special case for argv[0] which may contain the basename. */
const char *base;
if (argv[0][0] == '/') {
if (closure->run_argv[0][0] != '/') {
base = sudo_basename(argv[0]);
if (strcmp(base, closure->run_argv[0]) == 0)
continue;
}
} else {
if (closure->run_argv[0][0] == '/') {
base = sudo_basename(closure->run_argv[0]);
if (strcmp(argv[0], base) == 0)
continue;
}
}
}
ret = false;
sudo_warnx(
U_("%s[%d] mismatch, expected \"%s\", got \"%s\""),
"argv", i, closure->run_argv[i], argv[i]);
}
}
for (i = 0; i < envc; i++) {
if (closure->run_envp[i] == NULL) {
ret = false;
sudo_warnx(
U_("%s[%d] mismatch, expected \"%s\", got \"%s\""),
"envp", i, "(NULL)", envp[i] ? envp[i] : "(NULL)");
break;
} else if (envp[i] == NULL) {
ret = false;
sudo_warnx(
U_("%s[%d] mismatch, expected \"%s\", got \"%s\""),
"envp", i, closure->run_envp[i], "(NULL)");
break;
} else if (strcmp(envp[i], closure->run_envp[i]) != 0) {
ret = false;
sudo_warnx(
U_("%s[%d] mismatch, expected \"%s\", got \"%s\""),
"envp", i, closure->run_envp[i], envp[i]);
}
}
debug_return_bool(ret);
}
/*
* Verify that the execve(2) argument we wrote match the contents of closure.
* Returns true if they match, else false.
*/
static bool
verify_execve_args(pid_t pid, struct sudo_ptrace_regs *regs,
struct intercept_closure *closure)
{
char *pathname, **argv, **envp, *buf;
int argc, envc;
bool ret = false;
debug_decl(verify_execve_args, SUDO_DEBUG_EXEC);
buf = get_execve_info(pid, regs, &pathname, &argc, &argv,
&envc, &envp);
if (buf != NULL) {
ret = execve_args_match(pathname, argc, argv, envc, envp, false, closure);
free(buf);
}
debug_return_bool(ret);
}
/*
* Verify that the command executed matches the arguments we checked.
* Returns true on success and false on error.
*/
static bool
ptrace_verify_post_exec(pid_t pid, struct sudo_ptrace_regs *regs,
struct intercept_closure *closure)
{
char **argv, **envp, *argbuf = NULL;
char pathname[PATH_MAX];
sigset_t chldmask;
bool ret = false;
int argc, envc, i, status;
size_t bufsize;
ssize_t len;
debug_decl(ptrace_verify_post_exec, SUDO_DEBUG_EXEC);
/* Block SIGCHLD for the critical section (waitpid). */
sigemptyset(&chldmask);
sigaddset(&chldmask, SIGCHLD);
sigprocmask(SIG_BLOCK, &chldmask, NULL);
/* Allow execve(2) to continue and wait for PTRACE_EVENT_EXEC. */
ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
for (;;) {
if (waitpid(pid, &status, __WALL) != -1)
break;
if (errno == EINTR)
continue;
sudo_warn(U_("%s: %s"), __func__, "waitpid");
goto done;
}
if (!WIFSTOPPED(status)) {
sudo_warnx(U_("process %d exited unexpectedly"), (int)pid);
goto done;
}
if (status >> 8 != (SIGTRAP | (PTRACE_EVENT_EXEC << 8))) {
sudo_warnx(U_("process %d unexpected status 0x%x"), (int)pid, status);
goto done;
}
/* Get the executable path. */
if (!proc_read_link(pid, "exe", pathname, sizeof(pathname))) {
/* Missing /proc file system is not a fatal error. */
sudo_debug_printf(SUDO_DEBUG_ERROR, "%s: unable to read /proc/%d/exe",
__func__, (int)pid);
ret = true;
goto done;
}
sudo_debug_printf(SUDO_DEBUG_INFO, "%s: %d: verify %s", __func__,
(int)pid, pathname);
/* Allocate a single buffer for argv, envp and their strings. */
bufsize = arg_max;
argbuf = malloc(bufsize);
if (argbuf == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
goto done;
}
len = proc_read_vec(pid, "cmdline", &argc, &argv, &argbuf, &bufsize, 0);
if (len == -1) {
sudo_debug_printf(
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"unable to read execve argv for process %d", (int)pid);
goto done;
}
len = proc_read_vec(pid, "environ", &envc, &envp, &argbuf, &bufsize,
(size_t)len);
if (len == -1) {
sudo_debug_printf(
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"unable to read execve envp for process %d", (int)pid);
goto done;
}
/* Convert offsets in argv and envp to pointers. */
argv = (char **)(argbuf + (unsigned long)argv);
for (i = 0; i < argc; i++) {
argv[i] = argbuf + (unsigned long)argv[i];
}
envp = (char **)(argbuf + (unsigned long)envp);
for (i = 0; i < envc; i++) {
envp[i] = argbuf + (unsigned long)envp[i];
}
ret = execve_args_match(pathname, argc, argv, envc, envp, true, closure);
if (!ret) {
sudo_debug_printf(SUDO_DEBUG_ERROR,
"%s: %d new execve args don't match closure", __func__, (int)pid);
}
done:
free(argbuf);
sigprocmask(SIG_UNBLOCK, &chldmask, NULL);
debug_return_bool(ret);
}
/*
* Intercept execve(2) and perform a policy check.
* Reads current registers and execve(2) arguments.
* If the command is not allowed by policy, fail with EACCES.
* If the command is allowed, update argv if needed before continuing.
* Returns true on success and false on error.
*/
static bool
ptrace_intercept_execve(pid_t pid, struct intercept_closure *closure)
{
char *pathname, **argv, **envp, *buf;
const unsigned int flags = closure->details->flags;
int argc, envc, syscallno;
struct sudo_ptrace_regs regs;
bool path_mismatch = false;
bool argv_mismatch = false;
char cwd[PATH_MAX], *orig_argv0;
unsigned long msg;
bool ret = false;
int i, oldcwd = -1;
debug_decl(ptrace_intercept_execve, SUDO_DEBUG_EXEC);
/* Do not check the policy if we are executing the initial command. */
if (closure->initial_command != 0) {
closure->initial_command--;
debug_return_bool(true);
}
/* Get compat flag. */
if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, &msg) == -1) {
sudo_warn(U_("unable to get event message for process %d"), (int)pid);
debug_return_bool(false);
}
/* Get the registers. */
memset(®s, 0, sizeof(regs));
if (!ptrace_getregs(pid, ®s, msg)) {
sudo_warn(U_("unable to get registers for process %d"), (int)pid);
debug_return_bool(false);
}
sudo_debug_printf(SUDO_DEBUG_INFO, "%s: %d: compat: %s, wordsize: %u",
__func__, (int)pid, regs.compat ? "true" : "false", regs.wordsize);
# ifdef SECCOMP_AUDIT_ARCH_COMPAT
if (regs.compat) {
syscallno = get_syscallno(®s);
switch (syscallno) {
case COMPAT_execve:
/* Handled below. */
break;
case COMPAT_execveat:
/* We don't currently check execveat(2). */
debug_return_bool(true);
break;
default:
sudo_warnx("%s: unexpected compat system call %d",
__func__, syscallno);
debug_return_bool(false);
}
} else
# endif /* SECCOMP_AUDIT_ARCH_COMPAT */
{
syscallno = get_syscallno(®s);
switch (syscallno) {
# ifdef X32_execve
case X32_execve:
# endif
case __NR_execve:
/* Handled below. */
break;
# ifdef X32_execveat
case X32_execveat:
# endif
case __NR_execveat:
/* We don't currently check execveat(2). */
debug_return_bool(true);
break;
default:
sudo_warnx("%s: unexpected system call %d", __func__, syscallno);
debug_return_bool(false);
}
}
/* Get the current working directory and execve info. */
if (!proc_read_link(pid, "cwd", cwd, sizeof(cwd)))
(void)strlcpy(cwd, "unknown", sizeof(cwd));
buf = get_execve_info(pid, ®s, &pathname, &argc, &argv,
&envc, &envp);
if (buf == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
"%s: %d: unable to get execve info", __func__, (int)pid);
/* EIO from ptrace is like EFAULT from the kernel. */
if (errno == EIO)
errno = EFAULT;
ptrace_fail_syscall(pid, ®s, errno);
goto done;
}
/* Must have a pathname. */
if (pathname == NULL) {
ptrace_fail_syscall(pid, ®s, EINVAL);
goto done;
}
/* We can only pass the pathname to exececute via argv[0] (plugin API). */
orig_argv0 = argv[0] ? argv[0] : (char *)"";
argv[0] = pathname;
if (argc == 0) {
/* Rewrite an empty argv[] with the path to execute. */
argv[1] = NULL;
argc = 1;
argv_mismatch = true;
}
/* Perform a policy check. */
sudo_debug_printf(SUDO_DEBUG_INFO, "%s: %d: checking policy for %s",
__func__, (int)pid, pathname);
if (!intercept_check_policy(pathname, argc, argv, envc, envp, cwd,
&oldcwd, closure)) {
if (closure->errstr != NULL)
sudo_warnx("%s", U_(closure->errstr));
}
switch (closure->state) {
case POLICY_TEST:
path_mismatch = true;
argv_mismatch = true;
if (closure->command == NULL)
closure->command = pathname;
if (closure->run_argv == NULL)
closure->run_argv = argv;
if (closure->run_envp == NULL)
closure->run_envp = envp;
FALLTHROUGH;
case POLICY_ACCEPT:
/*
* Update pathname and argv if the policy modified it.
* We don't currently ever modify envp.
*/
if (strcmp(pathname, closure->command) != 0)
path_mismatch = true;
if (!path_mismatch) {
/* Path unchanged, restore original argv[0]. */
if (strcmp(argv[0], orig_argv0) != 0) {
argv[0] = orig_argv0;
free(closure->run_argv[0]);
closure->run_argv[0] = strdup(orig_argv0);
if (closure->run_argv[0] == NULL) {
sudo_warnx(U_("%s: %s"), __func__,
U_("unable to allocate memory"));
}
}
}
for (i = 0; closure->run_argv[i] != NULL && argv[i] != NULL; i++) {
if (strcmp(closure->run_argv[i], argv[i]) != 0) {
argv_mismatch = true;
break;
}
}
if (closure->run_argv[i] != NULL || argv[i] != NULL)
argv_mismatch = true;
if (path_mismatch || argv_mismatch) {
/*
* Need to rewrite pathname and/or argv.
* We can use space below the stack pointer to store the data.
* On amd64 there is a 128 byte red zone that must be avoided.
* Note: on pa-risc the stack grows up, not down.
*/
unsigned long sp = get_stack_pointer(®s) - 128;
unsigned long strtab;
size_t space = 0;
ssize_t nwritten;
sudo_debug_execve(SUDO_DEBUG_DIAG, closure->command,
closure->run_argv, envp);
/*
* Calculate the amount of space required for pointers + strings.
* Since ptrace(2) always writes in sizeof(long) increments we
* need to be careful to avoid overwriting what we have already
* written for compat binaries (where the word size doesn't match).
*
* This is mostly a problem for the string table since we do
* interleaved writes of the argument vector pointers and the
* strings they refer to. For native binaries, it is sufficient
* to align the string table on a word boundary. For compat
* binaries, if argc is odd, writing the last pointer will overlap
* the first string so leave an extra word in between them.
*/
if (argv_mismatch) {
/* argv pointers */
space += ((size_t)argc + 1 + regs.compat) * regs.wordsize;
/* argv strings */
for (argc = 0; closure->run_argv[argc] != NULL; argc++) {
space += strlen(closure->run_argv[argc]) + 1;
}
}
if (path_mismatch) {
/* pathname string */
space += strlen(closure->command) + 1;
}
/* Reserve stack space for path, argv (w/ NULL) and its strings. */
sp -= WORDALIGN(space, regs);
strtab = sp;
if (argv_mismatch) {
/* Update argv address in the tracee to our new value. */
set_sc_arg2(®s, sp);
/* Skip over argv pointers (plus NULL) for string table. */
strtab += ((size_t)argc + 1 + regs.compat) * regs.wordsize;
nwritten = ptrace_write_vec(pid, ®s, closure->run_argv,
sp, strtab);
if (nwritten == -1)
goto done;
strtab += (unsigned long)nwritten;
}
if (path_mismatch) {
/* Update pathname address in the tracee to our new value. */
set_sc_arg1(®s, strtab);
/* Write pathname to the string table. */
nwritten = ptrace_write_string(pid, strtab, closure->command);
if (nwritten == -1)
goto done;
}
/* Update args in the tracee to the new values. */
if (!ptrace_setregs(pid, ®s)) {
sudo_warn(U_("unable to set registers for process %d"),
(int)pid);
goto done;
}
if (closure->state == POLICY_TEST) {
/* Verify the contents of what we just wrote. */
if (!verify_execve_args(pid, ®s, closure)) {
sudo_debug_printf(SUDO_DEBUG_ERROR,
"%s: new execve args don't match closure", __func__);
}
}
}
if (closure->state == POLICY_ACCEPT && ISSET(flags, CD_INTERCEPT)) {
if (ISSET(flags, CD_INTERCEPT_VERIFY)) {
/* Verify execve(2) args post-exec. */
if (!ptrace_verify_post_exec(pid, ®s, closure)) {
if (errno != ESRCH)
kill(pid, SIGKILL);
}
}
}
break;
case POLICY_REJECT:
/* If rejected, fake the syscall and set return to EACCES */
errno = EACCES;
FALLTHROUGH;
default:
ptrace_fail_syscall(pid, ®s, errno);
break;
}
ret = true;
done:
if (oldcwd != -1) {
if (fchdir(oldcwd) == -1) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
"%s: unable to restore saved cwd", __func__);
}
close(oldcwd);
}
free(buf);
intercept_closure_reset(closure);
debug_return_bool(ret);
}
/*
* Handle a process stopped due to ptrace.
* Restarts the tracee with PTRACE_LISTEN (for a group-stop)
* or PTRACE_CONT (for signal-delivery-stop).
* Returns true if stopped by a group-stop, else false.
*/
bool
exec_ptrace_stopped(pid_t pid, int status, void *intercept)
{
struct intercept_closure *closure = intercept;
const int stopsig = WSTOPSIG(status);
const int sigtrap = status >> 8;
long signo = 0;
bool group_stop = false;
debug_decl(exec_ptrace_stopped, SUDO_DEBUG_EXEC);
if (sigtrap == (SIGTRAP | (PTRACE_EVENT_SECCOMP << 8))) {
if (!ptrace_intercept_execve(pid, closure)) {
sudo_debug_printf(SUDO_DEBUG_ERROR,
"%s: %d failed to intercept execve", __func__, (int)pid);
}
} else if (sigtrap == (SIGTRAP | (PTRACE_EVENT_EXEC << 8))) {
sudo_debug_printf(SUDO_DEBUG_ERROR,
"%s: %d PTRACE_EVENT_EXEC", __func__, (int)pid);
} else if (sigtrap == (SIGTRAP | (PTRACE_EVENT_CLONE << 8)) ||
sigtrap == (SIGTRAP | (PTRACE_EVENT_VFORK << 8)) ||
sigtrap == (SIGTRAP | (PTRACE_EVENT_FORK << 8))) {
unsigned long new_pid;
/* New child process, it will inherit the parent's trace flags. */
if (sudo_debug_needed(SUDO_DEBUG_INFO)) {
if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, &new_pid) != -1) {
sudo_debug_printf(SUDO_DEBUG_INFO,
"%s: %d forked new child %lu", __func__, (int)pid, new_pid);
} else {
sudo_debug_printf(
SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"ptrace(PTRACE_GETEVENTMSG, %d, NULL, %p)", (int)pid,
&new_pid);
}
}
} else {
switch (stopsig) {
case SIGSTOP:
case SIGTSTP:
case SIGTTIN:
case SIGTTOU:
/* Is this a group-stop? */
if (status >> 16 == PTRACE_EVENT_STOP) {
/* Group-stop, do not deliver signal. */
sudo_debug_printf(SUDO_DEBUG_INFO,
"%s: %d: group-stop signal %d",
__func__, (int)pid, stopsig);
group_stop = true;
break;
}
FALLTHROUGH;
default:
/* Signal-delivery-stop, deliver signal. */
sudo_debug_printf(SUDO_DEBUG_INFO,
"%s: %d: signal-delivery-stop signal %d",
__func__, (int)pid, stopsig);
signo = stopsig;
break;
}
}
if (group_stop) {
/*
* Restart child but prevent it from executing
* until SIGCONT is received (simulate SIGSTOP, etc).
*/
if (ptrace(PTRACE_LISTEN, pid, NULL, 0L) == -1 && errno != ESRCH)
sudo_warn("%s: ptrace(PTRACE_LISTEN, %d, NULL, 0L)",
__func__, (int)pid);
} else {
/* Restart child immediately. */
if (ptrace(PTRACE_CONT, pid, NULL, signo) == -1 && errno != ESRCH)
sudo_warn("%s: ptrace(PTRACE_CONT, %d, NULL, %ld)",
__func__, (int)pid, signo);
}
debug_return_bool(group_stop);
}
bool
exec_ptrace_intercept_supported(void)
{
# ifdef __mips__
/* MIPS doesn't support changing the syscall return value. */
return false;
# else
if (seccomp_trap_supported == -1)
seccomp_trap_supported = have_seccomp_action("trap");
return seccomp_trap_supported == true;
# endif
}
bool
exec_ptrace_subcmds_supported(void)
{
if (seccomp_trap_supported == -1)
seccomp_trap_supported = have_seccomp_action("trap");
return seccomp_trap_supported == true;
}
#else
/* STUB */
bool
exec_ptrace_stopped(pid_t pid, int status, void *intercept)
{
return true;
}
/* STUB */
int
exec_ptrace_seize(pid_t child)
{
return true;
}
/* STUB */
bool
exec_ptrace_intercept_supported(void)
{
return false;
}
/* STUB */
bool
exec_ptrace_subcmds_supported(void)
{
return false;
}
#endif /* HAVE_PTRACE_INTERCEPT */
/*
* Adjust flags based on the availability of ptrace support.
*/
void
exec_ptrace_fix_flags(struct command_details *details)
{
debug_decl(exec_ptrace_fix_flags, SUDO_DEBUG_EXEC);
if (ISSET(details->flags, CD_USE_PTRACE)) {
/* If both CD_INTERCEPT and CD_LOG_SUBCMDS set, CD_INTERCEPT wins. */
if (ISSET(details->flags, CD_INTERCEPT)) {
if (!exec_ptrace_intercept_supported())
CLR(details->flags, CD_USE_PTRACE);
} else if (ISSET(details->flags, CD_LOG_SUBCMDS)) {
if (!exec_ptrace_subcmds_supported())
CLR(details->flags, CD_USE_PTRACE);
} else {
CLR(details->flags, CD_USE_PTRACE);
}
}
debug_return;
}
|