summaryrefslogtreecommitdiff
path: root/java/openjdk6/files/icedtea/openjdk/8012453-runtime.exec.patch
blob: 143a0c1f286d175b11dc8269b1ac0f53e2a2b728 (plain) (blame)
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
# HG changeset patch
# User uta
# Date 1383008821 0
#      Tue Oct 29 01:07:01 2013 +0000
# Node ID 20c88fd14959c6a4df2e0f36bd759b56efa6f2cb
# Parent  3e758b40337ef9da5ad030d0ac60ab4407357277
8012453: (process) Runtime.exec(String) fails if command contains spaces [win]
Reviewed-by: alanb

diff -r 3e758b40337e -r 20c88fd14959 src/share/classes/java/lang/ProcessBuilder.java
--- jdk/src/share/classes/java/lang/ProcessBuilder.java	Tue Jul 30 17:20:22 2013 -0400
+++ jdk/src/share/classes/java/lang/ProcessBuilder.java	Tue Oct 29 01:07:01 2013 +0000
@@ -490,6 +490,15 @@
                 + (dir == null ? "" : " (in directory \"" + dir + "\")")
                 + exceptionInfo,
                 cause);
+        } catch (IllegalArgumentException e) {
+            String exceptionInfo = ": " + e.getMessage();
+            // It's much easier for us to create a high-quality error
+            // message than the low-level C code which found the problem.
+            throw new IOException(
+                "Cannot run program \"" + prog + "\""
+                + (dir == null ? "" : " (in directory \"" + dir + "\")")
+                + exceptionInfo,
+                e);
         }
     }
 }
diff -r 3e758b40337e -r 20c88fd14959 src/windows/classes/java/lang/ProcessImpl.java
--- jdk/src/windows/classes/java/lang/ProcessImpl.java	Tue Jul 30 17:20:22 2013 -0400
+++ jdk/src/windows/classes/java/lang/ProcessImpl.java	Tue Oct 29 01:07:01 2013 +0000
@@ -26,6 +26,9 @@
 package java.lang;
 
 import java.io.*;
+import java.util.ArrayList;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /* This class is for the exclusive use of ProcessBuilder.start() to
  * create new processes.
@@ -47,6 +50,66 @@
         return new ProcessImpl(cmdarray, envblock, dir, redirectErrorStream);
     }
 
+    private static class LazyPattern {
+        // Escape-support version:
+        //    "(\")((?:\\\\\\1|.)+?)\\1|([^\\s\"]+)";
+        private static final Pattern PATTERN =
+            Pattern.compile("[^\\s\"]+|\"[^\"]*\"");
+    };
+
+    /* Parses the command string parameter into the executable name and
+     * program arguments.
+     *
+     * The command string is broken into tokens. The token separator is a space
+     * or quota character. The space inside quotation is not a token separator.
+     * There are no escape sequences.
+     */
+    private static String[] getTokensFromCommand(String command) {
+        ArrayList<String> matchList = new ArrayList<String>(8);
+        Matcher regexMatcher = LazyPattern.PATTERN.matcher(command);
+        while (regexMatcher.find())
+            matchList.add(regexMatcher.group());
+        return matchList.toArray(new String[matchList.size()]);
+    }
+
+    private static String createCommandLine(boolean isCmdFile,
+                                     final String executablePath,
+                                     final String cmd[])
+    {
+        StringBuilder cmdbuf = new StringBuilder(80);
+
+        cmdbuf.append(executablePath);
+
+        for (int i = 1; i < cmd.length; ++i) {
+            cmdbuf.append(' ');
+            String s = cmd[i];
+            if (needsEscaping(isCmdFile, s)) {
+                cmdbuf.append('"');
+                cmdbuf.append(s);
+
+                // The code protects the [java.exe] and console command line
+                // parser, that interprets the [\"] combination as an escape
+                // sequence for the ["] char.
+                //     http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
+                //
+                // If the argument is an FS path, doubling of the tail [\]
+                // char is not a problem for non-console applications.
+                //
+                // The [\"] sequence is not an escape sequence for the [cmd.exe]
+                // command line parser. The case of the [""] tail escape
+                // sequence could not be realized due to the argument validation
+                // procedure.
+                if (!isCmdFile && s.endsWith("\\")) {
+                    cmdbuf.append('\\');
+                }
+                cmdbuf.append('"');
+            } else {
+                cmdbuf.append(s);
+            }
+        }
+        return cmdbuf.toString();
+    }
+
     // We guarantee the only command file execution for implicit [cmd.exe] run.
     //    http://technet.microsoft.com/en-us/library/bb490954.aspx
     private static final char CMD_BAT_ESCAPE[] = {' ', '\t', '<', '>', '&', '|', '^'};
@@ -128,6 +191,16 @@
         return fileToRun.getPath();
     }
 
+    private boolean isShellFile(String executablePath) {
+        String upPath = executablePath.toUpperCase();
+        return (upPath.endsWith(".CMD") || upPath.endsWith(".BAT"));
+    }
+
+    private String quoteString(String arg) {
+        StringBuilder argbuf = new StringBuilder(arg.length() + 2);
+        return argbuf.append('"').append(arg).append('"').toString();
+    }
+
 
     private long handle = 0;
     private FileDescriptor stdin_fd;
@@ -143,36 +216,66 @@
                         boolean redirectErrorStream)
         throws IOException
     {
-        // The [executablePath] is not quoted for any case.
-        String executablePath = getExecutablePath(cmd[0]);
+        String cmdstr;
+        SecurityManager security = System.getSecurityManager();
+        boolean allowAmbigousCommands = false;
+        if (security == null) {
+            String value = System.getProperty("jdk.lang.Process.allowAmbigousCommands");
+            if (value != null)
+                allowAmbigousCommands = !"false".equalsIgnoreCase(value);
+        }
+        if (allowAmbigousCommands) {
+            // Legacy mode.
 
-        // We need to extend the argument verification procedure
-        // to guarantee the only command file execution for implicit [cmd.exe]
-        // run.
-        String upPath = executablePath.toUpperCase();
-        boolean isCmdFile = (upPath.endsWith(".CMD") || upPath.endsWith(".BAT"));
+            // Normalize path if possible.
+            String executablePath = new File(cmd[0]).getPath();
 
-        StringBuilder cmdbuf = new StringBuilder(80);
+            // No worry about internal and unpaired ["] .
+            if (needsEscaping(false, executablePath) )
+                executablePath = quoteString(executablePath);
 
-        // Quotation protects from interpretation of the [path] argument as
-        // start of longer path with spaces. Quotation has no influence to
-        // [.exe] extension heuristic.
-        cmdbuf.append('"');
-        cmdbuf.append(executablePath);
-        cmdbuf.append('"');
+            cmdstr = createCommandLine(
+                false, //legacy mode doesn't worry about extended verification
+                executablePath,
+                cmd);
+        } else {
+            String executablePath;
+            try {
+                executablePath = getExecutablePath(cmd[0]);
+            } catch (IllegalArgumentException e) {
+                // Workaround for the calls like
+                // Runtime.getRuntime().exec("\"C:\\Program Files\\foo\" bar")
 
-        for (int i = 1; i < cmd.length; i++) {
-            cmdbuf.append(' ');
-            String s = cmd[i];
-            if (needsEscaping(isCmdFile, s)) {
-                cmdbuf.append('"');
-                cmdbuf.append(s);
-                cmdbuf.append('"');
-            } else {
-                cmdbuf.append(s);
+                // No chance to avoid CMD/BAT injection, except to do the work
+                // right from the beginning. Otherwise we have too many corner
+                // cases from
+                //    Runtime.getRuntime().exec(String[] cmd [, ...])
+                // calls with internal ["] and escape sequences.
+
+                // Restore original command line.
+                StringBuilder join = new StringBuilder();
+                // terminal space in command line is ok
+                for (String s : cmd)
+                    join.append(s).append(' ');
+
+                // Parse the command line again.
+                cmd = getTokensFromCommand(join.toString());
+                executablePath = getExecutablePath(cmd[0]);
+
+                // Check new executable name once more
+                if (security != null)
+                    security.checkExec(executablePath);
             }
+
+            // Quotation protects from interpretation of the [path] argument as
+            // start of longer path with spaces. Quotation has no influence to
+            // [.exe] extension heuristic.
+            cmdstr = createCommandLine(
+                    // We need the extended verification procedure for CMD files.
+                    isShellFile(executablePath),
+                    quoteString(executablePath),
+                    cmd);
         }
-        String cmdstr = cmdbuf.toString();
 
         stdin_fd  = new FileDescriptor();
         stdout_fd = new FileDescriptor();
diff -r 3e758b40337e -r 20c88fd14959 test/java/lang/Runtime/exec/ExecCommand.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ jdk/test/java/lang/Runtime/exec/ExecCommand.java	Tue Oct 29 01:07:01 2013 +0000
@@ -0,0 +1,163 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+
+/**
+ * @test
+ * @bug 8012453
+ * @run main/othervm ExecCommand
+ * @summary workaround for legacy applications with Runtime.getRuntime().exec(String command)
+ */
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.security.AccessControlException;
+
+public class ExecCommand {
+    static class SecurityMan extends SecurityManager {
+        public static String unquote(String str)
+        {
+            int length = (str == null)
+                ? 0
+                : str.length();
+
+            if (length > 1
+                && str.charAt(0) == '\"'
+                && str.charAt(length - 1) == '\"')
+            {
+               return str.substring(1, length - 1);
+            }
+            return str;
+        }
+
+        @Override public void checkExec(String cmd) {
+            String ncmd = (new File(unquote(cmd))).getPath();
+            if ( ncmd.equals(".\\Program")
+              || ncmd.equals("\".\\Program")
+              || ncmd.equals(".\\Program Files\\do.cmd")
+              || ncmd.equals(".\\Program.cmd"))
+            {
+                return;
+            }
+            super.checkExec(cmd);
+        }
+    }
+
+    // Parameters for the Runtime.exec calls
+    private static final String TEST_RTE_ARG[] = {
+        ".\\Program Files\\do.cmd",
+        "\".\\Program Files\\doNot.cmd\" arg",
+        "\".\\Program Files\\do.cmd\" arg",
+        // compatibility
+        "\".\\Program.cmd\" arg",
+        ".\\Program.cmd arg",
+    };
+
+    private static final String doCmdCopy[] = {
+        ".\\Program.cmd",
+        ".\\Program Files\\doNot.cmd",
+        ".\\Program Files\\do.cmd",
+    };
+
+    // Golden image for results
+    private static final String TEST_RTE_GI[][] = {
+                    //Pure system | Legacy mode | Legacy mode & SM
+        // [.\Program File\do.cmd]
+        new String[]{"IOException",  // [.\Program] not found
+                     "Success",
+                     "IOException"}, //SM - no legacy mode [.\Program] - OK
+
+        // [".\Program File\doNot.cmd" arg]
+        new String[]{"Success",
+                     "Success",
+                     "AccessControlException"}, //SM   - [".\Program] - OK,
+                                 //     [.\\Program Files\\doNot.cmd] - Fail
+
+        // [".\Program File\do.cmd" arg]
+        // AccessControlException
+        new String[]{"Success",
+                     "Success",
+                     "Success"}, //SM - [".\Program] - OK,
+                                 //     [.\\Program Files\\do.cmd] - OK
+
+        // compatibility
+        new String[]{"Success", "Success", "Success"}, //[".\Program.cmd"]
+        new String[]{"Success", "Success", "Success"}  //[.\Program.cmd]
+    };
+
+    public static void main(String[] _args) throws Exception {
+        if (!System.getProperty("os.name").startsWith("Windows")) {
+            return;
+        }
+
+        // tear up
+        try {
+            new File(".\\Program Files").mkdirs();
+            for (int i = 0; i < doCmdCopy.length; ++i) {
+                try (BufferedWriter outCmd = new BufferedWriter(
+                             new FileWriter(doCmdCopy[i]))) {
+                    outCmd.write("@echo %1");
+                }
+            }
+        } catch (IOException e) {
+            throw new Error(e.getMessage());
+        }
+
+        // action
+        for (int k = 0; k < 3; ++k) {
+            switch (k) {
+            case 1:
+                System.setProperty("jdk.lang.Process.allowAmbigousCommands", "");
+                break;
+            case 2:
+                System.setSecurityManager( new SecurityMan() );
+                break;
+            }
+            for (int i = 0; i < TEST_RTE_ARG.length; ++i) {
+                String outRes;
+                try {
+                    Process exec = Runtime.getRuntime().exec(TEST_RTE_ARG[i]);
+                    exec.waitFor();
+                    outRes = "Success";
+                } catch (IOException ioe) {
+                    outRes = "IOException: " + ioe.getMessage();
+                } catch (IllegalArgumentException iae) {
+                    outRes = "IllegalArgumentException: " + iae.getMessage();
+                } catch (AccessControlException se) {
+                    outRes = "AccessControlException: " + se.getMessage();
+                }
+
+                if (!outRes.startsWith(TEST_RTE_GI[i][k])) {
+                    throw new Error("Unexpected output! Step" + k + "" + i
+                                + " \nArgument: " + TEST_RTE_ARG[i]
+                                + "\nExpected: " + TEST_RTE_GI[i][k]
+                                + "\n  Output: " + outRes);
+                } else {
+                    System.out.println("RTE OK:" + TEST_RTE_ARG[i]);
+                }
+            }
+        }
+    }
+}