This is an automated email from the ASF dual-hosted git repository.
sarutak pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.x by this push:
new a7088634f605 [SPARK-59756][CORE] Make Utils.chmod700 atomic on POSIX FS
a7088634f605 is described below
commit a7088634f605ac0c4c8afd1a825cab929a733fcf
Author: Holden Karau <[email protected]>
AuthorDate: Mon Nov 9 03:23:08 2026 +0900
[SPARK-59756][CORE] Make Utils.chmod700 atomic on POSIX FS
### What changes were proposed in this pull request?
`Utils.chmod700` now makes a single `Files.setPosixFilePermissions(path,
"rwx------")` call on POSIX filesystems, instead of six
`File.setReadable/setWritable/setExecutable` calls.
On non-POSIX filesystems the `File.set*` sequence is kept as a best-effort,
non-atomic fallback. On Windows, these calls change nothing for a directory and
return false.
Review notes:
- `chmod700` guards the empty path explicitly: `new File("").toPath`
resolves to the process working directory in NIO, and the old `File.set*`
sequence returned `false` for it; the guard preserves that behavior instead of
chmod-ing the cwd.
- A `SecurityException` from the POSIX attempt now becomes `false`
(matching the documented `return`) instead of propagating; only reachable under
a `SecurityManager` that denies file write.
### Why are the changes needed?
Each `File.set*` call is a non-atomic stat-then-chmod, so concurrent
`chmod700` calls on the same path cannot interleave into a broken permission
set when POSIX filesystems use a single atomic `chmod` instead.
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
Added tests to `UtilsSuite`: concurrent `chmod700` calls on the same path
always leave `rwx------` (POSIX-only), a nonexistent path returns `false`, and
an empty path returns `false`. Ran the full `core` module build and test suite
locally (GREEN).
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Cursor (Kimi K3)
Closes #59061 from holdenk/spark-59756-chmod700.
Lead-authored-by: Holden Karau <[email protected]>
Co-authored-by: Claude <Holden's Claude Robot [email protected]>
Signed-off-by: Kousuke Saruta <[email protected]>
(cherry picked from commit 76ddf554d0ea791a7a15ea203af94dc7d3dd650c)
Signed-off-by: Kousuke Saruta <[email protected]>
skip-checks: true
---
.../main/scala/org/apache/spark/util/Utils.scala | 49 ++++++++++++---
.../scala/org/apache/spark/util/UtilsSuite.scala | 69 +++++++++++++++++++++-
2 files changed, 109 insertions(+), 9 deletions(-)
diff --git a/core/src/main/scala/org/apache/spark/util/Utils.scala
b/core/src/main/scala/org/apache/spark/util/Utils.scala
index 40b4e20f5761..00f427245031 100644
--- a/core/src/main/scala/org/apache/spark/util/Utils.scala
+++ b/core/src/main/scala/org/apache/spark/util/Utils.scala
@@ -27,8 +27,9 @@ import java.nio.ByteBuffer
import java.nio.channels.Channels
import java.nio.charset.StandardCharsets
import java.nio.file.Files
+import java.nio.file.attribute.PosixFilePermissions
import java.security.SecureRandom
-import java.util.{HexFormat, Locale, Properties, Random, UUID}
+import java.util.{Collections, HexFormat, Locale, Properties, Random, UUID}
import java.util.concurrent._
import java.util.concurrent.TimeUnit.NANOSECONDS
import java.util.zip.{GZIPInputStream, ZipInputStream}
@@ -223,19 +224,53 @@ private[spark] object Utils
writeByteBufferImpl(bb, out.write)
}
+ // fromString returns a mutable set; wrap it so no future caller can mutate
the shared set.
+ private val chmod700Perms =
+ Collections.unmodifiableSet(PosixFilePermissions.fromString("rwx------"))
+
/**
* JDK equivalent of `chmod 700 file`.
*
+ * On POSIX filesystems this is a single `chmod` call, which is atomic:
concurrent calls on
+ * the same path cannot interleave into a broken permission set, unlike the
`File.set*`
+ * fallback below, each call of which is a non-atomic stat-then-chmod. The
POSIX call writes
+ * exactly the nine rwx bits, so it also clears setuid, setgid, and sticky
bits, which the
+ * previous `File.set*` implementation kept.
+ *
* @param file the file whose permissions will be modified
* @return true if the permissions were successfully changed, false
otherwise.
*/
def chmod700(file: File): Boolean = {
- file.setReadable(false, false) &&
- file.setReadable(true, true) &&
- file.setWritable(false, false) &&
- file.setWritable(true, true) &&
- file.setExecutable(false, false) &&
- file.setExecutable(true, true)
+ if (file.getPath.isEmpty) {
+ // The NIO empty path resolves to the process working directory; the
File.set* sequence
+ // returned false for it, so keep that behavior rather than chmod-ing
the cwd.
+ return false
+ }
+ try {
+ Files.setPosixFilePermissions(file.toPath, chmod700Perms)
+ true
+ } catch {
+ case _: UnsupportedOperationException =>
+ // Non-POSIX filesystems use this best-effort, non-atomic fallback. On
Windows, these
+ // calls change nothing for a directory and return false. Attempt
every operation even
+ // after a failure, and never grant group/other access.
+ val succeeded =
+ file.setReadable(false, false) &
+ file.setWritable(false, false) &
+ file.setExecutable(false, false) &
+ file.setReadable(true, true) &
+ file.setWritable(true, true) &
+ file.setExecutable(true, true)
+ if (!succeeded) {
+ // Structural on Windows: File.setReadable(false, false) always
fails there (the
+ // filesystem has no read permission to revoke), so do not warn on
every call.
+ logDebug(log"Failed to set permissions to 700 on ${MDC(PATH,
file.getAbsolutePath)}")
+ }
+ succeeded
+ case NonFatal(e) =>
+ logWarning(log"Failed to set permissions to 700 on ${MDC(PATH,
file.getAbsolutePath)}", e)
+ false
+ }
}
/**
diff --git a/core/src/test/scala/org/apache/spark/util/UtilsSuite.scala
b/core/src/test/scala/org/apache/spark/util/UtilsSuite.scala
index cf68516b6802..e296dc96bb10 100644
--- a/core/src/test/scala/org/apache/spark/util/UtilsSuite.scala
+++ b/core/src/test/scala/org/apache/spark/util/UtilsSuite.scala
@@ -23,10 +23,11 @@ import java.lang.reflect.Field
import java.net.{BindException, ServerSocket, URI}
import java.nio.{ByteBuffer, ByteOrder}
import java.nio.charset.StandardCharsets.UTF_8
-import java.nio.file.Files
+import java.nio.file.{Files, FileSystems}
+import java.nio.file.attribute.{PosixFilePermission, PosixFilePermissions}
import java.text.DecimalFormatSymbols
import java.util.Locale
-import java.util.concurrent.TimeUnit
+import java.util.concurrent.{CyclicBarrier, TimeUnit}
import java.util.zip.{GZIPOutputStream, ZipEntry, ZipOutputStream}
import scala.collection.mutable.ListBuffer
@@ -573,6 +574,70 @@ class UtilsSuite extends SparkFunSuite with
ResetSystemProperties {
assert(!Utils.createDirectory(scenario8))
}
+ test("chmod700 leaves rwx------ under concurrent calls on the same path") {
+ // Check both the final mode and samples taken during the race: with an
atomic chmod every
+ // sample is the initial or the final mode, and anything else is a
non-atomic interleaving.
+
assume(FileSystems.getDefault.supportedFileAttributeViews().contains("posix"))
+ withTempDir { dir =>
+ val initialPerms = PosixFilePermissions.fromString("rwxr-xr-x")
+ val expectedPerms = PosixFilePermissions.fromString("rwx------")
+ // Start from a permissive mode so the assertions cannot pass vacuously
under umask 077.
+ Files.setPosixFilePermissions(dir.toPath, initialPerms)
+ assert(Utils.chmod700(dir))
+ Files.setPosixFilePermissions(dir.toPath, initialPerms)
+ @volatile var observing = true
+ // Written only by the observer and read after observer.join(), which
synchronizes.
+ var brokenStates = Set.empty[java.util.Set[PosixFilePermission]]
+ var observerError: Throwable = null
+ val observer = new Thread(() => {
+ try {
+ while (observing) {
+ val perms = Files.getPosixFilePermissions(dir.toPath)
+ if (perms != initialPerms && perms != expectedPerms) {
+ brokenStates += perms
+ }
+ }
+ } catch {
+ case t: Throwable => observerError = t
+ }
+ })
+ observer.start()
+ try {
+ val numThreads = 4
+ (1 to 10).foreach { _ =>
+ // Align the thread starts to widen the race window.
+ val barrier = new CyclicBarrier(numThreads)
+ val threads = (1 to numThreads).map { _ =>
+ new Thread(() => {
+ barrier.await()
+ (1 to 20).foreach(_ => Utils.chmod700(dir))
+ })
+ }
+ threads.foreach(_.start())
+ threads.foreach(_.join())
+ assert(Files.getPosixFilePermissions(dir.toPath) === expectedPerms)
+ // Reset so the next round also races from the permissive mode.
+ Files.setPosixFilePermissions(dir.toPath, initialPerms)
+ }
+ } finally {
+ observing = false
+ observer.join()
+ }
+ assert(observerError == null, s"Observer thread failed: $observerError")
+ assert(brokenStates.isEmpty, s"Observed non-atomic permission states:
$brokenStates")
+ }
+ }
+
+ test("chmod700 returns false instead of throwing for a nonexistent path") {
+ val missing = new File(System.getProperty("java.io.tmpdir"), "chmod700-" +
System.nanoTime())
+ assert(!Utils.chmod700(missing))
+ }
+
+ test("chmod700 returns false for an empty path") {
+ // The NIO empty path resolves to the process working directory; chmod700
must not touch it.
+ assert(!Utils.chmod700(new File("")))
+ }
+
test("doesDirectoryContainFilesNewerThan") {
// create some temporary directories and files
withTempDir { parent =>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]