Andrey Yarovoy created HDDS-17781:
-------------------------------------

             Summary: Shaded Ozone filesystem client registers its relocated 
Bouncy Castle provider as the JVM-wide "BC", breaking the application's own 
Bouncy Castle
                 Key: HDDS-17781
                 URL: https://issues.apache.org/jira/browse/HDDS-17781
             Project: Apache Ozone
          Issue Type: Bug
          Components: Ozone Client
            Reporter: Andrey Yarovoy


h3. Problem

The shaded filesystem jars relocate Bouncy Castle to 
\{{org.apache.ozone.shaded.org.bouncycastle}}, but the relocated provider still 
calls itself \{{"BC"}} (\{{BouncyCastleProvider.PROVIDER_NAME}} is a string, 
which relocation doesn't change). \{{SecurityConfig.initSecurityProvider}} 
registers it with \{{Security.addProvider(new BouncyCastleProvider())}}, which 
puts it in the JVM-wide provider list under the name \{{"BC"}}. Only one 
provider can hold that name, and the first one registered wins.

Up to 2.2.x, the \{{SecurityConfig}} constructor does this. Every \{{ofs}} / 
\{{o3fs}} initialization creates a \{{SecurityConfig}} 
(\{{BasicRootedOzoneClientAdapterImpl}}, \{{BasicOzoneClientAdapterImpl}}), 
with or without security enabled. So any JVM that opens an Ozone file system 
registers Ozone's private, relocated BC as the JVM's \{{"BC"}}. After that:
* the application's own \{{Security.addProvider(new BouncyCastleProvider())}} 
returns \{{-1}} and is silently ignored;
* \{{getInstance(..., "BC")}} in the application returns relocated objects. A 
cast to a Bouncy Castle type (\{{org.bouncycastle.jce.interfaces.ECPublicKey}}, 
\{{BCECPublicKey}}, …) throws \{{ClassCastException}};
* the application runs on Ozone's Bouncy Castle version, not its own, without 
knowing it.

If the application registers BC first, it's the other way round: Ozone silently 
uses the application's BC, whatever version that is.

h3. Reproduced

{\{new SecurityConfig(new OzoneConfiguration())}}, which is what an \{{ofs}} 
initialize does, then the application registers BC 1.86:

||filesystem jar||\{{"BC"}} after \{{new SecurityConfig(conf)}}||application's 
\{{addProvider(BC)}}||application's cast of an EC key from \{{"BC"}}||
|2.2.0|\{{org.apache.ozone.shaded.org.bouncycastle.jce.provider.BouncyCastleProvider}}|\{{-1}},
 ignored|fails: the key is \{{org.apache.ozone.shaded...BCECPublicKey}}|
|master (with HDDS-17353)|not registered|position 14|OK|

On master, HDDS-17353 removed the registration from the constructor. In the 
2.3.0-SNAPSHOT jar, only \{{CertificateCodec.getCertFactory()}} (PEM 
certificate paths) and \{{SecurityConfig.getProvider()}} (via 
\{{HDDSKeyGenerator}}) still register BC. A bytecode scan of the jar finds no 
caller of either from client code. The bug is therefore gone for today's client 
paths, but the mechanism is still there:
* calling \{{SecurityConfig.initSecurityProvider("BC")}} with the 
2.3.0-SNAPSHOT jar still registers the relocated provider globally, with the 
same effects as above;
* any future client path that reaches a by-name BC lookup brings the bug back, 
and nothing would catch it.

h3. Proposed change

# *Never register a relocated provider globally.* In 
\{{SecurityConfig.initSecurityProvider}}, if 
\{{BouncyCastleProvider.class.getName()}} is not 
\{{org.bouncycastle.jce.provider.BouncyCastleProvider}} (i.e. this is the 
shaded jar), don't touch \{{java.security.Security}}. Keep a private 
\{{BouncyCastleProvider}} instance and resolve \{{provider}} to it. With 
unrelocated BC (servers, the unshaded client) the behaviour doesn't change; the 
server code passes the provider name to bcpkix builders 
(\{{setProvider("BC")}}) and needs the global registration.
# *Look up by instance, not by name, in code shared with the client:*
#* \{{CertificateCodec.getCertFactory()}}: 
\{{CertificateFactory.getInstance("X.509", provider)}};
#* \{{HDDSKeyGenerator}}: \{{KeyPairGenerator.getInstance(algo, provider)}}.
#* Both work the same for the unrelocated case.
# *Regression test against the shaded jar:* initialize an \{{ofs}} file system 
(secure and non-secure configuration) and call 
\{{CertificateCodec.getCertFactory()}}. Check that 
\{{Security.getProvider("BC")}} is still \{{null}}, or not a relocated class, 
and that an application's \{{Security.addProvider(new BouncyCastleProvider())}} 
afterwards returns a position, not \{{-1}}.
# *Release lines:* for 2.2.x, the minimal fix is the constructor part of 
HDDS-17353, which stops \{{new SecurityConfig(conf)}} from registering BC. 
Steps 1 and 2 make it impossible in any version.

*Alternative considered:* stop relocating Bouncy Castle (HDDS-13231). That 
removes the class mismatch, but the application and Ozone would then share one 
BC on the classpath, with version-skew risks of its own, and it's a much bigger 
change. This fix is independent of it.

h3. Compatibility

* *Applications using the shaded filesystem jar:* their own \{{"BC"}} works 
again, whatever the order of initialization.
* *Ozone in the shaded jar:* uses its own BC instance for the few BC operations 
it does, never the application's.
* *Servers and unshaded clients:* no change.
* No wire, proto, config or on-disk changes.

h3. Testing

* The regression test of step 3, run against \{{ozone-filesystem-hadoop3}} (and 
\{{-hadoop2}}).
* *Unit, for \{{SecurityConfig}}:* with unrelocated BC, 
\{{initSecurityProvider("BC")}} still registers \{{"BC"}}, and an existing 
\{{"BC"}} is still reused.
* *Existing security tests:* certificate client, CA server, gRPC TLS. They 
cover the unrelocated path.



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

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to