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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <bsm/adt.h>
#include <bsm/adt_event.h>
#include <assert.h>
#include <bsm/audit.h>
#include <bsm/audit_record.h>
#include <bsm/libbsm.h>
#include <door.h>
#include <errno.h>
#include <generic.h>
#include <md5.h>
#include <sys/mkdev.h>
#include <netdb.h>
#include <nss_dbdefs.h>
#include <pwd.h>
#include <sys/stat.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <synch.h>
#include <sys/systeminfo.h>
#include <syslog.h>
#include <thread.h>
#include <unistd.h>
#include <adt_xlate.h>
#include <adt_ucred.h>
static int adt_selected(struct adt_event_state *, au_event_t, int);
static int adt_init(adt_internal_state_t *, int);
static int adt_import(adt_internal_state_t *, const adt_export_data_t *);
static m_label_t *adt_ucred_label(ucred_t *);
static void adt_setto_unaudited(adt_internal_state_t *);
#ifdef C2_DEBUG
#define DPRINTF(x) {printf x; }
#define DFLUSH fflush(stdout);
#else
#define DPRINTF(x)
#define DFLUSH
#endif
extern int _mutex_lock(mutex_t *);
extern int _mutex_unlock(mutex_t *);
static int auditstate = AUC_DISABLED; /* default state */
/*
* adt_write_syslog
*
* errors that are not the user's fault (bugs or whatever in
* the underlying audit code are noted in syslog.)
*
* Avoid calling adt_write_syslog for things that can happen
* at high volume.
*
* syslog's open (openlog) and close (closelog) are interesting;
* openlog *may* create a file descriptor and is optional. closelog
* *will* close any open file descriptors and is also optional.
*
* Since syslog may also be used by the calling application, the
* choice is to avoid openlog, which sets some otherwise useful
* parameters, and to embed "Solaris_audit" in the log message.
*/
void
adt_write_syslog(const char *message, int err)
{
int save_errno;
int mask_priority;
save_errno = errno;
errno = err;
DPRINTF(("syslog called: %s\n", message));
mask_priority = setlogmask(LOG_MASK(LOG_ALERT));
syslog(LOG_ALERT, "Solaris_audit %s: %m", message, err);
(void) setlogmask(mask_priority);
errno = save_errno;
}
/*
* return true if audit is enabled. "Enabled" is any state
* other than AUC_DISABLED.
*
* states are
* AUC_INIT_AUDIT -- c2audit queuing enabled.
* AUC_AUDITING -- up and running
* AUC_DISABLED -- no audit subsystem loaded
* AUC_UNSET -- early boot state
* AUC_NOAUDIT -- subsystem loaded, turned off via
* auditon(A_SETCOND...)
* AUC_NOSPACE -- up and running, but log partitions are full
*
* For purpose of this API, anything but AUC_DISABLED or
* AUC_UNSET is enabled; however one never actually sees
* AUC_DISABLED since auditon returns EINVAL in that case. Any
* auditon error is considered the same as EINVAL for our
* purpose. auditstate is not changed by auditon if an error
* is returned.
*/
boolean_t
adt_audit_enabled(void) {
(void) auditon(A_GETCOND, (caddr_t)&auditstate, sizeof (auditstate));
return (auditstate != AUC_DISABLED);
}
/*
* The man page for getpwuid_r says the buffer must be big enough
* or ERANGE will be returned, but offers no guidance for how big
* the buffer should be or a way to calculate it. If you get
* ERANGE, double pwd_buff's size.
*
* This may be called even when auditing is off.
*/
#define NAFLAG_LEN 512
static int
adt_get_mask_from_user(uid_t uid, au_mask_t *mask)
{
struct passwd pwd;
char pwd_buff[NSS_BUFSIZ];
char naflag_buf[NAFLAG_LEN];
if (auditstate == AUC_DISABLED) {
mask->am_success = 0;
mask->am_failure = 0;
} else if (uid >= 0) {
if (getpwuid_r(uid, &pwd, pwd_buff, NSS_BUFSIZ) == NULL) {
/*
* getpwuid_r returns NULL without setting
* errno if the user does not exist; only
* if the input is the wrong length does it
* set errno.
*/
if (errno != ERANGE)
errno = EINVAL;
return (-1);
}
if (au_user_mask(pwd.pw_name, mask)) {
errno = EFAULT; /* undetermined failure */
return (-1);
}
} else if (getacna(naflag_buf, NAFLAG_LEN - 1) == 0) {
if (getauditflagsbin(naflag_buf, mask))
return (-1);
} else {
return (-1);
}
return (0);
}
/*
* adt_get_unique_id -- generate a hopefully unique 32 bit value
*
* there will be a follow up to replace this with the use of /dev/random
*
* An MD5 hash is taken on a buffer of
* hostname . audit id . unix time . pid . count
*
* "count = noise++;" is subject to a race condition but I don't
* see a need to put a lock around it.
*/
static au_id_t
adt_get_unique_id(uid_t uid)
{
char hostname[MAXHOSTNAMELEN];
union {
au_id_t v[4];
unsigned char obuff[128/8];
} output;
MD5_CTX context;
static int noise = 0;
int count = noise++;
time_t timebits = time(NULL);
pid_t pidbits = getpid();
au_id_t retval = 0;
if (gethostname(hostname, MAXHOSTNAMELEN)) {
adt_write_syslog("gethostname call failed", errno);
(void) strncpy(hostname, "invalidHostName", MAXHOSTNAMELEN);
}
while (retval == 0) { /* 0 is the only invalid result */
MD5Init(&context);
MD5Update(&context, (unsigned char *)hostname,
(unsigned int) strlen((const char *)hostname));
MD5Update(&context, (unsigned char *) &uid, sizeof (uid_t));
MD5Update(&context,
(unsigned char *) &timebits, sizeof (time_t));
MD5Update(&context, (unsigned char *) &pidbits,
sizeof (pid_t));
MD5Update(&context, (unsigned char *) &(count), sizeof (int));
MD5Final(output.obuff, &context);
retval = output.v[count % 4];
}
return (retval);
}
/*
* the following "port" function deals with the following issues:
*
* 1 the kernel and ucred deal with a dev_t as a 64 bit value made
* up from a 32 bit major and 32 bit minor.
* 2 User space deals with a dev_t as either the above 64 bit value
* or a 32 bit value made from a 14 bit major and an 18 bit minor.
* 3 The various audit interfaces (except ucred) pass the 32 or
* 64 bit version depending the architecture of the userspace
* application. If you get a port value from ucred and pass it
* to the kernel via auditon(), it must be squeezed into a 32
* bit value because the kernel knows the userspace app's bit
* size.
*
* The internal state structure for adt (adt_internal_state_t) uses
* dev_t, so adt converts data from ucred to fit. The import/export
* functions, however, can't know if they are importing/exporting
* from 64 or 32 bit applications, so they always send 64 bits and
* the 32 bit end(s) are responsible to convert 32 -> 64 -> 32 as
* appropriate.
*/
/*
* adt_cpy_tid() -- if lib is 64 bit, just copy it (dev_t and port are
* both 64 bits). If lib is 32 bits, squeeze the two-int port into
* a 32 bit dev_t. A port fits in the "minor" part of au_port_t,
* so it isn't broken up into pieces. (When it goes to the kernel
* and back, however, it will have been split into major/minor
* pieces.)
*/
static void
adt_cpy_tid(au_tid_addr_t *dest, const au_tid64_addr_t *src)
{
#ifdef _LP64
(void) memcpy(dest, src, sizeof (au_tid_addr_t));
#else
dest->at_type = src->at_type;
dest->at_port = src->at_port.at_minor & MAXMIN32;
dest->at_port |= (src->at_port.at_major & MAXMAJ32) <<
NBITSMINOR32;
(void) memcpy(dest->at_addr, src->at_addr, 4 * sizeof (uint32_t));
#endif
}
/*
* adt_start_session -- create interface handle, create context
*
* The imported_state input is normally NULL, if not, it represents
* a continued session; its values obviate the need for a subsequent
* call to adt_set_user().
*
* The flag is used to decide how to set the initial state of the session.
* If 0, the session is "no audit" until a call to adt_set_user; if
* ADT_USE_PROC_DATA, the session is built from the process audit
* characteristics obtained from the kernel. If imported_state is
* not NULL, the resulting audit mask is an OR of the current process
* audit mask and that passed in.
*
* The basic model is that the caller can use the pointer returned
* by adt_start_session whether or not auditing is enabled or an
* error was returned. The functions that take the session handle
* as input generally return without doing anything if auditing is
* disabled.
*/
int
adt_start_session(adt_session_data_t **new_session,
const adt_export_data_t *imported_state, adt_session_flags_t flags)
{
adt_internal_state_t *state;
adt_session_flags_t flgmask = ADT_FLAGS_ALL;
*new_session = NULL; /* assume failure */
/* ensure that auditstate is set */
(void) adt_audit_enabled();
if ((flags & ~flgmask) != 0) {
errno = EINVAL;
goto return_err;
}
state = calloc(1, sizeof (adt_internal_state_t));
if (state == NULL)
goto return_err;
if (adt_init(state, flags & ADT_USE_PROC_DATA) != 0)
goto return_err_free; /* errno from adt_init() */
/*
* The imported state overwrites the initial state if the
* imported state represents a valid audit trail
*/
if (imported_state != NULL) {
if (adt_import(state, imported_state) != 0) {
goto return_err_free;
}
} else if (flags & ADT_USE_PROC_DATA) {
state->as_session_model = ADT_PROCESS_MODEL;
}
state->as_flags = flags;
DPRINTF(("(%d) Starting session id = %08X\n",
getpid(), state->as_info.ai_asid));
if (state->as_audit_enabled) {
*new_session = (adt_session_data_t *)state;
} else {
free(state);
}
return (0);
return_err_free:
free(state);
return_err:
adt_write_syslog("audit session create failed", errno);
return (-1);
}
/*
* adt_get_asid() and adt_set_asid()
*
* if you use this interface, you are responsible to insure that the
* rest of the session data is populated correctly before calling
* adt_proccess_attr()
*
* neither of these are intended for general use and will likely
* remain private interfaces for a long time. Forever is a long
* time. In the case of adt_set_asid(), you should have a very,
* very good reason for setting your own session id. The process
* audit characteristics are not changed by put, use adt_set_proc().
*
* These are "volatile" (more changable than "evolving") and will
* probably change in the S10 period.
*/
void
adt_get_asid(const adt_session_data_t *session_data, au_asid_t *asid)
{
if (session_data == NULL) {
*asid = 0;
} else {
assert(((adt_internal_state_t *)session_data)->as_check ==
ADT_VALID);
*asid = ((adt_internal_state_t *)session_data)->as_info.ai_asid;
}
}
void
adt_set_asid(const adt_session_data_t *session_data, const au_asid_t session_id)
{
if (session_data != NULL) {
assert(((adt_internal_state_t *)session_data)->as_check ==
ADT_VALID);
((adt_internal_state_t *)session_data)->as_have_user_data |=
ADT_HAVE_ASID;
((adt_internal_state_t *)session_data)->as_info.ai_asid =
session_id;
}
}
/*
* adt_get_auid() and adt_set_auid()
*
* neither of these are intended for general use and will likely
* remain private interfaces for a long time. Forever is a long
* time. In the case of adt_set_auid(), you should have a very,
* very good reason for setting your own audit id. The process
* audit characteristics are not changed by put, use adt_set_proc().
*/
void
adt_get_auid(const adt_session_data_t *session_data, au_id_t *auid)
{
if (session_data == NULL) {
*auid = AU_NOAUDITID;
} else {
assert(((adt_internal_state_t *)session_data)->as_check ==
ADT_VALID);
*auid = ((adt_internal_state_t *)session_data)->as_info.ai_auid;
}
}
void
adt_set_auid(const adt_session_data_t *session_data, const au_id_t audit_id)
{
if (session_data != NULL) {
assert(((adt_internal_state_t *)session_data)->as_check ==
ADT_VALID);
((adt_internal_state_t *)session_data)->as_have_user_data |=
ADT_HAVE_AUID;
((adt_internal_state_t *)session_data)->as_info.ai_auid =
audit_id;
}
}
/*
* adt_get_termid(), adt_set_termid()
*
* if you use this interface, you are responsible to insure that the
* rest of the session data is populated correctly before calling
* adt_proccess_attr()
*
* The process audit characteristics are not changed by put, use
* adt_set_proc().
*/
void
adt_get_termid(const adt_session_data_t *session_data, au_tid_addr_t *termid)
{
if (session_data == NULL) {
(void) memset(termid, 0, sizeof (au_tid_addr_t));
termid->at_type = AU_IPv4;
} else {
assert(((adt_internal_state_t *)session_data)->as_check ==
ADT_VALID);
*termid =
((adt_internal_state_t *)session_data)->as_info.ai_termid;
}
}
void
adt_set_termid(const adt_session_data_t *session_data,
const au_tid_addr_t *termid)
{
if (session_data != NULL) {
assert(((adt_internal_state_t *)session_data)->as_check ==
ADT_VALID);
((adt_internal_state_t *)session_data)->as_info.ai_termid =
*termid;
((adt_internal_state_t *)session_data)->as_have_user_data |=
ADT_HAVE_TID;
}
}
/*
* adt_get_mask(), adt_set_mask()
*
* if you use this interface, you are responsible to insure that the
* rest of the session data is populated correctly before calling
* adt_proccess_attr()
*
* The process audit characteristics are not changed by put, use
* adt_set_proc().
*/
void
adt_get_mask(const adt_session_data_t *session_data, au_mask_t *mask)
{
if (session_data == NULL) {
mask->am_success = 0;
mask->am_failure = 0;
} else {
assert(((adt_internal_state_t *)session_data)->as_check ==
ADT_VALID);
*mask = ((adt_internal_state_t *)session_data)->as_info.ai_mask;
}
}
void
adt_set_mask(const adt_session_data_t *session_data, const au_mask_t *mask)
{
if (session_data != NULL) {
assert(((adt_internal_state_t *)session_data)->as_check ==
ADT_VALID);
((adt_internal_state_t *)session_data)->as_info.ai_mask = *mask;
((adt_internal_state_t *)session_data)->as_have_user_data |=
ADT_HAVE_MASK;
}
}
/*
* helpers for adt_load_termid
*/
static void
adt_do_ipv6_address(struct sockaddr_in6 *peer, struct sockaddr_in6 *sock,
au_tid_addr_t *termid)
{
termid->at_port = ((peer->sin6_port<<16) | (sock->sin6_port));
termid->at_type = AU_IPv6;
(void) memcpy(termid->at_addr, &peer->sin6_addr, 4 * sizeof (uint_t));
}
static void
adt_do_ipv4_address(struct sockaddr_in *peer, struct sockaddr_in *sock,
au_tid_addr_t *termid)
{
termid->at_port = ((peer->sin_port<<16) | (sock->sin_port));
termid->at_type = AU_IPv4;
termid->at_addr[0] = (uint32_t)peer->sin_addr.s_addr;
(void) memset(&(termid->at_addr[1]), 0, 3 * sizeof (uint_t));
}
/*
* adt_load_termid: convenience function; inputs file handle and
* outputs an au_tid_addr struct.
*
* This code was stolen from audit_settid.c; it differs from audit_settid()
* in that it does not write the terminal id to the process.
*/
int
adt_load_termid(int fd, adt_termid_t **termid)
{
au_tid_addr_t *p_term;
struct sockaddr_in6 peer;
struct sockaddr_in6 sock;
int peerlen = sizeof (peer);
int socklen = sizeof (sock);
*termid = NULL;
/* get peer name if its a socket, else assume local terminal */
if (getpeername(fd, (struct sockaddr *)&peer, (socklen_t *)&peerlen)
< 0) {
if (errno == ENOTSOCK)
return (adt_load_hostname(NULL, termid));
goto return_err;
}
if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL)
goto return_err;
/* get sock name */
if (getsockname(fd, (struct sockaddr *)&sock,
(socklen_t *)&socklen) < 0)
goto return_err_free;
if (peer.sin6_family == AF_INET6) {
adt_do_ipv6_address(&peer, &sock, p_term);
} else {
adt_do_ipv4_address((struct sockaddr_in *)&peer,
(struct sockaddr_in *)&sock, p_term);
}
*termid = (adt_termid_t *)p_term;
return (0);
return_err_free:
free(p_term);
return_err:
return (-1);
}
static boolean_t
adt_have_termid(au_tid_addr_t *dest)
{
struct auditinfo_addr audit_data;
if (getaudit_addr(&audit_data, sizeof (audit_data)) < 0) {
adt_write_syslog("getaudit failed", errno);
return (B_FALSE);
}
if ((audit_data.ai_termid.at_type == 0) ||
(audit_data.ai_termid.at_addr[0] |
audit_data.ai_termid.at_addr[1] |
audit_data.ai_termid.at_addr[2] |
audit_data.ai_termid.at_addr[3]) == 0)
return (B_FALSE);
(void) memcpy(dest, &(audit_data.ai_termid),
sizeof (au_tid_addr_t));
return (B_TRUE);
}
static int
adt_get_hostIP(const char *hostname, au_tid_addr_t *p_term)
{
struct addrinfo *ai;
void *p;
if (getaddrinfo(hostname, NULL, NULL, &ai) != 0)
return (-1);
switch (ai->ai_family) {
case AF_INET:
/* LINTED */
p = &((struct sockaddr_in *)ai->ai_addr)->sin_addr;
(void) memcpy(p_term->at_addr, p,
sizeof (((struct sockaddr_in *)NULL)->sin_addr));
p_term->at_type = AU_IPv4;
break;
case AF_INET6:
/* LINTED */
p = &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr,
(void) memcpy(p_term->at_addr, p,
sizeof (((struct sockaddr_in6 *)NULL)->sin6_addr));
p_term->at_type = AU_IPv6;
break;
default:
return (-1);
}
freeaddrinfo(ai);
return (0);
}
/*
* adt_load_hostname() is called when the caller does not have a file
* handle that gives access to the socket info or any other way to
* pass in both port and ip address. The hostname input is ignored if
* the terminal id has already been set; instead it returns the
* existing terminal id.
*
* If audit is off and the hostname lookup fails, no error is
* returned, since an error may be interpreted by the caller
* as grounds for denying a login. Otherwise the caller would
* need to be aware of the audit state.
*/
int
adt_load_hostname(const char *hostname, adt_termid_t **termid)
{
char localhost[ADT_STRING_MAX + 1];
au_tid_addr_t *p_term;
*termid = NULL;
if (!adt_audit_enabled())
return (0);
if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL)
goto return_err;
if (adt_have_termid(p_term)) {
*termid = (adt_termid_t *)p_term;
return (0);
}
p_term->at_port = 0;
if (hostname == NULL || *hostname == '\0') {
(void) sysinfo(SI_HOSTNAME, localhost, ADT_STRING_MAX);
hostname = localhost;
}
if (adt_get_hostIP(hostname, p_term))
goto return_err_free;
*termid = (adt_termid_t *)p_term;
return (0);
return_err_free:
free(p_term);
return_err:
if ((auditstate == AUC_DISABLED) ||
(auditstate == AUC_NOAUDIT))
return (0);
return (-1);
}
/*
* adt_load_ttyname() is called when the caller does not have a file
* handle that gives access to the local terminal or any other way
* of determining the device id. The ttyname input is ignored if
* the terminal id has already been set; instead it returns the
* existing terminal id.
*
* If audit is off and the ttyname lookup fails, no error is
* returned, since an error may be interpreted by the caller
* as grounds for denying a login. Otherwise the caller would
* need to be aware of the audit state.
*/
int
adt_load_ttyname(const char *ttyname, adt_termid_t **termid)
{
char localhost[ADT_STRING_MAX + 1];
au_tid_addr_t *p_term;
struct stat stat_buf;
*termid = NULL;
if (!adt_audit_enabled())
return (0);
if ((p_term = calloc(1, sizeof (au_tid_addr_t))) == NULL)
goto return_err;
if (adt_have_termid(p_term)) {
*termid = (adt_termid_t *)p_term;
return (0);
}
p_term->at_port = 0;
if (sysinfo(SI_HOSTNAME, localhost, ADT_STRING_MAX) < 0)
goto return_err_free; /* errno from sysinfo */
if (ttyname != NULL) {
if (stat(ttyname, &stat_buf) < 0)
goto return_err_free;
p_term->at_port = stat_buf.st_rdev;
}
if (adt_get_hostIP(localhost, p_term))
goto return_err_free;
*termid = (adt_termid_t *)p_term;
return (0);
return_err_free:
free(p_term);
return_err:
if ((auditstate == AUC_DISABLED) ||
(auditstate == AUC_NOAUDIT))
return (0);
return (-1);
}
/*
* adt_get_session_id returns a stringified representation of
* the audit session id. See also adt_get_asid() for how to
* get the unexpurgated version. No guarantees as to how long
* the returned string will be or its general form; hex for now.
*
* An empty string is returned if auditing is off; length = 1
* and the pointer is valid.
*
* returns strlen + 1 if buffer is valid; else 0 and errno.
*/
size_t
adt_get_session_id(const adt_session_data_t *session_data, char **buff)
{
au_asid_t session_id;
size_t length;
/*
* output is 0x followed by
* two characters per byte
* plus terminator,
* except leading 0's are suppressed, so a few bytes may
* be unused.
*/
length = 2 + (2 * sizeof (session_id)) + 1;
*buff = malloc(length);
if (*buff == NULL) {
return (0);
}
if (session_data == NULL) { /* NULL is not an error */
**buff = '\0';
return (1);
}
adt_get_asid(session_data, &session_id);
length = snprintf(*buff, length, "0x%X", (int)session_id);
/* length < 1 is a bug: the session data type may have changed */
assert(length > 0);
return (length);
}
/*
* adt_end_session -- close handle, clear context
*
* if as_check is invalid, no harm, no foul, EXCEPT that this could
* be an attempt to free data already free'd, so output to syslog
* to help explain why the process cored dumped.
*/
int
adt_end_session(adt_session_data_t *session_data)
{
adt_internal_state_t *state;
if (session_data != NULL) {
state = (adt_internal_state_t *)session_data;
if (state->as_check != ADT_VALID) {
adt_write_syslog("freeing invalid data", EINVAL);
} else {
state->as_check = 0;
m_label_free(state->as_label);
free(session_data);
}
}
/* no errors yet defined */
return (0);
}
/*
* adt_dup_session -- copy the session data
*/
int
adt_dup_session(const adt_session_data_t *source, adt_session_data_t **dest)
{
adt_internal_state_t *source_state;
adt_internal_state_t *dest_state = NULL;
int rc = 0;
if (source != NULL) {
source_state = (adt_internal_state_t *)source;
assert(source_state->as_check == ADT_VALID);
dest_state = malloc(sizeof (adt_internal_state_t));
if (dest_state == NULL) {
rc = -1;
goto return_rc;
}
(void) memcpy(dest_state, source,
sizeof (struct adt_internal_state));
if (source_state->as_label != NULL) {
dest_state->as_label = NULL;
if ((rc = m_label_dup(&dest_state->as_label,
source_state->as_label)) != 0) {
free(dest_state);
dest_state = NULL;
}
}
}
return_rc:
*dest = (adt_session_data_t *)dest_state;
return (rc);
}
/*
* from_export_format()
* read from a network order buffer into struct adt_session_data
*/
static size_t
adt_from_export_format(adt_internal_state_t *internal,
const adt_export_data_t *external)
{
struct export_header head;
struct export_link link;
adr_t context;
int32_t offset;
int32_t length;
int32_t version;
size_t label_len;
char *p = (char *)external;
adrm_start(&context, (char *)external);
adrm_int32(&context, (int *)&head, 4);
if ((internal->as_check = head.ax_check) != ADT_VALID) {
errno = EINVAL;
return (0);
}
offset = head.ax_link.ax_offset;
version = head.ax_link.ax_version;
length = head.ax_buffer_length;
/*
* Skip newer versions.
*/
while (version > PROTOCOL_VERSION_2) {
if (offset < 1) {
return (0); /* failed to match version */
}
p += offset; /* point to next version # */
if (p > (char *)external + length) {
return (0);
}
adrm_start(&context, p);
adrm_int32(&context, (int *)&link, 2);
offset = link.ax_offset;
version = link.ax_version;
assert(version != 0);
}
/*
* Adjust buffer pointer to the first data item (euid).
*/
if (p == (char *)external) {
adrm_start(&context, (char *)(p + sizeof (head)));
} else {
adrm_start(&context, (char *)(p + sizeof (link)));
}
/*
* if down rev version, neither pid nor label are included
* in v1 ax_size_of_tsol_data intentionally ignored
*/
if (version == PROTOCOL_VERSION_1) {
adrm_int32(&context, (int *)&(internal->as_euid), 1);
adrm_int32(&context, (int *)&(internal->as_ruid), 1);
adrm_int32(&context, (int *)&(internal->as_egid), 1);
adrm_int32(&context, (int *)&(internal->as_rgid), 1);
adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1);
adrm_int32(&context,
(int *)&(internal->as_info.ai_mask.am_success), 2);
adrm_int32(&context,
(int *)&(internal->as_info.ai_termid.at_port), 1);
adrm_int32(&context,
(int *)&(internal->as_info.ai_termid.at_type), 1);
adrm_int32(&context,
(int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1);
adrm_int32(&context, (int *)&(internal->as_audit_enabled), 1);
internal->as_pid = (pid_t)-1;
internal->as_label = NULL;
} else if (version == PROTOCOL_VERSION_2) {
adrm_int32(&context, (int *)&(internal->as_euid), 1);
adrm_int32(&context, (int *)&(internal->as_ruid), 1);
adrm_int32(&context, (int *)&(internal->as_egid), 1);
adrm_int32(&context, (int *)&(internal->as_rgid), 1);
adrm_int32(&context, (int *)&(internal->as_info.ai_auid), 1);
adrm_int32(&context,
(int *)&(internal->as_info.ai_mask.am_success), 2);
adrm_int32(&context,
(int *)&(internal->as_info.ai_termid.at_port), 1);
adrm_int32(&context,
(int *)&(internal->as_info.ai_termid.at_type), 1);
adrm_int32(&context,
(int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
adrm_int32(&context, (int *)&(internal->as_info.ai_asid), 1);
adrm_int32(&context, (int *)&(internal->as_audit_enabled), 1);
adrm_int32(&context, (int *)&(internal->as_pid), 1);
adrm_int32(&context, (int *)&label_len, 1);
if (label_len > 0) {
/* read in and deal with different sized labels. */
size_t my_label_len = blabel_size();
if ((internal->as_label =
m_label_alloc(MAC_LABEL)) == NULL) {
return (0);
}
if (label_len > my_label_len) {
errno = EINVAL;
m_label_free(internal->as_label);
return (0);
}
(void) memset(internal->as_label, 0, my_label_len);
adrm_int32(&context, (int *)(internal->as_label),
label_len / sizeof (int32_t));
} else {
internal->as_label = NULL;
}
}
return (length);
}
/*
* adt_to_export_format
* read from struct adt_session_data into a network order buffer.
*
* (network order 'cause this data may be shared with a remote host.)
*/
static size_t
adt_to_export_format(adt_export_data_t *external,
adt_internal_state_t *internal)
{
struct export_header head;
struct export_link tail;
adr_t context;
size_t label_len = 0;
adrm_start(&context, (char *)external);
if (internal->as_label != NULL) {
label_len = blabel_size();
}
head.ax_check = ADT_VALID;
head.ax_buffer_length = sizeof (struct adt_export_data) + label_len;
/* version 2 first */
head.ax_link.ax_version = PROTOCOL_VERSION_2;
head.ax_link.ax_offset = sizeof (struct export_header) +
sizeof (struct adt_export_v2) + label_len;
adrm_putint32(&context, (int *)&head, 4);
adrm_putint32(&context, (int *)&(internal->as_euid), 1);
adrm_putint32(&context, (int *)&(internal->as_ruid), 1);
adrm_putint32(&context, (int *)&(internal->as_egid), 1);
adrm_putint32(&context, (int *)&(internal->as_rgid), 1);
adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1);
adrm_putint32(&context,
(int *)&(internal->as_info.ai_mask.am_success), 2);
adrm_putint32(&context,
(int *)&(internal->as_info.ai_termid.at_port), 1);
adrm_putint32(&context,
(int *)&(internal->as_info.ai_termid.at_type), 1);
adrm_putint32(&context,
(int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1);
adrm_putint32(&context, (int *)&(internal->as_audit_enabled), 1);
adrm_putint32(&context, (int *)&(internal->as_pid), 1);
adrm_putint32(&context, (int *)&label_len, 1);
if (internal->as_label != NULL) {
/* serialize the label */
adrm_putint32(&context, (int *)(internal->as_label),
(label_len / sizeof (int32_t)));
}
/* now version 1 */
tail.ax_version = PROTOCOL_VERSION_1;
tail.ax_offset = 0;
adrm_putint32(&context, (int *)&tail, 2);
adrm_putint32(&context, (int *)&(internal->as_euid), 1);
adrm_putint32(&context, (int *)&(internal->as_ruid), 1);
adrm_putint32(&context, (int *)&(internal->as_egid), 1);
adrm_putint32(&context, (int *)&(internal->as_rgid), 1);
adrm_putint32(&context, (int *)&(internal->as_info.ai_auid), 1);
adrm_putint32(&context,
(int *)&(internal->as_info.ai_mask.am_success), 2);
adrm_putint32(&context,
(int *)&(internal->as_info.ai_termid.at_port), 1);
adrm_putint32(&context,
(int *)&(internal->as_info.ai_termid.at_type), 1);
adrm_putint32(&context,
(int *)&(internal->as_info.ai_termid.at_addr[0]), 4);
adrm_putint32(&context, (int *)&(internal->as_info.ai_asid), 1);
adrm_putint32(&context, (int *)&(internal->as_audit_enabled), 1);
/* ignored in v1 */
adrm_putint32(&context, (int *)&label_len, 1);
/* finally terminator */
tail.ax_version = 0; /* invalid version number */
tail.ax_offset = 0;
adrm_putint32(&context, (int *)&tail, 2);
return (head.ax_buffer_length);
}
/*
* adt_import_proc() is used by a server acting on behalf
* of a client which has connected via an ipc mechanism such as
* a door.
*
* Since the interface is via ucred, the info.ap_termid.port
* value is always the 64 bit version. What is stored depends
* on how libbsm is compiled.
*/
size_t
adt_import_proc(pid_t pid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid,
adt_export_data_t **external)
{
size_t length = 0;
adt_internal_state_t *state;
ucred_t *ucred;
const au_tid64_addr_t *tid;
state = calloc(1, sizeof (adt_internal_state_t));
if (state == NULL)
return (0);
if (adt_init(state, 0) != 0)
goto return_length_free; /* errno from adt_init() */
/*
* ucred_getauid() returns AU_NOAUDITID if audit is off, which
* is the right answer for adt_import_proc().
*
* Create a local context as near as possible.
*/
ucred = ucred_get(pid);
if (ucred == NULL)
goto return_length_free;
state->as_ruid = ruid != ADT_NO_CHANGE ? ruid : ucred_getruid(ucred);
state->as_euid = euid != ADT_NO_CHANGE ? euid : ucred_geteuid(ucred);
state->as_rgid = rgid != ADT_NO_CHANGE ? rgid : ucred_getrgid(ucred);
state->as_egid = egid != ADT_NO_CHANGE ? egid : ucred_getegid(ucred);
state->as_info.ai_auid = ucred_getauid(ucred);
if (state->as_info.ai_auid == AU_NOAUDITID) {
state->as_info.ai_asid = adt_get_unique_id(ruid);
if (adt_get_mask_from_user(ruid, &(state->as_info.ai_mask)))
goto return_all_free;
} else {
const au_mask_t *mask = ucred_getamask(ucred);
if (mask != NULL)
state->as_info.ai_mask = *mask;
else
goto return_all_free;
state->as_info.ai_asid = ucred_getasid(ucred);
}
tid = ucred_getatid(ucred);
if (tid != NULL) {
adt_cpy_tid(&(state->as_info.ai_termid), tid);
} else {
(void) memset((void *)&(state->as_info.ai_termid), 0,
sizeof (au_tid_addr_t));
state->as_info.ai_termid.at_type = AU_IPv4;
}
DPRINTF(("import_proc/asid = %X %u\n", state->as_info.ai_asid,
state->as_info.ai_asid));
DPRINTF(("import_proc/masks = %X %X\n",
state->as_info.ai_mask.am_success,
state->as_info.ai_mask.am_failure));
if (state->as_label == NULL) {
*external = malloc(sizeof (adt_export_data_t));
} else {
*external = malloc(sizeof (adt_export_data_t) + blabel_size());
}
if (*external == NULL)
goto return_all_free;
length = adt_to_export_format(*external, state);
/*
* yes, state is supposed to be free'd for both pass and fail
*/
return_all_free:
ucred_free(ucred);
return_length_free:
free(state);
return (length);
}
/*
* adt_ucred_label() -- if label is available, duplicate it.
*/
static m_label_t *
adt_ucred_label(ucred_t *uc)
{
m_label_t *ul = NULL;
if (ucred_getlabel(uc) != NULL) {
(void) m_label_dup(&ul, ucred_getlabel(uc));
}
return (ul);
}
/*
* adt_import() -- convert from network order to machine-specific order
*/
static int
adt_import(adt_internal_state_t *internal, const adt_export_data_t *external)
{
au_mask_t mask;
/* save local audit enabled state */
int local_audit_enabled = internal->as_audit_enabled;
if (adt_from_export_format(internal, external) < 1)
return (-1); /* errno from adt_from_export_format */
/*
* If audit isn't enabled on the remote, they were unable
* to generate the audit mask, so generate it based on
* local configuration. If the user id has changed, the
* resulting mask may miss some subtleties that occurred
* on the remote system.
*
* If the remote failed to generate a terminal id, it is not
* recoverable.
*/
if (!internal->as_audit_enabled) {
if (adt_get_mask_from_user(internal->as_info.ai_auid,
&(internal->as_info.ai_mask)))
return (-1);
if (internal->as_info.ai_auid != internal->as_ruid) {
if (adt_get_mask_from_user(internal->as_info.ai_auid,
&mask))
return (-1);
internal->as_info.ai_mask.am_success |=
mask.am_success;
internal->as_info.ai_mask.am_failure |=
mask.am_failure;
}
}
internal->as_audit_enabled = local_audit_enabled;
DPRINTF(("(%d)imported asid = %X %u\n", getpid(),
internal->as_info.ai_asid,
internal->as_info.ai_asid));
internal->as_have_user_data = ADT_HAVE_ALL;
return (0);
}
/*
* adt_export_session_data()
* copies a adt_session_data struct into a network order buffer
*
* In a misconfigured network, the local host may have auditing
* off while the destination may have auditing on, so if there
* is sufficient memory, a buffer will be returned even in the
* audit off case.
*/
size_t
adt_export_session_data(const adt_session_data_t *internal,
adt_export_data_t **external)
{
size_t length = 0;
if ((internal != NULL) &&
((adt_internal_state_t *)internal)->as_label != NULL) {
length = blabel_size();
}
*external = malloc(sizeof (adt_export_data_t) + length);
if (*external == NULL)
return (0);
if (internal == NULL) {
adt_internal_state_t *dummy;
dummy = malloc(sizeof (adt_internal_state_t));
if (dummy == NULL)
goto return_length_free;
if (adt_init(dummy, 0)) { /* 0 == don't copy from proc */
free(dummy);
goto return_length_free;
}
length = adt_to_export_format(*external, dummy);
free(dummy);
} else {
length = adt_to_export_format(*external,
(adt_internal_state_t *)internal);
}
return (length);
return_length_free:
free(*external);
*external = NULL;
return (0);
}
static void
adt_setto_unaudited(adt_internal_state_t *state)
{
state->as_ruid = AU_NOAUDITID;
state->as_euid = AU_NOAUDITID;
state->as_rgid = AU_NOAUDITID;
state->as_egid = AU_NOAUDITID;
state->as_pid = (pid_t)-1;
state->as_label = NULL;
if (state->as_audit_enabled) {
state->as_info.ai_asid = 0;
state->as_info.ai_auid = AU_NOAUDITID;
(void) memset((void *)&(state->as_info.ai_termid), 0,
sizeof (au_tid_addr_t));
state->as_info.ai_termid.at_type = AU_IPv4;
(void) memset((void *)&(state->as_info.ai_mask), 0,
sizeof (au_mask_t));
state->as_have_user_data = 0;
}
}
/*
* adt_init -- set session context by copying the audit characteristics
* from the proc and picking up current uid/tid information.
*
* By default, an audit session is based on the process; the default
* is overriden by adt_set_user()
*/
static int
adt_init(adt_internal_state_t *state, int use_proc_data)
{
state->as_audit_enabled = (auditstate == AUC_DISABLED) ? 0 : 1;
if (use_proc_data) {
state->as_ruid = getuid();
state->as_euid = geteuid();
state->as_rgid = getgid();
state->as_egid = getegid();
state->as_pid = getpid();
if (state->as_audit_enabled) {
const au_tid64_addr_t *tid;
const au_mask_t *mask;
ucred_t *ucred = ucred_get(P_MYID);
/*
* Even if the ucred is NULL, the underlying
* credential may have a valid terminal id; if the
* terminal id is set, then that's good enough. An
* example of where this matters is failed login,
* where rlogin/telnet sets the terminal id before
* calling login; login does not load the credential
* since auth failed.
*/
if (ucred == NULL) {
if (!adt_have_termid(
&(state->as_info.ai_termid)))
return (-1);
} else {
mask = ucred_getamask(ucred);
if (mask != NULL) {
state->as_info.ai_mask = *mask;
} else {
ucred_free(ucred);
return (-1);
}
tid = ucred_getatid(ucred);
if (tid != NULL) {
adt_cpy_tid(&(state->as_info.ai_termid),
tid);
} else {
ucred_free(ucred);
return (-1);
}
state->as_info.ai_asid = ucred_getasid(ucred);
state->as_info.ai_auid = ucred_getauid(ucred);
state->as_label = adt_ucred_label(ucred);
ucred_free(ucred);
}
state->as_have_user_data = ADT_HAVE_ALL;
}
} else {
adt_setto_unaudited(state);
}
state->as_session_model = ADT_SESSION_MODEL; /* default */
if (state->as_audit_enabled &&
auditon(A_GETPOLICY, (caddr_t)&(state->as_kernel_audit_policy),
sizeof (state->as_kernel_audit_policy))) {
return (-1); /* errno set by auditon */
}
state->as_check = ADT_VALID;
return (0);
}
/*
* adt_set_proc
*
* Copy the current session state to the process. If this function
* is called, the model becomes a process model rather than a
* session model.
*
* In the current implementation, the value state->as_have_user_data
* must contain all of: ADT_HAVE_{AUID,MASK,TID,ASID}. These are all set
* by adt_set_user() when the ADT_SETTID or ADT_NEW flag is passed in.
*
*/
int
adt_set_proc(const adt_session_data_t *session_data)
{
int rc;
adt_internal_state_t *state;
if (auditstate == AUC_DISABLED || (session_data == NULL))
return (0);
state = (adt_internal_state_t *)session_data;
assert(state->as_check == ADT_VALID);
if ((state->as_have_user_data & (ADT_HAVE_ALL & ~ADT_HAVE_IDS)) !=
(ADT_HAVE_ALL & ~ADT_HAVE_IDS)) {
errno = EINVAL;
goto return_err;
}
rc = setaudit_addr((auditinfo_addr_t *)&(state->as_info),
sizeof (auditinfo_addr_t));
if (rc < 0)
goto return_err; /* errno set by setaudit_addr() */
state->as_session_model = ADT_PROCESS_MODEL;
return (0);
return_err:
adt_write_syslog("failed to set process audit characteristics", errno);
return (-1);
}
static int
adt_newuser(adt_internal_state_t *state, uid_t ruid, au_tid_addr_t *termid)
{
au_tid_addr_t no_tid = {0, AU_IPv4, 0, 0, 0, 0};
au_mask_t no_mask = {0, 0};
if (ruid == ADT_NO_AUDIT) {
state->as_info.ai_auid = AU_NOAUDITID;
state->as_info.ai_asid = 0;
state->as_info.ai_termid = no_tid;
state->as_info.ai_mask = no_mask;
return (0);
}
state->as_info.ai_auid = ruid;
state->as_info.ai_asid = adt_get_unique_id(ruid);
if (termid != NULL)
state->as_info.ai_termid = *termid;
if (adt_get_mask_from_user(ruid, &(state->as_info.ai_mask)))
return (-1);
/* Assume intending to audit as this process */
if (state->as_pid == (pid_t)-1)
state->as_pid = getpid();
if (is_system_labeled() && state->as_label == NULL) {
ucred_t *ucred = ucred_get(P_MYID);
state->as_label = adt_ucred_label(ucred);
ucred_free(ucred);
}
return (0);
}
static int
adt_changeuser(adt_internal_state_t *state, uid_t ruid)
{
au_mask_t mask;
if (!(state->as_have_user_data & ADT_HAVE_AUID))
state->as_info.ai_auid = ruid;
if (!(state->as_have_user_data & ADT_HAVE_ASID))
state->as_info.ai_asid = adt_get_unique_id(ruid);
if (ruid >= 0) {
if (adt_get_mask_from_user(ruid, &mask))
return (-1);
state->as_info.ai_mask.am_success |= mask.am_success;
state->as_info.ai_mask.am_failure |= mask.am_failure;
}
DPRINTF(("changed mask to %08X/%08X for ruid=%d\n",
state->as_info.ai_mask.am_success,
state->as_info.ai_mask.am_failure,
ruid));
return (0);
}
/*
* adt_set_user -- see also adt_set_from_ucred()
*
* ADT_NO_ATTRIB is a valid uid/gid meaning "not known" or
* "unattributed." If ruid, change the model to session.
*
* ADT_NO_CHANGE is a valid uid/gid meaning "do not change this value"
* only valid with ADT_UPDATE.
*
* ADT_NO_AUDIT is the external equivalent to AU_NOAUDITID -- there
* isn't a good reason to call adt_set_user() with it unless you don't
* have a good value yet and intend to replace it later; auid will be
* AU_NOAUDITID.
*
* adt_set_user should be called even if auditing is not enabled
* so that adt_export_session_data() will have useful stuff to
* work with.
*
* See the note preceding adt_set_proc() about the use of ADT_HAVE_TID
* and ADT_HAVE_ALL.
*/
int
adt_set_user(const adt_session_data_t *session_data, uid_t euid, gid_t egid,
uid_t ruid, gid_t rgid, const adt_termid_t *termid,
enum adt_user_context user_context)
{
adt_internal_state_t *state;
int rc;
if (session_data == NULL) /* no session exists to audit */
return (0);
state = (adt_internal_state_t *)session_data;
assert(state->as_check == ADT_VALID);
switch (user_context) {
case ADT_NEW:
if (ruid == ADT_NO_CHANGE || euid == ADT_NO_CHANGE ||
rgid == ADT_NO_CHANGE || egid == ADT_NO_CHANGE) {
errno = EINVAL;
return (-1);
}
if ((rc = adt_newuser(state, ruid,
(au_tid_addr_t *)termid)) != 0)
return (rc);
state->as_have_user_data = ADT_HAVE_ALL;
break;
case ADT_UPDATE:
if (state->as_have_user_data != ADT_HAVE_ALL) {
errno = EINVAL;
return (-1);
}
if (ruid != ADT_NO_CHANGE)
if ((rc = adt_changeuser(state, ruid)) != 0)
return (rc);
break;
case ADT_USER:
if (state->as_have_user_data != ADT_HAVE_ALL) {
errno = EINVAL;
return (-1);
}
break;
case ADT_SETTID:
assert(termid != NULL);
state->as_info.ai_termid = *((au_tid_addr_t *)termid);
/* avoid fooling pam_setcred()... */
state->as_info.ai_auid = AU_NOAUDITID;
state->as_info.ai_asid = 0;
state->as_info.ai_mask.am_failure = 0;
state->as_info.ai_mask.am_success = 0;
state->as_have_user_data = ADT_HAVE_TID |
ADT_HAVE_AUID | ADT_HAVE_ASID | ADT_HAVE_MASK;
return (0);
default:
errno = EINVAL;
return (-1);
}
if (ruid == ADT_NO_AUDIT) {
state->as_ruid = AU_NOAUDITID;
state->as_euid = AU_NOAUDITID;
state->as_rgid = AU_NOAUDITID;
state->as_egid = AU_NOAUDITID;
} else {
if (ruid != ADT_NO_CHANGE)
state->as_ruid = ruid;
if (euid != ADT_NO_CHANGE)
state->as_euid = euid;
if (rgid != ADT_NO_CHANGE)
state->as_rgid = rgid;
if (egid != ADT_NO_CHANGE)
state->as_egid = egid;
}
if (ruid == ADT_NO_ATTRIB) {
state->as_session_model = ADT_SESSION_MODEL;
}
return (0);
}
/*
* adt_set_from_ucred()
*
* an alternate to adt_set_user that fills the same role but uses
* a pointer to a ucred rather than a list of id's. If the ucred
* pointer is NULL, use the credential from the this process.
*
* A key difference is that for ADT_NEW, adt_set_from_ucred() does
* not overwrite the asid and auid unless auid has not been set.
* ADT_NEW differs from ADT_UPDATE in that it does not OR together
* the incoming audit mask with the one that already exists.
*
* adt_set_from_ucred should be called even if auditing is not enabled
* so that adt_export_session_data() will have useful stuff to
* work with.
*/
int
adt_set_from_ucred(const adt_session_data_t *session_data, const ucred_t *uc,
enum adt_user_context user_context)
{
adt_internal_state_t *state;
int rc = -1;
const au_tid64_addr_t *tid64;
au_tid_addr_t termid, *tid;
ucred_t *ucred = (ucred_t *)uc;
boolean_t local_uc = B_FALSE;
if (session_data == NULL) /* no session exists to audit */
return (0);
state = (adt_internal_state_t *)session_data;
assert(state->as_check == ADT_VALID);
if (ucred == NULL) {
ucred = ucred_get(P_MYID);
if (ucred == NULL)
goto return_rc;
local_uc = B_TRUE;
}
switch (user_context) {
case ADT_NEW:
tid64 = ucred_getatid(ucred);
if (tid64 != NULL) {
adt_cpy_tid(&termid, tid64);
tid = &termid;
} else {
tid = NULL;
}
if (ucred_getauid(ucred) == AU_NOAUDITID) {
adt_setto_unaudited(state);
state->as_have_user_data = ADT_HAVE_ALL;
rc = 0;
goto return_rc;
} else {
state->as_info.ai_auid = ucred_getauid(ucred);
state->as_info.ai_asid = ucred_getasid(ucred);
state->as_info.ai_mask = *ucred_getamask(ucred);
state->as_info.ai_termid = *tid;
}
state->as_have_user_data = ADT_HAVE_ALL;
break;
case ADT_UPDATE:
if (state->as_have_user_data != ADT_HAVE_ALL) {
errno = EINVAL;
goto return_rc;
}
if ((rc = adt_changeuser(state, ucred_getruid(ucred))) != 0)
goto return_rc;
break;
case ADT_USER:
if (state->as_have_user_data != ADT_HAVE_ALL) {
errno = EINVAL;
goto return_rc;
}
break;
default:
errno = EINVAL;
goto return_rc;
}
rc = 0;
state->as_ruid = ucred_getruid(ucred);
state->as_euid = ucred_geteuid(ucred);
state->as_rgid = ucred_getrgid(ucred);
state->as_egid = ucred_getegid(ucred);
state->as_pid = ucred_getpid(ucred);
state->as_label = adt_ucred_label(ucred);
return_rc:
if (local_uc) {
ucred_free(ucred);
}
return (rc);
}
/*
* adt_alloc_event() returns a pointer to allocated memory
*
*/
adt_event_data_t
*adt_alloc_event(const adt_session_data_t *session_data, au_event_t event_id)
{
struct adt_event_state *event_state;
adt_internal_state_t *session_state;
adt_event_data_t *return_event = NULL;
/*
* need to return a valid event pointer even if audit is
* off, else the caller will end up either (1) keeping its
* own flags for on/off or (2) writing to a NULL pointer.
* If auditing is on, the session data must be valid; otherwise
* we don't care.
*/
if (session_data != NULL) {
session_state = (adt_internal_state_t *)session_data;
assert(session_state->as_check == ADT_VALID);
}
event_state = calloc(1, sizeof (struct adt_event_state));
if (event_state == NULL)
goto return_ptr;
event_state->ae_check = ADT_VALID;
event_state->ae_event_id = event_id;
event_state->ae_session = (struct adt_internal_state *)session_data;
return_event = (adt_event_data_t *)&(event_state->ae_event_data);
/*
* preload data so the adt_au_*() functions can detect un-supplied
* values (0 and NULL are free via calloc()).
*/
adt_preload(event_id, return_event);
return_ptr:
return (return_event);
}
/*
* adt_getXlateTable -- look up translation table address for event id
*/
static struct translation *
adt_getXlateTable(au_event_t event_id)
{
/* xlate_table is global in adt_xlate.c */
struct translation **p_xlate = &xlate_table[0];
struct translation *p_event;
while (*p_xlate != NULL) {
p_event = *p_xlate;
if (event_id == p_event->tx_external_event)
return (p_event);
p_xlate++;
}
return (NULL);
}
/*
* adt_calcOffsets
*
* the call to this function is surrounded by a mutex.
*
* i walks down the table picking up next_token. j walks again to
* calculate the offset to the input data. k points to the next
* token's row. Finally, l, is used to sum the values in the
* datadef array.
*
* What's going on? The entry array is in the order of the input
* fields but the processing of array entries is in the order of
* the output (see next_token). Calculating the offset to the
* "next" input can't be done in the outer loop (i) since i doesn't
* point to the current entry and it can't be done with the k index
* because it doesn't represent the order of input fields.
*
* While the resulting algorithm is n**2, it is only done once per
* event type.
*/
/*
* adt_calcOffsets is only called once per event type, but it uses
* the address alignment of memory allocated for that event as if it
* were the same for all subsequently allocated memory. This is
* guaranteed by calloc/malloc. Arrays take special handling since
* what matters for figuring out the correct alignment is the size
* of the array element.
*/
static void
adt_calcOffsets(struct entry *p_entry, int tablesize, void *p_data)
{
int i, j;
size_t this_size, prev_size;
void *struct_start = p_data;
for (i = 0; i < tablesize; i++) {
if (p_entry[i].en_type_def == NULL) {
p_entry[i].en_offset = 0;
continue;
}
prev_size = 0;
p_entry[i].en_offset = (char *)p_data - (char *)struct_start;
for (j = 0; j < p_entry[i].en_count_types; j++) {
if (p_entry[i].en_type_def[j].dd_datatype == ADT_MSG)
this_size = sizeof (enum adt_generic);
else
this_size =
p_entry[i].en_type_def[j].dd_input_size;
/* adj for first entry */
if (prev_size == 0)
prev_size = this_size;
if (p_entry[i].en_type_def[j].dd_datatype ==
ADT_UINT32ARRAY) {
p_data = (char *)adt_adjust_address(p_data,
prev_size, sizeof (uint32_t)) +
this_size - sizeof (uint32_t);
prev_size = sizeof (uint32_t);
} else {
p_data = adt_adjust_address(p_data, prev_size,
this_size);
prev_size = this_size;
}
}
}
}
/*
* adt_generate_event
* generate event record from external struct. The order is based on
* the output tokens, allowing for the possibility that the input data
* is in a different order.
*
*/
static void
adt_generate_event(const adt_event_data_t *p_extdata,
struct adt_event_state *p_event,
struct translation *p_xlate)
{
struct entry *p_entry;
static mutex_t lock = DEFAULTMUTEX;
p_entry = p_xlate->tx_first_entry;
assert(p_entry != NULL);
p_event->ae_internal_id = p_xlate->tx_internal_event;
adt_token_open(p_event);
/*
* offsets are not pre-calculated; the initial offsets are all
* 0; valid offsets are >= 0. Offsets for no-input tokens such
* as subject are set to -1 by adt_calcOffset()
*/
if (p_xlate->tx_offsetsCalculated == 0) {
(void) _mutex_lock(&lock);
p_xlate->tx_offsetsCalculated = 1;
adt_calcOffsets(p_xlate->tx_top_entry, p_xlate->tx_entries,
(void *)p_extdata);
(void) _mutex_unlock(&lock);
}
while (p_entry != NULL) {
adt_generate_token(p_entry, (char *)p_extdata,
p_event);
p_entry = p_entry->en_next_token;
}
adt_token_close(p_event);
}
/*
* adt_put_event -- main event generation function.
* The input "event" is the address of the struct containing
* event-specific data.
*
* However if auditing is off or the session handle
* is NULL, no attempt to write a record is made.
*/
int
adt_put_event(const adt_event_data_t *event, int status, int return_val)
{
struct adt_event_state *event_state;
struct translation *xlate;
int rc = 0;
if (event == NULL) {
errno = EINVAL;
rc = -1;
goto return_rc;
}
event_state = (struct adt_event_state *)event;
/* if audit off or this is a broken session, exit */
if (auditstate == AUC_DISABLED || (event_state->ae_session == NULL))
goto return_rc;
assert(event_state->ae_check == ADT_VALID);
event_state->ae_rc = status;
event_state->ae_type = return_val;
/* look up the event */
xlate = adt_getXlateTable(event_state->ae_event_id);
if (xlate == NULL) {
errno = EINVAL;
rc = -1;
goto return_rc;
}
DPRINTF(("got event %d\n", xlate->tx_internal_event));
if (adt_selected(event_state, xlate->tx_internal_event, status))
adt_generate_event(event, event_state, xlate);
return_rc:
return (rc);
}
/*
* adt_free_event -- invalidate and free
*/
void
adt_free_event(adt_event_data_t *event)
{
struct adt_event_state *event_state;
if (event == NULL)
return;
event_state = (struct adt_event_state *)event;
assert(event_state->ae_check == ADT_VALID);
event_state->ae_check = 0;
free(event_state);
}
/*
* adt_is_selected -- helper to adt_selected(), below.
*
* "sorf" is "success or fail" status; au_preselect compares
* that with success, fail, or both.
*/
static int
adt_is_selected(au_event_t e, au_mask_t *m, int sorf)
{
int prs_sorf;
if (sorf == 0)
prs_sorf = AU_PRS_SUCCESS;
else
prs_sorf = AU_PRS_FAILURE;
return (au_preselect(e, m, prs_sorf, AU_PRS_REREAD));
}
/*
* selected -- see if this event is preselected.
*
* if errors are encountered trying to check a preselection mask
* or look up a user name, the event is selected. Otherwise, the
* preselection mask is used for the job.
*/
static int
adt_selected(struct adt_event_state *event, au_event_t actual_id, int status)
{
adt_internal_state_t *sp;
au_mask_t namask;
sp = event->ae_session;
if ((sp->as_have_user_data & ADT_HAVE_IDS) == 0) {
adt_write_syslog("No user data available", EINVAL);
return (1); /* default is "selected" */
}
/* non-attributable? */
if ((sp->as_info.ai_auid == AU_NOAUDITID) ||
(sp->as_info.ai_auid == ADT_NO_AUDIT)) {
if (auditon(A_GETKMASK, (caddr_t)&namask,
sizeof (namask)) != 0) {
adt_write_syslog("auditon failure", errno);
return (1);
}
return (adt_is_selected(actual_id, &namask, status));
} else {
return (adt_is_selected(actual_id, &(sp->as_info.ai_mask),
status));
}
}
|