SecurityManager checkLink(String) in Java

checkLink(String): This method is available in the java.lang.SecurityManager class of Java.

Syntax:

void java.lang.SecurityManager.checkLink(String lib)

This method takes one argument. This method throws a SecurityException if the calling thread is not allowed to dynamic link the library code specified by the string argument file.

The argument is either a simple library name or a complete filename.

This method is invoked for the current security manager by methods load and loadLibrary of class Runtime.

Parameters: One parameter is required for this method.

lib: the name of the library.

Returns: NA

Throws:

1. SecurityException - if the calling thread does not have permission to dynamically link the library.

2. NullPointerException - if the lib argument is null.

Approach 1: SecurityException 

Java

package com.SecurityManager;

public class SecurityManagercheckLink {
    public static void main(String[] args) {

        SecurityManager securityManager = new SecurityManager();

        String lib = "C:\\Windows\\System32\\crypt32.dl";
        securityManager.checkLink(lib);
        System.out.println("Successfully check the access");
    }
}

Output:

Exception in thread "main" java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "loadLibrary.C:\Windows\System32\crypt32.dl") at java.base/java.security.AccessControlContext.checkPermission(AccessControlContext.java:472) at java.base/java.security.AccessController.checkPermission(AccessController.java:1036) at java.base/java.lang.SecurityManager.checkPermission(SecurityManager.java:408) at java.base/java.lang.SecurityManager.checkLink(SecurityManager.java:694) at com.SecurityManager.SecurityManagercheckLink.main(SecurityManagercheckLink.java:9)


Approach 2: NullPointerException

Java

package com.SecurityManager;

public class SecurityManagercheckLink {
    public static void main(String[] args) {

        SecurityManager securityManager = new SecurityManager();

        String lib = null;
        securityManager.checkLink(lib);
        System.out.println("Successfully check the access");
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: library can't be null at java.base/java.lang.SecurityManager.checkLink(SecurityManager.java:692) at com.SecurityManager.SecurityManagercheckLink.main(SecurityManagercheckLink.java:9)


Some other methods of SecurityManager

SecurityManager()This method constructs a new SecurityManager.

checkAccept(String, int)This method throws a SecurityException if the calling thread is not permitted to accept a socket connection from the specified host and port number.

checkAccess(Thread)This method throws a SecurityException if the calling thread is not allowed to modify the thread argument.

checkAccess(ThreadGroup)This method throws a SecurityException if the calling thread is not allowed to modify the thread group argument.

checkConnect(String, int)This method throws a SecurityException if the calling thread is not allowed to open a socket connection to the specified host and port number.

checkConnect(String, int, Object)This method throws a SecurityException if the specified security context is not allowed to open a socket connection to the specified host and port number.

checkCreateClassLoader()This method throws a SecurityException if the calling thread is not allowed to create a new class loader.

checkDelete(String)This method throws a SecurityException if the calling thread is not allowed to delete the specified file.

checkExec(String)This method throws a SecurityException if the calling thread is not allowed to create a subprocess.

checkExit(int)This method throws a SecurityException if the calling thread is not allowed to cause the Java Virtual Machine to halt with the specified status code.

checkLink(String)This method throws a SecurityException if the calling thread is not allowed to dynamic link the library code specified by the string argument file.

checkListen(int)This method throws a SecurityException if the calling thread is not allowed to wait for a connection request on the specified local port number.

checkMulticast(InetAddress)This method throws a SecurityException if the calling thread is not allowed to use(join/leave/send/receive) IP multicast.

checkPackageAccess(String)This method throws a SecurityException if the calling thread is not allowed to access the specified package.

checkPackageDefinition(String)This method throws a SecurityException if the calling thread is not allowed to define classes in the specified package.

checkPermission(Permission)This method throws a SecurityException if the requested access, specified by the given permission, is not permitted based on the security policy currently in effect.

checkPermission(Permission, Object)This method throws a SecurityException if the specified security context is denied access to the resource specified by the given permission.

checkPrintJobAccess()This method throws a SecurityException if the calling thread is not allowed to initiate a print job request.

checkPropertiesAccess()This method throws a SecurityException if the calling thread is not allowed to access or modify the system properties.

checkPropertyAccess(String)This method throws a SecurityException if the calling thread is not allowed to access the system property with the specified key name.

checkRead(FileDescriptor)This method throws a SecurityException if the calling thread is not allowed to read from the specified file descriptor.

checkRead(String)This method throws a SecurityException if the calling thread is not allowed to read the file specified by the string argument.

checkRead(String, Object)This method throws a SecurityException if the specified security context is not allowed to read the file specified by the string argument.

checkSecurityAccess(String)This method determines whether permission with the specified target name should be granted or denied.

checkSetFactory()This method throws a SecurityException if the calling thread is not allowed to set the socket factory used by ServerSocket or Socket or the stream handler factory used by URL.

checkWrite(FileDescriptor) This method throws a SecurityException if the calling thread is not allowed to write to the specified file descriptor.

checkWrite(String)This method throws a SecurityException if the calling thread is not allowed to write to the file specified by the string argument.

getSecurityContext()This method creates an object that encapsulates the current execution environment.

getThreadGroup()This method returns the thread group into which to instantiate any new thread being created at the time this is being called.

No comments:

Post a Comment