Repository navigation
Expand file tree
/
Copy pathtree.c
More file actions
1842 lines (1579 loc) · 53.4 KB
/
Copy pathtree.c
File metadata and controls
1842 lines (1579 loc) · 53.4 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
/* MIB service - tree.c - tree access and management */
#include "mib.h"
/*
* Does the given identifier fall within the range of static identifiers in the
* given parent? This check can be used to enumerate all static array entries
* in the given parent, starting from zero. The check does not guarantee that
* the entry is actually for a valid node, nor does it guarantee that there is
* not a dynamic node with this identifier.
*/
#define IS_STATIC_ID(parent, id) ((unsigned int)(id) < (parent)->node_size)
/*
* Scratch buffer, used for various cases of temporary data storage. It must
* be large enough to fit a sysctldesc structure followed by the longest
* supported description. It must also be large enough to serve as temporary
* storage for data being written in the majority of cases. Finally, it must
* be large enough to contain an entire page, for mib_copyin_str().
*/
#define MAXDESCLEN 1024 /* from NetBSD */
#define SCRATCH_SIZE MAX(PAGE_SIZE, sizeof(struct sysctldesc) + MAXDESCLEN)
static char scratch[SCRATCH_SIZE] __aligned(sizeof(int32_t));
unsigned int mib_nodes; /* how many nodes are there in the tree? */
unsigned int mib_objects; /* how many memory objects are allocated? */
unsigned int mib_remotes; /* how many remote subtrees are there? */
/*
* Find a node through its parent node and identifier. Return the node if it
* was found, and optionally store a pointer to the pointer to its dynode
* superstructure (for removal). If no matching node was found, return NULL.
*/
static struct mib_node *
mib_find(struct mib_node * parent, int id, struct mib_dynode *** prevpp)
{
struct mib_node *node;
struct mib_dynode **dynp;
if (id < 0)
return NULL;
/*
* Is there a static node with this identifier? The static nodes are
* all in a single array, so lookup is O(1) for these nodes. We use
* the node flags field to see whether the array entry is valid.
*/
if (IS_STATIC_ID(parent, id)) {
node = &parent->node_scptr[id];
if (node->node_flags != 0) {
/* Found a matching static node. */
if (prevpp != NULL)
*prevpp = NULL;
return node;
}
}
/*
* Is there a dynamic node with this identifier? The dynamic nodes
* form a linked list. This is predominantly because userland may pick
* the identifier number at creation time, so we cannot rely on all
* dynamically created nodes falling into a small identifier range.
* That eliminates the option of a dynamic array indexed by identifier,
* and a linked list is the simplest next option. Thus, dynamic node
* lookup is O(n). However, since the list is sorted by identifier,
* we may be able to stop the search early.
*/
for (dynp = &parent->node_dcptr; *dynp != NULL;
dynp = &((*dynp)->dynode_next)) {
if ((*dynp)->dynode_id == id) {
/* Found a matching dynamic node. */
if (prevpp != NULL)
*prevpp = dynp;
return &(*dynp)->dynode_node;
} else if ((*dynp)->dynode_id > id)
break; /* no need to look further */
}
return NULL;
}
/*
* Copy out a node to userland, using the exchange format for nodes (namely,
* a sysctlnode structure). Return the size of the object that is (or, if the
* node falls outside the requested data range, would be) copied out on
* success, or a negative error code on failure. The function may return 0
* to indicate that nothing was copied out after all (this is unused here).
*/
static ssize_t
mib_copyout_node(struct mib_call * call, struct mib_oldp * oldp, size_t off,
int id, const struct mib_node * node)
{
struct sysctlnode scn;
int visible;
if (!mib_inrange(oldp, off))
return sizeof(scn); /* nothing to do */
memset(&scn, 0, sizeof(scn));
/*
* We use CTLFLAG_PARENT, CTLFLAG_VERIFY, and CTLFLAG_REMOTE internally
* only. NetBSD uses the values of these flags for different purposes.
* Either way, do not expose them to userland.
*/
scn.sysctl_flags = SYSCTL_VERSION | (node->node_flags &
~(CTLFLAG_PARENT | CTLFLAG_VERIFY | CTLFLAG_REMOTE));
scn.sysctl_num = id;
strlcpy(scn.sysctl_name, node->node_name, sizeof(scn.sysctl_name));
scn.sysctl_ver = node->node_ver;
scn.sysctl_size = node->node_size;
/* Some information is only visible if the user can access the node. */
visible = (!(node->node_flags & CTLFLAG_PRIVATE) || mib_authed(call));
/*
* For immediate types, store the immediate value in the resulting
* structure, unless the caller is not authorized to obtain the value.
*/
if ((node->node_flags & CTLFLAG_IMMEDIATE) && visible) {
switch (SYSCTL_TYPE(node->node_flags)) {
case CTLTYPE_BOOL:
scn.sysctl_bdata = node->node_bool;
break;
case CTLTYPE_INT:
scn.sysctl_idata = node->node_int;
break;
case CTLTYPE_QUAD:
scn.sysctl_qdata = node->node_quad;
}
}
/* Special rules apply to parent nodes. */
if (SYSCTL_TYPE(node->node_flags) == CTLTYPE_NODE) {
/* Report the node size the way NetBSD does, just in case. */
scn.sysctl_size = sizeof(scn);
/*
* If this is a remote node, use the values we have of the root
* of the remote subtree. If we did not have these values, we
* would have to call into the remote service here, which for
* reliability purposes is a bad idea.
*
* If this is a real parent node, report child information. In
* both these cases, expose child information only if the node
* itself is accessible by the caller.
*
* If this is a function-driven node, indicate this by setting
* a nonzero function address. This allows trace(1) to
* determine that it should not attempt to descend into this
* part of the tree as usual, because a) accessing subnodes may
* have side effects, and b) meta-identifiers may not work as
* expected in these parts of the tree. Do not return the real
* function pointer, as this would leak anti-ASR information.
*/
if (node->node_flags & CTLFLAG_REMOTE) {
if (visible) {
scn.sysctl_csize = node->node_rcsize;
scn.sysctl_clen = node->node_rclen;
}
} else if (node->node_flags & CTLFLAG_PARENT) {
if (visible) {
scn.sysctl_csize = node->node_csize;
scn.sysctl_clen = node->node_clen;
}
} else
scn.sysctl_func = SYSCTL_NODE_FN;
}
/* Copy out the resulting node. */
return mib_copyout(oldp, off, &scn, sizeof(scn));
}
/*
* Given a query on a non-leaf (parent) node, provide the user with an array of
* this node's children.
*/
static ssize_t
mib_query(struct mib_call * call, struct mib_node * parent,
struct mib_oldp * oldp, struct mib_newp * newp)
{
struct sysctlnode scn;
struct mib_node *node;
struct mib_dynode *dynode;
size_t off;
int r, id;
/* If the user passed in version numbers, check them. */
if (newp != NULL) {
if ((r = mib_copyin(newp, &scn, sizeof(scn))) != OK)
return r;
if (SYSCTL_VERS(scn.sysctl_flags) != SYSCTL_VERSION)
return EINVAL;
/*
* If a node version number is given, it must match the version
* of the parent or the root.
*/
if (scn.sysctl_ver != 0 &&
scn.sysctl_ver != mib_root.node_ver &&
scn.sysctl_ver != parent->node_ver)
return EINVAL;
}
/*
* We need not return the nodes strictly in ascending order of
* identifiers, as this is not expected by userland. For example,
* sysctlgetmibinfo(3) performs its own sorting after a query.
* Thus, we can go through the static and dynamic nodes separately.
*/
off = 0;
/* First enumerate the static nodes. */
for (id = 0; IS_STATIC_ID(parent, id); id++) {
node = &parent->node_scptr[id];
if (node->node_flags == 0)
continue;
if ((r = mib_copyout_node(call, oldp, off, id, node)) < 0)
return r;
off += r;
}
/* Then enumerate the dynamic nodes. */
for (dynode = parent->node_dcptr; dynode != NULL;
dynode = dynode->dynode_next) {
node = &dynode->dynode_node;
if ((r = mib_copyout_node(call, oldp, off, dynode->dynode_id,
node)) < 0)
return r;
off += r;
}
return off;
}
/*
* Check whether the given name buffer contains a valid node name string. If
* the name is nonempty, properly terminated, and contains only acceptable
* characters, return the length of the string excluding null terminator.
* Otherwise, return zero to indicate failure.
*/
static size_t
mib_check_name(const char * name, size_t namesize)
{
size_t namelen;
char c;
/* Names must be nonempty, null terminated, C symbol style strings. */
for (namelen = 0; namelen < namesize; namelen++) {
if ((c = name[namelen]) == '\0')
break;
/* A-Z, a-z, 0-9, _ only, and no digit as first character. */
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
c == '_' || (c >= '0' && c <= '9' && namelen > 0)))
return 0;
}
if (namelen == 0 || namelen == namesize)
return 0;
return namelen;
}
/*
* Scan a parent node's children, as part of new nod
8
e creati
10BC8
on. Search for
* either a free node identifier (if given_id < 0) or collisions with the node
* identifier to use (if given_id >= 0). Also check for name collisions. Upon
* success, return OK, with the resulting node identifier stored in 'idp' and a
* pointer to the pointer for the new dynamic node stored in 'prevpp'. Upon
* failure, return an error code. If the failure is EEXIST, 'idp' will contain
* the ID of the conflicting node, and 'nodep' will point to the node.
*/
static int
mib_scan(struct mib_node * parent, int given_id, const char * name, int * idp,
struct mib_dynode *** prevpp, struct mib_node ** nodep)
{
struct mib_dynode **prevp, **dynp;
struct mib_node *node;
int id;
/*
* We must verify that no entry already exists with the given name. In
* addition, if a nonnegative identifier is given, we should use that
* identifier and make sure it does not already exist. Otherwise, we
* must find a free identifier. Finally, we sort the dynamic nodes in
* ascending identifier order, so we must find the right place at which
* to insert the new node.
*
* For finding a free identifier, choose an ID that falls (well)
* outside the static range, both to avoid accidental retrieval by an
* application that uses a static ID, and to simplify verifying that
* the ID is indeed free. The sorting of dynamic nodes by identifier
* ensures that searching for a free identifier is O(n).
*
* At this time, we do not support some NetBSD features. We do not
* force success if the new node is sufficiently like an existing one.
* Also, we do not use global autoincrement for dynamic identifiers,
* although that could easily be changed.
*/
/* First check the static node array, just for collisions. */
for (id = 0; IS_STATIC_ID(parent, id); id++) {
node = &parent->node_scptr[id];
if (node->node_flags == 0)
continue;
if (id == given_id || !strcmp(name, node->node_name)) {
*idp = id;
*nodep = node;
return EEXIST;
}
}
/*
* Then try to find the place to insert a new dynamic node. At the
* same time, check for both identifier and name collisions.
*/
if (given_id >= 0)
id = given_id;
else
id = MAX(CREATE_BASE, parent->node_size);
for (prevp = &parent->node_dcptr; *prevp != NULL;
prevp = &((*prevp)->dynode_next)) {
if ((*prevp)->dynode_id > id)
break;
if ((*prevp)->dynode_id == id) {
if (given_id >= 0) {
*idp = id;
*nodep = &(*prevp)->dynode_node;
return EEXIST;
} else
id++;
}
if (!strcmp(name, (*prevp)->dynode_node.node_name)) {
*idp = (*prevp)->dynode_id;
*nodep = &(*prevp)->dynode_node;
return EEXIST;
}
}
/* Finally, check the rest of the dynamic nodes for name collisions. */
for (dynp = prevp; *dynp != NULL; dynp = &((*dynp)->dynode_next)) {
assert((*dynp)->dynode_id > id);
if (!strcmp(name, (*dynp)->dynode_node.node_name)) {
*idp = (*dynp)->dynode_id;
*nodep = &(*dynp)->dynode_node;
return EEXIST;
}
}
*idp = id;
*prevpp = prevp;
return OK;
}
/*
* Copy in a string from the user process, located at the given remote address,
* into the given local buffer. If no buffer is given, just compute the length
* of the string. On success, return OK. If 'sizep' is not NULL, it will be
* filled with the string size, including the null terminator. If a non-NULL
* buffer was given, the string will be copied into the provided buffer (also
* including null terminator). Return an error code on failure, which includes
* the case that no null terminator was found within the range of bytes that
* would fit in the given buffer.
*/
static int
mib_copyin_str(struct mib_newp * __restrict newp, vir_bytes addr,
char * __restrict buf, size_t bufsize, size_t * __restrict sizep)
{
char *ptr, *endp;
size_t chunk, len;
int r;
assert(newp != NULL);
assert(bufsize <= SSIZE_MAX);
if (addr == 0)
return EINVAL;
/*
* NetBSD has a kernel routine for copying in a string from userland.
* MINIX3 does not, since its system call interface has always relied
* on userland passing in string lengths. The sysctl(2) API does not
* provide the string length, and thus, we have to do a bit of guess
* work. If we copy too little at once, performance suffers. If we
* copy too much at once, we may trigger an unneeded page fault. Make
* use of page boundaries to strike a balance between those two. If we
* are requested to just get the string length, use the scratch buffer.
*/
len = 0;
while (bufsize > 0) {
chunk = PAGE_SIZE - (addr % PAGE_SIZE);
if (chunk > bufsize)
chunk = bufsize;
ptr = (buf != NULL) ? &buf[len] : scratch;
if ((r = mib_copyin_aux(newp, addr, ptr, chunk)) != OK)
return r;
if ((endp = memchr(ptr, '\0', chunk)) != NULL) {
/* A null terminator was found - success. */
if (sizep != NULL)
*sizep = len + (size_t)(endp - ptr) + 1;
return OK;
}
addr += chunk;
len += chunk;
bufsize -= chunk;
}
/* No null terminator found. */
return EINVAL;
}
/*
* Increase the version of the root node, and copy this new version to all
* nodes on the path to the given node, including that node itself.
*/
static void
mib_upgrade(struct mib_node * node)
{
uint32_t ver;
assert(node != NULL);
/*
* The root node determines the version of the entire tree. Do not use
* version number 0, as a zero version number indicates no interest in
* versions elsewhere.
*/
ver = mib_root.node_ver + 1;
if (ver == 0)
ver = 1;
/* Copy the new version to all the nodes on the path. */
do {
node->node_ver = ver;
node = node->node_parent;
} while (node != NULL);
}
/*
* Add a new dynamically allocated node into the tree, inserting it into the
* linked-list position of the parent tree as given by 'prevp'. Also update
* versions and counters accordingly. This function never fails.
*/
static void
mib_add(struct mib_dynode * dynode, struct mib_dynode ** prevp)
{
struct mib_node *parent;
parent = dynode->dynode_node.node_parent;
assert(parent != NULL);
/* Link the dynamic node into the list, in the right place. */
assert(prevp != NULL);
dynode->dynode_next = *prevp;
*prevp = dynode;
/* The parent node now has one more child. */
parent->node_csize++;
parent->node_clen++;
/* There is now one more node in the tree. */
mib_nodes++;
/*
* Bump the version of all nodes on the path to the new node, including
* the node itself.
*/
mib_upgrade(&dynode->dynode_node);
}
/*
* Create a node.
*/
static ssize_t
mib_create(struct mib_call * call, struct mib_node * parent,
struct mib_oldp * oldp, struct mib_newp * newp)
{
struct mib_dynode *dynode, **prevp;
struct mib_node *node;
struct sysctlnode scn;
size_t namelen, size;
ssize_t len;
bool b;
char c;
int r, id;
/* This is a privileged operation. */
if (!mib_authed(call))
return EPERM;
/*
* The parent must not be a remote node, but this is already implied by
* the fact that we got here at all.
*/
assert(SYSCTL_TYPE(parent->node_flags) == CTLTYPE_NODE);
assert(!(parent->node_flags & CTLFLAG_REMOTE));
/* The parent node must not be marked as read-only. */
if (!(parent->node_flags & CTLFLAG_READWRITE))
return EPERM;
/*
* Has the parent reached its child node limit? This check is entirely
* theoretical as long as we support only 32-bit virtual memory.
*/
if (parent->node_csize == INT_MAX)
return EINVAL;
assert(parent->node_clen <= parent->node_csize);
/* The caller must supply information on the child node to create. */
if (newp == NULL)
return EINVAL;
if ((r = mib_copyin(newp, &scn, sizeof(scn))) != OK)
return r;
/*
* We perform as many checks as possible before we start allocating
* memory. Then again, after allocation, copying in data may still
* fail. Unlike when setting values, we do not first copy data into a
* temporary buffer here, because we do not need to: if the copy fails,
* the entire create operation fails, so atomicity is not an issue.
*/
if (SYSCTL_VERS(scn.sysctl_flags) != SYSCTL_VERSION)
return EINVAL;
/*
* If a node version number is giv
10BC8
en, it must match the version of
* either the parent or the root node. The given version number is
* *not* used for the node being created.
*/
if (scn.sysctl_ver != 0 && scn.sysctl_ver != mib_root.node_ver &&
scn.sysctl_ver != parent->node_ver)
return EINVAL;
/*
* Validate the node flags. In addition to the NetBSD-allowed flags,
* we also allow UNSIGNED, and leave its interpretation to userland.
*/
if (SYSCTL_FLAGS(scn.sysctl_flags) &
~(SYSCTL_USERFLAGS | CTLFLAG_UNSIGNED))
return EINVAL;
if (!(scn.sysctl_flags & CTLFLAG_IMMEDIATE)) {
/*
* Without either IMMEDIATE or OWNDATA, data pointers are
* actually kernel addresses--a concept we do not support.
* Otherwise, if IMMEDIATE is not set, we are going to have to
* allocate extra memory for the data, so force OWNDATA to be.
* set. Node-type nodes have no data, though.
*/
if (SYSCTL_TYPE(scn.sysctl_flags) != CTLTYPE_NODE) {
if (!(scn.sysctl_flags & CTLFLAG_OWNDATA) &&
scn.sysctl_data != NULL)
return EINVAL; /* not meaningful on MINIX3 */
scn.sysctl_flags |= CTLFLAG_OWNDATA;
}
} else if (scn.sysctl_flags & CTLFLAG_OWNDATA)
return EINVAL;
/* The READWRITE flag consists of multiple bits. Sanitize. */
if (scn.sysctl_flags & CTLFLAG_READWRITE)
scn.sysctl_flags |= CTLFLAG_READWRITE;
/* Validate the node type and size, and do some additional checks. */
switch (SYSCTL_TYPE(scn.sysctl_flags)) {
case CTLTYPE_BOOL:
if (scn.sysctl_size != sizeof(bool))
return EINVAL;
break;
case CTLTYPE_INT:
if (scn.sysctl_size != sizeof(int))
return EINVAL;
break;
case CTLTYPE_QUAD:
if (scn.sysctl_size != sizeof(u_quad_t))
return EINVAL;
break;
case CTLTYPE_STRING:
/*
* For strings, a zero length means that we are supposed to
* allocate a buffer size based on the given string size.
*/
if (scn.sysctl_size == 0 && scn.sysctl_data != NULL) {
if ((r = mib_copyin_str(newp,
(vir_bytes)scn.sysctl_data, NULL, SSIZE_MAX,
&size)) != OK)
return r;
scn.sysctl_size = size;
}
/* FALLTHROUGH */
case CTLTYPE_STRUCT:
/*
* We do not set an upper size on the data size, since it would
* still be possible to create a large number of nodes, and
* this is a privileged operation ayway.
*/
if (scn.sysctl_size == 0 || scn.sysctl_size > SSIZE_MAX)
return EINVAL;
if (scn.sysctl_flags & CTLFLAG_IMMEDIATE)
return EINVAL;
break;
case CTLTYPE_NODE:
/*
* The zero size is an API requirement, but we also rely on the
* zero value internally, as the node has no static children.
*/
if (scn.sysctl_size != 0)
return EINVAL;
if (scn.sysctl_flags & (CTLFLAG_IMMEDIATE | CTLFLAG_OWNDATA))
return EINVAL;
if (scn.sysctl_csize != 0 || scn.sysctl_clen != 0 ||
scn.sysctl_child != NULL)
return EINVAL;
break;
default:
return EINVAL;
}
if (scn.sysctl_func != NULL || scn.sysctl_parent != NULL)
return EINVAL;
/* Verify that the given name is valid, and get its string length. */
namelen = mib_check_name(scn.sysctl_name, sizeof(scn.sysctl_name));
if (namelen == 0)
return EINVAL;
/*
* Find a free identifier, or check for ID collisions if a specific
* identifier was given. At the same time, scan for name collisions,
* and find the location at which to insert the new node in the list.
*/
r = mib_scan(parent, scn.sysctl_num, scn.sysctl_name, &id, &prevp,
&node);
if (r != OK) {
/*
* On collisions, if requested, copy out the existing node.
* This does not quite fit the general interaction model, as
* the service must now return a nonzero old length from a call
* that actually failed (in contrast to ENOMEM failures).
*/
if (r == EEXIST && oldp != NULL) {
len = mib_copyout_node(call, oldp, 0, id, node);
if (len > 0)
mib_setoldlen(call, len);
}
return r;
}
/*
* All checks so far have passed. "id" now contains the new node
* identifier, and "prevp" points to the pointer at which to insert the
* new node in its parent's linked list of dynamic nodes.
*
* We can now attempt to create and initialize a new dynamic node.
* Allocating nodes this way may cause heavy memory fragmentation over
* time, but we do not expect the tree to see heavy modification at run
* time, and the superuser has easier ways to get the MIB service in
* trouble. We note that even in low-memory conditions, the MIB
* service is always able to provide basic functionality.
*/
size = sizeof(*dynode) + namelen;
if (!(scn.sysctl_flags & CTLFLAG_IMMEDIATE))
size += scn.sysctl_size;
if ((dynode = malloc(size)) == NULL)
return EINVAL; /* do not return ENOMEM */
mib_objects++;
/* From here on, we have to free "dynode" before returning an error. */
r = OK;
memset(dynode, 0, sizeof(*dynode)); /* no need to zero all of "size" */
dynode->dynode_id = id;
strlcpy(dynode->dynode_name, scn.sysctl_name, namelen + 1);
node = &dynode->dynode_node;
node->node_flags = scn.sysctl_flags & ~SYSCTL_VERS_MASK;
if (SYSCTL_TYPE(scn.sysctl_flags) == CTLTYPE_NODE)
node->node_flags |= CTLFLAG_PARENT;
node->node_size = scn.sysctl_size;
node->node_parent = parent;
node->node_name = dynode->dynode_name;
/* Initialize the node value. */
if (scn.sysctl_flags & CTLFLAG_IMMEDIATE) {
switch (SYSCTL_TYPE(scn.sysctl_flags)) {
case CTLTYPE_BOOL:
/* Sanitize booleans. See the C99 _Bool comment. */
memcpy(&c, &scn.sysctl_bdata, sizeof(c));
node->node_bool = (bool)c;
break;
case CTLTYPE_INT:
node->node_int = scn.sysctl_idata;
break;
case CTLTYPE_QUAD:
node->node_quad = scn.sysctl_qdata;
break;
default:
assert(0);
}
} else if (SYSCTL_TYPE(scn.sysctl_flags) != CTLTYPE_NODE) {
node->node_data = dynode->dynode_name + namelen + 1;
/* Did the user supply initial data? If not, use zeroes. */
if (scn.sysctl_data != NULL) {
/*
* For strings, do not copy in more than needed. This
* is just a nice feature which allows initialization
* of large string buffers with short strings.
*/
if (SYSCTL_TYPE(scn.sysctl_flags) == CTLTYPE_STRING)
r = mib_copyin_str(newp,
(vir_bytes)scn.sysctl_data,
node->node_data, scn.sysctl_size, NULL);
else
r = mib_copyin_aux(newp,
(vir_bytes)scn.sysctl_data,
node->node_data, scn.sysctl_size);
} else
memset(node->node_data, 0, scn.sysctl_size);
/*
* Sanitize booleans. See the C99 _Bool comment elsewhere.
* In this case it is not as big of a deal, as we will not be
* accessing the boolean value directly ourselves.
*/
if (r == OK && SYSCTL_TYPE(scn.sysctl_flags) == CTLTYPE_BOOL) {
b = (bool)*(char *)node->node_data;
memcpy(node->node_data, &b, sizeof(b));
}
}
/*
* Even though it would be entirely possible to set a description right
* away as well, this does not seem to be supported on NetBSD at all.
*/
/* Deal with earlier failures now. */
if (r != OK) {
free(dynode);
mib_objects--;
return r;
}
/*
* At this point, actual creation can no longer fail. Add the node
* into the tree, and update versions and counters.
*/
mib_add(dynode, prevp);
/*
* Copy out the newly created node as resulting ("old") data. Do not
* undo the creation if this fails, though.
*/
return mib_copyout_node(call, oldp, 0, id, node);
}
/*
* Remove the given node from the tree. If 'prevp' is NULL, the node is a
* static node which should be zeroed out. If 'prevp' is not NULL, the node is
* a dynamic node which should be freed; 'prevp' will then point to the pointer
* to its dynode container. Also update versions and counters as appropriate.
* This function never fails.
*/
static void
mib_remove(struct mib_node * node, struct mib_dynode ** prevp)
{
struct mib_dynode *dynode;
struct mib_node *parent;
parent = node->node_parent;
assert(parent != NULL);
/* If the description was allocated, free it. */
if (node->node_flags & CTLFLAG_OWNDESC) {
free(__UNCONST(node->node_desc));
mib_objects--;
}
/*
* Static nodes only use static memory, and dynamic nodes have the data
* area embedded in the dynode object. In neither case is data memory
* allocated separately, and thus, it need never be freed separately.
* Therefore we *must not* check CTLFLAG_OWNDATA here.
*/
assert(parent->node_csize > 0);
assert(parent->node_clen > 0);
/*
* Dynamic nodes must be freed. Freeing the dynode object also frees
* the node name and any associated data. Static nodes are zeroed out,
* and the static memory they referenced will become inaccessible.
*/
if (prevp != NULL) {
dynode = *prevp;
*prevp = dynode->dynode_next;
assert(node == &dynode->dynode_node);
free(dynode);
mib_objects--;
parent->node_csize--;
} else
memset(node, 0, sizeof(*node));
parent->node_clen--;
mib_nodes--;
/* Bump the version of all nodes on the path to the destroyed node. */
mib_upgrade(parent);
}
/*
* Destroy a node.
*/
static ssize_t
mib_destroy(struct mib_call * call, struct mib_node * parent,
struct mib_oldp * oldp, struct mib_newp * newp)
{
struct mib_dynode **prevp;
struct mib_node *node;
struct sysctlnode scn;
ssize_t r;
/* This is a privileged operation. */
if (!mib_authed(call))
return EPERM;
/* The parent node must not be marked as read-only. */
if (!(parent->node_flags & CTLFLAG_READWRITE))
return EPERM;
/* The caller must specify which child node to destroy. */
if (newp == NULL)
return EINVAL;
if ((r = mib_copyin(newp, &scn, sizeof(scn))) != OK)
return r;
if (SYSCTL_VERS(scn.sysctl_flags) != SYSCTL_VERSION)
return EINVAL;
/* Locate the child node. */
if ((node = mib_find(parent, scn.sysctl_num, &prevp)) == NULL)
return ENOENT;
/* The node must not be marked as permanent. */
if (node->node_flags & CTLFLAG_PERMANENT)
return EPERM;
/* For node-type nodes, extra rules apply. */
if (SYSCTL_TYPE(node->node_flags) == CTLTYPE_NODE) {
/* The node must not be a mount point. */
if (node->node_flags & CTLFLAG_REMOTE)
return EBUSY;
/* The node must not have an associated function. */
if (!(node->node_flags & CTLFLAG_PARENT))
return EPERM;
/* The target node must itself not have child nodes. */
if (node->node_clen != 0)
return ENOTEMPTY;
}
/* If the user supplied a version, it must match the node version. */
if (scn.sysctl_ver != 0 && scn.sysctl_ver != node->node_ver)
return EINVAL; /* NetBSD inconsistently throws ENOENT here */
/* If the user supplied a name, it must match the node name. */
if (scn.sysctl_name[0] != '\0') {
if (memchr(scn.sysctl_name, '\0',
sizeof(scn.sysctl_name)) == NULL)
return EINVAL;
if (strcmp(scn.sysctl_name, node->node_name))
return EINVAL; /* also ENOENT on NetBSD */
}
/*
* Copy out the old node if requested, otherwise return the length
* anyway. The node will be destroyed even if this call fails,
* because that is how NetBSD behaves.
*/
r = mib_copyout_node(call, oldp, 0, scn.sysctl_num, node);
/*
* Remove the node from the tree. The procedure depends on whether the
* node is static (prevp == NULL) or dynamic (prevp != NULL). Also
* update versions and counters.
*/
mib_remove(node, prevp);
return r;
}
/*
* Copy out a node description to userland, using the exchange format for node
* descriptions (namely, a sysctldesc structure). Return the size of the
* object that is (or, if the description falls outside the requested data
* range, would be) copied out on success, or a negative error code on failure.
* The function may return 0 to indicate that nothing was copied out after all.
*/
static ssize_t
mib_copyout_desc(struct mib_call * call, struct mib_oldp * oldp, size_t off,
int id, const struct mib_node * node)
{
struct sysctldesc *scd;
size_t size;
int r;
/* Descriptions of private nodes are considered private too. */
if ((node->node_flags & CTLFLAG_PRIVATE) && !mib_authed(call))
return 0;
/* The description length includes the null terminator. */
if (node->node_desc != NULL)
size = strlen(node->node_desc) + 1;
else
size = 1;
assert(sizeof(*scd) + size <= sizeof(scratch));
scd = (struct sysctldesc *)scratch;
memset(scd, 0, sizeof(*scd));
scd->descr_num = id;
scd->descr_ver = node->node_ver;
scd->descr_len = size;
if (node->node_desc != NULL)
strlcpy(scd->descr_str, node->node_desc,
sizeof(scratch) - sizeof(*scd));
else
scd->descr_str[0] = '\0';
size += offsetof(struct sysctldesc, descr_str);
if ((r = mib_copyout(oldp, off, scratch, size)) < 0)
return r;
/*
* By aligning just the size, we may leave garbage between the entries
* copied out, which is fine because it is userland's own data.
*/
return roundup2(size, sizeof(int32_t));
}
/*
* Retrieve node descriptions in bulk, or retrieve or assign a particular
* node's description.
*/
static ssize_t
mib_describe(struct mib_call * call, struct mib_node * parent,
struct mib_oldp * oldp, struct mib_newp * newp)
{
struct sysctlnode scn;
struct mib_node *node;
struct mib_dynode *dynode;
size_t off;
int r, id;
/* If new data are given, they identify a particular target node. */
if (newp != NULL) {
if ((r = mib_copyin(newp, &scn, sizeof(scn))) != OK)
return r;
if (SYSCTL_VERS(scn.sysctl_flags) != SYSCTL_VERSION)
return EINVAL;
/* Locate the child node. */
if ((node = mib_find(parent, scn.sysctl_num, NULL)) == NULL)
return ENOENT;
/* Descriptions of private nodes are considered private too. */
if ((node->node_flags & CTLFLAG_PRIVATE) && !mib_authed(call))
return EPERM;
/*
* If a description pointer was given, this is a request to
* set the node's description.