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
|
/*
* Copyright (c) 1982, 1986, 1989, 1990, 1991, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
* Copyright (c) 2000-2001 Robert N. M. Watson. All rights reserved.
*
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
* @(#)kern_prot.c 8.6 (Berkeley) 1/21/94
* $FreeBSD$
*/
/*
* System calls related to processes and protection
*/
#include "opt_compat.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/acct.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/sx.h>
#include <sys/proc.h>
#include <sys/sysproto.h>
#include <sys/jail.h>
#include <sys/pioctl.h>
#include <sys/resourcevar.h>
#include <sys/sysctl.h>
static MALLOC_DEFINE(M_CRED, "cred", "credentials");
SYSCTL_DECL(_security);
SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0,
"BSD security policy");
#ifndef _SYS_SYSPROTO_H_
struct getpid_args {
int dummy;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
getpid(td, uap)
struct thread *td;
struct getpid_args *uap;
{
struct proc *p = td->td_proc;
int s;
s = mtx_lock_giant(kern_giant_proc);
td->td_retval[0] = p->p_pid;
#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
PROC_LOCK(p);
td->td_retval[1] = p->p_pptr->p_pid;
PROC_UNLOCK(p);
#endif
mtx_unlock_giant(s);
return (0);
}
#ifndef _SYS_SYSPROTO_H_
struct getppid_args {
int dummy;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
getppid(td, uap)
struct thread *td;
struct getppid_args *uap;
{
struct proc *p = td->td_proc;
int s;
s = mtx_lock_giant(kern_giant_proc);
PROC_LOCK(p);
td->td_retval[0] = p->p_pptr->p_pid;
PROC_UNLOCK(p);
mtx_unlock_giant(s);
return (0);
}
/*
* Get process group ID; note that POSIX getpgrp takes no parameter.
*/
#ifndef _SYS_SYSPROTO_H_
struct getpgrp_args {
int dummy;
};
#endif
/*
* MPSAFE
*/
int
getpgrp(td, uap)
struct thread *td;
struct getpgrp_args *uap;
{
struct proc *p = td->td_proc;
int s;
s = mtx_lock_giant(kern_giant_proc);
PROC_LOCK(p);
td->td_retval[0] = p->p_pgrp->pg_id;
PROC_UNLOCK(p);
mtx_unlock_giant(s);
return (0);
}
/* Get an arbitary pid's process group id */
#ifndef _SYS_SYSPROTO_H_
struct getpgid_args {
pid_t pid;
};
#endif
/*
* MPSAFE
*/
int
getpgid(td, uap)
struct thread *td;
struct getpgid_args *uap;
{
struct proc *p = td->td_proc;
struct proc *pt;
int error, s;
s = mtx_lock_giant(kern_giant_proc);
error = 0;
if (uap->pid == 0) {
PROC_LOCK(p);
td->td_retval[0] = p->p_pgrp->pg_id;
PROC_UNLOCK(p);
} else if ((pt = pfind(uap->pid)) == NULL)
error = ESRCH;
else {
error = p_cansee(p, pt);
if (error == 0)
td->td_retval[0] = pt->p_pgrp->pg_id;
PROC_UNLOCK(pt);
}
mtx_unlock_giant(s);
return (error);
}
/*
* Get an arbitary pid's session id.
*/
#ifndef _SYS_SYSPROTO_H_
struct getsid_args {
pid_t pid;
};
#endif
/*
* MPSAFE
*/
int
getsid(td, uap)
struct thread *td;
struct getsid_args *uap;
{
struct proc *p = td->td_proc;
struct proc *pt;
int error;
int s;
s = mtx_lock_giant(kern_giant_proc);
error = 0;
if (uap->pid == 0) {
PROC_LOCK(p);
td->td_retval[0] = p->p_session->s_sid;
PROC_UNLOCK(p);
} else if ((pt = pfind(uap->pid)) == NULL)
error = ESRCH;
else {
error = p_cansee(p, pt);
if (error == 0)
td->td_retval[0] = pt->p_session->s_sid;
PROC_UNLOCK(pt);
}
mtx_unlock_giant(s);
return (error);
}
#ifndef _SYS_SYSPROTO_H_
struct getuid_args {
int dummy;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
getuid(td, uap)
struct thread *td;
struct getuid_args *uap;
{
struct proc *p = td->td_proc;
mtx_lock(&Giant);
td->td_retval[0] = p->p_ucred->cr_ruid;
#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
td->td_retval[1] = p->p_ucred->cr_uid;
#endif
mtx_unlock(&Giant);
return (0);
}
#ifndef _SYS_SYSPROTO_H_
struct geteuid_args {
int dummy;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
geteuid(td, uap)
struct thread *td;
struct geteuid_args *uap;
{
mtx_lock(&Giant);
td->td_retval[0] = td->td_proc->p_ucred->cr_uid;
mtx_unlock(&Giant);
return (0);
}
#ifndef _SYS_SYSPROTO_H_
struct getgid_args {
int dummy;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
getgid(td, uap)
struct thread *td;
struct getgid_args *uap;
{
struct proc *p = td->td_proc;
mtx_lock(&Giant);
td->td_retval[0] = p->p_ucred->cr_rgid;
#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
td->td_retval[1] = p->p_ucred->cr_groups[0];
#endif
mtx_unlock(&Giant);
return (0);
}
/*
* Get effective group ID. The "egid" is groups[0], and could be obtained
* via getgroups. This syscall exists because it is somewhat painful to do
* correctly in a library function.
*/
#ifndef _SYS_SYSPROTO_H_
struct getegid_args {
int dummy;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
getegid(td, uap)
struct thread *td;
struct getegid_args *uap;
{
struct proc *p = td->td_proc;
mtx_lock(&Giant);
td->td_retval[0] = p->p_ucred->cr_groups[0];
mtx_unlock(&Giant);
return (0);
}
#ifndef _SYS_SYSPROTO_H_
struct getgroups_args {
u_int gidsetsize;
gid_t *gidset;
};
#endif
/*
* MPSAFE
*/
int
getgroups(td, uap)
struct thread *td;
register struct getgroups_args *uap;
{
struct ucred *cred;
struct proc *p = td->td_proc;
u_int ngrp;
int error;
mtx_lock(&Giant);
error = 0;
cred = p->p_ucred;
if ((ngrp = uap->gidsetsize) == 0) {
td->td_retval[0] = cred->cr_ngroups;
goto done2;
}
if (ngrp < cred->cr_ngroups) {
error = EINVAL;
goto done2;
}
ngrp = cred->cr_ngroups;
if ((error = copyout((caddr_t)cred->cr_groups,
(caddr_t)uap->gidset, ngrp * sizeof(gid_t))))
goto done2;
td->td_retval[0] = ngrp;
done2:
mtx_unlock(&Giant);
return (error);
}
#ifndef _SYS_SYSPROTO_H_
struct setsid_args {
int dummy;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
setsid(td, uap)
register struct thread *td;
struct setsid_args *uap;
{
struct pgrp *pgrp;
int error;
struct proc *p = td->td_proc;
struct pgrp *newpgrp;
struct session *newsess;
error = 0;
pgrp = NULL;
mtx_lock(&Giant);
MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
MALLOC(newsess, struct session *, sizeof(struct session), M_SESSION, M_WAITOK | M_ZERO);
PGRPSESS_XLOCK();
if (p->p_pgid == p->p_pid || (pgrp = pgfind(p->p_pid)) != NULL) {
if (pgrp != NULL)
PGRP_UNLOCK(pgrp);
error = EPERM;
goto fail;
} else {
(void)enterpgrp(p, p->p_pid, newpgrp, newsess);
td->td_retval[0] = p->p_pid;
error = 0;
}
PGRPSESS_XUNLOCK();
mtx_unlock(&Giant);
return (0);
fail:
PGRPSESS_XUNLOCK();
FREE(newpgrp, M_PGRP);
FREE(newsess, M_SESSION);
mtx_unlock(&Giant);
return (0);
}
/*
* set process group (setpgid/old setpgrp)
*
* caller does setpgid(targpid, targpgid)
*
* pid must be caller or child of caller (ESRCH)
* if a child
* pid must be in same session (EPERM)
* pid can't have done an exec (EACCES)
* if pgid != pid
* there must exist some pid in same session having pgid (EPERM)
* pid must not be session leader (EPERM)
*/
#ifndef _SYS_SYSPROTO_H_
struct setpgid_args {
int pid; /* target process id */
int pgid; /* target pgrp id */
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
setpgid(td, uap)
struct thread *td;
register struct setpgid_args *uap;
{
struct proc *curp = td->td_proc;
register struct proc *targp; /* target process */
register struct pgrp *pgrp; /* target pgrp */
int error;
struct pgrp *newpgrp;
if (uap->pgid < 0)
return (EINVAL);
error = 0;
mtx_lock(&Giant);
MALLOC(newpgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
PGRPSESS_XLOCK();
if (uap->pid != 0 && uap->pid != curp->p_pid) {
sx_slock(&proctree_lock);
if ((targp = pfind(uap->pid)) == NULL) {
if (targp)
PROC_UNLOCK(targp);
sx_sunlock(&proctree_lock);
error = ESRCH;
goto fail;
}
if (!inferior(targp)) {
PROC_UNLOCK(targp);
sx_sunlock(&proctree_lock);
error = ESRCH;
goto fail;
}
sx_sunlock(&proctree_lock);
if ((error = p_cansee(curproc, targp))) {
PROC_UNLOCK(targp);
goto fail;
}
if (targp->p_pgrp == NULL ||
targp->p_session != curp->p_session) {
PROC_UNLOCK(targp);
error = EPERM;
goto fail;
}
if (targp->p_flag & P_EXEC) {
PROC_UNLOCK(targp);
error = EACCES;
goto fail;
}
PROC_UNLOCK(targp);
} else
targp = curp;
if (SESS_LEADER(targp)) {
error = EPERM;
goto fail;
}
if (uap->pgid == 0)
uap->pgid = targp->p_pid;
if (uap->pgid == targp->p_pid) {
if (targp->p_pgid == uap->pgid)
goto done;
error = enterpgrp(targp, uap->pgid, newpgrp, NULL);
if (error == 0)
newpgrp = NULL;
} else {
if ((pgrp = pgfind(uap->pgid)) == NULL ||
pgrp->pg_session != curp->p_session) {
if (pgrp != NULL)
PGRP_UNLOCK(pgrp);
error = EPERM;
goto fail;
}
if (pgrp == targp->p_pgrp) {
PGRP_UNLOCK(pgrp);
goto done;
}
PGRP_UNLOCK(pgrp);
error = enterthispgrp(targp, pgrp);
}
done:
PGRPSESS_XUNLOCK();
if (newpgrp != NULL)
FREE(newpgrp, M_PGRP);
mtx_unlock(&Giant);
return (0);
fail:
PGRPSESS_XUNLOCK();
KASSERT(newpgrp != NULL, ("setpgid failed and newpgrp is null."));
KASSERT(error != 0, ("setpgid successfully failed?"));
FREE(newpgrp, M_PGRP);
mtx_unlock(&Giant);
return (error);
}
/*
* Use the clause in B.4.2.2 that allows setuid/setgid to be 4.2/4.3BSD
* compatible. It says that setting the uid/gid to euid/egid is a special
* case of "appropriate privilege". Once the rules are expanded out, this
* basically means that setuid(nnn) sets all three id's, in all permitted
* cases unless _POSIX_SAVED_IDS is enabled. In that case, setuid(getuid())
* does not set the saved id - this is dangerous for traditional BSD
* programs. For this reason, we *really* do not want to set
* _POSIX_SAVED_IDS and do not want to clear POSIX_APPENDIX_B_4_2_2.
*/
#define POSIX_APPENDIX_B_4_2_2
#ifndef _SYS_SYSPROTO_H_
struct setuid_args {
uid_t uid;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
setuid(td, uap)
struct thread *td;
struct setuid_args *uap;
{
struct proc *p = td->td_proc;
struct ucred *newcred, *oldcred;
uid_t uid;
int error;
uid = uap->uid;
mtx_lock(&Giant);
error = 0;
oldcred = p->p_ucred;
/*
* See if we have "permission" by POSIX 1003.1 rules.
*
* Note that setuid(geteuid()) is a special case of
* "appropriate privileges" in appendix B.4.2.2. We need
* to use this clause to be compatible with traditional BSD
* semantics. Basically, it means that "setuid(xx)" sets all
* three id's (assuming you have privs).
*
* Notes on the logic. We do things in three steps.
* 1: We determine if the euid is going to change, and do EPERM
* right away. We unconditionally change the euid later if this
* test is satisfied, simplifying that part of the logic.
* 2: We determine if the real and/or saved uids are going to
* change. Determined by compile options.
* 3: Change euid last. (after tests in #2 for "appropriate privs")
*/
if (uid != oldcred->cr_ruid && /* allow setuid(getuid()) */
#ifdef _POSIX_SAVED_IDS
uid != oldcred->cr_svuid && /* allow setuid(saved gid) */
#endif
#ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
uid != oldcred->cr_uid && /* allow setuid(geteuid()) */
#endif
(error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
goto done2;
newcred = crdup(oldcred);
#ifdef _POSIX_SAVED_IDS
/*
* Do we have "appropriate privileges" (are we root or uid == euid)
* If so, we are changing the real uid and/or saved uid.
*/
if (
#ifdef POSIX_APPENDIX_B_4_2_2 /* Use the clause from B.4.2.2 */
uid == oldcred->cr_uid ||
#endif
suser_xxx(oldcred, NULL, PRISON_ROOT) == 0) /* we are using privs */
#endif
{
/*
* Set the real uid and transfer proc count to new user.
*/
if (uid != oldcred->cr_ruid) {
change_ruid(newcred, uid);
setsugid(p);
}
/*
* Set saved uid
*
* XXX always set saved uid even if not _POSIX_SAVED_IDS, as
* the security of seteuid() depends on it. B.4.2.2 says it
* is important that we should do this.
*/
if (uid != oldcred->cr_svuid) {
change_svuid(newcred, uid);
setsugid(p);
}
}
/*
* In all permitted cases, we are changing the euid.
* Copy credentials so other references do not see our changes.
*/
if (uid != oldcred->cr_uid) {
change_euid(newcred, uid);
setsugid(p);
}
p->p_ucred = newcred;
crfree(oldcred);
done2:
mtx_unlock(&Giant);
return (error);
}
#ifndef _SYS_SYSPROTO_H_
struct seteuid_args {
uid_t euid;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
seteuid(td, uap)
struct thread *td;
struct seteuid_args *uap;
{
struct proc *p = td->td_proc;
struct ucred *newcred, *oldcred;
uid_t euid;
int error;
euid = uap->euid;
mtx_lock(&Giant);
error = 0;
oldcred = p->p_ucred;
if (euid != oldcred->cr_ruid && /* allow seteuid(getuid()) */
euid != oldcred->cr_svuid && /* allow seteuid(saved uid) */
(error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
goto done2;
/*
* Everything's okay, do it. Copy credentials so other references do
* not see our changes.
*/
newcred = crdup(oldcred);
if (oldcred->cr_uid != euid) {
change_euid(newcred, euid);
setsugid(p);
}
p->p_ucred = newcred;
crfree(oldcred);
done2:
mtx_unlock(&Giant);
return (error);
}
#ifndef _SYS_SYSPROTO_H_
struct setgid_args {
gid_t gid;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
setgid(td, uap)
struct thread *td;
struct setgid_args *uap;
{
struct proc *p = td->td_proc;
struct ucred *newcred, *oldcred;
gid_t gid;
int error;
gid = uap->gid;
mtx_lock(&Giant);
error = 0;
oldcred = p->p_ucred;
/*
* See if we have "permission" by POSIX 1003.1 rules.
*
* Note that setgid(getegid()) is a special case of
* "appropriate privileges" in appendix B.4.2.2. We need
* to use this clause to be compatible with traditional BSD
* semantics. Basically, it means that "setgid(xx)" sets all
* three id's (assuming you have privs).
*
* For notes on the logic here, see setuid() above.
*/
if (gid != oldcred->cr_rgid && /* allow setgid(getgid()) */
#ifdef _POSIX_SAVED_IDS
gid != oldcred->cr_svgid && /* allow setgid(saved gid) */
#endif
#ifdef POSIX_APPENDIX_B_4_2_2 /* Use BSD-compat clause from B.4.2.2 */
gid != oldcred->cr_groups[0] && /* allow setgid(getegid()) */
#endif
(error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
goto done2;
newcred = crdup(oldcred);
#ifdef _POSIX_SAVED_IDS
/*
* Do we have "appropriate privileges" (are we root or gid == egid)
* If so, we are changing the real uid and saved gid.
*/
if (
#ifdef POSIX_APPENDIX_B_4_2_2 /* use the clause from B.4.2.2 */
gid == oldcred->cr_groups[0] ||
#endif
suser_xxx(oldcred, NULL, PRISON_ROOT) == 0) /* we are using privs */
#endif
{
/*
* Set real gid
*/
if (oldcred->cr_rgid != gid) {
change_rgid(newcred, gid);
setsugid(p);
}
/*
* Set saved gid
*
* XXX always set saved gid even if not _POSIX_SAVED_IDS, as
* the security of setegid() depends on it. B.4.2.2 says it
* is important that we should do this.
*/
if (oldcred->cr_svgid != gid) {
change_svgid(newcred, gid);
setsugid(p);
}
}
/*
* In all cases permitted cases, we are changing the egid.
* Copy credentials so other references do not see our changes.
*/
if (oldcred->cr_groups[0] != gid) {
change_egid(newcred, gid);
setsugid(p);
}
p->p_ucred = newcred;
crfree(oldcred);
done2:
mtx_unlock(&Giant);
return (error);
}
#ifndef _SYS_SYSPROTO_H_
struct setegid_args {
gid_t egid;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
setegid(td, uap)
struct thread *td;
struct setegid_args *uap;
{
struct proc *p = td->td_proc;
struct ucred *newcred, *oldcred;
gid_t egid;
int error;
egid = uap->egid;
mtx_lock(&Giant);
error = 0;
oldcred = p->p_ucred;
if (egid != oldcred->cr_rgid && /* allow setegid(getgid()) */
egid != oldcred->cr_svgid && /* allow setegid(saved gid) */
(error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
goto done2;
newcred = crdup(oldcred);
if (oldcred->cr_groups[0] != egid) {
change_egid(newcred, egid);
setsugid(p);
}
p->p_ucred = newcred;
crfree(oldcred);
done2:
mtx_unlock(&Giant);
return (error);
}
#ifndef _SYS_SYSPROTO_H_
struct setgroups_args {
u_int gidsetsize;
gid_t *gidset;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
setgroups(td, uap)
struct thread *td;
struct setgroups_args *uap;
{
struct proc *p = td->td_proc;
struct ucred *newcred, *oldcred;
u_int ngrp;
int error;
ngrp = uap->gidsetsize;
mtx_lock(&Giant);
oldcred = p->p_ucred;
if ((error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
goto done2;
if (ngrp > NGROUPS) {
error = EINVAL;
goto done2;
}
/*
* XXX A little bit lazy here. We could test if anything has
* changed before crcopy() and setting P_SUGID.
*/
newcred = crdup(oldcred);
if (ngrp < 1) {
/*
* setgroups(0, NULL) is a legitimate way of clearing the
* groups vector on non-BSD systems (which generally do not
* have the egid in the groups[0]). We risk security holes
* when running non-BSD software if we do not do the same.
*/
newcred->cr_ngroups = 1;
} else {
if ((error = copyin((caddr_t)uap->gidset,
(caddr_t)newcred->cr_groups, ngrp * sizeof(gid_t)))) {
crfree(newcred);
goto done2;
}
newcred->cr_ngroups = ngrp;
}
setsugid(p);
p->p_ucred = newcred;
crfree(oldcred);
done2:
mtx_unlock(&Giant);
return (error);
}
#ifndef _SYS_SYSPROTO_H_
struct setreuid_args {
uid_t ruid;
uid_t euid;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
setreuid(td, uap)
register struct thread *td;
struct setreuid_args *uap;
{
struct proc *p = td->td_proc;
struct ucred *newcred, *oldcred;
uid_t euid, ruid;
int error;
euid = uap->euid;
ruid = uap->ruid;
mtx_lock(&Giant);
error = 0;
oldcred = p->p_ucred;
if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
ruid != oldcred->cr_svuid) ||
(euid != (uid_t)-1 && euid != oldcred->cr_uid &&
euid != oldcred->cr_ruid && euid != oldcred->cr_svuid)) &&
(error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
goto done2;
newcred = crdup(oldcred);
if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
change_euid(newcred, euid);
setsugid(p);
}
if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
change_ruid(newcred, ruid);
setsugid(p);
}
if ((ruid != (uid_t)-1 || newcred->cr_uid != newcred->cr_ruid) &&
newcred->cr_svuid != newcred->cr_uid) {
change_svuid(newcred, newcred->cr_uid);
setsugid(p);
}
p->p_ucred = newcred;
crfree(oldcred);
done2:
mtx_unlock(&Giant);
return (error);
}
#ifndef _SYS_SYSPROTO_H_
struct setregid_args {
gid_t rgid;
gid_t egid;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
setregid(td, uap)
register struct thread *td;
struct setregid_args *uap;
{
struct proc *p = td->td_proc;
struct ucred *newcred, *oldcred;
gid_t egid, rgid;
int error;
egid = uap->egid;
rgid = uap->rgid;
mtx_lock(&Giant);
error = 0;
oldcred = p->p_ucred;
if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
rgid != oldcred->cr_svgid) ||
(egid != (gid_t)-1 && egid != oldcred->cr_groups[0] &&
egid != oldcred->cr_rgid && egid != oldcred->cr_svgid)) &&
(error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
goto done2;
newcred = crdup(oldcred);
if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
change_egid(newcred, egid);
setsugid(p);
}
if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
change_rgid(newcred, rgid);
setsugid(p);
}
if ((rgid != (gid_t)-1 || newcred->cr_groups[0] != newcred->cr_rgid) &&
newcred->cr_svgid != newcred->cr_groups[0]) {
change_svgid(newcred, newcred->cr_groups[0]);
setsugid(p);
}
p->p_ucred = newcred;
crfree(oldcred);
done2:
mtx_unlock(&Giant);
return (error);
}
/*
* setresuid(ruid, euid, suid) is like setreuid except control over the
* saved uid is explicit.
*/
#ifndef _SYS_SYSPROTO_H_
struct setresuid_args {
uid_t ruid;
uid_t euid;
uid_t suid;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
setresuid(td, uap)
register struct thread *td;
struct setresuid_args *uap;
{
struct proc *p = td->td_proc;
struct ucred *newcred, *oldcred;
uid_t euid, ruid, suid;
int error;
euid = uap->euid;
ruid = uap->ruid;
suid = uap->suid;
mtx_lock(&Giant);
oldcred = p->p_ucred;
if (((ruid != (uid_t)-1 && ruid != oldcred->cr_ruid &&
ruid != oldcred->cr_svuid &&
ruid != oldcred->cr_uid) ||
(euid != (uid_t)-1 && euid != oldcred->cr_ruid &&
euid != oldcred->cr_svuid &&
euid != oldcred->cr_uid) ||
(suid != (uid_t)-1 && suid != oldcred->cr_ruid &&
suid != oldcred->cr_svuid &&
suid != oldcred->cr_uid)) &&
(error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
goto done2;
newcred = crdup(oldcred);
if (euid != (uid_t)-1 && oldcred->cr_uid != euid) {
change_euid(newcred, euid);
setsugid(p);
}
if (ruid != (uid_t)-1 && oldcred->cr_ruid != ruid) {
change_ruid(newcred, ruid);
setsugid(p);
}
if (suid != (uid_t)-1 && oldcred->cr_svuid != suid) {
change_svuid(newcred, suid);
setsugid(p);
}
p->p_ucred = newcred;
crfree(oldcred);
error = 0;
done2:
mtx_unlock(&Giant);
return (error);
}
/*
* setresgid(rgid, egid, sgid) is like setregid except control over the
* saved gid is explicit.
*/
#ifndef _SYS_SYSPROTO_H_
struct setresgid_args {
gid_t rgid;
gid_t egid;
gid_t sgid;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
setresgid(td, uap)
register struct thread *td;
struct setresgid_args *uap;
{
struct proc *p = td->td_proc;
struct ucred *newcred, *oldcred;
gid_t egid, rgid, sgid;
int error;
egid = uap->egid;
rgid = uap->rgid;
sgid = uap->sgid;
mtx_lock(&Giant);
oldcred = p->p_ucred;
if (((rgid != (gid_t)-1 && rgid != oldcred->cr_rgid &&
rgid != oldcred->cr_svgid &&
rgid != oldcred->cr_groups[0]) ||
(egid != (gid_t)-1 && egid != oldcred->cr_rgid &&
egid != oldcred->cr_svgid &&
egid != oldcred->cr_groups[0]) ||
(sgid != (gid_t)-1 && sgid != oldcred->cr_rgid &&
sgid != oldcred->cr_svgid &&
sgid != oldcred->cr_groups[0])) &&
(error = suser_xxx(oldcred, NULL, PRISON_ROOT)) != 0)
goto done2;
newcred = crdup(oldcred);
if (egid != (gid_t)-1 && oldcred->cr_groups[0] != egid) {
change_egid(newcred, egid);
setsugid(p);
}
if (rgid != (gid_t)-1 && oldcred->cr_rgid != rgid) {
change_rgid(newcred, rgid);
setsugid(p);
}
if (sgid != (gid_t)-1 && oldcred->cr_svgid != sgid) {
change_svgid(newcred, sgid);
setsugid(p);
}
p->p_ucred = newcred;
crfree(oldcred);
error = 0;
done2:
mtx_unlock(&Giant);
return (error);
}
#ifndef _SYS_SYSPROTO_H_
struct getresuid_args {
uid_t *ruid;
uid_t *euid;
uid_t *suid;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
getresuid(td, uap)
register struct thread *td;
struct getresuid_args *uap;
{
struct ucred *cred;
struct proc *p = td->td_proc;
int error1 = 0, error2 = 0, error3 = 0;
mtx_lock(&Giant);
cred = p->p_ucred;
if (uap->ruid)
error1 = copyout((caddr_t)&cred->cr_ruid,
(caddr_t)uap->ruid, sizeof(cred->cr_ruid));
if (uap->euid)
error2 = copyout((caddr_t)&cred->cr_uid,
(caddr_t)uap->euid, sizeof(cred->cr_uid));
if (uap->suid)
error3 = copyout((caddr_t)&cred->cr_svuid,
(caddr_t)uap->suid, sizeof(cred->cr_svuid));
mtx_unlock(&Giant);
return (error1 ? error1 : error2 ? error2 : error3);
}
#ifndef _SYS_SYSPROTO_H_
struct getresgid_args {
gid_t *rgid;
gid_t *egid;
gid_t *sgid;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
getresgid(td, uap)
register struct thread *td;
struct getresgid_args *uap;
{
struct ucred *cred;
struct proc *p = td->td_proc;
int error1 = 0, error2 = 0, error3 = 0;
mtx_lock(&Giant);
cred = p->p_ucred;
if (uap->rgid)
error1 = copyout((caddr_t)&cred->cr_rgid,
(caddr_t)uap->rgid, sizeof(cred->cr_rgid));
if (uap->egid)
error2 = copyout((caddr_t)&cred->cr_groups[0],
(caddr_t)uap->egid, sizeof(cred->cr_groups[0]));
if (uap->sgid)
error3 = copyout((caddr_t)&cred->cr_svgid,
(caddr_t)uap->sgid, sizeof(cred->cr_svgid));
mtx_unlock(&Giant);
return (error1 ? error1 : error2 ? error2 : error3);
}
#ifndef _SYS_SYSPROTO_H_
struct issetugid_args {
int dummy;
};
#endif
/*
* NOT MPSAFE?
*/
/* ARGSUSED */
int
issetugid(td, uap)
register struct thread *td;
struct issetugid_args *uap;
{
struct proc *p = td->td_proc;
/*
* Note: OpenBSD sets a P_SUGIDEXEC flag set at execve() time,
* we use P_SUGID because we consider changing the owners as
* "tainting" as well.
* This is significant for procs that start as root and "become"
* a user without an exec - programs cannot know *everything*
* that libc *might* have put in their data segment.
*/
PROC_LOCK(p);
td->td_retval[0] = (p->p_flag & P_SUGID) ? 1 : 0;
PROC_UNLOCK(p);
return (0);
}
/*
* MPSAFE
*/
int
__setugid(td, uap)
struct thread *td;
struct __setugid_args *uap;
{
#ifdef REGRESSION
int error;
mtx_lock(&Giant);
error = 0;
switch (uap->flag) {
case 0:
PROC_LOCK(td->td_proc);
td->td_proc->p_flag &= ~P_SUGID;
PROC_UNLOCK(td->td_proc);
break;
case 1:
PROC_LOCK(td->td_proc);
td->td_proc->p_flag |= P_SUGID;
PROC_UNLOCK(td->td_proc);
break;
default:
error = EINVAL;
break;
}
mtx_unlock(&Giant);
return (error);
#else /* !REGRESSION */
return (ENOSYS);
#endif /* REGRESSION */
}
/*
* Check if gid is a member of the group set.
*/
int
groupmember(gid, cred)
gid_t gid;
struct ucred *cred;
{
register gid_t *gp;
gid_t *egp;
egp = &(cred->cr_groups[cred->cr_ngroups]);
for (gp = cred->cr_groups; gp < egp; gp++)
if (*gp == gid)
return (1);
return (0);
}
/*
* `suser_enabled' (which can be set by the security.suser_enabled
* sysctl) determines whether the system 'super-user' policy is in effect.
* If it is nonzero, an effective uid of 0 connotes special privilege,
* overriding many mandatory and discretionary protections. If it is zero,
* uid 0 is offered no special privilege in the kernel security policy.
* Setting it to zero may seriously impact the functionality of many
* existing userland programs, and should not be done without careful
* consideration of the consequences.
*/
int suser_enabled = 1;
SYSCTL_INT(_security_bsd, OID_AUTO, suser_enabled, CTLFLAG_RW,
&suser_enabled, 0, "processes with uid 0 have privilege");
TUNABLE_INT("security.bsd.suser_enabled", &suser_enabled);
/*
* Test whether the specified credentials imply "super-user" privilege.
* Return 0 or EPERM.
*/
int
suser(p)
struct proc *p;
{
return (suser_xxx(0, p, 0));
}
/*
* version for when the thread pointer is available and not the proc.
* (saves having to include proc.h into every file that needs to do the change.)
*/
int
suser_td(td)
struct thread *td;
{
return (suser_xxx(0, td->td_proc, 0));
}
/*
* wrapper to use if you have the thread on hand but not the proc.
*/
int
suser_xxx_td(cred, td, flag)
struct ucred *cred;
struct thread *td;
int flag;
{
return(suser_xxx(cred, td->td_proc, flag));
}
int
suser_xxx(cred, proc, flag)
struct ucred *cred;
struct proc *proc;
int flag;
{
if (!suser_enabled)
return (EPERM);
if (!cred && !proc) {
printf("suser_xxx(): THINK!\n");
return (EPERM);
}
if (cred == NULL)
cred = proc->p_ucred;
if (cred->cr_uid != 0)
return (EPERM);
if (jailed(cred) && !(flag & PRISON_ROOT))
return (EPERM);
return (0);
}
/*
* Test the active securelevel against a given level. securelevel_gt()
* implements (securelevel > level). securelevel_ge() implements
* (securelevel >= level). Note that the logic is inverted -- these
* functions return EPERM on "success" and 0 on "failure".
*
* cr is permitted to be NULL for the time being, as there were some
* existing securelevel checks that occurred without a process/credential
* context. In the future this will be disallowed, so a kernel message
* is displayed.
*/
int
securelevel_gt(struct ucred *cr, int level)
{
int active_securelevel;
active_securelevel = securelevel;
if (cr == NULL)
printf("securelevel_gt: cr is NULL\n");
if (cr->cr_prison != NULL) {
mtx_lock(&cr->cr_prison->pr_mtx);
active_securelevel = imax(cr->cr_prison->pr_securelevel,
active_securelevel);
mtx_unlock(&cr->cr_prison->pr_mtx);
}
return (active_securelevel > level ? EPERM : 0);
}
int
securelevel_ge(struct ucred *cr, int level)
{
int active_securelevel;
active_securelevel = securelevel;
if (cr == NULL)
printf("securelevel_gt: cr is NULL\n");
if (cr->cr_prison != NULL) {
mtx_lock(&cr->cr_prison->pr_mtx);
active_securelevel = imax(cr->cr_prison->pr_securelevel,
active_securelevel);
mtx_unlock(&cr->cr_prison->pr_mtx);
}
return (active_securelevel >= level ? EPERM : 0);
}
/*
* 'see_other_uids' determines whether or not visibility of processes
* and sockets with credentials holding different real uids is possible
* using a variety of system MIBs.
* XXX: data declarations should be together near the beginning of the file.
*/
static int see_other_uids = 1;
SYSCTL_INT(_security_bsd, OID_AUTO, see_other_uids, CTLFLAG_RW,
&see_other_uids, 0,
"Unprivileged processes may see subjects/objects with different real uid");
/*-
* Determine if u1 "can see" the subject specified by u2.
* Returns: 0 for permitted, an errno value otherwise
* Locks: none
* References: *u1 and *u2 must not change during the call
* u1 may equal u2, in which case only one reference is required
*/
int
cr_cansee(struct ucred *u1, struct ucred *u2)
{
int error;
if ((error = prison_check(u1, u2)))
return (error);
if (!see_other_uids && u1->cr_ruid != u2->cr_ruid) {
if (suser_xxx(u1, NULL, PRISON_ROOT) != 0)
return (ESRCH);
}
return (0);
}
/*-
* Determine if p1 "can see" the subject specified by p2.
* Returns: 0 for permitted, an errno value otherwise
* Locks: Sufficient locks to protect p1->p_ucred and p2->p_ucred must
* be held. Normally, p1 will be curproc, and a lock must be held
* for p2.
* References: p1 and p2 must be valid for the lifetime of the call
*/
int
p_cansee(struct proc *p1, struct proc *p2)
{
/* Wrap cr_cansee() for all functionality. */
return (cr_cansee(p1->p_ucred, p2->p_ucred));
}
/*-
* Determine whether cred may deliver the specified signal to proc.
* Returns: 0 for permitted, an errno value otherwise.
* Locks: A lock must be held for proc.
* References: cred and proc must be valid for the lifetime of the call.
*/
int
cr_cansignal(struct ucred *cred, struct proc *proc, int signum)
{
int error;
/*
* Jail semantics limit the scope of signalling to proc in the
* same jail as cred, if cred is in jail.
*/
error = prison_check(cred, proc->p_ucred);
if (error)
return (error);
/*
* UNIX signal semantics depend on the status of the P_SUGID
* bit on the target process. If the bit is set, then additional
* restrictions are placed on the set of available signals.
*/
if (proc->p_flag & P_SUGID) {
switch (signum) {
case 0:
case SIGKILL:
case SIGINT:
case SIGTERM:
case SIGSTOP:
case SIGTTIN:
case SIGTTOU:
case SIGTSTP:
case SIGHUP:
case SIGUSR1:
case SIGUSR2:
/*
* Generally, permit job and terminal control
* signals.
*/
break;
default:
/* Not permitted without privilege. */
error = suser_xxx(cred, NULL, PRISON_ROOT);
if (error)
return (error);
}
}
/*
* Generally, the target credential's ruid or svuid must match the
* subject credential's ruid or euid.
*/
if (cred->cr_ruid != proc->p_ucred->cr_ruid &&
cred->cr_ruid != proc->p_ucred->cr_svuid &&
cred->cr_uid != proc->p_ucred->cr_ruid &&
cred->cr_uid != proc->p_ucred->cr_svuid) {
/* Not permitted without privilege. */
error = suser_xxx(cred, NULL, PRISON_ROOT);
if (error)
return (error);
}
return (0);
}
/*-
* Determine whether p1 may deliver the specified signal to p2.
* Returns: 0 for permitted, an errno value otherwise
* Locks: Sufficient locks to protect various components of p1 and p2
* must be held. Normally, p1 will be curproc, and a lock must
* be held for p2.
* References: p1 and p2 must be valid for the lifetime of the call
*/
int
p_cansignal(struct proc *p1, struct proc *p2, int signum)
{
if (p1 == p2)
return (0);
/*
* UNIX signalling semantics require that processes in the same
* session always be able to deliver SIGCONT to one another,
* overriding the remaining protections.
*/
if (signum == SIGCONT && p1->p_session == p2->p_session)
return (0);
return (cr_cansignal(p1->p_ucred, p2, signum));
}
/*-
* Determine whether p1 may reschedule p2.
* Returns: 0 for permitted, an errno value otherwise
* Locks: Sufficient locks to protect various components of p1 and p2
* must be held. Normally, p1 will be curproc, and a lock must
* be held for p2.
* References: p1 and p2 must be valid for the lifetime of the call
*/
int
p_cansched(struct proc *p1, struct proc *p2)
{
int error;
if (p1 == p2)
return (0);
if ((error = prison_check(p1->p_ucred, p2->p_ucred)))
return (error);
if (p1->p_ucred->cr_ruid == p2->p_ucred->cr_ruid)
return (0);
if (p1->p_ucred->cr_uid == p2->p_ucred->cr_ruid)
return (0);
if (suser_xxx(0, p1, PRISON_ROOT) == 0)
return (0);
#ifdef CAPABILITIES
if (!cap_check(NULL, p1, CAP_SYS_NICE, PRISON_ROOT))
return (0);
#endif
return (EPERM);
}
/*
* The 'unprivileged_proc_debug' flag may be used to disable a variety of
* unprivileged inter-process debugging services, including some procfs
* functionality, ptrace(), and ktrace(). In the past, inter-process
* debugging has been involved in a variety of security problems, and sites
* not requiring the service might choose to disable it when hardening
* systems.
*
* XXX: Should modifying and reading this variable require locking?
* XXX: data declarations should be together near the beginning of the file.
*/
static int unprivileged_proc_debug = 1;
SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_proc_debug, CTLFLAG_RW,
&unprivileged_proc_debug, 0,
"Unprivileged processes may use process debugging facilities");
/*-
* Determine whether p1 may debug p2.
* Returns: 0 for permitted, an errno value otherwise
* Locks: Sufficient locks to protect various components of p1 and p2
* must be held. Normally, p1 will be curproc, and a lock must
* be held for p2.
* References: p1 and p2 must be valid for the lifetime of the call
*/
int
p_candebug(struct proc *p1, struct proc *p2)
{
int credentialchanged, error, grpsubset, i, uidsubset;
if (!unprivileged_proc_debug) {
error = suser_xxx(NULL, p1, PRISON_ROOT);
if (error)
return (error);
}
if (p1 == p2)
return (0);
if ((error = prison_check(p1->p_ucred, p2->p_ucred)))
return (error);
/*
* Is p2's group set a subset of p1's effective group set? This
* includes p2's egid, group access list, rgid, and svgid.
*/
grpsubset = 1;
for (i = 0; i < p2->p_ucred->cr_ngroups; i++) {
if (!groupmember(p2->p_ucred->cr_groups[i], p1->p_ucred)) {
grpsubset = 0;
break;
}
}
grpsubset = grpsubset &&
groupmember(p2->p_ucred->cr_rgid, p1->p_ucred) &&
groupmember(p2->p_ucred->cr_svgid, p1->p_ucred);
/*
* Are the uids present in p2's credential equal to p1's
* effective uid? This includes p2's euid, svuid, and ruid.
*/
uidsubset = (p1->p_ucred->cr_uid == p2->p_ucred->cr_uid &&
p1->p_ucred->cr_uid == p2->p_ucred->cr_svuid &&
p1->p_ucred->cr_uid == p2->p_ucred->cr_ruid);
/*
* Has the credential of the process changed since the last exec()?
*/
credentialchanged = (p2->p_flag & P_SUGID);
/*
* If p2's gids aren't a subset, or the uids aren't a subset,
* or the credential has changed, require appropriate privilege
* for p1 to debug p2. For POSIX.1e capabilities, this will
* require CAP_SYS_PTRACE.
*/
if (!grpsubset || !uidsubset || credentialchanged) {
error = suser_xxx(NULL, p1, PRISON_ROOT);
if (error)
return (error);
}
/* Can't trace init when securelevel > 0. */
if (p2 == initproc) {
error = securelevel_gt(p1->p_ucred, 0);
if (error)
return (error);
}
/*
* Can't trace a process that's currently exec'ing.
* XXX: Note, this is not a security policy decision, it's a
* basic correctness/functionality decision. Therefore, this check
* should be moved to the caller's of p_candebug().
*/
if ((p2->p_flag & P_INEXEC) != 0)
return (EAGAIN);
return (0);
}
/*
* Allocate a zeroed cred structure.
*/
struct ucred *
crget()
{
register struct ucred *cr;
MALLOC(cr, struct ucred *, sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
cr->cr_ref = 1;
cr->cr_mtxp = mtx_pool_find(cr);
return (cr);
}
/*
* Claim another reference to a ucred structure.
*/
struct ucred *
crhold(cr)
struct ucred *cr;
{
mtx_lock(cr->cr_mtxp);
cr->cr_ref++;
mtx_unlock(cr->cr_mtxp);
return (cr);
}
/*
* Free a cred structure.
* Throws away space when ref count gets to 0.
*/
void
crfree(cr)
struct ucred *cr;
{
struct mtx *mtxp = cr->cr_mtxp;
mtx_lock(mtxp);
KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
if (--cr->cr_ref == 0) {
/*
* Some callers of crget(), such as nfs_statfs(),
* allocate a temporary credential, but don't
* allocate a uidinfo structure.
*/
mtx_unlock(mtxp);
if (cr->cr_uidinfo != NULL)
uifree(cr->cr_uidinfo);
if (cr->cr_ruidinfo != NULL)
uifree(cr->cr_ruidinfo);
/*
* Free a prison, if any.
*/
if (jailed(cr))
prison_free(cr->cr_prison);
FREE((caddr_t)cr, M_CRED);
} else {
mtx_unlock(mtxp);
}
}
/*
* Check to see if this ucred is shared.
*/
int
crshared(cr)
struct ucred *cr;
{
int shared;
mtx_lock(cr->cr_mtxp);
shared = (cr->cr_ref > 1);
mtx_unlock(cr->cr_mtxp);
return (shared);
}
/*
* Copy a ucred's contents from a template. Does not block.
*/
void
crcopy(dest, src)
struct ucred *dest, *src;
{
KASSERT(crshared(dest) == 0, ("crcopy of shared ucred"));
bcopy(&src->cr_startcopy, &dest->cr_startcopy,
(unsigned)((caddr_t)&src->cr_endcopy -
(caddr_t)&src->cr_startcopy));
uihold(dest->cr_uidinfo);
uihold(dest->cr_ruidinfo);
if (jailed(dest))
prison_hold(dest->cr_prison);
}
/*
* Dup cred struct to a new held one.
*/
struct ucred *
crdup(cr)
struct ucred *cr;
{
struct ucred *newcr;
newcr = crget();
crcopy(newcr, cr);
return (newcr);
}
/*
* Fill in a struct xucred based on a struct ucred.
*/
void
cru2x(cr, xcr)
struct ucred *cr;
struct xucred *xcr;
{
bzero(xcr, sizeof(*xcr));
xcr->cr_version = XUCRED_VERSION;
xcr->cr_uid = cr->cr_uid;
xcr->cr_ngroups = cr->cr_ngroups;
bcopy(cr->cr_groups, xcr->cr_groups, sizeof(cr->cr_groups));
}
/*
* small routine to swap a thread's current ucred for the correct one
* taken from the process.
*/
void
cred_update_thread(struct thread *td)
{
struct proc *p;
struct ucred *cred;
p = td->td_proc;
cred = td->td_ucred;
mtx_lock(&Giant);
PROC_LOCK(p);
td->td_ucred = crhold(p->p_ucred);
PROC_UNLOCK(p);
if (cred != NULL)
crfree(cred);
mtx_unlock(&Giant);
}
/*
* Get login name, if available.
*/
#ifndef _SYS_SYSPROTO_H_
struct getlogin_args {
char *namebuf;
u_int namelen;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
getlogin(td, uap)
struct thread *td;
struct getlogin_args *uap;
{
int error;
char login[MAXLOGNAME];
struct proc *p = td->td_proc;
mtx_lock(&Giant);
if (uap->namelen > MAXLOGNAME)
uap->namelen = MAXLOGNAME;
PROC_LOCK(p);
SESS_LOCK(p->p_session);
bcopy(p->p_session->s_login, login, uap->namelen);
SESS_UNLOCK(p->p_session);
PROC_UNLOCK(p);
error = copyout((caddr_t) login, (caddr_t) uap->namebuf, uap->namelen);
mtx_unlock(&Giant);
return(error);
}
/*
* Set login name.
*/
#ifndef _SYS_SYSPROTO_H_
struct setlogin_args {
char *namebuf;
};
#endif
/*
* MPSAFE
*/
/* ARGSUSED */
int
setlogin(td, uap)
struct thread *td;
struct setlogin_args *uap;
{
struct proc *p = td->td_proc;
int error;
char logintmp[MAXLOGNAME];
mtx_lock(&Giant);
if ((error = suser_xxx(0, p, PRISON_ROOT)) != 0)
goto done2;
error = copyinstr((caddr_t) uap->namebuf, (caddr_t) logintmp,
sizeof(logintmp), (size_t *)0);
if (error == ENAMETOOLONG)
error = EINVAL;
else if (!error) {
PROC_LOCK(p);
SESS_LOCK(p->p_session);
(void) memcpy(p->p_session->s_login, logintmp,
sizeof(logintmp));
SESS_UNLOCK(p->p_session);
PROC_UNLOCK(p);
}
done2:
mtx_unlock(&Giant);
return (error);
}
void
setsugid(p)
struct proc *p;
{
p->p_flag |= P_SUGID;
if (!(p->p_pfsflags & PF_ISUGID))
p->p_stops = 0;
}
/*-
* Change a process's effective uid.
* Side effects: newcred->cr_uid and newcred->cr_uidinfo will be modified.
* References: newcred must be an exclusive credential reference for the
* duration of the call.
*/
void
change_euid(newcred, euid)
struct ucred *newcred;
uid_t euid;
{
newcred->cr_uid = euid;
uifree(newcred->cr_uidinfo);
newcred->cr_uidinfo = uifind(euid);
}
/*-
* Change a process's effective gid.
* Side effects: newcred->cr_gid will be modified.
* References: newcred must be an exclusive credential reference for the
* duration of the call.
*/
void
change_egid(newcred, egid)
struct ucred *newcred;
gid_t egid;
{
newcred->cr_groups[0] = egid;
}
/*-
* Change a process's real uid.
* Side effects: newcred->cr_ruid will be updated, newcred->cr_ruidinfo
* will be updated, and the old and new cr_ruidinfo proc
* counts will be updated.
* References: newcred must be an exclusive credential reference for the
* duration of the call.
*/
void
change_ruid(newcred, ruid)
struct ucred *newcred;
uid_t ruid;
{
(void)chgproccnt(newcred->cr_ruidinfo, -1, 0);
newcred->cr_ruid = ruid;
uifree(newcred->cr_ruidinfo);
newcred->cr_ruidinfo = uifind(ruid);
(void)chgproccnt(newcred->cr_ruidinfo, 1, 0);
}
/*-
* Change a process's real gid.
* Side effects: newcred->cr_rgid will be updated.
* References: newcred must be an exclusive credential reference for the
* duration of the call.
*/
void
change_rgid(newcred, rgid)
struct ucred *newcred;
gid_t rgid;
{
newcred->cr_rgid = rgid;
}
/*-
* Change a process's saved uid.
* Side effects: newcred->cr_svuid will be updated.
* References: newcred must be an exclusive credential reference for the
* duration of the call.
*/
void
change_svuid(newcred, svuid)
struct ucred *newcred;
uid_t svuid;
{
newcred->cr_svuid = svuid;
}
/*-
* Change a process's saved gid.
* Side effects: newcred->cr_svgid will be updated.
* References: newcred must be an exclusive credential reference for the
* duration of the call.
*/
void
change_svgid(newcred, svgid)
struct ucred *newcred;
gid_t svgid;
{
newcred->cr_svgid = svgid;
}
|