This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
/
ObjectInputFilter.java
1413 lines (1338 loc) · 70 KB
/
ObjectInputFilter.java
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) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.io;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.StaticProperty;
import sun.security.action.GetBooleanAction;
import java.lang.reflect.InvocationTargetException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.Security;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import static java.io.ObjectInputFilter.Status.*;
import static java.lang.System.Logger.Level.TRACE;
import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.Logger.Level.ERROR;
/**
* Filter classes, array lengths, and graph metrics during deserialization.
*
* <p><strong>Warning: Deserialization of untrusted data is inherently dangerous
* and should be avoided. Untrusted data should be carefully validated according to the
* "Serialization and Deserialization" section of the
* {@extLink secure_coding_guidelines_javase Secure Coding Guidelines for Java SE}.
* {@extLink serialization_filter_guide Serialization Filtering} describes best
* practices for defensive use of serial filters.
* </strong></p>
*
* <p>To protect against deserialization vulnerabilities, application developers
* need a clear description of the objects that can be deserialized
* by each component or library. For each context and use case, developers should
* construct and apply an appropriate filter.
*
* <h2>Deserialization Filtering Factory and Filters</h2>
* The parts of deserialization filtering are the filters, composite filters, and filter factory.
* Each filter performs checks on classes and resource limits to determine the status as
* rejected, allowed, or undecided.
* Filters can be composed of other filters and merge or combine their results.
* The filter factory is responsible for establishing and updating the filter
* for each {@link ObjectInputStream}.
*
* <p>For simple cases, a static JVM-wide filter can be set for the entire application,
* without setting a filter factory.
* The JVM-wide filter can be set either with a system property on the command line or by
* calling {@linkplain Config#setSerialFilter(ObjectInputFilter) Config.setSerialFilter}.
* No custom filter factory needs to be specified, defaulting to the builtin filter factory.
* The builtin filter factory provides the {@linkplain Config#getSerialFilter static JVM-wide filter}
* for each {@linkplain ObjectInputStream ObjectInputStream}.
*
* <p>For example, a filter that allows example classes, allows classes in the
* {@code java.base} module, and rejects all other classes can be set:
*
* <pre>{@code As a command line property:
* % java -Djdk.serialFilter="example.*;java.base/*;!*" ...}</pre>
*
* <pre>{@code Or programmatically:
* var filter = ObjectInputFilter.Config.createFilter("example.*;java.base/*;!*")
* ObjectInputFilter.Config.setSerialFilter(filter);}</pre>
*
* <p>In an application with multiple execution contexts, the application can provide a
* {@linkplain Config#setSerialFilterFactory(BinaryOperator) filter factory} to
* protect individual contexts by providing a custom filter for each. When the stream
* is constructed, the filter factory is called to identify the execution context from the available
* information, including the current thread-local state, hierarchy of callers, library, module,
* and class loader. At that point, the filter factory policy for creating or selecting filters
* can choose a specific filter or composition of filters based on the context.
* The JVM-wide deserialization filter factory ensures that a context-specific deserialization
* filter can be set on every {@link ObjectInputStream} and every object read from the
* stream can be checked.
*
* <h2>Invoking the Filter Factory</h2>
* <p>The JVM-wide filter factory is a function invoked when each {@link ObjectInputStream} is
* {@linkplain ObjectInputStream#ObjectInputStream() constructed} and when the
* {@linkplain ObjectInputStream#setObjectInputFilter(ObjectInputFilter) stream-specific filter is set}.
* The parameters are the current filter and a requested filter and it
* returns the filter to be used for the stream. When invoked from the
* {@linkplain ObjectInputStream#ObjectInputStream(InputStream) ObjectInputStream constructors},
* the first parameter is {@code null} and the second parameter is the
* {@linkplain ObjectInputFilter.Config#getSerialFilter() static JVM-wide filter}.
* When invoked from {@link ObjectInputStream#setObjectInputFilter ObjectInputStream.setObjectInputFilter},
* the first parameter is the filter currently set on the stream (which was set in the constructor),
* and the second parameter is the filter given to {@code ObjectInputStream.setObjectInputFilter}.
* The current and new filter may each be {@code null} and the factory may return {@code null}.
* Note that the filter factory implementation can also use any contextual information
* at its disposal, for example, extracted from the application thread context, or its call stack,
* to compose and combine a new filter. It is not restricted to only use its two parameters.
*
* <p>The active deserialization filter factory is either:
* <ul>
* <li>The application specific filter factory set via {@link Config#setSerialFilterFactory(BinaryOperator)}
* or the system property {@code jdk.serialFilterFactory} or
* the security property {@code jdk.serialFilterFactory}.
* <li>Otherwise, a builtin deserialization filter factory
* provides the {@linkplain Config#getSerialFilter static JVM-wide filter} when invoked from the
* {@linkplain ObjectInputStream#ObjectInputStream(InputStream) ObjectInputStream constructors}
* and replaces the static filter when invoked from
* {@link ObjectInputStream#setObjectInputFilter(ObjectInputFilter)}.
* See {@linkplain Config#getSerialFilterFactory() getSerialFilterFactory}.
* </ul>
*
* <h2>Filters</h2>
* Filters can be created from a {@linkplain Config#createFilter(String) pattern string},
* or based on a {@linkplain Predicate predicate of a class} to
* {@linkplain #allowFilter(Predicate, Status) allow} or
* {@linkplain #rejectFilter(Predicate, Status) reject} classes.
*
* <p>The filter's {@link #checkInput checkInput(FilterInfo)} method is invoked
* zero or more times while {@linkplain ObjectInputStream#readObject() reading objects}.
* The method is called to validate classes, the length of each array,
* the number of objects being read from the stream, the depth of the graph,
* and the total number of bytes read from the stream.
*
* <p>Composite filters combine or check the results of other filters.
* The {@link #merge(ObjectInputFilter, ObjectInputFilter) merge(filter, anotherFilter)}
* filter combines the status value of two filters.
* The {@link #rejectUndecidedClass(ObjectInputFilter) rejectUndecidedClass(filter)}
* checks the result of a filter for classes when the status is {@code UNDECIDED}.
* In many cases any class not {@code ALLOWED} by the filter should be {@code REJECTED}.
* <p>
* A deserialization filter determines whether the arguments are allowed or rejected and
* should return the appropriate status: {@link Status#ALLOWED ALLOWED} or {@link Status#REJECTED REJECTED}.
* If the filter cannot determine the status it should return {@link Status#UNDECIDED UNDECIDED}.
* Filters should be designed for the specific use case and expected types.
* A filter designed for a particular use may be passed a class outside
* of the scope of the filter. If the purpose of the filter is to reject classes
* then it can reject a candidate class that matches and report {@code UNDECIDED} for others.
* A filter may be called with class equals {@code null}, {@code arrayLength} equal -1,
* the depth, number of references, and stream size and return a status
* that reflects only one or only some of the values.
* This allows a filter to be specific about the choice it is reporting and
* to use other filters without forcing either allowed or rejected status.
*
* <h2>Filter Model Examples</h2>
* For simple applications, a single predefined filter listing allowed or rejected
* classes may be sufficient to manage the risk of deserializing unexpected classes.
* <p>For an application composed from multiple modules or libraries, the structure
* of the application can be used to identify the classes to be allowed or rejected
* by each {@link ObjectInputStream} in each context of the application.
* The deserialization filter factory is invoked when each stream is constructed and
* can examine the thread or program to determine a context-specific filter to be applied.
* Some possible examples:
* <ul>
* <li>Thread-local state can hold the filter to be applied or composed
* with a stream-specific filter.
* Filters could be pushed and popped from a virtual stack of filters
* maintained by the application or libraries.
* <li>The filter factory can identify the caller of the deserialization method
* and use module or library context to select a filter or compose an appropriate
* context-specific filter.
* A mechanism could identify a callee with restricted or unrestricted
* access to serialized classes and choose a filter accordingly.
* </ul>
*
* <h2>Example to filter every deserialization in a thread</h2>
*
* This class shows how an application provided filter factory can combine filters
* to check every deserialization operation that takes place in a thread.
* It defines a thread-local variable to hold the thread-specific filter, and constructs a filter factory
* that composes that filter with the static JVM-wide filter and the stream-specific filter.
* The {@code doWithSerialFilter} method does the setup of the thread-specific filter
* and invokes the application provided {@link Runnable Runnable}.
*
* <pre>{@code
* public static final class FilterInThread implements BinaryOperator<ObjectInputFilter> {
*
* private final ThreadLocal<ObjectInputFilter> filterThreadLocal = new ThreadLocal<>();
*
* // Construct a FilterInThread deserialization filter factory.
* public FilterInThread() {}
*
* // Returns a composite filter of the static JVM-wide filter, a thread-specific filter,
* // and the stream-specific filter.
* public ObjectInputFilter apply(ObjectInputFilter curr, ObjectInputFilter next) {
* if (curr == null) {
* // Called from the OIS constructor or perhaps OIS.setObjectInputFilter with no current filter
* var filter = filterThreadLocal.get();
* if (filter != null) {
* // Prepend a filter to reject all UNDECIDED results
* filter = ObjectInputFilter.rejectUndecidedClass(filter);
* }
* if (next != null) {
* // Prepend the next filter to the thread filter, if any
* // Initially this is the static JVM-wide filter passed from the OIS constructor
* // Append the filter to reject all UNDECIDED results
* filter = ObjectInputFilter.merge(next, filter);
* filter = ObjectInputFilter.rejectUndecidedClass(filter);
* }
* return filter;
* } else {
* // Called from OIS.setObjectInputFilter with a current filter and a stream-specific filter.
* // The curr filter already incorporates the thread filter and static JVM-wide filter
* // and rejection of undecided classes
* // If there is a stream-specific filter prepend it and a filter to recheck for undecided
* if (next != null) {
* next = ObjectInputFilter.merge(next, curr);
* next = ObjectInputFilter.rejectUndecidedClass(next);
* return next;
* }
* return curr;
* }
* }
*
* // Applies the filter to the thread and invokes the runnable.
* public void doWithSerialFilter(ObjectInputFilter filter, Runnable runnable) {
* var prevFilter = filterThreadLocal.get();
* try {
* filterThreadLocal.set(filter);
* runnable.run();
* } finally {
* filterThreadLocal.set(prevFilter);
* }
* }
* }
* }</pre>
* <h3>Using the Filter Factory</h3>
* To use {@code FilterInThread} utility create an instance and configure it as the
* JVM-wide filter factory. The {@code doWithSerialFilter} method is invoked with a
* filter allowing the example application and core classes:
* <pre>{@code
* // Create a FilterInThread filter factory and set
* var filterInThread = new FilterInThread();
* ObjectInputFilter.Config.setSerialFilterFactory(filterInThread);
*
* // Create a filter to allow example.* classes and reject all others
* var filter = ObjectInputFilter.Config.createFilter("example.*;java.base/*;!*");
* filterInThread.doWithSerialFilter(filter, () -> {
* byte[] bytes = ...;
* var o = deserializeObject(bytes);
* });
* }</pre>
* <p>
* Unless otherwise noted, passing a {@code null} argument to a
* method in this interface and its nested classes will cause a
* {@link NullPointerException} to be thrown.
*
* @see ObjectInputStream#setObjectInputFilter(ObjectInputFilter)
* @since 9
*/
@FunctionalInterface
public interface ObjectInputFilter {
/**
* Check the class, array length, number of object references, depth,
* stream size, and other available filtering information.
* Implementations of this method check the contents of the object graph being created
* during deserialization. The filter returns {@link Status#ALLOWED Status.ALLOWED},
* {@link Status#REJECTED Status.REJECTED}, or {@link Status#UNDECIDED Status.UNDECIDED}.
*
* <p>If {@code filterInfo.serialClass()} is {@code non-null}, there is a class to be checked.
* If {@code serialClass()} is {@code null}, there is no class and the info contains
* only metrics related to the depth of the graph being deserialized, the number of
* references, and the size of the stream read.
*
* @apiNote Each filter implementing {@code checkInput} should return one of the values of {@link Status}.
* Returning {@code null} may result in a {@link NullPointerException} or other unpredictable behavior.
*
* @param filterInfo provides information about the current object being deserialized,
* if any, and the status of the {@link ObjectInputStream}
* @return {@link Status#ALLOWED Status.ALLOWED} if accepted,
* {@link Status#REJECTED Status.REJECTED} if rejected,
* {@link Status#UNDECIDED Status.UNDECIDED} if undecided.
*/
Status checkInput(FilterInfo filterInfo);
/**
* Returns a filter that returns {@code Status.ALLOWED} if the predicate
* on the class is {@code true}.
* The filter returns {@code ALLOWED} or the {@code otherStatus} based on the predicate
* of the {@code non-null} class and {@code UNDECIDED} if the class is {@code null}.
*
* <p>When the filter's {@link ObjectInputFilter#checkInput checkInput(info)} method is invoked,
* the predicate is applied to the {@link FilterInfo#serialClass() info.serialClass()},
* the return Status is:
* <ul>
* <li>{@link Status#UNDECIDED UNDECIDED}, if the {@code serialClass} is {@code null},</li>
* <li>{@link Status#ALLOWED ALLOWED}, if the predicate on the class returns {@code true},</li>
* <li>Otherwise, return {@code otherStatus}.</li>
* </ul>
* <p>
* Example, to create a filter that will allow any class loaded from the platform
* or bootstrap classloaders.
* <pre><code>
* ObjectInputFilter f
* = allowFilter(cl -> cl.getClassLoader() == ClassLoader.getPlatformClassLoader() ||
* cl.getClassLoader() == null, Status.UNDECIDED);
* </code></pre>
*
* @param predicate a predicate to test a non-null Class
* @param otherStatus a Status to use if the predicate is {@code false}
* @return a filter that returns {@code ALLOWED} if the predicate
* on the class is {@code true}
* @since 17
*/
static ObjectInputFilter allowFilter(Predicate<Class<?>> predicate, Status otherStatus) {
Objects.requireNonNull(predicate, "predicate");
Objects.requireNonNull(otherStatus, "otherStatus");
return new Config.PredicateFilter(predicate, ALLOWED, otherStatus);
}
/**
* Returns a filter that returns {@code Status.REJECTED} if the predicate
* on the class is {@code true}.
* The filter returns {@code REJECTED} or the {@code otherStatus} based on the predicate
* of the {@code non-null} class and {@code UNDECIDED} if the class is {@code null}.
*
* When the filter's {@link ObjectInputFilter#checkInput checkInput(info)} method is invoked,
* the predicate is applied to the {@link FilterInfo#serialClass() serialClass()},
* the return Status is:
* <ul>
* <li>{@link Status#UNDECIDED UNDECIDED}, if the {@code serialClass} is {@code null},</li>
* <li>{@link Status#REJECTED REJECTED}, if the predicate on the class returns {@code true},</li>
* <li>Otherwise, return {@code otherStatus}.</li>
* </ul>
* <p>
* Example, to create a filter that will reject any class loaded from the application classloader.
* <pre><code>
* ObjectInputFilter f = rejectFilter(cl ->
* cl.getClassLoader() == ClassLoader.ClassLoader.getSystemClassLoader(), Status.UNDECIDED);
* </code></pre>
*
* @param predicate a predicate to test a non-null Class
* @param otherStatus a Status to use if the predicate is {@code false}
* @return returns a filter that returns {@code REJECTED} if the predicate
* on the class is {@code true}
* @since 17
*/
static ObjectInputFilter rejectFilter(Predicate<Class<?>> predicate, Status otherStatus) {
Objects.requireNonNull(predicate, "predicate");
Objects.requireNonNull(otherStatus, "otherStatus");
return new Config.PredicateFilter(predicate, REJECTED, otherStatus);
}
/**
* Returns a filter that merges the status of a filter and another filter.
* If {@code another} filter is {@code null}, the {@code filter} is returned.
* Otherwise, a {@code filter} is returned to merge the pair of {@code non-null} filters.
*
* The filter returned implements the {@link ObjectInputFilter#checkInput(FilterInfo)} method
* as follows:
* <ul>
* <li>Invoke {@code filter} on the {@code FilterInfo} to get its {@code status};
* <li>Return {@code REJECTED} if the {@code status} is {@code REJECTED};
* <li>Invoke {@code anotherFilter} to get the {@code otherStatus};
* <li>Return {@code REJECTED} if the {@code otherStatus} is {@code REJECTED};
* <li>Return {@code ALLOWED}, if either {@code status} or {@code otherStatus}
* is {@code ALLOWED}, </li>
* <li>Otherwise, return {@code UNDECIDED}</li>
* </ul>
*
* @param filter a filter
* @param anotherFilter a filter to be merged with the filter, may be {@code null}
* @return an {@link ObjectInputFilter} that merges the status of the filter and another filter
* @since 17
*/
static ObjectInputFilter merge(ObjectInputFilter filter, ObjectInputFilter anotherFilter) {
Objects.requireNonNull(filter, "filter");
return (anotherFilter == null) ? filter : new Config.MergeFilter(filter, anotherFilter);
}
/**
* Returns a filter that invokes a given filter and maps {@code UNDECIDED} to {@code REJECTED}
* for classes, with some special cases, and otherwise returns the status.
* If the class is not a primitive class and not an array, the status returned is {@code REJECTED}.
* If the class is a primitive class or an array class additional checks are performed;
* see the list below for details.
*
* <p>Object deserialization accepts a class if the filter returns {@code UNDECIDED}.
* Adding a filter to reject undecided results for classes that have not been
* either allowed or rejected can prevent classes from slipping through the filter.
*
* @implSpec
* The filter returned implements the {@link ObjectInputFilter#checkInput(FilterInfo)} method
* as follows:
* <ul>
* <li>Invoke the filter on the {@code FilterInfo} to get its {@code status};
* <li>Return the {@code status} if the status is {@code REJECTED} or {@code ALLOWED};
* <li>Return {@code UNDECIDED} if the {@code filterInfo.getSerialClass() serialClass}
* is {@code null};
* <li>Return {@code REJECTED} if the class is not an {@linkplain Class#isArray() array};
* <li>Determine the base component type if the {@code serialClass} is
* an {@linkplain Class#isArray() array};
* <li>Return {@code UNDECIDED} if the base component type is
* a {@linkplain Class#isPrimitive() primitive class};
* <li>Invoke the filter on the {@code base component type} to get its
* {@code component status};</li>
* <li>Return {@code ALLOWED} if the component status is {@code ALLOWED};
* <li>Otherwise, return {@code REJECTED}.</li>
* </ul>
*
* @param filter a filter
* @return an {@link ObjectInputFilter} that maps an {@link Status#UNDECIDED}
* status to {@link Status#REJECTED} for classes, otherwise returns the
* filter status
* @since 17
*/
static ObjectInputFilter rejectUndecidedClass(ObjectInputFilter filter) {
Objects.requireNonNull(filter, "filter");
return new Config.RejectUndecidedFilter(filter);
}
/**
* FilterInfo provides access to information about the current object
* being deserialized and the status of the {@link ObjectInputStream}.
* @since 9
*/
interface FilterInfo {
/**
* The class of an object being deserialized.
* For arrays, it is the array type.
* For example, the array class name of a 2 dimensional array of strings is
* "{@code [[Ljava.lang.String;}".
* To check the array's element type, iteratively use
* {@link Class#getComponentType() Class.getComponentType} while the result
* is an array and then check the class.
* The {@code serialClass is null} in the case where a new object is not being
* created and to give the filter a chance to check the depth, number of
* references to existing objects, and the stream size.
*
* @return class of an object being deserialized; may be null
*/
Class<?> serialClass();
/**
* The number of array elements when deserializing an array of the class.
*
* @return the non-negative number of array elements when deserializing
* an array of the class, otherwise -1
*/
long arrayLength();
/**
* The current depth.
* The depth starts at {@code 1} and increases for each nested object and
* decrements when each nested object returns.
*
* @return the current depth
*/
long depth();
/**
* The current number of object references.
*
* @return the non-negative current number of object references
*/
long references();
/**
* The current number of bytes consumed.
* @implSpec {@code streamBytes} is implementation specific
* and may not be directly related to the object in the stream
* that caused the callback.
*
* @return the non-negative current number of bytes consumed
*/
long streamBytes();
}
/**
* The status of a check on the class, array length, number of references,
* depth, and stream size.
*
* @since 9
*/
enum Status {
/**
* The status is undecided, not allowed and not rejected.
*/
UNDECIDED,
/**
* The status is allowed.
*/
ALLOWED,
/**
* The status is rejected.
*/
REJECTED;
}
/**
* A utility class to set and get the JVM-wide deserialization filter factory,
* the static JVM-wide filter, or to create a filter from a pattern string.
* The static filter factory and the static filter apply to the whole Java runtime,
* or "JVM-wide", there is only one of each. For a complete description of
* the function and use refer to {@link ObjectInputFilter}.
*
* <p>The JVM-wide deserialization filter factory and the static JVM-wide filter
* can be configured from system properties during the initialization of the
* {@code ObjectInputFilter.Config} class.
*
* <p>If the Java virtual machine is started with the system property
* {@systemProperty jdk.serialFilter}, its value is used to configure the filter.
* If the system property is not defined, and the {@link java.security.Security} property
* {@code jdk.serialFilter} is defined then it is used to configure the filter.
* Otherwise, the filter is not configured during initialization and
* can be set with {@link #setSerialFilter(ObjectInputFilter) Config.setSerialFilter}.
* Setting the {@code jdk.serialFilter} with {@link System#setProperty(String, String)
* System.setProperty} <em>does not set the filter</em>.
* The syntax for the property value is the same as for the
* {@link #createFilter(String) createFilter} method.
*
* <p>
* If the Java virtual machine is started with the system property
* {@systemProperty jdk.serialFilterFactory} or the {@link java.security.Security} property
* of the same name, its value names the class to configure the JVM-wide deserialization
* filter factory.
* If the system property is not defined, and the {@link java.security.Security} property
* {@code jdk.serialFilterFactory} is defined then it is used to configure the filter factory.
* If it remains unset, the filter factory is a builtin filter factory compatible
* with previous versions.
*
* <p>The class must be public, must have a public zero-argument constructor, implement the
* {@link BinaryOperator {@literal BinaryOperator<ObjectInputFilter>}} interface, provide its implementation and
* be accessible via the {@linkplain ClassLoader#getSystemClassLoader() application class loader}.
* If the filter factory constructor is not invoked successfully, an {@link ExceptionInInitializerError}
* is thrown and subsequent use of the filter factory for deserialization fails with
* {@link IllegalStateException}.
* The filter factory configured using the system or security property during initialization
* can NOT be replaced with {@link #setSerialFilterFactory(BinaryOperator) Config.setSerialFilterFactory}.
* This ensures that a filter factory set on the command line is not overridden accidentally
* or intentionally by the application.
*
* <p>Setting the {@code jdk.serialFilterFactory} with {@link System#setProperty(String, String)
* System.setProperty} <em>does not set the filter factory</em>.
* The syntax for the system property value and security property value is the
* fully qualified class name of the deserialization filter factory.
* @since 9
*/
@SuppressWarnings("removal")
final class Config {
/**
* Lock object for filter and filter factory.
*/
private final static Object serialFilterLock = new Object();
/**
* The property name for the filter.
* Used as a system property and a java.security.Security property.
*/
private static final String SERIAL_FILTER_PROPNAME = "jdk.serialFilter";
/**
* The property name for the filter factory.
* Used as a system property and a java.security.Security property.
*/
private static final String SERIAL_FILTER_FACTORY_PROPNAME = "jdk.serialFilterFactory";
/**
* Current static filter.
*/
private static volatile ObjectInputFilter serialFilter;
/**
* Current serial filter factory.
* @see Config#setSerialFilterFactory(BinaryOperator)
*/
private static volatile BinaryOperator<ObjectInputFilter> serialFilterFactory;
/**
* Boolean to indicate that the filter factory can not be set or replaced.
* - an ObjectInputStream has already been created using the current filter factory
* - has been set on the command line
* @see Config#setSerialFilterFactory(BinaryOperator)
*/
private static final AtomicBoolean filterFactoryNoReplace = new AtomicBoolean();
/**
* Debug and Trace Logger
*/
private static final System.Logger configLog;
static {
/*
* Initialize the configuration containing the filter factory, static filter, and logger.
* <ul>
* <li>The logger is created.
* <li>The property 'jdk.serialFilter" is read, either as a system property or a security property,
* and if set, defines the configured static JVM-wide filter and is logged.
* <li>The property jdk.serialFilterFactory is read, either as a system property or a security property,
* and if set, defines the initial filter factory and is logged.
* </ul>
*/
// Initialize the logger.
configLog = System.getLogger("java.io.serialization");
// Get the values of the system properties, if they are defined
String factoryClassName = StaticProperty.jdkSerialFilterFactory();
if (factoryClassName == null) {
// Fallback to security property
factoryClassName = AccessController.doPrivileged((PrivilegedAction<String>) () ->
Security.getProperty(SERIAL_FILTER_FACTORY_PROPNAME));
}
String filterString = StaticProperty.jdkSerialFilter();
if (filterString == null) {
// Fallback to security property
filterString = AccessController.doPrivileged((PrivilegedAction<String>) () ->
Security.getProperty(SERIAL_FILTER_PROPNAME));
}
// Initialize the static filter if the jdk.serialFilter is present
ObjectInputFilter filter = null;
if (filterString != null) {
configLog.log(DEBUG,
"Creating deserialization filter from {0}", filterString);
try {
filter = createFilter(filterString);
} catch (RuntimeException re) {
configLog.log(ERROR,
"Error configuring filter: {0}", re);
}
}
serialFilter = filter;
// Initialize the filter factory if the jdk.serialFilterFactory is defined
// otherwise use the builtin filter factory.
if (factoryClassName == null) {
serialFilterFactory = new BuiltinFilterFactory();
} else {
try {
// Load using the system class loader, the named class may be an application class.
// Cause Config.setSerialFilterFactory to throw {@link IllegalStateException}
// if Config.setSerialFilterFactory is called as a side effect of the
// static initialization of the class or constructor.
filterFactoryNoReplace.set(true);
Class<?> factoryClass = Class.forName(factoryClassName, true,
ClassLoader.getSystemClassLoader());
@SuppressWarnings("unchecked")
BinaryOperator<ObjectInputFilter> factory =
(BinaryOperator<ObjectInputFilter>)
factoryClass.getConstructor().newInstance(new Object[0]);
configLog.log(DEBUG,
"Creating deserialization filter factory for {0}", factoryClassName);
serialFilterFactory = factory;
} catch (RuntimeException | ClassNotFoundException | NoSuchMethodException |
IllegalAccessException | InstantiationException | InvocationTargetException ex) {
Throwable th = (ex instanceof InvocationTargetException ite) ? ite.getCause() : ex;
configLog.log(ERROR,
"Error configuring filter factory: {0}", (Object)th);
// Do not continue if configuration not initialized
throw new ExceptionInInitializerError(th);
}
}
// Setup shared secrets for RegistryImpl to use.
SharedSecrets.setJavaObjectInputFilterAccess(Config::createFilter2);
}
/**
* Config has no instances.
*/
private Config() {
}
/**
* Logger for filter actions.
*/
private static void traceFilter(String msg, Object... args) {
configLog.log(TRACE, msg, args);
}
/**
* Returns the static JVM-wide deserialization filter or {@code null} if not configured.
*
* @return the static JVM-wide deserialization filter or {@code null} if not configured
*/
public static ObjectInputFilter getSerialFilter() {
return serialFilter;
}
/**
* Set the static JVM-wide filter if it has not already been configured or set.
*
* @param filter the deserialization filter to set as the JVM-wide filter; not null
* @throws SecurityException if there is security manager and the
* {@code SerializablePermission("serialFilter")} is not granted
* @throws IllegalStateException if the filter has already been set
*/
public static void setSerialFilter(ObjectInputFilter filter) {
Objects.requireNonNull(filter, "filter");
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(ObjectStreamConstants.SERIAL_FILTER_PERMISSION);
}
synchronized (serialFilterLock) {
if (serialFilter != null) {
throw new IllegalStateException("Serial filter can only be set once");
}
serialFilter = filter;
}
}
/**
* Returns the JVM-wide deserialization filter factory.
* If the filter factory has been {@linkplain #setSerialFilterFactory(BinaryOperator) set} it is returned,
* otherwise, a builtin deserialization filter factory is returned.
* The filter factory provides a filter for every ObjectInputStream when invoked from
* {@linkplain ObjectInputStream#ObjectInputStream(InputStream) ObjectInputStream constructors}
* and when a stream-specific filter is set with
* {@link ObjectInputStream#setObjectInputFilter(ObjectInputFilter) setObjectInputFilter}.
*
* @implSpec
* The builtin deserialization filter factory provides the
* {@linkplain #getSerialFilter static JVM-wide filter} when invoked from
* {@linkplain ObjectInputStream#ObjectInputStream(InputStream) ObjectInputStream constructors}.
* When invoked {@link ObjectInputStream#setObjectInputFilter(ObjectInputFilter)
* to set the stream-specific filter} the requested filter replaces the static JVM-wide filter,
* unless it has already been set.
* The builtin deserialization filter factory implements the behavior of earlier versions of
* setting the initial filter in the {@link ObjectInputStream} constructor and
* {@link ObjectInputStream#setObjectInputFilter}.
*
* @return the JVM-wide deserialization filter factory; non-null
* @throws IllegalStateException if the filter factory initialization is incomplete
* @since 17
*/
public static BinaryOperator<ObjectInputFilter> getSerialFilterFactory() {
if (serialFilterFactory == null)
throw new IllegalStateException("Serial filter factory initialization incomplete");
return serialFilterFactory;
}
/**
* Returns the serial filter factory singleton and prevents it from changing
* thereafter.
* This package private method is *only* called by {@link ObjectInputStream#ObjectInputStream()}
* and {@link ObjectInputStream#ObjectInputStream(InputStream)}.
* {@link ObjectInputFilter.Config#setSerialFilterFactory(BinaryOperator)} enforces
* the requirement that the filter factory can not be changed after an ObjectInputStream
* is created.
*
* @return the serial filter factory
* @throws IllegalStateException if the filter factory initialization is incomplete
*/
/* package-private */
static BinaryOperator<ObjectInputFilter> getSerialFilterFactorySingleton() {
filterFactoryNoReplace.set(true);
return getSerialFilterFactory();
}
/**
* Set the {@linkplain #getSerialFilterFactory() JVM-wide deserialization filter factory}.
* The filter factory can be configured exactly once with one of:
* setting the {@code jdk.serialFilterFactory} property on the command line,
* setting the {@code jdk.serialFilterFactory} property in the {@link java.security.Security}
* file, or using this {@code setSerialFilterFactory} method.
* The filter factory can be set only before any {@link ObjectInputStream} has been
* created to avoid any inconsistency in which filter factory is being used.
*
* <p>The JVM-wide filter factory is invoked when an ObjectInputStream
* {@linkplain ObjectInputStream#ObjectInputStream() is constructed} and when the
* {@linkplain ObjectInputStream#setObjectInputFilter(ObjectInputFilter) stream-specific filter is set}.
* The parameters are the current filter and a requested filter and it
* returns the filter to be used for the stream.
* If the current filter is {@code non-null}, the filter factory must return a
* {@code non-null} filter; this is to prevent unintentional disabling of filtering
* after it has been enabled.
* The factory determines the filter to be used for {@code ObjectInputStream} streams based
* on its inputs, any other filters, context, or state that is available.
* The factory may throw runtime exceptions to signal incorrect use or invalid parameters.
* See the {@linkplain ObjectInputFilter filter models} for examples of composition and delegation.
*
* @param filterFactory the deserialization filter factory to set as the
* JVM-wide filter factory; not null
* @throws IllegalStateException if the builtin deserialization filter factory
* has already been replaced or any instance of {@link ObjectInputStream}
* has been created.
* @throws SecurityException if there is security manager and the
* {@code SerializablePermission("serialFilter")} is not granted
* @since 17
*/
public static void setSerialFilterFactory(BinaryOperator<ObjectInputFilter> filterFactory) {
Objects.requireNonNull(filterFactory, "filterFactory");
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(ObjectStreamConstants.SERIAL_FILTER_PERMISSION);
}
if (filterFactoryNoReplace.getAndSet(true)) {
final String msg = serialFilterFactory != null
? serialFilterFactory.getClass().getName()
: "initialization incomplete";
throw new IllegalStateException("Cannot replace filter factory: " + msg);
}
configLog.log(DEBUG,
"Setting deserialization filter factory to {0}", filterFactory.getClass().getName());
serialFilterFactory = filterFactory;
}
/**
* Returns an ObjectInputFilter from a string of patterns.
* <p>
* Patterns are separated by ";" (semicolon). Whitespace is significant and
* is considered part of the pattern.
* If a pattern includes an equals assignment, "{@code =}" it sets a limit.
* If a limit appears more than once the last value is used.
* <ul>
* <li>maxdepth={@code value} - the maximum depth of a graph</li>
* <li>maxrefs={@code value} - the maximum number of internal references</li>
* <li>maxbytes={@code value} - the maximum number of bytes in the input stream</li>
* <li>maxarray={@code value} - the maximum array length allowed</li>
* </ul>
* <p>
* Other patterns match or reject class or package name
* as returned from {@link Class#getName() Class.getName()} and
* if an optional module name is present
* {@link Module#getName() class.getModule().getName()}.
* Note that for arrays the element type is used in the pattern,
* not the array type.
* <ul>
* <li>If the pattern starts with "!", the class is rejected if the remaining pattern is matched;
* otherwise the class is allowed if the pattern matches.
* <li>If the pattern contains "/", the non-empty prefix up to the "/" is the module name;
* if the module name matches the module name of the class then
* the remaining pattern is matched with the class name.
* If there is no "/", the module name is not compared.
* <li>If the pattern ends with ".**" it matches any class in the package and all subpackages.
* <li>If the pattern ends with ".*" it matches any class in the package.
* <li>If the pattern ends with "*", it matches any class with the pattern as a prefix.
* <li>If the pattern is equal to the class name, it matches.
* <li>Otherwise, the pattern is not matched.
* </ul>
* <p>
* The resulting filter performs the limit checks and then
* tries to match the class, if any. If any of the limits are exceeded,
* the filter returns {@link Status#REJECTED Status.REJECTED}.
* If the class is an array type, the class to be matched is the element type.
* Arrays of any number of dimensions are treated the same as the element type.
* For example, a pattern of "{@code !example.Foo}",
* rejects creation of any instance or array of {@code example.Foo}.
* The first pattern that matches, working from left to right, determines
* the {@link Status#ALLOWED Status.ALLOWED}
* or {@link Status#REJECTED Status.REJECTED} result.
* If the limits are not exceeded and no pattern matches the class,
* the result is {@link Status#UNDECIDED Status.UNDECIDED}.
*
* @param pattern the pattern string to parse; not null
* @return a filter to check a class being deserialized;
* {@code null} if no patterns
* @throws IllegalArgumentException if the pattern string is illegal or
* malformed and cannot be parsed.
* In particular, if any of the following is true:
* <ul>
* <li> if a limit is missing the name or the name is not one of
* "maxdepth", "maxrefs", "maxbytes", or "maxarray"
* <li> if the value of the limit can not be parsed by
* {@link Long#parseLong Long.parseLong} or is negative
* <li> if the pattern contains "/" and the module name is missing
* or the remaining pattern is empty
* <li> if the package is missing for ".*" and ".**"
* </ul>
*/
public static ObjectInputFilter createFilter(String pattern) {
Objects.requireNonNull(pattern, "pattern");
return Global.createFilter(pattern, true);
}
/**
* Returns an ObjectInputFilter from a string of patterns that
* checks only the length for arrays, not the component type.
*
* @param pattern the pattern string to parse; not null
* @return a filter to check a class being deserialized;
* {@code null} if no patterns
*/
static ObjectInputFilter createFilter2(String pattern) {
Objects.requireNonNull(pattern, "pattern");
return Global.createFilter(pattern, false);
}
/**
* Implementation of ObjectInputFilter that performs the checks of
* the JVM-wide deserialization filter. If configured, it will be
* used for all ObjectInputStreams that do not set their own filters.
*
*/
final static class Global implements ObjectInputFilter {
/**
* The pattern used to create the filter.
*/
private final String pattern;
/**
* The list of class filters.
*/
private final List<Function<Class<?>, Status>> filters;
/**
* Maximum allowed bytes in the stream.
*/
private long maxStreamBytes;
/**
* Maximum depth of the graph allowed.
*/
private long maxDepth;
/**
* Maximum number of references in a graph.
*/
private long maxReferences;
/**
* Maximum length of any array.
*/
private long maxArrayLength;
/**
* True to check the component type for arrays.
*/
private final boolean checkComponentType;
/**
* Returns an ObjectInputFilter from a string of patterns.
*
* @param pattern the pattern string to parse
* @param checkComponentType true if the filter should check
* the component type of arrays
* @return a filter to check a class being deserialized;
* {@code null} if no patterns
* @throws IllegalArgumentException if the parameter is malformed
* if the pattern is missing the name, the long value
* is not a number or is negative.
*/
static ObjectInputFilter createFilter(String pattern, boolean checkComponentType) {
try {
return new Global(pattern, checkComponentType);
} catch (UnsupportedOperationException uoe) {
// no non-empty patterns
return null;
}
}
/**
* Construct a new filter from the pattern String.
*
* @param pattern a pattern string of filters
* @param checkComponentType true if the filter should check
* the component type of arrays
* @throws IllegalArgumentException if the pattern is malformed
* @throws UnsupportedOperationException if there are no non-empty patterns
*/
private Global(String pattern, boolean checkComponentType) {
boolean hasLimits = false;
this.pattern = pattern;
this.checkComponentType = checkComponentType;
maxArrayLength = Long.MAX_VALUE; // Default values are unlimited
maxDepth = Long.MAX_VALUE;
maxReferences = Long.MAX_VALUE;
maxStreamBytes = Long.MAX_VALUE;
String[] patterns = pattern.split(";");
filters = new ArrayList<>(patterns.length);
for (int i = 0; i < patterns.length; i++) {
String p = patterns[i];
int nameLen = p.length();
if (nameLen == 0) {
continue;
}
if (parseLimit(p)) {
// If the pattern contained a limit setting, i.e. type=value
hasLimits = true;
continue;
}
boolean negate = p.charAt(0) == '!';
int poffset = negate ? 1 : 0;
// isolate module name, if any
int slash = p.indexOf('/', poffset);
if (slash == poffset) {
throw new IllegalArgumentException("module name is missing in: \"" + pattern + "\"");
}
final String moduleName = (slash >= 0) ? p.substring(poffset, slash) : null;
poffset = (slash >= 0) ? slash + 1 : poffset;