# Re: About doPrivileged() method.

Jan Luehe (Jan.Luehe@eng.sun.com)
Fri, 19 Mar 1999 10:51:46 -0800 (PST)

- Messages sorted by: [ date ][ thread ][ subject ][ author ]
- Next message: ana: "Please, I need help !!!!!!!"
- Previous message: Chosho Kyo - Nihon Sun JCTC: "About doPrivileged() method."
- Maybe in reply to: Chosho Kyo - Nihon Sun JCTC: "About doPrivileged() method."

    Message-Id: <199903191851.KAA29232@laguna.eng.sun.com>

    Date: Fri, 19 Mar 1999 10:51:46 -0800 (PST)

    From: Jan Luehe <Jan.Luehe@eng.sun.com>

    Subject: Re: About doPrivileged() method.

    To: java-security@java.sun.com, Chosho.Kyo@japan.sun.com

Chosho:

If you look at the javadocs for
java.security.AccessController, you'll see the
following comment:

* AccessController.doPrivileged(new PrivilegedAction() {
* public Object run() {
* // Code goes here. Any permission checks from this
* // point forward require both the current context and
* // the snapshot's context to have the desired permission.
* }
* }, acc);
In your example, there are 2 domains on the stack when the permission
check is done:

The caller's domain (which has the directory where you ran
the GetProperty program as its codebase and the default permissions
assigned to that codebase), and the snapshot's domain
(the protection domain that you constructed, i.e.,
the domain with a "null" codebase and AllPermission.)

The caller's domain does not have permission to read the
"user.home" property, hence the permission check fails
(even though the snapshot's domain has the required permission).

It is important to understand that the doPrivileged method
that takes an AccessControlContext also requires the current
context to have the requested permission, not just the
context that you passed to it.

Jan

> Date: Fri, 19 Mar 1999 16:30:55 +0900 (JST)
> From: Chosho Kyo - Nihon Sun JCTC <Chosho.Kyo@japan.sun.com>
> Subject: About doPrivileged() method.
> To: java-security@java.sun.com
> MIME-Version: 1.0
> Content-MD5: 1in8WVu3laevIYtVcwl49Q==
>
> Hi,
>
> I wrote a source GetProperty.java to test doPrivileged() method
> according to the explanation of access control mechanisms on the web.
>
> But when I run it with the following command,
> java -Djava.security.manager GetProperty
>
> I got the exception message like that:
> "Exception in thread "main" java.security.AccessControlException:
> access denied (java.util.PropertyPermission user.home read)
> ........".
>
> In my understanding, GetProperty.java needs not the .java.policy file
> to grant {java.util.PropertyPermission "user.home", "read"}.
>
> Is my understanding wrong?
> Could you please give me any hint or tell me how to get
> a completed source sample which uses the doPrivileged() method?
>
> Regards.
>
> //--------------GetProperty.java start--------------------
> import java.security.*;
> import java.net.*;
> import java.io.*;
> import java.util.*;
>
> public class GetProperty {
>
> AccessControlContext getAccessControlContext(){
> PermissionCollection perms = new Permissions();
> perms.add(new AllPermission());
> ProtectionDomain domain = new ProtectionDomain(new CodeSource(null,
> null), perms);
> AccessControlContext acc = new AccessControlContext(new
> ProtectionDomain[] {domain});
> return acc;
> }
>
>
> void go(){
> AccessControlContext acc = getAccessControlContext();
> System.out.println(acc.toString());
> AccessController.doPrivileged( new PrivilegedAction(){
> public Object run(){
> System.out.println(System.getProperty("user.home"));
> return null;
> }
> }, acc);
> }
>
> public static void main(String a[]) throws Throwable{
> new GetProperty().go();
> }
> }
>
> //--------------GetProperty.java End--------------------
>
> Chosho Kyo
> Nihon Sun Microsystems K.K.
> SBS Tower, 4-10-1 Yoga, Setagaya-ku
> Tokyo, 158 Japan
> Tel:03-5717-5041
> Fax:03-5717-2582
> chosho.kyo@Japan.Sun.COM
>

- Next message: ana: "Please, I need help !!!!!!!"
- Previous message: Chosho Kyo - Nihon Sun JCTC: "About doPrivileged() method."
- Maybe in reply to: Chosho Kyo - Nihon Sun JCTC: "About doPrivileged() method."