Skip to contentSkip to conversation

compat_xt_counters is not compatible

RFC PATCH In review Andreas Schwab 5 participants 25 messages Apr 14, 2006 – Apr 21, 2006 1 message not in the archive netfilter

lore
Share & tools
lore
#0 lore raw

struct compat_xt_counters uses uint32_t[4], which has 4 byte alignment, but userspace uses uint64_t[2], which has 8 byte alignment. This breaks iptables in 2.6.17-rc1.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#1 lore raw

compat_xt_counters should be used only for 32bit processes, which has 4 byte alignment.

Quoted 5 lines · Andreas Schwab

struct compat_xt_counters uses uint32_t[4], which has 4 byte alignment, but userspace uses uint64_t[2], which has 8 byte alignment. This breaks iptables in 2.6.17-rc1.

Andreas.

Signature · 2 lines
Thanks,
Dmitry.
#2 lore raw
Quoted 2 lines · Dmitry Mishin

compat_xt_counters should be used only for 32bit processes, which has 4 byte alignment.

This is wrong.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#3 lore raw

Could you give more details on what's wrong?

dim@dhcp0-24:~> cat test.c
#include <linux/types.h>

struct x

{
        __u64 ptr;
        __u32 ptr2;
};

int main(int argc, char *argv[])

{
        printf("%d\n", sizeof(struct x));
        return 0;
}

dim@dhcp0-24:~> gcc -m32 -o test test.c
dim@dhcp0-24:~> ./test
12
dim@dhcp0-24:~> gcc -o test test.c
dim@dhcp0-24:~> ./test
16
dim@dhcp0-24:~> uname -p
x86_64

Quoted 7 lines · Andreas Schwab
Quoted 2 lines · Dmitry Mishin

compat_xt_counters should be used only for 32bit processes, which has 4 byte alignment.

This is wrong.

Andreas.

Signature · 2 lines
Thanks,
Dmitry.
#4 lore raw
Quoted 1 line · Dmitry Mishin

Could you give more details on what's wrong?

Your claim that long long has 4 byte alignment.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#5 lore raw

I believes, that on 32 bit arch any alignment <= 4 bytes. Could you point out the cases, where it is not true?

Quoted 6 lines · Andreas Schwab
Quoted 1 line · Dmitry Mishin

Could you give more details on what's wrong?

Your claim that long long has 4 byte alignment.

Andreas.

Signature · 2 lines
Thanks,
Dmitry.
#6 lore raw
Quoted 1 line · Dmitry Mishin

I believes, that on 32 bit arch any alignment <= 4 bytes.

Why do you think so?

Quoted 1 line

Could you point out the cases, where it is not true?

Almost every architecture has alignment = size for basic types.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#7 lore raw
Quoted 4 lines

Almost every architecture has alignment = size for basic types.

Andreas.

I mean structures. I don't see a bug still. Can you point out wrong code?

Signature · 2 lines
Thanks,
Dmitry.
#8 lore raw
Quoted 5 lines · Dmitry Mishin
Quoted 4 lines

Almost every architecture has alignment = size for basic types.

Andreas.

I mean structures.

So do I.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#9 lore raw
In reply to Dmitry Mishin

From: Dmitry Mishin <[email protected]>
Date: Sun, 16 Apr 2006 10:28:38 +0400

Quoted 5 lines
Quoted 4 lines

Almost every architecture has alignment = size for basic types.

Andreas.

I mean structures. I don't see a bug still. Can you point out wrong code?

I can't believe such a conversation even gets this far. :(

LOOK!

struct {
	int x;
	unsigned long long y;
};

This structure requires 64-bit alignment on 32-bit Sparc and 32-bit PPC and 32-bit MIPS and 32-bit ....

It is x86, and only x86, that has only a 32-bit alignment requirement for this structure.

#10 lore raw
In reply to Andreas Schwab
Quoted 3 lines · Andreas Schwab

struct compat_xt_counters uses uint32_t[4], which has 4 byte alignment, but userspace uses uint64_t[2], which has 8 byte alignment. This breaks iptables in 2.6.17-rc1.

Did you actually see the breakage? gcc on x86 uses 8 byte alignment for u_int64_t, but 4 byte alignment for structures with embedded u_int64_t members. It appears to work just fine on my x86_64.

#11 lore raw
Quoted 6 lines · Patrick McHardy
Quoted 3 lines · Andreas Schwab

struct compat_xt_counters uses uint32_t[4], which has 4 byte alignment, but userspace uses uint64_t[2], which has 8 byte alignment. This breaks iptables in 2.6.17-rc1.

Did you actually see the breakage?

Of course.

Quoted 1 line

gcc on x86

Not all the worlds an x86.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#12 lore raw
Quoted 12 lines · Andreas Schwab
Quoted 7 lines · Patrick McHardy
Quoted 3 lines · Andreas Schwab

struct compat_xt_counters uses uint32_t[4], which has 4 byte alignment, but userspace uses uint64_t[2], which has 8 byte alignment. This breaks iptables in 2.6.17-rc1.

Did you actually see the breakage?

Quoted 7 lines

Of course.

Quoted 1 line

gcc on x86

Not all the worlds an x86.

Well, then how about providing the necessary information, starting with the architecture?

#13 lore raw

From: Patrick McHardy <[email protected]>
Date: Sat, 15 Apr 2006 19:52:01 +0200

Quoted 2 lines

Well, then how about providing the necessary information, starting with the architecture?

A struct containing a u64 has to be 64-bit aligned on at least sparc64 and ppc64, both of which use the compat layer for 32-bit programs, so I think it's quite clear this will have to be implemented differently.

#14 lore raw
Quoted 3 lines · David S. Miller

A struct containing a u64 has to be 64-bit aligned on at least sparc64 and ppc64, both of which use the compat layer for 32-bit programs, so I think it's quite clear this will have to be implemented differently.

Do these alignment requirements also hold for 32bit compiled programms? This is what we need to match with compat_xt_counters. Or in other
words: we need a type that when compiled for 64bit has the same alignment requirements as structures with embedded u_int64_t members compiled for 32bit on all architectures supporting CONFIG_COMPAT. I don't think such a type exists, so I guess we'll have to use different types depending on the architecture.

I'll send a patch to do that if there are no better suggestions.

#15 lore raw

From: Patrick McHardy <[email protected]>
Date: Sun, 16 Apr 2006 17:07:26 +0200

Quoted 6 lines
Quoted 3 lines · David S. Miller

A struct containing a u64 has to be 64-bit aligned on at least sparc64 and ppc64, both of which use the compat layer for 32-bit programs, so I think it's quite clear this will have to be implemented differently.

Do these alignment requirements also hold for 32bit compiled programms?

Yes, it does.

Quoted 1 line

This is what we need to match with compat_xt_counters.

This issue is exactly why the compat layer requirements are so wildly different between "x86_64/ia64 --> ia32" and all other compat situations.

ia32 doesn't have the 64-bit alignment requirement for structures containing u64 objects, whereas every other 32-bit platform does.

#16 lore raw

RFC PATCH 1/1 compat_uint64_t type

This patch introduces compat_uint64_t type, which is required for proper handling different alignment of structures, containing u64 objects, on compatible with ia32 64bit platforms and others.

Quoted 20 lines · David S. Miller

From: Patrick McHardy <[email protected]>
Date: Sun, 16 Apr 2006 17:07:26 +0200

Quoted 6 lines
Quoted 3 lines · David S. Miller

A struct containing a u64 has to be 64-bit aligned on at least sparc64 and ppc64, both of which use the compat layer for 32-bit programs, so I think it's quite clear this will have to be implemented differently.

Do these alignment requirements also hold for 32bit compiled programms?

Yes, it does.

Quoted 1 line

This is what we need to match with compat_xt_counters.

This issue is exactly why the compat layer requirements are so wildly different between "x86_64/ia64 --> ia32" and all other compat situations.

ia32 doesn't have the 64-bit alignment requirement for structures containing u64 objects, whereas every other 32-bit platform does.

Signature · 2 lines
Thanks,
Dmitry.
include/asm-ia64/compat.h +2 -0
index 40d01d8..3e6417b 100644
@@ -34,6 +34,8 @@ typedef s32		compat_long_t;
 typedef u32		compat_uint_t;
 typedef u32		compat_ulong_t;
 
+typedef u64 __attribute__((aligned(4)))	compat_uint64_t;
+
 struct compat_timespec {
 	compat_time_t	tv_sec;
 	s32		tv_nsec;
include/asm-mips/compat.h +2 -0
index 986511d..f95640f 100644
@@ -38,6 +38,8 @@ typedef s32		compat_long_t;
 typedef u32		compat_uint_t;
 typedef u32		compat_ulong_t;
 
+typedef u64		compat_uint64_t;
+
 struct compat_timespec {
 	compat_time_t	tv_sec;
 	s32		tv_nsec;
include/asm-parisc/compat.h +2 -0
index 289624d..312829d 100644
@@ -33,6 +33,8 @@ typedef s32	compat_long_t;
 typedef u32	compat_uint_t;
 typedef u32	compat_ulong_t;
 
+typedef u64	compat_uint64_t;
+
 struct compat_timespec {
 	compat_time_t		tv_sec;
 	s32			tv_nsec;
include/asm-powerpc/compat.h +2 -0
index aacaabd..e90fa22 100644
@@ -36,6 +36,8 @@ typedef s32		compat_long_t;
 typedef u32		compat_uint_t;
 typedef u32		compat_ulong_t;
 
+typedef u64		compat_uint64_t;
+
 struct compat_timespec {
 	compat_time_t	tv_sec;
 	s32		tv_nsec;
include/asm-s390/compat.h +2 -0
index 356a0b1..1b8c7f7 100644
@@ -35,6 +35,8 @@ typedef s32		compat_long_t;
 typedef u32		compat_uint_t;
 typedef u32		compat_ulong_t;
 
+typedef u64		compat_uint64_t;
+
 struct compat_timespec {
 	compat_time_t	tv_sec;
 	s32		tv_nsec;
include/asm-sparc64/compat.h +2 -0
index c73935d..6adc10c 100644
@@ -34,6 +34,8 @@ typedef s32		compat_long_t;
 typedef u32		compat_uint_t;
 typedef u32		compat_ulong_t;
 
+typedef u64		compat_uint64_t;
+
 struct compat_timespec {
 	compat_time_t	tv_sec;
 	s32		tv_nsec;
include/asm-x86_64/compat.h +2 -0
index b37ab82..4d7eb9c 100644
@@ -36,6 +36,8 @@ typedef s32		compat_long_t;
 typedef u32		compat_uint_t;
 typedef u32		compat_ulong_t;
 
+typedef u64 __attribute__((aligned(4)))	compat_uint64_t;
+
 struct compat_timespec {
 	compat_time_t	tv_sec;
 	s32		tv_nsec;
include/linux/netfilter/x_tables.h +1 -1
index f6bdef8..558125c 100644
@@ -361,7 +361,7 @@ struct compat_xt_entry_target
 
 struct compat_xt_counters
 {
-	u_int32_t cnt[4];
+	compat_uint64_t pcnt, bcnt;
 };
 
 struct compat_xt_counters_info
#17 lore raw

Re: [RFC][PATCH] compat_uint64_t type

include/asm-ia64/compat.h typedef s32 compat_long_t; · line 37

Quoted 9 lines · Dmitry Mishin
include/asm-ia64/compat.h +1 -0
index 40d01d8..3e6417b 100644
@@ -34,6 +34,8 @@ typedef s32		compat_long_t;
 typedef u32		compat_uint_t;
 typedef u32		compat_ulong_t;
 
+typedef u64 __attribute__((aligned(4)))	compat_uint64_t;

The aligned attribute cannot decrease alignment.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#18 lore raw

Re: [RFC][PATCH] compat_uint64_t type

Quoted 9 lines · Andreas Schwab
Quoted 8 lines · Dmitry Mishin
include/asm-ia64/compat.h +0 -0
index 40d01d8..3e6417b 100644
@@ -34,6 +34,8 @@ typedef s32		compat_long_t;
 typedef u32		compat_uint_t;
 typedef u32		compat_ulong_t;
 
Quoted 3 lines

include/asm-ia64/compat.h typedef s32 compat_long_t; · line 37

Quoted 1 line
include/asm-ia64/compat.h +1 -0
+typedef u64 __attribute__((aligned(4)))	compat_uint64_t;

The aligned attribute cannot decrease alignment.

With my gcc (3.3.5) it can. Which solution do you propose? __attribute__((packed, aligned(4)))?
Or
#ifdef ARCH_HAS_FUNNY_ALIGNMENT

struct compat_xt_counters {
	uint32_t cnt[4];
}

#else
#define compat_xt_counters xt_counters
#endif
?

Quoted 2 lines

Andreas.

Signature · 2 lines
Thanks,
Dmitry.
#19 lore raw

Re: [RFC][PATCH] compat_uint64_t type

Quoted 10 lines · Dmitry Mishin
Quoted 9 lines · Andreas Schwab
Quoted 8 lines · Dmitry Mishin
include/asm-ia64/compat.h +0 -0
index 40d01d8..3e6417b 100644
@@ -34,6 +34,8 @@ typedef s32		compat_long_t;
 typedef u32		compat_uint_t;
 typedef u32		compat_ulong_t;
 
Quoted 4 lines
Quoted 3 lines

include/asm-ia64/compat.h typedef s32 compat_long_t; · line 37

Quoted 1 line
include/asm-ia64/compat.h +1 -0
+typedef u64 __attribute__((aligned(4)))	compat_uint64_t;

The aligned attribute cannot decrease alignment.

With my gcc (3.3.5) it can.

You're right, this is a special case (in general it can't without the packed attribute).

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#20 lore raw

Re: [RFC][PATCH] compat_uint64_t type

In reply to Dmitry Mishin
Quoted 10 lines · Dmitry Mishin

This patch introduces compat_uint64_t type, which is required for proper handling different alignment of structures, containing u64 objects, on compatible with ia32 64bit platforms and others.

include/asm-ia64/compat.h +0 -0
index 40d01d8..3e6417b 100644

include/asm-ia64/compat.h typedef s32 compat_long_t; · line 41

Quoted 8 lines
include/asm-ia64/compat.h +2 -0
@@ -34,6 +34,8 @@ typedef s32		compat_long_t;
 typedef u32		compat_uint_t;
 typedef u32		compat_ulong_t;
 
+typedef u64 __attribute__((aligned(4)))	compat_uint64_t;
+
 struct compat_timespec {
 	compat_time_t	tv_sec;
 	s32		tv_nsec;

I'm not sure if we should really introduce this new type, it is misleading for at least x86. IIRC the situation there is:

- gcc < 4.0 always aligns u64 types to 4byte
- gcc >= 4.0 aligns basic u64 types to 8byte, u64's embedded in structures to 4 byte

So there is no one compat_uint64_t type, but it depends on the situation.

#21 lore raw

Re: [RFC][PATCH] compat_uint64_t type

Quoted 5 lines · Patrick McHardy
Quoted 3 lines · Dmitry Mishin

This patch introduces compat_uint64_t type, which is required for proper handling different alignment of structures, containing u64 objects, on compatible with ia32 64bit platforms and others.

This is my proposed alternative. It assumes all architectures besides x86 use native alignment for basic types.

[NETFILTER]: Fix compat_xt_counters alignment for non-x86

Some (?) non-x86 architectures require 8byte alignment for u_int64_t even when compiled for 32bit, using u_int32_t in compat_xt_counters breaks on these architectures, use u_int64_t for everything but x86.

Reported by Andreas Schwab <[email protected]>.

---
commit 2666afe70fb68b9b884f6d3fa7e4236183818028
tree 65eac95933d59a455853245f7442cbb38dc9f19c
parent 2ceefa038e908d5da21aefedae02da4eab1b2787
author Patrick McHardy <[email protected]> Mon, 17 Apr 2006 18:04:37 +0200 committer Patrick McHardy <[email protected]> Mon, 17 Apr 2006 18:04:37 +0200

 include/linux/netfilter/x_tables.h |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
include/linux/netfilter/x_tables.h +4 -0
index f6bdef8..3870145 100644
@@ -361,7 +361,11 @@ struct compat_xt_entry_target
 
 struct compat_xt_counters
 {
+#if defined(CONFIG_X86_64) || defined(CONFIG_IA64)
 	u_int32_t cnt[4];
+#else
+	u_int64_t cnt[2];
+#endif
 };
 
 struct compat_xt_counters_info
#22 lore raw

Re: [RFC][PATCH] compat_uint64_t type

From: Patrick McHardy <[email protected]>
Date: Mon, 17 Apr 2006 18:03:39 +0200

Quoted 9 lines
Quoted 5 lines · Patrick McHardy
Quoted 3 lines · Dmitry Mishin

This patch introduces compat_uint64_t type, which is required for proper handling different alignment of structures, containing u64 objects, on compatible with ia32 64bit platforms and others.

This is my proposed alternative. It assumes all architectures besides x86 use native alignment for basic types.

Ok. Perhaps we can eventually define a CONFIG_* object for this.

#23 lore raw

Re: [RFC][PATCH] compat_uint64_t type

Quoted 9 lines · David S. Miller

From: Patrick McHardy <[email protected]>
Date: Mon, 17 Apr 2006 18:03:39 +0200

Quoted 2 lines

This is my proposed alternative. It assumes all architectures besides x86 use native alignment for basic types.

Ok. Perhaps we can eventually define a CONFIG_* object for this.

Since this appears to be a pure x86 32 bit emulation oddity so far, I think CONFIG_IA32_EMULATION would express that best. Unfortunately IA64 uses a different config option, so I left it as is.

I'll send the patch with a batch of other fixes in a few hours.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#25 lore raw

Re: [RFC][PATCH] compat_uint64_t type

Quoted 9 lines · Andreas Schwab
Quoted 4 lines · Patrick McHardy

I'm not sure if we should really introduce this new type, it is misleading for at least x86. IIRC the situation there is:

- gcc < 4.0 always aligns u64 types to 4byte

Are you sure about that? I can't reproduce that with gcc 3.3.3.

I mixed up the versions, the change was between 2.x and 3.x:

gcc-2.95 -O2:

basic type:     align 4
embedded type:  align 4
gcc-3.3 -O2:
basic type:     align 8
embedded type:  align 4

It only shows with optimization, with -O0 gcc-2.95 also uses 8 for the basic type.

Quoted 6 lines
Quoted 2 lines

So there is no one compat_uint64_t type, but it depends on the situation.

The alignment of standalone objects is uninteresting, what is important is getting compatible layout of aggregates.

I couldn't come up with a realistic example where people would make mistakes because of this, so I guess I have to agree, although my feeling still disagrees :)