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
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* policy.c Bus security policy
*
* Copyright (C) 2003, 2004 Red Hat, Inc.
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <config.h>
#include "check.h"
#include "policy.h"
#include "services.h"
#include "test.h"
#include "utils.h"
#include "smack.h"
#include <dbus/dbus-list.h>
#include <dbus/dbus-hash.h>
#include <dbus/dbus-internals.h>
BusPolicyRule*
bus_policy_rule_new (BusPolicyRuleType type,
BusPolicyRuleAccess access)
{
BusPolicyRule *rule;
rule = dbus_new0 (BusPolicyRule, 1);
if (rule == NULL)
return NULL;
rule->type = type;
rule->refcount = 1;
rule->access = access;
switch (rule->type)
{
case BUS_POLICY_RULE_USER:
rule->d.user.uid = DBUS_UID_UNSET;
break;
case BUS_POLICY_RULE_GROUP:
rule->d.group.gid = DBUS_GID_UNSET;
break;
case BUS_POLICY_RULE_SEND:
rule->d.send.message_type = DBUS_MESSAGE_TYPE_INVALID;
/* allow rules default to TRUE (only requested replies allowed)
* check rules default to TRUE (only requested replies are checked)
* deny rules default to FALSE (only unrequested replies denied)
*/
rule->d.send.requested_reply = rule->access != BUS_POLICY_RULE_ACCESS_DENY;
break;
case BUS_POLICY_RULE_RECEIVE:
rule->d.receive.message_type = DBUS_MESSAGE_TYPE_INVALID;
/* allow rules default to TRUE (only requested replies allowed)
* check rules default to TRUE (only requested replies are checked)
* deny rules default to FALSE (only unrequested replies denied)
*/
rule->d.receive.requested_reply = rule->access != BUS_POLICY_RULE_ACCESS_DENY;
break;
case BUS_POLICY_RULE_OWN:
break;
}
return rule;
}
BusPolicyRule *
bus_policy_rule_ref (BusPolicyRule *rule)
{
_dbus_assert (rule->refcount > 0);
rule->refcount += 1;
return rule;
}
void
bus_policy_rule_unref (BusPolicyRule *rule)
{
_dbus_assert (rule->refcount > 0);
rule->refcount -= 1;
if (rule->refcount == 0)
{
switch (rule->type)
{
case BUS_POLICY_RULE_SEND:
dbus_free (rule->d.send.path);
dbus_free (rule->d.send.interface);
dbus_free (rule->d.send.member);
dbus_free (rule->d.send.error);
dbus_free (rule->d.send.destination);
break;
case BUS_POLICY_RULE_RECEIVE:
dbus_free (rule->d.receive.path);
dbus_free (rule->d.receive.interface);
dbus_free (rule->d.receive.member);
dbus_free (rule->d.receive.error);
dbus_free (rule->d.receive.origin);
break;
case BUS_POLICY_RULE_OWN:
dbus_free (rule->d.own.service_name);
break;
case BUS_POLICY_RULE_USER:
break;
case BUS_POLICY_RULE_GROUP:
break;
}
dbus_free (rule->privilege);
dbus_free (rule);
}
}
struct BusPolicy
{
int refcount;
DBusList *default_rules; /**< Default policy rules */
DBusList *mandatory_rules; /**< Mandatory policy rules */
DBusHashTable *rules_by_uid; /**< per-UID policy rules */
DBusHashTable *rules_by_gid; /**< per-GID policy rules */
DBusHashTable *rules_by_smack_label; /**< per-SMACK label policy rules */
DBusList *at_console_true_rules; /**< console user policy rules where at_console="true"*/
DBusList *at_console_false_rules; /**< console user policy rules where at_console="false"*/
};
static void
free_rule_func (void *data,
void *user_data)
{
BusPolicyRule *rule = data;
bus_policy_rule_unref (rule);
}
static void
free_rule_list_func (void *data)
{
DBusList **list = data;
if (list == NULL) /* DBusHashTable is on crack */
return;
_dbus_list_foreach (list, free_rule_func, NULL);
_dbus_list_clear (list);
dbus_free (list);
}
BusPolicy*
bus_policy_new (void)
{
BusPolicy *policy;
policy = dbus_new0 (BusPolicy, 1);
if (policy == NULL)
return NULL;
policy->refcount = 1;
policy->rules_by_uid = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
NULL,
free_rule_list_func);
if (policy->rules_by_uid == NULL)
goto failed;
policy->rules_by_gid = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
NULL,
free_rule_list_func);
if (policy->rules_by_gid == NULL)
goto failed;
#ifdef DBUS_ENABLE_SMACK
policy->rules_by_smack_label = _dbus_hash_table_new (DBUS_HASH_STRING,
(DBusFreeFunction) dbus_free,
free_rule_list_func);
if (policy->rules_by_smack_label == NULL)
goto failed;
#endif
return policy;
failed:
bus_policy_unref (policy);
return NULL;
}
BusPolicy *
bus_policy_ref (BusPolicy *policy)
{
_dbus_assert (policy->refcount > 0);
policy->refcount += 1;
return policy;
}
void
bus_policy_unref (BusPolicy *policy)
{
_dbus_assert (policy->refcount > 0);
policy->refcount -= 1;
if (policy->refcount == 0)
{
_dbus_list_foreach (&policy->default_rules, free_rule_func, NULL);
_dbus_list_clear (&policy->default_rules);
_dbus_list_foreach (&policy->mandatory_rules, free_rule_func, NULL);
_dbus_list_clear (&policy->mandatory_rules);
_dbus_list_foreach (&policy->at_console_true_rules, free_rule_func, NULL);
_dbus_list_clear (&policy->at_console_true_rules);
_dbus_list_foreach (&policy->at_console_false_rules, free_rule_func, NULL);
_dbus_list_clear (&policy->at_console_false_rules);
if (policy->rules_by_uid)
{
_dbus_hash_table_unref (policy->rules_by_uid);
policy->rules_by_uid = NULL;
}
if (policy->rules_by_gid)
{
_dbus_hash_table_unref (policy->rules_by_gid);
policy->rules_by_gid = NULL;
}
if (policy->rules_by_smack_label)
{
_dbus_hash_table_unref (policy->rules_by_smack_label);
policy->rules_by_smack_label = NULL;
}
dbus_free (policy);
}
}
static dbus_bool_t
add_list_to_client (DBusList **list,
BusClientPolicy *client)
{
DBusList *link;
link = _dbus_list_get_first_link (list);
while (link != NULL)
{
BusPolicyRule *rule = link->data;
link = _dbus_list_get_next_link (list, link);
switch (rule->type)
{
case BUS_POLICY_RULE_USER:
case BUS_POLICY_RULE_GROUP:
/* These aren't per-connection policies */
break;
case BUS_POLICY_RULE_OWN:
case BUS_POLICY_RULE_SEND:
case BUS_POLICY_RULE_RECEIVE:
/* These are per-connection */
if (!bus_client_policy_append_rule (client, rule))
return FALSE;
break;
}
}
return TRUE;
}
BusClientPolicy*
bus_policy_create_client_policy (BusPolicy *policy,
DBusConnection *connection,
DBusError *error)
{
BusClientPolicy *client;
dbus_uid_t uid;
dbus_bool_t at_console;
_dbus_assert (dbus_connection_get_is_authenticated (connection));
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
client = bus_client_policy_new ();
if (client == NULL)
goto nomem;
if (!add_list_to_client (&policy->default_rules,
client))
goto nomem;
/* we avoid the overhead of looking up user's groups
* if we don't have any group rules anyway
*/
if (_dbus_hash_table_get_n_entries (policy->rules_by_gid) > 0)
{
unsigned long *groups;
int n_groups;
int i;
if (!bus_connection_get_unix_groups (connection, &groups, &n_groups, error))
goto failed;
i = 0;
while (i < n_groups)
{
DBusList **list;
list = _dbus_hash_table_lookup_uintptr (policy->rules_by_gid,
groups[i]);
if (list != NULL)
{
if (!add_list_to_client (list, client))
{
dbus_free (groups);
goto nomem;
}
}
++i;
}
dbus_free (groups);
}
if (dbus_connection_get_unix_user (connection, &uid))
{
if (_dbus_hash_table_get_n_entries (policy->rules_by_uid) > 0)
{
DBusList **list;
list = _dbus_hash_table_lookup_uintptr (policy->rules_by_uid,
uid);
if (list != NULL)
{
if (!add_list_to_client (list, client))
goto nomem;
}
}
/* Add console rules */
at_console = _dbus_unix_user_is_at_console (uid, error);
if (at_console)
{
if (!add_list_to_client (&policy->at_console_true_rules, client))
goto nomem;
}
else if (dbus_error_is_set (error) == TRUE)
{
goto failed;
}
else if (!add_list_to_client (&policy->at_console_false_rules, client))
{
goto nomem;
}
}
if (policy->rules_by_smack_label &&
_dbus_hash_table_get_n_entries (policy->rules_by_smack_label) > 0)
{
DBusList **list;
dbus_bool_t nomem_err = FALSE;
list = bus_smack_generate_allowed_list(connection, policy->rules_by_smack_label, &nomem_err);
if (list != NULL)
{
nomem_err = !add_list_to_client (list, client);
_dbus_list_clear (list);
dbus_free(list);
}
if (nomem_err)
goto nomem;
}
if (!add_list_to_client (&policy->mandatory_rules,
client))
goto nomem;
bus_client_policy_optimize (client);
return client;
nomem:
BUS_SET_OOM (error);
failed:
_DBUS_ASSERT_ERROR_IS_SET (error);
if (client)
bus_client_policy_unref (client);
return NULL;
}
static dbus_bool_t
list_allows_user (dbus_bool_t def,
DBusList **list,
unsigned long uid,
const unsigned long *group_ids,
int n_group_ids)
{
DBusList *link;
dbus_bool_t allowed;
allowed = def;
link = _dbus_list_get_first_link (list);
while (link != NULL)
{
BusPolicyRule *rule = link->data;
link = _dbus_list_get_next_link (list, link);
if (rule->type == BUS_POLICY_RULE_USER)
{
_dbus_verbose ("List %p user rule uid="DBUS_UID_FORMAT"\n",
list, rule->d.user.uid);
if (rule->d.user.uid == DBUS_UID_UNSET)
; /* '*' wildcard */
else if (rule->d.user.uid != uid)
continue;
}
else if (rule->type == BUS_POLICY_RULE_GROUP)
{
_dbus_verbose ("List %p group rule gid="DBUS_GID_FORMAT"\n",
list, rule->d.group.gid);
if (rule->d.group.gid == DBUS_GID_UNSET)
; /* '*' wildcard */
else
{
int i;
i = 0;
while (i < n_group_ids)
{
if (rule->d.group.gid == group_ids[i])
break;
++i;
}
if (i == n_group_ids)
continue;
}
}
else
continue;
/* We don't intend to support <check user="..." /> and <check group="..." />
rules. They are treated like deny.
*/
allowed = rule->access == BUS_POLICY_RULE_ACCESS_ALLOW;
}
return allowed;
}
dbus_bool_t
bus_policy_allow_unix_user (BusPolicy *policy,
unsigned long uid)
{
dbus_bool_t allowed;
unsigned long *group_ids;
int n_group_ids;
/* On OOM or error we always reject the user */
if (!_dbus_unix_groups_from_uid (uid, &group_ids, &n_group_ids))
{
_dbus_verbose ("Did not get any groups for UID %lu\n",
uid);
return FALSE;
}
/* Default to "user owning bus" can connect */
allowed = _dbus_unix_user_is_process_owner (uid);
allowed = list_allows_user (allowed,
&policy->default_rules,
uid,
group_ids, n_group_ids);
allowed = list_allows_user (allowed,
&policy->mandatory_rules,
uid,
group_ids, n_group_ids);
dbus_free (group_ids);
_dbus_verbose ("UID %lu allowed = %d\n", uid, allowed);
return allowed;
}
/* For now this is never actually called because the default
* DBusConnection behavior of 'same user that owns the bus can
* connect' is all it would do. Set the windows user function in
* connection.c if the config file ever supports doing something
* interesting here.
*/
dbus_bool_t
bus_policy_allow_windows_user (BusPolicy *policy,
const char *windows_sid)
{
/* Windows has no policies here since only the session bus
* is really used for now, so just checking that the
* connecting person is the same as the bus owner is fine.
*/
return _dbus_windows_user_is_process_owner (windows_sid);
}
dbus_bool_t
bus_policy_append_default_rule (BusPolicy *policy,
BusPolicyRule *rule)
{
if (!_dbus_list_append (&policy->default_rules, rule))
return FALSE;
bus_policy_rule_ref (rule);
return TRUE;
}
dbus_bool_t
bus_policy_append_mandatory_rule (BusPolicy *policy,
BusPolicyRule *rule)
{
if (!_dbus_list_append (&policy->mandatory_rules, rule))
return FALSE;
bus_policy_rule_ref (rule);
return TRUE;
}
static DBusList**
get_list (DBusHashTable *hash,
unsigned long key)
{
DBusList **list;
list = _dbus_hash_table_lookup_uintptr (hash, key);
if (list == NULL)
{
list = dbus_new0 (DBusList*, 1);
if (list == NULL)
return NULL;
if (!_dbus_hash_table_insert_uintptr (hash, key, list))
{
dbus_free (list);
return NULL;
}
}
return list;
}
dbus_bool_t
bus_policy_append_user_rule (BusPolicy *policy,
dbus_uid_t uid,
BusPolicyRule *rule)
{
DBusList **list;
list = get_list (policy->rules_by_uid, uid);
if (list == NULL)
return FALSE;
if (!_dbus_list_append (list, rule))
return FALSE;
bus_policy_rule_ref (rule);
return TRUE;
}
dbus_bool_t
bus_policy_append_group_rule (BusPolicy *policy,
dbus_gid_t gid,
BusPolicyRule *rule)
{
DBusList **list;
list = get_list (policy->rules_by_gid, gid);
if (list == NULL)
return FALSE;
if (!_dbus_list_append (list, rule))
return FALSE;
bus_policy_rule_ref (rule);
return TRUE;
}
#ifdef DBUS_ENABLE_SMACK
static DBusList **
get_list_string (DBusHashTable *table,
const char *key)
{
DBusList **list;
if (key == NULL)
return NULL;
list = _dbus_hash_table_lookup_string (table, key);
if (list == NULL)
{
char *new_key;
list = dbus_new0 (DBusList*, 1);
if (list == NULL)
return NULL;
new_key = _dbus_strdup (key);
if (new_key == NULL)
{
dbus_free (list);
return NULL;
}
if (!_dbus_hash_table_insert_string (table, new_key, list))
{
dbus_free (list);
dbus_free (new_key);
return NULL;
}
}
return list;
}
#endif
dbus_bool_t
bus_policy_append_smack_rule (BusPolicy *policy,
const char *label,
BusPolicyRule *rule)
{
#ifdef DBUS_ENABLE_SMACK
DBusList **list;
list = get_list_string (policy->rules_by_smack_label, label);
if (list == NULL)
return FALSE;
if (!_dbus_list_append (list, rule))
return FALSE;
bus_policy_rule_ref (rule);
#endif
return TRUE;
}
dbus_bool_t
bus_policy_append_console_rule (BusPolicy *policy,
dbus_bool_t at_console,
BusPolicyRule *rule)
{
if (at_console)
{
if (!_dbus_list_append (&policy->at_console_true_rules, rule))
return FALSE;
}
else
{
if (!_dbus_list_append (&policy->at_console_false_rules, rule))
return FALSE;
}
bus_policy_rule_ref (rule);
return TRUE;
}
static dbus_bool_t
append_copy_of_policy_list (DBusList **list,
DBusList **to_append)
{
DBusList *link;
DBusList *tmp_list;
tmp_list = NULL;
/* Preallocate all our links */
link = _dbus_list_get_first_link (to_append);
while (link != NULL)
{
if (!_dbus_list_append (&tmp_list, link->data))
{
_dbus_list_clear (&tmp_list);
return FALSE;
}
link = _dbus_list_get_next_link (to_append, link);
}
/* Now append them */
while ((link = _dbus_list_pop_first_link (&tmp_list)))
{
bus_policy_rule_ref (link->data);
_dbus_list_append_link (list, link);
}
return TRUE;
}
static dbus_bool_t
merge_id_hash (DBusHashTable *dest,
DBusHashTable *to_absorb)
{
DBusHashIter iter;
_dbus_hash_iter_init (to_absorb, &iter);
while (_dbus_hash_iter_next (&iter))
{
unsigned long id = _dbus_hash_iter_get_uintptr_key (&iter);
DBusList **list = _dbus_hash_iter_get_value (&iter);
DBusList **target = get_list (dest, id);
if (target == NULL)
return FALSE;
if (!append_copy_of_policy_list (target, list))
return FALSE;
}
return TRUE;
}
#ifdef DBUS_ENABLE_SMACK
static dbus_bool_t
merge_string_hash (DBusHashTable *dest,
DBusHashTable *to_absorb)
{
DBusHashIter iter;
_dbus_hash_iter_init (to_absorb, &iter);
while (_dbus_hash_iter_next (&iter))
{
const char *absorb_label = _dbus_hash_iter_get_string_key(&iter);
DBusList **list = _dbus_hash_iter_get_value (&iter);
DBusList **target = get_list_string (dest, absorb_label);
if (target == NULL)
return FALSE;
if (!append_copy_of_policy_list (target, list))
return FALSE;
}
return TRUE;
}
#endif
dbus_bool_t
bus_policy_merge (BusPolicy *policy,
BusPolicy *to_absorb)
{
/* FIXME Not properly atomic, but as used for configuration files we
* don't rely on it quite so much.
*/
if (!append_copy_of_policy_list (&policy->default_rules,
&to_absorb->default_rules))
return FALSE;
if (!append_copy_of_policy_list (&policy->mandatory_rules,
&to_absorb->mandatory_rules))
return FALSE;
if (!append_copy_of_policy_list (&policy->at_console_true_rules,
&to_absorb->at_console_true_rules))
return FALSE;
if (!append_copy_of_policy_list (&policy->at_console_false_rules,
&to_absorb->at_console_false_rules))
return FALSE;
if (!merge_id_hash (policy->rules_by_uid,
to_absorb->rules_by_uid))
return FALSE;
if (!merge_id_hash (policy->rules_by_gid,
to_absorb->rules_by_gid))
return FALSE;
#ifdef DBUS_ENABLE_SMACK
if (!merge_string_hash (policy->rules_by_smack_label,
to_absorb->rules_by_smack_label))
return FALSE;
#endif
return TRUE;
}
struct BusClientPolicy
{
int refcount;
DBusList *rules;
};
BusClientPolicy*
bus_client_policy_new (void)
{
BusClientPolicy *policy;
policy = dbus_new0 (BusClientPolicy, 1);
if (policy == NULL)
return NULL;
policy->refcount = 1;
return policy;
}
BusClientPolicy *
bus_client_policy_ref (BusClientPolicy *policy)
{
_dbus_assert (policy->refcount > 0);
policy->refcount += 1;
return policy;
}
static void
rule_unref_foreach (void *data,
void *user_data)
{
BusPolicyRule *rule = data;
bus_policy_rule_unref (rule);
}
void
bus_client_policy_unref (BusClientPolicy *policy)
{
_dbus_assert (policy->refcount > 0);
policy->refcount -= 1;
if (policy->refcount == 0)
{
_dbus_list_foreach (&policy->rules,
rule_unref_foreach,
NULL);
_dbus_list_clear (&policy->rules);
dbus_free (policy);
}
}
static void
remove_rules_by_type_up_to (BusClientPolicy *policy,
BusPolicyRuleType type,
DBusList *up_to)
{
DBusList *link;
link = _dbus_list_get_first_link (&policy->rules);
while (link != up_to)
{
BusPolicyRule *rule = link->data;
DBusList *next = _dbus_list_get_next_link (&policy->rules, link);
if (rule->type == type)
{
_dbus_list_remove_link (&policy->rules, link);
bus_policy_rule_unref (rule);
}
link = next;
}
}
void
bus_client_policy_optimize (BusClientPolicy *policy)
{
DBusList *link;
/* The idea here is that if we have:
*
* <allow send_interface="foo.bar"/>
* <deny send_interface="*"/>
*
* (for example) the deny will always override the allow. So we
* delete the allow. Ditto for deny followed by allow, etc. This is
* a dumb thing to put in a config file, but the <include> feature
* of files allows for an "inheritance and override" pattern where
* it could make sense. If an included file wants to "start over"
* with a blanket deny, no point keeping the rules from the parent
* file.
*/
_dbus_verbose ("Optimizing policy with %d rules\n",
_dbus_list_get_length (&policy->rules));
link = _dbus_list_get_first_link (&policy->rules);
while (link != NULL)
{
BusPolicyRule *rule;
DBusList *next;
dbus_bool_t remove_preceding;
next = _dbus_list_get_next_link (&policy->rules, link);
rule = link->data;
remove_preceding = FALSE;
_dbus_assert (rule != NULL);
switch (rule->type)
{
case BUS_POLICY_RULE_SEND:
remove_preceding =
rule->d.send.message_type == DBUS_MESSAGE_TYPE_INVALID &&
rule->d.send.path == NULL &&
rule->d.send.interface == NULL &&
rule->d.send.member == NULL &&
rule->d.send.error == NULL &&
rule->d.send.destination == NULL;
break;
case BUS_POLICY_RULE_RECEIVE:
remove_preceding =
rule->d.receive.message_type == DBUS_MESSAGE_TYPE_INVALID &&
rule->d.receive.path == NULL &&
rule->d.receive.interface == NULL &&
rule->d.receive.member == NULL &&
rule->d.receive.error == NULL &&
rule->d.receive.origin == NULL;
break;
case BUS_POLICY_RULE_OWN:
remove_preceding =
rule->d.own.service_name == NULL;
break;
case BUS_POLICY_RULE_USER:
case BUS_POLICY_RULE_GROUP:
_dbus_assert_not_reached ("invalid rule");
break;
}
if (remove_preceding)
remove_rules_by_type_up_to (policy, rule->type,
link);
link = next;
}
_dbus_verbose ("After optimization, policy has %d rules\n",
_dbus_list_get_length (&policy->rules));
}
dbus_bool_t
bus_client_policy_append_rule (BusClientPolicy *policy,
BusPolicyRule *rule)
{
_dbus_verbose ("Appending rule %p with type %d to policy %p\n",
rule, rule->type, policy);
if (!_dbus_list_append (&policy->rules, rule))
return FALSE;
bus_policy_rule_ref (rule);
return TRUE;
}
BusResult
bus_client_policy_check_can_send (DBusConnection *sender,
BusClientPolicy *policy,
BusRegistry *registry,
dbus_bool_t requested_reply,
DBusConnection *addressed_recipient,
DBusConnection *receiver,
DBusMessage *message,
dbus_int32_t *toggles,
dbus_bool_t *log,
const char **privilege_param,
BusDeferredMessage **deferred_message)
{
DBusList *link;
BusResult result;
const char *privilege;
/* policy->rules is in the order the rules appeared
* in the config file, i.e. last rule that applies wins
*/
_dbus_verbose (" (policy) checking send rules\n");
*toggles = 0;
result = BUS_RESULT_FALSE;
link = _dbus_list_get_first_link (&policy->rules);
while (link != NULL)
{
BusPolicyRule *rule = link->data;
link = _dbus_list_get_next_link (&policy->rules, link);
/* Rule is skipped if it specifies a different
* message name from the message, or a different
* destination from the message
*/
if (rule->type != BUS_POLICY_RULE_SEND)
{
_dbus_verbose (" (policy) skipping non-send rule\n");
continue;
}
if (rule->d.send.message_type != DBUS_MESSAGE_TYPE_INVALID)
{
if (dbus_message_get_type (message) != rule->d.send.message_type)
{
_dbus_verbose (" (policy) skipping rule for different message type\n");
continue;
}
}
/* If it's a reply, the requested_reply flag kicks in */
if (dbus_message_get_reply_serial (message) != 0)
{
/* for allow or check requested_reply=true means the rule applies
* only when reply was requested. requested_reply=false means the
* rule always applies
*/
if (!requested_reply && rule->access != BUS_POLICY_RULE_ACCESS_DENY && rule->d.send.requested_reply && !rule->d.send.eavesdrop)
{
_dbus_verbose (" (policy) skipping %s rule since it only applies to requested replies and does not allow eavesdropping\n",
rule->access == BUS_POLICY_RULE_ACCESS_ALLOW ? "allow" : "check");
continue;
}
/* for deny, requested_reply=false means the rule applies only
* when the reply was not requested. requested_reply=true means the
* rule always applies.
*/
if (requested_reply && rule->access == BUS_POLICY_RULE_ACCESS_DENY && !rule->d.send.requested_reply)
{
_dbus_verbose (" (policy) skipping deny rule since it only applies to unrequested replies\n");
continue;
}
}
if (rule->d.send.path != NULL)
{
if (dbus_message_get_path (message) != NULL &&
strcmp (dbus_message_get_path (message),
rule->d.send.path) != 0)
{
_dbus_verbose (" (policy) skipping rule for different path\n");
continue;
}
}
if (rule->d.send.interface != NULL)
{
/* The interface is optional in messages. For allow rules, if the message
* has no interface we want to skip the rule (and thus not allow);
* for deny rules, if the message has no interface we want to use the
* rule (and thus deny). Check rules are meant to be used like allow
* rules (they can grant access, but not remove it), so we treat it like
* allow here.
*/
dbus_bool_t no_interface;
no_interface = dbus_message_get_interface (message) == NULL;
if ((no_interface && rule->access != BUS_POLICY_RULE_ACCESS_DENY) ||
(!no_interface &&
strcmp (dbus_message_get_interface (message),
rule->d.send.interface) != 0))
{
_dbus_verbose (" (policy) skipping rule for different interface\n");
continue;
}
}
if (rule->d.send.member != NULL)
{
if (dbus_message_get_member (message) != NULL &&
strcmp (dbus_message_get_member (message),
rule->d.send.member) != 0)
{
_dbus_verbose (" (policy) skipping rule for different member\n");
continue;
}
}
if (rule->d.send.error != NULL)
{
if (dbus_message_get_error_name (message) != NULL &&
strcmp (dbus_message_get_error_name (message),
rule->d.send.error) != 0)
{
_dbus_verbose (" (policy) skipping rule for different error name\n");
continue;
}
}
if (rule->d.send.destination != NULL)
{
/* receiver can be NULL for messages that are sent to the
* message bus itself, we check the strings in that case as
* built-in services don't have a DBusConnection but messages
* to them have a destination service name.
*/
if (receiver == NULL)
{
if (!dbus_message_has_destination (message,
rule->d.send.destination))
{
_dbus_verbose (" (policy) skipping rule because message dest is not %s\n",
rule->d.send.destination);
continue;
}
}
else
{
DBusString str;
BusService *service;
_dbus_string_init_const (&str, rule->d.send.destination);
service = bus_registry_lookup (registry, &str);
if (service == NULL)
{
_dbus_verbose (" (policy) skipping rule because dest %s doesn't exist\n",
rule->d.send.destination);
continue;
}
if (!bus_service_has_owner (service, receiver))
{
_dbus_verbose (" (policy) skipping rule because dest %s isn't owned by receiver\n",
rule->d.send.destination);
continue;
}
}
}
/* Use this rule */
switch (rule->access)
{
case BUS_POLICY_RULE_ACCESS_ALLOW:
result = BUS_RESULT_TRUE;
break;
case BUS_POLICY_RULE_ACCESS_DENY:
result = BUS_RESULT_FALSE;
break;
case BUS_POLICY_RULE_ACCESS_CHECK:
result = BUS_RESULT_LATER;
privilege = rule->privilege;
break;
}
*log = rule->d.send.log;
(*toggles)++;
_dbus_verbose (" (policy) used rule, result now = %d\n",
result);
}
if (result == BUS_RESULT_LATER)
{
BusContext *context = bus_connection_get_context(sender);
BusCheck *check = bus_context_get_check(context);
result = bus_check_privilege(check, message, sender, addressed_recipient, receiver,
privilege, BUS_DEFERRED_MESSAGE_CHECK_SEND, deferred_message);
if (result == BUS_RESULT_LATER && deferred_message != NULL)
bus_deferred_message_set_policy_check_info(*deferred_message, requested_reply,
*toggles, privilege);
}
else
privilege = NULL;
if (privilege_param != NULL)
*privilege_param = privilege;
return result;
}
/* See docs on what the args mean on bus_context_check_security_policy()
* comment
*/
BusResult
bus_client_policy_check_can_receive (BusClientPolicy *policy,
BusRegistry *registry,
dbus_bool_t requested_reply,
DBusConnection *sender,
DBusConnection *addressed_recipient,
DBusConnection *proposed_recipient,
DBusMessage *message,
dbus_int32_t *toggles,
const char **privilege_param,
BusDeferredMessage **deferred_message)
{
DBusList *link;
dbus_bool_t eavesdropping;
BusResult result;
const char *privilege;
eavesdropping =
addressed_recipient != proposed_recipient &&
dbus_message_get_destination (message) != NULL;
/* policy->rules is in the order the rules appeared
* in the config file, i.e. last rule that applies wins
*/
_dbus_verbose (" (policy) checking receive rules, eavesdropping = %d\n", eavesdropping);
*toggles = 0;
result = BUS_RESULT_FALSE;
link = _dbus_list_get_first_link (&policy->rules);
while (link != NULL)
{
BusPolicyRule *rule = link->data;
link = _dbus_list_get_next_link (&policy->rules, link);
if (rule->type != BUS_POLICY_RULE_RECEIVE)
{
_dbus_verbose (" (policy) skipping non-receive rule\n");
continue;
}
if (rule->d.receive.message_type != DBUS_MESSAGE_TYPE_INVALID)
{
if (dbus_message_get_type (message) != rule->d.receive.message_type)
{
_dbus_verbose (" (policy) skipping rule for different message type\n");
continue;
}
}
/* for allow or check, eavesdrop=false means the rule doesn't apply when
* eavesdropping. eavesdrop=true means the rule always applies
*/
if (eavesdropping && rule->access != BUS_POLICY_RULE_ACCESS_DENY && !rule->d.receive.eavesdrop)
{
_dbus_verbose (" (policy) skipping %s rule since it doesn't apply to eavesdropping\n",
rule->access == BUS_POLICY_RULE_ACCESS_ALLOW ? "allow" : "check");
continue;
}
/* for deny, eavesdrop=true means the rule applies only when
* eavesdropping; eavesdrop=false means always deny.
*/
if (!eavesdropping && rule->access == BUS_POLICY_RULE_ACCESS_DENY && rule->d.receive.eavesdrop)
{
_dbus_verbose (" (policy) skipping deny rule since it only applies to eavesdropping\n");
continue;
}
/* If it's a reply, the requested_reply flag kicks in */
if (dbus_message_get_reply_serial (message) != 0)
{
/* for allow or check requested_reply=true means the rule applies
* only when reply was requested. requested_reply=false means the
* rule always applies
*/
if (!requested_reply && rule->access != BUS_POLICY_RULE_ACCESS_DENY && rule->d.send.requested_reply && !rule->d.send.eavesdrop)
{
_dbus_verbose (" (policy) skipping %s rule since it only applies to requested replies and does not allow eavesdropping\n",
rule->access == BUS_POLICY_RULE_ACCESS_DENY ? "allow" : "deny");
continue;
}
/* for deny, requested_reply=false means the rule applies only
* when the reply was not requested. requested_reply=true means the
* rule always applies.
*/
if (requested_reply && rule->access == BUS_POLICY_RULE_ACCESS_DENY && !rule->d.receive.requested_reply)
{
_dbus_verbose (" (policy) skipping deny rule since it only applies to unrequested replies\n");
continue;
}
}
if (rule->d.receive.path != NULL)
{
if (dbus_message_get_path (message) != NULL &&
strcmp (dbus_message_get_path (message),
rule->d.receive.path) != 0)
{
_dbus_verbose (" (policy) skipping rule for different path\n");
continue;
}
}
if (rule->d.receive.interface != NULL)
{
/* The interface is optional in messages. For allow rules, if the message
* has no interface we want to skip the rule (and thus not allow);
* for deny rules, if the message has no interface we want to use the
* rule (and thus deny). Check rules are treated like allow rules.
*/
dbus_bool_t no_interface;
no_interface = dbus_message_get_interface (message) == NULL;
if ((no_interface && rule->access != BUS_POLICY_RULE_ACCESS_DENY) ||
(!no_interface &&
strcmp (dbus_message_get_interface (message),
rule->d.receive.interface) != 0))
{
_dbus_verbose (" (policy) skipping rule for different interface\n");
continue;
}
}
if (rule->d.receive.member != NULL)
{
if (dbus_message_get_member (message) != NULL &&
strcmp (dbus_message_get_member (message),
rule->d.receive.member) != 0)
{
_dbus_verbose (" (policy) skipping rule for different member\n");
continue;
}
}
if (rule->d.receive.error != NULL)
{
if (dbus_message_get_error_name (message) != NULL &&
strcmp (dbus_message_get_error_name (message),
rule->d.receive.error) != 0)
{
_dbus_verbose (" (policy) skipping rule for different error name\n");
continue;
}
}
if (rule->d.receive.origin != NULL)
{
/* sender can be NULL for messages that originate from the
* message bus itself, we check the strings in that case as
* built-in services don't have a DBusConnection but will
* still set the sender on their messages.
*/
if (sender == NULL)
{
if (!dbus_message_has_sender (message,
rule->d.receive.origin))
{
_dbus_verbose (" (policy) skipping rule because message sender is not %s\n",
rule->d.receive.origin);
continue;
}
}
else
{
BusService *service;
DBusString str;
_dbus_string_init_const (&str, rule->d.receive.origin);
service = bus_registry_lookup (registry, &str);
if (service == NULL)
{
_dbus_verbose (" (policy) skipping rule because origin %s doesn't exist\n",
rule->d.receive.origin);
continue;
}
if (!bus_service_has_owner (service, sender))
{
_dbus_verbose (" (policy) skipping rule because origin %s isn't owned by sender\n",
rule->d.receive.origin);
continue;
}
}
}
/* Use this rule */
switch (rule->access)
{
case BUS_POLICY_RULE_ACCESS_ALLOW:
result = BUS_RESULT_TRUE;
break;
case BUS_POLICY_RULE_ACCESS_DENY:
result = BUS_RESULT_FALSE;
break;
case BUS_POLICY_RULE_ACCESS_CHECK:
result = BUS_RESULT_LATER;
privilege = rule->privilege;
break;
}
(*toggles)++;
_dbus_verbose (" (policy) used rule, result now = %d\n",
result);
}
if (result == BUS_RESULT_LATER)
{
BusContext *context = bus_connection_get_context(proposed_recipient);
BusCheck *check = bus_context_get_check(context);
result = bus_check_privilege(check, message, sender, addressed_recipient, proposed_recipient,
privilege, BUS_DEFERRED_MESSAGE_CHECK_RECEIVE, deferred_message);
if (result == BUS_RESULT_LATER && deferred_message != NULL)
bus_deferred_message_set_policy_check_info(*deferred_message, requested_reply,
*toggles, privilege);
}
else
privilege = NULL;
if (privilege_param != NULL)
*privilege_param = privilege;
return result;
}
static BusResult
bus_rules_check_can_own (DBusList *rules,
const DBusString *service_name,
DBusConnection *connection,
DBusMessage *message)
{
DBusList *link;
BusResult result;
const char *privilege;
/* rules is in the order the rules appeared
* in the config file, i.e. last rule that applies wins
*/
result = BUS_RESULT_FALSE;
link = _dbus_list_get_first_link (&rules);
while (link != NULL)
{
BusPolicyRule *rule = link->data;
link = _dbus_list_get_next_link (&rules, link);
/* Rule is skipped if it specifies a different service name from
* the desired one.
*/
if (rule->type != BUS_POLICY_RULE_OWN)
continue;
if (!rule->d.own.prefix && rule->d.own.service_name != NULL)
{
if (!_dbus_string_equal_c_str (service_name,
rule->d.own.service_name))
continue;
}
else if (rule->d.own.prefix)
{
const char *data;
char next_char;
if (!_dbus_string_starts_with_c_str (service_name,
rule->d.own.service_name))
continue;
data = _dbus_string_get_const_data (service_name);
next_char = data[strlen (rule->d.own.service_name)];
if (next_char != '\0' && next_char != '.')
continue;
}
/* Use this rule */
switch (rule->access)
{
case BUS_POLICY_RULE_ACCESS_ALLOW:
result = BUS_RESULT_TRUE;
break;
case BUS_POLICY_RULE_ACCESS_DENY:
result = BUS_RESULT_FALSE;
break;
case BUS_POLICY_RULE_ACCESS_CHECK:
result = BUS_RESULT_LATER;
privilege = rule->privilege;
break;
}
}
if (result == BUS_RESULT_LATER)
{
BusContext *context = bus_connection_get_context(connection);
BusCheck *check = bus_context_get_check(context);
BusDeferredMessage *deferred_message;
result = bus_check_privilege(check, message, connection, NULL, NULL,
privilege, BUS_DEFERRED_MESSAGE_CHECK_OWN, &deferred_message);
if (result == BUS_RESULT_LATER)
{
bus_deferred_message_disable_sender(deferred_message);
}
}
return result;
}
BusResult
bus_client_policy_check_can_own (BusClientPolicy *policy,
const DBusString *service_name,
DBusConnection *connection,
DBusMessage *message)
{
return bus_rules_check_can_own (policy->rules, service_name, connection, message);
}
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
dbus_bool_t
bus_policy_check_can_own (BusPolicy *policy,
const DBusString *service_name)
{
return bus_rules_check_can_own (policy->default_rules, service_name, NULL, NULL);
}
#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
|