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

8282578: AIOOBE in javax.sound.sampled.Clip #9016

Closed
wants to merge 7 commits into from

Conversation

azuev-java
Copy link
Member

@azuev-java azuev-java commented Jun 3, 2022

Add try/catch clause to ignore an exception since it is harmless for we isolated
the massge data before passing it ro processor.
Add test case.


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

Issue

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/9016/head:pull/9016
$ git checkout pull/9016

Update a local copy of the PR:
$ git checkout pull/9016
$ git pull https://git.openjdk.org/jdk pull/9016/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 9016

View PR using the GUI difftool:
$ git pr show -t 9016

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/9016.diff

Surround SysEx message processing block with try/catch allowing MIDI subsystem to
attempt to ingest the rest of the file.

Added test case.
@bridgekeeper
Copy link

bridgekeeper bot commented Jun 3, 2022

👋 Welcome back kizune! 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 openjdk bot added the rfr Pull request is ready for review label Jun 3, 2022
@openjdk
Copy link

openjdk bot commented Jun 3, 2022

@azuev-java 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.

@openjdk openjdk bot added the client client-libs-dev@openjdk.org label Jun 3, 2022
@mlbridge
Copy link

mlbridge bot commented Jun 3, 2022

Webrevs

@victordyakov
Copy link

@azvegint @prrace please review

@mrserb
Copy link
Member

mrserb commented Jun 3, 2022

Probably it is better to check the data length for each sys message and discard it if the data is too small? Ignoring all possible AIOOBE from this large method which calls many other large methods from SoftVoice/SoftTuning may hide some other bugs.

@openjdk
Copy link

openjdk bot commented Jun 3, 2022

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

8282578: AIOOBE in javax.sound.sampled.Clip

Reviewed-by: prr, aivanov, azvegint

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 201 new commits pushed to the master branch:

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 ready Pull request is ready to be integrated label Jun 3, 2022
@azuev-java
Copy link
Member Author

Probably it is better to check the data length for each sys message and discard it if the data is too small? Ignoring all possible AIOOBE from this large method which calls many other large methods from SoftVoice/SoftTuning may hide some other bugs.

No, because determining the correct length of the message will require basically to parse it all, the correct length could be a 2 bytes or hundreds of bytes - in order to determine we have to process the message completely.

@mrserb
Copy link
Member

mrserb commented Jun 4, 2022

No, because determining the correct length of the message will require basically to parse it all, the correct length could be a 2 bytes or hundreds of bytes - in order to determine we have to process the message completely.

There are 64 usages of data in that method most of them use constants like 1/2/3 and other usages are in the loops, both can be easily checked. There is also code like int[] destinations = new int[(data.length - 7) / 2]; which could cause the NegativeArrayException, or probably some others. It s better to check the out of bands access before access than to have a try/catch block for 300 lines of code.

@bridgekeeper
Copy link

bridgekeeper bot commented Jul 3, 2022

@azuev-java This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@AJ1062910
Copy link

AJ1062910 commented Jul 4, 2022

pre submit tests failed*

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 11, 2022

@azuev-java This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 8, 2022

@azuev-java This pull request has been inactive for more than 8 weeks and will now be automatically closed. If you would like to continue working on this pull request in the future, feel free to reopen it! This can be done using the /open pull request command.

@bridgekeeper bridgekeeper bot closed this Sep 8, 2022
@azuev-java
Copy link
Member Author

Just uploaded the new version with proper length analysis so would be thankful for the continuation of the review.

@azuev-java
Copy link
Member Author

/open

@openjdk openjdk bot reopened this Dec 4, 2022
@openjdk
Copy link

openjdk bot commented Dec 4, 2022

@azuev-java This pull request is now open

@azuev-java
Copy link
Member Author

@azvegint Can you please take a look?

int subid2 = data[4] & 0xFF;
switch (subid2) {
case 0x01: // BULK TUNING DUMP (NON-REAL-TIME)
{
// http://www.midi.org/about-midi/tuning.shtml
//if (!checksumOK2(data))
// break;
if (data.length < 406) {
Copy link
Member

Choose a reason for hiding this comment

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

At first glance it looks like a magic numbers to me.

for better readability we could declare r before this check and use 128*3 + r

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense, fixed.

int ll = data[6] & 0xFF;
if (data.length < ll * 4 + 8) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't it be ll * 4 + 7 == ll * 4 + r?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes.

@@ -193,6 +228,9 @@ public void load(byte[] data) {
// Real-Time/REAL-TIME)
{
// http://www.midi.org/about-midi/tuning-scale.shtml
if (data.length < 21) {
Copy link
Member

Choose a reason for hiding this comment

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

< 20?

int[] data = new int[20];
for (int i = 0; i < 12; i++) {
    System.out.println(data[i + 8]);
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. Math is hard.

int[] ranges = new int[(data.length - 7) / 2];
int ix = 0;
for (int j = 6; j < data.length - 1; j += 2) {
destinations[ix] = data[j] & 0xFF;
Copy link
Member

Choose a reason for hiding this comment

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

Here is another possible AIOOOBE

e.g. if data length is 8, we will have destination and ranges length of 0:

int[] data = new int[8];

System.out.println("data len " + data.length);
if (data.length < 7) {
    System.out.println("Prevent");
    return;
}

int newSize = (data.length - 7) / 2;
System.out.println("new size " + newSize);

int[] destinations = new int[newSize];
int[] ranges = new int[newSize];
int ix = 0;
for (int j = 6; j < data.length - 1; j += 2) {
    System.out.println("index %d %d".formatted(j, ix) );
    destinations[ix] = data[j] & 0xFF;
    System.out.println("index " + (j + 1));
    ranges[ix] = data[j + 1] & 0xFF;
    ix++;
}

Same applies to similar cases below.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, i think there are 3 places total with this possibility when increment goes by 2 so fixed them all.

Copy link
Member

Choose a reason for hiding this comment

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

Length check won't help here:

  int[] data = new int[100];
  if (data.length < 8) {
      return;
  }
  int[] destinations = new int[(data.length - 7) / 2];
  int[] ranges = new int[(data.length - 7) / 2];
  int ix = 0;
  for (int j = 6; j < data.length - 1; j += 2) {
      destinations[ix] = data[j] & 0xFF;
      ranges[ix] = data[j + 1] & 0xFF;
      ix++;
  }

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 46 out of bounds for length 46

We might want to add more test cases to the test.

Copy link
Member Author

Choose a reason for hiding this comment

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

Length check won't help here:

I think the problem here is in the original code: the array allocation was reused across 3 places and it reserved buffers for ranges and destinations that should load up the remaining of data from the offset 7, but in first two places are trying to load data from offset 6, which causes buffer overflow. The idea of my new fix here is to create a proper buffers.

Copy link
Member Author

Choose a reason for hiding this comment

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

We might want to add more test cases to the test.

That would be an extensive task, there is a set of the tests being run by third party - and that's them who created the initial issue. I do not think it is practical to bring all the cases they test here. That testing takes a very long time.


case 0x0A: // Key Based Instrument Control
{
if (data.length < 7 || (data[4] & 0xFF) != 01) {
Copy link
Member

Choose a reason for hiding this comment

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

Neat: 0x01

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok.

break;
}
default:
break;
// http://www.midi.org/about-midi/tuning_extens.shtml
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated to change, but looks like those links become unavailable years ago.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. That's the problem referencing the external site for information. Unfortunately the documentation layout on midi site changed and one needs to read trough all of it to figure out the correct path that both relates to the midi-1.0 standard that we support and is not overwritten by some later addendum making it non-informative.

Copy link
Member

Choose a reason for hiding this comment

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

We might want to track this under some issue, if not done already.

Copy link
Member Author

Choose a reason for hiding this comment

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

Created JDK-8298319 to track this issue.

@azuev-java
Copy link
Member Author

/integrate

@openjdk
Copy link

openjdk bot commented Dec 8, 2022

Going to push as commit af8fb7e.
Since your change was applied there have been 201 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Dec 8, 2022
@openjdk openjdk bot closed this Dec 8, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Dec 8, 2022
@openjdk
Copy link

openjdk bot commented Dec 8, 2022

@azuev-java Pushed as commit af8fb7e.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@mrserb
Copy link
Member

mrserb commented Dec 8, 2022

Thank you for removing that large try/catch block. Looks much better.

@aivanov-jdk
Copy link
Member

Thank you for removing that large try/catch block. Looks much better.

I agree, the code is clearer now.

I started reviewing the updated code but I haven't finished it. It's integrated now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client client-libs-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

7 participants