Permission problems when trying to execute simple system command

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sayeo87
    New Member
    • Dec 2008
    • 5

    Permission problems when trying to execute simple system command

    Hi, I am quite new to JSP so please forgive me if I ask really simple things... I am trying to run system commands on the server and display the output on a webpage. This is what I've got:


    Code:
    <%@ page import="java.io.*" %>
    <HTML>
    <BODY>
    <%
        Runtime rt = Runtime.getRuntime();
    
    Process p = rt.exec("/bin/ls");
    
    // for reading the output of the program
    // (out of the program is in for us)
    BufferedReader sOut = new BufferedReader(new
            InputStreamReader(p.getInputStream()));
    
    // read the output
    String line;
    while ((line = sOut.readLine()) != null)
    {
            out.println("line<BR/>");
    }
    
    %>
    </BODY>
    </HTML>

    However, when I run this, I get the following:



    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    org.apache.jasp er.JasperExcept ion: Exception in JSP: /research/test.jsp:7

    4: <%
    5: Runtime rt = Runtime.getRunt ime();
    6:
    7: Process p = rt.exec("/bin/ls");
    8:
    9: // for reading the output of the program
    10: // (out of the program is in for us)


    Stacktrace:
    org.apache.jasp er.servlet.JspS ervletWrapper.h andleJspExcepti on(JspServletWr apper.java:451)
    org.apache.jasp er.servlet.JspS ervletWrapper.s ervice(JspServl etWrapper.java: 373)
    org.apache.jasp er.servlet.JspS ervlet.serviceJ spFile(JspServl et.java:329)
    org.apache.jasp er.servlet.JspS ervlet.service( JspServlet.java :265)
    javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:802)
    sun.reflect.Gen eratedMethodAcc essor63.invoke( Unknown Source)
    sun.reflect.Del egatingMethodAc cessorImpl.invo ke(DelegatingMe thodAccessorImp l.java:25)
    java.lang.refle ct.Method.invok e(Method.java:5 97)
    org.apache.cata lina.security.S ecurityUtil$1.r un(SecurityUtil .java:244)
    java.security.A ccessController .doPrivileged(N ative Method)
    javax.security. auth.Subject.do AsPrivileged(Su bject.java:517)
    org.apache.cata lina.security.S ecurityUtil.exe cute(SecurityUt il.java:276)
    org.apache.cata lina.security.S ecurityUtil.doA sPrivilege(Secu rityUtil.java:1 62)

    root cause

    java.security.A ccessControlExc eption: access denied (java.io.FilePe rmission /bin/ls execute)
    java.security.A ccessControlCon text.checkPermi ssion(AccessCon trolContext.jav a:323)
    java.security.A ccessController .checkPermissio n(AccessControl ler.java:546)
    java.lang.Secur ityManager.chec kPermission(Sec urityManager.ja va:532)
    java.lang.Secur ityManager.chec kExec(SecurityM anager.java:779 )
    java.lang.Proce ssBuilder.start (ProcessBuilder .java:447)
    java.lang.Runti me.exec(Runtime .java:593)
    java.lang.Runti me.exec(Runtime .java:431)
    java.lang.Runti me.exec(Runtime .java:328)
    org.apache.jsp. research.test_j sp._jspService( test_jsp.java:4 9)
    org.apache.jasp er.runtime.Http JspBase.service (HttpJspBase.ja va:98)
    javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:802)
    org.apache.jasp er.servlet.JspS ervletWrapper.s ervice(JspServl etWrapper.java: 331)
    org.apache.jasp er.servlet.JspS ervlet.serviceJ spFile(JspServl et.java:329)
    org.apache.jasp er.servlet.JspS ervlet.service( JspServlet.java :265)
    javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:802)
    sun.reflect.Gen eratedMethodAcc essor63.invoke( Unknown Source)
    sun.reflect.Del egatingMethodAc cessorImpl.invo ke(DelegatingMe thodAccessorImp l.java:25)
    java.lang.refle ct.Method.invok e(Method.java:5 97)
    org.apache.cata lina.security.S ecurityUtil$1.r un(SecurityUtil .java:244)
    java.security.A ccessController .doPrivileged(N ative Method)
    javax.security. auth.Subject.do AsPrivileged(Su bject.java:517)
    org.apache.cata lina.security.S ecurityUtil.exe cute(SecurityUt il.java:276)
    org.apache.cata lina.security.S ecurityUtil.doA sPrivilege(Secu rityUtil.java:1 62)





    Seems like it has something to do with the permissions granted to the JVM? I have googled high and low and still haven't found a way to solve it. I've also tried adding
    Code:
    grant { permission java.io.FilePermission "file:/bin/ls", "execute"; }
    to my java.policy file but to no avail. Could somebody point out how to solve this? Thanks!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    If you were allowed to do what you want to do you can, say, execute the command "rm -rf /". You don't want to be able to do that and that's why the installed SecurityManager forbids you to do that. Be grateful.

    kind regards,

    Jos

    Comment

    • sayeo87
      New Member
      • Dec 2008
      • 5

      #3
      Thanks for your reply. That sounds very logical but I'm sure its still something that many people wish there was a way to do. OK, eventually my goal is to be able to execute a Perl script and grab its output. Is there any way I can make the security manager grant access to a particular script etc?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by sayeo87
        Thanks for your reply. That sounds very logical but I'm sure its still something that many people wish there was a way to do. OK, eventually my goal is to be able to execute a Perl script and grab its output. Is there any way I can make the security manager grant access to a particular script etc?
        You have to (re)configure your (web)server and make it install another SecurityManager (or no manager at all) if possible. The manager itself is a breeze:

        Code:
        import java.security.Permission;
        
        public class MySecurityManager extends SecurityManager {
        	
        	public MySecurityManager() { }
        	
        	/* allow everything: */
        	public void checkPermission(Permission perm) { }
        	public void checkPermission(Permission perm, Object context) { }
        }
        kind regards,

        Jos

        Comment

        • sayeo87
          New Member
          • Dec 2008
          • 5

          #5
          OK, I'm using a Tomcat webserver and the security manager was not turned on. What else could be blocking access then?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Well, something forbid you to do your ugly deed (check if there's a SecurityManager installed; I bet there is). What does your catalina.policy file say?

            kind regards,

            Jos

            Comment

            Working...