[NETFILTER] x_tables: fix compat related crash on non-x86
authorPatrick McHardy <kaber@trash.net>
Tue, 2 May 2006 03:12:22 +0000 (05:12 +0200)
committerLinus Torvalds <torvalds@g5.osdl.org>
Tue, 2 May 2006 03:48:32 +0000 (20:48 -0700)
When iptables userspace adds an ipt_standard_target, it calculates the size
of the entire entry as:

sizeof(struct ipt_entry) + XT_ALIGN(sizeof(struct ipt_standard_target))

ipt_standard_target looks like this:

  struct xt_standard_target
  {
        struct xt_entry_target target;
        int verdict;
  };

xt_entry_target contains a pointer, so when compiled for 64 bit the
structure gets an extra 4 byte of padding at the end. On 32 bit
architectures where iptables aligns to 8 byte it will also have 4
byte padding at the end because it is only 36 bytes large.

The compat_ipt_standard_fn in the kernel adjusts the offsets by

  sizeof(struct ipt_standard_target) - sizeof(struct compat_ipt_standard_target),

which will always result in 4, even if the structure from userspace
was already padded to a multiple of 8. On x86 this works out by
accident because userspace only aligns to 4, on all other
architectures this is broken and causes incorrect adjustments to
the size and following offsets.

Thanks to Linus for lots of debugging help and testing.

Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
include/linux/netfilter/x_tables.h
net/ipv4/netfilter/ip_tables.c

index 38701454e197cffcf59bcf27ee52e79531cc8756..48cc32d83f77acb935768224e8cdd7ac8a3d982f 100644 (file)
@@ -337,6 +337,10 @@ struct compat_xt_entry_match
                        char name[XT_FUNCTION_MAXNAMELEN - 1];
                        u_int8_t revision;
                } user;
+               struct {
+                       u_int16_t match_size;
+                       compat_uptr_t match;
+               } kernel;
                u_int16_t match_size;
        } u;
        unsigned char data[0];
@@ -350,6 +354,10 @@ struct compat_xt_entry_target
                        char name[XT_FUNCTION_MAXNAMELEN - 1];
                        u_int8_t revision;
                } user;
+               struct {
+                       u_int16_t target_size;
+                       compat_uptr_t target;
+               } kernel;
                u_int16_t target_size;
        } u;
        unsigned char data[0];
index d25ac8ba6ebaedb0e0e1cbe658b80c94e0814b39..6d1c11563943f1cdfa35fcded77e30f5d0b14c02 100644 (file)
@@ -956,15 +956,16 @@ struct compat_ipt_standard_target
        compat_int_t verdict;
 };
 
-#define IPT_ST_OFFSET  (sizeof(struct ipt_standard_target) - \
-                               sizeof(struct compat_ipt_standard_target))
-
 struct compat_ipt_standard
 {
        struct compat_ipt_entry entry;
        struct compat_ipt_standard_target target;
 };
 
+#define IPT_ST_LEN             XT_ALIGN(sizeof(struct ipt_standard_target))
+#define IPT_ST_COMPAT_LEN      COMPAT_XT_ALIGN(sizeof(struct compat_ipt_standard_target))
+#define IPT_ST_OFFSET          (IPT_ST_LEN - IPT_ST_COMPAT_LEN)
+
 static int compat_ipt_standard_fn(void *target,
                void **dstptr, int *size, int convert)
 {
@@ -975,35 +976,29 @@ static int compat_ipt_standard_fn(void *target,
        ret = 0;
        switch (convert) {
                case COMPAT_TO_USER:
-                       pst = (struct ipt_standard_target *)target;
+                       pst = target;
                        memcpy(&compat_st.target, &pst->target,
-                                       sizeof(struct ipt_entry_target));
+                               sizeof(compat_st.target));
                        compat_st.verdict = pst->verdict;
                        if (compat_st.verdict > 0)
                                compat_st.verdict -=
                                        compat_calc_jump(compat_st.verdict);
-                       compat_st.target.u.user.target_size =
-                       sizeof(struct compat_ipt_standard_target);
-                       if (__copy_to_user(*dstptr, &compat_st,
-                               sizeof(struct compat_ipt_standard_target)))
+                       compat_st.target.u.user.target_size = IPT_ST_COMPAT_LEN;
+                       if (copy_to_user(*dstptr, &compat_st, IPT_ST_COMPAT_LEN))
                                ret = -EFAULT;
                        *size -= IPT_ST_OFFSET;
-                       *dstptr += sizeof(struct compat_ipt_standard_target);
+                       *dstptr += IPT_ST_COMPAT_LEN;
                        break;
                case COMPAT_FROM_USER:
-                       pcompat_st =
-                               (struct compat_ipt_standard_target *)target;
-                       memcpy(&st.target, &pcompat_st->target,
-                                       sizeof(struct ipt_entry_target));
+                       pcompat_st = target;
+                       memcpy(&st.target, &pcompat_st->target, IPT_ST_COMPAT_LEN);
                        st.verdict = pcompat_st->verdict;
                        if (st.verdict > 0)
                                st.verdict += compat_calc_jump(st.verdict);
-                       st.target.u.user.target_size =
-                       sizeof(struct ipt_standard_target);
-                       memcpy(*dstptr, &st,
-                                       sizeof(struct ipt_standard_target));
+                       st.target.u.user.target_size = IPT_ST_LEN;
+                       memcpy(*dstptr, &st, IPT_ST_LEN);
                        *size += IPT_ST_OFFSET;
-                       *dstptr += sizeof(struct ipt_standard_target);
+                       *dstptr += IPT_ST_LEN;
                        break;
                case COMPAT_CALC_SIZE:
                        *size += IPT_ST_OFFSET;