netfilter: x_tables: do compat validation via translate_table
[firefly-linux-kernel-4.4.55.git] / net / ipv4 / netfilter / ip_tables.c
index 143c611239f0b9b9b2b41114a3bd8069e2399d4d..c2f7f58c001076e955829c6bc5b9e951e346c207 100644 (file)
@@ -1482,16 +1482,14 @@ check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
                                  struct xt_table_info *newinfo,
                                  unsigned int *size,
                                  const unsigned char *base,
-                                 const unsigned char *limit,
-                                 const unsigned int *hook_entries,
-                                 const unsigned int *underflows)
+                                 const unsigned char *limit)
 {
        struct xt_entry_match *ematch;
        struct xt_entry_target *t;
        struct xt_target *target;
        unsigned int entry_offset;
        unsigned int j;
-       int ret, off, h;
+       int ret, off;
 
        duprintf("check_compat_entry_size_and_hooks %p\n", e);
        if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
@@ -1543,17 +1541,6 @@ check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
        if (ret)
                goto out;
 
-       /* Check hooks & underflows */
-       for (h = 0; h < NF_INET_NUMHOOKS; h++) {
-               if ((unsigned char *)e - base == hook_entries[h])
-                       newinfo->hook_entry[h] = hook_entries[h];
-               if ((unsigned char *)e - base == underflows[h])
-                       newinfo->underflow[h] = underflows[h];
-       }
-
-       /* Clear counters and comefrom */
-       memset(&e->counters, 0, sizeof(e->counters));
-       e->comefrom = 0;
        return 0;
 
 out:
@@ -1596,6 +1583,7 @@ compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
        xt_compat_target_from_user(t, dstptr, size);
 
        de->next_offset = e->next_offset - (origsize - *size);
+
        for (h = 0; h < NF_INET_NUMHOOKS; h++) {
                if ((unsigned char *)de - base < newinfo->hook_entry[h])
                        newinfo->hook_entry[h] -= origsize - *size;
@@ -1604,48 +1592,6 @@ compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
        }
 }
 
-static int
-compat_check_entry(struct ipt_entry *e, struct net *net, const char *name)
-{
-       struct xt_entry_match *ematch;
-       struct xt_mtchk_param mtpar;
-       unsigned int j;
-       int ret = 0;
-
-       e->counters.pcnt = xt_percpu_counter_alloc();
-       if (IS_ERR_VALUE(e->counters.pcnt))
-               return -ENOMEM;
-
-       j = 0;
-       mtpar.net       = net;
-       mtpar.table     = name;
-       mtpar.entryinfo = &e->ip;
-       mtpar.hook_mask = e->comefrom;
-       mtpar.family    = NFPROTO_IPV4;
-       xt_ematch_foreach(ematch, e) {
-               ret = check_match(ematch, &mtpar);
-               if (ret != 0)
-                       goto cleanup_matches;
-               ++j;
-       }
-
-       ret = check_target(e, net, name);
-       if (ret)
-               goto cleanup_matches;
-       return 0;
-
- cleanup_matches:
-       xt_ematch_foreach(ematch, e) {
-               if (j-- == 0)
-                       break;
-               cleanup_match(ematch, net);
-       }
-
-       xt_percpu_counter_free(e->counters.pcnt);
-
-       return ret;
-}
-
 static int
 translate_compat_table(struct net *net,
                       struct xt_table_info **pinfo,
@@ -1656,7 +1602,7 @@ translate_compat_table(struct net *net,
        struct xt_table_info *newinfo, *info;
        void *pos, *entry0, *entry1;
        struct compat_ipt_entry *iter0;
-       struct ipt_entry *iter1;
+       struct ipt_replace repl;
        unsigned int size;
        int ret;
 
@@ -1665,12 +1611,6 @@ translate_compat_table(struct net *net,
        size = compatr->size;
        info->number = compatr->num_entries;
 
-       /* Init all hooks to impossible value. */
-       for (i = 0; i < NF_INET_NUMHOOKS; i++) {
-               info->hook_entry[i] = 0xFFFFFFFF;
-               info->underflow[i] = 0xFFFFFFFF;
-       }
-
        duprintf("translate_compat_table: size %u\n", info->size);
        j = 0;
        xt_compat_lock(AF_INET);
@@ -1679,9 +1619,7 @@ translate_compat_table(struct net *net,
        xt_entry_foreach(iter0, entry0, compatr->size) {
                ret = check_compat_entry_size_and_hooks(iter0, info, &size,
                                                        entry0,
-                                                       entry0 + compatr->size,
-                                                       compatr->hook_entry,
-                                                       compatr->underflow);
+                                                       entry0 + compatr->size);
                if (ret != 0)
                        goto out_unlock;
                ++j;
@@ -1694,23 +1632,6 @@ translate_compat_table(struct net *net,
                goto out_unlock;
        }
 
-       /* Check hooks all assigned */
-       for (i = 0; i < NF_INET_NUMHOOKS; i++) {
-               /* Only hooks which are valid */
-               if (!(compatr->valid_hooks & (1 << i)))
-                       continue;
-               if (info->hook_entry[i] == 0xFFFFFFFF) {
-                       duprintf("Invalid hook entry %u %u\n",
-                                i, info->hook_entry[i]);
-                       goto out_unlock;
-               }
-               if (info->underflow[i] == 0xFFFFFFFF) {
-                       duprintf("Invalid underflow %u %u\n",
-                                i, info->underflow[i]);
-                       goto out_unlock;
-               }
-       }
-
        ret = -ENOMEM;
        newinfo = xt_alloc_table_info(size);
        if (!newinfo)
@@ -1718,8 +1639,8 @@ translate_compat_table(struct net *net,
 
        newinfo->number = compatr->num_entries;
        for (i = 0; i < NF_INET_NUMHOOKS; i++) {
-               newinfo->hook_entry[i] = info->hook_entry[i];
-               newinfo->underflow[i] = info->underflow[i];
+               newinfo->hook_entry[i] = compatr->hook_entry[i];
+               newinfo->underflow[i] = compatr->underflow[i];
        }
        entry1 = newinfo->entries;
        pos = entry1;
@@ -1728,47 +1649,30 @@ translate_compat_table(struct net *net,
                compat_copy_entry_from_user(iter0, &pos, &size,
                                            newinfo, entry1);
 
+       /* all module references in entry0 are now gone.
+        * entry1/newinfo contains a 64bit ruleset that looks exactly as
+        * generated by 64bit userspace.
+        *
+        * Call standard translate_table() to validate all hook_entrys,
+        * underflows, check for loops, etc.
+        */
        xt_compat_flush_offsets(AF_INET);
        xt_compat_unlock(AF_INET);
 
-       ret = -ELOOP;
-       if (!mark_source_chains(newinfo, compatr->valid_hooks, entry1))
-               goto free_newinfo;
+       memcpy(&repl, compatr, sizeof(*compatr));
 
-       i = 0;
-       xt_entry_foreach(iter1, entry1, newinfo->size) {
-               ret = compat_check_entry(iter1, net, compatr->name);
-               if (ret != 0)
-                       break;
-               ++i;
-               if (strcmp(ipt_get_target(iter1)->u.user.name,
-                   XT_ERROR_TARGET) == 0)
-                       ++newinfo->stacksize;
-       }
-       if (ret) {
-               /*
-                * The first i matches need cleanup_entry (calls ->destroy)
-                * because they had called ->check already. The other j-i
-                * entries need only release.
-                */
-               int skip = i;
-               j -= i;
-               xt_entry_foreach(iter0, entry0, newinfo->size) {
-                       if (skip-- > 0)
-                               continue;
-                       if (j-- == 0)
-                               break;
-                       compat_release_entry(iter0);
-               }
-               xt_entry_foreach(iter1, entry1, newinfo->size) {
-                       if (i-- == 0)
-                               break;
-                       cleanup_entry(iter1, net);
-               }
-               xt_free_table_info(newinfo);
-               return ret;
+       for (i = 0; i < NF_INET_NUMHOOKS; i++) {
+               repl.hook_entry[i] = newinfo->hook_entry[i];
+               repl.underflow[i] = newinfo->underflow[i];
        }
 
+       repl.num_counters = 0;
+       repl.counters = NULL;
+       repl.size = newinfo->size;
+       ret = translate_table(net, newinfo, entry1, &repl);
+       if (ret)
+               goto free_newinfo;
+
        *pinfo = newinfo;
        *pentry0 = entry1;
        xt_free_table_info(info);
@@ -1776,17 +1680,16 @@ translate_compat_table(struct net *net,
 
 free_newinfo:
        xt_free_table_info(newinfo);
-out:
+       return ret;
+out_unlock:
+       xt_compat_flush_offsets(AF_INET);
+       xt_compat_unlock(AF_INET);
        xt_entry_foreach(iter0, entry0, compatr->size) {
                if (j-- == 0)
                        break;
                compat_release_entry(iter0);
        }
        return ret;
-out_unlock:
-       xt_compat_flush_offsets(AF_INET);
-       xt_compat_unlock(AF_INET);
-       goto out;
 }
 
 static int