[ 
https://issues.apache.org/jira/browse/FLINK-41435?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

LuChengxiang updated FLINK-41435:
---------------------------------
    Description: 
h2. Summary

DefaultDelegationTokenManager runs the initial token update directly on the 
caller thread, although scheduled refreshes use the configured IO executor. 
When the initial call runs on a Pekko ForkJoinWorkerThread, 
HadoopFSDelegationTokenProvider performs a blocking HDFS RPC while 
freshUGI.doAs(...) is effective. On the Hadoop CompletableFuture IPC path, 
ForkJoinPool managed blocking can create a replacement worker during that 
window. The Thread constructor copies the current effective 
AccessControlContext, so the new pooled worker retains the transient Subject 
after doAs returns.

The primary fix is to run the initial update on the same IO executor used by 
scheduled refreshes. This keeps provider and receiver work off the callers FJP 
thread and preserves the existing synchronous start semantics with FutureTask.

PR: https://github.com/apache/flink/pull/30026

h2. Observed failure

This was observed in a Flink 1.20.5 Kubernetes application-mode job using 
ZooKeeper-based high availability and HDFS-backed checkpoint storage. The job 
used a Hadoop IPC client with the CompletableFuture-based wait path. The 
primary failure was in checkpoint storage:

{noformat}
CheckpointCoordinator.initializeCheckpointLocation
  -> FsCheckpointStorageAccess.initializeLocationForCheckpoint
  -> HadoopFileSystem.mkdirs
  -> DFSClient.mkdirs
{noformat}

After the 24h deadline, the checkpoint-storage client reported InvalidToken for 
its startup token even though refresh had obtained a new HDFS token and the 
receiver reported success. The same JVM also showed a FileSystemJobResultStore 
stack using the expired token during cleanup.

Not every application-mode job is affected. The failure is timing-dependent: a 
long-lived Hadoop client must capture one Subject/UGI lifecycle, and a later 
token-refresh receiver must run under a different one.

h2. Root cause

The initial update path is inconsistent with the scheduled refresh path:

* {{DefaultDelegationTokenManager.start()}} calls {{startTokensUpdate()}} 
directly on the caller thread.
* A scheduled refresh uses {{ioExecutor.execute(this::startTokensUpdate)}}.
* {{ClusterEntrypoint}} creates the cluster IO executor before creating the 
token manager, so the executor is already the intended home for blocking token 
refresh work.

The problematic startup sequence is:

{noformat}
ResourceManager.onStart()
  -> DefaultDelegationTokenManager.start()
  -> startTokensUpdate()                         [Pekko FJP worker]
  -> HadoopFSDelegationTokenProvider.obtainDelegationTokens()
  -> freshUGI.doAs(...)
  -> fs.addDelegationTokens(...)
  -> HDFS RPC in CompletableFuture.get()          [Hadoop 3.5.0+]
  -> ForkJoinPool.managedBlock()
  -> tryCompensate()
  -> createWorker()
  -> new dispatcher worker inherits the fresh Subject
  -> later creates a long-lived cluster-io worker
{noformat}

The key is not the thread name itself. The thread constructor captures the 
effective AccessControlContext at creation time. A worker created while 
freshUGI.doAs(...) is active therefore retains that Subject even after the 
provider returns to the login Subject.

The same inheritance mechanism was also observed on a long-lived RpcEndpoint 
main-scheduler thread. That thread only forwards scheduled work to the RPC 
endpoint main thread and is not a confirmed token-receiver failure path; it is 
evidence that the thread-creation hazard is broader than one pool.

h2. Proposed change

Run the initial token update on the configured IO executor, matching the 
scheduled refresh path:

* Submit the initial {{startTokensUpdate()}} to {{ioExecutor}}.
* Wait for completion with {{FutureTask.get()}} so {{start()}} remains 
synchronous.
* Use {{FutureTask}} rather than {{CompletableFuture.get()}} so the waiting 
caller does not itself enter ForkJoinPool managed blocking.

This removes the provider from the Pekko FJP worker on the initial path. The IO 
executor creates or reuses its worker before {{startTokensUpdate()}} runs, and 
provider/receiver work then executes on the stable IO thread.

h2. Scope and boundaries

h3. Out of scope

* General hardening for all long-lived threads created during a transient 
security context. This remains a separate follow-up.
* Changing HadoopDelegationTokenReceiver target selection. The receiver still 
depends on its executing thread context; a receiver-side context-first change 
is a related latent hardening, not required for the reproduced startup path.

The earlier receiver-target PR #30025 has been closed as superseded by PR 
#30026.
* Rebinding Hadoop clients already created under a different Subject.
* Double security installation and SecurityUtils.install idempotency.

h3. Follow-up hardening

The general invariant remains: long-lived worker threads must not be created 
while a transient Subject is effective, or security-sensitive work on 
long-lived executors must receive an explicit SecurityContext/UGI rather than 
resolving identity from the executing thread. This includes the RpcEndpoint 
main-scheduler observation and any other pooled worker created inside 
freshUGI.doAs(...).

h2. Deployment and version notes

The trigger is the caller thread and blocking IPC path, not Kubernetes as a 
platform name. YARN is not confirmed to reproduce in the tested normal 
application-mode path, but is not claimed to be immune.

The CompletableFuture-based IPC wait path is present in Hadoop 3.5.0 
(HADOOP-19235). Earlier Hadoop clients that use AsyncGet.Util.wait do not enter 
FJP managed blocking from the provider wait itself, although the receiver 
target assumption is independent of that version boundary.

h2. Testing

* Unit test verifies that the initial token update runs on the configured IO 
executor.
* ./mvnw -pl flink-runtime -Dtest=DefaultDelegationTokenManagerTest 
-DfailIfNoTests=false -Denforcer.skip=true test
  * Tests run: 17, Failures: 0, Errors: 0, Skipped: 0
* A deployed application-mode job using the CompletableFuture IPC path should 
survive the 24h HDFS token deadline and continue checkpointing without 
SecretManager$InvalidToken.
* A controlled probe should verify that the initial provider call runs on the 
IO executor and does not create a replacement Pekko dispatcher worker inside 
freshUGI.doAs(...).

h2. Acceptance criteria

* The initial delegation-token update runs on the configured IO executor.
* The initial provider call does not execute on a Pekko ForkJoinWorkerThread 
and does not trigger FJP compensation while freshUGI.doAs(...) is effective.
* A long-running application-mode job using the CompletableFuture IPC path 
survives the 24h HDFS token deadline.
* Scheduled refreshes continue to run on the IO executor and preserve the 
existing receiver/listener behavior.


  was:
h2. Summary

DefaultDelegationTokenManager runs the initial token update directly on the 
caller thread, although scheduled refreshes use the configured IO executor. 
When the initial call runs on a Pekko ForkJoinWorkerThread, 
HadoopFSDelegationTokenProvider performs a blocking HDFS RPC while 
freshUGI.doAs(...) is effective. On the Hadoop CompletableFuture IPC path, 
ForkJoinPool managed blocking can create a replacement worker during that 
window. The Thread constructor copies the current effective 
AccessControlContext, so the new pooled worker retains the transient Subject 
after doAs returns.

The primary fix is to run the initial update on the same IO executor used by 
scheduled refreshes. This keeps provider and receiver work off the callers FJP 
thread and preserves the existing synchronous start semantics with FutureTask.

PR: https://github.com/apache/flink/pull/30026

h2. Observed failure

This was observed in a Flink 1.20.5 Kubernetes application-mode job using 
ZooKeeper-based high availability and HDFS-backed checkpoint storage. The job 
used a Hadoop IPC client with the CompletableFuture-based wait path. The 
primary failure was in checkpoint storage:

{noformat}
CheckpointCoordinator.initializeCheckpointLocation
  -> FsCheckpointStorageAccess.initializeLocationForCheckpoint
  -> HadoopFileSystem.mkdirs
  -> DFSClient.mkdirs
{noformat}

After the 24h deadline, the checkpoint-storage client reported InvalidToken for 
its startup token even though refresh had obtained a new HDFS token and the 
receiver reported success. The same JVM also showed a FileSystemJobResultStore 
stack using the expired token during cleanup.

Not every application-mode job is affected. The failure is timing-dependent: a 
long-lived Hadoop client must capture one Subject/UGI lifecycle, and a later 
token-refresh receiver must run under a different one.

h2. Root cause

The initial update path is inconsistent with the scheduled refresh path:

* {{DefaultDelegationTokenManager.start()}} calls {{startTokensUpdate()}} 
directly on the caller thread.
* A scheduled refresh uses {{ioExecutor.execute(this::startTokensUpdate)}}.
* {{ClusterEntrypoint}} creates the cluster IO executor before creating the 
token manager, so the executor is already the intended home for blocking token 
refresh work.

The problematic startup sequence is:

{noformat}
ResourceManager.onStart()
  -> DefaultDelegationTokenManager.start()
  -> startTokensUpdate()                         [Pekko FJP worker]
  -> HadoopFSDelegationTokenProvider.obtainDelegationTokens()
  -> freshUGI.doAs(...)
  -> fs.addDelegationTokens(...)
  -> HDFS RPC in CompletableFuture.get()          [Hadoop 3.5.0+]
  -> ForkJoinPool.managedBlock()
  -> tryCompensate()
  -> createWorker()
  -> new dispatcher worker inherits the fresh Subject
  -> later creates a long-lived cluster-io worker
{noformat}

The key is not the thread name itself. The thread constructor captures the 
effective AccessControlContext at creation time. A worker created while 
freshUGI.doAs(...) is active therefore retains that Subject even after the 
provider returns to the login Subject.

The same inheritance mechanism was also observed on a long-lived RpcEndpoint 
main-scheduler thread. That thread only forwards scheduled work to the RPC 
endpoint main thread and is not a confirmed token-receiver failure path; it is 
evidence that the thread-creation hazard is broader than one pool.

h2. Proposed change

Run the initial token update on the configured IO executor, matching the 
scheduled refresh path:

* Submit the initial {{startTokensUpdate()}} to {{ioExecutor}}.
* Wait for completion with {{FutureTask.get()}} so {{start()}} remains 
synchronous.
* Use {{FutureTask}} rather than {{CompletableFuture.get()}} so the waiting 
caller does not itself enter ForkJoinPool managed blocking.

This removes the provider from the Pekko FJP worker on the initial path. The IO 
executor creates or reuses its worker before {{startTokensUpdate()}} runs, and 
provider/receiver work then executes on the stable IO thread.

h2. Scope and boundaries

h3. Out of scope

* General hardening for all long-lived threads created during a transient 
security context. This remains a separate follow-up.
* Changing HadoopDelegationTokenReceiver target selection. The receiver still 
depends on its executing thread context; a receiver-side context-first change 
is a related latent hardening, not required for the reproduced startup path.
* Rebinding Hadoop clients already created under a different Subject.
* Double security installation and SecurityUtils.install idempotency.

h3. Follow-up hardening

The general invariant remains: long-lived worker threads must not be created 
while a transient Subject is effective, or security-sensitive work on 
long-lived executors must receive an explicit SecurityContext/UGI rather than 
resolving identity from the executing thread. This includes the RpcEndpoint 
main-scheduler observation and any other pooled worker created inside 
freshUGI.doAs(...).

h2. Deployment and version notes

The trigger is the caller thread and blocking IPC path, not Kubernetes as a 
platform name. YARN is not confirmed to reproduce in the tested normal 
application-mode path, but is not claimed to be immune.

The CompletableFuture-based IPC wait path is present in Hadoop 3.5.0 
(HADOOP-19235). Earlier Hadoop clients that use AsyncGet.Util.wait do not enter 
FJP managed blocking from the provider wait itself, although the receiver 
target assumption is independent of that version boundary.

h2. Testing

* Unit test verifies that the initial token update runs on the configured IO 
executor.
* ./mvnw -pl flink-runtime -Dtest=DefaultDelegationTokenManagerTest 
-DfailIfNoTests=false -Denforcer.skip=true test
  * Tests run: 17, Failures: 0, Errors: 0, Skipped: 0
* A deployed application-mode job using the CompletableFuture IPC path should 
survive the 24h HDFS token deadline and continue checkpointing without 
SecretManager$InvalidToken.
* A controlled probe should verify that the initial provider call runs on the 
IO executor and does not create a replacement Pekko dispatcher worker inside 
freshUGI.doAs(...).

h2. Acceptance criteria

* The initial delegation-token update runs on the configured IO executor.
* The initial provider call does not execute on a Pekko ForkJoinWorkerThread 
and does not trigger FJP compensation while freshUGI.doAs(...) is effective.
* A long-running application-mode job using the CompletableFuture IPC path 
survives the 24h HDFS token deadline.
* Scheduled refreshes continue to run on the IO executor and preserve the 
existing receiver/listener behavior.



> Initial delegation token update can leak a transient Subject into pooled 
> workers
> --------------------------------------------------------------------------------
>
>                 Key: FLINK-41435
>                 URL: https://issues.apache.org/jira/browse/FLINK-41435
>             Project: Flink
>          Issue Type: Bug
>          Components: Runtime / Configuration
>    Affects Versions: 2.0.0, 1.19.0, 1.20.0, 2.1.0, 2.2.0, 2.3.0, 2.4.0
>         Environment: Flink 1.19.0 and later (1.19.x, 1.20.x, 2.0 through 
> 2.4), plus master (2.5-SNAPSHOT); Kubernetes and standalone application mode 
> with Kerberos and HDFS delegation tokens. The mismatch is more likely when 
> FileSystemJobResultStore and the token manager share the cluster-io executor 
> (FLINK-27204).
>            Reporter: LuChengxiang
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 2.5.0
>
>
> h2. Summary
> DefaultDelegationTokenManager runs the initial token update directly on the 
> caller thread, although scheduled refreshes use the configured IO executor. 
> When the initial call runs on a Pekko ForkJoinWorkerThread, 
> HadoopFSDelegationTokenProvider performs a blocking HDFS RPC while 
> freshUGI.doAs(...) is effective. On the Hadoop CompletableFuture IPC path, 
> ForkJoinPool managed blocking can create a replacement worker during that 
> window. The Thread constructor copies the current effective 
> AccessControlContext, so the new pooled worker retains the transient Subject 
> after doAs returns.
> The primary fix is to run the initial update on the same IO executor used by 
> scheduled refreshes. This keeps provider and receiver work off the callers 
> FJP thread and preserves the existing synchronous start semantics with 
> FutureTask.
> PR: https://github.com/apache/flink/pull/30026
> h2. Observed failure
> This was observed in a Flink 1.20.5 Kubernetes application-mode job using 
> ZooKeeper-based high availability and HDFS-backed checkpoint storage. The job 
> used a Hadoop IPC client with the CompletableFuture-based wait path. The 
> primary failure was in checkpoint storage:
> {noformat}
> CheckpointCoordinator.initializeCheckpointLocation
>   -> FsCheckpointStorageAccess.initializeLocationForCheckpoint
>   -> HadoopFileSystem.mkdirs
>   -> DFSClient.mkdirs
> {noformat}
> After the 24h deadline, the checkpoint-storage client reported InvalidToken 
> for its startup token even though refresh had obtained a new HDFS token and 
> the receiver reported success. The same JVM also showed a 
> FileSystemJobResultStore stack using the expired token during cleanup.
> Not every application-mode job is affected. The failure is timing-dependent: 
> a long-lived Hadoop client must capture one Subject/UGI lifecycle, and a 
> later token-refresh receiver must run under a different one.
> h2. Root cause
> The initial update path is inconsistent with the scheduled refresh path:
> * {{DefaultDelegationTokenManager.start()}} calls {{startTokensUpdate()}} 
> directly on the caller thread.
> * A scheduled refresh uses {{ioExecutor.execute(this::startTokensUpdate)}}.
> * {{ClusterEntrypoint}} creates the cluster IO executor before creating the 
> token manager, so the executor is already the intended home for blocking 
> token refresh work.
> The problematic startup sequence is:
> {noformat}
> ResourceManager.onStart()
>   -> DefaultDelegationTokenManager.start()
>   -> startTokensUpdate()                         [Pekko FJP worker]
>   -> HadoopFSDelegationTokenProvider.obtainDelegationTokens()
>   -> freshUGI.doAs(...)
>   -> fs.addDelegationTokens(...)
>   -> HDFS RPC in CompletableFuture.get()          [Hadoop 3.5.0+]
>   -> ForkJoinPool.managedBlock()
>   -> tryCompensate()
>   -> createWorker()
>   -> new dispatcher worker inherits the fresh Subject
>   -> later creates a long-lived cluster-io worker
> {noformat}
> The key is not the thread name itself. The thread constructor captures the 
> effective AccessControlContext at creation time. A worker created while 
> freshUGI.doAs(...) is active therefore retains that Subject even after the 
> provider returns to the login Subject.
> The same inheritance mechanism was also observed on a long-lived RpcEndpoint 
> main-scheduler thread. That thread only forwards scheduled work to the RPC 
> endpoint main thread and is not a confirmed token-receiver failure path; it 
> is evidence that the thread-creation hazard is broader than one pool.
> h2. Proposed change
> Run the initial token update on the configured IO executor, matching the 
> scheduled refresh path:
> * Submit the initial {{startTokensUpdate()}} to {{ioExecutor}}.
> * Wait for completion with {{FutureTask.get()}} so {{start()}} remains 
> synchronous.
> * Use {{FutureTask}} rather than {{CompletableFuture.get()}} so the waiting 
> caller does not itself enter ForkJoinPool managed blocking.
> This removes the provider from the Pekko FJP worker on the initial path. The 
> IO executor creates or reuses its worker before {{startTokensUpdate()}} runs, 
> and provider/receiver work then executes on the stable IO thread.
> h2. Scope and boundaries
> h3. Out of scope
> * General hardening for all long-lived threads created during a transient 
> security context. This remains a separate follow-up.
> * Changing HadoopDelegationTokenReceiver target selection. The receiver still 
> depends on its executing thread context; a receiver-side context-first change 
> is a related latent hardening, not required for the reproduced startup path.
> The earlier receiver-target PR #30025 has been closed as superseded by PR 
> #30026.
> * Rebinding Hadoop clients already created under a different Subject.
> * Double security installation and SecurityUtils.install idempotency.
> h3. Follow-up hardening
> The general invariant remains: long-lived worker threads must not be created 
> while a transient Subject is effective, or security-sensitive work on 
> long-lived executors must receive an explicit SecurityContext/UGI rather than 
> resolving identity from the executing thread. This includes the RpcEndpoint 
> main-scheduler observation and any other pooled worker created inside 
> freshUGI.doAs(...).
> h2. Deployment and version notes
> The trigger is the caller thread and blocking IPC path, not Kubernetes as a 
> platform name. YARN is not confirmed to reproduce in the tested normal 
> application-mode path, but is not claimed to be immune.
> The CompletableFuture-based IPC wait path is present in Hadoop 3.5.0 
> (HADOOP-19235). Earlier Hadoop clients that use AsyncGet.Util.wait do not 
> enter FJP managed blocking from the provider wait itself, although the 
> receiver target assumption is independent of that version boundary.
> h2. Testing
> * Unit test verifies that the initial token update runs on the configured IO 
> executor.
> * ./mvnw -pl flink-runtime -Dtest=DefaultDelegationTokenManagerTest 
> -DfailIfNoTests=false -Denforcer.skip=true test
>   * Tests run: 17, Failures: 0, Errors: 0, Skipped: 0
> * A deployed application-mode job using the CompletableFuture IPC path should 
> survive the 24h HDFS token deadline and continue checkpointing without 
> SecretManager$InvalidToken.
> * A controlled probe should verify that the initial provider call runs on the 
> IO executor and does not create a replacement Pekko dispatcher worker inside 
> freshUGI.doAs(...).
> h2. Acceptance criteria
> * The initial delegation-token update runs on the configured IO executor.
> * The initial provider call does not execute on a Pekko ForkJoinWorkerThread 
> and does not trigger FJP compensation while freshUGI.doAs(...) is effective.
> * A long-running application-mode job using the CompletableFuture IPC path 
> survives the 24h HDFS token deadline.
> * Scheduled refreshes continue to run on the IO executor and preserve the 
> existing receiver/listener behavior.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to