Skip to content
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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

honkar-jdk
Copy link
Contributor

@honkar-jdk honkar-jdk commented Jan 10, 2025

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.

  • Profile/Device class
  • Color Space
  • Rendering Intent
  • PCS
  • Header Size check (ICC Header Size = 128 bytes)

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

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Warning

 ⚠️ Patch contains a binary file (test/jdk/java/awt/color/ICC_Profile/ValidateICCHeaderData/invalidSRGB.icc)

Issue

  • JDK-8347377: Add validation checks for ICC_Profile header fields (Bug - P3)

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

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 10, 2025

👋 Welcome back honkar! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Jan 10, 2025

@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:

8347377: Add validation checks for ICC_Profile header fields

Reviewed-by: prr, jdv

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 master branch:

  • d9d2e19: 8348365: Bad format string in CLDRDisplayNamesTest
  • 59e7509: 8348301: Remove unused Reference.waitForReferenceProcessing break-ins in tests
  • 605b53e: 8348299: Update List/ItemEventTest/ItemEventTest.java
  • cba0f78: 8348387: Add fixpath if needed for user-supplied tools
  • 44e5cca: 8348391: Keep case if possible for TOPDIR
  • 7460a0a: 8348392: Make claims "other matches are possible" even when that is not true
  • 5cc690d: 8347994: Add additional diagnostics to macOS failure handler to assist with diagnosing MCast test failures
  • c00557f: 8345049: Remove the jmx.tabular.data.hash.map compatibility property
  • 8b46db0: 8345045: Remove the jmx.remote.x.buffer.size JMX notification property
  • 119899b: 8345048: Remove the jmx.extend.open.types compatibility property
  • ... and 200 more: https://git.openjdk.org/jdk/compare/931914af76932c9b91fc9affd55d24b2562c72d2...master

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 master branch, type /integrate in a new comment.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jan 10, 2025
@openjdk
Copy link

openjdk bot commented Jan 10, 2025

@honkar-jdk The following label will be automatically applied to this pull request:

  • client

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.

@mlbridge
Copy link

mlbridge bot commented Jan 10, 2025

@@ -790,6 +791,12 @@ public static ICC_Profile getInstance(byte[] data) {
} catch (CMMException c) {
throw new IllegalArgumentException("Invalid ICC Profile Data");
}

if (p != null) {
Copy link
Member

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.

Copy link
Member

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)

Copy link
Contributor Author

@honkar-jdk honkar-jdk Jan 13, 2025

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.

Copy link
Contributor Author

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");
Copy link
Member

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?

Copy link
Contributor Author

@honkar-jdk honkar-jdk Jan 13, 2025

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.

Copy link
Contributor Author

@honkar-jdk honkar-jdk Jan 13, 2025

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.

Copy link
Contributor

@prrace prrace Jan 13, 2025

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.

Copy link
Member

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.

Copy link
Member

@mrserb mrserb Jan 24, 2025

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?

Copy link
Member

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?

Copy link
Contributor Author

@honkar-jdk honkar-jdk Jan 24, 2025

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).

Copy link
Contributor Author

@honkar-jdk honkar-jdk Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrserb

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.

Copy link
Member

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);
Copy link
Contributor Author

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).

Copy link
Contributor Author

@honkar-jdk honkar-jdk left a 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?

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jan 22, 2025
@honkar-jdk
Copy link
Contributor Author

@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);
Copy link
Member

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) |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int renderingIntent = ((header[index+2] & 0xff) << 8) |
int renderingIntent = ((header[index+2] & 0xff) << 8) |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client [email protected] ready Pull request is ready to be integrated rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

6 participants