4 ms·
So I look at a random source file, and find: https://git.openswitch.net/cgit/openswitch/ops-arpmgrd/tree/src/arpmgrd.c https://git.openswitch.net/cgit/openswit
by Animats 11y ago
So I look at a random source file, and find:
https://git.openswitch.net/cgit/openswitch/ops-arpmgrd/tree/src/arpmgrd.c https://git.openswitch.net/cgit/openswitch/ops-arpmgrd/tree/...
/* Build hash key for a given vrf name and ip address */
static int
get_hash_key(char* vrf_name, char *ip, char *key)
{
sprintf(key, "%s-%s", vrf_name, ip);
return strlen(key);
} /* get_hash_key */
What could possibly go wrong?
Definitely not thinking hard about security over there.
- function_seven 11y agoHa. Kind of like PHP's function name lengths being used as a "hash" back in the day. But, what contexts is this function used in? Is it supposed to be one-way at all? If not, then this is just a simple way of distributing vrf/ip pairs into buckets. Still smells funny, though. EDIT: This comment looks like I'm defending the implementation here. I'm not. Seems like it'd me more uniform to just add the bytes in the vrf/IP pair and mod them with something. Seeing a hyphen in the format string of sprintf() is really weird, considering that just he string's length is being used. Again, assuming this is in no way security-related, then this is just a general, run-of-the-mill WTF.
- Animats 11y agoAn unchecked "sprintf" is a potential buffer overflow, especially in something like this where the input and output string buffers are parameters to the function. I didn't even go looking for bad code. I was just curious to see what language they were using. This was near the top of the first file I looked at. The code looks like C code circa 1990. If a first glance turned up this, what else bad is in there?
- ArkyBeagle 11y agoWhether this is bad in practice depends. It is declared static, so it's just a small helper function, not a major API entry point. In a review, I'd call out requirements for buffer overflow detection outside this function.
- pjmlp 11y agoOh, well another example of the typical C code I have seen at many companies. Just check Herb Sutter's talk at CppCon 2015, to the question who uses static analyzers, about 1% of the audience said yes.