netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Cc: davem@davemloft.net, netdev@vger.kernel.org
Subject: [PATCH 08/27] xtables: add xt_match, xt_target and data copy_to_user functions
Date: Fri,  3 Feb 2017 13:25:19 +0100	[thread overview]
Message-ID: <1486124738-3013-9-git-send-email-pablo@netfilter.org> (raw)
In-Reply-To: <1486124738-3013-1-git-send-email-pablo@netfilter.org>

From: Willem de Bruijn <willemb@google.com>

xt_entry_target, xt_entry_match and their private data may contain
kernel data.

Introduce helper functions xt_match_to_user, xt_target_to_user and
xt_data_to_user that copy only the expected fields. These replace
existing logic that calls copy_to_user on entire structs, then
overwrites select fields.

Private data is defined in xt_match and xt_target. All matches and
targets that maintain kernel data store this at the tail of their
private structure. Extend xt_match and xt_target with .usersize to
limit how many bytes of data are copied. The remainder is cleared.

If compatsize is specified, usersize can only safely be used if all
fields up to usersize use platform-independent types. Otherwise, the
compat_to_user callback must be defined.

This patch does not yet enable the support logic.

Signed-off-by: Willem de Bruijn <willemb@google.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 include/linux/netfilter/x_tables.h |  9 +++++++
 net/netfilter/x_tables.c           | 54 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+)

diff --git a/include/linux/netfilter/x_tables.h b/include/linux/netfilter/x_tables.h
index 5117e4d2ddfa..be378cf47fcc 100644
--- a/include/linux/netfilter/x_tables.h
+++ b/include/linux/netfilter/x_tables.h
@@ -167,6 +167,7 @@ struct xt_match {
 
 	const char *table;
 	unsigned int matchsize;
+	unsigned int usersize;
 #ifdef CONFIG_COMPAT
 	unsigned int compatsize;
 #endif
@@ -207,6 +208,7 @@ struct xt_target {
 
 	const char *table;
 	unsigned int targetsize;
+	unsigned int usersize;
 #ifdef CONFIG_COMPAT
 	unsigned int compatsize;
 #endif
@@ -287,6 +289,13 @@ int xt_check_match(struct xt_mtchk_param *, unsigned int size, u_int8_t proto,
 int xt_check_target(struct xt_tgchk_param *, unsigned int size, u_int8_t proto,
 		    bool inv_proto);
 
+int xt_match_to_user(const struct xt_entry_match *m,
+		     struct xt_entry_match __user *u);
+int xt_target_to_user(const struct xt_entry_target *t,
+		      struct xt_entry_target __user *u);
+int xt_data_to_user(void __user *dst, const void *src,
+		    int usersize, int size);
+
 void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
 				 struct xt_counters_info *info, bool compat);
 
diff --git a/net/netfilter/x_tables.c b/net/netfilter/x_tables.c
index 2ff499680cc6..feccf527abdd 100644
--- a/net/netfilter/x_tables.c
+++ b/net/netfilter/x_tables.c
@@ -262,6 +262,60 @@ struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
 }
 EXPORT_SYMBOL_GPL(xt_request_find_target);
 
+
+static int xt_obj_to_user(u16 __user *psize, u16 size,
+			  void __user *pname, const char *name,
+			  u8 __user *prev, u8 rev)
+{
+	if (put_user(size, psize))
+		return -EFAULT;
+	if (copy_to_user(pname, name, strlen(name) + 1))
+		return -EFAULT;
+	if (put_user(rev, prev))
+		return -EFAULT;
+
+	return 0;
+}
+
+#define XT_OBJ_TO_USER(U, K, TYPE, C_SIZE)				\
+	xt_obj_to_user(&U->u.TYPE##_size, C_SIZE ? : K->u.TYPE##_size,	\
+		       U->u.user.name, K->u.kernel.TYPE->name,		\
+		       &U->u.user.revision, K->u.kernel.TYPE->revision)
+
+int xt_data_to_user(void __user *dst, const void *src,
+		    int usersize, int size)
+{
+	usersize = usersize ? : size;
+	if (copy_to_user(dst, src, usersize))
+		return -EFAULT;
+	if (usersize != size && clear_user(dst + usersize, size - usersize))
+		return -EFAULT;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(xt_data_to_user);
+
+#define XT_DATA_TO_USER(U, K, TYPE, C_SIZE)				\
+	xt_data_to_user(U->data, K->data,				\
+			K->u.kernel.TYPE->usersize,			\
+			C_SIZE ? : K->u.kernel.TYPE->TYPE##size)
+
+int xt_match_to_user(const struct xt_entry_match *m,
+		     struct xt_entry_match __user *u)
+{
+	return XT_OBJ_TO_USER(u, m, match, 0) ||
+	       XT_DATA_TO_USER(u, m, match, 0);
+}
+EXPORT_SYMBOL_GPL(xt_match_to_user);
+
+int xt_target_to_user(const struct xt_entry_target *t,
+		      struct xt_entry_target __user *u)
+{
+	return XT_OBJ_TO_USER(u, t, target, 0) ||
+	       XT_DATA_TO_USER(u, t, target, 0);
+}
+EXPORT_SYMBOL_GPL(xt_target_to_user);
+
 static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
 {
 	const struct xt_match *m;
-- 
2.1.4

  parent reply	other threads:[~2017-02-03 12:25 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-03 12:25 [PATCH 00/27] Netfilter updates for net-next Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 01/27] netfilter: merge udp and udplite conntrack helpers Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 02/27] netfilter: nat: merge udp and udplite helpers Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 03/27] netfilter: nf_tables: add missing descriptions in nft_ct_keys Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 04/27] netfilter: nft_ct: add average bytes per packet support Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 05/27] netfilter: select LIBCRC32C together with SCTP conntrack Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 06/27] netfilter: conntrack: validate SCTP crc32c in PREROUTING Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 07/27] netfilter: xt_connlimit: use rb_entry() Pablo Neira Ayuso
2017-02-03 12:25 ` Pablo Neira Ayuso [this message]
2017-02-03 12:25 ` [PATCH 09/27] iptables: use match, target and data copy_to_user helpers Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 10/27] ip6tables: " Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 11/27] arptables: " Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 12/27] ebtables: " Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 13/27] xtables: use match, target and data copy_to_user helpers in compat Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 14/27] xtables: extend matches and targets with .usersize Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 15/27] netfilter: pkttype: unnecessary to check ipv6 multicast address Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 16/27] netfilter: nft_meta: deal with PACKET_LOOPBACK in netdev family Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 17/27] netfilter: nf_tables: eliminate useless condition checks Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 18/27] netfilter: nf_tables: Eliminate duplicated code in nf_tables_table_enable() Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 19/27] netfilter: conntrack: no need to pass ctinfo to error handler Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 20/27] netfilter: reset netfilter state when duplicating packet Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 21/27] netfilter: reduce direct skb->nfct usage Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 22/27] skbuff: add and use skb_nfct helper Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 23/27] netfilter: add and use nf_ct_set helper Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 24/27] netfilter: guarantee 8 byte minalign for template addresses Pablo Neira Ayuso
2017-02-06 10:08   ` David Laight
2017-02-03 12:25 ` [PATCH 25/27] netfilter: merge ctinfo into nfct pointer storage area Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 26/27] ipvs: free ip_vs_dest structs when refcnt=0 Pablo Neira Ayuso
2017-02-03 12:25 ` [PATCH 27/27] netfilter: allow logging from non-init namespaces Pablo Neira Ayuso
2017-02-03 22:08 ` [PATCH 00/27] Netfilter updates for net-next David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1486124738-3013-9-git-send-email-pablo@netfilter.org \
    --to=pablo@netfilter.org \
    --cc=davem@davemloft.net \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).