summaryrefslogtreecommitdiffstats
path: root/luni/src/main/java/java/lang/SecurityManager.java
blob: dc624e37cffee380282e254f229c8b165d437ed1 (plain)
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
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package java.lang;

// BEGIN android-added
import dalvik.system.VMStack;
// END android-added

import java.io.File;
import java.io.FileDescriptor;
import java.io.FilePermission;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.net.InetAddress;
import java.net.SocketPermission;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.AllPermission;
import java.security.Permission;
import java.security.Security;
import java.security.SecurityPermission;
import java.util.PropertyPermission;
import java.util.StringTokenizer;

import org.apache.harmony.luni.util.PriviAction;

/**
 * Provides security verification facilities for applications. {@code
 * SecurityManager} contains a set of {@code checkXXX} methods which determine
 * if it is safe to perform a specific operation such as establishing network
 * connections, modifying files, and many more. In general, these methods simply
 * return if they allow the application to perform the operation; if an
 * operation is not allowed, then they throw a {@link SecurityException}. The
 * only exception is {@link #checkTopLevelWindow(Object)}, which returns a
 * boolean to indicate permission.
 * 
 * @since Android 1.0
 */
public class SecurityManager {

    private static final PropertyPermission READ_WRITE_ALL_PROPERTIES_PERMISSION = new PropertyPermission(
            "*", "read,write"); //$NON-NLS-1$ //$NON-NLS-2$

    private static final String PKG_ACC_KEY = "package.access"; //$NON-NLS-1$

    private static final String PKG_DEF_KEY = "package.definition"; //$NON-NLS-1$

    /**
     * Flag to indicate whether a security check is in progress.
     * 
     * @deprecated Use {@link #checkPermission}
     * @since Android 1.0
     */
    @Deprecated
    protected boolean inCheck;

    /**
     * Constructs a new {@code SecurityManager} instance.
     * <p>
     * The {@code RuntimePermission("createSecurityManager")} is checked if a
     * security manager is installed.
     * </p>
     * 
     * @since Android 1.0
     */
    public SecurityManager() {
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security
                    .checkPermission(RuntimePermission.permissionToCreateSecurityManager);
        }
        Class<?> type = Security.class; // initialize Security properties
        if (type == null) {
            throw new AssertionError();
        }
    }

    /**
     * Checks whether the calling thread is allowed to accept socket
     * connections.
     * 
     * @param host
     *            the address of the host that attempts to connect.
     * @param port
     *            the port number to check.
     * @throws NullPointerException
     *             if {@code host} is {@code null}.
     * @throws SecurityException
     *             if the calling thread is not allowed to accept socket
     *             connections from {@code host} through {@code port}.
     * @since Android 1.0
     */
    public void checkAccept(String host, int port) {
        if (host == null) {
            throw new NullPointerException();
        }
        checkPermission(new SocketPermission(host + ':' + port, "accept")); //$NON-NLS-1$
    }

    /**
     * Checks whether the calling thread is allowed to modify the specified
     * thread.
     * 
     * @param thread
     *            the thread to access.
     * @throws SecurityException
     *             if the calling thread is not allowed to access {@code thread}.
     * @since Android 1.0
     */
    public void checkAccess(Thread thread) {
        // Only worry about system threads. Dead threads have a null group.
        ThreadGroup group = thread.getThreadGroup();
        if ((group != null) && (group.parent == null)) {
            checkPermission(RuntimePermission.permissionToModifyThread);
        }
    }

    /**
     * Checks whether the calling thread is allowed to modify the specified
     * thread group.
     * 
     * @param group
     *            the thread group to access.
     * @throws NullPointerException
     *             if {@code group} is {@code null}.
     * @throws SecurityException
     *             if the calling thread is not allowed to access {@code group}.
     * @since Android 1.0
     */
    public void checkAccess(ThreadGroup group) {
        // Only worry about system threads.
        if (group == null) {
            throw new NullPointerException();
        }
        if (group.parent == null) {
            checkPermission(RuntimePermission.permissionToModifyThreadGroup);
        }
    }

    /**
     * Checks whether the calling thread is allowed to establish socket
     * connections. A -1 port indicates the caller is trying to resolve the
     * hostname.
     * 
     * @param host
     *            the address of the host to connect to.
     * @param port
     *            the port number to check, or -1 for resolve.
     * @throws NullPointerException
     *             if {@code host} is {@code null}.
     * @throws SecurityException
     *             if the calling thread is not allowed to connect to {@code
     *             host} through {@code port}.
     * @since Android 1.0
     */
    public void checkConnect(String host, int port) {
        if (host == null) {
            throw new NullPointerException();
        }
        if (port > 0) {
            checkPermission(new SocketPermission(host + ':' + port, "connect")); //$NON-NLS-1$
        } else {
            checkPermission(new SocketPermission(host, "resolve")); //$NON-NLS-1$
        }
    }

    /**
     * Checks whether the specified security context is allowed to establish
     * socket connections. A -1 port indicates the caller is trying to resolve
     * the hostname.
     * 
     * @param host
     *            the address of the host to connect to.
     * @param port
     *            the port number to check, or -1 for resolve.
     * @param context
     *            the security context to use for the check.
     * @throws NullPointerException
     *             if {@code host} is {@code null}.
     * @throws SecurityException
     *             if {@code context} is not allowed to connect to {@code host}
     *             through {@code port}.
     * @since Android 1.0
     */
    public void checkConnect(String host, int port, Object context) {
        // BEGIN android-added
        if (host == null) {
            throw new NullPointerException();
        }
        // END android-added
        if (port > 0) {
            checkPermission(new SocketPermission(host + ':' + port, "connect"), //$NON-NLS-1$
                    context);
        } else {
            checkPermission(new SocketPermission(host, "resolve"), context); //$NON-NLS-1$
        }
    }

    /**
     * Checks whether the calling thread is allowed to create a class loader.
     * 
     * @throws SecurityException
     *             if the calling thread is not allowed to create a class
     *             loader.
     * @since Android 1.0
     */
    public void checkCreateClassLoader() {
        checkPermission(RuntimePermission.permissionToCreateClassLoader);
    }

    /**
     * Checks whether the calling thread is allowed to delete the file with the
     * specified name, which should be passed in canonical form.
     * 
     * @param file
     *            the name of the file to delete.
     * @throws SecurityException
     *             if the calling thread is not allowed to delete {@code file}.
     * @since Android 1.0
     */
    public void checkDelete(String file) {
        checkPermission(new FilePermission(file, "delete")); //$NON-NLS-1$
    }

    /**
     * Checks whether the calling thread is allowed to execute the specified
     * platform specific command.
     * 
     * @param cmd
     *            the command line to execute.
     * @throws SecurityException
     *             if the calling thread is not allowed to execute {@code cmd}.
     * @since Android 1.0
     */
    public void checkExec(String cmd) {
        checkPermission(new FilePermission(new File(cmd).isAbsolute() ? cmd
                : "<<ALL FILES>>", "execute")); //$NON-NLS-1$ //$NON-NLS-2$
    }

    /**
     * Checks whether the calling thread is allowed to terminate the virtual
     * machine.
     * 
     * @param status
     *            the status that the virtual machine returns when it is
     *            terminated.
     * @throws SecurityException
     *             if the calling thread is not allowed to terminate the virtual
     *             machine with {@code status}.
     * @since Android 1.0
     */
    public void checkExit(int status) {
        checkPermission(RuntimePermission.permissionToExitVM);
    }

    /**
     * Checks whether the calling thread is allowed to load the specified native
     * library.
     * 
     * @param libName
     *            the name of the library to load.
     * @throws SecurityException
     *             if the calling thread is not allowed to load {@code libName}.
     * @since Android 1.0
     */
    public void checkLink(String libName) {
        if (libName == null) {
            throw new NullPointerException();
        }
        checkPermission(new RuntimePermission("loadLibrary." + libName)); //$NON-NLS-1$
    }

    /**
     * Checks whether the calling thread is allowed to listen on the specified
     * port.
     * 
     * @param port
     *            the port number to check.
     * @throws SecurityException
     *             if the calling thread is not allowed listen on {@code port}.
     * @since Android 1.0
     */
    public void checkListen(int port) {
        if (port == 0) {
            checkPermission(new SocketPermission("localhost:1024-", "listen")); //$NON-NLS-1$ //$NON-NLS-2$
        } else {
            checkPermission(new SocketPermission("localhost:" + port, "listen")); //$NON-NLS-1$//$NON-NLS-2$
        }
    }

    /**
     * Checks whether the calling thread is allowed to access members. The
     * default is to allow access to public members (that is, {@code
     * java.lang.reflect.Member.PUBLIC}) and to classes loaded by the same
     * loader as the original caller (that is, the method that called the
     * reflect API). Due to the nature of the check, overriding implementations
     * cannot call {@code super.checkMemberAccess()} since the stack would no
     * longer be of the expected shape.
     * 
     * @param cls
     *            the class of which members are accessed.
     * @param type
     *            the access type, either {@code
     *            java.lang.reflect.Member.PUBLIC} or {@code
     *            java.lang.reflect.Member.DECLARED}.
     * @throws SecurityException
     *             if the calling thread is not allowed to access members of
     *             {@code cls}.
     * @since Android 1.0
     */
    public void checkMemberAccess(Class<?> cls, int type) {
        if (cls == null) {
            throw new NullPointerException();
        }
        if (type == Member.PUBLIC) {
            return;
        }
        //
        // Need to compare the classloaders.
        // Stack shape is
        // <user code> <- want this class
        // Class.getDeclared*();
        // Class.checkMemberAccess();
        // SecurityManager.checkMemberAccess(); <- current frame
        //
        // Use getClassLoaderImpl() since getClassLoader()
        // returns null for the bootstrap class loader.
        if (ClassLoader.getStackClassLoader(3) == cls.getClassLoaderImpl()) {
            return;
        }

        // Forward off to the permission mechanism.
        checkPermission(new RuntimePermission("accessDeclaredMembers")); //$NON-NLS-1$
    }

    /**
     * Checks whether the calling thread is allowed to use the specified IP
     * multicast group address.
     * 
     * @param maddr
     *            the internet group address to use.
     * @throws SecurityException
     *             if the calling thread is not allowed to use {@code maddr}.
     * @since Android 1.0
     */
    public void checkMulticast(InetAddress maddr) {
        checkPermission(new SocketPermission(maddr.getHostAddress(),
                "accept,connect")); //$NON-NLS-1$
    }

    /**
     * Checks whether the calling thread is allowed to use the specified IP
     * multicast group address.
     * 
     * @param maddr
     *            the internet group address to use.
     * @param ttl
     *            the value in use for multicast send. This parameter is
     *            ignored.
     * @throws SecurityException
     *             if the calling thread is not allowed to use {@code maddr}.
     * @deprecated use {@link #checkMulticast(java.net.InetAddress)}
     * @since Android 1.0
     */
    @Deprecated
    public void checkMulticast(InetAddress maddr, byte ttl) {
        checkPermission(new SocketPermission(maddr.getHostAddress(),
                "accept,connect")); //$NON-NLS-1$
    }

    /**
     * Checks whether the calling thread is allowed to access the specified
     * package.
     * 
     * @param packageName
     *            the name of the package to access.
     * @throws SecurityException
     *             if the calling thread is not allowed to access {@code
     *             packageName}.
     * @since Android 1.0
     */
    public void checkPackageAccess(String packageName) {
        if (packageName == null) {
            throw new NullPointerException();
        }
        if (checkPackageProperty(PKG_ACC_KEY, packageName)) {
            checkPermission(new RuntimePermission("accessClassInPackage." //$NON-NLS-1$
                    + packageName));
        }
    }

    /**
     * Checks whether the calling thread is allowed to define new classes in the
     * specified package.
     * 
     * @param packageName
     *            the name of the package to add a class to.
     * @throws SecurityException
     *             if the calling thread is not allowed to add classes to
     *             {@code packageName}.
     * @since Android 1.0
     */
    public void checkPackageDefinition(String packageName) {
        if (packageName == null) {
            throw new NullPointerException();
        }
        if (checkPackageProperty(PKG_DEF_KEY, packageName)) {
            checkPermission(new RuntimePermission("defineClassInPackage." //$NON-NLS-1$
                    + packageName));
        }
    }

    /**
     * Returns true if the package name is restricted by the specified security
     * property.
     */
    private static boolean checkPackageProperty(final String property,
            final String pkg) {
        String list = AccessController.doPrivileged(PriviAction
                .getSecurityProperty(property));
        if (list != null) {
            int plen = pkg.length();
            StringTokenizer tokenizer = new StringTokenizer(list, ", "); //$NON-NLS-1$
            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken();
                int tlen = token.length();
                if (plen > tlen
                        && pkg.startsWith(token)
                        && (token.charAt(tlen - 1) == '.' || pkg.charAt(tlen) == '.')) {
                    return true;
                } else if (plen == tlen && token.startsWith(pkg)) {
                    return true;
                } else if (plen + 1 == tlen && token.startsWith(pkg)
                        && token.charAt(tlen - 1) == '.') {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * Checks whether the calling thread is allowed to access the system
     * properties.
     * 
     * @throws SecurityException
     *             if the calling thread is not allowed to access system
     *             properties.
     * @since Android 1.0
     */
    public void checkPropertiesAccess() {
        checkPermission(READ_WRITE_ALL_PROPERTIES_PERMISSION);
    }

    /**
     * Checks whether the calling thread is allowed to access a particular
     * system property.
     * 
     * @param key
     *            the name of the property to access.
     * @throws SecurityException
     *             if the calling thread is not allowed to access the {@code
     *             key} system property.
     * @since Android 1.0
     */
    public void checkPropertyAccess(String key) {
        checkPermission(new PropertyPermission(key, "read")); //$NON-NLS-1$
    }

    /**
     * Checks whether the calling thread is allowed to read from the file with
     * the specified file descriptor.
     * 
     * @param fd
     *            the file descriptor of the file to read from.
     * @throws SecurityException
     *             if the calling thread is not allowed to read from {@code fd}.
     * @since Android 1.0
     */
    public void checkRead(FileDescriptor fd) {
        if (fd == null) {
            throw new NullPointerException();
        }
        checkPermission(RuntimePermission.permissionToReadFileDescriptor);
    }

    /**
     * Checks whether the calling thread is allowed to read from the file with
     * the specified name, which should be passed in canonical form.
     * 
     * @param file
     *            the name of the file or directory to read from.
     * @throws SecurityException
     *             if the calling thread is not allowed to read from {@code
     *             file}.
     * @since Android 1.0
     */
    public void checkRead(String file) {
        checkPermission(new FilePermission(file, "read")); //$NON-NLS-1$
    }

    /**
     * Checks whether the given security context is allowed to read from the
     * file named by the argument, which should be passed in canonical form.
     * 
     * @param file
     *            the name of the file or directory to check.
     * @param context
     *            the security context to use for the check.
     * @throws SecurityException
     *             if {@code context} is not allowed to read from {@code file}.
     * @since Android 1.0
     */
    public void checkRead(String file, Object context) {
        checkPermission(new FilePermission(file, "read"), context); //$NON-NLS-1$
    }

    /**
     * Checks whether the calling thread is allowed to perform the security
     * operation named by the target.
     * 
     * @param target
     *            the name of the operation to perform.
     * @throws SecurityException
     *             if the calling thread is not allowed to perform
     *             {@code target}.
     * @since Android 1.0
     */
    public void checkSecurityAccess(String target) {
        checkPermission(new SecurityPermission(target));
    }

    /**
     * Checks whether the calling thread is allowed to set the net object
     * factories.
     * 
     * @throws SecurityException
     *             if the calling thread is not allowed to set the net object
     *             factories.
     * @since Android 1.0
     */
    public void checkSetFactory() {
        checkPermission(RuntimePermission.permissionToSetFactory);
    }

    /**
     * Checks whether the calling thread is trusted to show the specified top
     * level window.
     * 
     * @param window
     *            the window to show.
     * @return {@code true} if the calling thread is allowed to show {@code
     *         window}; {@code false} otherwise.
     * @throws NullPointerException
     *             if {@code window} is {@code null}.
     * @since Android 1.0
     */
    public boolean checkTopLevelWindow(Object window) {
        if (window == null) {
            throw new NullPointerException();
        }
        try {
            Class<?> awtPermission = Class.forName("java.awt.AWTPermission"); //$NON-NLS-1$
            Constructor<?> constructor = awtPermission
                    .getConstructor(String.class);
            Object perm = constructor
                    .newInstance("showWindowWithoutWarningBanner"); //$NON-NLS-1$
            checkPermission((Permission) perm);
        } catch (ClassNotFoundException e) {
        } catch (NoSuchMethodException e) {
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        } catch (SecurityException e) {
            return false;
        }
        return true;
    }

    /**
     * Checks whether the calling thread is allowed to access the system
     * clipboard.
     * 
     * @throws SecurityException
     *             if the calling thread is not allowed to access the system
     *             clipboard.
     * @since Android 1.0
     */
    public void checkSystemClipboardAccess() {
        try {
            Class<?> awtPermission = Class.forName("java.awt.AWTPermission"); //$NON-NLS-1$
            Constructor<?> constructor = awtPermission
                    .getConstructor(String.class);
            Object perm = constructor.newInstance("accessClipboard"); //$NON-NLS-1$
            checkPermission((Permission) perm);
            return;
        } catch (ClassNotFoundException e) {
        } catch (NoSuchMethodException e) {
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        }
        throw new SecurityException();
    }

    /**
     * Checks whether the calling thread is allowed to access the AWT event
     * queue.
     * 
     * @throws SecurityException
     *             if the calling thread is not allowed to access the AWT event
     *             queue.
     * @since Android 1.0
     */
    public void checkAwtEventQueueAccess() {
        try {
            Class<?> awtPermission = Class.forName("java.awt.AWTPermission"); //$NON-NLS-1$
            Constructor<?> constructor = awtPermission
                    .getConstructor(String.class);
            Object perm = constructor.newInstance("accessEventQueue"); //$NON-NLS-1$
            checkPermission((Permission) perm);
            return;
        } catch (ClassNotFoundException e) {
        } catch (NoSuchMethodException e) {
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        }
        throw new SecurityException();
    }

    /**
     * Checks whether the calling thread is allowed to start a new print job.
     * 
     * @throws SecurityException
     *             if the calling thread is not allowed to start a new print
     *             job.
     * @since Android 1.0
     */
    public void checkPrintJobAccess() {
        checkPermission(RuntimePermission.permissionToQueuePrintJob);
    }

    /**
     * Checks whether the calling thread is allowed to write to the file with
     * the specified file descriptor.
     * 
     * @param fd
     *            the file descriptor of the file to write to.
     * @throws SecurityException
     *             if the calling thread is not allowed to write to {@code fd}.
     * @since Android 1.0
     */
    public void checkWrite(FileDescriptor fd) {
        if (fd == null) {
            throw new NullPointerException();
        }
        checkPermission(RuntimePermission.permissionToWriteFileDescriptor);
    }

    /**
     * Checks whether the calling thread is allowed to write to the file with
     * the specified name, which should be passed in canonical form.
     * 
     * @param file
     *            the name of the file or directory to write to.
     * @throws SecurityException
     *             if the calling thread is not allowed to write to 
     *             {@code file}.
     * @since Android 1.0
     */
    public void checkWrite(String file) {
        checkPermission(new FilePermission(file, "write")); //$NON-NLS-1$
    }

    /**
     * Indicates if this security manager is currently checking something.
     * 
     * @return {@code true} if this security manager is executing a security
     *         check method; {@code false} otherwise.
     * @deprecated Use {@link #checkPermission}.
     * @since Android 1.0
     */
    @Deprecated
    public boolean getInCheck() {
        return inCheck;
    }

    /**
     * Returns an array containing one entry for each method in the current
     * execution stack. Each entry is the {@code java.lang.Class} which
     * represents the class in which the method is defined.
     * 
     * @return all classes in the execution stack.
     * @since Android 1.0
     */
    @SuppressWarnings("unchecked")
    protected Class[] getClassContext() {
        return VMStack.getClasses(-1, false);
    }

    /**
     * Returns the class loader of the first class in the execution stack whose
     * class loader is not a system class loader.
     * 
     * @return the most recent non-system class loader.
     * @deprecated Use {@link #checkPermission}.
     * @since Android 1.0
     */
    @Deprecated
    protected ClassLoader currentClassLoader() {

        /*
         * First, check if AllPermission is allowed. If so, then we are
         * effectively running in an unsafe environment, so just answer null
         * (==> everything is a system class).
         */
        try {
            checkPermission(new AllPermission());
            return null;
        } catch (SecurityException ex) {
        }

        /*
         * Now, check if there are any non-system class loaders in the stack up
         * to the first privileged method (or the end of the stack.
         */
        Class<?>[] classes = Class.getStackClasses(-1, true);
        for (int i = 0; i < classes.length; i++) {
            ClassLoader cl = classes[i].getClassLoaderImpl();
            if (!cl.isSystemClassLoader()) {
                return cl;
            }
        }
        return null;
    }

    /**
     * Returns the index in the call stack of the first class whose class loader
     * is not a system class loader.
     * 
     * @return the frame index of the first method whose class was loaded by a
     *         non-system class loader.
     * @deprecated Use {@link #checkPermission}.
     * @since Android 1.0
     */
    @Deprecated
    protected int classLoaderDepth() {
        /*
         * First, check if AllPermission is allowed. If so, then we are
         * effectively running in an unsafe environment, so just answer -1 (==>
         * everything is a system class).
         */
        try {
            checkPermission(new AllPermission());
            return -1;
        } catch (SecurityException ex) {
        }

        /*
         * Now, check if there are any non-system class loaders in the stack up
         * to the first privileged method (or the end of the stack.
         */
        Class<?>[] classes = Class.getStackClasses(-1, true);
        for (int i = 0; i < classes.length; i++) {
            ClassLoader cl = classes[i].getClassLoaderImpl();
            if (!cl.isSystemClassLoader()) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Returns the first class in the call stack that was loaded by a class
     * loader which is not a system class loader.
     * 
     * @return the most recent class loaded by a non-system class loader.
     * @deprecated Use {@link #checkPermission}.
     * @since Android 1.0
     */
    @Deprecated
    protected Class<?> currentLoadedClass() {
        /*
         * First, check if AllPermission is allowed. If so, then we are
         * effectively running in an unsafe environment, so just answer null
         * (==> everything is a system class).
         */
        try {
            checkPermission(new AllPermission());
            return null;
        } catch (SecurityException ex) {
        }

        /*
         * Now, check if there are any non-system class loaders in the stack up
         * to the first privileged method (or the end of the stack.
         */
        Class<?>[] classes = Class.getStackClasses(-1, true);
        for (int i = 0; i < classes.length; i++) {
            ClassLoader cl = classes[i].getClassLoaderImpl();
            if (!cl.isSystemClassLoader()) {
                return classes[i];
            }
        }
        return null;
    }

    /**
     * Returns the index in the call stack of the first method which is
     * contained in the class with the specified name. Returns -1 if no methods
     * from this class are in the stack.
     * 
     * @param name
     *            the name of the class to look for.
     * @return the frame index of the first method found is contained in the
     *         class identified by {@code name}.
     * @deprecated Use {@link #checkPermission}.
     * @since Android 1.0
     */
    @Deprecated
    protected int classDepth(String name) {
        Class<?>[] classes = Class.getStackClasses(-1, false);
        for (int i = 0; i < classes.length; i++) {
            if (classes[i].getName().equals(name)) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Indicates whether there is a method in the call stack from the class with
     * the specified name.
     * 
     * @param name
     *            the name of the class to look for.
     * @return {@code true} if a method from the class identified by {@code
     *         name} is executing; {@code false} otherwise.
     * @deprecated Use {@link #checkPermission}.
     * @since Android 1.0
     */
    @Deprecated
    protected boolean inClass(String name) {
        return classDepth(name) != -1;
    }

    /**
     * Indicates whether there is a method in the call stack from a class which
     * was defined by a non-system class loader.
     * 
     * @return {@code true} if a method from a class that was defined by a
     *         non-system class loader is executing; {@code false} otherwise.
     * @deprecated Use {@link #checkPermission}
     * @since Android 1.0
     */
    @Deprecated
    protected boolean inClassLoader() {
        return currentClassLoader() != null;
    }

    /**
     * Returns the thread group which should be used to instantiate new threads.
     * By default, this is the same as the thread group of the thread running
     * this method.
     * 
     * @return ThreadGroup the thread group to create new threads in.
     * @since Android 1.0
     */
    public ThreadGroup getThreadGroup() {
        return Thread.currentThread().getThreadGroup();
    }

    /**
     * Returns an object which encapsulates the security state of the current
     * point in the execution. In the Android reference implementation, this is
     * an {@link java.security.AccessControlContext}.
     * 
     * @return an object that encapsulates information about the current
     *         execution environment.
     * @since Android 1.0
     */
    public Object getSecurityContext() {
        return AccessController.getContext();
    }

    /**
     * Checks whether the calling thread is allowed to access the resource being
     * guarded by the specified permission object.
     * 
     * @param permission
     *            the permission to check.
     * @throws SecurityException
     *             if the requested {@code permission} is denied according to
     *             the current security policy.
     * @since Android 1.0
     */
    public void checkPermission(Permission permission) {
        try {
            inCheck = true;
            AccessController.checkPermission(permission);
        } finally {
            inCheck = false;
        }
    }

    /**
     * Checks whether the specified security context is allowed to access the
     * resource being guarded by the specified permission object.
     * 
     * @param permission
     *            the permission to check.
     * @param context
     *            the security context for which to check permission.
     * @throws SecurityException
     *             if {@code context} is not an instance of {@code
     *             AccessControlContext} or if the requested {@code permission}
     *             is denied for {@code context} according to the current
     *             security policy.
     * @since Android 1.0
     */
    public void checkPermission(Permission permission, Object context) {
        try {
            inCheck = true;
            // Must be an AccessControlContext. If we don't check
            // this, then applications could pass in an arbitrary
            // object which circumvents the security check.
            if (context instanceof AccessControlContext) {
                ((AccessControlContext) context).checkPermission(permission);
            } else {
                throw new SecurityException();
            }
        } finally {
            inCheck = false;
        }
    }
}