Skip to contentSkip to conversation

devmem: handle partial kmem write/read

PATCH In review Wu Fengguang 4 participants 32 messages Sep 14, 2009 – Sep 18, 2009

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

PATCH 1/1 devmem: handle partial kmem write/read

Current vwrite silently ignores vwrite() to vmap holes. Better behavior should be stop the write and return on such holes.

Also return on partial read, which may happen in future (eg. hit hwpoison pages).

---
 drivers/char/mem.c |   30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/char/mem.c +18 -12
@@ -444,18 +444,22 @@ static ssize_t read_kmem(struct file *fi
 		if (!kbuf)
 			return -ENOMEM;
 		while (count > 0) {
+			unsigned long n;
+
 			sz = size_inside_page(p, count);
-			sz = vread(kbuf, (char *)p, sz);
-			if (!sz)
+			n = vread(kbuf, (char *)p, sz);
+			if (!n)
 				break;
-			if (copy_to_user(buf, kbuf, sz)) {
+			if (copy_to_user(buf, kbuf, n)) {
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			count -= sz;
-			buf += sz;
-			read += sz;
-			p += sz;
+			count -= n;
+			buf += n;
+			read += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}
@@ -551,11 +555,13 @@ static ssize_t write_kmem(struct file * 
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			sz = vwrite(kbuf, (char *)p, sz);
-			count -= sz;
-			buf += sz;
-			virtr += sz;
-			p += sz;
+			n = vwrite(kbuf, (char *)p, sz);
+			count -= n;
+			buf += n;
+			virtr += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}
#1 lore raw

Hi Kame,

This patch needs more work. I first intent to fix a bug:

                        sz = vwrite(kbuf, (char *)p, sz);
                        p += sz;
                }

So if the returned len is 0, the kbuf/p pointers will mismatch.

Then I realize it changed the write behavior. The current vwrite() behavior is strange, it returns 0 if _whole range_ is hole, otherwise ignores the hole silently. So holes can be treated differently even in the original code.

I'm not really sure about the right behavior. KAME-san, do you have any suggestions?

Thanks,
Fengguang

Quoted 64 lines · Wu Fengguang

Current vwrite silently ignores vwrite() to vmap holes. Better behavior should be stop the write and return on such holes.

Also return on partial read, which may happen in future (eg. hit hwpoison pages).

---
 drivers/char/mem.c |   30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/char/mem.c +18 -12
@@ -444,18 +444,22 @@ static ssize_t read_kmem(struct file *fi
 		if (!kbuf)
 			return -ENOMEM;
 		while (count > 0) {
+			unsigned long n;
+
 			sz = size_inside_page(p, count);
-			sz = vread(kbuf, (char *)p, sz);
-			if (!sz)
+			n = vread(kbuf, (char *)p, sz);
+			if (!n)
 				break;
-			if (copy_to_user(buf, kbuf, sz)) {
+			if (copy_to_user(buf, kbuf, n)) {
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			count -= sz;
-			buf += sz;
-			read += sz;
-			p += sz;
+			count -= n;
+			buf += n;
+			read += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}
@@ -551,11 +555,13 @@ static ssize_t write_kmem(struct file * 
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			sz = vwrite(kbuf, (char *)p, sz);
-			count -= sz;
-			buf += sz;
-			virtr += sz;
-			p += sz;
+			n = vwrite(kbuf, (char *)p, sz);
+			count -= n;
+			buf += n;
+			virtr += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}
#2 lore raw
Quoted 8 lines · On Mon, 14 Sep 2009 12:34:44 +0800 Wu Fengguang <[email protected]> wrote:

Hi Kame,

This patch needs more work. I first intent to fix a bug:

                        sz = vwrite(kbuf, (char *)p, sz);
                        p += sz;
                }
Quoted 7 lines

So if the returned len is 0, the kbuf/p pointers will mismatch.

Then I realize it changed the write behavior. The current vwrite() behavior is strange, it returns 0 if _whole range_ is hole, otherwise ignores the hole silently. So holes can be treated differently even in the original code.

Ah, ok...

Quoted 3 lines

I'm not really sure about the right behavior. KAME-san, do you have any suggestions?

maybe following make sense.
=
written = vwrite(kbuf, (char *p), sz);
if (!written) // whole vmem was a hole

	written = sz;

==
needs fix.

Anyway, I wonder kmem is broken. It's should be totally rewritten.

For example, this doesn't check anything.
==

        if (p < (unsigned long) high_memory) {

==

But....are there users ?
If necessary, I'll write some...

Thanks,
-Kame

Quoted 74 lines

Thanks,
Fengguang

Quoted 54 lines · Wu Fengguang

Current vwrite silently ignores vwrite() to vmap holes. Better behavior should be stop the write and return on such holes.

Also return on partial read, which may happen in future (eg. hit hwpoison pages).

---
 drivers/char/mem.c |   30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/char/mem.c +11 -12
@@ -444,18 +444,22 @@ static ssize_t read_kmem(struct file *fi
 		if (!kbuf)
 			return -ENOMEM;
 		while (count > 0) {
+			unsigned long n;
+
 			sz = size_inside_page(p, count);
-			sz = vread(kbuf, (char *)p, sz);
-			if (!sz)
+			n = vread(kbuf, (char *)p, sz);
+			if (!n)
 				break;
-			if (copy_to_user(buf, kbuf, sz)) {
+			if (copy_to_user(buf, kbuf, n)) {
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			count -= sz;
-			buf += sz;
-			read += sz;
-			p += sz;
+			count -= n;
+			buf += n;
+			read += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}
@@ -551,11 +555,13 @@ static ssize_t write_kmem(struct file * 
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			sz = vwrite(kbuf, (char *)p, sz);
-			count -= sz;
-			buf += sz;
-			virtr += sz;
-			p += sz;

drivers/char/mem.c static ssize_t write_kmem(struct file * · line 567

Quoted 10 lines
drivers/char/mem.c +7 -0
+			n = vwrite(kbuf, (char *)p, sz);
+			count -= n;
+			buf += n;
+			virtr += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#3 lore raw
Quoted 11 lines · On Tue, 15 Sep 2009 09:24:48 +0900 KAMEZAWA Hiroyuki…
Quoted 8 lines · On Mon, 14 Sep 2009 12:34:44 +0800 Wu Fengguang <[email protected]> wrote:

Hi Kame,

This patch needs more work. I first intent to fix a bug:

                        sz = vwrite(kbuf, (char *)p, sz);
                        p += sz;
                }
Quoted 9 lines
Quoted 7 lines

So if the returned len is 0, the kbuf/p pointers will mismatch.

Then I realize it changed the write behavior. The current vwrite() behavior is strange, it returns 0 if _whole range_ is hole, otherwise ignores the hole silently. So holes can be treated differently even in the original code.

Ah, ok...

I'd like to post a fix...but it seems you are woriking. Hmm, I'll post a patch including your fix (and your sign.) Is it ok ?

Thanks,
-Kame

Quoted 107 lines
Quoted 3 lines

I'm not really sure about the right behavior. KAME-san, do you have any suggestions?

maybe following make sense.
=
written = vwrite(kbuf, (char *p), sz);
if (!written) // whole vmem was a hole

	written = sz;

==
needs fix.

Anyway, I wonder kmem is broken. It's should be totally rewritten.

For example, this doesn't check anything.
==

        if (p < (unsigned long) high_memory) {

==

But....are there users ?
If necessary, I'll write some...

Thanks,
-Kame

Quoted 65 lines

Thanks,
Fengguang

Quoted 61 lines · Wu Fengguang

Current vwrite silently ignores vwrite() to vmap holes. Better behavior should be stop the write and return on such holes.

Also return on partial read, which may happen in future (eg. hit hwpoison pages).

---
 drivers/char/mem.c |   30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/char/mem.c +18 -12
@@ -444,18 +444,22 @@ static ssize_t read_kmem(struct file *fi
 		if (!kbuf)
 			return -ENOMEM;
 		while (count > 0) {
+			unsigned long n;
+
 			sz = size_inside_page(p, count);
-			sz = vread(kbuf, (char *)p, sz);
-			if (!sz)
+			n = vread(kbuf, (char *)p, sz);
+			if (!n)
 				break;
-			if (copy_to_user(buf, kbuf, sz)) {
+			if (copy_to_user(buf, kbuf, n)) {
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			count -= sz;
-			buf += sz;
-			read += sz;
-			p += sz;
+			count -= n;
+			buf += n;
+			read += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}
@@ -551,11 +555,13 @@ static ssize_t write_kmem(struct file * 
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			sz = vwrite(kbuf, (char *)p, sz);
-			count -= sz;
-			buf += sz;
-			virtr += sz;
-			p += sz;
+			n = vwrite(kbuf, (char *)p, sz);
+			count -= n;
+			buf += n;
+			virtr += n;
+			p += n;
+			if (n < sz)
+				break;
Quoted 9 lines

drivers/char/mem.c static ssize_t read_kmem(struct file *fi · line 465

Quoted 3 lines
drivers/char/mem.c +0 -0
 		}
 		free_page((unsigned long)kbuf);
 	}

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#4 lore raw
Quoted 21 lines · KAMEZAWA Hiroyuki
Quoted 18 lines · On Tue, 15 Sep 2009 09:24:48 +0900 KAMEZAWA Hiroyuki…
Quoted 15 lines · On Mon, 14 Sep 2009 12:34:44 +0800 Wu Fengguang <[email protected]> wrote:

Hi Kame,

This patch needs more work. I first intent to fix a bug:

                        sz = vwrite(kbuf, (char *)p, sz);
                        p += sz;
                }

So if the returned len is 0, the kbuf/p pointers will mismatch.

Then I realize it changed the write behavior. The current vwrite() behavior is strange, it returns 0 if _whole range_ is hole, otherwise ignores the hole silently. So holes can be treated differently even in the original code.

Quoted 6 lines
Quoted 2 lines

Ah, ok...

I'd like to post a fix...but it seems you are woriking. Hmm, I'll post a patch including your fix (and your sign.) Is it ok ?

Sure OK. You can work based on my patches. Many of them are in -mm now and there are three more. I'll show you in a series.

Thanks,
Fengguang

Quoted 107 lines
Quoted 107 lines
Quoted 3 lines

I'm not really sure about the right behavior. KAME-san, do you have any suggestions?

maybe following make sense.
=
written = vwrite(kbuf, (char *p), sz);
if (!written) // whole vmem was a hole

	written = sz;

==
needs fix.

Anyway, I wonder kmem is broken. It's should be totally rewritten.

For example, this doesn't check anything.
==

        if (p < (unsigned long) high_memory) {

==

But....are there users ?
If necessary, I'll write some...

Thanks,
-Kame

Quoted 65 lines

Thanks,
Fengguang

Quoted 61 lines · Wu Fengguang

Current vwrite silently ignores vwrite() to vmap holes. Better behavior should be stop the write and return on such holes.

Also return on partial read, which may happen in future (eg. hit hwpoison pages).

---
 drivers/char/mem.c |   30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/char/mem.c +18 -12
@@ -444,18 +444,22 @@ static ssize_t read_kmem(struct file *fi
 		if (!kbuf)
 			return -ENOMEM;
 		while (count > 0) {
+			unsigned long n;
+
 			sz = size_inside_page(p, count);
-			sz = vread(kbuf, (char *)p, sz);
-			if (!sz)
+			n = vread(kbuf, (char *)p, sz);
+			if (!n)
 				break;
-			if (copy_to_user(buf, kbuf, sz)) {
+			if (copy_to_user(buf, kbuf, n)) {
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			count -= sz;
-			buf += sz;
-			read += sz;
-			p += sz;
+			count -= n;
+			buf += n;
+			read += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}
@@ -551,11 +555,13 @@ static ssize_t write_kmem(struct file * 
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			sz = vwrite(kbuf, (char *)p, sz);
-			count -= sz;
-			buf += sz;
-			virtr += sz;
-			p += sz;
+			n = vwrite(kbuf, (char *)p, sz);
+			count -= n;
+			buf += n;
+			virtr += n;
+			p += n;
+			if (n < sz)
+				break;
Quoted 9 lines

drivers/char/mem.c static ssize_t read_kmem(struct file *fi · line 465

Quoted 3 lines
drivers/char/mem.c +0 -0
 		}
 		free_page((unsigned long)kbuf);
 	}

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#5 lore raw
In reply to KAMEZAWA Hiroyuki
Quoted 20 lines · KAMEZAWA Hiroyuki
Quoted 8 lines · On Mon, 14 Sep 2009 12:34:44 +0800 Wu Fengguang <[email protected]> wrote:

Hi Kame,

This patch needs more work. I first intent to fix a bug:

                        sz = vwrite(kbuf, (char *)p, sz);
                        p += sz;
                }
Quoted 7 lines

So if the returned len is 0, the kbuf/p pointers will mismatch.

Then I realize it changed the write behavior. The current vwrite() behavior is strange, it returns 0 if _whole range_ is hole, otherwise ignores the hole silently. So holes can be treated differently even in the original code.

Ah, ok...

Quoted 8 lines
Quoted 3 lines

I'm not really sure about the right behavior. KAME-san, do you have any suggestions?

maybe following make sense.
=
written = vwrite(kbuf, (char *p), sz);
if (!written) // whole vmem was a hole

	written = sz;

Since the 0 return value won't be used at all, it would be simpler to tell vwrite() return the untouched buflen/sz, like this. It will ignore _all_ the holes silently. Need to update comments too.

mm/vmalloc.c 2009-09-15 09:43:33.000000000 +0800 renamed from mm/vmalloc.c 2009-09-15 09:40:08.000000000 +0800 +0 -4
@@ -1834,7 +1834,6 @@ long vwrite(char *buf, char *addr, unsig                                                                       
        struct vm_struct *tmp;                                                                                                       
        char *vaddr;                                                                                                                 
        unsigned long n, buflen;                                                                                                     
-       int copied = 0;                                                                                                              
                                                                                                                                     
        /* Don't allow overflow */                                                                                                   
        if ((unsigned long) addr + count < count)                                                                                    
@@ -1858,7 +1857,6 @@ long vwrite(char *buf, char *addr, unsig                                                                       
                        n = count;                                                                                                   
                if (!(tmp->flags & VM_IOREMAP)) {                                                                                    
                        aligned_vwrite(buf, addr, n);                                                                                
-                       copied++;                                                                                                    
                }                                                                                                                    
                buf += n;                                                                                                            
                addr += n;                                                                                                           
@@ -1866,8 +1864,6 @@ long vwrite(char *buf, char *addr, unsig                                                                       
        }                                                                                                                            
 finished:                                                                                                                           
        read_unlock(&vmlist_lock);                                                                                                   
-       if (!copied)                                                                                                                 
-               return 0;                                                                                                            
        return buflen;                                                                                                               
 }                                                                                                                                   
 
Quoted 5 lines

==
needs fix.

Anyway, I wonder kmem is broken. It's should be totally rewritten.

Quoted 8 lines

For example, this doesn't check anything.
==

        if (p < (unsigned long) high_memory) {

==

But....are there users ?
If necessary, I'll write some...

I'm trying to stop possible mem/kmem users to access hwpoison pages.. I'm not the user, but rather a tester ;)

Thanks,
Fengguang

Quoted 78 lines

Thanks,
-Kame

Quoted 74 lines

Thanks,
Fengguang

Quoted 54 lines · Wu Fengguang

Current vwrite silently ignores vwrite() to vmap holes. Better behavior should be stop the write and return on such holes.

Also return on partial read, which may happen in future (eg. hit hwpoison pages).

---
 drivers/char/mem.c |   30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/char/mem.c +11 -12
@@ -444,18 +444,22 @@ static ssize_t read_kmem(struct file *fi
 		if (!kbuf)
 			return -ENOMEM;
 		while (count > 0) {
+			unsigned long n;
+
 			sz = size_inside_page(p, count);
-			sz = vread(kbuf, (char *)p, sz);
-			if (!sz)
+			n = vread(kbuf, (char *)p, sz);
+			if (!n)
 				break;
-			if (copy_to_user(buf, kbuf, sz)) {
+			if (copy_to_user(buf, kbuf, n)) {
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			count -= sz;
-			buf += sz;
-			read += sz;
-			p += sz;
+			count -= n;
+			buf += n;
+			read += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}
@@ -551,11 +555,13 @@ static ssize_t write_kmem(struct file * 
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			sz = vwrite(kbuf, (char *)p, sz);
-			count -= sz;
-			buf += sz;
-			virtr += sz;
-			p += sz;

drivers/char/mem.c static ssize_t write_kmem(struct file * · line 567

Quoted 10 lines
drivers/char/mem.c +7 -0
+			n = vwrite(kbuf, (char *)p, sz);
+			count -= n;
+			buf += n;
+			virtr += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#6 lore raw
Quoted 30 lines · On Tue, 15 Sep 2009 10:02:08 +0800 Wu Fengguang <[email protected]> wrote:
Quoted 20 lines · KAMEZAWA Hiroyuki
Quoted 8 lines · On Mon, 14 Sep 2009 12:34:44 +0800 Wu Fengguang <[email protected]> wrote:

Hi Kame,

This patch needs more work. I first intent to fix a bug:

                        sz = vwrite(kbuf, (char *)p, sz);
                        p += sz;
                }
Quoted 7 lines

So if the returned len is 0, the kbuf/p pointers will mismatch.

Then I realize it changed the write behavior. The current vwrite() behavior is strange, it returns 0 if _whole range_ is hole, otherwise ignores the hole silently. So holes can be treated differently even in the original code.

Ah, ok...

Quoted 8 lines
Quoted 3 lines

I'm not really sure about the right behavior. KAME-san, do you have any suggestions?

maybe following make sense.
=
written = vwrite(kbuf, (char *p), sz);
if (!written) // whole vmem was a hole

	written = sz;
Quoted 4 lines

Since the 0 return value won't be used at all, it would be simpler to tell vwrite() return the untouched buflen/sz, like this. It will ignore _all_ the holes silently. Need to update comments too.

Hmm. IIUC the original kmem code returns immediately if vread/vwrite returns 0. But it seems this depends on wrong assumption that vmap area is continuous, at the first look.

In man(4) mem,kmem
==
Byte addresses in mem are interpreted as physical memory addresses. References to nonexistent locations cause errors to be returned.
.....
The file kmem is the same as mem, except that the kernel virtual memory rather than physical memory is accessed.

==

Then, we have to return error for accesses to "nonexistent locations".

memory-hole in vmap area is ....."nonexistent" ?
I think it's nonexistent if there are no overlaps between requested [pos, pos+len) and registerred vmalloc area.

But, hmm, there are no way for users to know "existing vmalloc area". Then, my above idea may be wrong.

Then, I'd like to modify as following,

- If is_vmalloc_or_module_addr(requested address) is false, return -EFAULT.
- If is_vmalloc_or_module_addr(requested address) is true, return no error. Even if specified range doesn't include no exsiting vmalloc area.

How do you think ?

Thanks,
-Kame
p.s. I wonder current /dev/kmem cannot read text area of kernel if it's not directly mapped.

Quoted 126 lines
mm/vmalloc.c 2009-09-15 09:43:33.000000000 +0800 renamed from mm/vmalloc.c 2009-09-15 09:40:08.000000000 +0800 +0 -4
@@ -1834,7 +1834,6 @@ long vwrite(char *buf, char *addr, unsig                                                                       
        struct vm_struct *tmp;                                                                                                       
        char *vaddr;                                                                                                                 
        unsigned long n, buflen;                                                                                                     
-       int copied = 0;                                                                                                              
 
        /* Don't allow overflow */                                                                                                   
        if ((unsigned long) addr + count < count)                                                                                    
@@ -1858,7 +1857,6 @@ long vwrite(char *buf, char *addr, unsig                                                                       
                        n = count;                                                                                                   
                if (!(tmp->flags & VM_IOREMAP)) {                                                                                    
                        aligned_vwrite(buf, addr, n);                                                                                
-                       copied++;                                                                                                    
                }                                                                                                                    
                buf += n;                                                                                                            
                addr += n;                                                                                                           
@@ -1866,8 +1864,6 @@ long vwrite(char *buf, char *addr, unsig                                                                       
        }                                                                                                                            
 finished:                                                                                                                           
        read_unlock(&vmlist_lock);                                                                                                   
-       if (!copied)                                                                                                                 
-               return 0;                                                                                                            
        return buflen;                                                                                                               
 }                                                                                                                                   
 
Quoted 5 lines

==
needs fix.

Anyway, I wonder kmem is broken. It's should be totally rewritten.

Quoted 8 lines

For example, this doesn't check anything.
==

        if (p < (unsigned long) high_memory) {

==

But....are there users ?
If necessary, I'll write some...

I'm trying to stop possible mem/kmem users to access hwpoison pages.. I'm not the user, but rather a tester ;)

Thanks,
Fengguang

Quoted 78 lines

Thanks,
-Kame

Quoted 74 lines

Thanks,
Fengguang

Quoted 54 lines · Wu Fengguang

Current vwrite silently ignores vwrite() to vmap holes. Better behavior should be stop the write and return on such holes.

Also return on partial read, which may happen in future (eg. hit hwpoison pages).

---
 drivers/char/mem.c |   30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/char/mem.c +11 -12
@@ -444,18 +444,22 @@ static ssize_t read_kmem(struct file *fi
 		if (!kbuf)
 			return -ENOMEM;
 		while (count > 0) {
+			unsigned long n;
+
 			sz = size_inside_page(p, count);
-			sz = vread(kbuf, (char *)p, sz);
-			if (!sz)
+			n = vread(kbuf, (char *)p, sz);
+			if (!n)
 				break;
-			if (copy_to_user(buf, kbuf, sz)) {
+			if (copy_to_user(buf, kbuf, n)) {
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			count -= sz;
-			buf += sz;
-			read += sz;
-			p += sz;
+			count -= n;
+			buf += n;
+			read += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}
@@ -551,11 +555,13 @@ static ssize_t write_kmem(struct file * 
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			sz = vwrite(kbuf, (char *)p, sz);
-			count -= sz;
-			buf += sz;
-			virtr += sz;
-			p += sz;

drivers/char/mem.c static ssize_t write_kmem(struct file * · line 567

Quoted 10 lines
drivers/char/mem.c +7 -0
+			n = vwrite(kbuf, (char *)p, sz);
+			count -= n;
+			buf += n;
+			virtr += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#7 lore raw
Quoted 33 lines · KAMEZAWA Hiroyuki
Quoted 30 lines · On Tue, 15 Sep 2009 10:02:08 +0800 Wu Fengguang <[email protected]> wrote:
Quoted 28 lines · KAMEZAWA Hiroyuki
Quoted 8 lines · On Mon, 14 Sep 2009 12:34:44 +0800 Wu Fengguang <[email protected]> wrote:

Hi Kame,

This patch needs more work. I first intent to fix a bug:

                        sz = vwrite(kbuf, (char *)p, sz);
                        p += sz;
                }
Quoted 7 lines

So if the returned len is 0, the kbuf/p pointers will mismatch.

Then I realize it changed the write behavior. The current vwrite() behavior is strange, it returns 0 if _whole range_ is hole, otherwise ignores the hole silently. So holes can be treated differently even in the original code.

Ah, ok...

Quoted 3 lines

I'm not really sure about the right behavior. KAME-san, do you have any suggestions?

maybe following make sense.
=
written = vwrite(kbuf, (char *p), sz);
if (!written) // whole vmem was a hole

	written = sz;
Quoted 6 lines
Quoted 4 lines

Since the 0 return value won't be used at all, it would be simpler to tell vwrite() return the untouched buflen/sz, like this. It will ignore _all_ the holes silently. Need to update comments too.

Hmm. IIUC the original kmem code returns immediately if vread/vwrite returns 0.

Agreed for vread. It seems vwrite don't do that for both kmem and vmalloc part.

Quoted 2 lines

But it seems this depends on wrong assumption that vmap area is continuous, at the first look.

You mean it is possible that not all pages are mapped in a vmlist addr,size range? Is vmalloc areas guaranteed to be page aligned? Sorry for the dumb questions.

Quoted 11 lines

In man(4) mem,kmem
==
Byte addresses in mem are interpreted as physical memory addresses. References to nonexistent locations cause errors to be returned.
.....
The file kmem is the same as mem, except that the kernel virtual memory rather than physical memory is accessed.

==

Then, we have to return error for accesses to "nonexistent locations".

That's one important factor to consider. On the other hand, the original kmem read/write implementation don't return error code for holes. Instead kmem read returns early, while kmem write ignores holes but is buggy..

Quoted 6 lines

memory-hole in vmap area is ....."nonexistent" ?
I think it's nonexistent if there are no overlaps between requested [pos, pos+len) and registerred vmalloc area.
But, hmm, there are no way for users to know "existing vmalloc area". Then, my above idea may be wrong.

Quoted 7 lines

Then, I'd like to modify as following,

- If is_vmalloc_or_module_addr(requested address) is false, return -EFAULT.
- If is_vmalloc_or_module_addr(requested address) is true, return no error. Even if specified range doesn't include no exsiting vmalloc area.

How do you think ?

Looks reasonable to me. But it's good to hear more wide opinions..

Quoted 4 lines

Thanks,
-Kame
p.s. I wonder current /dev/kmem cannot read text area of kernel if it's not directly mapped.

Attached is a small tool (from LKML) for reading 'modprobe_path' from kmem, it's not text, but is close..

Thanks,
Fengguang

Quoted 120 lines
Quoted 120 lines
mm/vmalloc.c 2009-09-15 09:43:33.000000000 +0800 renamed from mm/vmalloc.c 2009-09-15 09:40:08.000000000 +0800 +0 -4
@@ -1834,7 +1834,6 @@ long vwrite(char *buf, char *addr, unsig                                                                       
        struct vm_struct *tmp;                                                                                                       
        char *vaddr;                                                                                                                 
        unsigned long n, buflen;                                                                                                     
-       int copied = 0;                                                                                                              
 
        /* Don't allow overflow */                                                                                                   
        if ((unsigned long) addr + count < count)                                                                                    
@@ -1858,7 +1857,6 @@ long vwrite(char *buf, char *addr, unsig                                                                       
                        n = count;                                                                                                   
                if (!(tmp->flags & VM_IOREMAP)) {                                                                                    
                        aligned_vwrite(buf, addr, n);                                                                                
-                       copied++;                                                                                                    
                }                                                                                                                    
                buf += n;                                                                                                            
                addr += n;                                                                                                           
@@ -1866,8 +1864,6 @@ long vwrite(char *buf, char *addr, unsig                                                                       
        }                                                                                                                            
 finished:                                                                                                                           
        read_unlock(&vmlist_lock);                                                                                                   
-       if (!copied)                                                                                                                 
-               return 0;                                                                                                            
        return buflen;                                                                                                               
 }                                                                                                                                   
 
Quoted 5 lines

==
needs fix.

Anyway, I wonder kmem is broken. It's should be totally rewritten.

Quoted 8 lines

For example, this doesn't check anything.
==

        if (p < (unsigned long) high_memory) {

==

But....are there users ?
If necessary, I'll write some...

I'm trying to stop possible mem/kmem users to access hwpoison pages.. I'm not the user, but rather a tester ;)

Thanks,
Fengguang

Quoted 71 lines

Thanks,
-Kame

Quoted 67 lines

Thanks,
Fengguang

Quoted 63 lines · Wu Fengguang

Current vwrite silently ignores vwrite() to vmap holes. Better behavior should be stop the write and return on such holes.

Also return on partial read, which may happen in future (eg. hit hwpoison pages).

---
 drivers/char/mem.c |   30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/char/mem.c +18 -12
@@ -444,18 +444,22 @@ static ssize_t read_kmem(struct file *fi
 		if (!kbuf)
 			return -ENOMEM;
 		while (count > 0) {
+			unsigned long n;
+
 			sz = size_inside_page(p, count);
-			sz = vread(kbuf, (char *)p, sz);
-			if (!sz)
+			n = vread(kbuf, (char *)p, sz);
+			if (!n)
 				break;
-			if (copy_to_user(buf, kbuf, sz)) {
+			if (copy_to_user(buf, kbuf, n)) {
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			count -= sz;
-			buf += sz;
-			read += sz;
-			p += sz;
+			count -= n;
+			buf += n;
+			read += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}
@@ -551,11 +555,13 @@ static ssize_t write_kmem(struct file * 
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			sz = vwrite(kbuf, (char *)p, sz);
-			count -= sz;
-			buf += sz;
-			virtr += sz;
-			p += sz;
+			n = vwrite(kbuf, (char *)p, sz);
+			count -= n;
+			buf += n;
+			virtr += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines

drivers/char/mem.c static ssize_t write_kmem(struct file * · line 567

Quoted 1 line
drivers/char/mem.c +0 -0
 	}

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <sys/mman.h>

int page_size;
#define PAGE_SIZE page_size
#define PAGE_MASK (~(PAGE_SIZE-1))

void get_var (unsigned long addr) {
	off_t ptr = addr & ~(PAGE_MASK);
	off_t offset = addr & PAGE_MASK;
	int i = 0;
	char *map;
	static int kfd = -1;

	kfd = open("/dev/kmem",O_RDONLY);
	if (kfd < 0) {
		perror("open");
		exit(0);
	}

	map = mmap(NULL,PAGE_SIZE,PROT_READ,MAP_SHARED,kfd,offset);
	if (map == MAP_FAILED) {
		perror("mmap");
		exit(-1);
	}
	printf("%s\n",map+ptr);

	return;
}

int main(int argc, char **argv)

{
	FILE *fp;
	char addr_str[11]="0x";
	char var[51];
	unsigned long addr;
	char ch;
	int r;

	if (argc != 2) {
		fprintf(stderr,"usage: %s System.map\n",argv[0]);
		exit(-1);
	}

	if ((fp = fopen(argv[1],"r")) == NULL) {
		perror("fopen");
		exit(-1);
	}

	do {
		/* ffffffff81723880 D modprobe_path */
		r = fscanf(fp,"%16s %c %50s\n",&addr_str[2],&ch,var);
		if (strcmp(var,"modprobe_path")==0)
			break;
	} while(r > 0);
	if (r < 0) {
		printf("could not find modprobe_path\n");
		exit(-1);
	}
	page_size = getpagesize();
	addr = strtoul(addr_str,NULL,16);
	printf("found modprobe_path at (%s) %08lx\n",addr_str,addr);
	get_var(addr);

	return 0;
}
#8 lore raw

Question: how to handle too big f_pos Re: [PATCH] devmem: handle partial kmem write/read

In reply to Wu Fengguang

I'm writing a patch against /dev/kmem...I found a problem.

/dev/kmem (and /proc/<pid>/mem) puts virtual addres to f->f_pos.

And, f->f_pos is loff_t .... signed value (not unsigned)

Then, this check
rw_verify_area(READ, file, pos, count);
=>

        pos = *ppos;
        if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
                return retval;

always returns -EINVAL when I read /dev/kmem's valid address.

Then, how should I do for read /dev/kmem ? Any idea ? (Added Al Viro to CC:)

What I'm really afraid of is /proc/<pid>/mem. IIUC, it's used by gdb to snoop memory, (for example, at gcore).
I think gdb uses ptrace if it fails to read /proc/<pid>/mem but...it's deadly slow.

Thanks,
-Kame

p.s. no problems in /proc/kcore..its offset is not bare addresss.

Quoted 70 lines · On Mon, 14 Sep 2009 11:29:01 +0800 Wu Fengguang <[email protected]> wrote:

Current vwrite silently ignores vwrite() to vmap holes. Better behavior should be stop the write and return on such holes.

Also return on partial read, which may happen in future (eg. hit hwpoison pages).

---
 drivers/char/mem.c |   30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/char/mem.c +18 -13
@@ -444,18 +444,22 @@ static ssize_t read_kmem(struct file *fi
 		if (!kbuf)
 			return -ENOMEM;
 		while (count > 0) {
+			unsigned long n;
+
 			sz = size_inside_page(p, count);
-			sz = vread(kbuf, (char *)p, sz);
-			if (!sz)
+			n = vread(kbuf, (char *)p, sz);
+			if (!n)
 				break;
-			if (copy_to_user(buf, kbuf, sz)) {
+			if (copy_to_user(buf, kbuf, n)) {
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			count -= sz;
-			buf += sz;
-			read += sz;
-			p += sz;
+			count -= n;
+			buf += n;
+			read += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}
@@ -551,11 +555,13 @@ static ssize_t write_kmem(struct file * 
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			sz = vwrite(kbuf, (char *)p, sz);
-			count -= sz;
-			buf += sz;
-			virtr += sz;
-			p += sz;
+			n = vwrite(kbuf, (char *)p, sz);
+			count -= n;
+			buf += n;
+			virtr += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#9 lore raw

Re: Question: how to handle too big f_pos Re: [PATCH] devmem: handle partial kmem write/read

Good catch, Kame!

FYI mmap is OK. I succeeded in reading this address via kmem:

        ffffffff81723880 D modprobe_path

Thanks,
Fengguang

Quoted 105 lines · KAMEZAWA Hiroyuki

I'm writing a patch against /dev/kmem...I found a problem.

/dev/kmem (and /proc/<pid>/mem) puts virtual addres to f->f_pos.

And, f->f_pos is loff_t .... signed value (not unsigned)

Then, this check
rw_verify_area(READ, file, pos, count);
=>

        pos = *ppos;
        if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
                return retval;

always returns -EINVAL when I read /dev/kmem's valid address.

Then, how should I do for read /dev/kmem ? Any idea ? (Added Al Viro to CC:)

What I'm really afraid of is /proc/<pid>/mem. IIUC, it's used by gdb to snoop memory, (for example, at gcore).
I think gdb uses ptrace if it fails to read /proc/<pid>/mem but...it's deadly slow.

Thanks,
-Kame

p.s. no problems in /proc/kcore..its offset is not bare addresss.

Quoted 70 lines · On Mon, 14 Sep 2009 11:29:01 +0800 Wu Fengguang <[email protected]> wrote:

Current vwrite silently ignores vwrite() to vmap holes. Better behavior should be stop the write and return on such holes.

Also return on partial read, which may happen in future (eg. hit hwpoison pages).

---
 drivers/char/mem.c |   30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
drivers/char/mem.c +18 -13
@@ -444,18 +444,22 @@ static ssize_t read_kmem(struct file *fi
 		if (!kbuf)
 			return -ENOMEM;
 		while (count > 0) {
+			unsigned long n;
+
 			sz = size_inside_page(p, count);
-			sz = vread(kbuf, (char *)p, sz);
-			if (!sz)
+			n = vread(kbuf, (char *)p, sz);
+			if (!n)
 				break;
-			if (copy_to_user(buf, kbuf, sz)) {
+			if (copy_to_user(buf, kbuf, n)) {
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			count -= sz;
-			buf += sz;
-			read += sz;
-			p += sz;
+			count -= n;
+			buf += n;
+			read += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}
@@ -551,11 +555,13 @@ static ssize_t write_kmem(struct file * 
 				free_page((unsigned long)kbuf);
 				return -EFAULT;
 			}
-			sz = vwrite(kbuf, (char *)p, sz);
-			count -= sz;
-			buf += sz;
-			virtr += sz;
-			p += sz;
+			n = vwrite(kbuf, (char *)p, sz);
+			count -= n;
+			buf += n;
+			virtr += n;
+			p += n;
+			if (n < sz)
+				break;
 		}
 		free_page((unsigned long)kbuf);
 	}
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#10 lore raw

Re: Question: how to handle too big f_pos Re: [PATCH] devmem: handle partial kmem write/read

In reply to KAMEZAWA Hiroyuki
Quoted 9 lines · KAMEZAWA Hiroyuki

I'm writing a patch against /dev/kmem...I found a problem.

/dev/kmem (and /proc/<pid>/mem) puts virtual addres to f->f_pos.

And, f->f_pos is loff_t .... signed value (not unsigned)

Then, this check
rw_verify_area(READ, file, pos, count);
=>

Quoted 8 lines
        pos = *ppos;
        if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
                return retval;

always returns -EINVAL when I read /dev/kmem's valid address.

Then, how should I do for read /dev/kmem ? Any idea ? (Added Al Viro to CC:)

I believe what's supposed to happen is that special drivers like /dev/mem and /dev/kmem provide their own .llseek method (they do). But at some point along the way rw_verify_area() got "improved" and now prevents them working on many configurations.

I noticed around 2.6.24-rc1, and have been building my debug kernels with the patch below ever since then, as I sometimes like to peek and poke into /dev/kmem to examine and try different things while running.

But whether it's the right patch is another matter. Though this should be independent of that, I've also got a patch at the drivers/char/mem.c end (I'll post that shortly in the main thread, not appropriate here), and have never found time to consider whether these hacks amount to anything more than "works for me".

IIRC, this patch below only covers one aspect of the negative offset problem. Where the problems lie does change from time to time - back in 2003 I used to use pread() and pwrite() to avoid llseek() issues; but when I came back to it in 2007, I think I found those no longer worked (on i386 or x86_64 or powerpc?), and lseek64() was the best bet.

Anyway, for what it's worth, ...

fs/read_write.c +7 -2
@@ -222,8 +222,13 @@ int rw_verify_area(int read_write, struc
 	if (unlikely((ssize_t) count < 0))
 		return retval;
 	pos = *ppos;
-	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
-		return retval;
+	if (pos >= 0) {
+		if (unlikely((loff_t) (pos + count) < 0))
+			count = 1 + (size_t) LLONG_MAX - pos;
+	} else {
+		if (unlikely((loff_t) (pos + count) > 0))
+			count = - pos;
+	}
 
 	if (unlikely(inode->i_flock && mandatory_lock(inode))) {
 		retval = locks_mandatory_area(
#11 lore raw

RFC PATCH 1/1 more checks for negative f_pos handling (Was Re: Question: how to handle too big f_pos

In reply to KAMEZAWA Hiroyuki

The problem:

Quoted 5 lines

I'm writing a patch against /dev/kmem...I found a problem.

/dev/kmem (and /proc/<pid>/mem) puts virtual addres to f->f_pos.

but f->f_pos is always negative and rw_verify_ara() returns -EINVAL always.

Changed CC: List.

This is a trial to consider how to fix negative f_pos problem shown in above.

Hmm, even after this patch, x86's vsyscall area is not readable. ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] But maybe no problems. (now, it cannot be read, anyway.)

I tested /dev/kmem on x86-64 and this works fine. I added a fix for /proc/<pid>/mem because I know ia64's hugetlbe area is not readable via /proc/<pid>/mem. (But I'm not sure other 64bit arch has this kind of problems in /proc/<pid>/mem)

==
From: KAMEZAWA Hiruyoki <[email protected]>

Modifying rw_verify_area()'s negative f_pos check.

Now, rw_verify_area() has this check
if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
return -EINVAL

And access to special files as /dev/mem,kmem, /proc/<pid>/mem returns unexpected -EINVAL.
(For example, ia64 maps hugetlb at 0x8000000000000000- region)

This patch tries to make range check more precise by using llseek ops defined per special files.

---
 fs/proc/base.c  |   22 +++++++++++++++++-----
 fs/read_write.c |   39 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 54 insertions(+), 7 deletions(-)
fs/read_write.c +37 -2
Index: mmotm-2.6.31-Sep14/fs/read_write.c
===================================================================
@@ -205,6 +205,37 @@ bad:
 }
 #endif
 
+static int
+__verify_negative_pos_range(struct file *file, loff_t pos, size_t count)
+{
+	unsigned long long upos, end;
+	loff_t ret;
+
+	/* disallow overflow */
+	upos = (unsigned long long)pos;
+	end = upos + count;
+	if (end < pos)
+		return -EOVERFLOW;
+	/*
+	 * Sanity check...subsystem has to provide llseek for handle big pos.
+	 * Subsystem's llseek should verify f_pos's value comaparing with its
+	 * max file size.
+	 * Note1: generic file ops' llseek cannot handle negative pos.
+	 * Note2: should we take care of pos == -EINVAL ?
+	 * Note3: we check flags and ops here for avoiding taking locks in.
+	 * default_lseek.
+	 */
+	ret = -EINVAL;
+	if ((file->f_mode & FMODE_LSEEK) &&
+	    (file->f_op && file->f_op->llseek)) {
+		ret = vfs_llseek(file, 0, SEEK_CUR);
+		if (ret == pos)
+			return 0;
+	}
+
+	return (int)ret;
+}
+
 /*
  * rw_verify_area doesn't like huge counts. We limit
  * them to something that fits in "int" so that others
@@ -222,8 +253,12 @@ int rw_verify_area(int read_write, struc
 	if (unlikely((ssize_t) count < 0))
 		return retval;
 	pos = *ppos;
-	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
-		return retval;
+	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) {
+		/* some files requires special care */
+		retval = __verify_negative_pos_range(file, pos, count);
+		if (retval)
+			return retval;
+	}
 
 	if (unlikely(inode->i_flock && mandatory_lock(inode))) {
 		retval = locks_mandatory_area(
fs/proc/base.c +17 -5
Index: mmotm-2.6.31-Sep14/fs/proc/base.c
===================================================================
@@ -903,18 +903,30 @@ out_no_task:
 
 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
 {
+	struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
+	unsigned long long new_offset = -EINVAL;
+
+	if (!task) /* lseek's spec doesn't allow -ESRCH but... */
+		return -ESRCH;
+
 	switch (orig) {
 	case 0:
-		file->f_pos = offset;
+		new_offset = offset;
 		break;
 	case 1:
-		file->f_pos += offset;
+		new_offset = (unsigned long long)f->f_pos + offset;
 		break;
 	default:
-		return -EINVAL;
+		new_offset = -EINVAL;
+		break;
 	}
-	force_successful_syscall_return();
-	return file->f_pos;
+	if (new_offset < (unsigned long long)TASK_SIZE_OF(task)) {
+		file->f_pos = (loff_t)new_offset;
+		force_successful_syscall_return();
+	} else
+		new_offset = -EINVAL;
+	put_task_struct(task);
+	return (loff_t)new_offset;
 }
 
 static const struct file_operations proc_mem_operations = {
#12 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling (Was Re: Question: how to handle too big f_pos

Quoted 105 lines · KAMEZAWA Hiroyuki

The problem:

Quoted 5 lines

I'm writing a patch against /dev/kmem...I found a problem.

/dev/kmem (and /proc/<pid>/mem) puts virtual addres to f->f_pos.

but f->f_pos is always negative and rw_verify_ara() returns -EINVAL always.

Changed CC: List.

This is a trial to consider how to fix negative f_pos problem shown in above.

Hmm, even after this patch, x86's vsyscall area is not readable. ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0  [vsyscall] But maybe no problems. (now, it cannot be read, anyway.)

I tested /dev/kmem on x86-64 and this works fine. I added a fix for /proc/<pid>/mem because I know ia64's hugetlbe area is not readable via /proc/<pid>/mem. (But I'm not sure other 64bit arch has this kind of problems in /proc/<pid>/mem)

==
From: KAMEZAWA Hiruyoki <[email protected]>

Modifying rw_verify_area()'s negative f_pos check.

Now, rw_verify_area() has this check
  if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) return -EINVAL

And access to special files as /dev/mem,kmem, /proc/<pid>/mem returns unexpected -EINVAL.
(For example, ia64 maps hugetlb at 0x8000000000000000- region)

This patch tries to make range check more precise by using llseek ops defined per special files.

---
 fs/proc/base.c  |   22 +++++++++++++++++----- fs/read_write.c |   39 +++++++++++++++++++++++++++++++++++++--

 2 files changed, 54 insertions(+), 7 deletions(-)

Index: mmotm-2.6.31-Sep14/fs/read_write.c
===================================================================

fs/read_write.c +37 -2
@@ -205,6 +205,37 @@ bad:
  }
  #endif
 
+static int
+__verify_negative_pos_range(struct file *file, loff_t pos, size_t count)
+{
+       unsigned long long upos, end;
+       loff_t ret;
+
+       /* disallow overflow */
+       upos = (unsigned long long)pos;
+       end = upos + count;
+       if (end < pos)
+               return -EOVERFLOW;
+       /*
+        * Sanity check...subsystem has to provide llseek for handle big pos.
+        * Subsystem's llseek should verify f_pos's value comaparing with its
+        * max file size.
+        * Note1: generic file ops' llseek cannot handle negative pos.
+        * Note2: should we take care of pos == -EINVAL ?
+        * Note3: we check flags and ops here for avoiding taking locks in.
+        * default_lseek.
+        */
+       ret = -EINVAL;
+       if ((file->f_mode & FMODE_LSEEK) &&
+           (file->f_op && file->f_op->llseek)) {
+               ret = vfs_llseek(file, 0, SEEK_CUR);
+               if (ret == pos)
+                       return 0;
+       }
+
+       return (int)ret;
+}
+
  /*
  * rw_verify_area doesn't like huge counts. We limit
  * them to something that fits in "int" so that others
@@ -222,8 +253,12 @@ int rw_verify_area(int read_write, struc
        if (unlikely((ssize_t) count < 0))
                return retval;
        pos = *ppos;
-       if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
-               return retval;
+       if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) {
+               /* some files requires special care */
+               retval = __verify_negative_pos_range(file, pos, count);
+               if (retval)
+                       return retval;
+       }
 
        if (unlikely(inode->i_flock && mandatory_lock(inode))) {

               retval = locks_mandatory_area(
Index: mmotm-2.6.31-Sep14/fs/proc/base.c
===================================================================

fs/proc/base.c out_no_task: · line 907

Quoted 5 lines
fs/proc/base.c +2 -0
@@ -903,18 +903,30 @@ out_no_task:
 
  loff_t mem_lseek(struct file *file, loff_t offset, int orig)
  {
+       struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
+       unsigned long long new_offset = -EINVAL;

Why not make 'new_offset' as loff_t? This can make your code easier.

fs/proc/base.c out_no_task: · line 909

Quoted 2 lines
fs/proc/base.c +2 -0
+
+       if (!task) /* lseek's spec doesn't allow -ESRCH but... */

No worry, we have many ESRCH for proc files.

Quoted 11 lines
fs/proc/base.c +2 -0
+               return -ESRCH;
+
        switch (orig) {

       case 0:
-               file->f_pos = offset;
+               new_offset = offset;
               break;
       case 1:
-               file->f_pos += offset;
+               new_offset = (unsigned long long)f->f_pos + offset; break;

Quoted 8 lines

       default:
-               return -EINVAL;
+               new_offset = -EINVAL;
+               break;
       }
-       force_successful_syscall_return();
-       return file->f_pos;
+       if (new_offset < (unsigned long long)TASK_SIZE_OF(task)) {

Hmm, why this check?

Quoted 9 lines

+               file->f_pos = (loff_t)new_offset;
+               force_successful_syscall_return();
+       } else
+               new_offset = -EINVAL;
+       put_task_struct(task);
+       return (loff_t)new_offset;
 }

 static const struct file_operations proc_mem_operations = {

Thanks.

#13 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling (Was Re: Question: how to handle too big f_pos

Ah, sorry. I should CC: you.

Quoted 108 lines · On Wed, 16 Sep 2009 16:20:32 +0800 Américo Wang <[email protected]>…
Quoted 106 lines · KAMEZAWA Hiroyuki

The problem:

Quoted 5 lines

I'm writing a patch against /dev/kmem...I found a problem.

/dev/kmem (and /proc/<pid>/mem) puts virtual addres to f->f_pos.

but f->f_pos is always negative and rw_verify_ara() returns -EINVAL always.

Changed CC: List.

This is a trial to consider how to fix negative f_pos problem shown in above.

Hmm, even after this patch, x86's vsyscall area is not readable. ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0  [vsyscall] But maybe no problems. (now, it cannot be read, anyway.)

I tested /dev/kmem on x86-64 and this works fine. I added a fix for /proc/<pid>/mem because I know ia64's hugetlbe area is not readable via /proc/<pid>/mem. (But I'm not sure other 64bit arch has this kind of problems in /proc/<pid>/mem)

==
From: KAMEZAWA Hiruyoki <[email protected]>

Modifying rw_verify_area()'s negative f_pos check.

Now, rw_verify_area() has this check
  if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) return -EINVAL

And access to special files as /dev/mem,kmem, /proc/<pid>/mem returns unexpected -EINVAL.
(For example, ia64 maps hugetlb at 0x8000000000000000- region)

This patch tries to make range check more precise by using llseek ops defined per special files.

---
 fs/proc/base.c  |   22 +++++++++++++++++----- fs/read_write.c |   39 +++++++++++++++++++++++++++++++++++++--

 2 files changed, 54 insertions(+), 7 deletions(-)

Index: mmotm-2.6.31-Sep14/fs/read_write.c
===================================================================

fs/read_write.c +37 -2
@@ -205,6 +205,37 @@ bad:
  }
  #endif
 
+static int
+__verify_negative_pos_range(struct file *file, loff_t pos, size_t count)
+{
+       unsigned long long upos, end;
+       loff_t ret;
+
+       /* disallow overflow */
+       upos = (unsigned long long)pos;
+       end = upos + count;
+       if (end < pos)
+               return -EOVERFLOW;
+       /*
+        * Sanity check...subsystem has to provide llseek for handle big pos.
+        * Subsystem's llseek should verify f_pos's value comaparing with its
+        * max file size.
+        * Note1: generic file ops' llseek cannot handle negative pos.
+        * Note2: should we take care of pos == -EINVAL ?
+        * Note3: we check flags and ops here for avoiding taking locks in.
+        * default_lseek.
+        */
+       ret = -EINVAL;
+       if ((file->f_mode & FMODE_LSEEK) &&
+           (file->f_op && file->f_op->llseek)) {
+               ret = vfs_llseek(file, 0, SEEK_CUR);
+               if (ret == pos)
+                       return 0;
+       }
+
+       return (int)ret;
+}
+
  /*
  * rw_verify_area doesn't like huge counts. We limit
  * them to something that fits in "int" so that others
@@ -222,8 +253,12 @@ int rw_verify_area(int read_write, struc
        if (unlikely((ssize_t) count < 0))
                return retval;
        pos = *ppos;
-       if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
-               return retval;
+       if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) {
+               /* some files requires special care */
+               retval = __verify_negative_pos_range(file, pos, count);
+               if (retval)
+                       return retval;
+       }
 
        if (unlikely(inode->i_flock && mandatory_lock(inode))) {

               retval = locks_mandatory_area(
Index: mmotm-2.6.31-Sep14/fs/proc/base.c
===================================================================

fs/proc/base.c +0 -0
@@ -903,18 +903,30 @@ out_no_task:
 
Quoted 8 lines

fs/proc/base.c out_no_task: · line 907

Quoted 4 lines
fs/proc/base.c +2 -0
  loff_t mem_lseek(struct file *file, loff_t offset, int orig)
  {
+       struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
+       unsigned long long new_offset = -EINVAL;

Why not make 'new_offset' as loff_t? This can make your code easier.

loff_t is "long long", I wanted "unsigned long long" for showing f_pos here is treated as "unsigned".

Quoted 6 lines

fs/proc/base.c out_no_task: · line 909

Quoted 2 lines
fs/proc/base.c +2 -0
+
+       if (!task) /* lseek's spec doesn't allow -ESRCH but... */

No worry, we have many ESRCH for proc files.

I know ;)

Quoted 21 lines
Quoted 11 lines
fs/proc/base.c +2 -0
+               return -ESRCH;
+
        switch (orig) {

       case 0:
-               file->f_pos = offset;
+               new_offset = offset;
               break;
       case 1:
-               file->f_pos += offset;
+               new_offset = (unsigned long long)f->f_pos + offset; break;

Quoted 8 lines

       default:
-               return -EINVAL;
+               new_offset = -EINVAL;
+               break;
       }
-       force_successful_syscall_return();
-       return file->f_pos;
+       if (new_offset < (unsigned long long)TASK_SIZE_OF(task)) {

Quoted 2 lines

Hmm, why this check?

2 reasons.

1. If this lseek has to check something, this is it.
2. On architecture where 32bit program can ran on 64bit, moving f_pos above 4G is out-of-range, for example.

But mem_read() will catch any bad f_pos, anyway. So, just making allow all f_pos here is maybe a choice. Considering lseek, providing this range check here is not so bad.

Thanks.
-Kame

Quoted 12 lines
Quoted 9 lines

+               file->f_pos = (loff_t)new_offset;
+               force_successful_syscall_return();
+       } else
+               new_offset = -EINVAL;
+       put_task_struct(task);
+       return (loff_t)new_offset;
 }

 static const struct file_operations proc_mem_operations = {

Thanks.

Reviewed-by
#14 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling (Was Re: Question: how to handle too big f_pos

Quoted 1 line · KAMEZAWA Hiroyuki

Ah, sorry. I should CC: you.

No problem. :)

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
fs/proc/base.c +0 -0
@@ -903,18 +903,30 @@ out_no_task:
 
Quoted 8 lines

fs/proc/base.c out_no_task: · line 907

Quoted 4 lines
fs/proc/base.c +2 -0
  loff_t mem_lseek(struct file *file, loff_t offset, int orig)
  {
+       struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
+       unsigned long long new_offset = -EINVAL;

Why not make 'new_offset' as loff_t? This can make your code easier.

Quoted 3 lines

loff_t is "long long", I wanted "unsigned long long" for showing f_pos here is treated as "unsigned".

Yeah, the same as for __verify_negative_pos_range(), right...

<snip>

Quoted 10 lines
Quoted 8 lines

fs/proc/base.c out_no_task: · line 923

Quoted 4 lines
fs/proc/base.c +1 -2
        }
-       force_successful_syscall_return();
-       return file->f_pos;
+       if (new_offset < (unsigned long long)TASK_SIZE_OF(task)) {

Hmm, why this check?

2 reasons.

Quoted 7 lines

 1. If this lseek has to check something, this is it.
 2. On architecture where 32bit program can ran on 64bit, moving f_pos above 4G is out-of-range, for example.

But mem_read() will catch any bad f_pos, anyway. So, just making allow all f_pos here is maybe a choice. Considering lseek, providing this range check here is not so bad.

Ok, I misunderstood the macro 'TASK_SIZE_OF', then no problem.

Thanks.

#15 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling (Was Re: Question: how to handle too big f_pos

Quoted 44 lines · Am�$(D+1rico_Wang wrote:
Quoted 1 line · KAMEZAWA Hiroyuki

Ah, sorry. I should CC: you.

No problem. :)

Quoted 8 lines
Quoted 8 lines

fs/proc/base.c out_no_task: · line 906

Quoted 7 lines
fs/proc/base.c +1 -0
@@ -903,18 +903,30 @@ out_no_task:
 
 ?loff_t mem_lseek(struct file *file, loff_t offset, int orig)
 ?{
+ ? ? ? struct task_struct *task =

get_proc_task(file->f_path.dentry->d_inode);

Quoted 8 lines
Quoted 5 lines

fs/proc/base.c out_no_task: · line 907

Quoted 1 line
fs/proc/base.c +1 -0
+ ? ? ? unsigned long long new_offset = -EINVAL;

Why not make 'new_offset' as loff_t? This can make your code easier.

loff_t is "long long", I wanted "unsigned long long" for showing f_pos here is treated as "unsigned".

Yeah, the same as for __verify_negative_pos_range(), right...

<snip>

Quoted 14 lines
Quoted 8 lines

fs/proc/base.c out_no_task: · line 909

Quoted 4 lines
fs/proc/base.c +1 -2
 ? ? ? ?}
- ? ? ? force_successful_syscall_return();
- ? ? ? return file->f_pos;
+ ? ? ? if (new_offset < (unsigned long long)TASK_SIZE_OF(task)) {

Hmm, why this check?

2 reasons.

?1. If this lseek has to check something, this is it. ?2. On architecture where 32bit program can ran on 64bit, ? ? moving f_pos above 4G is out-of-range, for example.

Quoted 8 lines
Quoted 3 lines

But mem_read() will catch any bad f_pos, anyway. So, just making allow all f_pos here is maybe a choice. Considering lseek, providing this range check here is not so bad.

Ok, I misunderstood the macro 'TASK_SIZE_OF', then no problem.

Ah, very sorry. I noticed I didn't handle pread/pwrite, splice, etc... I'll do retry.

Sorry,
-Kame

Quoted 7 lines

Thanks.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#16 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling (Was Re: Question: how to handle too big f_pos

2009/9/16 KAMEZAWA Hiroyuki <[email protected]>:

Quoted 49 lines
Quoted 48 lines · Am�+1rico_Wang wrote:
Quoted 1 line · KAMEZAWA Hiroyuki

Ah, sorry. I should CC: you.

No problem. :)

Quoted 8 lines
Quoted 8 lines

fs/proc/base.c out_no_task: · line 906

Quoted 7 lines
fs/proc/base.c +1 -0
@@ -903,18 +903,30 @@ out_no_task:
 
 ?loff_t mem_lseek(struct file *file, loff_t offset, int orig)
 ?{
+ ? ? ? struct task_struct *task =

get_proc_task(file->f_path.dentry->d_inode);

Quoted 8 lines
Quoted 5 lines

fs/proc/base.c out_no_task: · line 907

Quoted 1 line
fs/proc/base.c +1 -0
+ ? ? ? unsigned long long new_offset = -EINVAL;

Why not make 'new_offset' as loff_t? This can make your code easier.

loff_t is "long long", I wanted "unsigned long long" for showing f_pos here is treated as "unsigned".

Yeah, the same as for __verify_negative_pos_range(), right...

<snip>

Quoted 17 lines
Quoted 8 lines

fs/proc/base.c out_no_task: · line 909

Quoted 4 lines
fs/proc/base.c +1 -2
 ? ? ? ?}
- ? ? ? force_successful_syscall_return();
- ? ? ? return file->f_pos;
+ ? ? ? if (new_offset < (unsigned long long)TASK_SIZE_OF(task)) {

Hmm, why this check?

2 reasons.

?1. If this lseek has to check something, this is it. ?2. On architecture where 32bit program can ran on 64bit, ? ? moving f_pos above 4G is out-of-range, for example.

But mem_read() will catch any bad f_pos, anyway. So, just making allow all f_pos here is maybe a choice. Considering lseek, providing this range check here is not so bad.

Quoted 6 lines
Quoted 4 lines

Ok, I misunderstood the macro 'TASK_SIZE_OF', then no problem.

Ah, very sorry. I noticed I didn't handle pread/pwrite, splice, etc... I'll do retry.

Yes? Not necessary...

In man page, it said /proc/<pid>/mem can be accessed via open(), read(), fseek(), no pread(), no splice()...

#17 lore raw

RFC PATCH 1/1 more checks for negative f_pos handling v2

In reply to KAMEZAWA Hiroyuki

Considering several approaches, I think this is the simplest.. Any idea ?

Thanks,
-Kame
==
From: KAMEZAWA Hiroyuki <[email protected]>

Now, rw_verify_area() checsk f_pos is negative or not. And if negative, returns -EINVAL.

But, some special files as /dev/(k)mem and /proc/<pid>/mem etc.. has negative offsets. And we can't do any access via read/write to the file(device).

This patch introduce a flag S_VERYBIG and allow negative file offsets.

---
 drivers/char/mem.c |   23 +++++++++++++----------
 fs/proc/base.c     |    2 ++
 fs/read_write.c    |   20 ++++++++++++++++++--
 include/linux/fs.h |    2 ++
 4 files changed, 35 insertions(+), 12 deletions(-)
fs/read_write.c +18 -2
Index: mmotm-2.6.31-Sep14/fs/read_write.c
===================================================================
@@ -205,6 +205,19 @@ bad:
 }
 #endif
 
+static bool
+__negative_fpos_check(struct inode *inode, loff_t pos, size_t count)
+{
+	struct inode *inode;
+	loff_t isize;
+
+	if (pos + count < pos)
+		return -EOVERFLOW;
+	if (IS_VERYBIG(inode))
+		return 0;
+	return -EINVAL;
+}
+
 /*
  * rw_verify_area doesn't like huge counts. We limit
  * them to something that fits in "int" so that others
@@ -222,8 +235,11 @@ int rw_verify_area(int read_write, struc
 	if (unlikely((ssize_t) count < 0))
 		return retval;
 	pos = *ppos;
-	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
-		return retval;
+	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) {
+		retval = __negative_fpos_check(inode, pos, count))
+		if (retval)
+			return retval;
+	}
 
 	if (unlikely(inode->i_flock && mandatory_lock(inode))) {
 		retval = locks_mandatory_area(
include/linux/fs.h +2 -0
Index: mmotm-2.6.31-Sep14/include/linux/fs.h
===================================================================
@@ -231,6 +231,7 @@ struct inodes_stat_t {
 #define S_NOCMTIME	128	/* Do not update file c/mtime */
 #define S_SWAPFILE	256	/* Do not truncate: swapon got its bmaps */
 #define S_PRIVATE	512	/* Inode is fs-internal */
+#define S_VERYBIG	1024	/* Allow file's loff_t can be negative */
 
 /*
  * Note that nosuid etc flags are inode-specific: setting some file-system
@@ -265,6 +266,7 @@ struct inodes_stat_t {
 #define IS_NOCMTIME(inode)	((inode)->i_flags & S_NOCMTIME)
 #define IS_SWAPFILE(inode)	((inode)->i_flags & S_SWAPFILE)
 #define IS_PRIVATE(inode)	((inode)->i_flags & S_PRIVATE)
+#define IS_VERYBIG(inode)	((inode)->i_flags & S_VERYBIG)
 
 /* the read-only stuff doesn't really belong here, but any other place is
    probably as bad and I don't want to create yet another include file. */
drivers/char/mem.c +13 -10
Index: mmotm-2.6.31-Sep14/drivers/char/mem.c
===================================================================
@@ -825,22 +825,23 @@ static const struct memdev {
 	const char *name;
 	const struct file_operations *fops;
 	struct backing_dev_info *dev_info;
+	bool	verybig;
 } devlist[] = {
-	[ 1] = { "mem", &mem_fops, &directly_mappable_cdev_bdi },
+	[1] = { "mem", &mem_fops, &directly_mappable_cdev_bdi, true },
 #ifdef CONFIG_DEVKMEM
-	[ 2] = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi },
+	[2] = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi, true },
 #endif
-	[ 3] = {"null", &null_fops, NULL },
+	[3] = {"null", &null_fops, NULL, false },
 #ifdef CONFIG_DEVPORT
-	[ 4] = { "port", &port_fops, NULL },
+	[4] = { "port", &port_fops, NULL, false },
 #endif
-	[ 5] = { "zero", &zero_fops, &zero_bdi },
-	[ 6] = { "full", &full_fops, NULL },
-	[ 7] = { "random", &random_fops, NULL },
-	[ 9] = { "urandom", &urandom_fops, NULL },
-	[11] = { "kmsg", &kmsg_fops, NULL },
+	[5] = { "zero", &zero_fops, &zero_bdi, false },
+	[6] = { "full", &full_fops, NULL, false },
+	[7] = { "random", &random_fops, NULL, false },
+	[9] = { "urandom", &urandom_fops, NULL, false },
+	[11] = { "kmsg", &kmsg_fops, NULL, false },
 #ifdef CONFIG_CRASH_DUMP
-	[12] = { "oldmem", &oldmem_fops, NULL },
+	[12] = { "oldmem", &oldmem_fops, NULL, false },
 #endif
 };
 
@@ -868,6 +869,8 @@ static int memory_open(struct inode *ino
 		ret = dev->fops->open(inode, filp);
 	else
 		ret = 0;
+	if (dev->verybig)
+		inode->i_flags |= S_VERYBIG;
 out:
 	unlock_kernel();
 	return ret;
fs/proc/base.c +2 -0
Index: mmotm-2.6.31-Sep14/fs/proc/base.c
===================================================================
@@ -779,6 +779,8 @@ static const struct file_operations proc
 static int mem_open(struct inode* inode, struct file* file)
 {
 	file->private_data = (void*)((long)current->self_exec_id);
+	/* this file is read only and we can catch out-pf-range */
+	inode->i_flags |= S_VERYBIG;
 	return 0;
 }
 
#18 lore raw

RFC PATCH 1/1 more checks for negative f_pos handling v3

Very sorry...reflesh miss.
==
From: KAMEZAWA Hiroyuki <[email protected]>

Now, rw_verify_area() checsk f_pos is negative or not. And if negative, returns -EINVAL.

But, some special files as /dev/(k)mem and /proc/<pid>/mem etc.. has negative offsets. And we can't do any access via read/write to the file(device).

This patch introduce a flag S_VERYBIG and allow negative file offsets.

Changelog:
- fixed bug in rw_verify_area (it cannot be compiled)

---
 drivers/char/mem.c |   23 +++++++++++++----------
 fs/proc/base.c     |    2 ++
 fs/read_write.c    |   20 ++++++++++++++++++--
 include/linux/fs.h |    2 ++
 4 files changed, 35 insertions(+), 12 deletions(-)
fs/read_write.c +15 -2
Index: mmotm-2.6.31-Sep14/fs/read_write.c
===================================================================
@@ -205,6 +205,16 @@ bad:
 }
 #endif
 
+static bool
+__negative_fpos_check(struct inode *inode, loff_t pos, size_t count)
+{
+	if (pos + count < pos)
+		return -EOVERFLOW;
+	if (IS_VERYBIG(inode))
+		return 0;
+	return -EINVAL;
+}
+
 /*
  * rw_verify_area doesn't like huge counts. We limit
  * them to something that fits in "int" so that others
@@ -222,8 +232,11 @@ int rw_verify_area(int read_write, struc
 	if (unlikely((ssize_t) count < 0))
 		return retval;
 	pos = *ppos;
-	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
-		return retval;
+	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) {
+		retval = __negative_fpos_check(inode, pos, count);
+		if (retval)
+			return retval;
+	}
 
 	if (unlikely(inode->i_flock && mandatory_lock(inode))) {
 		retval = locks_mandatory_area(
include/linux/fs.h +2 -0
Index: mmotm-2.6.31-Sep14/include/linux/fs.h
===================================================================
@@ -231,6 +231,7 @@ struct inodes_stat_t {
 #define S_NOCMTIME	128	/* Do not update file c/mtime */
 #define S_SWAPFILE	256	/* Do not truncate: swapon got its bmaps */
 #define S_PRIVATE	512	/* Inode is fs-internal */
+#define S_VERYBIG	1024	/* Allow file's loff_t can be negative */
 
 /*
  * Note that nosuid etc flags are inode-specific: setting some file-system
@@ -265,6 +266,7 @@ struct inodes_stat_t {
 #define IS_NOCMTIME(inode)	((inode)->i_flags & S_NOCMTIME)
 #define IS_SWAPFILE(inode)	((inode)->i_flags & S_SWAPFILE)
 #define IS_PRIVATE(inode)	((inode)->i_flags & S_PRIVATE)
+#define IS_VERYBIG(inode)	((inode)->i_flags & S_VERYBIG)
 
 /* the read-only stuff doesn't really belong here, but any other place is
    probably as bad and I don't want to create yet another include file. */
drivers/char/mem.c +13 -10
Index: mmotm-2.6.31-Sep14/drivers/char/mem.c
===================================================================
@@ -825,22 +825,23 @@ static const struct memdev {
 	const char *name;
 	const struct file_operations *fops;
 	struct backing_dev_info *dev_info;
+	bool	verybig;
 } devlist[] = {
-	[ 1] = { "mem", &mem_fops, &directly_mappable_cdev_bdi },
+	[1] = { "mem", &mem_fops, &directly_mappable_cdev_bdi, true },
 #ifdef CONFIG_DEVKMEM
-	[ 2] = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi },
+	[2] = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi, true },
 #endif
-	[ 3] = {"null", &null_fops, NULL },
+	[3] = {"null", &null_fops, NULL, false },
 #ifdef CONFIG_DEVPORT
-	[ 4] = { "port", &port_fops, NULL },
+	[4] = { "port", &port_fops, NULL, false },
 #endif
-	[ 5] = { "zero", &zero_fops, &zero_bdi },
-	[ 6] = { "full", &full_fops, NULL },
-	[ 7] = { "random", &random_fops, NULL },
-	[ 9] = { "urandom", &urandom_fops, NULL },
-	[11] = { "kmsg", &kmsg_fops, NULL },
+	[5] = { "zero", &zero_fops, &zero_bdi, false },
+	[6] = { "full", &full_fops, NULL, false },
+	[7] = { "random", &random_fops, NULL, false },
+	[9] = { "urandom", &urandom_fops, NULL, false },
+	[11] = { "kmsg", &kmsg_fops, NULL, false },
 #ifdef CONFIG_CRASH_DUMP
-	[12] = { "oldmem", &oldmem_fops, NULL },
+	[12] = { "oldmem", &oldmem_fops, NULL, false },
 #endif
 };
 
@@ -868,6 +869,8 @@ static int memory_open(struct inode *ino
 		ret = dev->fops->open(inode, filp);
 	else
 		ret = 0;
+	if (dev->verybig)
+		inode->i_flags |= S_VERYBIG;
 out:
 	unlock_kernel();
 	return ret;
fs/proc/base.c +2 -0
Index: mmotm-2.6.31-Sep14/fs/proc/base.c
===================================================================
@@ -779,6 +779,8 @@ static const struct file_operations proc
 static int mem_open(struct inode* inode, struct file* file)
 {
 	file->private_data = (void*)((long)current->self_exec_id);
+	/* this file is read only and we can catch out-pf-range */
+	inode->i_flags |= S_VERYBIG;
 	return 0;
 }
 
#19 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling v3

Quoted 31 lines · KAMEZAWA Hiroyuki

Very sorry...reflesh miss.
==
From: KAMEZAWA Hiroyuki <[email protected]>

Now, rw_verify_area() checsk f_pos is negative or not. And if negative, returns -EINVAL.

But, some special files as /dev/(k)mem and /proc/<pid>/mem etc.. has negative offsets. And we can't do any access via read/write to the file(device).

This patch introduce a flag S_VERYBIG and allow negative file offsets.

Changelog:
- fixed bug in rw_verify_area (it cannot be compiled)

---
 drivers/char/mem.c |   23 +++++++++++++----------
 fs/proc/base.c     |    2 ++
 fs/read_write.c    |   20 ++++++++++++++++++--
 include/linux/fs.h |    2 ++
 4 files changed, 35 insertions(+), 12 deletions(-)

Index: mmotm-2.6.31-Sep14/fs/read_write.c
===================================================================

fs/read_write.c +0 -0
@@ -205,6 +205,16 @@ bad:
 }

fs/read_write.c bad: · line 215

Quoted 10 lines
fs/read_write.c +8 -0
 #endif
 
+static bool
+__negative_fpos_check(struct inode *inode, loff_t pos, size_t count)
+{
+	if (pos + count < pos)
+		return -EOVERFLOW;
+	if (IS_VERYBIG(inode))
+		return 0;
+	return -EINVAL;

This always return negative values for !IS_VERYBIG inodes, looks weird at least.

Quoted 65 lines
fs/read_write.c +7 -2
+}
+
 /*
  * rw_verify_area doesn't like huge counts. We limit
  * them to something that fits in "int" so that others
@@ -222,8 +232,11 @@ int rw_verify_area(int read_write, struc
 	if (unlikely((ssize_t) count < 0))
 		return retval;
 	pos = *ppos;
-	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
-		return retval;
+	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) {
+		retval = __negative_fpos_check(inode, pos, count);
+		if (retval)
+			return retval;
+	}
 
 	if (unlikely(inode->i_flock && mandatory_lock(inode))) {
 		retval = locks_mandatory_area(

Index: mmotm-2.6.31-Sep14/include/linux/fs.h
===================================================================

include/linux/fs.h +2 -0
@@ -231,6 +231,7 @@ struct inodes_stat_t {
 #define S_NOCMTIME	128	/* Do not update file c/mtime */
 #define S_SWAPFILE	256	/* Do not truncate: swapon got its bmaps */
 #define S_PRIVATE	512	/* Inode is fs-internal */
+#define S_VERYBIG	1024	/* Allow file's loff_t can be negative */
 
 /*
  * Note that nosuid etc flags are inode-specific: setting some file-system
@@ -265,6 +266,7 @@ struct inodes_stat_t {
 #define IS_NOCMTIME(inode)	((inode)->i_flags & S_NOCMTIME)
 #define IS_SWAPFILE(inode)	((inode)->i_flags & S_SWAPFILE)
 #define IS_PRIVATE(inode)	((inode)->i_flags & S_PRIVATE)
+#define IS_VERYBIG(inode)	((inode)->i_flags & S_VERYBIG)
 
 /* the read-only stuff doesn't really belong here, but any other place is
    probably as bad and I don't want to create yet another include file. */

Index: mmotm-2.6.31-Sep14/drivers/char/mem.c
===================================================================

drivers/char/mem.c +5 -8
@@ -825,22 +825,23 @@ static const struct memdev {
 	const char *name;
 	const struct file_operations *fops;
 	struct backing_dev_info *dev_info;
+	bool	verybig;
 } devlist[] = {
-	[ 1] = { "mem", &mem_fops, &directly_mappable_cdev_bdi },
+	[1] = { "mem", &mem_fops, &directly_mappable_cdev_bdi, true },
 #ifdef CONFIG_DEVKMEM
-	[ 2] = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi },
+	[2] = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi, true },
 #endif
-	[ 3] = {"null", &null_fops, NULL },
+	[3] = {"null", &null_fops, NULL, false },
 #ifdef CONFIG_DEVPORT
-	[ 4] = { "port", &port_fops, NULL },
+	[4] = { "port", &port_fops, NULL, false },
 #endif
-	[ 5] = { "zero", &zero_fops, &zero_bdi },
-	[ 6] = { "full", &full_fops, NULL },
-	[ 7] = { "random", &random_fops, NULL },
-	[ 9] = { "urandom", &urandom_fops, NULL },

drivers/char/mem.c static const struct memdev { · line 845

Quoted 10 lines
drivers/char/mem.c +6 -2
-	[11] = { "kmsg", &kmsg_fops, NULL },
+	[5] = { "zero", &zero_fops, &zero_bdi, false },
+	[6] = { "full", &full_fops, NULL, false },
+	[7] = { "random", &random_fops, NULL, false },
+	[9] = { "urandom", &urandom_fops, NULL, false },
+	[11] = { "kmsg", &kmsg_fops, NULL, false },
 #ifdef CONFIG_CRASH_DUMP
-	[12] = { "oldmem", &oldmem_fops, NULL },
+	[12] = { "oldmem", &oldmem_fops, NULL, false },
 #endif

How about make the fields aligned? That would look much nicer :)

Thanks,
Fengguang

Quoted 24 lines
drivers/char/mem.c +2 -0
 };
 
@@ -868,6 +869,8 @@ static int memory_open(struct inode *ino
 		ret = dev->fops->open(inode, filp);
 	else
 		ret = 0;
+	if (dev->verybig)
+		inode->i_flags |= S_VERYBIG;
 out:
 	unlock_kernel();
 	return ret;

Index: mmotm-2.6.31-Sep14/fs/proc/base.c
===================================================================

fs/proc/base.c +2 -0
@@ -779,6 +779,8 @@ static const struct file_operations proc
 static int mem_open(struct inode* inode, struct file* file)
 {
 	file->private_data = (void*)((long)current->self_exec_id);
+	/* this file is read only and we can catch out-pf-range */
+	inode->i_flags |= S_VERYBIG;
 	return 0;
 }
 
#20 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling v3

Quoted 34 lines · On Thu, 17 Sep 2009 14:21:24 +0800 Wu Fengguang <[email protected]> wrote:
Quoted 33 lines · KAMEZAWA Hiroyuki

Very sorry...reflesh miss.
==
From: KAMEZAWA Hiroyuki <[email protected]>

Now, rw_verify_area() checsk f_pos is negative or not. And if negative, returns -EINVAL.

But, some special files as /dev/(k)mem and /proc/<pid>/mem etc.. has negative offsets. And we can't do any access via read/write to the file(device).

This patch introduce a flag S_VERYBIG and allow negative file offsets.

Changelog:
- fixed bug in rw_verify_area (it cannot be compiled)

---
 drivers/char/mem.c |   23 +++++++++++++----------
 fs/proc/base.c     |    2 ++
 fs/read_write.c    |   20 ++++++++++++++++++--
 include/linux/fs.h |    2 ++
 4 files changed, 35 insertions(+), 12 deletions(-)

Index: mmotm-2.6.31-Sep14/fs/read_write.c
===================================================================

fs/read_write.c +0 -0
@@ -205,6 +205,16 @@ bad:
 }
 #endif
 
Quoted 12 lines

fs/read_write.c bad: · line 215

Quoted 8 lines
fs/read_write.c +8 -0
+static bool
+__negative_fpos_check(struct inode *inode, loff_t pos, size_t count)
+{
+	if (pos + count < pos)
+		return -EOVERFLOW;
+	if (IS_VERYBIG(inode))
+		return 0;
+	return -EINVAL;

This always return negative values for !IS_VERYBIG inodes, looks weird at least.

I need to refresh my eyes, too...
Thanks, will fix.. return value is not bool, but int...

Quoted 69 lines
Quoted 69 lines
fs/read_write.c +7 -2
+}
+
 /*
  * rw_verify_area doesn't like huge counts. We limit
  * them to something that fits in "int" so that others
@@ -222,8 +232,11 @@ int rw_verify_area(int read_write, struc
 	if (unlikely((ssize_t) count < 0))
 		return retval;
 	pos = *ppos;
-	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
-		return retval;
+	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) {
+		retval = __negative_fpos_check(inode, pos, count);
+		if (retval)
+			return retval;
+	}
 
 	if (unlikely(inode->i_flock && mandatory_lock(inode))) {
 		retval = locks_mandatory_area(

Index: mmotm-2.6.31-Sep14/include/linux/fs.h
===================================================================

include/linux/fs.h +2 -0
@@ -231,6 +231,7 @@ struct inodes_stat_t {
 #define S_NOCMTIME	128	/* Do not update file c/mtime */
 #define S_SWAPFILE	256	/* Do not truncate: swapon got its bmaps */
 #define S_PRIVATE	512	/* Inode is fs-internal */
+#define S_VERYBIG	1024	/* Allow file's loff_t can be negative */
 
 /*
  * Note that nosuid etc flags are inode-specific: setting some file-system
@@ -265,6 +266,7 @@ struct inodes_stat_t {
 #define IS_NOCMTIME(inode)	((inode)->i_flags & S_NOCMTIME)
 #define IS_SWAPFILE(inode)	((inode)->i_flags & S_SWAPFILE)
 #define IS_PRIVATE(inode)	((inode)->i_flags & S_PRIVATE)
+#define IS_VERYBIG(inode)	((inode)->i_flags & S_VERYBIG)
 
 /* the read-only stuff doesn't really belong here, but any other place is
    probably as bad and I don't want to create yet another include file. */

Index: mmotm-2.6.31-Sep14/drivers/char/mem.c
===================================================================

drivers/char/mem.c +8 -9
@@ -825,22 +825,23 @@ static const struct memdev {
 	const char *name;
 	const struct file_operations *fops;
 	struct backing_dev_info *dev_info;
+	bool	verybig;
 } devlist[] = {
-	[ 1] = { "mem", &mem_fops, &directly_mappable_cdev_bdi },
+	[1] = { "mem", &mem_fops, &directly_mappable_cdev_bdi, true },
 #ifdef CONFIG_DEVKMEM
-	[ 2] = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi },
+	[2] = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi, true },
 #endif
-	[ 3] = {"null", &null_fops, NULL },
+	[3] = {"null", &null_fops, NULL, false },
 #ifdef CONFIG_DEVPORT
-	[ 4] = { "port", &port_fops, NULL },
+	[4] = { "port", &port_fops, NULL, false },
 #endif
-	[ 5] = { "zero", &zero_fops, &zero_bdi },
-	[ 6] = { "full", &full_fops, NULL },
-	[ 7] = { "random", &random_fops, NULL },
-	[ 9] = { "urandom", &urandom_fops, NULL },
-	[11] = { "kmsg", &kmsg_fops, NULL },
+	[5] = { "zero", &zero_fops, &zero_bdi, false },
+	[6] = { "full", &full_fops, NULL, false },
+	[7] = { "random", &random_fops, NULL, false },
Quoted 9 lines

drivers/char/mem.c static const struct memdev { · line 845

Quoted 6 lines
drivers/char/mem.c +3 -1
+	[9] = { "urandom", &urandom_fops, NULL, false },
+	[11] = { "kmsg", &kmsg_fops, NULL, false },
 #ifdef CONFIG_CRASH_DUMP
-	[12] = { "oldmem", &oldmem_fops, NULL },
+	[12] = { "oldmem", &oldmem_fops, NULL, false },
 #endif

How about make the fields aligned? That would look much nicer :)

checkpatch.pl complains agaisnt [ 5]...but ok, ajust "=" placement.

Thanks,
-kame

Quoted 28 lines

Thanks,
Fengguang

Quoted 24 lines
drivers/char/mem.c +2 -0
 };
 
@@ -868,6 +869,8 @@ static int memory_open(struct inode *ino
 		ret = dev->fops->open(inode, filp);
 	else
 		ret = 0;
+	if (dev->verybig)
+		inode->i_flags |= S_VERYBIG;
 out:
 	unlock_kernel();
 	return ret;

Index: mmotm-2.6.31-Sep14/fs/proc/base.c
===================================================================

fs/proc/base.c +2 -0
@@ -779,6 +779,8 @@ static const struct file_operations proc
 static int mem_open(struct inode* inode, struct file* file)
 {
 	file->private_data = (void*)((long)current->self_exec_id);
+	/* this file is read only and we can catch out-pf-range */
+	inode->i_flags |= S_VERYBIG;
 	return 0;
 }
 
#21 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling v3

Quoted 3 lines · KAMEZAWA Hiroyuki
Quoted 2 lines

How about make the fields aligned? That would look much nicer :)

checkpatch.pl complains agaisnt [ 5]...but ok, ajust "=" placement.

How about this? checkpatch won't complain on it :)

} devlist[] = {
	[1] = {     "mem",     &mem_fops, &directly_mappable_cdev_bdi,  true },
#ifdef CONFIG_DEVKMEM
	[2] = {    "kmem",    &kmem_fops, &directly_mappable_cdev_bdi,  true },

#endif
[3] = { "null", &null_fops, NULL, false },
#ifdef CONFIG_DEVPORT
[4] = { "port", &port_fops, NULL, false },
#endif

	[5] = {    "zero",    &zero_fops,                   &zero_bdi, false },
	[6] = {    "full",    &full_fops,                        NULL, false },
	[7] = {  "random",  &random_fops,                        NULL, false },
	[9] = { "urandom", &urandom_fops,                        NULL, false },
	[11] = {   "kmsg",    &kmsg_fops,                        NULL, false },

#ifdef CONFIG_CRASH_DUMP
[12] = { "oldmem", &oldmem_fops, NULL, false },
#endif
};

Thanks,
Fengguang

#22 lore raw

RFC PATCH 1/1 more checks for negative f_pos handling v4

In reply to Wu Fengguang

From: KAMEZAWA Hiroyuki <[email protected]>

Now, rw_verify_area() checsk f_pos is negative or not. And if negative, returns -EINVAL.

But, some special files as /dev/(k)mem and /proc/<pid>/mem etc.. has negative offsets. And we can't do any access via read/write to the file(device).

This patch introduce a flag S_VERYBIG and allow negative file offsets for big files. (usual files don't allow it.)

Changelog: v3->v4
- make changes in mem.c aligned.
- change __negative_fpos_check() to return int.
- fixed bug in "pos" check.
- added comments.

Changelog: v2->v3
- fixed bug in rw_verify_area (it cannot be compiled)

---
 drivers/char/mem.c |   23 +++++++++++++----------
 fs/proc/base.c     |    2 ++
 fs/read_write.c    |   22 ++++++++++++++++++++--
 include/linux/fs.h |    2 ++
 4 files changed, 37 insertions(+), 12 deletions(-)
fs/read_write.c +20 -2
Index: mmotm-2.6.31-Sep14/fs/read_write.c
===================================================================
@@ -205,6 +205,21 @@ bad:
 }
 #endif
 
+static int
+__negative_fpos_check(struct inode *inode, loff_t pos, size_t count)
+{
+	/*
+	 * pos or pos+count is negative here, check overflow.
+	 * too big "count" will be caught in rw_verify_area().
+	 */
+	if ((pos < 0) && (pos + count < pos))
+		return -EOVERFLOW;
+	/* If !VERYBIG inode, negative pos(pos+count) is not allowed */
+	if (!IS_VERYBIG(inode))
+		return -EINVAL;
+	return 0;
+}
+
 /*
  * rw_verify_area doesn't like huge counts. We limit
  * them to something that fits in "int" so that others
@@ -222,8 +237,11 @@ int rw_verify_area(int read_write, struc
 	if (unlikely((ssize_t) count < 0))
 		return retval;
 	pos = *ppos;
-	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
-		return retval;
+	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) {
+		retval = __negative_fpos_check(inode, pos, count);
+		if (retval)
+			return retval;
+	}
 
 	if (unlikely(inode->i_flock && mandatory_lock(inode))) {
 		retval = locks_mandatory_area(
include/linux/fs.h +2 -0
Index: mmotm-2.6.31-Sep14/include/linux/fs.h
===================================================================
@@ -231,6 +231,7 @@ struct inodes_stat_t {
 #define S_NOCMTIME	128	/* Do not update file c/mtime */
 #define S_SWAPFILE	256	/* Do not truncate: swapon got its bmaps */
 #define S_PRIVATE	512	/* Inode is fs-internal */
+#define S_VERYBIG	1024	/* Allow file's loff_t can be negative */
 
 /*
  * Note that nosuid etc flags are inode-specific: setting some file-system
@@ -265,6 +266,7 @@ struct inodes_stat_t {
 #define IS_NOCMTIME(inode)	((inode)->i_flags & S_NOCMTIME)
 #define IS_SWAPFILE(inode)	((inode)->i_flags & S_SWAPFILE)
 #define IS_PRIVATE(inode)	((inode)->i_flags & S_PRIVATE)
+#define IS_VERYBIG(inode)	((inode)->i_flags & S_VERYBIG)
 
 /* the read-only stuff doesn't really belong here, but any other place is
    probably as bad and I don't want to create yet another include file. */
drivers/char/mem.c +13 -10
Index: mmotm-2.6.31-Sep14/drivers/char/mem.c
===================================================================
@@ -825,22 +825,23 @@ static const struct memdev {
 	const char *name;
 	const struct file_operations *fops;
 	struct backing_dev_info *dev_info;
+	bool	verybig;
 } devlist[] = {
-	[ 1] = { "mem", &mem_fops, &directly_mappable_cdev_bdi },
+	[1]  = { "mem", &mem_fops, &directly_mappable_cdev_bdi, true },
 #ifdef CONFIG_DEVKMEM
-	[ 2] = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi },
+	[2]  = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi, true },
 #endif
-	[ 3] = {"null", &null_fops, NULL },
+	[3]  = {"null", &null_fops, NULL, false },
 #ifdef CONFIG_DEVPORT
-	[ 4] = { "port", &port_fops, NULL },
+	[4]  = { "port", &port_fops, NULL, false },
 #endif
-	[ 5] = { "zero", &zero_fops, &zero_bdi },
-	[ 6] = { "full", &full_fops, NULL },
-	[ 7] = { "random", &random_fops, NULL },
-	[ 9] = { "urandom", &urandom_fops, NULL },
-	[11] = { "kmsg", &kmsg_fops, NULL },
+	[5]  = { "zero", &zero_fops, &zero_bdi, false },
+	[6]  = { "full", &full_fops, NULL, false },
+	[7]  = { "random", &random_fops, NULL, false },
+	[9]  = { "urandom", &urandom_fops, NULL, false },
+	[11] = { "kmsg", &kmsg_fops, NULL, false },
 #ifdef CONFIG_CRASH_DUMP
-	[12] = { "oldmem", &oldmem_fops, NULL },
+	[12] = { "oldmem", &oldmem_fops, NULL, false },
 #endif
 };
 
@@ -868,6 +869,8 @@ static int memory_open(struct inode *ino
 		ret = dev->fops->open(inode, filp);
 	else
 		ret = 0;
+	if (dev->verybig)
+		inode->i_flags |= S_VERYBIG;
 out:
 	unlock_kernel();
 	return ret;
fs/proc/base.c +2 -0
Index: mmotm-2.6.31-Sep14/fs/proc/base.c
===================================================================
@@ -779,6 +779,8 @@ static const struct file_operations proc
 static int mem_open(struct inode* inode, struct file* file)
 {
 	file->private_data = (void*)((long)current->self_exec_id);
+	/* this file is read only and we can catch out-pf-range */
+	inode->i_flags |= S_VERYBIG;
 	return 0;
 }
 
#23 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling v4

Quoted 36 lines · KAMEZAWA Hiroyuki

From: KAMEZAWA Hiroyuki <[email protected]>

Now, rw_verify_area() checsk f_pos is negative or not. And if negative, returns -EINVAL.

But, some special files as /dev/(k)mem and /proc/<pid>/mem etc.. has negative offsets. And we can't do any access via read/write to the file(device).

This patch introduce a flag S_VERYBIG and allow negative file offsets for big files. (usual files don't allow it.)

Changelog: v3->v4
- make changes in mem.c aligned.
- change __negative_fpos_check() to return int.
- fixed bug in "pos" check.
- added comments.

Changelog: v2->v3
- fixed bug in rw_verify_area (it cannot be compiled)

---
 drivers/char/mem.c |   23 +++++++++++++----------
 fs/proc/base.c     |    2 ++
 fs/read_write.c    |   22 ++++++++++++++++++++--
 include/linux/fs.h |    2 ++
 4 files changed, 37 insertions(+), 12 deletions(-)

Index: mmotm-2.6.31-Sep14/fs/read_write.c
===================================================================

fs/read_write.c +0 -0
@@ -205,6 +205,21 @@ bad:
 }
 #endif

fs/read_write.c bad: · line 216

Quoted 10 lines
fs/read_write.c +9 -0
 
+static int
+__negative_fpos_check(struct inode *inode, loff_t pos, size_t count)
+{
+	/*
+	 * pos or pos+count is negative here, check overflow.
+	 * too big "count" will be caught in rw_verify_area().
+	 */
+	if ((pos < 0) && (pos + count < pos))
+		return -EOVERFLOW;

This returns -EOVERFLOW when pos=-10 and count=1. What's the intention? Just to return a different error code other than -EINVAL?

Also it seems you did two behavior changes at the same time: the above -EOVERFLOW and the below IS_VERYBIG(). Are they tightly coupled?

Quoted 10 lines
fs/read_write.c +6 -0
+	/* If !VERYBIG inode, negative pos(pos+count) is not allowed */
+	if (!IS_VERYBIG(inode))
+		return -EINVAL;
+	return 0;
+}
+
 /*
  * rw_verify_area doesn't like huge counts. We limit
  * them to something that fits in "int" so that others

fs/read_write.c int rw_verify_area(int read_write, struc · line 244

Quoted 10 lines
fs/read_write.c +5 -2
@@ -222,8 +237,11 @@ int rw_verify_area(int read_write, struc
 	if (unlikely((ssize_t) count < 0))
 		return retval;
 	pos = *ppos;
-	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
-		return retval;
+	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) {
+		retval = __negative_fpos_check(inode, pos, count);
+		if (retval)
+			return retval;
+	}

This could look more nicer:

        retval = __negative_fpos_check(inode, pos, count);
        if (retval)
                return retval;

But they are all minor issues :)

Thanks,
Fengguang

Quoted 83 lines
fs/read_write.c +0 -0
 	if (unlikely(inode->i_flock && mandatory_lock(inode))) {
 		retval = locks_mandatory_area(

Index: mmotm-2.6.31-Sep14/include/linux/fs.h
===================================================================

include/linux/fs.h +2 -0
@@ -231,6 +231,7 @@ struct inodes_stat_t {
 #define S_NOCMTIME	128	/* Do not update file c/mtime */
 #define S_SWAPFILE	256	/* Do not truncate: swapon got its bmaps */
 #define S_PRIVATE	512	/* Inode is fs-internal */
+#define S_VERYBIG	1024	/* Allow file's loff_t can be negative */
 
 /*
  * Note that nosuid etc flags are inode-specific: setting some file-system
@@ -265,6 +266,7 @@ struct inodes_stat_t {
 #define IS_NOCMTIME(inode)	((inode)->i_flags & S_NOCMTIME)
 #define IS_SWAPFILE(inode)	((inode)->i_flags & S_SWAPFILE)
 #define IS_PRIVATE(inode)	((inode)->i_flags & S_PRIVATE)
+#define IS_VERYBIG(inode)	((inode)->i_flags & S_VERYBIG)
 
 /* the read-only stuff doesn't really belong here, but any other place is
    probably as bad and I don't want to create yet another include file. */

Index: mmotm-2.6.31-Sep14/drivers/char/mem.c
===================================================================

drivers/char/mem.c +13 -10
@@ -825,22 +825,23 @@ static const struct memdev {
 	const char *name;
 	const struct file_operations *fops;
 	struct backing_dev_info *dev_info;
+	bool	verybig;
 } devlist[] = {
-	[ 1] = { "mem", &mem_fops, &directly_mappable_cdev_bdi },
+	[1]  = { "mem", &mem_fops, &directly_mappable_cdev_bdi, true },
 #ifdef CONFIG_DEVKMEM
-	[ 2] = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi },
+	[2]  = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi, true },
 #endif
-	[ 3] = {"null", &null_fops, NULL },
+	[3]  = {"null", &null_fops, NULL, false },
 #ifdef CONFIG_DEVPORT
-	[ 4] = { "port", &port_fops, NULL },
+	[4]  = { "port", &port_fops, NULL, false },
 #endif
-	[ 5] = { "zero", &zero_fops, &zero_bdi },
-	[ 6] = { "full", &full_fops, NULL },
-	[ 7] = { "random", &random_fops, NULL },
-	[ 9] = { "urandom", &urandom_fops, NULL },
-	[11] = { "kmsg", &kmsg_fops, NULL },
+	[5]  = { "zero", &zero_fops, &zero_bdi, false },
+	[6]  = { "full", &full_fops, NULL, false },
+	[7]  = { "random", &random_fops, NULL, false },
+	[9]  = { "urandom", &urandom_fops, NULL, false },
+	[11] = { "kmsg", &kmsg_fops, NULL, false },
 #ifdef CONFIG_CRASH_DUMP
-	[12] = { "oldmem", &oldmem_fops, NULL },
+	[12] = { "oldmem", &oldmem_fops, NULL, false },
 #endif
 };
 
@@ -868,6 +869,8 @@ static int memory_open(struct inode *ino
 		ret = dev->fops->open(inode, filp);
 	else
 		ret = 0;
+	if (dev->verybig)
+		inode->i_flags |= S_VERYBIG;
 out:
 	unlock_kernel();
 	return ret;

Index: mmotm-2.6.31-Sep14/fs/proc/base.c
===================================================================

fs/proc/base.c +2 -0
@@ -779,6 +779,8 @@ static const struct file_operations proc
 static int mem_open(struct inode* inode, struct file* file)
 {
 	file->private_data = (void*)((long)current->self_exec_id);
+	/* this file is read only and we can catch out-pf-range */
+	inode->i_flags |= S_VERYBIG;
 	return 0;
 }
 
#24 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling v4

Quoted 38 lines · On Thu, 17 Sep 2009 15:14:28 +0800 Wu Fengguang <[email protected]> wrote:
Quoted 37 lines · KAMEZAWA Hiroyuki

From: KAMEZAWA Hiroyuki <[email protected]>

Now, rw_verify_area() checsk f_pos is negative or not. And if negative, returns -EINVAL.

But, some special files as /dev/(k)mem and /proc/<pid>/mem etc.. has negative offsets. And we can't do any access via read/write to the file(device).

This patch introduce a flag S_VERYBIG and allow negative file offsets for big files. (usual files don't allow it.)

Changelog: v3->v4
- make changes in mem.c aligned.
- change __negative_fpos_check() to return int.
- fixed bug in "pos" check.
- added comments.

Changelog: v2->v3
- fixed bug in rw_verify_area (it cannot be compiled)

---
 drivers/char/mem.c |   23 +++++++++++++----------
 fs/proc/base.c     |    2 ++
 fs/read_write.c    |   22 ++++++++++++++++++++--
 include/linux/fs.h |    2 ++
 4 files changed, 37 insertions(+), 12 deletions(-)

Index: mmotm-2.6.31-Sep14/fs/read_write.c
===================================================================

fs/read_write.c +0 -0
@@ -205,6 +205,21 @@ bad:
 }
 #endif
 
Quoted 11 lines

fs/read_write.c bad: · line 216

Quoted 9 lines
fs/read_write.c +9 -0
+static int
+__negative_fpos_check(struct inode *inode, loff_t pos, size_t count)
+{
+	/*
+	 * pos or pos+count is negative here, check overflow.
+	 * too big "count" will be caught in rw_verify_area().
+	 */
+	if ((pos < 0) && (pos + count < pos))
+		return -EOVERFLOW;

This returns -EOVERFLOW when pos=-10 and count=1. What's the intention?

Hmm ?

pos+count=-9 > -10 ? it's ok. no -EOVERFLOW

pos=-10, count=11,
pos+count=1 > -10, then overflow.

Quoted 2 lines

Just to return a different error code other than -EINVAL?

For showing what this "if" checks. EINVAL is better ?

Thanks,
-Kame

Quoted 119 lines

Also it seems you did two behavior changes at the same time: the above -EOVERFLOW and the below IS_VERYBIG(). Are they tightly coupled?

Quoted 10 lines
fs/read_write.c +6 -0
+	/* If !VERYBIG inode, negative pos(pos+count) is not allowed */
+	if (!IS_VERYBIG(inode))
+		return -EINVAL;
+	return 0;
+}
+
 /*
  * rw_verify_area doesn't like huge counts. We limit
  * them to something that fits in "int" so that others

fs/read_write.c int rw_verify_area(int read_write, struc · line 244

Quoted 10 lines
fs/read_write.c +5 -2
@@ -222,8 +237,11 @@ int rw_verify_area(int read_write, struc
 	if (unlikely((ssize_t) count < 0))
 		return retval;
 	pos = *ppos;
-	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
-		return retval;
+	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) {
+		retval = __negative_fpos_check(inode, pos, count);
+		if (retval)
+			return retval;
+	}

This could look more nicer:

        retval = __negative_fpos_check(inode, pos, count);
        if (retval)
                return retval;

But they are all minor issues :)

Thanks,
Fengguang

Quoted 83 lines
fs/read_write.c +0 -0
 	if (unlikely(inode->i_flock && mandatory_lock(inode))) {
 		retval = locks_mandatory_area(

Index: mmotm-2.6.31-Sep14/include/linux/fs.h
===================================================================

include/linux/fs.h +2 -0
@@ -231,6 +231,7 @@ struct inodes_stat_t {
 #define S_NOCMTIME	128	/* Do not update file c/mtime */
 #define S_SWAPFILE	256	/* Do not truncate: swapon got its bmaps */
 #define S_PRIVATE	512	/* Inode is fs-internal */
+#define S_VERYBIG	1024	/* Allow file's loff_t can be negative */
 
 /*
  * Note that nosuid etc flags are inode-specific: setting some file-system
@@ -265,6 +266,7 @@ struct inodes_stat_t {
 #define IS_NOCMTIME(inode)	((inode)->i_flags & S_NOCMTIME)
 #define IS_SWAPFILE(inode)	((inode)->i_flags & S_SWAPFILE)
 #define IS_PRIVATE(inode)	((inode)->i_flags & S_PRIVATE)
+#define IS_VERYBIG(inode)	((inode)->i_flags & S_VERYBIG)
 
 /* the read-only stuff doesn't really belong here, but any other place is
    probably as bad and I don't want to create yet another include file. */

Index: mmotm-2.6.31-Sep14/drivers/char/mem.c
===================================================================

drivers/char/mem.c +13 -10
@@ -825,22 +825,23 @@ static const struct memdev {
 	const char *name;
 	const struct file_operations *fops;
 	struct backing_dev_info *dev_info;
+	bool	verybig;
 } devlist[] = {
-	[ 1] = { "mem", &mem_fops, &directly_mappable_cdev_bdi },
+	[1]  = { "mem", &mem_fops, &directly_mappable_cdev_bdi, true },
 #ifdef CONFIG_DEVKMEM
-	[ 2] = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi },
+	[2]  = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi, true },
 #endif
-	[ 3] = {"null", &null_fops, NULL },
+	[3]  = {"null", &null_fops, NULL, false },
 #ifdef CONFIG_DEVPORT
-	[ 4] = { "port", &port_fops, NULL },
+	[4]  = { "port", &port_fops, NULL, false },
 #endif
-	[ 5] = { "zero", &zero_fops, &zero_bdi },
-	[ 6] = { "full", &full_fops, NULL },
-	[ 7] = { "random", &random_fops, NULL },
-	[ 9] = { "urandom", &urandom_fops, NULL },
-	[11] = { "kmsg", &kmsg_fops, NULL },
+	[5]  = { "zero", &zero_fops, &zero_bdi, false },
+	[6]  = { "full", &full_fops, NULL, false },
+	[7]  = { "random", &random_fops, NULL, false },
+	[9]  = { "urandom", &urandom_fops, NULL, false },
+	[11] = { "kmsg", &kmsg_fops, NULL, false },
 #ifdef CONFIG_CRASH_DUMP
-	[12] = { "oldmem", &oldmem_fops, NULL },
+	[12] = { "oldmem", &oldmem_fops, NULL, false },
 #endif
 };
 
@@ -868,6 +869,8 @@ static int memory_open(struct inode *ino
 		ret = dev->fops->open(inode, filp);
 	else
 		ret = 0;
+	if (dev->verybig)
+		inode->i_flags |= S_VERYBIG;
 out:
 	unlock_kernel();
 	return ret;

Index: mmotm-2.6.31-Sep14/fs/proc/base.c
===================================================================

fs/proc/base.c +2 -0
@@ -779,6 +779,8 @@ static const struct file_operations proc
 static int mem_open(struct inode* inode, struct file* file)
 {
 	file->private_data = (void*)((long)current->self_exec_id);
+	/* this file is read only and we can catch out-pf-range */
+	inode->i_flags |= S_VERYBIG;
 	return 0;
 }
 
#25 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling v4

Quoted 52 lines · KAMEZAWA Hiroyuki
Quoted 38 lines · On Thu, 17 Sep 2009 15:14:28 +0800 Wu Fengguang <[email protected]> wrote:
Quoted 37 lines · KAMEZAWA Hiroyuki

From: KAMEZAWA Hiroyuki <[email protected]>

Now, rw_verify_area() checsk f_pos is negative or not. And if negative, returns -EINVAL.

But, some special files as /dev/(k)mem and /proc/<pid>/mem etc.. has negative offsets. And we can't do any access via read/write to the file(device).

This patch introduce a flag S_VERYBIG and allow negative file offsets for big files. (usual files don't allow it.)

Changelog: v3->v4
- make changes in mem.c aligned.
- change __negative_fpos_check() to return int.
- fixed bug in "pos" check.
- added comments.

Changelog: v2->v3
- fixed bug in rw_verify_area (it cannot be compiled)

---
 drivers/char/mem.c |   23 +++++++++++++----------
 fs/proc/base.c     |    2 ++
 fs/read_write.c    |   22 ++++++++++++++++++++--
 include/linux/fs.h |    2 ++
 4 files changed, 37 insertions(+), 12 deletions(-)

Index: mmotm-2.6.31-Sep14/fs/read_write.c
===================================================================

fs/read_write.c +0 -0
@@ -205,6 +205,21 @@ bad:
 }
 #endif
 
Quoted 11 lines

fs/read_write.c bad: · line 216

Quoted 9 lines
fs/read_write.c +9 -0
+static int
+__negative_fpos_check(struct inode *inode, loff_t pos, size_t count)
+{
+	/*
+	 * pos or pos+count is negative here, check overflow.
+	 * too big "count" will be caught in rw_verify_area().
+	 */
+	if ((pos < 0) && (pos + count < pos))
+		return -EOVERFLOW;

This returns -EOVERFLOW when pos=-10 and count=1. What's the intention?

Quoted 6 lines

Hmm ?

pos+count=-9 > -10 ? it's ok. no -EOVERFLOW

pos=-10, count=11,
pos+count=1 > -10, then overflow.

Ah yes, was confused..

Quoted 3 lines
Quoted 2 lines

Just to return a different error code other than -EINVAL?

For showing what this "if" checks. EINVAL is better ?

..so please ignore this question.

Thanks,
Fengguang

Quoted 120 lines
Quoted 120 lines

Also it seems you did two behavior changes at the same time: the above -EOVERFLOW and the below IS_VERYBIG(). Are they tightly coupled?

Quoted 10 lines
fs/read_write.c +6 -0
+	/* If !VERYBIG inode, negative pos(pos+count) is not allowed */
+	if (!IS_VERYBIG(inode))
+		return -EINVAL;
+	return 0;
+}
+
 /*
  * rw_verify_area doesn't like huge counts. We limit
  * them to something that fits in "int" so that others

fs/read_write.c int rw_verify_area(int read_write, struc · line 244

Quoted 10 lines
fs/read_write.c +5 -2
@@ -222,8 +237,11 @@ int rw_verify_area(int read_write, struc
 	if (unlikely((ssize_t) count < 0))
 		return retval;
 	pos = *ppos;
-	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
-		return retval;
+	if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) {
+		retval = __negative_fpos_check(inode, pos, count);
+		if (retval)
+			return retval;
+	}

This could look more nicer:

        retval = __negative_fpos_check(inode, pos, count);
        if (retval)
                return retval;

But they are all minor issues :)

Thanks,
Fengguang

Quoted 83 lines
fs/read_write.c +0 -0
 	if (unlikely(inode->i_flock && mandatory_lock(inode))) {
 		retval = locks_mandatory_area(

Index: mmotm-2.6.31-Sep14/include/linux/fs.h
===================================================================

include/linux/fs.h +2 -0
@@ -231,6 +231,7 @@ struct inodes_stat_t {
 #define S_NOCMTIME	128	/* Do not update file c/mtime */
 #define S_SWAPFILE	256	/* Do not truncate: swapon got its bmaps */
 #define S_PRIVATE	512	/* Inode is fs-internal */
+#define S_VERYBIG	1024	/* Allow file's loff_t can be negative */
 
 /*
  * Note that nosuid etc flags are inode-specific: setting some file-system
@@ -265,6 +266,7 @@ struct inodes_stat_t {
 #define IS_NOCMTIME(inode)	((inode)->i_flags & S_NOCMTIME)
 #define IS_SWAPFILE(inode)	((inode)->i_flags & S_SWAPFILE)
 #define IS_PRIVATE(inode)	((inode)->i_flags & S_PRIVATE)
+#define IS_VERYBIG(inode)	((inode)->i_flags & S_VERYBIG)
 
 /* the read-only stuff doesn't really belong here, but any other place is
    probably as bad and I don't want to create yet another include file. */

Index: mmotm-2.6.31-Sep14/drivers/char/mem.c
===================================================================

drivers/char/mem.c +13 -10
@@ -825,22 +825,23 @@ static const struct memdev {
 	const char *name;
 	const struct file_operations *fops;
 	struct backing_dev_info *dev_info;
+	bool	verybig;
 } devlist[] = {
-	[ 1] = { "mem", &mem_fops, &directly_mappable_cdev_bdi },
+	[1]  = { "mem", &mem_fops, &directly_mappable_cdev_bdi, true },
 #ifdef CONFIG_DEVKMEM
-	[ 2] = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi },
+	[2]  = { "kmem", &kmem_fops, &directly_mappable_cdev_bdi, true },
 #endif
-	[ 3] = {"null", &null_fops, NULL },
+	[3]  = {"null", &null_fops, NULL, false },
 #ifdef CONFIG_DEVPORT
-	[ 4] = { "port", &port_fops, NULL },
+	[4]  = { "port", &port_fops, NULL, false },
 #endif
-	[ 5] = { "zero", &zero_fops, &zero_bdi },
-	[ 6] = { "full", &full_fops, NULL },
-	[ 7] = { "random", &random_fops, NULL },
-	[ 9] = { "urandom", &urandom_fops, NULL },
-	[11] = { "kmsg", &kmsg_fops, NULL },
+	[5]  = { "zero", &zero_fops, &zero_bdi, false },
+	[6]  = { "full", &full_fops, NULL, false },
+	[7]  = { "random", &random_fops, NULL, false },
+	[9]  = { "urandom", &urandom_fops, NULL, false },
+	[11] = { "kmsg", &kmsg_fops, NULL, false },
 #ifdef CONFIG_CRASH_DUMP
-	[12] = { "oldmem", &oldmem_fops, NULL },
+	[12] = { "oldmem", &oldmem_fops, NULL, false },
 #endif
 };
 
@@ -868,6 +869,8 @@ static int memory_open(struct inode *ino
 		ret = dev->fops->open(inode, filp);
 	else
 		ret = 0;
+	if (dev->verybig)
+		inode->i_flags |= S_VERYBIG;
 out:
 	unlock_kernel();
 	return ret;

Index: mmotm-2.6.31-Sep14/fs/proc/base.c
===================================================================

fs/proc/base.c +2 -0
@@ -779,6 +779,8 @@ static const struct file_operations proc
 static int mem_open(struct inode* inode, struct file* file)
 {
 	file->private_data = (void*)((long)current->self_exec_id);
+	/* this file is read only and we can catch out-pf-range */
+	inode->i_flags |= S_VERYBIG;
 	return 0;
 }
 
#26 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling v4

In reply to KAMEZAWA Hiroyuki
Quoted 52 lines · KAMEZAWA Hiroyuki
Quoted 38 lines · On Thu, 17 Sep 2009 15:14:28 +0800 Wu Fengguang <[email protected]> wrote:
Quoted 37 lines · KAMEZAWA Hiroyuki

From: KAMEZAWA Hiroyuki <[email protected]>

Now, rw_verify_area() checsk f_pos is negative or not. And if negative, returns -EINVAL.

But, some special files as /dev/(k)mem and /proc/<pid>/mem etc.. has negative offsets. And we can't do any access via read/write to the file(device).

This patch introduce a flag S_VERYBIG and allow negative file offsets for big files. (usual files don't allow it.)

Changelog: v3->v4
- make changes in mem.c aligned.
- change __negative_fpos_check() to return int.
- fixed bug in "pos" check.
- added comments.

Changelog: v2->v3
- fixed bug in rw_verify_area (it cannot be compiled)

---
 drivers/char/mem.c |   23 +++++++++++++----------
 fs/proc/base.c     |    2 ++
 fs/read_write.c    |   22 ++++++++++++++++++++--
 include/linux/fs.h |    2 ++
 4 files changed, 37 insertions(+), 12 deletions(-)

Index: mmotm-2.6.31-Sep14/fs/read_write.c
===================================================================

fs/read_write.c +0 -0
@@ -205,6 +205,21 @@ bad:
 }
 #endif
 
Quoted 11 lines

fs/read_write.c bad: · line 216

Quoted 9 lines
fs/read_write.c +9 -0
+static int
+__negative_fpos_check(struct inode *inode, loff_t pos, size_t count)
+{
+	/*
+	 * pos or pos+count is negative here, check overflow.
+	 * too big "count" will be caught in rw_verify_area().
+	 */
+	if ((pos < 0) && (pos + count < pos))
+		return -EOVERFLOW;

This returns -EOVERFLOW when pos=-10 and count=1. What's the intention?

Quoted 6 lines

Hmm ?

pos+count=-9 > -10 ? it's ok. no -EOVERFLOW

pos=-10, count=11,
pos+count=1 > -10, then overflow.

Hmm, it seems less confusing to do

static int __negative_fpos_check(struct inode *inode,
unsigned long pos, unsigned long count)

{
        if (pos + count < pos)
                return -EOVERFLOW;
        ...
}

Thanks,
Fengguang

#27 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling v4

Quoted 64 lines · Wu Fengguang
Quoted 53 lines · KAMEZAWA Hiroyuki
Quoted 38 lines · On Thu, 17 Sep 2009 15:14:28 +0800 Wu Fengguang <[email protected]> wrote:
Quoted 37 lines · KAMEZAWA Hiroyuki

From: KAMEZAWA Hiroyuki <[email protected]>

Now, rw_verify_area() checsk f_pos is negative or not. And if negative, returns -EINVAL.

But, some special files as /dev/(k)mem and /proc/<pid>/mem etc.. has negative offsets. And we can't do any access via read/write to the file(device).

This patch introduce a flag S_VERYBIG and allow negative file offsets for big files. (usual files don't allow it.)

Changelog: v3->v4
- make changes in mem.c aligned.
- change __negative_fpos_check() to return int.
- fixed bug in "pos" check.
- added comments.

Changelog: v2->v3
- fixed bug in rw_verify_area (it cannot be compiled)

---
 drivers/char/mem.c |   23 +++++++++++++----------
 fs/proc/base.c     |    2 ++
 fs/read_write.c    |   22 ++++++++++++++++++++--
 include/linux/fs.h |    2 ++
 4 files changed, 37 insertions(+), 12 deletions(-)

Index: mmotm-2.6.31-Sep14/fs/read_write.c
===================================================================

fs/read_write.c +0 -0
@@ -205,6 +205,21 @@ bad:
 }
 #endif
 
Quoted 2 lines

fs/read_write.c bad: · line 209

Quoted 2 lines
fs/read_write.c +2 -0
+static int
+__negative_fpos_check(struct inode *inode, loff_t pos, size_t

count)

Quoted 9 lines

fs/read_write.c bad: · line 216

Quoted 7 lines
fs/read_write.c +7 -0
+{
+	/*
+	 * pos or pos+count is negative here, check overflow.
+	 * too big "count" will be caught in rw_verify_area().
+	 */
+	if ((pos < 0) && (pos + count < pos))
+		return -EOVERFLOW;

This returns -EOVERFLOW when pos=-10 and count=1. What's the

Quoted 7 lines

intention?
Hmm ?

pos+count=-9 > -10 ? it's ok. no -EOVERFLOW

pos=-10, count=11,
pos+count=1 > -10, then overflow.

Hmm, it seems less confusing to do

Quoted 9 lines

static int __negative_fpos_check(struct inode *inode,
unsigned long pos, unsigned long count)

{
        if (pos + count < pos)
                return -EOVERFLOW;
        ...
}

have to avoid pos == LONGLONGMAX case.

Thanks,
-Kame

Quoted 9 lines

Thanks,
Fengguang

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#28 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling v4

Quoted 74 lines · KAMEZAWA Hiroyuki
Quoted 64 lines · Wu Fengguang
Quoted 53 lines · KAMEZAWA Hiroyuki
Quoted 38 lines · On Thu, 17 Sep 2009 15:14:28 +0800 Wu Fengguang <[email protected]> wrote:
Quoted 37 lines · KAMEZAWA Hiroyuki

From: KAMEZAWA Hiroyuki <[email protected]>

Now, rw_verify_area() checsk f_pos is negative or not. And if negative, returns -EINVAL.

But, some special files as /dev/(k)mem and /proc/<pid>/mem etc.. has negative offsets. And we can't do any access via read/write to the file(device).

This patch introduce a flag S_VERYBIG and allow negative file offsets for big files. (usual files don't allow it.)

Changelog: v3->v4
- make changes in mem.c aligned.
- change __negative_fpos_check() to return int.
- fixed bug in "pos" check.
- added comments.

Changelog: v2->v3
- fixed bug in rw_verify_area (it cannot be compiled)

---
 drivers/char/mem.c |   23 +++++++++++++----------
 fs/proc/base.c     |    2 ++
 fs/read_write.c    |   22 ++++++++++++++++++++--
 include/linux/fs.h |    2 ++
 4 files changed, 37 insertions(+), 12 deletions(-)

Index: mmotm-2.6.31-Sep14/fs/read_write.c
===================================================================

fs/read_write.c +0 -0
@@ -205,6 +205,21 @@ bad:
 }
 #endif
 
Quoted 2 lines

fs/read_write.c bad: · line 209

Quoted 2 lines
fs/read_write.c +2 -0
+static int
+__negative_fpos_check(struct inode *inode, loff_t pos, size_t

count)

Quoted 9 lines

fs/read_write.c bad: · line 216

Quoted 7 lines
fs/read_write.c +7 -0
+{
+	/*
+	 * pos or pos+count is negative here, check overflow.
+	 * too big "count" will be caught in rw_verify_area().
+	 */
+	if ((pos < 0) && (pos + count < pos))
+		return -EOVERFLOW;

This returns -EOVERFLOW when pos=-10 and count=1. What's the

Quoted 7 lines

intention?
Hmm ?

pos+count=-9 > -10 ? it's ok. no -EOVERFLOW

pos=-10, count=11,
pos+count=1 > -10, then overflow.

Hmm, it seems less confusing to do

Quoted 9 lines

static int __negative_fpos_check(struct inode *inode,
unsigned long pos, unsigned long count)

{
        if (pos + count < pos)
                return -EOVERFLOW;
        ...
}
Quoted 2 lines

have to avoid pos == LONGLONGMAX case.

Ah, you ask me to do cast from loff_t to unsigned long long ?

Not making much difference, I think. This is usual math. But ok, I don't want to explain again.
If I post v5, I'll do.

Thanks,
-Kame

Quoted 22 lines

Thanks,
-Kame

Quoted 10 lines

Thanks,
Fengguang

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#29 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling v4

In reply to KAMEZAWA Hiroyuki
Quoted 74 lines · KAMEZAWA Hiroyuki
Quoted 64 lines · Wu Fengguang
Quoted 53 lines · KAMEZAWA Hiroyuki
Quoted 38 lines · On Thu, 17 Sep 2009 15:14:28 +0800 Wu Fengguang <[email protected]> wrote:
Quoted 37 lines · KAMEZAWA Hiroyuki

From: KAMEZAWA Hiroyuki <[email protected]>

Now, rw_verify_area() checsk f_pos is negative or not. And if negative, returns -EINVAL.

But, some special files as /dev/(k)mem and /proc/<pid>/mem etc.. has negative offsets. And we can't do any access via read/write to the file(device).

This patch introduce a flag S_VERYBIG and allow negative file offsets for big files. (usual files don't allow it.)

Changelog: v3->v4
- make changes in mem.c aligned.
- change __negative_fpos_check() to return int.
- fixed bug in "pos" check.
- added comments.

Changelog: v2->v3
- fixed bug in rw_verify_area (it cannot be compiled)

---
 drivers/char/mem.c |   23 +++++++++++++----------
 fs/proc/base.c     |    2 ++
 fs/read_write.c    |   22 ++++++++++++++++++++--
 include/linux/fs.h |    2 ++
 4 files changed, 37 insertions(+), 12 deletions(-)

Index: mmotm-2.6.31-Sep14/fs/read_write.c
===================================================================

fs/read_write.c +0 -0
@@ -205,6 +205,21 @@ bad:
 }
 #endif
 
Quoted 2 lines

fs/read_write.c bad: · line 209

Quoted 2 lines
fs/read_write.c +2 -0
+static int
+__negative_fpos_check(struct inode *inode, loff_t pos, size_t

count)

Quoted 9 lines

fs/read_write.c bad: · line 216

Quoted 7 lines
fs/read_write.c +7 -0
+{
+	/*
+	 * pos or pos+count is negative here, check overflow.
+	 * too big "count" will be caught in rw_verify_area().
+	 */
+	if ((pos < 0) && (pos + count < pos))
+		return -EOVERFLOW;

This returns -EOVERFLOW when pos=-10 and count=1. What's the

Quoted 7 lines

intention?
Hmm ?

pos+count=-9 > -10 ? it's ok. no -EOVERFLOW

pos=-10, count=11,
pos+count=1 > -10, then overflow.

Hmm, it seems less confusing to do

Quoted 9 lines

static int __negative_fpos_check(struct inode *inode,
unsigned long pos, unsigned long count)

{
        if (pos + count < pos)
                return -EOVERFLOW;
        ...
}
Quoted 1 line

have to avoid pos == LONGLONGMAX case.

Ah sorry, didn't know loff_t could be long long..

Thanks,
Fengguang

#30 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling v4

Quoted 9 lines · On Thu, 17 Sep 2009 20:40:39 +0800 Wu Fengguang <[email protected]> wrote:
Quoted 6 lines
Quoted 5 lines
        if (pos + count < pos)
                return -EOVERFLOW;
        ...
}

have to avoid pos == LONGLONGMAX case.

Ah sorry, didn't know loff_t could be long long..

Thank you. I'll restart this after merge window.
(And maybe I had to CC fsdevel...)

Thanks,
-Kame

#31 lore raw

Re: [RFC][PATCH][bugfix] more checks for negative f_pos handling v4

Quoted 8 lines · KAMEZAWA Hiroyuki
Quoted 5 lines · On Thu, 17 Sep 2009 20:40:39 +0800 Wu Fengguang <[email protected]> wrote:
Quoted 5 lines
Quoted 5 lines

        if (pos + count < pos)
                return -EOVERFLOW;
        ...
}

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

have to avoid pos == LONGLONGMAX case.

Ah sorry, didn't know loff_t could be long long..

Thank you. I'll restart this after merge window.
(And maybe I had to CC fsdevel...)

Yes, please do so. I would like to hear from Al..