]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - net/ipv4/netfilter/ip_tables.c
Merge remote-tracking branch 'remotes/stable/linux-4.4.y' into karo-tx6-mainline
[karo-tx-linux.git] / net / ipv4 / netfilter / ip_tables.c
index 242bf9a6799ccc473a0efaf136bd07cffe11cd14..a399c5419622d5f47acd74922bb3771fc55bad73 100644 (file)
@@ -1313,55 +1313,17 @@ do_add_counters(struct net *net, const void __user *user,
        unsigned int i;
        struct xt_counters_info tmp;
        struct xt_counters *paddc;
-       unsigned int num_counters;
-       const char *name;
-       int size;
-       void *ptmp;
        struct xt_table *t;
        const struct xt_table_info *private;
        int ret = 0;
        struct ipt_entry *iter;
        unsigned int addend;
-#ifdef CONFIG_COMPAT
-       struct compat_xt_counters_info compat_tmp;
-
-       if (compat) {
-               ptmp = &compat_tmp;
-               size = sizeof(struct compat_xt_counters_info);
-       } else
-#endif
-       {
-               ptmp = &tmp;
-               size = sizeof(struct xt_counters_info);
-       }
-
-       if (copy_from_user(ptmp, user, size) != 0)
-               return -EFAULT;
-
-#ifdef CONFIG_COMPAT
-       if (compat) {
-               num_counters = compat_tmp.num_counters;
-               name = compat_tmp.name;
-       } else
-#endif
-       {
-               num_counters = tmp.num_counters;
-               name = tmp.name;
-       }
-
-       if (len != size + num_counters * sizeof(struct xt_counters))
-               return -EINVAL;
-
-       paddc = vmalloc(len - size);
-       if (!paddc)
-               return -ENOMEM;
 
-       if (copy_from_user(paddc, user + size, len - size) != 0) {
-               ret = -EFAULT;
-               goto free;
-       }
+       paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
+       if (IS_ERR(paddc))
+               return PTR_ERR(paddc);
 
-       t = xt_find_table_lock(net, AF_INET, name);
+       t = xt_find_table_lock(net, AF_INET, tmp.name);
        if (IS_ERR_OR_NULL(t)) {
                ret = t ? PTR_ERR(t) : -ENOENT;
                goto free;
@@ -1369,7 +1331,7 @@ do_add_counters(struct net *net, const void __user *user,
 
        local_bh_disable();
        private = t->private;
-       if (private->number != num_counters) {
+       if (private->number != tmp.num_counters) {
                ret = -EINVAL;
                goto unlock_up_free;
        }
@@ -1482,16 +1444,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 +1503,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:
@@ -1567,7 +1516,7 @@ release_matches:
        return ret;
 }
 
-static int
+static void
 compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
                            unsigned int *size,
                            struct xt_table_info *newinfo, unsigned char *base)
@@ -1576,10 +1525,9 @@ compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
        struct xt_target *target;
        struct ipt_entry *de;
        unsigned int origsize;
-       int ret, h;
+       int h;
        struct xt_entry_match *ematch;
 
-       ret = 0;
        origsize = *size;
        de = (struct ipt_entry *)*dstptr;
        memcpy(de, e, sizeof(struct ipt_entry));
@@ -1588,66 +1536,22 @@ compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
        *dstptr += sizeof(struct ipt_entry);
        *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
 
-       xt_ematch_foreach(ematch, e) {
-               ret = xt_compat_match_from_user(ematch, dstptr, size);
-               if (ret != 0)
-                       return ret;
-       }
+       xt_ematch_foreach(ematch, e)
+               xt_compat_match_from_user(ematch, dstptr, size);
+
        de->target_offset = e->target_offset - (origsize - *size);
        t = compat_ipt_get_target(e);
        target = t->u.kernel.target;
        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;
                if ((unsigned char *)de - base < newinfo->underflow[h])
                        newinfo->underflow[h] -= origsize - *size;
        }
-       return ret;
-}
-
-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
@@ -1660,7 +1564,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;
 
@@ -1669,12 +1573,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);
@@ -1683,9 +1581,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;
@@ -1698,23 +1594,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)
@@ -1722,61 +1601,40 @@ 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;
        size = compatr->size;
-       xt_entry_foreach(iter0, entry0, compatr->size) {
-               ret = compat_copy_entry_from_user(iter0, &pos, &size,
-                                                 newinfo, entry1);
-               if (ret != 0)
-                       break;
-       }
+       xt_entry_foreach(iter0, entry0, compatr->size)
+               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);
-       if (ret)
-               goto free_newinfo;
 
-       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);
@@ -1784,17 +1642,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