Skip to content

Commit 5fcfb7a

Browse files
Dongbo HeRealFYang
Dongbo He
authored andcommittedMar 21, 2022
8194154: System property user.dir should not be changed
Cached user.dir so getCanonicalPath uses the cached value. Reviewed-by: sgehwolf Backport-of: 4ea684bf31fc4e3cdee2ae51c0000a7b3e914151
1 parent 94cb2ef commit 5fcfb7a

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed
 

‎jdk/src/solaris/classes/java/io/UnixFileSystem.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -34,6 +34,7 @@ class UnixFileSystem extends FileSystem {
3434
private final char slash;
3535
private final char colon;
3636
private final String javaHome;
37+
private final String userDir;
3738

3839
public UnixFileSystem() {
3940
slash = AccessController.doPrivileged(
@@ -42,6 +43,8 @@ public UnixFileSystem() {
4243
new GetPropertyAction("path.separator")).charAt(0);
4344
javaHome = AccessController.doPrivileged(
4445
new GetPropertyAction("java.home"));
46+
userDir = AccessController.doPrivileged(
47+
new GetPropertyAction("user.dir"));
4548
}
4649

4750

@@ -130,7 +133,11 @@ public boolean isAbsolute(File f) {
130133

131134
public String resolve(File f) {
132135
if (isAbsolute(f)) return f.getPath();
133-
return resolve(System.getProperty("user.dir"), f.getPath());
136+
SecurityManager sm = System.getSecurityManager();
137+
if (sm != null) {
138+
sm.checkPropertyAccess("user.dir");
139+
}
140+
return resolve(userDir, f.getPath());
134141
}
135142

136143
// Caches for canonicalization results to improve startup performance.

‎jdk/src/windows/classes/java/io/WinNTFileSystem.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -40,13 +40,16 @@ class WinNTFileSystem extends FileSystem {
4040
private final char slash;
4141
private final char altSlash;
4242
private final char semicolon;
43+
private final String userDir;
4344

4445
public WinNTFileSystem() {
4546
slash = AccessController.doPrivileged(
4647
new GetPropertyAction("file.separator")).charAt(0);
4748
semicolon = AccessController.doPrivileged(
4849
new GetPropertyAction("path.separator")).charAt(0);
4950
altSlash = (this.slash == '\\') ? '/' : '\\';
51+
userDir = AccessController.doPrivileged(
52+
new GetPropertyAction("user.dir"));
5053
}
5154

5255
private boolean isSlash(char c) {
@@ -343,7 +346,11 @@ public String resolve(File f) {
343346
private String getUserPath() {
344347
/* For both compatibility and security,
345348
we must look this up every time */
346-
return normalize(System.getProperty("user.dir"));
349+
SecurityManager sm = System.getSecurityManager();
350+
if (sm != null) {
351+
sm.checkPropertyAccess("user.dir");
352+
}
353+
return normalize(userDir);
347354
}
348355

349356
private String getDrive(String path) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/* @test
25+
@bug 8194154
26+
@summary Test changing property user.dir on impacting getCanonicalPath
27+
@run main/othervm UserDirChangedTest
28+
*/
29+
30+
import java.io.File;
31+
32+
public class UserDirChangedTest {
33+
public static void main(String[] args) throws Exception {
34+
String keyUserDir = "user.dir";
35+
String userDirNew = "/home/a/b/c/";
36+
String fileName = "./a";
37+
38+
String userDir = System.getProperty(keyUserDir);
39+
File file = new File(fileName);
40+
String canFilePath = file.getCanonicalPath();
41+
42+
// now reset user.dir, this will cause crash on linux without bug 8194154 fixed.
43+
System.setProperty(keyUserDir, userDirNew);
44+
String newCanFilePath = file.getCanonicalPath();
45+
System.out.format("%24s %48s%n", "Canonical Path = ", canFilePath);
46+
System.out.format("%24s %48s%n", "new Canonical Path = ", newCanFilePath);
47+
if (!canFilePath.equals(newCanFilePath)) {
48+
throw new RuntimeException("Changing property user.dir should have no effect on getCanonicalPath");
49+
}
50+
}
51+
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Mar 21, 2022

@openjdk-notifier[bot]
Please sign in to comment.