Skip to contentSkip to conversation

bugfix for drivers/char/mem.c

PATCH In review Steven Hand 2 participants 3 messages Aug 1, 1999 – Aug 2, 1999

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bugfix for drivers/char/mem.c

There is a bug in the function read_kmem() in drivers/char/mem.c - the file position is incorrectly updated after a read() from /dev/kmem. This means that successive reads from /dev/kmem without an intervening seek will return incorrect data.

The bug has been around for quite a while, and is present in all the 2.2.x kernels. The following patch (against v.2.3.12) fixes it.

S.

drivers/char/mem.c renamed from linux/drivers/char/mem.c +8 -5
@@ -245,11 +245,14 @@
 		count -= read;
 	}
 
-	virtr = vread(buf, (char *)p, count);
-	if (virtr < 0)
-		return virtr;
-	*ppos += p + virtr;
-	return virtr + read;
+	if(count) {
+		if((virtr = vread(buf, (char *)p, count)) < 0)
+			return virtr;
+		read += virtr;
+	}
+
+	*ppos += read;
+	return read;
 }
 
 /*

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://humbolt.geo.uu.nl/Linux-MM/

#1 lore raw
Quoted 25 lines

There is a bug in the function read_kmem() in drivers/char/mem.c - the file position is incorrectly updated after a read() from /dev/kmem. This means that successive reads from /dev/kmem without an intervening seek will return incorrect data.

The bug has been around for quite a while, and is present in all the 2.2.x kernels. The following patch (against v.2.3.12) fixes it.

S.

drivers/char/mem.c renamed from linux/drivers/char/mem.c +1 -5
@@ -245,11 +245,14 @@
 		count -= read;
 	}
 
-	virtr = vread(buf, (char *)p, count);
-	if (virtr < 0)
-		return virtr;
-	*ppos += p + virtr;
-	return virtr + read;
+	if(count) {

drivers/char/mem.c line 258

Quoted 10 lines
drivers/char/mem.c renamed from linux/drivers/char/mem.c +7 -0
+		if((virtr = vread(buf, (char *)p, count)) < 0)
+			return virtr;
+		read += virtr;
+	}
+
+	*ppos += read;
+	return read;
 }
 
 /*

I see the problem. Can I suggest a simpler fix, based on the fact that vread() returns 0 when count == 0?

- *ppos += p + virtr;
+ *ppos += read + virtr;

Kanoj
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://humbolt.geo.uu.nl/Linux-MM/

#2 lore raw

Review

Quoted 5 lines

I see the problem. Can I suggest a simpler fix, based on the fact that vread() returns 0 when count == 0?

(quoted diff) +1 -1
-	*ppos += p + virtr;
+	*ppos += read + virtr;

Sure if you prefer it.

The main reason I added the if(count) { ... } was just to avoid calling vread() when count is zero (since in this case there's no entry to find on the vmlist, and hence the for loop in vread() will just iterate over all vmalloc()'d areas before returning zero.)

S.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://humbolt.geo.uu.nl/Linux-MM/