summaryrefslogtreecommitdiff
path: root/common/netlinkclient/src/android/net/netlink
diff options
context:
space:
mode:
authorTreehugger Robot <treehugger-gerrit@google.com>2020-04-16 18:24:51 +0000
committerLorenzo Colitti <lorenzo@google.com>2020-04-17 07:46:17 +0000
commit17fd4b42c696edbecb2f931ea521b37b05816c76 (patch)
tree781a5fcfdcae6cca59b55e876814c4ff2e35cc6b /common/netlinkclient/src/android/net/netlink
parent548bf84252ec9de3ba77ce2de598379e44cfa3ab (diff)
Fix parsing a NduseroptMessage inside a NetlinkMessage.
The parsing code inside NduseroptMessage assumed that the message started at the beginning of the buffer. Fix this, and also restore the original endianness and limit of the buffer after parsing. Add tests for this, and make the existing tests a bit more readable. Bug: 153694684 Test: new unit tests Merged-In: Id0b4dc18dd219b4d35846e161022a37c8de3e3bb Change-Id: Id0b4dc18dd219b4d35846e161022a37c8de3e3bb
Diffstat (limited to 'common/netlinkclient/src/android/net/netlink')
-rw-r--r--common/netlinkclient/src/android/net/netlink/NduseroptMessage.java12
1 files changed, 8 insertions, 4 deletions
diff --git a/common/netlinkclient/src/android/net/netlink/NduseroptMessage.java b/common/netlinkclient/src/android/net/netlink/NduseroptMessage.java
index 4940f6e..7b976f1 100644
--- a/common/netlinkclient/src/android/net/netlink/NduseroptMessage.java
+++ b/common/netlinkclient/src/android/net/netlink/NduseroptMessage.java
@@ -66,22 +66,23 @@ public class NduseroptMessage extends NetlinkMessage {
super(header);
// The structure itself.
- buf.order(ByteOrder.nativeOrder());
+ buf.order(ByteOrder.nativeOrder()); // Restored in the finally clause inside parse().
+ final int start = buf.position();
family = buf.get();
buf.get(); // Skip 1 byte of padding.
opts_len = Short.toUnsignedInt(buf.getShort());
ifindex = buf.getInt();
icmp_type = buf.get();
icmp_code = buf.get();
- buf.order(ByteOrder.BIG_ENDIAN);
buf.position(buf.position() + 6); // Skip 6 bytes of padding.
// The ND option.
// Ensure we don't read past opts_len even if the option length is invalid.
// Note that this check is not really necessary since if the option length is not valid,
// this struct won't be very useful to the caller.
+ buf.order(ByteOrder.BIG_ENDIAN);
int oldLimit = buf.limit();
- buf.limit(STRUCT_SIZE + opts_len);
+ buf.limit(start + STRUCT_SIZE + opts_len);
try {
option = NdOption.parse(buf);
} finally {
@@ -89,7 +90,7 @@ public class NduseroptMessage extends NetlinkMessage {
}
// The source address.
- int newPosition = STRUCT_SIZE + opts_len;
+ int newPosition = start + STRUCT_SIZE + opts_len;
if (newPosition >= buf.limit()) {
throw new IllegalArgumentException("ND options extend past end of buffer");
}
@@ -118,6 +119,7 @@ public class NduseroptMessage extends NetlinkMessage {
*/
public static NduseroptMessage parse(@NonNull StructNlMsgHdr header, @NonNull ByteBuffer buf) {
if (buf == null || buf.remaining() < STRUCT_SIZE) return null;
+ ByteOrder oldOrder = buf.order();
try {
return new NduseroptMessage(header, buf);
} catch (IllegalArgumentException | UnknownHostException | BufferUnderflowException e) {
@@ -125,6 +127,8 @@ public class NduseroptMessage extends NetlinkMessage {
// Convention in this package is that null indicates that the option was truncated, so
// callers must already handle it.
return null;
+ } finally {
+ buf.order(oldOrder);
}
}