-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JDK-8347377 : Add validation checks for ICC_Profile header fields #23044
base: master
Are you sure you want to change the base?
Conversation
👋 Welcome back honkar! A progress list of the required criteria for merging this PR into |
@honkar-jdk This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 210 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
@honkar-jdk The following label will be automatically applied to this pull request:
When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command. |
Webrevs
|
@@ -790,6 +791,12 @@ public static ICC_Profile getInstance(byte[] data) { | |||
} catch (CMMException c) { | |||
throw new IllegalArgumentException("Invalid ICC Profile Data"); | |||
} | |||
|
|||
if (p != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it possible to get null here we should thrown an exception, but I think we thrown that exception already in the native.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is probably better to add this validation into ProfileDataVerifier.verify(data), and check it even before .getModule().loadProfile(data)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This requires making the new method verifyHeader()
public so that it can be used in ProfileDataVerifier.verify(data) as follows.
byte[] theHeader = new byte[HEADER_SIZE];
System.arraycopy(data,0, theHeader, 0, HEADER_SIZE);
ICC_Profile.verifyHeader(theHeader);
or it can be added before .getModule().loadProfile(data) within ICC_Profile.getInstance() and this keeps verifyHeader() private.
public static ICC_Profile getInstance(byte[] data) {
ProfileDataVerifier.verify(data);
Profile p;
try {
byte[] theHeader = new byte[HEADER_SIZE];
System.arraycopy(data, 0, theHeader, 0, HEADER_SIZE);
verifyHeader(theHeader);
p = CMSManager.getModule().loadProfile(data);
} catch (CMMException c) {
throw new IllegalArgumentException("Invalid ICC Profile Data");
}
@prrace Your suggestion on whether to have verifyHeader()
as private or public method? If we decide to make it public then a CSR is required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrserb I have changed it so that verifyHeader is called before CMSManager.getModule().loadProfile(data);
Validation checks cannot be moved to ProfileDataVerifier without making verifyHeader() public.
NOTE: ProfileDataVerifier.verify() verifies entire profile data and not just the header and at this point we have not created a profile yet.
icSaturation, icAbsoluteColorimetric -> { | ||
return true; | ||
} | ||
default -> throw new IllegalArgumentException("Unknown Rendering Intent"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how it is handled by the lcms library? don't we need to ignore unknown intents(and other parameters) and lets lcms decide what to do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrserb
Non-header data are updated using cooked approach (and validated by LCMS) whereas header data are updated using raw LCMS APIs hence require additional validation before setData() is called (On native side it is handled here: setTagDataNative() in LCMS.c).
Without the fix, if invalid rendering intent, PCS, ColorSpace or Device class is updated using setData() it does not throw IAE.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't we need to ignore unknown intents(and other parameters) and lets lcms decide what to do?
Yes, LCMS ignores invalid header data and updates the profile, this can cause exceptions later on for instance when the modified profile is used to create BufferedImage which can be prevented by adding checks in setData()
and restricting updates to only allowed values as specified in ICC Spec Doc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how it is handled by the lcms library? don't we need to ignore unknown intents(and other parameters) and lets lcms decide what to do?
The ICC spec. defines only these 4 intents, so I don't see a problem here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrserb Non-header data are updated using cooked approach (and validated by LCMS) whereas header data are updated using raw LCMS APIs hence require additional validation before setData() is called (On native side it is handled here: setTagDataNative() in LCMS.c).
Then probably we can use approach similar to 8282577: f66070b and try to rely on lcms for validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did the upstream provide any feedback/comments about this validation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW
Typically, the user or application will set the rendering intent dynamically at runtime or embedding time. Therefore, this flag may not have any meaning until the profile is used in some context, e.g. in a DeviceLink or an embedded source profile."
Why this usecase should not be covered by the java? As of the current version of patch it will not be possible to load the profile and then set the intent, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is right. It's not a javase spec, and there may be a reason why the most common library for icc profiles accepts thad data. We shouldn't be more strict than that, it will limit java applications compared to the alternatives.
The path that you mentioned in LCMSTransformfor RenderingIntent involves creating a new color space transform using 2 or more profiles and does not involve creating or updating a profile.
Moreover when a random value is added for rendering intent in LCMSTransform, LCMS throws CMMException. If LCMS validates the Rendering Intent while creating a new color space transform then wouldn't it be better to validate it while creating/updating a ICC_Profile?
And as for the different values specified in ICC_Spec vs LCMS API doc, @prrace confirmed that jdk is required to follow ICC_Spec. In other words the color mgnt engine can have different implementations (LCMS or KCMS) but we need to follow the ICC Specification.
Did the upstream provide any feedback/comments about this validation?
Discussed with @prrace, in this case it was decided that contacting upstream is not necessary since we are choosing to do validations in any case (whether or not LCMS validates).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there may be a reason why the most common library for icc profiles accepts thad data. We shouldn't be more strict than that, it will limit java applications compared to the alternatives.
I see your point but color management engines may have different implementations (LCMS or KCMS) and ICC Specification is a standard.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your point but color management engines may have different implementations (LCMS or KCMS) and ICC Specification is a standard.
I am pretty sure the lcms/kcms follow the standard, because of the next statement in the "ICC.1-2022-05.pdf"
Any colour management system, application, utility or device driver that claims conformance with this ICC
specification shall have the ability to read the profiles as they are defined in this ICC specification. Any
profile-generating software and/or hardware that claims conformance with this ICC specification shall have the
ability to create profiles as they are defined in this ICC specification.
Since you already mentioned "non-ICC intent", please specify the part of the specification in ICC.1-2022-05 where their use is prohibited.
@@ -786,10 +787,15 @@ public static ICC_Profile getInstance(byte[] data) { | |||
ProfileDataVerifier.verify(data); | |||
Profile p; | |||
try { | |||
byte[] theHeader = new byte[HEADER_SIZE]; | |||
System.arraycopy(data, 0, theHeader, 0, HEADER_SIZE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We won't run into ArrayIndexOutOfBoundsException here since the incoming data array size is already being verified in ProfileDataVerifier.verify(data).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrserb Can you please re-review the latest code and comments?
test/jdk/java/awt/color/ICC_Profile/ValidateICCHeaderData/ValidateICCHeaderData.java
Outdated
Show resolved
Hide resolved
@jayathirthrao @aivanov-jdk Can you please have another look and review the updated code when you get a chance? |
if (data == null || data.length < HEADER_SIZE) { | ||
throw new IllegalArgumentException("Invalid header data"); | ||
} | ||
getProfileClass(data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can any of these fields have custom values (not covered by java constants inside iccCStoJCS) that can still be used for color transformation?
* shall be set to zero. Thus, we are ignoring two most significant | ||
* bytes here. Please refer ICC Spec Document for more details. | ||
*/ | ||
int renderingIntent = ((header[index+2] & 0xff) << 8) | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int renderingIntent = ((header[index+2] & 0xff) << 8) | | |
int renderingIntent = ((header[index+2] & 0xff) << 8) | |
ICC_Profile.setData(..) does validation of the specified tag contents and throws an exception if it is not valid. But if the tag represents the header, at least some of the validation is lazy, occurring only when the data is used, leading to unexpected exceptions at a later time. The check should be done up-front when the data is set, as in other cases.
verifyHeader(byte[] data)
is called when header data is being updated and the following fields are validated according to the ICC Spec Document. [1] Pg#19.These validation checks are added to ICC_Profile.getInstance(..) & ICC_Profile.setData(..) methods.
Reference: [1] https://www.color.org/specification/ICC.1-2022-05.pdf
Progress
Warning
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/23044/head:pull/23044
$ git checkout pull/23044
Update a local copy of the PR:
$ git checkout pull/23044
$ git pull https://git.openjdk.org/jdk.git pull/23044/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 23044
View PR using the GUI difftool:
$ git pr show -t 23044
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/23044.diff
Using Webrev
Link to Webrev Comment