Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
J
jdk17u
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
einsteinathome
openjdk
jdk17u
Commits
49b1ca94
Commit
49b1ca94
authored
6 months ago
by
Goetz Lindenmaier
Browse files
Options
Downloads
Patches
Plain Diff
8300416: java.security.MessageDigestSpi clone can result in thread-unsafe clones
Backport-of: 2e2e71e1fa326b8d30f018a3e0726bbcd6d24019
parent
ac1f8688
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/java.base/share/classes/java/security/MessageDigestSpi.java
+10
-2
10 additions, 2 deletions
...va.base/share/classes/java/security/MessageDigestSpi.java
test/jdk/java/security/MessageDigest/TestCloneable.java
+54
-4
54 additions, 4 deletions
test/jdk/java/security/MessageDigest/TestCloneable.java
with
64 additions
and
6 deletions
src/java.base/share/classes/java/security/MessageDigestSpi.java
+
10
−
2
View file @
49b1ca94
/*
/*
* Copyright (c) 1997, 202
0
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 202
3
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -204,7 +204,15 @@ public abstract class MessageDigestSpi {
...
@@ -204,7 +204,15 @@ public abstract class MessageDigestSpi {
*/
*/
public
Object
clone
()
throws
CloneNotSupportedException
{
public
Object
clone
()
throws
CloneNotSupportedException
{
if
(
this
instanceof
Cloneable
)
{
if
(
this
instanceof
Cloneable
)
{
return
super
.
clone
();
MessageDigestSpi
o
=
(
MessageDigestSpi
)
super
.
clone
();
if
(
o
.
tempArray
!=
null
)
{
// New byte arrays are allocated when the ByteBuffer argument
// to engineUpdate is not backed by a byte array.
// Here, the newly allocated byte array must also be cloned
// to prevent threads from sharing the same memory.
o
.
tempArray
=
tempArray
.
clone
();
}
return
o
;
}
else
{
}
else
{
throw
new
CloneNotSupportedException
();
throw
new
CloneNotSupportedException
();
}
}
...
...
This diff is collapsed.
Click to expand it.
test/jdk/java/security/MessageDigest/TestCloneable.java
+
54
−
4
View file @
49b1ca94
/*
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020,
2023,
Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -23,12 +23,16 @@
...
@@ -23,12 +23,16 @@
/*
/*
* @test
* @test
* @bug 8246077
* @bug 8246077
8300416
* @summary Make sure that digest spi and the resulting digest impl are
* @summary Make sure that digest spi and the resulting digest impl are
* consistent in the impl of Cloneable interface
* consistent in the impl of Cloneable interface, and that clones do not
* share memory.
* @run testng TestCloneable
* @run testng TestCloneable
*/
*/
import
java.nio.ByteBuffer
;
import
java.security.*
;
import
java.security.*
;
import
java.util.Arrays
;
import
java.util.Random
;
import
java.util.Objects
;
import
java.util.Objects
;
import
org.testng.annotations.DataProvider
;
import
org.testng.annotations.DataProvider
;
import
org.testng.annotations.Test
;
import
org.testng.annotations.Test
;
...
@@ -53,7 +57,7 @@ public class TestCloneable {
...
@@ -53,7 +57,7 @@ public class TestCloneable {
@Test
(
dataProvider
=
"testData"
)
@Test
(
dataProvider
=
"testData"
)
public
void
test
(
String
algo
,
String
provName
)
public
void
test
(
String
algo
,
String
provName
)
throws
NoSuchProviderException
,
NoSuchAlgorithmException
,
throws
NoSuchProviderException
,
NoSuchAlgorithmException
,
CloneNotSupportedException
{
CloneNotSupportedException
,
InterruptedException
{
System
.
out
.
print
(
"Testing "
+
algo
+
" impl from "
+
provName
);
System
.
out
.
print
(
"Testing "
+
algo
+
" impl from "
+
provName
);
Provider
p
=
Security
.
getProvider
(
provName
);
Provider
p
=
Security
.
getProvider
(
provName
);
Provider
.
Service
s
=
p
.
getService
(
"MessageDigest"
,
algo
);
Provider
.
Service
s
=
p
.
getService
(
"MessageDigest"
,
algo
);
...
@@ -71,6 +75,52 @@ public class TestCloneable {
...
@@ -71,6 +75,52 @@ public class TestCloneable {
System
.
out
.
println
(
": NOT Cloneable"
);
System
.
out
.
println
(
": NOT Cloneable"
);
Assert
.
assertThrows
(
CNSE
,
()->
md
.
clone
());
Assert
.
assertThrows
(
CNSE
,
()->
md
.
clone
());
}
}
System
.
out
.
print
(
"Testing "
+
algo
+
" impl from "
+
provName
);
final
var
d1
=
MessageDigest
.
getInstance
(
algo
,
provName
);
final
var
buffer
=
ByteBuffer
.
allocateDirect
(
1024
);
final
var
r
=
new
Random
(
1024
);
fillBuffer
(
r
,
buffer
);
d1
.
update
(
buffer
);
// this statement triggers tempArray allocation
final
var
d2
=
(
MessageDigest
)
d1
.
clone
();
assert
Arrays
.
equals
(
d1
.
digest
(),
d2
.
digest
());
final
var
t1
=
updateThread
(
d1
);
final
var
t2
=
updateThread
(
d2
);
t1
.
join
();
t2
.
join
();
System
.
out
.
println
(
": Shared data check"
);
// Random is producing the same sequence of bytes for each thread,
// and thus each MessageDigest should be equal. When the memory is
// shared, they inevitably overwrite each other's tempArray and
// you get different results.
if
(!
Arrays
.
equals
(
d1
.
digest
(),
d2
.
digest
()))
{
throw
new
AssertionError
(
"digests differ"
);
}
System
.
out
.
println
(
"Test Passed"
);
System
.
out
.
println
(
"Test Passed"
);
}
}
private
static
void
fillBuffer
(
final
Random
r
,
final
ByteBuffer
buffer
)
{
final
byte
[]
bytes
=
new
byte
[
buffer
.
capacity
()];
r
.
nextBytes
(
bytes
);
buffer
.
clear
();
buffer
.
put
(
bytes
);
buffer
.
flip
();
}
public
static
Thread
updateThread
(
final
MessageDigest
d
)
{
final
var
t
=
new
Thread
(()
->
{
final
var
r
=
new
Random
(
1024
);
final
ByteBuffer
buffer
=
ByteBuffer
.
allocateDirect
(
1024
);
for
(
int
i
=
0
;
i
<
1024
;
i
++)
{
fillBuffer
(
r
,
buffer
);
d
.
update
(
buffer
);
}
});
t
.
start
();
return
t
;
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment