Sitelet https://web.archive.org/web/20201023082340/https://github.com/dnsjava/dnsjava/issues/102
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

RRset.cycle() short overflows #102

Closed
alkoclick opened this issue Apr 29, 2020 · 3 comments · May be fixed by spotify/dns-java#41
Closed

RRset.cycle() short overflows #102

alkoclick opened this issue Apr 29, 2020 · 3 comments · May be fixed by spotify/dns-java#41
Milestone

Comments

@alkoclick
Copy link

@alkoclick alkoclick commented Apr 29, 2020 •

Hey there!

We recently run into the following issue, after upgrading to 3.0.2:

Stacktrace

java.lang.IndexOutOfBoundsException: fromIndex = -1
	at java.base/java.util.AbstractList.subListRangeCheck(AbstractList.java:505)
	at java.base/java.util.ArrayList.subList(ArrayList.java:1137)
	at org.xbill.DNS.RRset.rrs(RRset.java:150)
	at org.xbill.DNS.Lookup.processResponse(Lookup.java:445)
	at org.xbill.DNS.Lookup.lookup(Lookup.java:484)
	at org.xbill.DNS.Lookup.resolve(Lookup.java:543)
	at org.xbill.DNS.Lookup.run(Lookup.java:561)

Cause:

This happens because of:

  private short position;

  ...

    int start = position++ % rrs.size();
    l.addAll(rrs.subList(start, rrs.size()));

Eventually (when position > 32.767) it overflows and goes negative. This leads to a negative modulo because... that's how Java wills it. Sublist then blows up because it always expects positive

Reproducing:

  • Create an RRset
  • Add 2 or more elements
  • call rrs() or rrs(true) more than 32.767 times

Test (uses Junit 5 but feel free to switch it to 4):

package dns;

import org.junit.jupiter.api.Test;
import org.xbill.DNS.Name;
import org.xbill.DNS.RRset;
import org.xbill.DNS.Record;

import java.lang.reflect.Field;
import java.util.ArrayList;

public class RRsetTest {

    @Test
    void cycleBelowShort() throws Exception {
        runSim(100);
    }

    @Test
    void cycleAboveShort() throws Exception {
        runSim(50_000);
    }

    private void runSim(int numOfCalls) throws Exception {
        RRset rRset = new RRset();

        // Sorry for the dirtiness here. I hacked this because
        // I couldn't figure out an easy second sample that would get through validation
        Field rrsField = RRset.class.getDeclaredField("rrs");
        rrsField.setAccessible(true);
        ((ArrayList<Record>) rrsField.get(rRset)).add(Record.newRecord(Name.root, 1, 1));
        ((ArrayList<Record>) rrsField.get(rRset)).add(Record.newRecord(Name.root, 1, 1));

        for (int i = 0; i < numOfCalls; i++) {
            rRset.rrs(true);
        }
    }

}

The cycleAboveShort() method eventually throws.

Suggested solution:

You can either use one of the suggestions in the link for always positive modulo, or an iterator that resets. I like the latter solution much more

Other notices:

  • Cycling does not work as expected on lists > 32.767 elements (though honestly, whatever)

Have a great day!

@ibauersachs
Copy link
Member

@ibauersachs ibauersachs commented Apr 29, 2020

🤦‍♂️
Thanks! Since you already wrote such a nice unit test, would care to put that into a pull request?

@alkoclick
Copy link
Author

@alkoclick alkoclick commented Apr 29, 2020

Sure! I need to get some confirmations first though 🔒 . If it goes well, I'll share the tests and a suggested solution tomorrow

@ibauersachs ibauersachs modified the milestones: v3.1, v3.0.3 May 2, 2020
ibauersachs added a commit that referenced this issue May 6, 2020
Closes #102
@ibauersachs ibauersachs linked a pull request that will close this issue May 15, 2020
@alkoclick
Copy link
Author

@alkoclick alkoclick commented May 15, 2020

Sorry, was still stuck in bureaucracy hell 😞

rschildmeijer added a commit to rschildmeijer/dns-java that referenced this issue Jun 9, 2020
rschildmeijer added a commit to rschildmeijer/dns-java that referenced this issue Jun 9, 2020
rschildmeijer added a commit to rschildmeijer/dns-java that referenced this issue Jun 9, 2020
rschildmeijer added a commit to rschildmeijer/dns-java that referenced this issue Jun 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants
You can’t perform that action at this time.