-
Notifications
You must be signed in to change notification settings - Fork 62
/
connection.c
2215 lines (1890 loc) · 57.2 KB
/
connection.c
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
/*
* Copyright (C) 2013-2015 Kay Sievers
* Copyright (C) 2013-2015 Greg Kroah-Hartman <[email protected]>
* Copyright (C) 2013-2015 Daniel Mack <[email protected]>
* Copyright (C) 2013-2015 David Herrmann <[email protected]>
* Copyright (C) 2013-2015 Linux Foundation
* Copyright (C) 2014-2015 Djalal Harouni <[email protected]>
*
* kdbus is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
*/
#include <linux/audit.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/fs_struct.h>
#include <linux/hashtable.h>
#include <linux/idr.h>
#include <linux/init.h>
#include <linux/math64.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/path.h>
#include <linux/poll.h>
#include <linux/sched.h>
#include <linux/shmem_fs.h>
#include <linux/sizes.h>
#include <linux/slab.h>
#include <linux/syscalls.h>
#include <linux/uio.h>
#include "bus.h"
#include "connection.h"
#include "endpoint.h"
#include "handle.h"
#include "match.h"
#include "message.h"
#include "metadata.h"
#include "names.h"
#include "domain.h"
#include "item.h"
#include "notify.h"
#include "policy.h"
#include "pool.h"
#include "reply.h"
#include "util.h"
#include "queue.h"
#define KDBUS_CONN_ACTIVE_BIAS (INT_MIN + 2)
#define KDBUS_CONN_ACTIVE_NEW (INT_MIN + 1)
static struct kdbus_conn *kdbus_conn_new(struct kdbus_ep *ep, bool privileged,
struct kdbus_cmd_hello *hello,
const char *name,
const struct kdbus_creds *creds,
const struct kdbus_pids *pids,
const char *seclabel,
const char *conn_description)
{
#ifdef CONFIG_DEBUG_LOCK_ALLOC
static struct lock_class_key __key;
#endif
struct kdbus_pool_slice *slice = NULL;
struct kdbus_bus *bus = ep->bus;
struct kdbus_conn *conn;
u64 attach_flags_send;
u64 attach_flags_recv;
u64 items_size = 0;
bool is_policy_holder;
bool is_activator;
bool is_monitor;
struct kvec kvec;
int ret;
struct {
u64 size;
u64 type;
struct kdbus_bloom_parameter bloom;
} bloom_item;
is_monitor = hello->flags & KDBUS_HELLO_MONITOR;
is_activator = hello->flags & KDBUS_HELLO_ACTIVATOR;
is_policy_holder = hello->flags & KDBUS_HELLO_POLICY_HOLDER;
if (!hello->pool_size || !IS_ALIGNED(hello->pool_size, PAGE_SIZE))
return ERR_PTR(-EINVAL);
if (is_monitor + is_activator + is_policy_holder > 1)
return ERR_PTR(-EINVAL);
if (name && !is_activator && !is_policy_holder)
return ERR_PTR(-EINVAL);
if (!name && (is_activator || is_policy_holder))
return ERR_PTR(-EINVAL);
if (name && !kdbus_name_is_valid(name, true))
return ERR_PTR(-EINVAL);
if (is_monitor && ep->user)
return ERR_PTR(-EOPNOTSUPP);
if (!privileged && (is_activator || is_policy_holder || is_monitor))
return ERR_PTR(-EPERM);
if ((creds || pids || seclabel) && !privileged)
return ERR_PTR(-EPERM);
ret = kdbus_sanitize_attach_flags(hello->attach_flags_send,
&attach_flags_send);
if (ret < 0)
return ERR_PTR(ret);
ret = kdbus_sanitize_attach_flags(hello->attach_flags_recv,
&attach_flags_recv);
if (ret < 0)
return ERR_PTR(ret);
/* The attach flags must always satisfy the bus requirements. */
if (bus->attach_flags_req & ~attach_flags_send)
return ERR_PTR(-ECONNREFUSED);
conn = kzalloc(sizeof(*conn), GFP_KERNEL);
if (!conn)
return ERR_PTR(-ENOMEM);
kref_init(&conn->kref);
atomic_set(&conn->active, KDBUS_CONN_ACTIVE_NEW);
#ifdef CONFIG_DEBUG_LOCK_ALLOC
lockdep_init_map(&conn->dep_map, "s_active", &__key, 0);
#endif
mutex_init(&conn->lock);
INIT_LIST_HEAD(&conn->names_list);
INIT_LIST_HEAD(&conn->names_queue_list);
INIT_LIST_HEAD(&conn->reply_list);
atomic_set(&conn->name_count, 0);
atomic_set(&conn->request_count, 0);
atomic_set(&conn->lost_count, 0);
INIT_DELAYED_WORK(&conn->work, kdbus_reply_list_scan_work);
conn->cred = get_current_cred();
init_waitqueue_head(&conn->wait);
kdbus_queue_init(&conn->queue);
conn->privileged = privileged;
conn->ep = kdbus_ep_ref(ep);
conn->id = atomic64_inc_return(&bus->domain->last_id);
conn->flags = hello->flags;
atomic64_set(&conn->attach_flags_send, attach_flags_send);
atomic64_set(&conn->attach_flags_recv, attach_flags_recv);
INIT_LIST_HEAD(&conn->monitor_entry);
if (conn_description) {
conn->description = kstrdup(conn_description, GFP_KERNEL);
if (!conn->description) {
ret = -ENOMEM;
goto exit_unref;
}
}
conn->pool = kdbus_pool_new(conn->description, hello->pool_size);
if (IS_ERR(conn->pool)) {
ret = PTR_ERR(conn->pool);
conn->pool = NULL;
goto exit_unref;
}
conn->match_db = kdbus_match_db_new();
if (IS_ERR(conn->match_db)) {
ret = PTR_ERR(conn->match_db);
conn->match_db = NULL;
goto exit_unref;
}
/* return properties of this connection to the caller */
hello->bus_flags = bus->bus_flags;
hello->id = conn->id;
BUILD_BUG_ON(sizeof(bus->id128) != sizeof(hello->id128));
memcpy(hello->id128, bus->id128, sizeof(hello->id128));
conn->meta = kdbus_meta_proc_new();
if (IS_ERR(conn->meta)) {
ret = PTR_ERR(conn->meta);
conn->meta = NULL;
goto exit_unref;
}
/* privileged processes can impersonate somebody else */
if (creds || pids || seclabel) {
ret = kdbus_meta_proc_fake(conn->meta, creds, pids, seclabel);
if (ret < 0)
goto exit_unref;
conn->faked_meta = true;
} else {
ret = kdbus_meta_proc_collect(conn->meta,
KDBUS_ATTACH_CREDS |
KDBUS_ATTACH_PIDS |
KDBUS_ATTACH_AUXGROUPS |
KDBUS_ATTACH_TID_COMM |
KDBUS_ATTACH_PID_COMM |
KDBUS_ATTACH_EXE |
KDBUS_ATTACH_CMDLINE |
KDBUS_ATTACH_CGROUP |
KDBUS_ATTACH_CAPS |
KDBUS_ATTACH_SECLABEL |
KDBUS_ATTACH_AUDIT);
if (ret < 0)
goto exit_unref;
}
/*
* Account the connection against the current user (UID), or for
* custom endpoints use the anonymous user assigned to the endpoint.
* Note that limits are always accounted against the real UID, not
* the effective UID (cred->user always points to the accounting of
* cred->uid, not cred->euid).
*/
if (ep->user) {
conn->user = kdbus_user_ref(ep->user);
} else {
conn->user = kdbus_user_lookup(ep->bus->domain, current_uid());
if (IS_ERR(conn->user)) {
ret = PTR_ERR(conn->user);
conn->user = NULL;
goto exit_unref;
}
}
if (atomic_inc_return(&conn->user->connections) > KDBUS_USER_MAX_CONN) {
/* decremented by destructor as conn->user is valid */
ret = -EMFILE;
goto exit_unref;
}
bloom_item.size = sizeof(bloom_item);
bloom_item.type = KDBUS_ITEM_BLOOM_PARAMETER;
bloom_item.bloom = bus->bloom;
kdbus_kvec_set(&kvec, &bloom_item, bloom_item.size, &items_size);
slice = kdbus_pool_slice_alloc(conn->pool, items_size, false);
if (IS_ERR(slice)) {
ret = PTR_ERR(slice);
slice = NULL;
goto exit_unref;
}
ret = kdbus_pool_slice_copy_kvec(slice, 0, &kvec, 1, items_size);
if (ret < 0)
goto exit_unref;
kdbus_pool_slice_publish(slice, &hello->offset, &hello->items_size);
kdbus_pool_slice_release(slice);
return conn;
exit_unref:
kdbus_pool_slice_release(slice);
kdbus_conn_unref(conn);
return ERR_PTR(ret);
}
static void __kdbus_conn_free(struct kref *kref)
{
struct kdbus_conn *conn = container_of(kref, struct kdbus_conn, kref);
WARN_ON(kdbus_conn_active(conn));
WARN_ON(delayed_work_pending(&conn->work));
WARN_ON(!list_empty(&conn->queue.msg_list));
WARN_ON(!list_empty(&conn->names_list));
WARN_ON(!list_empty(&conn->names_queue_list));
WARN_ON(!list_empty(&conn->reply_list));
if (conn->user) {
atomic_dec(&conn->user->connections);
kdbus_user_unref(conn->user);
}
kdbus_meta_proc_unref(conn->meta);
kdbus_match_db_free(conn->match_db);
kdbus_pool_free(conn->pool);
kdbus_ep_unref(conn->ep);
put_cred(conn->cred);
kfree(conn->description);
kfree(conn->quota);
kfree(conn);
}
/**
* kdbus_conn_ref() - take a connection reference
* @conn: Connection, may be %NULL
*
* Return: the connection itself
*/
struct kdbus_conn *kdbus_conn_ref(struct kdbus_conn *conn)
{
if (conn)
kref_get(&conn->kref);
return conn;
}
/**
* kdbus_conn_unref() - drop a connection reference
* @conn: Connection (may be NULL)
*
* When the last reference is dropped, the connection's internal structure
* is freed.
*
* Return: NULL
*/
struct kdbus_conn *kdbus_conn_unref(struct kdbus_conn *conn)
{
if (conn)
kref_put(&conn->kref, __kdbus_conn_free);
return NULL;
}
/**
* kdbus_conn_active() - connection is not disconnected
* @conn: Connection to check
*
* Return true if the connection was not disconnected, yet. Note that a
* connection might be disconnected asynchronously, unless you hold the
* connection lock. If that's not suitable for you, see kdbus_conn_acquire() to
* suppress connection shutdown for a short period.
*
* Return: true if the connection is still active
*/
bool kdbus_conn_active(const struct kdbus_conn *conn)
{
return atomic_read(&conn->active) >= 0;
}
/**
* kdbus_conn_acquire() - acquire an active connection reference
* @conn: Connection
*
* Users can close a connection via KDBUS_BYEBYE (or by destroying the
* endpoint/bus/...) at any time. Whenever this happens, we should deny any
* user-visible action on this connection and signal ECONNRESET instead.
* To avoid testing for connection availability everytime you take the
* connection-lock, you can acquire a connection for short periods.
*
* By calling kdbus_conn_acquire(), you gain an "active reference" to the
* connection. You must also hold a regular reference at any time! As long as
* you hold the active-ref, the connection will not be shut down. However, if
* the connection was shut down, you can never acquire an active-ref again.
*
* kdbus_conn_disconnect() disables the connection and then waits for all active
* references to be dropped. It will also wake up any pending operation.
* However, you must not sleep for an indefinite period while holding an
* active-reference. Otherwise, kdbus_conn_disconnect() might stall. If you need
* to sleep for an indefinite period, either release the reference and try to
* acquire it again after waking up, or make kdbus_conn_disconnect() wake up
* your wait-queue.
*
* Return: 0 on success, negative error code on failure.
*/
int kdbus_conn_acquire(struct kdbus_conn *conn)
{
if (!atomic_inc_unless_negative(&conn->active))
return -ECONNRESET;
#ifdef CONFIG_DEBUG_LOCK_ALLOC
rwsem_acquire_read(&conn->dep_map, 0, 1, _RET_IP_);
#endif
return 0;
}
/**
* kdbus_conn_release() - release an active connection reference
* @conn: Connection
*
* This releases an active reference that has been acquired via
* kdbus_conn_acquire(). If the connection was already disabled and this is the
* last active-ref that is dropped, the disconnect-waiter will be woken up and
* properly close the connection.
*/
void kdbus_conn_release(struct kdbus_conn *conn)
{
int v;
if (!conn)
return;
#ifdef CONFIG_DEBUG_LOCK_ALLOC
rwsem_release(&conn->dep_map, 1, _RET_IP_);
#endif
v = atomic_dec_return(&conn->active);
if (v != KDBUS_CONN_ACTIVE_BIAS)
return;
wake_up_all(&conn->wait);
}
static int kdbus_conn_connect(struct kdbus_conn *conn, const char *name)
{
struct kdbus_ep *ep = conn->ep;
struct kdbus_bus *bus = ep->bus;
int ret;
if (WARN_ON(atomic_read(&conn->active) != KDBUS_CONN_ACTIVE_NEW))
return -EALREADY;
/* make sure the ep-node is active while we add our connection */
if (!kdbus_node_acquire(&ep->node))
return -ESHUTDOWN;
/* lock order: domain -> bus -> ep -> names -> conn */
mutex_lock(&ep->lock);
down_write(&bus->conn_rwlock);
/* link into monitor list */
if (kdbus_conn_is_monitor(conn))
list_add_tail(&conn->monitor_entry, &bus->monitors_list);
/* link into bus and endpoint */
list_add_tail(&conn->ep_entry, &ep->conn_list);
hash_add(bus->conn_hash, &conn->hentry, conn->id);
/* enable lookups and acquire active ref */
atomic_set(&conn->active, 1);
#ifdef CONFIG_DEBUG_LOCK_ALLOC
rwsem_acquire_read(&conn->dep_map, 0, 1, _RET_IP_);
#endif
up_write(&bus->conn_rwlock);
mutex_unlock(&ep->lock);
kdbus_node_release(&ep->node);
/*
* Notify subscribers about the new active connection, unless it is
* a monitor. Monitors are invisible on the bus, can't be addressed
* directly, and won't cause any notifications.
*/
if (!kdbus_conn_is_monitor(conn)) {
ret = kdbus_notify_id_change(conn->ep->bus, KDBUS_ITEM_ID_ADD,
conn->id, conn->flags);
if (ret < 0)
goto exit_disconnect;
}
if (kdbus_conn_is_activator(conn)) {
u64 flags = KDBUS_NAME_ACTIVATOR;
if (WARN_ON(!name)) {
ret = -EINVAL;
goto exit_disconnect;
}
ret = kdbus_name_acquire(bus->name_registry, conn, name,
flags, NULL);
if (ret < 0)
goto exit_disconnect;
}
kdbus_conn_release(conn);
kdbus_notify_flush(bus);
return 0;
exit_disconnect:
kdbus_conn_release(conn);
kdbus_conn_disconnect(conn, false);
return ret;
}
/**
* kdbus_conn_disconnect() - disconnect a connection
* @conn: The connection to disconnect
* @ensure_queue_empty: Flag to indicate if the call should fail in
* case the connection's message list is not
* empty
*
* If @ensure_msg_list_empty is true, and the connection has pending messages,
* -EBUSY is returned.
*
* Return: 0 on success, negative errno on failure
*/
int kdbus_conn_disconnect(struct kdbus_conn *conn, bool ensure_queue_empty)
{
struct kdbus_queue_entry *entry, *tmp;
struct kdbus_bus *bus = conn->ep->bus;
struct kdbus_reply *r, *r_tmp;
struct kdbus_conn *c;
int i, v;
mutex_lock(&conn->lock);
v = atomic_read(&conn->active);
if (v == KDBUS_CONN_ACTIVE_NEW) {
/* was never connected */
mutex_unlock(&conn->lock);
return 0;
}
if (v < 0) {
/* already dead */
mutex_unlock(&conn->lock);
return -ECONNRESET;
}
if (ensure_queue_empty && !list_empty(&conn->queue.msg_list)) {
/* still busy */
mutex_unlock(&conn->lock);
return -EBUSY;
}
atomic_add(KDBUS_CONN_ACTIVE_BIAS, &conn->active);
mutex_unlock(&conn->lock);
wake_up_interruptible(&conn->wait);
#ifdef CONFIG_DEBUG_LOCK_ALLOC
rwsem_acquire(&conn->dep_map, 0, 0, _RET_IP_);
if (atomic_read(&conn->active) != KDBUS_CONN_ACTIVE_BIAS)
lock_contended(&conn->dep_map, _RET_IP_);
#endif
wait_event(conn->wait,
atomic_read(&conn->active) == KDBUS_CONN_ACTIVE_BIAS);
#ifdef CONFIG_DEBUG_LOCK_ALLOC
lock_acquired(&conn->dep_map, _RET_IP_);
rwsem_release(&conn->dep_map, 1, _RET_IP_);
#endif
cancel_delayed_work_sync(&conn->work);
kdbus_policy_remove_owner(&conn->ep->bus->policy_db, conn);
/* lock order: domain -> bus -> ep -> names -> conn */
mutex_lock(&conn->ep->lock);
down_write(&bus->conn_rwlock);
/* remove from bus and endpoint */
hash_del(&conn->hentry);
list_del(&conn->monitor_entry);
list_del(&conn->ep_entry);
up_write(&bus->conn_rwlock);
mutex_unlock(&conn->ep->lock);
/*
* Remove all names associated with this connection; this possibly
* moves queued messages back to the activator connection.
*/
kdbus_name_release_all(bus->name_registry, conn);
/* if we die while other connections wait for our reply, notify them */
mutex_lock(&conn->lock);
list_for_each_entry_safe(entry, tmp, &conn->queue.msg_list, entry) {
if (entry->reply)
kdbus_notify_reply_dead(bus,
entry->reply->reply_dst->id,
entry->reply->cookie);
kdbus_queue_entry_free(entry);
}
list_for_each_entry_safe(r, r_tmp, &conn->reply_list, entry)
kdbus_reply_unlink(r);
mutex_unlock(&conn->lock);
/* lock order: domain -> bus -> ep -> names -> conn */
down_read(&bus->conn_rwlock);
hash_for_each(bus->conn_hash, i, c, hentry) {
mutex_lock(&c->lock);
list_for_each_entry_safe(r, r_tmp, &c->reply_list, entry) {
if (r->reply_src == conn) {
if (r->sync) {
kdbus_sync_reply_wakeup(r, -EPIPE);
kdbus_reply_unlink(r);
continue;
}
/* send a 'connection dead' notification */
kdbus_notify_reply_dead(bus, c->id, r->cookie);
kdbus_reply_unlink(r);
}
}
mutex_unlock(&c->lock);
}
up_read(&bus->conn_rwlock);
if (!kdbus_conn_is_monitor(conn))
kdbus_notify_id_change(bus, KDBUS_ITEM_ID_REMOVE,
conn->id, conn->flags);
kdbus_notify_flush(bus);
return 0;
}
/**
* kdbus_conn_has_name() - check if a connection owns a name
* @conn: Connection
* @name: Well-know name to check for
*
* The caller must hold the registry lock of conn->ep->bus.
*
* Return: true if the name is currently owned by the connection
*/
bool kdbus_conn_has_name(struct kdbus_conn *conn, const char *name)
{
struct kdbus_name_entry *e;
lockdep_assert_held(&conn->ep->bus->name_registry->rwlock);
list_for_each_entry(e, &conn->names_list, conn_entry)
if (strcmp(e->name, name) == 0)
return true;
return false;
}
struct kdbus_quota {
uint32_t memory;
uint16_t msgs;
uint8_t fds;
};
/**
* kdbus_conn_quota_inc() - increase quota accounting
* @c: connection owning the quota tracking
* @u: user to account for (or NULL for kernel accounting)
* @memory: size of memory to account for
* @fds: number of FDs to account for
*
* This call manages the quotas on resource @c. That is, it's used if other
* users want to use the resources of connection @c, which so far only concerns
* the receive queue of the destination.
*
* This increases the quota-accounting for user @u by @memory bytes and @fds
* file descriptors. If the user has already reached the quota limits, this call
* will not do any accounting but return a negative error code indicating the
* failure.
*
* Return: 0 on success, negative error code on failure.
*/
int kdbus_conn_quota_inc(struct kdbus_conn *c, struct kdbus_user *u,
size_t memory, size_t fds)
{
struct kdbus_quota *quota;
size_t available, accounted;
unsigned int id;
/*
* Pool Layout:
* 50% of a pool is always owned by the connection. It is reserved for
* kernel queries, handling received messages and other tasks that are
* under control of the pool owner. The other 50% of the pool are used
* as incoming queue.
* As we optionally support user-space based policies, we need fair
* allocation schemes. Furthermore, resource utilization should be
* maximized, so only minimal resources stay reserved. However, we need
* to adapt to a dynamic number of users, as we cannot know how many
* users will talk to a connection. Therefore, the current allocations
* works like this:
* We limit the number of bytes in a destination's pool per sending
* user. The space available for a user is 33% of the unused pool space
* (whereas the space used by the user itself is also treated as
* 'unused'). This way, we favor users coming first, but keep enough
* pool space available for any following users. Given that messages are
* dequeued in FIFO order, this should balance nicely if the number of
* users grows. At the same time, this algorithm guarantees that the
* space available to a connection is reduced dynamically, the more
* concurrent users talk to a connection.
*/
/* per user-accounting is expensive, so we keep state small */
BUILD_BUG_ON(sizeof(quota->memory) != 4);
BUILD_BUG_ON(sizeof(quota->msgs) != 2);
BUILD_BUG_ON(sizeof(quota->fds) != 1);
BUILD_BUG_ON(KDBUS_CONN_MAX_MSGS > U16_MAX);
BUILD_BUG_ON(KDBUS_CONN_MAX_FDS_PER_USER > U8_MAX);
id = u ? u->id : KDBUS_USER_KERNEL_ID;
if (id >= c->n_quota) {
unsigned int users;
users = max(KDBUS_ALIGN8(id) + 8, id);
quota = krealloc(c->quota, users * sizeof(*quota),
GFP_KERNEL | __GFP_ZERO);
if (!quota)
return -ENOMEM;
c->n_quota = users;
c->quota = quota;
}
quota = &c->quota[id];
kdbus_pool_accounted(c->pool, &available, &accounted);
/* half the pool is _always_ reserved for the pool owner */
available /= 2;
/*
* Pool owner slices are un-accounted slices; they can claim more
* than 50% of the queue. However, the slice we're dealing with here
* belong to the incoming queue, hence they are 'accounted' slices
* to which the 50%-limit applies.
*/
if (available < accounted)
return -ENOBUFS;
/* 1/3 of the remaining space (including your own memory) */
available = (available - accounted + quota->memory) / 3;
if (available < quota->memory ||
available - quota->memory < memory ||
quota->memory + memory > U32_MAX)
return -ENOBUFS;
if (quota->msgs >= KDBUS_CONN_MAX_MSGS)
return -ENOBUFS;
if (quota->fds + fds < quota->fds ||
quota->fds + fds > KDBUS_CONN_MAX_FDS_PER_USER)
return -EMFILE;
quota->memory += memory;
quota->fds += fds;
++quota->msgs;
return 0;
}
/**
* kdbus_conn_quota_dec() - decrease quota accounting
* @c: connection owning the quota tracking
* @u: user which was accounted for (or NULL for kernel accounting)
* @memory: size of memory which was accounted for
* @fds: number of FDs which were accounted for
*
* This does the reverse of kdbus_conn_quota_inc(). You have to release any
* accounted resources that you called kdbus_conn_quota_inc() for. However, you
* must not call kdbus_conn_quota_dec() if the accounting failed (that is,
* kdbus_conn_quota_inc() failed).
*/
void kdbus_conn_quota_dec(struct kdbus_conn *c, struct kdbus_user *u,
size_t memory, size_t fds)
{
struct kdbus_quota *quota;
unsigned int id;
id = u ? u->id : KDBUS_USER_KERNEL_ID;
if (WARN_ON(id >= c->n_quota))
return;
quota = &c->quota[id];
if (!WARN_ON(quota->msgs == 0))
--quota->msgs;
if (!WARN_ON(quota->memory < memory))
quota->memory -= memory;
if (!WARN_ON(quota->fds < fds))
quota->fds -= fds;
}
/**
* kdbus_conn_lost_message() - handle lost messages
* @c: connection that lost a message
*
* kdbus is reliable. That means, we try hard to never lose messages. However,
* memory is limited, so we cannot rely on transmissions to never fail.
* Therefore, we use quota-limits to let callers know if there unicast message
* cannot be transmitted to a peer. This works fine for unicasts, but for
* broadcasts we cannot make the caller handle the transmission failure.
* Instead, we must let the destination know that it couldn't receive a
* broadcast.
* As this is an unlikely scenario, we keep it simple. A single lost-counter
* remembers the number of lost messages since the last call to RECV. The next
* message retrieval will notify the connection that it lost messages since the
* last message retrieval and thus should resync its state.
*/
void kdbus_conn_lost_message(struct kdbus_conn *c)
{
if (atomic_inc_return(&c->lost_count) == 1)
wake_up_interruptible(&c->wait);
}
/* Callers should take the conn_dst lock */
static struct kdbus_queue_entry *
kdbus_conn_entry_make(struct kdbus_conn *conn_dst,
const struct kdbus_kmsg *kmsg,
struct kdbus_user *user)
{
struct kdbus_queue_entry *entry;
/* The remote connection was disconnected */
if (!kdbus_conn_active(conn_dst))
return ERR_PTR(-ECONNRESET);
/*
* If the connection does not accept file descriptors but the message
* has some attached, refuse it.
*
* If this is a monitor connection, accept the message. In that
* case, all file descriptors will be set to -1 at receive time.
*/
if (!kdbus_conn_is_monitor(conn_dst) &&
!(conn_dst->flags & KDBUS_HELLO_ACCEPT_FD) &&
kmsg->res && kmsg->res->fds_count > 0)
return ERR_PTR(-ECOMM);
entry = kdbus_queue_entry_new(conn_dst, kmsg, user);
if (IS_ERR(entry))
return entry;
return entry;
}
/*
* Synchronously responding to a message, allocate a queue entry
* and attach it to the reply tracking object.
* The connection's queue will never get to see it.
*/
static int kdbus_conn_entry_sync_attach(struct kdbus_conn *conn_dst,
const struct kdbus_kmsg *kmsg,
struct kdbus_reply *reply_wake)
{
struct kdbus_queue_entry *entry;
int remote_ret;
int ret = 0;
mutex_lock(&reply_wake->reply_dst->lock);
/*
* If we are still waiting then proceed, allocate a queue
* entry and attach it to the reply object
*/
if (reply_wake->waiting) {
entry = kdbus_conn_entry_make(conn_dst, kmsg,
reply_wake->reply_src->user);
if (IS_ERR(entry))
ret = PTR_ERR(entry);
else
/* Attach the entry to the reply object */
reply_wake->queue_entry = entry;
} else {
ret = -ECONNRESET;
}
/*
* Update the reply object and wake up remote peer only
* on appropriate return codes
*
* * -ECOMM: if the replying connection failed with -ECOMM
* then wakeup remote peer with -EREMOTEIO
*
* We do this to differenciate between -ECOMM errors
* from the original sender perspective:
* -ECOMM error during the sync send and
* -ECOMM error during the sync reply, this last
* one is rewritten to -EREMOTEIO
*
* * Wake up on all other return codes.
*/
remote_ret = ret;
if (ret == -ECOMM)
remote_ret = -EREMOTEIO;
kdbus_sync_reply_wakeup(reply_wake, remote_ret);
kdbus_reply_unlink(reply_wake);
mutex_unlock(&reply_wake->reply_dst->lock);
return ret;
}
/**
* kdbus_conn_entry_insert() - enqueue a message into the receiver's pool
* @conn_src: The sending connection
* @conn_dst: The connection to queue into
* @kmsg: The kmsg to queue
* @reply: The reply tracker to attach to the queue entry
*
* Return: 0 on success. negative error otherwise.
*/
int kdbus_conn_entry_insert(struct kdbus_conn *conn_src,
struct kdbus_conn *conn_dst,
const struct kdbus_kmsg *kmsg,
struct kdbus_reply *reply)
{
struct kdbus_queue_entry *entry;
int ret;
kdbus_conn_lock2(conn_src, conn_dst);
entry = kdbus_conn_entry_make(conn_dst, kmsg,
conn_src ? conn_src->user : NULL);
if (IS_ERR(entry)) {
ret = PTR_ERR(entry);
goto exit_unlock;
}
if (reply) {
kdbus_reply_link(reply);
if (!reply->sync)
schedule_delayed_work(&conn_src->work, 0);
}
kdbus_queue_entry_enqueue(entry, reply);
wake_up_interruptible(&conn_dst->wait);
ret = 0;
exit_unlock:
kdbus_conn_unlock2(conn_src, conn_dst);
return ret;
}
static int kdbus_conn_wait_reply(struct kdbus_conn *conn_src,
struct kdbus_cmd_send *cmd_send,
struct file *ioctl_file,
struct file *cancel_fd,
struct kdbus_reply *reply_wait,
ktime_t expire)
{
struct kdbus_queue_entry *entry;
struct poll_wqueues pwq = {};
int ret;
if (WARN_ON(!reply_wait))
return -EIO;
/*
* Block until the reply arrives. reply_wait is left untouched
* by the timeout scans that might be conducted for other,
* asynchronous replies of conn_src.
*/
poll_initwait(&pwq);
poll_wait(ioctl_file, &conn_src->wait, &pwq.pt);
for (;;) {
/*
* Any of the following conditions will stop our synchronously
* blocking SEND command:
*
* a) The origin sender closed its connection
* b) The remote peer answered, setting reply_wait->waiting = 0
* c) The cancel FD was written to
* d) A signal was received
* e) The specified timeout was reached, and none of the above
* conditions kicked in.
*/
/*
* We have already acquired an active reference when
* entering here, but another thread may call
* KDBUS_CMD_BYEBYE which does not acquire an active
* reference, therefore kdbus_conn_disconnect() will
* not wait for us.
*/
if (!kdbus_conn_active(conn_src)) {
ret = -ECONNRESET;
break;
}
/*
* After the replying peer unset the waiting variable
* it will wake up us.
*/
if (!reply_wait->waiting) {
ret = reply_wait->err;
break;
}
if (cancel_fd) {
unsigned int r;
r = cancel_fd->f_op->poll(cancel_fd, &pwq.pt);
if (r & POLLIN) {
ret = -ECANCELED;
break;
}
}
if (signal_pending(current)) {
ret = -EINTR;
break;
}
if (!poll_schedule_timeout(&pwq, TASK_INTERRUPTIBLE,
&expire, 0)) {
ret = -ETIMEDOUT;
break;
}
/*
* Reset the poll worker func, so the waitqueues are not
* added to the poll table again. We just reuse what we've
* collected earlier for further iterations.
*/
init_poll_funcptr(&pwq.pt, NULL);
}
poll_freewait(&pwq);
if (ret == -EINTR) {
/*
* Interrupted system call. Unref the reply object, and pass
* the return value down the chain. Mark the reply as
* interrupted, so the cleanup work can remove it, but do not
* unlink it from the list. Once the syscall restarts, we'll
* pick it up and wait on it again.
*/
mutex_lock(&conn_src->lock);
reply_wait->interrupted = true;