Sanity check NBS message header values to stop the occasional bogus

source-unknown incomming.
This commit is contained in:
Eric House 2015-05-18 07:52:12 -07:00
parent 49dc8e7d10
commit f08c0c8b5e

View file

@ -74,6 +74,7 @@ public class SMSService extends XWService {
private static final int SMS_PROTO_VERSION = 0; private static final int SMS_PROTO_VERSION = 0;
private static final int MAX_LEN_TEXT = 100; private static final int MAX_LEN_TEXT = 100;
private static final int MAX_LEN_BINARY = 100; private static final int MAX_LEN_BINARY = 100;
private static final int MAX_MSG_COUNT = 16; // 1.6K enough? Should be....
private enum SMSAction { _NONE, private enum SMSAction { _NONE,
INVITE, INVITE,
SEND, SEND,
@ -453,16 +454,17 @@ public class SMSService extends XWService {
das.flush(); das.flush();
byte[] data = bas.toByteArray(); byte[] data = bas.toByteArray();
boolean result = false;
byte[][] msgs = breakAndEncode( data ); byte[][] msgs = breakAndEncode( data );
result = sendBuffers( msgs, phone ); boolean result = null != msgs && sendBuffers( msgs, phone );
return result; return result;
} }
private byte[][] breakAndEncode( byte msg[] ) throws java.io.IOException private byte[][] breakAndEncode( byte msg[] ) throws java.io.IOException
{ {
byte[][] result = null;
int count = (msg.length + (MAX_LEN_BINARY-1)) / MAX_LEN_BINARY; int count = (msg.length + (MAX_LEN_BINARY-1)) / MAX_LEN_BINARY;
byte[][] result = new byte[count][]; if ( count < MAX_MSG_COUNT ) {
result = new byte[count][];
int msgID = ++s_nSent % 0x000000FF; int msgID = ++s_nSent % 0x000000FF;
int start = 0; int start = 0;
@ -483,6 +485,10 @@ public class SMSService extends XWService {
result[ii] = part; result[ii] = part;
start = end; start = end;
} }
} else {
DbgUtils.logf( "breakAndEncode(): msg count %d too large; dropping",
count );
}
return result; return result;
} }
@ -549,17 +555,21 @@ public class SMSService extends XWService {
int count = buffer[3]; int count = buffer[3];
byte[] rest = new byte[buffer.length - 4]; byte[] rest = new byte[buffer.length - 4];
System.arraycopy( buffer, 4, rest, 0, rest.length ); System.arraycopy( buffer, 4, rest, 0, rest.length );
tryAssemble( senderPhone, id, index, count, rest ); if ( tryAssemble( senderPhone, id, index, count, rest ) ) {
sendResult( MultiEvent.SMS_RECEIVE_OK ); sendResult( MultiEvent.SMS_RECEIVE_OK );
} else {
DbgUtils.logf( "SMSService: receiveBuffer(): bogus message from"
+ " phone %s", senderPhone );
}
} }
private void tryAssemble( String senderPhone, int id, int index, private boolean tryAssemble( String senderPhone, int id, int index,
int count, byte[] msg ) int count, byte[] msg )
{ {
if ( index == 0 && count == 1 ) { boolean success = true;
if ( index == 0 && count == 1 ) { // most common case
disAssemble( senderPhone, msg ); disAssemble( senderPhone, msg );
} else { } else if ( count > 0 && count < MAX_MSG_COUNT && index < count ) {
// required? Should always be in main thread. // required? Should always be in main thread.
synchronized( s_partialMsgs ) { synchronized( s_partialMsgs ) {
HashMap<Integer, MsgStore> perPhone = HashMap<Integer, MsgStore> perPhone =
@ -579,7 +589,10 @@ public class SMSService extends XWService {
perPhone.remove( id ); perPhone.remove( id );
} }
} }
} else {
success = false;
} }
return success;
} }
private void disAssemble( String senderPhone, byte[] fullMsg ) private void disAssemble( String senderPhone, byte[] fullMsg )