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

Fix bug in SlicedInputStream with zero length #4863

Merged
merged 1 commit into from
Oct 22, 2022

Conversation

andrross
Copy link
Member

Per the contract of InputStream#read(byte[], int, int):

If len is zero, then no bytes are read and 0 is returned

SlicedInputStream had a bug where if a zero length was passed then it would drain all the underlying streams and return -1. This was uncovered by using InputStream#readAllBytes in new code under development. The only existing usage of SlicedInputStream should not be vulnerable to this bug.

I've also added a check for invalid arguments and created tests to ensure the proper exceptions are thrown per the InputStream contract. In the test I've replaced a "readFully" method with an equivalent "readNBytes" that was introduced in Java 11.

Signed-off-by: Andrew Ross andrross@amazon.com

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed per the DCO using --signoff
  • Commit changes are listed out in CHANGELOG.md file (See: Changelog)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@andrross andrross force-pushed the sliced-input-stream-bug branch from 38d0ade to 6156df1 Compare October 21, 2022 00:20
@andrross andrross marked this pull request as ready for review October 21, 2022 00:21
@andrross andrross requested review from a team and reta as code owners October 21, 2022 00:21
@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@dblock
Copy link
Member

dblock commented Oct 21, 2022

Rebase? I think CI is fixed.

@andrross andrross force-pushed the sliced-input-stream-bug branch from 6156df1 to 2ecfc8a Compare October 21, 2022 15:58
@andrross
Copy link
Member Author

Rebase? I think CI is fixed.

Spotless error. Rookie mistake :) Should be fixed now.

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@@ -100,6 +101,11 @@ public final int read() throws IOException {

@Override
public final int read(byte[] buffer, int offset, int length) throws IOException {
Objects.checkFromIndexSize(offset, length, buffer.length);
if (length == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should it be <= ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Objects.checkFromIndexSize handles the invalid cases, so less than zero will never get here.

Per the contract of InputStream#read(byte[], int, int):

    If len is zero, then no bytes are read and 0 is returned

SlicedInputStream had a bug where if a zero length was passed then it
would drain all the underlying streams and return -1. This was uncovered
by using InputStream#readAllBytes in new code under development. The
only existing usage of SlicedInputStream should not be vulnerable to this
bug.

I've also added a check for invalid arguments and created tests to
ensure the proper exceptions are thrown per the InputStream contract. In
the test I've replaced a "readFully" method with an equivalent
"readNBytes" that was introduced in Java 11.

Signed-off-by: Andrew Ross <andrross@amazon.com>
@andrross andrross force-pushed the sliced-input-stream-bug branch from 2ecfc8a to a1cf4c0 Compare October 22, 2022 00:46
@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@andrross
Copy link
Member Author

Test failure: #4881

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@codecov-commenter
Copy link

Codecov Report

Merging #4863 (a1cf4c0) into main (d85f8cd) will decrease coverage by 1.00%.
The diff coverage is 100.00%.

@@             Coverage Diff              @@
##               main    #4863      +/-   ##
============================================
- Coverage     71.79%   70.79%   -1.01%     
+ Complexity    62907    57861    -5046     
============================================
  Files          4689     4689              
  Lines        300505   276913   -23592     
  Branches      46129    40301    -5828     
============================================
- Hits         215750   196033   -19717     
+ Misses        67829    64681    -3148     
+ Partials      16926    16199     -727     
Impacted Files Coverage Δ
...h/index/snapshots/blobstore/SlicedInputStream.java 82.92% <100.00%> (-6.55%) ⬇️
...adonly/AddIndexBlockClusterStateUpdateRequest.java 0.00% <0.00%> (-75.00%) ⬇️
...luster/routing/allocation/RoutingExplanations.java 41.37% <0.00%> (-58.63%) ⬇️
...readonly/TransportVerifyShardIndexBlockAction.java 9.75% <0.00%> (-58.54%) ⬇️
...n/admin/indices/readonly/AddIndexBlockRequest.java 17.85% <0.00%> (-53.58%) ⬇️
.../java/org/opensearch/node/NodeClosedException.java 50.00% <0.00%> (-50.00%) ⬇️
...a/org/opensearch/tasks/TaskCancelledException.java 50.00% <0.00%> (-50.00%) ⬇️
.../org/opensearch/http/HttpReadTimeoutException.java 50.00% <0.00%> (-50.00%) ⬇️
...cluster/coordination/PendingClusterStateStats.java 20.00% <0.00%> (-48.00%) ⬇️
.../admin/cluster/reroute/ClusterRerouteResponse.java 55.00% <0.00%> (-45.00%) ⬇️
... and 681 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@reta reta merged commit 6571db7 into opensearch-project:main Oct 22, 2022
@andrross andrross added backport 2.x Backport to 2.x branch backport 2.4 Backport to 2.4 branch labels Nov 2, 2022
opensearch-trigger-bot bot pushed a commit that referenced this pull request Nov 2, 2022
Per the contract of InputStream#read(byte[], int, int):

    If len is zero, then no bytes are read and 0 is returned

SlicedInputStream had a bug where if a zero length was passed then it
would drain all the underlying streams and return -1. This was uncovered
by using InputStream#readAllBytes in new code under development. The
only existing usage of SlicedInputStream should not be vulnerable to this
bug.

I've also added a check for invalid arguments and created tests to
ensure the proper exceptions are thrown per the InputStream contract. In
the test I've replaced a "readFully" method with an equivalent
"readNBytes" that was introduced in Java 11.

Signed-off-by: Andrew Ross <andrross@amazon.com>

Signed-off-by: Andrew Ross <andrross@amazon.com>
(cherry picked from commit 6571db7)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md
opensearch-trigger-bot bot pushed a commit that referenced this pull request Nov 2, 2022
Per the contract of InputStream#read(byte[], int, int):

    If len is zero, then no bytes are read and 0 is returned

SlicedInputStream had a bug where if a zero length was passed then it
would drain all the underlying streams and return -1. This was uncovered
by using InputStream#readAllBytes in new code under development. The
only existing usage of SlicedInputStream should not be vulnerable to this
bug.

I've also added a check for invalid arguments and created tests to
ensure the proper exceptions are thrown per the InputStream contract. In
the test I've replaced a "readFully" method with an equivalent
"readNBytes" that was introduced in Java 11.

Signed-off-by: Andrew Ross <andrross@amazon.com>

Signed-off-by: Andrew Ross <andrross@amazon.com>
(cherry picked from commit 6571db7)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md
andrross pushed a commit that referenced this pull request Nov 2, 2022
Per the contract of InputStream#read(byte[], int, int):

    If len is zero, then no bytes are read and 0 is returned

SlicedInputStream had a bug where if a zero length was passed then it
would drain all the underlying streams and return -1. This was uncovered
by using InputStream#readAllBytes in new code under development. The
only existing usage of SlicedInputStream should not be vulnerable to this
bug.

I've also added a check for invalid arguments and created tests to
ensure the proper exceptions are thrown per the InputStream contract. In
the test I've replaced a "readFully" method with an equivalent
"readNBytes" that was introduced in Java 11.

Signed-off-by: Andrew Ross <andrross@amazon.com>

Signed-off-by: Andrew Ross <andrross@amazon.com>
(cherry picked from commit 6571db7)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Signed-off-by: Andrew Ross <andrross@amazon.com>
andrross pushed a commit that referenced this pull request Nov 2, 2022
Per the contract of InputStream#read(byte[], int, int):

    If len is zero, then no bytes are read and 0 is returned

SlicedInputStream had a bug where if a zero length was passed then it
would drain all the underlying streams and return -1. This was uncovered
by using InputStream#readAllBytes in new code under development. The
only existing usage of SlicedInputStream should not be vulnerable to this
bug.

I've also added a check for invalid arguments and created tests to
ensure the proper exceptions are thrown per the InputStream contract. In
the test I've replaced a "readFully" method with an equivalent
"readNBytes" that was introduced in Java 11.

Signed-off-by: Andrew Ross <andrross@amazon.com>

Signed-off-by: Andrew Ross <andrross@amazon.com>
(cherry picked from commit 6571db7)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Signed-off-by: Andrew Ross <andrross@amazon.com>
andrross pushed a commit that referenced this pull request Nov 2, 2022
Per the contract of InputStream#read(byte[], int, int):

    If len is zero, then no bytes are read and 0 is returned

SlicedInputStream had a bug where if a zero length was passed then it
would drain all the underlying streams and return -1. This was uncovered
by using InputStream#readAllBytes in new code under development. The
only existing usage of SlicedInputStream should not be vulnerable to this
bug.

I've also added a check for invalid arguments and created tests to
ensure the proper exceptions are thrown per the InputStream contract. In
the test I've replaced a "readFully" method with an equivalent
"readNBytes" that was introduced in Java 11.

Signed-off-by: Andrew Ross <andrross@amazon.com>
(cherry picked from commit 6571db7)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Signed-off-by: Andrew Ross <andrross@amazon.com>
andrross pushed a commit that referenced this pull request Nov 2, 2022
Per the contract of InputStream#read(byte[], int, int):

    If len is zero, then no bytes are read and 0 is returned

SlicedInputStream had a bug where if a zero length was passed then it
would drain all the underlying streams and return -1. This was uncovered
by using InputStream#readAllBytes in new code under development. The
only existing usage of SlicedInputStream should not be vulnerable to this
bug.

I've also added a check for invalid arguments and created tests to
ensure the proper exceptions are thrown per the InputStream contract. In
the test I've replaced a "readFully" method with an equivalent
"readNBytes" that was introduced in Java 11.

Signed-off-by: Andrew Ross <andrross@amazon.com>
(cherry picked from commit 6571db7)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Signed-off-by: Andrew Ross <andrross@amazon.com>
tlfeng pushed a commit to tlfeng/OpenSearch that referenced this pull request Nov 3, 2022
ashking94 pushed a commit to ashking94/OpenSearch that referenced this pull request Nov 7, 2022
Per the contract of InputStream#read(byte[], int, int):

    If len is zero, then no bytes are read and 0 is returned

SlicedInputStream had a bug where if a zero length was passed then it
would drain all the underlying streams and return -1. This was uncovered
by using InputStream#readAllBytes in new code under development. The
only existing usage of SlicedInputStream should not be vulnerable to this
bug.

I've also added a check for invalid arguments and created tests to
ensure the proper exceptions are thrown per the InputStream contract. In
the test I've replaced a "readFully" method with an equivalent
"readNBytes" that was introduced in Java 11.

Signed-off-by: Andrew Ross <andrross@amazon.com>

Signed-off-by: Andrew Ross <andrross@amazon.com>
@andrross andrross deleted the sliced-input-stream-bug branch November 9, 2022 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.x Backport to 2.x branch backport 2.4 Backport to 2.4 branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants