Skip to contentSkip to conversation

system lockup bug in >2.6.7-rc1-bk6 tracked down

PATCH Jeremy Huddleston 1 participant 3 messages Nov 27, 2004 – Dec 5, 2004 VFS

lore
Share & tools
lore
#0 lore raw

A change between 2.6.7-rc1-bk6 and 2.6.7-rc2 can results in a system lockdown. One situation in which this can be triggered is when we are a nfs client, and the nfs server shuts down (or on the server, you simply issue an 'ifconfig eth1 down'). I have tracked this down to a bug in dev_ifconf() in fs/compat_ioctl.c

The attached patch will revert this function to it's pre-2.6.7-rc2 state. This is not meant to be the suggested fix, but it is a workaround for now for anyone who is in need of it, and hopefully now that I've narrowed down the problem, someone else might be willing/able to fix it since it's past 3am here, and I don't have much experience with the fs/ section of the kernel...

Thanks.

Signature · 1 line
Jeremy Huddleston <[email protected]>
fs/compat_ioctl.c +48 -44
diff -Naurp linux-2.6.7-rc2/fs/compat_ioctl.c linux-2.6.7-rc2.nfsrevert/fs/compat_ioctl.c
@@ -490,9 +490,9 @@ static int dev_ifconf(unsigned int fd, u
 {
 	struct ifconf32 ifc32;
 	struct ifconf ifc;
-	struct ifconf __user *uifc;
 	struct ifreq32 __user *ifr32;
 	struct ifreq __user *ifr;
+	mm_segment_t old_fs;
 	unsigned int i, j;
 	int err;
 
@@ -502,59 +502,63 @@ static int dev_ifconf(unsigned int fd, u
 	if (ifc32.ifcbuf == 0) {
 		ifc32.ifc_len = 0;
 		ifc.ifc_len = 0;
-		ifc.ifc_req = NULL;
-		uifc = compat_alloc_user_space(sizeof(struct ifconf));
+		ifc.ifc_buf = NULL;
 	} else {
-		size_t len =((ifc32.ifc_len / sizeof (struct ifreq32)) + 1) *
+		ifc.ifc_len = ((ifc32.ifc_len / sizeof (struct ifreq32)) + 1) *
 			sizeof (struct ifreq);
-		uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
-		ifc.ifc_len = len;
-		ifr = ifc.ifc_req = (void __user *)(uifc + 1);
-		ifr32 = compat_ptr(ifc32.ifcbuf);
-		for (i = 0; i < ifc32.ifc_len; i += sizeof (struct ifreq32)) {
-			if (copy_in_user(ifr, ifr32, sizeof(struct ifreq32)))
-				return -EFAULT;
-			ifr++;
-			ifr32++; 
-		}
+		/* should the size be limited? -arnd */
+		ifc.ifc_buf = kmalloc (ifc.ifc_len, GFP_KERNEL);
+		if (!ifc.ifc_buf)
+			return -ENOMEM;
 	}
-	if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
-		return -EFAULT;
-
-	err = sys_ioctl (fd, SIOCGIFCONF, (unsigned long)uifc);	
-	if (err)
-		return err;
-
-	if (copy_from_user(&ifc, uifc, sizeof(struct ifconf))) 
-		return -EFAULT;
-
 	ifr = ifc.ifc_req;
 	ifr32 = compat_ptr(ifc32.ifcbuf);
-	for (i = 0, j = 0; i < ifc32.ifc_len && j < ifc.ifc_len;
-	     i += sizeof (struct ifreq32), j += sizeof (struct ifreq)) {
-		if (copy_in_user(ifr32, ifr, sizeof (struct ifreq32)))
+	for (i = 0; i < ifc32.ifc_len; i += sizeof (struct ifreq32)) {
+		if (copy_from_user(ifr, ifr32, sizeof (struct ifreq32))) {
+			kfree (ifc.ifc_buf);
 			return -EFAULT;
+		}
 		ifr32++;
 		ifr++;
 	}
-
-	if (ifc32.ifcbuf == 0) {
-		/* Translate from 64-bit structure multiple to
-		 * a 32-bit one.
-		 */
-		i = ifc.ifc_len;
-		i = ((i / sizeof(struct ifreq)) * sizeof(struct ifreq32));
-		ifc32.ifc_len = i;
-	} else {
-		if (i <= ifc32.ifc_len)
-			ifc32.ifc_len = i;
-		else
-			ifc32.ifc_len = i - sizeof (struct ifreq32);
+	old_fs = get_fs(); set_fs (KERNEL_DS);
+	err = sys_ioctl (fd, SIOCGIFCONF, (unsigned long)&ifc);	
+	set_fs (old_fs);
+	if (!err) {
+		ifr = ifc.ifc_req;
+		ifr32 = compat_ptr(ifc32.ifcbuf);
+		for (i = 0, j = 0; i < ifc32.ifc_len && j < ifc.ifc_len;
+		     i += sizeof (struct ifreq32), j += sizeof (struct ifreq)) {
+			int k = copy_to_user(ifr32, ifr, sizeof (struct ifreq32));
+			ifr32++;
+			ifr++;
+			if (k) {
+				err = -EFAULT;
+				break;
+			}
+		       
+		}
+		if (!err) {
+			if (ifc32.ifcbuf == 0) {
+				/* Translate from 64-bit structure multiple to
+				 * a 32-bit one.
+				 */
+				i = ifc.ifc_len;
+				i = ((i / sizeof(struct ifreq)) * sizeof(struct ifreq32));
+				ifc32.ifc_len = i;
+			} else {
+				if (i <= ifc32.ifc_len)
+					ifc32.ifc_len = i;
+				else
+					ifc32.ifc_len = i - sizeof (struct ifreq32);
+			}
+			if (copy_to_user(compat_ptr(arg), &ifc32, sizeof(struct ifconf32)))
+				err = -EFAULT;
+		}
 	}
-	if (copy_to_user(compat_ptr(arg), &ifc32, sizeof(struct ifconf32)))
-		return -EFAULT;
-
-	return 0;
+	if(ifc.ifc_buf != NULL)
+		kfree (ifc.ifc_buf);
+	return err;
 }
 
 static int ethtool_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
#1 lore raw
Quoted 5 lines · Jeremy Huddleston

A change between 2.6.7-rc1-bk6 and 2.6.7-rc2 can results in a system lockdown. One situation in which this can be triggered is when we are a nfs client, and the nfs server shuts down (or on the server, you simply issue an 'ifconfig eth1 down'). I have tracked this down to a bug in dev_ifconf() in fs/compat_ioctl.c

Ok, well I've cleaned up the patch a bit to be less intrusive, but the functionality is still pretty much the same (reverted to pre 2.6.7-rc2).

I've rattled my brain with it, but for the life of me, I don't see why the >=2.6.7-rc2 approach breaks... I've added some debugging printk's, and all I've really discovered is that when the server goes down, the client's dev_ifconf() gets called every few seconds until it goes back up... and the ifc.ifc_len=11040

The 11K isn't enough to overflow the stack, with the compat_alloc_user_space()...

copy_in_user() is fine...

The overlying logic is identical except that we use the user stack instead of kmalloc() and don't switch to KERNEL_DS, so what gives? Anybody? I'm ready to just give up and use this patch as is, but I'd like to figure out the root of this...

Signature · 1 line
Jeremy Huddleston <[email protected]>
fs/compat_ioctl.c +31 -19
@@ -495,11 +495,11 @@ static int dev_ifconf(unsigned int fd, u
 {
 	struct ifconf32 ifc32;
 	struct ifconf ifc;
-	struct ifconf __user *uifc;
 	struct ifreq32 __user *ifr32;
-	struct ifreq __user *ifr;
+	struct ifreq *ifr;
+	mm_segment_t old_fs;
 	unsigned int i, j;
-	int err;
+	int err = 0;
 
 	if (copy_from_user(&ifc32, compat_ptr(arg), sizeof(struct ifconf32)))
 		return -EFAULT;
@@ -508,37 +508,46 @@ static int dev_ifconf(unsigned int fd, u
 		ifc32.ifc_len = 0;
 		ifc.ifc_len = 0;
 		ifc.ifc_req = NULL;
-		uifc = compat_alloc_user_space(sizeof(struct ifconf));
 	} else {
 		size_t len =((ifc32.ifc_len / sizeof (struct ifreq32)) + 1) *
 			sizeof (struct ifreq);
-		uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
 		ifc.ifc_len = len;
-		ifr = ifc.ifc_req = (void __user *)(uifc + 1);
+
+		/* NOTE: Using compat_alloc_user_space() and calling sys_ioctl()
+		 * without first entering KERNEL_DS for some reason causes a
+		 * lockup on sparc64 -Jeremy Huddleston <[email protected]>
+		 */
+		ifc.ifc_buf = kmalloc (len, GFP_KERNEL);
+		if (!ifc.ifc_buf)
+			return -ENOMEM;
+
+		ifr = ifc.ifc_req;
 		ifr32 = compat_ptr(ifc32.ifcbuf);
 		for (i = 0; i < ifc32.ifc_len; i += sizeof (struct ifreq32)) {
-			if (copy_in_user(ifr, ifr32, sizeof(struct ifreq32)))
-				return -EFAULT;
+			if (copy_from_user(ifr, ifr32, sizeof(struct ifreq32))) {
+				err = -EFAULT;
+				goto __dev_ifconf_exit;
+			}
 			ifr++;
 			ifr32++; 
 		}
 	}
-	if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
-		return -EFAULT;
 
-	err = sys_ioctl (fd, SIOCGIFCONF, (unsigned long)uifc);	
-	if (err)
-		return err;
+	old_fs = get_fs(); set_fs (KERNEL_DS);
+	err = sys_ioctl (fd, SIOCGIFCONF, (unsigned long)&ifc);	
+	set_fs (old_fs);
 
-	if (copy_from_user(&ifc, uifc, sizeof(struct ifconf))) 
-		return -EFAULT;
+	if(err)
+		goto __dev_ifconf_exit;
 
 	ifr = ifc.ifc_req;
 	ifr32 = compat_ptr(ifc32.ifcbuf);
 	for (i = 0, j = 0; i < ifc32.ifc_len && j < ifc.ifc_len;
 	     i += sizeof (struct ifreq32), j += sizeof (struct ifreq)) {
-		if (copy_in_user(ifr32, ifr, sizeof (struct ifreq32)))
-			return -EFAULT;
+		if (copy_to_user(ifr32, ifr, sizeof (struct ifreq32))) {
+			err = -EFAULT;
+			goto __dev_ifconf_exit;
+		}
 		ifr32++;
 		ifr++;
 	}
@@ -557,9 +566,12 @@ static int dev_ifconf(unsigned int fd, u
 			ifc32.ifc_len = i - sizeof (struct ifreq32);
 	}
 	if (copy_to_user(compat_ptr(arg), &ifc32, sizeof(struct ifconf32)))
-		return -EFAULT;
+		err = -EFAULT;
 
-	return 0;
+	__dev_ifconf_exit:
+	if(ifc.ifc_buf != NULL)
+		kfree (ifc.ifc_buf);
+	return err;
 }
 
 static int ethtool_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
#2 lore raw
In reply to Jeremy Huddleston
Quoted 5 lines · Jeremy Huddleston

A change between 2.6.7-rc1-bk6 and 2.6.7-rc2 can results in a system lockdown. One situation in which this can be triggered is when we are a nfs client, and the nfs server shuts down (or on the server, you simply issue an 'ifconfig eth1 down'). I have tracked this down to a bug in dev_ifconf() in fs/compat_ioctl.c

Ok, I was mistaken. The bug isn't triggered by the NFS server becoming unavailable. It's actually triggered by NIS. ypbind is the process which eventually results in the dev_ifconf() call which locks up the system.

This problem was originally reported in this post
http://marc.theaimsgroup.com/?l=linux-sparc&m=109830513430004&w=2 and I confirmed with the poster of that message that they were using NIS as well and their NIS server was the same as the NFS server.

Sorry for the confusion.

Signature · 1 line
Jeremy Huddleston <[email protected]>