From 15b8d49dba3eac76be0491e5183a06bdbc312948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Sat, 21 Nov 2020 23:55:48 +0100 Subject: [PATCH] ipv4: have IP_MULTICAST_TTL accept 1-byte buffers this is a common length for this socket option. also denies values greater als 255. Change-Id: I3d480444d655c41e76d3bbc6625f72c1c4eb3808 Reviewed-on: https://review.haiku-os.org/c/haiku/+/3409 Reviewed-by: Adrien Destugues --- .../kernel/network/protocols/ipv4/ipv4.cpp | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/add-ons/kernel/network/protocols/ipv4/ipv4.cpp b/src/add-ons/kernel/network/protocols/ipv4/ipv4.cpp index 6c5f142606..9e539932e1 100644 --- a/src/add-ons/kernel/network/protocols/ipv4/ipv4.cpp +++ b/src/add-ons/kernel/network/protocols/ipv4/ipv4.cpp @@ -792,6 +792,19 @@ get_int_option(void* target, size_t length, int value) } +static status_t +get_char_int_option(void* target, size_t length, int value) +{ + if (length == sizeof(int)) + return user_memcpy(target, &value, sizeof(int)); + if (length == sizeof(unsigned char)) { + unsigned char uvalue = value; + return user_memcpy(target, &uvalue, sizeof(uvalue)); + } + return B_BAD_VALUE; +} + + template static status_t set_int_option(Type &target, const void* _value, size_t length) { @@ -808,6 +821,30 @@ set_int_option(Type &target, const void* _value, size_t length) } +template static status_t +set_char_int_option(Type &target, const void* _value, size_t length) +{ + if (length == sizeof(int)) { + int value; + if (user_memcpy(&value, _value, sizeof(int)) != B_OK) + return B_BAD_ADDRESS; + if (value > 255) + return B_BAD_VALUE; + target = value; + return B_OK; + } + if (length == sizeof(unsigned char)) { + unsigned char value; + if (user_memcpy(&value, _value, sizeof(value)) != B_OK) + return B_BAD_ADDRESS; + + target = value; + return B_OK; + } + return B_BAD_VALUE; +} + + static net_protocol_module_info* receiving_protocol(uint8 protocol) { @@ -1145,7 +1182,7 @@ ipv4_getsockopt(net_protocol* _protocol, int level, int option, void* value, if (option == IP_TOS) return get_int_option(value, *_length, protocol->service_type); if (option == IP_MULTICAST_TTL) { - return get_int_option(value, *_length, + return get_char_int_option(value, *_length, protocol->multicast_time_to_live); } if (option == IP_ADD_MEMBERSHIP @@ -1253,7 +1290,7 @@ ipv4_setsockopt(net_protocol* _protocol, int level, int option, return B_OK; } if (option == IP_MULTICAST_TTL) { - return set_int_option(protocol->multicast_time_to_live, value, + return set_char_int_option(protocol->multicast_time_to_live, value, length); } if (option == IP_ADD_MEMBERSHIP || option == IP_DROP_MEMBERSHIP) {