Skip to contentSkip to conversation

Unaligned accesses in compat_sys_msgrcv and sys_msgrcv

DISCUSSION Jurij Smakov 2 participants 5 messages Oct 21, 2006 – Oct 23, 2006

lore
Share & tools
lore
#0 lore raw

Hi,

Every time I compile anything on my box running 2.6.18, I get these messages in dmesg every 5 seconds:

Kernel unaligned access at TPC[4ec2f0] sys_msgrcv+0x2dc/0x32c Kernel unaligned access at TPC[4e9f6c] compat_sys_msgrcv+0xbc/0x124 Kernel unaligned access at TPC[4ec2f0] sys_msgrcv+0x2dc/0x32c Kernel unaligned access at TPC[4e9f6c] compat_sys_msgrcv+0xbc/0x124 Kernel unaligned access at TPC[4ec2f0] sys_msgrcv+0x2dc/0x32c

Decoding the addresses indicates that the unaligned accesses are generated by get_user call in compat_sys_msgrcv at ipc/compat.c:357 and put_user in sys_msgrcv at ipc/msg.c:836. As far as I can tell, the problem stems from the fact that allocating memory with

p = compat_alloc_user_space(second + sizeof(struct msgbuf));

at ipc/compat.c:352 does not guarantee alignment of p, so when we later try to ldxa from the address &p->mtype (in get_user) or stxa to &msgp->mtype (in put_user), we catch an exception.

As I'm not sure what would be the correct fix here, I'd appreciate any advice on how to deal with that.

Best regards,

Signature · 2 lines
Jurij Smakov                                           [email protected]
Key: http://www.wooyd.org/pgpkey/                      KeyID: C99E03CC
#1 lore raw

From: Jurij Smakov <[email protected]>
Date: Fri, 20 Oct 2006 21:11:08 -0700

Quoted 8 lines

p = compat_alloc_user_space(second + sizeof(struct msgbuf));

at ipc/compat.c:352 does not guarantee alignment of p, so when we later try to ldxa from the address &p->mtype (in get_user) or stxa to &msgp->mtype (in put_user), we catch an exception.

As I'm not sure what would be the correct fix here, I'd appreciate any advice on how to deal with that.

compat_alloc_user_space() should round down the pointer it returns to be 8-byte aligned.

Thanks for catching this, I'll cook up a patch over the weekend.

What application is calling sys_msgrev() during a compile btw?

#2 lore raw
In reply to Jurij Smakov

From: Jurij Smakov <[email protected]>
Date: Fri, 20 Oct 2006 21:11:08 -0700

Quoted 1 line

p = compat_alloc_user_space(second + sizeof(struct msgbuf));

Thanks again for this report Jurij, this patch should fix it up.

commit a94b1d1fd7ca3129e1d38d38167779fa6ee69780

Author: David S. Miller <[email protected]>
Date:   Sun Oct 22 21:53:30 2006 -0700

[SPARC64]: 8-byte align return value from compat_alloc_user_space()

Otherwise we get a ton of unaligned exceptions, for cases such as compat_sys_msgrcv() which go:

p = compat_alloc_user_space(second + sizeof(struct msgbuf));

and here 'second' can for example be an arbitrary odd value.

Based upon a bug report from Jurij Smakov.

include/asm-sparc64/compat.h +5 -2
index c73935d..36511ca 100644
@@ -164,7 +164,7 @@ static inline compat_uptr_t ptr_to_compa
 	return (u32)(unsigned long)uptr;
 }
 
-static __inline__ void __user *compat_alloc_user_space(long len)
+static inline void __user *compat_alloc_user_space(long len)
 {
 	struct pt_regs *regs = current_thread_info()->kregs;
 	unsigned long usp = regs->u_regs[UREG_I6];
@@ -174,7 +174,10 @@ static __inline__ void __user *compat_al
 	else
 		usp &= 0xffffffffUL;
 
-	return (void __user *) (usp - len);
+	usp -= len;
+	usp &= ~0x7UL;
+
+	return (void __user *) usp;
 }
 
 struct compat_ipc64_perm {
#3 lore raw
In reply to Jurij Smakov
Quoted 10 lines · David Miller

From: Jurij Smakov <[email protected]>
Date: Fri, 20 Oct 2006 21:11:08 -0700

Quoted 1 line

p = compat_alloc_user_space(second + sizeof(struct msgbuf));

Thanks again for this report Jurij, this patch should fix it up.

commit a94b1d1fd7ca3129e1d38d38167779fa6ee69780

Author: David S. Miller <[email protected]>
Date:   Sun Oct 22 21:53:30 2006 -0700

Thanks, that took care of the unaligned accesses nicely.

It turned out that the source of these messages was fakeroot - a Debian utility which wraps a few system calls to create an illusion that the process is running with root privileges. It is commonly used during package builds and uses IPC to communicate with the daemon which tracks the (fake) information about file ownership and permissions.

Best regards,

Signature · 2 lines
Jurij Smakov                                           [email protected]
Key: http://www.wooyd.org/pgpkey/                      KeyID: C99E03CC
#4 lore raw
In reply to Jurij Smakov

From: Jurij Smakov <[email protected]>
Date: Mon, 23 Oct 2006 09:52:00 -0700

Quoted 6 lines

It turned out that the source of these messages was fakeroot - a Debian utility which wraps a few system calls to create an illusion that the process is running with root privileges. It is commonly used during package builds and uses IPC to communicate with the daemon which tracks the (fake) information about file ownership and permissions.

Thanks Jurij, I knew about fakeroot but not that it used sysv IPC :-)