BYTEORDER.HTON.SEND

Byte order not converted before host-to-network send

There are two main approaches to storing multibyte values: big-endian and little-endian. When data is being transferred between systems that use different methods, multibyte values should be converted from the host byte order to the network byte order, or the reverse. The BYTEORDER checkers look for multibyte values that aren't converted correctly in these situations.

The BYTEORDER.HTON.SEND checker reports cases in which a multibyte value that originates on the host side isn't converted from host to network byte order before it's sent.

Vulnerability and risk

When programs that run under systems with different byte-order methods need to communicate with each other, the failure to convert byte order may lead to unexpected behavior. A type-appropriate conversion function should be used to avoid this problem when transferring data.

Vulnerable code example

Copy
   #include <sys/types.h>
   #include <sys/socket.h>
   #include <netinet/in.h>

   void test_01_send_A(int s, short x) {
         short u = x + 12;
         send(s, &u, sizeof u, 0); // <== error
   }

Klockwork reports BYTEORDER.HTON.SEND at line 7 to show that 'u' is sent, but not converted. In the host-to-network direction, 'u' should be converted to network values before it's sent to the network. If it's not fixed, this situation could result in unexpected program behavior.

Fixed code example

Copy
   #include <sys/types.h>
   #include <sys/socket.h>
   #include <netinet/in.h>

   void test_01_send_B(int s, short x) {
         short u = x + 12;
         short v = htons(u);
         send(s, &v, sizeof v, 0); // ok
   }

In the fixed code example, the value of 'short int' type is converted by a call to function 'htons' before being sent. Sending the correct value type for the network means that the program will operate as intended.

Extension

This checker can be extended. Platform-specific and application-specific information can be added through the Klocwork knowledge base. Configuration is used to describe properties of system functions that perform buffer manipulation. Several platform-specific configurations are provided as part of the standard distribution.

The related C/C++ knowledge base record kinds are:

See Tuning C/C++ analysis for more information.