Test for CVE 2023-31248
v4 cover Test for CVE 2023-31248
Add test for CVE 2023-31248. Support for older kernels requires some changes in tst_netlink_check_acks() and a new LAPI header.
Changes tested on kernels 4.4, 4.12 and 5.14.
Martin Doucha (4):
tst_netlink: Add helper functions for handling generic attributes tst_netlink_check_acks(): Stop on first error regardless of ACK
request
Add lapi/nf_tables.h
Add test for CVE 2023-31248
configure.ac | 1 + doc/C-Test-Network-API.asciidoc | 66 +++++--- include/lapi/nf_tables.h | 19 +++ include/tst_netdevice.h | 6 +- include/tst_netlink.h | 38 ++++- lib/tst_netdevice.c | 20 +-- lib/tst_netlink.c | 97 +++++++++++- runtest/cve | 1 + testcases/cve/tcindex01.c | 12 +- testcases/network/iptables/.gitignore | 1 + testcases/network/iptables/Makefile | 2 +- testcases/network/iptables/nft02.c | 216 ++++++++++++++++++++++++++ 12 files changed, 423 insertions(+), 56 deletions(-) create mode 100644 include/lapi/nf_tables.h create mode 100644 testcases/network/iptables/.gitignore create mode 100644 testcases/network/iptables/nft02.c
Signature · 5 lines
2.42.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v4 1/4 tst_netlink: Add helper functions for handling generic attributes
Refactor struct tst_rtnl_attr_list for generic use and add helper functions for handling generic struct nlattr message attributes.
---
Changes since v1: New patch
Changes since v2: None
Changes since v3: Updated docs
NETLINK_ADD_ATTR_STRING() is currently unused but it was added for future use.
doc/C-Test-Network-API.asciidoc | 66 +++++++++++++++++--------- include/tst_netdevice.h | 6 +-- include/tst_netlink.h | 38 ++++++++++++--- lib/tst_netdevice.c | 20 ++++---- lib/tst_netlink.c | 82 ++++++++++++++++++++++++++++++++- testcases/cve/tcindex01.c | 12 ++--- 6 files changed, 176 insertions(+), 48 deletions(-)
doc/C-Test-Network-API.asciidoc +45 -21
@@ -293,11 +293,11 @@ stage. ------------------------------------------------------------------------------- struct tst_netlink_context; -struct tst_rtnl_attr_list { +struct tst_netlink_attr_list { unsigned short type; const void *data; ssize_t len; - const struct tst_rtnl_attr_list *sublist; + const struct tst_netlink_attr_list *sublist; }; struct tst_netlink_message {
@@ -313,12 +313,13 @@ constructing and sending arbitrary messages using the functions described below. Create a new context using +NETLINK_CREATE_CONTEXT()+, then free it using +NETLINK_DESTROY_CONTEXT()+ when you're done with it. -+struct tst_rtnl_attr_list+ is a helper structure for defining complex ++struct tst_netlink_attr_list+ is a helper structure for defining complex rtnetlink message attribute payloads, including nested attribute lists. Every list and sublist defined using this structure is terminated by item with negative +len+. -- +type+ is the attribute type that will be stored in +struct rtattr.rta_type+. +- +type+ is the attribute type that will be stored in +struct nlattr.nla_type+ + or +struct rtattr.rta_type+. - +data+ contains arbitrary attribute payload.
@@ -326,10 +327,10 @@ negative +len+. set +len+ to 0. The last item in a list or sublist must have negative length. - +sublist+ contains a nested attribute list which will be appended after - +data+ as part of the attribute payload. +struct rtattr.rta_len+ will be - calculated automatically with proper alignment, do _not_ add the sublist size - to the +len+ field. If you do not want to add nested attributes, set - +sublist+ to +NULL+. + +data+ as part of the attribute payload. +struct nlattr.nla_len+ or + +struct rtattr.rta_len+ will be calculated automatically with proper + alignment, do _not_ add the sublist size to the +len+ field. If you do not + want to add nested attributes, set +sublist+ to +NULL+. +struct tst_netlink_message+ is a structure holding partially parsed netlink messages received from the kernel. +NETLINK_RECV()+ returns an array of these
@@ -412,24 +413,47 @@ structures with the last item having +NULL+ in the +header+ field. Call and +NETLINK_CHECK_ACKS()+ will not work correctly until the next +NETLINK_SEND()+. +- +int NETLINK_ADD_ATTR(struct tst_netlink_context *ctx, unsigned short type, + const void *data, unsigned short len)+ – Adds new +struct nlattr+ attribute + to the last message in +ctx+ buffer. See +NETLINK_ADD_MESSAGE()+. You need + to provide attribute +type+ which will be stored in +struct nlattr.nla_type+, + optional payload +data+ and payload size +len+ in bytes. If you don't want + to add any payload, set +data+ to +NULL+ and +len+ to 0. Returns 1 on + success, 0 on error. + +- +int NETLINK_ADD_ATTR_STRING(struct tst_netlink_context *ctx, unsigned short + type, const char *data)+ – Adds new +struct nlattr+ string attribute to the + last message in +ctx+ buffer. Parameters and return value are the same as + for +NETLINK_ADD_ATTR()+, except the payload length is calculated using + +strlen()+. + +- +int NETLINK_ADD_ATTR_LIST(struct tst_netlink_context *ctx, const struct + tst_netlink_attr_list *list)+ – Adds a list of +struct nlattr+ attributes + to the last message in +ctx+ buffer. See description of + +struct tst_netlink_attr_list+ and +NETLINK_ADD_MESSAGE()+ above. Returns + the number of added attributes on success (nested attributes are not + counted), -1 on error. + - +int RTNL_ADD_ATTR(struct tst_netlink_context *ctx, unsigned short type, - const void *data, unsigned short len)+ – Adds new attribute to the last - message in +ctx+ buffer. See +NETLINK_ADD_MESSAGE()+. You need to provide - attribute +type+ which will be stored in +struct rtattr.rta_type+, optional - payload +data+ and payload size +len+ in bytes. If you don't want to add any - payload, set +data+ to +NULL+ and +len+ to 0. Returns 1 on success, - 0 on error. + const void *data, unsigned short len)+ – Adds new +struct rtattr+ attribute + to the last message in +ctx+ buffer. See +NETLINK_ADD_MESSAGE()+. You need + to provide attribute +type+ which will be stored in +struct rtattr.rta_type+, + optional payload +data+ and payload size +len+ in bytes. If you don't want + to add any payload, set +data+ to +NULL+ and +len+ to 0. Returns 1 on + success, 0 on error. - +int RTNL_ADD_ATTR_STRING(struct tst_netlink_context *ctx, unsigned short - type, const char *data)+ – Adds new string attribute to the last message - in +ctx+ buffer. Parameters and return value are the same as for - +RTNL_ADD_ATTR()+, except the payload length is calculated using +strlen()+. + type, const char *data)+ – Adds new +struct rtattr+ string attribute to the + last message in +ctx+ buffer. Parameters and return value are the same as + for +RTNL_ADD_ATTR()+, except the payload length is calculated using + +strlen()+. - +int RTNL_ADD_ATTR_LIST(struct tst_netlink_context *ctx, const struct - tst_rtnl_attr_list *list)+ – Adds a list of attributes to the last message - in +ctx+ buffer. See description of +struct tst_rtnl_attr_list+ and - +NETLINK_ADD_MESSAGE()+ above. Returns the number of added attributes on - success (nested attributes are not counted), -1 on error. + tst_netlink_attr_list *list)+ – Adds a list of +struct rtattr+ attributes + to the last message in +ctx+ buffer. See description of + +struct tst_netlink_attr_list+ and +NETLINK_ADD_MESSAGE()+ above. Returns + the number of added attributes on success (nested attributes are not + counted), -1 on error. Example Usage +++++++++++++
include/tst_netdevice.h +3 -3
@@ -132,7 +132,7 @@ int tst_netdev_remove_route_inet(const char *file, const int lineno,int tst_netdev_add_qdisc (const char *file,const int lineno,int strict,const char *ifname,unsigned int family,unsigned int parent,unsigned int handle,const char *qd_kind, -const struct tst_rtnl_attr_list *config); +const struct tst_netlink_attr_list *config);#define NETDEV_ADD_QDISC (ifname, family, parent, handle, qd_kind, config) \ tst_netdev_add_qdisc(__FILE__, __LINE__, 1, (ifname), (family), \ (parent), (handle), (qd_kind), (config))
@@ -154,7 +154,7 @@ int tst_netdev_remove_qdisc(const char *file, const int lineno, int strict,int tst_netdev_add_traffic_class (const char *file,const int lineno,int strict,const char *ifname,unsigned int parent,unsigned int handle,const char *qd_kind, -const struct tst_rtnl_attr_list *config); +const struct tst_netlink_attr_list *config);#define NETDEV_ADD_TRAFFIC_CLASS (ifname, parent, handle, qd_kind, config) \ tst_netdev_add_traffic_class(__FILE__, __LINE__, 1, (ifname), \ (parent), (handle), (qd_kind), (config))
@@ -173,7 +173,7 @@ int tst_netdev_remove_traffic_class(const char *file, const int lineno,int tst_netdev_add_traffic_filter (const char *file,const int lineno,int strict,const char *ifname,unsigned int parent,unsigned int handle,unsigned int protocol,unsigned int priority, -const char *f_kind,const struct tst_rtnl_attr_list *config); +const char *f_kind,const struct tst_netlink_attr_list *config);#define NETDEV_ADD_TRAFFIC_FILTER (ifname, parent, handle, protocol, priority, \ f_kind, config) \ tst_netdev_add_traffic_filter(__FILE__, __LINE__, 1, (ifname), \
include/tst_netlink.h +31 -7
@@ -9,11 +9,11 @@struct tst_netlink_context ; -struct tst_rtnl_attr_list { +struct tst_netlink_attr_list {unsigned short type;const void *data;ssize_t len; -const struct tst_rtnl_attr_list *sublist; +const struct tst_netlink_attr_list *sublist; };struct tst_netlink_message {
@@ -72,25 +72,49 @@ int tst_netlink_add_message(const char *file, const int lineno,tst_netlink_add_message (__FILE__, __LINE__, (ctx), (header), \ (payload), (psize)) -/* Add arbitrary attribute to last message */ +/* Add arbitrary nlattr attribute to last message */ +int tst_netlink_add_attr (const char *file,const int lineno, +struct tst_netlink_context *ctx,unsigned short type,const void *data, +unsigned short len); +#define NETLINK_ADD_ATTR (ctx, type, data, len) \ + tst_netlink_add_attr(__FILE__, __LINE__, (ctx), (type), (data), (len)) + +/* Add string nlattr attribute to last message */ +int tst_netlink_add_attr_string (const char *file,const int lineno, +struct tst_netlink_context *ctx,unsigned short type,const char *data); +#define NETLINK_ADD_ATTR_STRING (ctx, type, data) \ + tst_netlink_add_attr_string(__FILE__, __LINE__, (ctx), (type), (data)) + +/* +* Add list of arbitrary nlattr attributes to last message. The list is +* terminated by attribute with negative length. Nested sublists are supported. +*/ +int tst_netlink_add_attr_list (const char *file,const int lineno, +struct tst_netlink_context *ctx, +const struct tst_netlink_attr_list *list); +#define NETLINK_ADD_ATTR_LIST (ctx, list) \ + tst_netlink_add_attr_list(__FILE__, __LINE__, (ctx), (list)) + +/* Add arbitrary rtattr attribute to last message */ int tst_rtnl_add_attr (const char *file,const int lineno,struct tst_netlink_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 */ +/* Add string rtattr attribute to last message */ int tst_rtnl_add_attr_string (const char *file,const int lineno,struct tst_netlink_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. +* Add list of arbitrary rtattr 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_netlink_context *ctx,const struct tst_rtnl_attr_list *list); +struct tst_netlink_context *ctx, +const struct tst_netlink_attr_list *list);#define RTNL_ADD_ATTR_LIST (ctx, list) \ tst_rtnl_add_attr_list(__FILE__, __LINE__, (ctx), (list))
lib/tst_netdevice.c +10 -10
@@ -105,17 +105,17 @@ int tst_create_veth_pair(const char *file, const int lineno, int strict,int ret;struct ifinfomsg info = { .ifi_family =AF_UNSPEC };struct tst_netlink_context *ctx; -struct tst_rtnl_attr_list peerinfo[] = { +struct tst_netlink_attr_list peerinfo[] = { {IFLA_IFNAME , ifname2,strlen (ifname2) +1 ,NULL }, {0 ,NULL ,-1 ,NULL } }; -struct tst_rtnl_attr_list peerdata[] = { +struct tst_netlink_attr_list peerdata[] = { {VETH_INFO_PEER , &info,sizeof (info), peerinfo}, {0 ,NULL ,-1 ,NULL } }; -struct tst_rtnl_attr_list attrs[] = { +struct tst_netlink_attr_list attrs[] = { {IFLA_IFNAME , ifname1,strlen (ifname1) +1 ,NULL }, - {IFLA_LINKINFO ,NULL ,0 , (const struct tst_rtnl_attr_list []){ + {IFLA_LINKINFO ,NULL ,0 , (const struct tst_netlink_attr_list []){ {IFLA_INFO_KIND ,"veth" ,4 ,NULL }, {IFLA_INFO_DATA ,NULL ,0 , peerdata}, {0 ,NULL ,-1 ,NULL }
@@ -164,9 +164,9 @@ int tst_netdev_add_device(const char *file, const int lineno, int strict,int ret;struct ifinfomsg info = { .ifi_family =AF_UNSPEC };struct tst_netlink_context *ctx; -struct tst_rtnl_attr_list attrs[] = { +struct tst_netlink_attr_list attrs[] = { {IFLA_IFNAME , ifname,strlen (ifname) +1 ,NULL }, - {IFLA_LINKINFO ,NULL ,0 , (const struct tst_rtnl_attr_list []){ + {IFLA_LINKINFO ,NULL ,0 , (const struct tst_netlink_attr_list []){ {IFLA_INFO_KIND , devtype,strlen (devtype),NULL }, {0 ,NULL ,-1 ,NULL } }},
@@ -527,7 +527,7 @@ static int modify_qdisc(const char *file, const int lineno, int strict, const char *object, unsigned int action, unsigned int nl_flags, const char *ifname, unsigned int family, unsigned int parent, unsigned int handle, unsigned int info, const char *qd_kind, - const struct tst_rtnl_attr_list *config) + const struct tst_netlink_attr_list *config) { struct tst_netlink_context *ctx;int ret;
@@ -585,7 +585,7 @@ static int modify_qdisc(const char *file, const int lineno, int strict,int tst_netdev_add_qdisc (const char *file,const int lineno,int strict,const char *ifname,unsigned int family,unsigned int parent,unsigned int handle,const char *qd_kind, -const struct tst_rtnl_attr_list *config) +const struct tst_netlink_attr_list *config) { return modify_qdisc(file, lineno, strict, "queueing discipline", RTM_NEWQDISC, NLM_F_CREATE | NLM_F_EXCL, ifname, family,
@@ -604,7 +604,7 @@ int tst_netdev_remove_qdisc(const char *file, const int lineno, int strict,int tst_netdev_add_traffic_class (const char *file,const int lineno,int strict,const char *ifname,unsigned int parent,unsigned int handle,const char *qd_kind, -const struct tst_rtnl_attr_list *config) +const struct tst_netlink_attr_list *config) { return modify_qdisc(file, lineno, strict, "traffic class", RTM_NEWTCLASS, NLM_F_CREATE | NLM_F_EXCL, ifname, AF_UNSPEC,
@@ -623,7 +623,7 @@ int tst_netdev_remove_traffic_class(const char *file, const int lineno,int tst_netdev_add_traffic_filter (const char *file,const int lineno,int strict,const char *ifname,unsigned int parent,unsigned int handle,unsigned int protocol,unsigned int priority, -const char *f_kind,const struct tst_rtnl_attr_list *config) +const char *f_kind,const struct tst_netlink_attr_list *config) { return modify_qdisc(file, lineno, strict, "traffic filter", RTM_NEWTFILTER, NLM_F_CREATE | NLM_F_EXCL, ifname, AF_UNSPEC,
lib/tst_netlink.c +81 -1
94 lines not on this page · raw message
testcases/cve/tcindex01.c +6 -6
@@ -46,15 +46,15 @@ static const struct tc_htb_glob qd_opt = {static struct tc_htb_opt cls_opt = {};/* htb qdisc and class options */ -static const struct tst_rtnl_attr_list qd_config[] = { - {TCA_OPTIONS ,NULL ,0 , (const struct tst_rtnl_attr_list []){ +static const struct tst_netlink_attr_list qd_config[] = { + {TCA_OPTIONS ,NULL ,0 , (const struct tst_netlink_attr_list []){ {TCA_HTB_INIT , &qd_opt,sizeof (qd_opt),NULL }, {0 ,NULL ,-1 ,NULL } }}, {0 ,NULL ,-1 ,NULL } }; -static const struct tst_rtnl_attr_list cls_config[] = { - {TCA_OPTIONS ,NULL ,0 , (const struct tst_rtnl_attr_list []){ +static const struct tst_netlink_attr_list cls_config[] = { + {TCA_OPTIONS ,NULL ,0 , (const struct tst_netlink_attr_list []){ {TCA_HTB_PARMS , &cls_opt,sizeof (cls_opt),NULL }, {0 ,NULL ,-1 ,NULL } }},
@@ -62,8 +62,8 @@ static const struct tst_rtnl_attr_list cls_config[] = { };/* tcindex filter options */ -static const struct tst_rtnl_attr_list f_config[] = { - {TCA_OPTIONS ,NULL ,0 , (const struct tst_rtnl_attr_list []){ +static const struct tst_netlink_attr_list f_config[] = { + {TCA_OPTIONS ,NULL ,0 , (const struct tst_netlink_attr_list []){ {TCA_TCINDEX_MASK , &mask,sizeof (mask),NULL }, {TCA_TCINDEX_SHIFT , &shift,sizeof (shift),NULL }, {TCA_TCINDEX_CLASSID , &clsid,sizeof (clsid),NULL },
Signature · 5 lines
2.42.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v4 2/4 tst_netlink_check_acks(): Stop on first error regardless of ACK request
In reply to Martin Douchatst_netlink_check_acks() currently ignores errors in response to netlink messages that did not explicitly ask for ack. If a multimessage netlink request fails early, this will lead to TBROK failures due to missing acks. Return the first netlink error even if the corresponding request did not ask for ack.
---
Changes since v2: New patch
lib/tst_netlink.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
lib/tst_netlink.c +8 -7
@@ -455,18 +455,19 @@ int tst_netlink_check_acks(const char *file, const int lineno,if (!(msg->nlmsg_flags &NLM_F_ACK ))continue ; -while (res->header && res->header->nlmsg_seq != msg->nlmsg_seq) +while (res->header && !(res->err && res->err->error) && + res->header->nlmsg_seq != msg->nlmsg_seq) res++; -if (!res->err || res->header->nlmsg_seq != msg->nlmsg_seq) { -tst_brk_ (file, lineno,TBROK , -"No ACK found for Netlink message %u" , - msg->nlmsg_seq); +if (res->err && res->err->error) { + tst_netlink_errno = -res->err->error;return 0 ; } -if (res->err->error) { - tst_netlink_errno = -res->err->error; +if (!res->header || res->header->nlmsg_seq != msg->nlmsg_seq) { +tst_brk_ (file, lineno,TBROK , +"No ACK found for Netlink message %u" , + msg->nlmsg_seq);return 0 ; } }
Signature · 5 lines
2.42.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v4 3/4 Add lapi/nf_tables.h
In reply to Martin Doucha---
Changes since v2: New patch
Changes since v3: Fixed copyright header
configure.ac | 1 + include/lapi/nf_tables.h | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 include/lapi/nf_tables.h
configure.ac +1 -0
@@ -35,6 +35,7 @@ AC_PREFIX_DEFAULT(/opt/ltp) AC_CHECK_DECLS([IFLA_NET_NS_PID],,,[#include <linux/if_link.h>]) AC_CHECK_DECLS([MADV_MERGEABLE],,,[#include <sys/mman.h>]) +AC_CHECK_DECLS([NFTA_CHAIN_ID, NFTA_VERDICT_CHAIN_ID],,,[#include <linux/netfilter/nf_tables.h>]) AC_CHECK_DECLS([PR_CAPBSET_DROP, PR_CAPBSET_READ],,,[#include <sys/prctl.h>]) AC_CHECK_DECLS([SEM_STAT_ANY],,,[#include <sys/sem.h>])
include/lapi/nf_tables.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Copyright (C) 2023 SUSE LLC <[email protected]> +*/ + +#ifndef LAPI_NF_TABLES_H__ +#define LAPI_NF_TABLES_H__ + +#include <linux/netfilter/nf_tables.h> + +#ifndef HAVE_DECL_NFTA_CHAIN_ID +# define NFTA_CHAIN_ID 11 +#endif + +#ifndef HAVE_DECL_NFTA_VERDICT_CHAIN_ID +# define NFTA_VERDICT_CHAIN_ID 3 +#endif + +#endif /* LAPI_NF_TABLES_H__ */
Signature · 5 lines
2.42.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v4 4/4 Add test for CVE 2023-31248
In reply to Martin DouchaFixes #1058
---
Changes since v1: New patch
Changes since v2:
- Use netfilter GOTO rule jumping into the deleted chain
- Check for ENOENT error instead of kernel taint
Changes since v3: Code style and typo fixes
The test does not use any external utilities so I've decided not to add it to the net.tcp_cmds runfile.
runtest/cve | 1 + testcases/network/iptables/.gitignore | 1 + testcases/network/iptables/Makefile | 2 +- testcases/network/iptables/nft02.c | 216 ++++++++++++++++++++++++++ 4 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 testcases/network/iptables/.gitignore create mode 100644 testcases/network/iptables/nft02.c
runtest/cve +1 -0
@@ -86,6 +86,7 @@ cve-2022-2590 dirtyc0w_shmem cve-2022-23222 bpf_prog07 cve-2023-1829 tcindex01 cve-2023-0461 setsockopt10 +cve-2023-31248 nft02 # Tests below may cause kernel memory leak cve-2020-25704 perf_event_open03 cve-2022-0185 fsconfig03
testcases/network/iptables/.gitignore new file +1 -0
@@ -0,0 +1 @@ +nft02
testcases/network/iptables/Makefile +1 -1
@@ -5,7 +5,7 @@ top_srcdir ?= ../../.. -include $(top_srcdir)/include/mk/env_pre.mk +include $(top_srcdir)/include/mk/testcases.mk INSTALL_TARGETS := *.sh
testcases/network/iptables/nft02.c new file +216 -0
216 lines not on this page · raw message
Signature · 5 lines
2.42.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v4 4/4] Add test for CVE 2023-31248
Hi Martin,
patchset merged, thank you!
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp