Skip to contentSkip to conversation

Fix kmem read on 32-bit archs

PATCH In review Chuck Ebbert 3 participants 4 messages Aug 14, 2005 Char and misc drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix kmem read on 32-bit archs

The first thing drivers/char/mem.c:read_kmem does is convert the
loff_t it gets as the offset for reading into an unsigned int. This patch makes the kmem driver's llseek operator do that up-front, so that fs/read_write.c:rw_verify_area doesn't return -EINVAL when we try to read from higher addresses.

It's ugly but so is the existing code. And it won't fix 64-bit
archs AFAICT. Tested on 2.6.11, patch offsets fixed up for 2.6.13-rc6.

drivers/char/mem.c +24 -1
Index: 2.6.13-rc6/drivers/char/mem.c
===================================================================
@@ -747,6 +747,29 @@
 	return ret;
 }
 
+static loff_t kmem_lseek(struct file * file, loff_t offset, int orig)
+{
+	loff_t ret = -EINVAL;
+
+	if (orig == 2)  /* sys_seek/llseek has checked orig = {012} */
+		goto out;
+
+	down(&file->f_dentry->d_inode->i_sem);
+	switch (orig) {
+		case 0:
+			file->f_pos = offset;
+			break;
+		case 1:
+			file->f_pos += offset;
+	}
+	ret = file->f_pos;
+	file->f_pos = (loff_t)(unsigned long)file->f_pos;
+	force_successful_syscall_return();
+	up(&file->f_dentry->d_inode->i_sem);
+out:
+	return ret;
+}
+
 static int open_port(struct inode * inode, struct file * filp)
 {
 	return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
@@ -769,7 +792,7 @@
 };
 
 static struct file_operations kmem_fops = {
-	.llseek		= memory_lseek,
+	.llseek		= kmem_lseek,
 	.read		= read_kmem,
 	.write		= write_kmem,
 	.mmap		= mmap_kmem,

__
Chuck

#1 lore raw
Quoted 8 lines · Chuck Ebbert

The first thing drivers/char/mem.c:read_kmem does is convert the
loff_t it gets as the offset for reading into an unsigned int. This patch makes the kmem driver's llseek operator do that up-front, so that fs/read_write.c:rw_verify_area doesn't return -EINVAL when we try to read from higher addresses.

It's ugly but so is the existing code. And it won't fix 64-bit
archs AFAICT. Tested on 2.6.11, patch offsets fixed up for 2.6.13-rc6.

I already have a full patch for this issue queued for 2.6.14.

-Andi

#2 lore raw
In reply to Chuck Ebbert
Quoted 6 lines · Chuck Ebbert

The first thing drivers/char/mem.c:read_kmem does is convert the
loff_t it gets as the offset for reading into an unsigned int. This patch makes the kmem driver's llseek operator do that up-front, so that fs/read_write.c:rw_verify_area doesn't return -EINVAL when we try to read from higher addresses.

Why would rw_verify_area() return -EINVAL? It does

        if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
                goto Einval;

but pos and "loff_t" is 64-bit, and if the thing is negative in 64 bits, then that thing really _should_ fail.

Yes, the thing needs to be opened with O_LARGEFILE and you need to use "llseek()" to seek into it, but once you do that, everything should be fine.

Linus

#3 lore raw
In reply to Chuck Ebbert
Quoted 3 lines · Chuck Ebbert

It's ugly but so is the existing code. And it won't fix 64-bit
archs AFAICT. Tested on 2.6.11, patch offsets fixed up for 2.6.13-rc6.

Btw, if you really want to allow negative ("huge positive") loff_t, then you should do it the way we did "file->f_maxcount", and just have a u64 "file->f_maxpos" that we initialize to LONG_LONG_MAX or something like that by default.

Then, the kmem_open() routine could set it to ULONG_LONG_MAX, and the rw_verify_area check would change from comparing pos for being negative to an unsigned compare of pos against "f_maxpos".

(That would also allow filesystems to limit the position to 32-bit values if they wanted to, and felt that they didn't want to even handle anything else).

Linus