-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathobjects.c
More file actions
1727 lines (1557 loc) · 59.1 KB
/
objects.c
File metadata and controls
1727 lines (1557 loc) · 59.1 KB
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
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "child.h"
#include "debug.h"
#include "deferred-free.h"
#include "fd.h"
#include "list.h"
#include "locks.h"
#include "objects.h"
#include "params.h"
#include "pc_format.h"
#include "pids.h"
#include "random.h"
#include "shm.h"
#include "trinity.h"
#include "utils.h"
static struct list_head global_obj_list = { &global_obj_list, &global_obj_list };
void register_global_obj_init(struct global_obj_entry *entry)
{
list_add_tail((struct list_head *) &entry->list, &global_obj_list);
}
void init_global_objects(void)
{
struct list_head *pos;
list_for_each(pos, &global_obj_list) {
struct global_obj_entry *entry = (struct global_obj_entry *) pos;
output(1, "Initializing %s objects.\n", entry->name);
entry->init();
}
}
/*
* Hash table mapping fd → (object, type) for O(1) lookup in
* remove_object_by_fd(). Open-addressing with linear probing.
*
* The table itself lives in shm (shm->fd_hash) so children can read
* the per-slot generation counter the parent updates on every fd-table
* mutation. Mutations happen under shm->objlock; child reads of the
* gen field are unlocked and use ACQUIRE semantics.
*/
void fd_hash_init(void)
{
unsigned int i;
for (i = 0; i < FD_HASH_SIZE; i++) {
shm->fd_hash[i].fd = -1;
shm->fd_hash[i].gen = 0;
}
shm->fd_hash_count = 0;
/*
* fd_live[] entries are gated by fd_live_count, so initialising
* just the count is sufficient; stale slot contents past the
* count are never read.
*/
shm->fd_live_count = 0;
}
/*
* Append fd to the parallel live-fd list. Caller must hold shm->objlock
* and have just transitioned an fd_hash[] slot from empty to occupied.
* Publishes the new entry first, then bumps fd_live_count with RELEASE
* so a lockless reader that ACQUIREs the count is guaranteed to see the
* entry. Silently drops the entry if the cap is hit; the only consumer
* (refcount-auditor) is a sampling auditor and tolerates a missed fd.
*/
static void fd_live_append(int fd)
{
unsigned int idx = shm->fd_live_count;
if (idx >= FD_LIVE_MAX)
return;
__atomic_store_n(&shm->fd_live[idx], fd, __ATOMIC_RELEASE);
__atomic_store_n(&shm->fd_live_count, idx + 1, __ATOMIC_RELEASE);
}
/*
* Swap-remove fd from the parallel live-fd list. Caller must hold
* shm->objlock and have just transitioned an fd_hash[] slot from
* occupied to empty. Linear scan over fd_live[0..count) is cheap —
* the list is bounded by FD_HASH_SIZE in the worst case but typically
* holds a few hundred entries. The replacement-then-decrement order
* keeps the visible window of fd_live[] entries valid: a concurrent
* lockless reader that loads count after the decrement sees a list
* whose every slot is a real live fd; one that loads count before the
* decrement may re-read the just-removed fd, which the auditor's
* dup() check naturally tolerates.
*/
static void fd_live_remove(int fd)
{
unsigned int count = shm->fd_live_count;
unsigned int i;
for (i = 0; i < count; i++) {
if (shm->fd_live[i] != fd)
continue;
if (i != count - 1) {
int last = shm->fd_live[count - 1];
__atomic_store_n(&shm->fd_live[i], last,
__ATOMIC_RELEASE);
}
__atomic_store_n(&shm->fd_live_count, count - 1,
__ATOMIC_RELEASE);
return;
}
}
static unsigned int fd_hash_slot(int fd)
{
return (unsigned int) fd & (FD_HASH_SIZE - 1);
}
/*
* Internal insert that preserves the entry's existing generation and
* doesn't update fd_hash_count. Used by fd_hash_remove to re-hash
* displaced entries: the entry's identity is unchanged, only its slot,
* so any cached gen on a child must continue to match.
*/
static void fd_hash_reinsert(int fd, struct object *obj, enum objecttype type,
uint32_t gen)
{
unsigned int slot;
unsigned int probe;
slot = fd_hash_slot(fd);
for (probe = 0; probe < FD_HASH_SIZE; probe++) {
if (shm->fd_hash[slot].fd == -1)
break;
slot = (slot + 1) & (FD_HASH_SIZE - 1);
}
if (probe == FD_HASH_SIZE)
return;
shm->fd_hash[slot].obj = obj;
shm->fd_hash[slot].type = type;
__atomic_store_n(&shm->fd_hash[slot].gen, gen, __ATOMIC_RELEASE);
__atomic_store_n(&shm->fd_hash[slot].fd, fd, __ATOMIC_RELEASE);
}
bool fd_hash_insert(int fd, struct object *obj, enum objecttype type)
{
unsigned int slot;
uint32_t gen;
if (fd < 0)
return true;
if (shm->fd_hash_count >= FD_HASH_SIZE)
return false;
slot = fd_hash_slot(fd);
while (shm->fd_hash[slot].fd != -1 && shm->fd_hash[slot].fd != fd)
slot = (slot + 1) & (FD_HASH_SIZE - 1);
if (shm->fd_hash[slot].fd == -1) {
shm->fd_hash_count++;
fd_live_append(fd);
}
shm->fd_hash[slot].obj = obj;
shm->fd_hash[slot].type = type;
/*
* Bump the slot's generation so any child that cached the
* previous occupant's (or absence) gen sees a mismatch. The
* RELEASE-store on fd publishes the entry — children using
* ACQUIRE-load on fd see the updated gen too.
*/
gen = shm->fd_hash[slot].gen + 1;
__atomic_store_n(&shm->fd_hash[slot].gen, gen, __ATOMIC_RELEASE);
__atomic_store_n(&shm->fd_hash[slot].fd, fd, __ATOMIC_RELEASE);
return true;
}
void fd_hash_remove(int fd)
{
unsigned int slot, next, i;
if (fd < 0)
return;
slot = fd_hash_slot(fd);
for (i = 0; i < FD_HASH_SIZE; i++) {
if (shm->fd_hash[slot].fd == -1)
return;
if (shm->fd_hash[slot].fd == fd) {
uint32_t gen;
/*
* Mark the slot empty and bump its generation so a
* child that cached this fd's gen sees a mismatch
* even before any replacement is inserted here.
*/
gen = shm->fd_hash[slot].gen + 1;
__atomic_store_n(&shm->fd_hash[slot].gen, gen,
__ATOMIC_RELEASE);
__atomic_store_n(&shm->fd_hash[slot].fd, -1,
__ATOMIC_RELEASE);
shm->fd_hash_count--;
fd_live_remove(fd);
next = (slot + 1) & (FD_HASH_SIZE - 1);
while (shm->fd_hash[next].fd != -1) {
struct fd_hash_entry displaced = shm->fd_hash[next];
__atomic_store_n(&shm->fd_hash[next].fd, -1,
__ATOMIC_RELEASE);
fd_hash_reinsert(displaced.fd, displaced.obj,
displaced.type, displaced.gen);
next = (next + 1) & (FD_HASH_SIZE - 1);
}
return;
}
slot = (slot + 1) & (FD_HASH_SIZE - 1);
}
}
struct fd_hash_entry *fd_hash_lookup(int fd)
{
unsigned int slot, i;
if (fd < 0)
return NULL;
slot = fd_hash_slot(fd);
for (i = 0; i < FD_HASH_SIZE; i++) {
int slot_fd = __atomic_load_n(&shm->fd_hash[slot].fd, __ATOMIC_ACQUIRE);
if (slot_fd == -1)
return NULL;
if (slot_fd == fd)
return &shm->fd_hash[slot];
slot = (slot + 1) & (FD_HASH_SIZE - 1);
}
return NULL;
}
static bool is_fd_type(enum objecttype type)
{
return type >= OBJ_FD_PIPE && type <= OBJ_FD_KVM_VCPU;
}
/*
* Per-objhead fd→object hash for OBJ_LOCAL fd-typed pools.
*
* Open-addressing with linear probing into a fixed power-of-two slot array
* (LOCAL_FD_HASH_SIZE). fd == -1 marks empty. The table lives in the
* owning child's private heap — head->fd_hash itself sits in shm alongside
* the rest of the objhead, but the buffer it points at is per-process and
* unreachable from any other address space, the same shape head->array
* uses for OBJ_LOCAL pools (objects.c:203-211).
*
* Replaces the O(n) linear walk over head->array in
* find_local_object_by_fd() with a single hash probe. That function is
* called from register_returned_fd() on every successful RET_FD syscall
* whose entry->ret_objtype is not OBJ_NONE (open, openat, socket, accept,
* eventfd, timerfd, perf_event_open, io_uring_setup, memfd_create,
* pidfd, fanotify_init, etc.), so the saving applies on the syscall hot
* path with head->num_entries typically in the tens-to-low-hundreds.
*/
static unsigned int local_fd_hash_slot_idx(int fd)
{
return (unsigned int)fd & (LOCAL_FD_HASH_SIZE - 1);
}
static void local_fd_hash_alloc(struct objhead *head)
{
unsigned int i;
head->fd_hash = malloc(LOCAL_FD_HASH_SIZE *
sizeof(struct local_fd_hash_slot));
if (head->fd_hash == NULL)
return;
for (i = 0; i < LOCAL_FD_HASH_SIZE; i++) {
head->fd_hash[i].fd = -1;
head->fd_hash[i].obj = NULL;
}
}
/*
* Internal insert that does not check for an existing entry — used by
* local_fd_hash_remove() to re-seat displaced entries after a removal.
* The displaced entry's identity is unchanged, so the original (fd, obj)
* pair is reinserted unconditionally into the first empty slot.
*/
static void local_fd_hash_reinsert(struct objhead *head, int fd,
struct object *obj)
{
unsigned int slot, probe;
slot = local_fd_hash_slot_idx(fd);
for (probe = 0; probe < LOCAL_FD_HASH_SIZE; probe++) {
if (head->fd_hash[slot].fd == -1) {
head->fd_hash[slot].fd = fd;
head->fd_hash[slot].obj = obj;
return;
}
slot = (slot + 1) & (LOCAL_FD_HASH_SIZE - 1);
}
}
static void local_fd_hash_insert(struct objhead *head, int fd,
struct object *obj)
{
unsigned int slot, probe;
if (fd < 0)
return;
if (head->fd_hash == NULL) {
local_fd_hash_alloc(head);
if (head->fd_hash == NULL)
return;
}
slot = local_fd_hash_slot_idx(fd);
for (probe = 0; probe < LOCAL_FD_HASH_SIZE; probe++) {
if (head->fd_hash[slot].fd == -1 ||
head->fd_hash[slot].fd == fd) {
head->fd_hash[slot].fd = fd;
head->fd_hash[slot].obj = obj;
return;
}
slot = (slot + 1) & (LOCAL_FD_HASH_SIZE - 1);
}
/*
* Table saturated. Realistically unreachable — LOCAL_FD_HASH_SIZE
* sits well above any per-(child, type) pool we have observed —
* but if it ever happens the caller gracefully falls back to the
* uninserted state: find_local_object_by_fd() returns NULL and
* register_returned_fd() simply re-adds, which is the same outcome
* as the pre-hash linear walk missing the entry.
*/
}
static void local_fd_hash_remove(struct objhead *head, int fd)
{
unsigned int slot, next, i;
if (fd < 0 || head->fd_hash == NULL)
return;
slot = local_fd_hash_slot_idx(fd);
for (i = 0; i < LOCAL_FD_HASH_SIZE; i++) {
if (head->fd_hash[slot].fd == -1)
return;
if (head->fd_hash[slot].fd == fd) {
head->fd_hash[slot].fd = -1;
head->fd_hash[slot].obj = NULL;
/*
* Linear-probing removal: re-seat any entries in the
* chain following us so a later lookup that hashes
* past this newly-empty slot still finds them.
*/
next = (slot + 1) & (LOCAL_FD_HASH_SIZE - 1);
while (head->fd_hash[next].fd != -1) {
struct local_fd_hash_slot displaced =
head->fd_hash[next];
head->fd_hash[next].fd = -1;
head->fd_hash[next].obj = NULL;
local_fd_hash_reinsert(head, displaced.fd,
displaced.obj);
next = (next + 1) & (LOCAL_FD_HASH_SIZE - 1);
}
return;
}
slot = (slot + 1) & (LOCAL_FD_HASH_SIZE - 1);
}
}
static struct object *local_fd_hash_lookup(struct objhead *head, int fd)
{
unsigned int slot, i;
if (fd < 0 || head->fd_hash == NULL)
return NULL;
slot = local_fd_hash_slot_idx(fd);
for (i = 0; i < LOCAL_FD_HASH_SIZE; i++) {
if (head->fd_hash[slot].fd == -1)
return NULL;
if (head->fd_hash[slot].fd == fd)
return head->fd_hash[slot].obj;
slot = (slot + 1) & (LOCAL_FD_HASH_SIZE - 1);
}
return NULL;
}
/*
* The trinity obj pool is split across two allocators by design:
*
* OBJ_GLOBAL: the obj struct lives in the shared obj heap
* (alloc_shared_obj). Every OBJ_GLOBAL provider sets
* head->shared_alloc=true in its init function and
* allocates each obj from the shared heap. Initialised
* in the parent before fork so children inherit the
* array via the shm mapping; children then read those
* pointers and follow them to the per-obj struct in
* shared memory. Children MUST NOT add to or destroy
* from these pools (enforced by the early return in
* add_object/destroy_object when getpid() != mainpid).
*
* OBJ_LOCAL: the obj struct lives in the calling process's private
* heap (alloc_object → zmalloc → malloc). Each child
* manages its own pool independently — head->array
* itself sits in shm (under child->objects[type]) so
* the parent's sanity walker can see slot count and
* raw addresses, but the obj structs the array points
* to are unreachable from any other process's address
* space. head->shared_alloc is ignored for OBJ_LOCAL
* pools; release_obj() routes to plain free().
*
* The split is intentional. OBJ_GLOBAL types are parent-curated
* resources visible fleet-wide (testfiles, mq's, pidfds, ...).
* OBJ_LOCAL types are per-child runtime state (sockets the child
* opened, futexes the child created, ...). Migrating OBJ_LOCAL into
* the shared heap would mix per-child state into shared bookkeeping
* with no benefit and would force every child to coordinate against
* alloc_shared_obj's lock-free CAS bump on every syscall pre/post
* hook — pointless contention on the hot path.
*
* Anything that walks another process's OBJ_LOCAL pool (debug.c
* dump_childdata is the one current caller) cannot dereference the
* obj pointers — they are foreign-private. See the matching note
* in dump_childdata().
*/
struct object * alloc_object(void)
{
return zmalloc(sizeof(struct object));
}
/*
* Release an obj struct via the right deallocator for its (scope, type).
*
* OBJ_GLOBAL types that opted into the shared obj heap (shared_alloc=true,
* set by the type's init function) came from alloc_shared_obj() and must
* be returned via free_shared_obj() — calling free() on a pointer into
* the shared heap would hand a non-malloc'd address to glibc.
*
* Everything else (OBJ_LOCAL always, plus any OBJ_GLOBAL type that did
* not opt into the shared heap) came from alloc_object() → zmalloc()
* and is routed through deferred_free_enqueue() rather than free()'d
* immediately. Plain free() ends an obj struct's lifetime the moment
* __destroy_object() drops the slot, but get_map() and friends read
* &obj->map after taking the slot pointer out of head->array — if the
* arg-gen path that invoked get_map() (or a stale slot pointer that
* survived a wild value-result-syscall write) hands the freed chunk
* back, the next deref hits a glibc-reclaimed cache line. Routing
* through deferred_free gives the chunk a 5-50 syscall TTL, which is
* far longer than any in-flight get_map() consumer holds the pointer.
*
* Before handing the chunk to the deferred-free ring we memset it to
* zero. The destructor (called by __destroy_object before us) has
* already torn down the obj's referenced state — for OBJ_MMAP_*
* map_destructor() unmaps the VMA and frees map->name, so the
* unzeroed remainder (map.ptr, map.size, map.prot, map.flags, fd,
* type, array_idx) describes a mapping that no longer exists. A
* later get_map() read of those fields via a stale slot pointer
* would happily pass the size>0 / size<4GB sanity check at
* mm/maps.c:85 and return a map* whose ptr addresses an unmapped
* VMA — a SIGSEGV/EFAULT in the very next consumer. Zeroing makes
* the post-destroy contents trip the size==0 band of that same check
* instead, so a stale-slot read is rejected at the get_map boundary
* rather than propagating into the syscall. The memset is also
* cheap on never-published objs (the add_object failure paths give
* us a zmalloc'd chunk whose contents are already zero) and the
* zeroed pointer fields make any double-deref reachable via a wild
* slot pointer fault on a NULL access instead of a wild address.
*/
static void release_obj(struct object *obj, enum obj_scope scope,
enum objecttype type)
{
if (scope == OBJ_GLOBAL && shm->global_objects[type].shared_alloc) {
free_shared_obj(obj, sizeof(struct object));
return;
}
memset(obj, 0, sizeof(*obj));
deferred_free_enqueue(obj, free);
}
struct objhead * get_objhead(enum obj_scope scope, enum objecttype type)
{
struct objhead *head;
if (scope == OBJ_GLOBAL)
head = &shm->global_objects[type];
else {
struct childdata *child;
child = this_child();
if (child == NULL)
return NULL;
head = &child->objects[type];
}
return head;
}
/*
* Fixed capacity for global object arrays. These are allocated in
* MAP_SHARED memory so children can safely read them. Using realloc()
* on private heap would put the new array in the parent's address space
* only, causing children to SIGSEGV when they follow the pointer.
*
* Exposed in objects.h so other code (e.g. mm/maps.c) can use the
* same upper bound when defending against a corrupt num_entries.
*/
void add_object(struct object *obj, enum obj_scope scope, enum objecttype type)
{
struct objhead *head;
bool was_protected = false;
char pcbuf[128];
if (unlikely(verbosity > 1)) {
output(2, "ADD-OBJ slot=%p type=%d caller=%s\n", obj, type,
pc_to_string(__builtin_return_address(0), pcbuf, sizeof(pcbuf)));
}
/*
* Reject obviously-corrupted fd values before they enter any pool.
* 1<<20 = 1048576 matches the kernel's NR_OPEN ceiling
* (include/uapi/linux/fs.h), the absolute upper bound RLIMIT_NOFILE
* may be raised to on every distro we exercise -- so any retval
* decoding to a value past this is a smoking-gun upper-bit
* corruption (sign-extended or wholesale-stomped rec->retval) that
* the existing "(long)retval >= 0" gate in register_returned_fd /
* the per-syscall .post handlers let through because the lower bits
* happened to be positive. Registering such a value into an
* OBJ_FD_* pool causes a later get_random_object() consumer to hand
* it back to the kernel as a real fd, where it either trips EBADF
* noise or, worse, a coincidentally-truncated int slot lands on a
* file-table entry an unrelated path opened. This is the same wild-
* write hazard class the per-caller-PC attribution ring landed in
* 8d1eade3b63c was built to surface; routing the rejection through
* post_handler_corrupt_ptr_bump on the rec==NULL path feeds that
* ring with the .post handler's return address so the dump names
* the syscall whose retval produced the bogus fd.
* __builtin_return_address read at depth 0 only -- depth >0 trips
* -Wframe-address and the resulting PC is unsafe under aggressive
* optimisation, so the PC capture site is always add_object itself
* and the recorded address names add_object's immediate caller.
*/
if (is_fd_type(type)) {
int fd = fd_from_object(obj, type);
if (fd < 0 || fd >= (1 << 20)) {
outputerr("add_object: rejecting out-of-bound fd=%d "
"type=%u caller=%s\n", fd, type,
pc_to_string(__builtin_return_address(0),
pcbuf, sizeof(pcbuf)));
post_handler_corrupt_ptr_bump(NULL,
__builtin_return_address(0));
release_obj(obj, scope, type);
return;
}
}
/* Children must not mutate global objects — the objhead metadata
* is in shared memory but the objects/arrays are in per-process
* heap (COW after fork). Mixing the two corrupts everything. */
if (scope == OBJ_GLOBAL && getpid() != mainpid) {
release_obj(obj, scope, type);
return;
}
if (scope == OBJ_GLOBAL) {
lock(&shm->objlock);
/* Most parent-side OBJ_GLOBAL adds happen during init,
* before freeze. The post-freeze case is fd regeneration
* via try_regenerate_fd() — temporarily lift the RO
* protection so the array writes can land. */
if (globals_are_protected()) {
thaw_global_objects();
was_protected = true;
}
}
head = get_objhead(scope, type);
/*
* Snapshot head->num_entries once and use the snapshot for the
* grow check, the size computation, the slot write, and the
* publish below. head->num_entries lives in shm (per-child for
* OBJ_LOCAL, shm->global_objects[] for OBJ_GLOBAL) and is reachable
* from any fuzzed value-result syscall whose length argument lands
* inside that struct -- the same wild-write hazard that motivated
* the OBJHEAD_SANE_LIMIT defence in objhead_looks_sane(). Without
* a local snapshot, a stomp landing between the grow check and the
* slot write lets the index used at head->array[N]=obj diverge
* from the index the grow check sized for, and the slot write
* lands past the array's bounds (heap-buffer-overflow at
* objects.c:411). Snapshotting once also collapses two reloads
* the compiler can't elide across the malloc / mprotect calls in
* the OBJ_LOCAL grow path, where every reload of head->num_entries
* widens the same TOCTOU window.
*/
unsigned int n = head->num_entries;
/* For global objects, the array was pre-allocated in shared
* memory by init_object_lists(). Never realloc — just reject
* if we've hit the fixed capacity. */
if (scope == OBJ_GLOBAL) {
if (n >= head->array_capacity) {
outputerr("add_object: global array full for type %u "
"(cap %u)\n", type, head->array_capacity);
if (is_fd_type(type)) {
int fd = fd_from_object(obj, type);
if (fd >= 0)
close(fd);
}
release_obj(obj, scope, type);
goto out_unlock;
}
} else if (n >= head->array_capacity) {
/*
* Local objects: grow on the private heap.
*
* Hand-rolled allocate-copy-defer-free instead of plain
* realloc(). realloc() returns the old chunk to glibc the
* moment the resize forces a move, but get_random_object()
* (and find_local_object_by_fd, for_each_obj iterators, the
* arg-gen path get_map → alloc_iovec → ...) read head->array
* lockless from the same child without any temporal barrier.
* A compiler-hoisted load of head->array, an interrupted code
* path holding the prior pointer, or a stale slot pointer
* that survived a wild value-result write can all keep the
* OLD array container live past the resize -- next deref
* lands inside a glibc-reclaimed chunk.
*
* Routing the old container through deferred_free_enqueue()
* gives it the same 5-50 syscall (effective 80-800 with
* DEFERRED_TICK_BATCH) TTL the obj struct frees already
* enjoy via release_obj() above. That is far longer than
* any in-flight head->array reader's window, and closes the
* UAF on the array container the same way the get_map fix
* (3a8d344f0f73, 546f576fae24) closed the UAF on the obj
* struct. Same hazard shape, same defence.
*
* The deferred_free ring rejects sub-page / canonical-out-of-
* range / misaligned ptrs (looks_like_corrupted_ptr) and ptrs
* overlapping any tracked shared region. The OBJ_LOCAL
* head->array sits in private heap returned by malloc, so it
* passes both bands trivially.
*/
struct object **newarray;
struct object **oldarray;
unsigned int newcap, oldcap;
/*
* Doubling-then-walk: the entry condition n >= array_capacity
* normally means n == array_capacity, so doubling
* array_capacity gives newcap = 2*n which strictly exceeds
* the index we are about to write. If a wild write has
* scribbled head->num_entries past array_capacity, the
* single double can come back smaller than the snapshot --
* walk the doubling until newcap > n. Bail with a
* release_obj if a further double would overflow unsigned
* int rather than letting the OOB land.
*/
if (head->array_capacity > UINT_MAX / 2) {
outputerr("add_object: cap overflow type=%u num_entries=%u capacity=%u\n",
type, n, head->array_capacity);
if (is_fd_type(type)) {
int fd = fd_from_object(obj, type);
if (fd >= 0)
close(fd);
}
release_obj(obj, scope, type);
return;
}
newcap = head->array_capacity ? head->array_capacity * 2 : 16;
while (newcap <= n) {
if (newcap > UINT_MAX / 2) {
outputerr("add_object: cap overflow type=%u num_entries=%u capacity=%u\n",
type, n, head->array_capacity);
if (is_fd_type(type)) {
int fd = fd_from_object(obj, type);
if (fd >= 0)
close(fd);
}
release_obj(obj, scope, type);
return;
}
newcap *= 2;
}
newarray = malloc(newcap * sizeof(struct object *));
if (newarray == NULL) {
outputerr("add_object: malloc failed for type %u (cap %u)\n",
type, newcap);
if (is_fd_type(type)) {
int fd = fd_from_object(obj, type);
if (fd >= 0)
close(fd);
}
release_obj(obj, scope, type);
return;
}
oldcap = head->array_capacity;
oldarray = head->array;
if (oldarray != NULL && oldcap > 0)
memcpy(newarray, oldarray,
oldcap * sizeof(struct object *));
head->array = newarray;
head->array_capacity = newcap;
if (oldarray != NULL) {
/*
* Diagnostic: a per-PC bump attributed to this site is
* firing at ~21/sec in live fuzz runs. oldarray is a
* glibc malloc result from this very function so it
* should pass the shape heuristic on every call. Log
* the actual value (rate-limited) when the heuristic
* would reject so we can tell whether the values are
* well-formed heap pointers (attribution artefact) or
* weird values (a real corruption upstream). Pure
* informational, does not change behaviour.
*/
if (looks_like_corrupted_ptr(NULL, oldarray)) {
static unsigned long shape_rejects;
unsigned long n2 = ++shape_rejects;
if ((n2 % 1000) == 1)
outputerr("add_object pre-defer-free oldarray=%p "
"(oldcap=%u, newcap=%u, head=%p) [%lu cumulative]\n",
oldarray, oldcap, newcap, head, n2);
}
deferred_free_enqueue(oldarray, free);
}
}
/*
* Bump the slot's version BEFORE publishing the new pointer so a
* concurrent lockless reader that snapshots slot_versions[n]
* after this point and reads array[n] sees a (version, ptr)
* pair that's internally consistent. RELEASE so the bump is
* visible to the child's ACQUIRE-load in get_random_object().
* Skipped for OBJ_LOCAL (no slot_versions array there — no
* lockless reader to coordinate with).
*/
if (scope == OBJ_GLOBAL && head->slot_versions != NULL)
__atomic_add_fetch(&head->slot_versions[n], 1,
__ATOMIC_RELEASE);
head->array[n] = obj;
obj->array_idx = n;
/*
* RELEASE-publish the new count so a child doing a lockless
* ACQUIRE-load in get_random_object() that sees count=N+1 also
* sees the array[N] = obj write that preceded it. For OBJ_LOCAL
* the pool is per-child private, so a plain store suffices.
*/
if (scope == OBJ_GLOBAL)
__atomic_store_n(&head->num_entries, n + 1, __ATOMIC_RELEASE);
else
head->num_entries = n + 1;
/* Mirror the parent-side global fd hash for OBJ_LOCAL fd-typed
* pools so find_local_object_by_fd() resolves in O(1). The buffer
* is lazily allocated by local_fd_hash_insert() on first use. */
if (scope == OBJ_LOCAL && is_fd_type(type)) {
int fd = fd_from_object(obj, type);
if (fd >= 0)
local_fd_hash_insert(head, fd, obj);
}
/* Track global fd-type objects in the hash table */
if (scope == OBJ_GLOBAL && is_fd_type(type)) {
int fd = fd_from_object(obj, type);
if (!fd_hash_insert(fd, obj, type)) {
outputerr("add_object: fd hash full for type %u, dropping fd %d\n",
type, fd);
/*
* Drop the count first so a concurrent lockless child
* read picking up the new snapshot sees the lower
* count and won't index past the (about-to-be-NULLed)
* tail slot. RELEASE pairs with the child's ACQUIRE.
* Roll back to the same n the slot write used so a
* wild write that scribbled head->num_entries between
* the publish above and here can't drop the count to
* a stale value or NULL the wrong slot.
*/
__atomic_store_n(&head->num_entries, n,
__ATOMIC_RELEASE);
head->array[n] = NULL;
/*
* Rollback bump: a lockless reader that briefly
* observed snapshot=n+1 may have captured the pre-
* rollback (slot_versions[n], array[n]) pair and be
* mid-validation. Bump the version again so its post-
* use re-acquire diverges and the obj — about to be
* release_obj()'d into the freelist — is rejected.
*/
if (head->slot_versions != NULL)
__atomic_add_fetch(&head->slot_versions[n], 1,
__ATOMIC_RELEASE);
if (fd >= 0)
close(fd);
release_obj(obj, scope, type);
goto out_unlock;
}
}
/* Per-object dumps are debug noise at startup (NFUTEXES = 5 * cpus
* identical "futex: 0 owner:0 scope:1" lines, etc.). Gate on -vv.
* dump_childdata() calls head->dump directly for crash diagnostics
* and is unaffected by this gate. */
if (head->dump != NULL && verbosity > 2)
head->dump(obj, scope);
out_unlock:
if (scope == OBJ_GLOBAL) {
if (was_protected)
freeze_global_objects();
unlock(&shm->objlock);
}
/* if we just added something to a child list, check
* to see if we need to do some pruning.
*/
if (scope == OBJ_LOCAL)
prune_objects();
}
void init_object_lists(enum obj_scope scope, struct childdata *child)
{
unsigned int i;
for (i = 0; i < MAX_OBJECT_TYPES; i++) {
struct objhead *head;
if (scope == OBJ_GLOBAL)
head = &shm->global_objects[i];
else {
if (child == NULL)
return;
head = &child->objects[i];
}
head->num_entries = 0;
if (scope == OBJ_GLOBAL) {
/* Pre-allocate the parallel array in MAP_SHARED memory
* so children can safely read it. Never realloc.
* Tagged global so freeze_global_objects() will mprotect
* it RO once init is done. */
head->array = alloc_shared_global(GLOBAL_OBJ_MAX_CAPACITY *
sizeof(struct object *));
memset(head->array, 0, GLOBAL_OBJ_MAX_CAPACITY *
sizeof(struct object *));
head->array_capacity = GLOBAL_OBJ_MAX_CAPACITY;
/*
* Parallel per-slot version counter for the lockless
* child reader's seqlock-style consistency check.
* Same backing region as ->array (alloc_shared_global)
* so freeze/thaw/mprotect cycles cover both.
*/
head->slot_versions =
alloc_shared_global(GLOBAL_OBJ_MAX_CAPACITY *
sizeof(unsigned int));
memset(head->slot_versions, 0, GLOBAL_OBJ_MAX_CAPACITY *
sizeof(unsigned int));
} else {
head->array = NULL;
head->array_capacity = 0;
head->slot_versions = NULL;
}
/*
* Per-OBJ_LOCAL fd→object hash starts empty. Lazily
* allocated in private heap on the first add_object() insert
* for fd-typed pools. Reset here even on the OBJ_GLOBAL path
* because shm slot reuse across child generations could leave
* a stale pointer from a prior child in the shared objhead;
* an unconditional NULL write keeps the lazy-alloc check in
* local_fd_hash_insert() honest.
*/
head->fd_hash = NULL;
/*
* child lists can inherit properties from global lists.
*/
if (scope == OBJ_LOCAL) {
struct objhead *globalhead;
globalhead = &shm->global_objects[i];
head->max_entries = globalhead->max_entries;
head->destroy = globalhead->destroy;
head->dump = globalhead->dump;
}
}
}
/*
* Pick a random object from a pool.
*
* Lockless child read path (OBJ_GLOBAL):
* Children must NOT take shm->objlock here. Doing so deadlocks the
* fleet whenever a child is killed mid-syscall while holding objlock —
* the parent's reaper then blocks forever waiting for the dead child
* to release a lock it can never release. The defensive pid_alive()
* bypass added in e4e32ff0 (zombie pid_alive) papered over one
* instance of this; eliminating the lock acquisition on the child
* read path closes the whole class. Audit (task 4LSD-ae2QTmkKyPKHPo7hQ)
* identified 23 HIGH sites where children reach this lock; this fix
* collapses the entire category-A cluster (get_random_object on the
* syscall arg-pickers' hot path).
*
* Memory ordering:
* The child snapshots head->num_entries with __ATOMIC_ACQUIRE,
* pairing with the parent mutators (add_object, __destroy_object)
* that publish updates with __ATOMIC_RELEASE. Acquire/release
* guarantees that if the child observes count = N+1, it also
* observes the parent's array[N] = obj store that preceded the
* count bump. Without this pairing, a child could pick an index
* into a slot whose backing store hadn't yet propagated.
* Modeled on fd_hash_lookup() (objects.c:159) which uses the same
* pattern for the parallel fd hash table.
*
* Worst-case race:
* The child reads array[idx] without taking objlock, so it can read
* a stale pointer that the parent is concurrently overwriting (swap-
* with-last in __destroy_object) or whose target object the parent
* has just free()d. This is the SAME failure mode as the existing
* "OBJ_GLOBAL objects allocated in parent heap break for children"
* problem tracked in trinity-todo.md (item: OBJ_GLOBAL pool entries
* allocated in parent heap break for children) — the structural fix
* is to allocate the struct objects themselves in shared memory.
* Until that lands, the caller validates the returned pointer and
* the catch-all sighandler turns any raw deref crash into _exit;
* we are NOT making it worse, only widening an existing window.
*
* Why lockless is safe enough:
* 1. Parent mutators run while shm->global_objects is mprotect-thawed
* and re-freeze on completion — the array memory itself isn't
* remapped or relocated under the child (capacity is fixed at
* init, GLOBAL_OBJ_MAX_CAPACITY).
* 2. ACQUIRE/RELEASE on num_entries gives a consistent (count, slots)
* pair w.r.t. the most recent publish.
* 3. The remaining race (stale array[idx] pointer) is upper-bounded
* by the OBJ_GLOBAL-in-parent-heap problem and addressed by the
* separately-tracked structural fix.
*/
/*
* Lockless seqlock-style sample of one OBJ_GLOBAL slot from a child.
*
* Reads slot_versions[idx] before and after sampling array[idx]; if the
* two versions match AND the obj pointer is non-NULL we have a (version,
* obj) pair that no concurrent destroy interleaved with. On mismatch
* the parent mutated the slot inside our window — return NULL to the
* caller's retry loop. On a stable but NULL slot (transient swap-with-
* last torn state) likewise return NULL so the retry picks a fresh idx.
*
* The caller saves *version_out for a later validate_object_handle()
* re-acquire if it carries the obj past its own deref window (e.g. the
* arg-gen path, where get_map() returns &obj->map and the consumer
* derefs map->ptr several frames downstream).
*/
static struct object *sample_global_slot(struct objhead *head,
unsigned int idx,
unsigned int *version_out)
{
unsigned int v_a, v_b;
struct object *obj;
v_a = __atomic_load_n(&head->slot_versions[idx], __ATOMIC_ACQUIRE);
obj = __atomic_load_n(&head->array[idx], __ATOMIC_ACQUIRE);
v_b = __atomic_load_n(&head->slot_versions[idx], __ATOMIC_ACQUIRE);
if (v_a != v_b || obj == NULL)
return NULL;
*version_out = v_a;
return obj;
}
/*
* Bounded retry budget for the lockless reader's seqlock loop.
*
* A single mismatch means one parent-side destroy raced with the
* sample; a small handful of retries absorbs back-to-back regen churn
* on the same pool without spinning forever in the (theoretical) case
* of a parent that destroys faster than the child can sample. Beyond
* that, surface NULL to the caller — most consumers (get_map, the
* fd_provider syscalls) treat NULL as "pick something else this round"
* and just retry at their own granularity.
*/
#define GET_RANDOM_OBJECT_RETRY_BUDGET 8
static struct object *get_random_object_global_lockless(struct objhead *head,
unsigned int *idx_out,
unsigned int *version_out)
{
unsigned int snapshot;
unsigned int idx;
unsigned int version;
struct object *obj;
int attempt;