Add SAFE_REALLOC() helper function to LTP library
PATCH v3 1/6 Add SAFE_REALLOC() helper function to LTP library
---
Sorry for resubmitting so soon, I forgot to include the gitignore/runfile paperwork for the new test. Fixed in patch 6.
Changes since v1: None
include/tst_safe_macros.h | 5 +++++ lib/tst_safe_macros.c | 15 +++++++++++++++ 2 files changed, 20 insertions(+)
include/tst_safe_macros.h +5 -0
@@ -67,6 +67,11 @@ int safe_dup2(const char *file, const int lineno, int oldfd, int newfd);#define SAFE_MALLOC (size) \ safe_malloc(__FILE__, __LINE__, NULL, (size)) +void *safe_realloc (const char *file,const int lineno,void *ptr,size_t size); + +#define SAFE_REALLOC (ptr, size) \ + safe_realloc(__FILE__, __LINE__, (ptr), (size)) +#define SAFE_MKDIR (pathname, mode) \ safe_mkdir(__FILE__, __LINE__, NULL, (pathname), (mode))
lib/tst_safe_macros.c +15 -0
@@ -5,6 +5,7 @@#define _GNU_SOURCE#include <unistd.h> +#include <stdlib.h> #include <errno.h> #include <sched.h> #include <sys/ptrace.h>
@@ -433,6 +434,20 @@ int safe_dup2(const char *file, const int lineno, int oldfd, int newfd)return rval; } +void *safe_realloc (const char *file,const int lineno,void *ptr,size_t size) +{ +void *ret; + + ret =realloc (ptr, size); + +if (!ret) { +tst_brk_ (file, lineno,TBROK |TERRNO , +"realloc(%p, %zu) failed" , ptr, size); + } + +return ret; +} +sighandler_t safe_signal (const char *file,const int lineno,int signum,sighandler_t handler) {
Signature · 1 line
2.31.1
PATCH v3 2/6 Add SAFE_RECV() helper function to LTP library
---
Changes since v1: None
include/safe_net_fn.h | 3 +++ include/tst_safe_net.h | 3 +++ lib/safe_net.c | 25 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+)
include/safe_net_fn.h +3 -0
@@ -47,6 +47,9 @@ ssize_t safe_sendto(const char *file, const int lineno, char len_strict,ssize_t safe_sendmsg (const char *file,const int lineno,size_t msg_len,int sockfd,const struct msghdr *msg,int flags); +ssize_t safe_recv (const char *file,const int lineno,size_t len, +int sockfd,void *buf,size_t size,int flags); +ssize_t safe_recvmsg (const char *file,const int lineno,size_t msg_len,int sockfd,struct msghdr *msg,int flags);
include/tst_safe_net.h +3 -0
@@ -42,6 +42,9 @@#define SAFE_SENDMSG (msg_len, fd, msg, flags) \ safe_sendmsg(__FILE__, __LINE__, msg_len, fd, msg, flags) +#define SAFE_RECV (msg_len, fd, buf, size, flags) \ + safe_recv(__FILE__, __LINE__, (msg_len), (fd), (buf), (size), (flags)) +#define SAFE_RECVMSG (msg_len, fd, msg, flags) \ safe_recvmsg(__FILE__, __LINE__, msg_len, fd, msg, flags)
lib/safe_net.c +25 -0
@@ -273,6 +273,31 @@ ssize_t safe_sendmsg(const char *file, const int lineno, size_t len,return rval; } +ssize_t safe_recv (const char *file,const int lineno,size_t len, +int sockfd,void *buf,size_t size,int flags) +{ +ssize_t rval; + + rval =recv (sockfd, buf, size, flags); + +if (rval ==-1 ) { +tst_brkm_ (file, lineno,TBROK |TERRNO ,NULL , +"recv(%d, %p, %zu, %d) failed" , sockfd, buf, size, + flags); + }else if (rval <0 ) { +tst_brkm_ (file, lineno,TBROK |TERRNO ,NULL , +"Invalid recv(%d, %p, %zu, %d) return value %zd" , + sockfd, buf, size, flags, rval); + }else if (len && (size_t )rval != len) { +tst_brkm_ (file, lineno,TBROK ,NULL , +"recv(%d, %p, %zu, %d) ret(%zd) != len(%zu)" , + sockfd, buf, size, flags, rval, len); + } + +return rval; + +} +ssize_t safe_recvmsg (const char *file,const int lineno,size_t len,int sockfd,struct msghdr *msg,int flags) {
Signature · 1 line
2.31.1
PATCH v3 2/6 Add SAFE_RECV() helper function to LTP library
PATCH v3 3/6 Add SAFE_IOCTL() variant for library code
In reply to Martin DouchaSAFE_IOCTL() cannot be implemented as a function due to the variadic argument but the macro still needs a variant which accepts arbitrary file:line position for error messages as arguments.
---
Changes since v1: New patch
include/tst_safe_macros.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
include/tst_safe_macros.h +5 -2
@@ -211,13 +211,16 @@ pid_t safe_getpgid(const char *file, const int lineno, pid_t pid);#define SAFE_READDIR (dirp) \ safe_readdir(__FILE__, __LINE__, NULL, (dirp)) -#define SAFE_IOCTL (fd, request, ...) \ +#define SAFE_IOCTL_ (file, lineno, fd, request, ...) \ ({int tst_ret_ = ioctl(fd, request, ##__VA_ARGS__); \ tst_ret_ < 0 ? \ - tst_brk(TBROK | TERRNO, \ + tst_brk_((file), (lineno), TBROK | TERRNO, \ "ioctl(%i,%s,...) failed", fd, #request), 0 \ : tst_ret_;}) +#define SAFE_IOCTL (fd, request, ...) \ + SAFE_IOCTL_(__FILE__, __LINE__, (fd), (request), ##__VA_ARGS__) +#define SAFE_FCNTL (fd, cmd, ...) \ ({int tst_ret_ = fcntl(fd, cmd, ##__VA_ARGS__); \ tst_ret_ == -1 ? \
Signature · 1 line
2.31.1
PATCH v3 3/6 Add SAFE_IOCTL() variant for library code
PATCH v3 3/6 Add SAFE_IOCTL() variant for library code
In reply to Martin DouchaPATCH v3 4/6 Add rtnetlink helper library
In reply to Martin DouchaThis library provides simple interface for creating arbitrary rtnetlink messages with complex attributes, sending requests and receiving results.
---
Changes since v1:
- fixed error handling in tst_rtnl_create_context()
- renamed tst_rtnl_free_context() to tst_rtnl_destroy_context()
- switched from select() to poll() in tst_rtnl_wait()
- use tst_rtnl_add_message() for adding NLMSG_DONE
- receive all pending messages in tst_rtnl_recv(), not just one
- use inline struct initialization where possible
include/tst_rtnetlink.h | 106 +++++++++++ lib/tst_rtnetlink.c | 407 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 513 insertions(+) create mode 100644 include/tst_rtnetlink.h create mode 100644 lib/tst_rtnetlink.c
include/tst_rtnetlink.h new file +106 -0
@@ -0,0 +1,106 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later +* Copyright (c) 2021 Linux Test Project +*/ + +#ifndef TST_RTNETLINK_H +#define TST_RTNETLINK_H + +struct tst_rtnl_context ; + +struct tst_rtnl_attr_list { +unsigned short type; +const void *data; +ssize_t len; +const struct tst_rtnl_attr_list *sublist; +}; + +struct tst_rtnl_message { +struct nlmsghdr *header; +struct nlmsgerr *err; +void *payload; +size_t payload_size; +}; + +/* Open a netlink socket */ +struct tst_rtnl_context *tst_rtnl_create_context (const char *file, +const int lineno); +#define RTNL_CREATE_CONTEXT () tst_rtnl_create_context(__FILE__, __LINE__) + +/* Free a tst_rtnl_message array returned by tst_rtnl_recv() */ +void tst_rtnl_free_message (struct tst_rtnl_message *msg); +#define RTNL_FREE_MESSAGE tst_rtnl_free_message + +/* Close netlink socket */ +void tst_rtnl_destroy_context (const char *file,const int lineno, +struct tst_rtnl_context *ctx); +#define RTNL_DESTROY_CONTEXT (ctx) \ + tst_rtnl_destroy_context(__FILE__, __LINE__, (ctx)) + +/* Send all messages in given buffer */ +int tst_rtnl_send (const char *file,const int lineno, +struct tst_rtnl_context *ctx); +#define RTNL_SEND (ctx) tst_rtnl_send(__FILE__, __LINE__, (ctx)) + +/* Send all messages in given buffer and validate kernel response */ +int tst_rtnl_send_validate (const char *file,const int lineno, +struct tst_rtnl_context *ctx); +#define RTNL_SEND_VALIDATE (ctx) \ + tst_rtnl_send_validate(__FILE__, __LINE__, (ctx)) + +/* Wait until data is available for reading from the netlink socket */ +int tst_rtnl_wait (struct tst_rtnl_context *ctx); +#define RTNL_WAIT tst_rtnl_wait + +/* +* Read from netlink socket and return an array of partially parsed messages. +* header == NULL indicates end of array. +*/ +struct tst_rtnl_message *tst_rtnl_recv (const char *file,const int lineno, +struct tst_rtnl_context *ctx); +#define RTNL_RECV (ctx) tst_rtnl_recv(__FILE__, __LINE__, (ctx)) + +/* Add new message to buffer */ +int tst_rtnl_add_message (const char *file,const int lineno, +struct tst_rtnl_context *ctx,const struct nlmsghdr *header, +const void *payload,size_t payload_size); +#define RTNL_ADD_MESSAGE (ctx, header, payload, psize) \ + tst_rtnl_add_message(__FILE__, __LINE__, (ctx), (header), (payload), \ + (psize)) + +/* Add arbitrary attribute to last message */ +int tst_rtnl_add_attr (const char *file,const int lineno, +struct tst_rtnl_context *ctx,unsigned short type,const void *data, +unsigned short len); +#define RTNL_ADD_ATTR (ctx, type, data, len) \ + tst_rtnl_add_attr(__FILE__, __LINE__, (ctx), (type), (data), (len)) + +/* Add string attribute to last message */ +int tst_rtnl_add_attr_string (const char *file,const int lineno, +struct tst_rtnl_context *ctx,unsigned short type,const char *data); +#define RTNL_ADD_ATTR_STRING (ctx, type, data) \ + tst_rtnl_add_attr_string(__FILE__, __LINE__, (ctx), (type), (data)) + +/* +* Add list of arbitrary attributes to last message. The list is terminated +* by attribute with negative length. Nested sublists are supported. +*/ +int tst_rtnl_add_attr_list (const char *file,const int lineno, +struct tst_rtnl_context *ctx,const struct tst_rtnl_attr_list *list); +#define RTNL_ADD_ATTR_LIST (ctx, list) \ + tst_rtnl_add_attr_list(__FILE__, __LINE__, (ctx), (list)) + +/* Check that all sent messages with NLM_F_ACK flag have been acked without +* error. Usage: +* +* tst_rtnl_send(ctx); +* tst_rtnl_wait(ctx); +* response = tst_rtnl_recv(ctx); +* if (!tst_rtnl_check_acks(ctx, response)) { ... } +* tst_rtnl_free_message(response); +*/ +int tst_rtnl_check_acks (const char *file,const int lineno, +struct tst_rtnl_context *ctx,struct tst_rtnl_message *response); +#define RTNL_CHECK_ACKS (ctx, response) \ + tst_rtnl_context(__FILE__, __LINE__, (ctx), (response)) + +#endif /* TST_RTNETLINK_H */
lib/tst_rtnetlink.c new file +407 -0
407 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v3 4/6 Add rtnetlink helper library
Hi Martin,
it'd be great if you (later) add docs from v1 cover letter [1] into doc/test-writing-guidelines.txt.
Kind regards,
Petr
[1] https://patchwork.ozlabs.org/project/ltp/cover/[email protected]/
PATCH v3 4/6 Add rtnetlink helper library
Quoted 6 lines · Petr Vorel
Hi Martin,
it'd be great if you (later) add docs from v1 cover letter [1] into
doc/test-writing-guidelines.txt.[1] https://patchwork.ozlabs.org/project/ltp/cover/[email protected]/
I think we should discuss the documentation structure first. test-writing-guidelines.txt is already too long and unreadable. We should probably split it by topic and put the network management docs into a separate file from the outset since I'm planning to build an entire packetdrill-like userspace TCP stack on top of it in the future.
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH v3 4/6 Add rtnetlink helper library
Quoted 10 lines · Petr Vorel
Quoted 6 lines
Hi Martin,
it'd be great if you (later) add docs from v1 cover letter [1] into
doc/test-writing-guidelines.txt.[1] https://patchwork.ozlabs.org/project/ltp/cover/[email protected]/
I think we should discuss the documentation structure first. test-writing-guidelines.txt is already too long and unreadable.
Agree. I was thinking this week to at least split C and shell API, that would help a bit.
Quoted 3 lines
We should probably split it by topic and put the network management docs into a separate file from the outset since I'm planning to build an entire packetdrill-like userspace TCP stack on top of it in the future.
Thanks a lot!
Kind regards,
Petr
PATCH v3 4/6 Add rtnetlink helper library
In reply to Martin DouchaQuoted 23 lines
This library provides simple interface for creating arbitrary rtnetlink messages with complex attributes, sending requests and receiving results.
Changes since v1:
- fixed error handling intst_rtnl_create_context()
- renamedtst_rtnl_free_context()totst_rtnl_destroy_context()
- switched fromselect()topoll()intst_rtnl_wait()
- usetst_rtnl_add_message()for addingNLMSG_DONE
- receive all pending messages intst_rtnl_recv(), not just one
- use inline struct initialization where possibleinclude/tst_rtnetlink.h | 106 +++++++++++ lib/tst_rtnetlink.c | 407 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 513 insertions(+) create mode 100644 include/tst_rtnetlink.h create mode 100644 lib/tst_rtnetlink.cinclude/tst_rtnetlink.h new file +0
-0
include/tst_rtnetlink.h line 6
Quoted 6 lines
include/tst_rtnetlink.h new file +6
-0@@ -0,0 +1,106 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later +* Copyright (c) 2021 Linux Test Project +*/ + +#ifndef TST_RTNETLINK_H +#define TST_RTNETLINK_H
I guess this header will always be internal, right? (only tst_netdevice.h is going to be used in tests) Otherwise it might need to include headers (<linux/netlink.h>, and also <unistd.h> or <sys/types.h> for ssize_t which are now only in C library sources).
...
include/tst_rtnetlink.h line 14
Quoted 8 lines
include/tst_rtnetlink.h new file +8
-0+int tst_rtnl_add_message (const char *file,const int lineno, +struct tst_rtnl_context *ctx,const struct nlmsghdr *header, +const void *payload,size_t payload_size) +{ +size_t size; +unsigned int extra_flags =0 ; + +if (!tst_rtnl_grow_buffer (file, lineno, ctx,NLMSG_SPACE (payload_size)))
Shouldn't there be an error message?
Or maybe at tst_rtnl_grow_buffer() on if (!buf)
Nice code, thanks!
Kind regards,
Petr
PATCH v3 4/6 Add rtnetlink helper library
Hi!
Quoted 10 lines
lib/tst_rtnetlink.c line 245
Quoted 8 lines
lib/tst_rtnetlink.c new file +8
-0+int tst_rtnl_add_message (const char *file,const int lineno, +struct tst_rtnl_context *ctx,const struct nlmsghdr *header, +const void *payload,size_t payload_size) +{ +size_t size; +unsigned int extra_flags =0 ; + +if (!tst_rtnl_grow_buffer (file, lineno, ctx,NLMSG_SPACE (payload_size)))Shouldn't there be an error message?
Or maybe attst_rtnl_grow_buffer()on if (!buf)
As far as I can tell we will get error message from the safe_realloc()
Quoted 5 lines
Reviewed-by Petr Vorel <[email protected]>Nice code, thanks!
Kind regards,
Petr
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v3 4/6 Add rtnetlink helper library
Quoted 12 lines
Hi!
Quoted 10 lines
lib/tst_rtnetlink.c line 245
Quoted 8 lines
lib/tst_rtnetlink.c new file +8
-0+int tst_rtnl_add_message (const char *file,const int lineno, +struct tst_rtnl_context *ctx,const struct nlmsghdr *header, +const void *payload,size_t payload_size) +{ +size_t size; +unsigned int extra_flags =0 ; + +if (!tst_rtnl_grow_buffer (file, lineno, ctx,NLMSG_SPACE (payload_size)))Shouldn't there be an error message?
Or maybe attst_rtnl_grow_buffer()on if (!buf)
Quoted 1 line
As far as I can tell we will get error message from the
safe_realloc()
Right, thanks for info!
Kind regards,
Petr
Quoted 5 lines
Quoted 5 lines
Reviewed-by Petr Vorel <[email protected]>Nice code, thanks!
Kind regards,
Petr
PATCH v3 4/6 Add rtnetlink helper library
In reply to Petr VorelQuoted 9 lines · Petr Vorel
Quoted 9 lines
include/tst_rtnetlink.h new file +3
-0@@ -0,0 +1,106 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later +* Copyright (c) 2021 Linux Test Project +*/
Quoted 8 lines
include/tst_rtnetlink.h line 6
Quoted 3 lines
include/tst_rtnetlink.h new file +3
-0+ +#ifndef TST_RTNETLINK_H +#define TST_RTNETLINK_H I guess this header will always be internal, right? (only
tst_netdevice.his going to be used in tests) Otherwise it might need to include headers (<linux/netlink.h>, and also <unistd.h> or <sys/types.h> forssize_twhich are now only in C library sources).
No, I expect that someone will use it for rtnetlink tests sooner or later. But I don't think those extra #includes matter all that much. If they get #included in the test itself in the right order, it'll work just fine.
Quoted 11 lines
...
include/tst_rtnetlink.h
Quoted 8 lines
include/tst_rtnetlink.h new file +8
-0+int tst_rtnl_add_message (const char *file,const int lineno, +struct tst_rtnl_context *ctx,const struct nlmsghdr *header, +const void *payload,size_t payload_size) +{ +size_t size; +unsigned int extra_flags =0 ; + +if (!tst_rtnl_grow_buffer (file, lineno, ctx,NLMSG_SPACE (payload_size)))Shouldn't there be an error message?
Or maybe attst_rtnl_grow_buffer()on if (!buf)
As Cyril already pointed out, the only way this call can fail is when safe_realloc() fails and prints a TWARN because we're in cleanup(). There's no need to print a second error message.
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH v3 4/6 Add rtnetlink helper library
Quoted 19 lines · Petr Vorel
Quoted 9 lines
Quoted 9 lines
include/tst_rtnetlink.h new file +3
-0@@ -0,0 +1,106 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later +* Copyright (c) 2021 Linux Test Project +*/ Quoted 8 lines
include/tst_rtnetlink.h line 6
Quoted 3 lines
include/tst_rtnetlink.h new file +3
-0+ +#ifndef TST_RTNETLINK_H +#define TST_RTNETLINK_H I guess this header will always be internal, right? (only
tst_netdevice.his going to be used in tests) Otherwise it might need to include headers (<linux/netlink.h>, and also <unistd.h> or <sys/types.h> forssize_twhich are now only in C library sources).
Quoted 4 lines
No, I expect that someone will use it for rtnetlink tests sooner or later. But I don't think those extra #includes matter all that much. If they get #included in the test itself in the right order, it'll work just fine.
Sure.
Quoted 12 lines
Quoted 11 lines
...
include/tst_rtnetlink.h
Quoted 8 lines
include/tst_rtnetlink.h new file +8
-0+int tst_rtnl_add_message (const char *file,const int lineno, +struct tst_rtnl_context *ctx,const struct nlmsghdr *header, +const void *payload,size_t payload_size) +{ +size_t size; +unsigned int extra_flags =0 ; + +if (!tst_rtnl_grow_buffer (file, lineno, ctx,NLMSG_SPACE (payload_size)))Shouldn't there be an error message?
Or maybe attst_rtnl_grow_buffer()on if (!buf)
Quoted 3 lines
As Cyril already pointed out, the only way this call can fail is when
safe_realloc()fails and prints a TWARN because we're incleanup(). There's no need to print a second error message.
You're right, thanks for info.
Kind regards,
Petr
PATCH v3 4/6 Add rtnetlink helper library
In reply to Martin DouchaPATCH v3 4/6 Add rtnetlink helper library
In reply to Martin DouchaHi,
Quoted 2 lines
This library provides simple interface for creating arbitrary rtnetlink messages with complex attributes, sending requests and receiving results.
I wonder if it'd be good to rewrite route-change-netlink.c to use tst_netdevice.c instead of libmnl (it'd be nice to get rid of libmnl dependency, but on the other hand using it means also testing it).
Kind regards,
Petr
PATCH v3 5/6 Add helper functions for managing network interfaces
In reply to Martin DouchaThe library currently supports:
- creating a virtual ethernet device pair
- removing network interfaces
- enabling or disabling a network interface
- managing interface addresses
- managing routing table entries
- moving network interfaces between network namespaces
---
Changes since v1:
- renamed tst_netdevice_index() to tst_netdev_index_by_name()
- renamed tst_netdevice_activate() to tst_netdev_set_state()
- shortened tst_netdevice_*() to tst_netdev_*()
- use inline struct initialization where possible
- use SAFE_IOCTL_()
- added modify_route_inet() internal helper function
Changes I've decided not to do:
- moving *_address_inet() and *_route_inet() functions to header file
- breaking long lines like if(foo & tst_rtnl_add_attr(...)) before the function call instead of in the argument list, the result would be too long
include/tst_netdevice.h | 118 ++++++++++ lib/tst_netdevice.c | 463 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 581 insertions(+) create mode 100644 include/tst_netdevice.h create mode 100644 lib/tst_netdevice.c
include/tst_netdevice.h new file +118 -0
@@ -0,0 +1,118 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later +* Copyright (c) 2021 Linux Test Project +*/ + +#ifndef TST_NETDEVICE_H +#define TST_NETDEVICE_H + +/* Find device index for given network interface name. */ +int tst_netdev_index_by_name (const char *file,const int lineno, +const char *ifname); +#define NETDEV_INDEX_BY_NAME (ifname) \ + tst_netdev_index_by_name(__FILE__, __LINE__, (ifname)) + +/* Activate or deactivate network interface */ +int tst_netdev_set_state (const char *file,const int lineno, +const char *ifname,int up); +#define NETDEV_SET_STATE (ifname, up) \ + tst_netdev_set_state(__FILE__, __LINE__, (ifname), (up)) + +/* Create a connected pair of virtual network devices */ +int tst_create_veth_pair (const char *file,const int lineno, +const char *ifname1,const char *ifname2); +#define CREATE_VETH_PAIR (ifname1, ifname2) \ + tst_create_veth_pair(__FILE__, __LINE__, (ifname1), (ifname2)) + +int tst_remove_netdev (const char *file,const int lineno,const char *ifname); +#define REMOVE_NETDEV (ifname) tst_remove_netdev(__FILE__, __LINE__, (ifname)) + +int tst_netdev_add_address (const char *file,const int lineno, +const char *ifname,unsigned int family,const void *address, +unsigned int prefix,size_t addrlen,unsigned int flags); +#define NETDEV_ADD_ADDRESS (ifname, family, address, prefix, addrlen, flags) \ + tst_netdev_add_address(__FILE__, __LINE__, (ifname), (family), \ + (address), (prefix), (addrlen), (flags)) + +int tst_netdev_add_address_inet (const char *file,const int lineno, +const char *ifname,in_addr_t address,unsigned int prefix, +unsigned int flags); +#define NETDEV_ADD_ADDRESS_INET (ifname, address, prefix, flags) \ + tst_netdev_add_address_inet(__FILE__, __LINE__, (ifname), (address), \ + (prefix), (flags)) + +int tst_netdev_remove_address (const char *file,const int lineno, +const char *ifname,unsigned int family,const void *address, +size_t addrlen); +#define NETDEV_REMOVE_ADDRESS (ifname, family, address, addrlen) \ + tst_netdev_remove_address(__FILE__, __LINE__, (ifname), (family), \ + (address), (addrlen)) + +int tst_netdev_remove_address_inet (const char *file,const int lineno, +const char *ifname,in_addr_t address); +#define NETDEV_REMOVE_ADDRESS_INET (ifname, address) \ + tst_netdev_remove_address_inet(__FILE__, __LINE__, (ifname), (address)) + +int tst_netdev_change_ns_fd (const char *file,const int lineno, +const char *ifname,int nsfd); +#define NETDEV_CHANGE_NS_FD (ifname, nsfd) \ + tst_netdev_change_ns_fd(__FILE__, __LINE__, (ifname), (nsfd)) + +int tst_netdev_change_ns_pid (const char *file,const int lineno, +const char *ifname,pid_t nspid); +#define NETDEV_CHANGE_NS_PID (ifname, nspid) \ + tst_netdev_change_ns_pid(__FILE__, __LINE__, (ifname), (nspid)) + +/* +* Add new static entry to main routing table. If you specify gateway address, +* the interface name is optional. +*/ +int tst_netdev_add_route (const char *file,const int lineno, +const char *ifname,unsigned int family,const void *srcaddr, +unsigned int srcprefix,size_t srclen,const void *dstaddr, +unsigned int dstprefix,size_t dstlen,const void *gateway, +size_t gatewaylen); +#define NETDEV_ADD_ROUTE (ifname, family, srcaddr, srcprefix, srclen, dstaddr, \ + dstprefix, dstlen, gateway, gatewaylen) \ + tst_netdev_add_route(__FILE__, __LINE__, (ifname), (family), \ + (srcaddr), (srcprefix), (srclen), (dstaddr), (dstprefix), \ + (dstlen), (gateway), (gatewaylen)) + +/* +* Simplified function for adding IPv4 static route. If you set srcprefix +* or dstprefix to 0, the corresponding address will be ignored. Interface +* name is optional if gateway address is non-zero. +*/ +int tst_netdev_add_route_inet (const char *file,const int lineno, +const char *ifname,in_addr_t srcaddr,unsigned int srcprefix, +in_addr_t dstaddr,unsigned int dstprefix,in_addr_t gateway); +#define NETDEV_ADD_ROUTE_INET (ifname, srcaddr, srcprefix, dstaddr, dstprefix, \ + gateway) \ + tst_netdev_add_route_inet(__FILE__, __LINE__, (ifname), (srcaddr), \ + (srcprefix), (dstaddr), (dstprefix), (gateway)) + +/* +* Remove static entry from main routing table. +*/ +int tst_netdev_remove_route (const char *file,const int lineno, +const char *ifname,unsigned int family,const void *srcaddr, +unsigned int srcprefix,size_t srclen,const void *dstaddr, +unsigned int dstprefix,size_t dstlen,const void *gateway, +size_t gatewaylen); +#define NETDEV_REMOVE_ROUTE (ifname, family, srcaddr, srcprefix, srclen, \ + dstaddr, dstprefix, dstlen, gateway, gatewaylen) \ + tst_netdev_remove_route(__FILE__, __LINE__, (ifname), (family), \ + (srcaddr), (srcprefix), (srclen), (dstaddr), (dstprefix), \ + (dstlen), (gateway), (gatewaylen)) + +/* +* Simplified function for removing IPv4 static route. +*/ +int tst_netdev_remove_route_inet (const char *file,const int lineno, +const char *ifname,in_addr_t srcaddr,unsigned int srcprefix, +in_addr_t dstaddr,unsigned int dstprefix,in_addr_t gateway); +#define NETDEV_REMOVE_ROUTE_INET (ifname, srcaddr, srcprefix, dstaddr, \ + dstprefix, gateway) \ + tst_netdev_remove_route_inet(__FILE__, __LINE__, (ifname), (srcaddr), \ + (srcprefix), (dstaddr), (dstprefix), (gateway)) + +#endif /* TST_NETDEVICE_H */
lib/tst_netdevice.c new file +463 -0
463 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v3 5/6 Add helper functions for managing network interfaces
PATCH v3 6/6 Add test for CVE 2020-25705
In reply to Martin DouchaFixes #742
---
Changes since v1: New patch
Changes since v2: Added missing gitignore and runfile entry for the new test
runtest/cve | 1 + testcases/cve/.gitignore | 1 + testcases/cve/cve-2020-25705.c | 262 +++++++++++++++++++++++++++++++++ 3 files changed, 264 insertions(+) create mode 100644 testcases/cve/cve-2020-25705.c
runtest/cve +1 -0
@@ -60,4 +60,5 @@ cve-2019-8912 af_alg07 cve-2020-11494 pty04 cve-2020-14386 sendto03 cve-2020-14416 pty03 +cve-2020-25705 cve-2020-25705 cve-2020-29373 io_uring02
testcases/cve/.gitignore +1 -0
@@ -10,3 +10,4 @@ stack_clash cve-2017-17052 cve-2017-16939 cve-2017-17053 +cve-2020-25705
testcases/cve/cve-2020-25705.c new file +262 -0
262 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v3 6/6 Add test for CVE 2020-25705
Quoted 1 line
Fixes #742
LGTM. Few unimportant comments below.
...
Quoted 9 lines
testcases/cve/cve-2020-25705.c new file +3
-0@@ -0,0 +1,262 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2020 SUSE LLC
testcases/cve/cve-2020-25705.c line 13
Quoted 10 lines
testcases/cve/cve-2020-25705.c new file +10
-0+* Author: Nicolai Stange <[email protected]> +* LTP port: Martin Doucha <[email protected]> +* +* CVE-2020-25705 +* +* Test of ICMP rate limiting behavior that may be abused for DNS cache +* poisoning attack. Send a few batches of 100 packets to a closed UDP port +* and count the ICMP errors. If the number of errors is always the same +* for each batch (not randomized), the system is vulnerable. Send packets +* from multiple IP addresses to bypass per-address ICMP throttling.
We probably turn this into docparse during merge.
Quoted 5 lines
testcases/cve/cve-2020-25705.c new file +5
-0+* +* Fixed in: +* +* commit b38e7819cae946e2edf869e604af1e65a5d241c5 +* Author: Eric Dumazet <[email protected]>
testcases/cve/cve-2020-25705.c line 28
Quoted 10 lines
testcases/cve/cve-2020-25705.c new file +10
-0+* Date: Thu Oct 15 11:42:00 2020 -0700 +* +* icmp: randomize the global rate limiter +*/ + +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <linux/if_addr.h> +#include <linux/errqueue.h>
#include <time.h>
to fix build failure on MUSL:
cve-2020-25705.c:262:1: warning: missing initializer for field 'needs_cmds' of 'struct tst_test' [-Wmissing-field-initializers]
https://travis-ci.org/github/pevik/ltp/jobs/769551152
Quoted 16 lines
testcases/cve/cve-2020-25705.c new file +16
-0+#include <sched.h> +#include <limits.h> +#include "tst_test.h" +#include "tst_netdevice.h" + +#define DSTADDR 0xfa444e02/* 250.68.78.2 */ +#define SRCADDR_BASE 0xfa444e41/* 250.68.78.65 */ +#define SRCADDR_COUNT 50 +#define BATCH_COUNT 8 +#define BUFSIZE 1024 + +static int parentns =-1 , childns =-1 ; +static int fds[SRCADDR_COUNT ]; + +static void setup (void ) +{
...
Quoted 5 lines
testcases/cve/cve-2020-25705.c new file +5
-0+/* +* Create network namespace to hide the destination interface from +* the test process. +*/ + parentns =SAFE_OPEN ("/proc/self/ns/net" ,O_RDONLY );
testcases/cve/cve-2020-25705.c line 75
Quoted 10 lines
testcases/cve/cve-2020-25705.c new file +10
-0+SAFE_UNSHARE (CLONE_NEWNET ); + +/* Do NOT close this FD, or both interfaces will be destroyed */ + childns =SAFE_OPEN ("/proc/self/ns/net" ,O_RDONLY ); + +/* Configure child namespace */ +CREATE_VETH_PAIR ("ltp_veth1" ,"ltp_veth2" ); + addr =DSTADDR ; +NETDEV_ADD_ADDRESS_INET ("ltp_veth2" ,htonl (addr),26 , +IFA_F_NOPREFIXROUTE );
I wonder if it'd be useful *later* (not bothering with it now) to allow tests just declare .needs_netdevice = 1 and have generic network setup done (similarly it's done in tst_net.sh). Or just define addresses a prefixes and do library to do the setup.
testcases/cve/cve-2020-25705.c line 77
Quoted 2 lines
testcases/cve/cve-2020-25705.c new file +2
-0+NETDEV_SET_STATE ("ltp_veth2" ,1 ); + NETDEV_ADD_ROUTE_INET("ltp_veth2", 0, 0, htonl(0xfa444e40), 26,
nit: maybe define 0xfa444e40 (and 0xfa444e00) and 26 as constants?
testcases/cve/cve-2020-25705.c line 83
Quoted 6 lines
testcases/cve/cve-2020-25705.c new file +6
-0+ 0); + +/* Configure parent namespace */ +NETDEV_CHANGE_NS_FD ("ltp_veth1" , parentns); +SAFE_SETNS (parentns,CLONE_NEWNET ); + addr =SRCADDR_BASE ;/* 250.68.78.65 */
nit: maybe repeating the address in the comment is not needed.
Quoted 75 lines
testcases/cve/cve-2020-25705.c new file +74
-0+ +for (i =0 ; i <SRCADDR_COUNT ; i++, addr++) { +NETDEV_ADD_ADDRESS_INET ("ltp_veth1" ,htonl (addr),26 , +IFA_F_NOPREFIXROUTE ); + } + +NETDEV_SET_STATE ("ltp_veth1" ,1 ); +NETDEV_ADD_ROUTE_INET ("ltp_veth1" ,0 ,0 ,htonl (0xfa444e00 ),26 ,0 ); +SAFE_FILE_PRINTF ("/proc/sys/net/ipv4/conf/ltp_veth1/forwarding" ,"1" ); + +/* Open test sockets */ +for (i =0 ; i <SRCADDR_COUNT ; i++) { + ipaddr.sin_addr.s_addr =htonl (SRCADDR_BASE + i); + fds[i] =SAFE_SOCKET (AF_INET ,SOCK_DGRAM ,0 ); +SAFE_SETSOCKOPT_INT (fds[i],IPPROTO_IP ,IP_RECVERR ,1 ); +SAFE_BIND (fds[i], (struct sockaddr *)&ipaddr,sizeof (ipaddr)); + } +} + +static int count_icmp_errors (int fd) +{ +int error_count =0 ; +ssize_t len; +char msgbuf[BUFSIZE ], errbuf[BUFSIZE ]; +struct sockaddr_in addr; +struct cmsghdr *cmsg; +struct sock_extended_err exterr; +struct iovec iov = { + .iov_base = msgbuf, + .iov_len =BUFSIZE + }; + +while (1 ) { +struct msghdr msg = { + .msg_name = (struct sockaddr *)&addr, + .msg_namelen =sizeof (addr), + .msg_iov = &iov, + .msg_iovlen =1 , + .msg_flags =0 , + .msg_control = errbuf, + .msg_controllen =BUFSIZE + }; + +memset (errbuf,0 ,BUFSIZE ); + errno =0 ; + len =recvmsg (fd, &msg,MSG_ERRQUEUE ); + +if (len ==-1 ) { +if (errno ==EWOULDBLOCK || errno ==EAGAIN ) +break ; + +tst_brk (TBROK |TERRNO ,"recvmsg() failed" ); + } + +if (len <0 ) { +tst_brk (TBROK |TERRNO , +"Invalid recvmsg() return value %zd" , len); + } + +for (cmsg =CMSG_FIRSTHDR (&msg); cmsg; + cmsg =CMSG_NXTHDR (&msg, cmsg)) { +if (cmsg->cmsg_level !=SOL_IP ) +continue ; + +if (cmsg->cmsg_type !=IP_RECVERR ) +continue ; + +memcpy (&exterr,CMSG_DATA (cmsg),sizeof (exterr)); + +if (exterr.ee_origin !=SO_EE_ORIGIN_ICMP ) +tst_brk (TBROK ,"Unexpected non-ICMP error" ); + +if (exterr.ee_errno !=ECONNREFUSED ) { +TST_ERR = exterr.ee_errno;
testcases/cve/cve-2020-25705.c line 167
Quoted 10 lines
testcases/cve/cve-2020-25705.c new file +10
-0+tst_brk (TBROK |TTERRNO , +"Unexpected ICMP error" ); + } + + error_count++; + } + } + +return error_count; +}
FYI I tested the test on several VM. Very old kernel detects problem only on more runs. But given it's 3.16 (and b38e7819cae9 is a fix for 4cdf507d5452 from v3.18-rc1 we can ignore this).
Kind regards,
Petr
# ./cve-2020-25705 tst_kconfig.c:64: TINFO: Parsing kernel config '/boot/config-3.16.0-11-amd64' tst_test.c:1313: TINFO: Timeout per run is 0h 05m 00s cve-2020-25705.c:217: TINFO: Batch 0: Got 85 ICMP errors cve-2020-25705.c:221: TINFO: Batch 1: Got 100 ICMP errors cve-2020-25705.c:230: TPASS: ICMP rate limit is randomized
-i2
# ./cve-2020-25705 -i2 tst_kconfig.c:64: TINFO: Parsing kernel config '/boot/config-3.16.0-11-amd64' tst_test.c:1313: TINFO: Timeout per run is 0h 05m 00s cve-2020-25705.c:217: TINFO: Batch 0: Got 85 ICMP errors cve-2020-25705.c:221: TINFO: Batch 1: Got 100 ICMP errors cve-2020-25705.c:230: TPASS: ICMP rate limit is randomized cve-2020-25705.c:217: TINFO: Batch 0: Got 100 ICMP errors cve-2020-25705.c:221: TINFO: Batch 1: Got 100 ICMP errors cve-2020-25705.c:221: TINFO: Batch 2: Got 100 ICMP errors cve-2020-25705.c:221: TINFO: Batch 3: Got 100 ICMP errors cve-2020-25705.c:221: TINFO: Batch 4: Got 100 ICMP errors cve-2020-25705.c:221: TINFO: Batch 5: Got 100 ICMP errors cve-2020-25705.c:221: TINFO: Batch 6: Got 100 ICMP errors cve-2020-25705.c:221: TINFO: Batch 7: Got 100 ICMP errors cve-2020-25705.c:226: TFAIL: ICMP rate limit not randomized, system is vulnerable
HINT: You _MAY_ be missing kernel fixes, see:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b38e7819cae9
HINT: You _MAY_ be vulnerable to CVE(s), see:
https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-25705
Summary:
passed 1 failed 1 broken 0 skipped 0
warnings 0
PATCH v3 6/6 Add test for CVE 2020-25705
Quoted 4 lines · Petr Vorel
I wonder if it'd be useful *later* (not bothering with it now) to allow tests just declare .needs_netdevice = 1 and have generic network setup done (similarly it's done in
tst_net.sh). Or just define addresses a prefixes and do library to do the setup.
Sounds like a good idea.
Quoted 11 lines
testcases/cve/cve-2020-25705.c line 77
Quoted 3 lines
testcases/cve/cve-2020-25705.c new file +2
-0+NETDEV_SET_STATE ("ltp_veth2" ,1 ); + NETDEV_ADD_ROUTE_INET("ltp_veth2", 0, 0, htonl(0xfa444e40), 26,nit: maybe define
0xfa444e40(and0xfa444e00) and 26 as constants?testcases/cve/cve-2020-25705.c line 83
Quoted 6 lines
testcases/cve/cve-2020-25705.c new file +6
-0+ 0); + +/* Configure parent namespace */ +NETDEV_CHANGE_NS_FD ("ltp_veth1" , parentns); +SAFE_SETNS (parentns,CLONE_NEWNET ); + addr =SRCADDR_BASE ;/* 250.68.78.65 */ nit: maybe repeating the address in the comment is not needed.
Yes, I should fix this and the <time.h> issue. Please push everything up to patch 5 and then I'll resubmit v4 just for this patch.
Quoted 3 lines
FYI I tested the test on several VM. Very old kernel detects problem only on more runs. But given it's 3.16 (and
b38e7819cae9is a fix for4cdf507d5452from v3.18-rc1 we can ignore this).
Pass is expected here. Vanilla v3.16 AFAIK does not have the global ICMP rate limiter which introduced the real vulnerability in the first place.
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH v3 6/6 Add test for CVE 2020-25705
Hi Martin,
Quoted 2 lines
Yes, I should fix this and the <
time.h> issue. Please push everything up to patch 5 and then I'll resubmit v4 just for this patch.
NOTE: <time.h> must be included *before* <linux/errqueue.h> (maybe bug in musl headers).
I suppose Cyril will merge these 4 patches soon.
Quoted 6 lines
Quoted 3 lines
FYI I tested the test on several VM. Very old kernel detects problem only on more runs. But given it's 3.16 (and
b38e7819cae9is a fix for4cdf507d5452from v3.18-rc1 we can ignore this).Pass is expected here. Vanilla v3.16 AFAIK does not have the global ICMP rate limiter which introduced the real vulnerability in the first place.
Sure. I was just surprised to get different result on -i1 and on -i >= 2.
Kind regards,
Petr
PATCH v3 6/6 Add test for CVE 2020-25705
In reply to Martin DouchaHi!
testcases/cve/cve-2020-25705.c
Quoted 1 line
testcases/cve/cve-2020-25705.c +0
-0
Can we please name the file icmp_rate_limit01.c or something more human readable than the cve-2020-25705?
Quoted 193 lines
new file mode 100644
index 000000000..7d6bbafa8testcases/cve/cve-2020-25705.c new file +188
-0@@ -0,0 +1,262 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2020 SUSE LLC +* Author: Nicolai Stange <[email protected]> +* LTP port: Martin Doucha <[email protected]> +* +* CVE-2020-25705 +* +* Test of ICMP rate limiting behavior that may be abused for DNS cache +* poisoning attack. Send a few batches of 100 packets to a closed UDP port +* and count the ICMP errors. If the number of errors is always the same +* for each batch (not randomized), the system is vulnerable. Send packets +* from multiple IP addresses to bypass per-address ICMP throttling. +* +* Fixed in: +* +* commit b38e7819cae946e2edf869e604af1e65a5d241c5 +* Author: Eric Dumazet <[email protected]> +* Date: Thu Oct 15 11:42:00 2020 -0700 +* +* icmp: randomize the global rate limiter +*/ + +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <linux/if_addr.h> +#include <linux/errqueue.h> + +#include <sched.h> +#include <limits.h> +#include "tst_test.h" +#include "tst_netdevice.h" + +#define DSTADDR 0xfa444e02/* 250.68.78.2 */ +#define SRCADDR_BASE 0xfa444e41/* 250.68.78.65 */ +#define SRCADDR_COUNT 50 +#define BATCH_COUNT 8 +#define BUFSIZE 1024 + +static int parentns =-1 , childns =-1 ; +static int fds[SRCADDR_COUNT ]; + +static void setup (void ) +{ +struct sockaddr_in ipaddr = { .sin_family =AF_INET }; +uint32_t addr; +int i; +int real_uid =getuid (); +int real_gid =getgid (); + +for (i =0 ; i <SRCADDR_COUNT ; i++) + fds[i] =-1 ; + +SAFE_UNSHARE (CLONE_NEWUSER ); +SAFE_UNSHARE (CLONE_NEWNET ); +SAFE_FILE_PRINTF ("/proc/self/setgroups" ,"deny" ); +SAFE_FILE_PRINTF ("/proc/self/uid_map" ,"0 %d 1\n" , real_uid); +SAFE_FILE_PRINTF ("/proc/self/gid_map" ,"0 %d 1\n" , real_gid); + +/* +* Create network namespace to hide the destination interface from +* the test process. +*/ + parentns =SAFE_OPEN ("/proc/self/ns/net" ,O_RDONLY ); +SAFE_UNSHARE (CLONE_NEWNET ); + +/* Do NOT close this FD, or both interfaces will be destroyed */ + childns =SAFE_OPEN ("/proc/self/ns/net" ,O_RDONLY ); + +/* Configure child namespace */ +CREATE_VETH_PAIR ("ltp_veth1" ,"ltp_veth2" ); + addr =DSTADDR ; +NETDEV_ADD_ADDRESS_INET ("ltp_veth2" ,htonl (addr),26 , +IFA_F_NOPREFIXROUTE ); +NETDEV_SET_STATE ("ltp_veth2" ,1 ); +NETDEV_ADD_ROUTE_INET ("ltp_veth2" ,0 ,0 ,htonl (0xfa444e40 ),26 , +0 ); + +/* Configure parent namespace */ +NETDEV_CHANGE_NS_FD ("ltp_veth1" , parentns); +SAFE_SETNS (parentns,CLONE_NEWNET ); + addr =SRCADDR_BASE ;/* 250.68.78.65 */ + +for (i =0 ; i <SRCADDR_COUNT ; i++, addr++) { +NETDEV_ADD_ADDRESS_INET ("ltp_veth1" ,htonl (addr),26 , +IFA_F_NOPREFIXROUTE ); + } + +NETDEV_SET_STATE ("ltp_veth1" ,1 ); +NETDEV_ADD_ROUTE_INET ("ltp_veth1" ,0 ,0 ,htonl (0xfa444e00 ),26 ,0 ); +SAFE_FILE_PRINTF ("/proc/sys/net/ipv4/conf/ltp_veth1/forwarding" ,"1" ); + +/* Open test sockets */ +for (i =0 ; i <SRCADDR_COUNT ; i++) { + ipaddr.sin_addr.s_addr =htonl (SRCADDR_BASE + i); + fds[i] =SAFE_SOCKET (AF_INET ,SOCK_DGRAM ,0 ); +SAFE_SETSOCKOPT_INT (fds[i],IPPROTO_IP ,IP_RECVERR ,1 ); +SAFE_BIND (fds[i], (struct sockaddr *)&ipaddr,sizeof (ipaddr)); + } +} + +static int count_icmp_errors (int fd) +{ +int error_count =0 ; +ssize_t len; +char msgbuf[BUFSIZE ], errbuf[BUFSIZE ]; +struct sockaddr_in addr; +struct cmsghdr *cmsg; +struct sock_extended_err exterr; +struct iovec iov = { + .iov_base = msgbuf, + .iov_len =BUFSIZE + }; + +while (1 ) { +struct msghdr msg = { + .msg_name = (struct sockaddr *)&addr, + .msg_namelen =sizeof (addr), + .msg_iov = &iov, + .msg_iovlen =1 , + .msg_flags =0 , + .msg_control = errbuf, + .msg_controllen =BUFSIZE + }; + +memset (errbuf,0 ,BUFSIZE ); + errno =0 ; + len =recvmsg (fd, &msg,MSG_ERRQUEUE ); + +if (len ==-1 ) { +if (errno ==EWOULDBLOCK || errno ==EAGAIN ) +break ; + +tst_brk (TBROK |TERRNO ,"recvmsg() failed" ); + } + +if (len <0 ) { +tst_brk (TBROK |TERRNO , +"Invalid recvmsg() return value %zd" , len); + } + +for (cmsg =CMSG_FIRSTHDR (&msg); cmsg; + cmsg =CMSG_NXTHDR (&msg, cmsg)) { +if (cmsg->cmsg_level !=SOL_IP ) +continue ; + +if (cmsg->cmsg_type !=IP_RECVERR ) +continue ; + +memcpy (&exterr,CMSG_DATA (cmsg),sizeof (exterr)); + +if (exterr.ee_origin !=SO_EE_ORIGIN_ICMP ) +tst_brk (TBROK ,"Unexpected non-ICMP error" ); + +if (exterr.ee_errno !=ECONNREFUSED ) { +TST_ERR = exterr.ee_errno; +tst_brk (TBROK |TTERRNO , +"Unexpected ICMP error" ); + } + + error_count++; + } + } + +return error_count; +} + +static int packet_batch (const struct sockaddr *addr,socklen_t addrsize) +{ +int i, j, error_count =0 ; +char data =0 ; + +for (i =0 ; i <SRCADDR_COUNT ; i++) { +for (j =0 ; j <2 ; j++) { + error_count +=count_icmp_errors (fds[i]); +TEST (sendto (fds[i], &data,sizeof (data),0 , addr, + addrsize)); + +if (TST_RET ==-1 ) { +if (TST_ERR ==ECONNREFUSED ) { + j--;/* flush ICMP errors and retry */ +continue ; + } + +tst_brk (TBROK |TTERRNO ,"sento() failed" ); + } +
testcases/cve/cve-2020-25705.c line 198
Quoted 10 lines
testcases/cve/cve-2020-25705.c new file +10
-0+if (TST_RET <0 ) { +tst_brk (TBROK |TTERRNO , +"Invalid sento() return value %ld" , +TST_RET ); + } + } + } + +/* Wait and collect pending ICMP errors */ +sleep (2 );
Can we do something better than sleep() here?
Maybe just loop over the loop that reads errors with some short sleep and exit if we haven't got any new errors for a while?
Something as:
unsigned int timeout_ms = 2000;
unsigned int no_changes_ms = 0;
while (timeout_ms) {
int last_err_cnt = error_count;
for (i = 0; i < SRCADDR_COUNT; i++)
error_count += count_icmp_errors(fds[i]);
if (last_err_cnt == error_count)
no_changes_ms++;
else
no_changes_ms = 0;
if (no_changes_ms > 500)
break;
usleep(1000);
timeout_ms--;
}Quoted 69 lines
testcases/cve/cve-2020-25705.c new file +63
-0+for (i =0 ; i <SRCADDR_COUNT ; i++) + error_count +=count_icmp_errors (fds[i]); + +return error_count; +} + +static void run (void ) +{ +int i, errors_baseline, errors; +struct sockaddr_in addr = { + .sin_family =AF_INET , + .sin_port =TST_GET_UNUSED_PORT (AF_INET ,SOCK_DGRAM ), + .sin_addr = {htonl (DSTADDR ) } + }; + + errors_baseline =packet_batch ((struct sockaddr *)&addr,sizeof (addr)); + errors = errors_baseline; +tst_res (TINFO ,"Batch 0: Got %d ICMP errors" , errors); + +for (i =1 ; i <BATCH_COUNT && errors == errors_baseline; i++) { + errors =packet_batch ((struct sockaddr *)&addr,sizeof (addr)); +tst_res (TINFO ,"Batch %d: Got %d ICMP errors" , i, errors); + } + +if (errors == errors_baseline) { +tst_res (TFAIL , +"ICMP rate limit not randomized, system is vulnerable" ); +return ; + } + +tst_res (TPASS ,"ICMP rate limit is randomized" ); +} + +static void cleanup (void ) +{ +int i; + +for (i =0 ; i <SRCADDR_COUNT ; i++) +if (fds[i] >=0 ) +SAFE_CLOSE (fds[i]); + +if (childns >=0 ) +SAFE_CLOSE (childns); + +if (parentns >=0 ) +SAFE_CLOSE (parentns); +} + +static struct tst_test test = { + .test_all = run, + .setup = setup, + .cleanup = cleanup, + .needs_kconfigs = (const char *[]) { +"CONFIG_USER_NS=y" , +"CONFIG_NET_NS=y" , +NULL + }, + .tags = (const struct tst_tag []) { + {"linux-git" ,"b38e7819cae9" }, + {"CVE" ,"2020-25705" }, + {} + } +};Signature · 5 lines
2.31.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v3 6/6 Add test for CVE 2020-25705
Hello, Michal and Nicolai,
please see the question about sleep() near the end of this e-mail. This is a new LTP test for the global ICMP rate limiter exploit that can enable the SADDNS attack.
Quoted 5 lines · Cyril Hrubis
Hi!
testcases/cve/cve-2020-25705.c
Quoted 1 line
testcases/cve/cve-2020-25705.c +0
-0Can we please name the file
icmp_rate_limit01.cor something more human readable than the cve-2020-25705?
I'll rename it in v4.
Quoted 226 lines
Quoted 193 lines
new file mode 100644
index 000000000..7d6bbafa8testcases/cve/cve-2020-25705.c new file +188
-0@@ -0,0 +1,262 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2020 SUSE LLC +* Author: Nicolai Stange <[email protected]> +* LTP port: Martin Doucha <[email protected]> +* +* CVE-2020-25705 +* +* Test of ICMP rate limiting behavior that may be abused for DNS cache +* poisoning attack. Send a few batches of 100 packets to a closed UDP port +* and count the ICMP errors. If the number of errors is always the same +* for each batch (not randomized), the system is vulnerable. Send packets +* from multiple IP addresses to bypass per-address ICMP throttling. +* +* Fixed in: +* +* commit b38e7819cae946e2edf869e604af1e65a5d241c5 +* Author: Eric Dumazet <[email protected]> +* Date: Thu Oct 15 11:42:00 2020 -0700 +* +* icmp: randomize the global rate limiter +*/ + +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <linux/if_addr.h> +#include <linux/errqueue.h> + +#include <sched.h> +#include <limits.h> +#include "tst_test.h" +#include "tst_netdevice.h" + +#define DSTADDR 0xfa444e02/* 250.68.78.2 */ +#define SRCADDR_BASE 0xfa444e41/* 250.68.78.65 */ +#define SRCADDR_COUNT 50 +#define BATCH_COUNT 8 +#define BUFSIZE 1024 + +static int parentns =-1 , childns =-1 ; +static int fds[SRCADDR_COUNT ]; + +static void setup (void ) +{ +struct sockaddr_in ipaddr = { .sin_family =AF_INET }; +uint32_t addr; +int i; +int real_uid =getuid (); +int real_gid =getgid (); + +for (i =0 ; i <SRCADDR_COUNT ; i++) + fds[i] =-1 ; + +SAFE_UNSHARE (CLONE_NEWUSER ); +SAFE_UNSHARE (CLONE_NEWNET ); +SAFE_FILE_PRINTF ("/proc/self/setgroups" ,"deny" ); +SAFE_FILE_PRINTF ("/proc/self/uid_map" ,"0 %d 1\n" , real_uid); +SAFE_FILE_PRINTF ("/proc/self/gid_map" ,"0 %d 1\n" , real_gid); + +/* +* Create network namespace to hide the destination interface from +* the test process. +*/ + parentns =SAFE_OPEN ("/proc/self/ns/net" ,O_RDONLY ); +SAFE_UNSHARE (CLONE_NEWNET ); + +/* Do NOT close this FD, or both interfaces will be destroyed */ + childns =SAFE_OPEN ("/proc/self/ns/net" ,O_RDONLY ); + +/* Configure child namespace */ +CREATE_VETH_PAIR ("ltp_veth1" ,"ltp_veth2" ); + addr =DSTADDR ; +NETDEV_ADD_ADDRESS_INET ("ltp_veth2" ,htonl (addr),26 , +IFA_F_NOPREFIXROUTE ); +NETDEV_SET_STATE ("ltp_veth2" ,1 ); +NETDEV_ADD_ROUTE_INET ("ltp_veth2" ,0 ,0 ,htonl (0xfa444e40 ),26 , +0 ); + +/* Configure parent namespace */ +NETDEV_CHANGE_NS_FD ("ltp_veth1" , parentns); +SAFE_SETNS (parentns,CLONE_NEWNET ); + addr =SRCADDR_BASE ;/* 250.68.78.65 */ + +for (i =0 ; i <SRCADDR_COUNT ; i++, addr++) { +NETDEV_ADD_ADDRESS_INET ("ltp_veth1" ,htonl (addr),26 , +IFA_F_NOPREFIXROUTE ); + } + +NETDEV_SET_STATE ("ltp_veth1" ,1 ); +NETDEV_ADD_ROUTE_INET ("ltp_veth1" ,0 ,0 ,htonl (0xfa444e00 ),26 ,0 ); +SAFE_FILE_PRINTF ("/proc/sys/net/ipv4/conf/ltp_veth1/forwarding" ,"1" ); + +/* Open test sockets */ +for (i =0 ; i <SRCADDR_COUNT ; i++) { + ipaddr.sin_addr.s_addr =htonl (SRCADDR_BASE + i); + fds[i] =SAFE_SOCKET (AF_INET ,SOCK_DGRAM ,0 ); +SAFE_SETSOCKOPT_INT (fds[i],IPPROTO_IP ,IP_RECVERR ,1 ); +SAFE_BIND (fds[i], (struct sockaddr *)&ipaddr,sizeof (ipaddr)); + } +} + +static int count_icmp_errors (int fd) +{ +int error_count =0 ; +ssize_t len; +char msgbuf[BUFSIZE ], errbuf[BUFSIZE ]; +struct sockaddr_in addr; +struct cmsghdr *cmsg; +struct sock_extended_err exterr; +struct iovec iov = { + .iov_base = msgbuf, + .iov_len =BUFSIZE + }; + +while (1 ) { +struct msghdr msg = { + .msg_name = (struct sockaddr *)&addr, + .msg_namelen =sizeof (addr), + .msg_iov = &iov, + .msg_iovlen =1 , + .msg_flags =0 , + .msg_control = errbuf, + .msg_controllen =BUFSIZE + }; + +memset (errbuf,0 ,BUFSIZE ); + errno =0 ; + len =recvmsg (fd, &msg,MSG_ERRQUEUE ); + +if (len ==-1 ) { +if (errno ==EWOULDBLOCK || errno ==EAGAIN ) +break ; + +tst_brk (TBROK |TERRNO ,"recvmsg() failed" ); + } + +if (len <0 ) { +tst_brk (TBROK |TERRNO , +"Invalid recvmsg() return value %zd" , len); + } + +for (cmsg =CMSG_FIRSTHDR (&msg); cmsg; + cmsg =CMSG_NXTHDR (&msg, cmsg)) { +if (cmsg->cmsg_level !=SOL_IP ) +continue ; + +if (cmsg->cmsg_type !=IP_RECVERR ) +continue ; + +memcpy (&exterr,CMSG_DATA (cmsg),sizeof (exterr)); + +if (exterr.ee_origin !=SO_EE_ORIGIN_ICMP ) +tst_brk (TBROK ,"Unexpected non-ICMP error" ); + +if (exterr.ee_errno !=ECONNREFUSED ) { +TST_ERR = exterr.ee_errno; +tst_brk (TBROK |TTERRNO , +"Unexpected ICMP error" ); + } + + error_count++; + } + } + +return error_count; +} + +static int packet_batch (const struct sockaddr *addr,socklen_t addrsize) +{ +int i, j, error_count =0 ; +char data =0 ; + +for (i =0 ; i <SRCADDR_COUNT ; i++) { +for (j =0 ; j <2 ; j++) { + error_count +=count_icmp_errors (fds[i]); +TEST (sendto (fds[i], &data,sizeof (data),0 , addr, + addrsize)); + +if (TST_RET ==-1 ) { +if (TST_ERR ==ECONNREFUSED ) { + j--;/* flush ICMP errors and retry */ +continue ; + } + +tst_brk (TBROK |TTERRNO ,"sento() failed" ); + } +testcases/cve/cve-2020-25705.c line 198
Quoted 10 lines
testcases/cve/cve-2020-25705.c new file +10
-0+if (TST_RET <0 ) { +tst_brk (TBROK |TTERRNO , +"Invalid sento() return value %ld" , +TST_RET ); + } + } + } + +/* Wait and collect pending ICMP errors */ +sleep (2 );Can we do something better than
sleep()here?Maybe just loop over the loop that reads errors with some short sleep and exit if we haven't got any new errors for a while?
Something as:
unsigned int timeout_ms = 2000; unsigned int no_changes_ms = 0; while (timeout_ms) { int last_err_cnt = error_count; for (i = 0; i < SRCADDR_COUNT; i++) error_count += count_icmp_errors(fds[i]); if (last_err_cnt == error_count) no_changes_ms++; else no_changes_ms = 0;
Quoted 6 lines
if (no_changes_ms > 500) break; usleep(1000); timeout_ms--; }
Well, that's a really tough question since we have no other way of synchronizing here and we don't even know how many errors we're supposed to get... Let's ask Michal and Nicolai for advice.
Quoted 70 lines
Quoted 69 lines
testcases/cve/cve-2020-25705.c new file +63
-0+for (i =0 ; i <SRCADDR_COUNT ; i++) + error_count +=count_icmp_errors (fds[i]); + +return error_count; +} + +static void run (void ) +{ +int i, errors_baseline, errors; +struct sockaddr_in addr = { + .sin_family =AF_INET , + .sin_port =TST_GET_UNUSED_PORT (AF_INET ,SOCK_DGRAM ), + .sin_addr = {htonl (DSTADDR ) } + }; + + errors_baseline =packet_batch ((struct sockaddr *)&addr,sizeof (addr)); + errors = errors_baseline; +tst_res (TINFO ,"Batch 0: Got %d ICMP errors" , errors); + +for (i =1 ; i <BATCH_COUNT && errors == errors_baseline; i++) { + errors =packet_batch ((struct sockaddr *)&addr,sizeof (addr)); +tst_res (TINFO ,"Batch %d: Got %d ICMP errors" , i, errors); + } + +if (errors == errors_baseline) { +tst_res (TFAIL , +"ICMP rate limit not randomized, system is vulnerable" ); +return ; + } + +tst_res (TPASS ,"ICMP rate limit is randomized" ); +} + +static void cleanup (void ) +{ +int i; + +for (i =0 ; i <SRCADDR_COUNT ; i++) +if (fds[i] >=0 ) +SAFE_CLOSE (fds[i]); + +if (childns >=0 ) +SAFE_CLOSE (childns); + +if (parentns >=0 ) +SAFE_CLOSE (parentns); +} + +static struct tst_test test = { + .test_all = run, + .setup = setup, + .cleanup = cleanup, + .needs_kconfigs = (const char *[]) { +"CONFIG_USER_NS=y" , +"CONFIG_NET_NS=y" , +NULL + }, + .tags = (const struct tst_tag []) { + {"linux-git" ,"b38e7819cae9" }, + {"CVE" ,"2020-25705" }, + {} + } +};Signature · 5 lines
2.31.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH v3 1/6 Add SAFE_REALLOC() helper function to LTP library
In reply to Martin Doucha