1*c8d645caSAndroid Build Coastguard Worker====================== 2*c8d645caSAndroid Build Coastguard WorkerNanopb: Security model 3*c8d645caSAndroid Build Coastguard Worker====================== 4*c8d645caSAndroid Build Coastguard Worker 5*c8d645caSAndroid Build Coastguard Worker.. include :: menu.rst 6*c8d645caSAndroid Build Coastguard Worker 7*c8d645caSAndroid Build Coastguard Worker.. contents :: 8*c8d645caSAndroid Build Coastguard Worker 9*c8d645caSAndroid Build Coastguard Worker 10*c8d645caSAndroid Build Coastguard Worker 11*c8d645caSAndroid Build Coastguard WorkerImportance of security in a Protocol Buffers library 12*c8d645caSAndroid Build Coastguard Worker==================================================== 13*c8d645caSAndroid Build Coastguard WorkerIn the context of protocol buffers, security comes into play when decoding 14*c8d645caSAndroid Build Coastguard Workeruntrusted data. Naturally, if the attacker can modify the contents of a 15*c8d645caSAndroid Build Coastguard Workerprotocol buffers message, he can feed the application any values possible. 16*c8d645caSAndroid Build Coastguard WorkerTherefore the application itself must be prepared to receive untrusted values. 17*c8d645caSAndroid Build Coastguard Worker 18*c8d645caSAndroid Build Coastguard WorkerWhere nanopb plays a part is preventing the attacker from running arbitrary 19*c8d645caSAndroid Build Coastguard Workercode on the target system. Mostly this means that there must not be any 20*c8d645caSAndroid Build Coastguard Workerpossibility to cause buffer overruns, memory corruption or invalid pointers 21*c8d645caSAndroid Build Coastguard Workerby the means of crafting a malicious message. 22*c8d645caSAndroid Build Coastguard Worker 23*c8d645caSAndroid Build Coastguard WorkerDivision of trusted and untrusted data 24*c8d645caSAndroid Build Coastguard Worker====================================== 25*c8d645caSAndroid Build Coastguard WorkerThe following data is regarded as **trusted**. It must be under the control of 26*c8d645caSAndroid Build Coastguard Workerthe application writer. Malicious data in these structures could cause 27*c8d645caSAndroid Build Coastguard Workersecurity issues, such as execution of arbitrary code: 28*c8d645caSAndroid Build Coastguard Worker 29*c8d645caSAndroid Build Coastguard Worker1. Callback, pointer and extension fields in message structures given to 30*c8d645caSAndroid Build Coastguard Worker pb_encode() and pb_decode(). These fields are memory pointers, and are 31*c8d645caSAndroid Build Coastguard Worker generated depending on the message definition in the .proto file. 32*c8d645caSAndroid Build Coastguard Worker2. The automatically generated field definitions, i.e. *pb_field_t* lists. 33*c8d645caSAndroid Build Coastguard Worker3. Contents of the *pb_istream_t* and *pb_ostream_t* structures (this does not 34*c8d645caSAndroid Build Coastguard Worker mean the contents of the stream itself, just the stream definition). 35*c8d645caSAndroid Build Coastguard Worker 36*c8d645caSAndroid Build Coastguard WorkerThe following data is regarded as **untrusted**. Invalid/malicious data in 37*c8d645caSAndroid Build Coastguard Workerthese will cause "garbage in, garbage out" behaviour. It will not cause 38*c8d645caSAndroid Build Coastguard Workerbuffer overflows, information disclosure or other security problems: 39*c8d645caSAndroid Build Coastguard Worker 40*c8d645caSAndroid Build Coastguard Worker1. All data read from *pb_istream_t*. 41*c8d645caSAndroid Build Coastguard Worker2. All fields in message structures, except: 42*c8d645caSAndroid Build Coastguard Worker 43*c8d645caSAndroid Build Coastguard Worker - callbacks (*pb_callback_t* structures) 44*c8d645caSAndroid Build Coastguard Worker - pointer fields (malloc support) and *_count* fields for pointers 45*c8d645caSAndroid Build Coastguard Worker - extensions (*pb_extension_t* structures) 46*c8d645caSAndroid Build Coastguard Worker 47*c8d645caSAndroid Build Coastguard WorkerInvariants 48*c8d645caSAndroid Build Coastguard Worker========== 49*c8d645caSAndroid Build Coastguard WorkerThe following invariants are maintained during operation, even if the 50*c8d645caSAndroid Build Coastguard Workeruntrusted data has been maliciously crafted: 51*c8d645caSAndroid Build Coastguard Worker 52*c8d645caSAndroid Build Coastguard Worker1. Nanopb will never read more than *bytes_left* bytes from *pb_istream_t*. 53*c8d645caSAndroid Build Coastguard Worker2. Nanopb will never write more than *max_size* bytes to *pb_ostream_t*. 54*c8d645caSAndroid Build Coastguard Worker3. Nanopb will never access memory out of bounds of the message structure. 55*c8d645caSAndroid Build Coastguard Worker4. After pb_decode() returns successfully, the message structure will be 56*c8d645caSAndroid Build Coastguard Worker internally consistent: 57*c8d645caSAndroid Build Coastguard Worker 58*c8d645caSAndroid Build Coastguard Worker - The *count* fields of arrays will not exceed the array size. 59*c8d645caSAndroid Build Coastguard Worker - The *size* field of bytes will not exceed the allocated size. 60*c8d645caSAndroid Build Coastguard Worker - All string fields will have null terminator. 61*c8d645caSAndroid Build Coastguard Worker - bool fields will have valid true/false values (since nanopb-0.3.9.4) 62*c8d645caSAndroid Build Coastguard Worker 63*c8d645caSAndroid Build Coastguard Worker5. After pb_encode() returns successfully, the resulting message is a valid 64*c8d645caSAndroid Build Coastguard Worker protocol buffers message. (Except if user-defined callbacks write incorrect 65*c8d645caSAndroid Build Coastguard Worker data.) 66*c8d645caSAndroid Build Coastguard Worker 67*c8d645caSAndroid Build Coastguard WorkerFurther considerations 68*c8d645caSAndroid Build Coastguard Worker====================== 69*c8d645caSAndroid Build Coastguard WorkerEven if the nanopb library is free of any security issues, there are still 70*c8d645caSAndroid Build Coastguard Workerseveral possible attack vectors that the application author must consider. 71*c8d645caSAndroid Build Coastguard WorkerThe following list is not comprehensive: 72*c8d645caSAndroid Build Coastguard Worker 73*c8d645caSAndroid Build Coastguard Worker1. Stack usage may depend on the contents of the message. The message 74*c8d645caSAndroid Build Coastguard Worker definition places an upper bound on how much stack will be used. Tests 75*c8d645caSAndroid Build Coastguard Worker should be run with all fields present, to record the maximum possible 76*c8d645caSAndroid Build Coastguard Worker stack usage. 77*c8d645caSAndroid Build Coastguard Worker2. Callbacks can do anything. The code for the callbacks must be carefully 78*c8d645caSAndroid Build Coastguard Worker checked if they are used with untrusted data. 79*c8d645caSAndroid Build Coastguard Worker3. If using stream input, a maximum size should be set in *pb_istream_t* to 80*c8d645caSAndroid Build Coastguard Worker stop a denial of service attack from using an infinite message. 81*c8d645caSAndroid Build Coastguard Worker4. If using network sockets as streams, a timeout should be set to stop 82*c8d645caSAndroid Build Coastguard Worker denial of service attacks. 83*c8d645caSAndroid Build Coastguard Worker5. If using *malloc()* support, some method of limiting memory use should be 84*c8d645caSAndroid Build Coastguard Worker employed. This can be done by defining custom *pb_realloc()* function. 85*c8d645caSAndroid Build Coastguard Worker Nanopb will properly detect and handle failed memory allocations. 86