mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [patch] mm: madvise correct return code
@ 2009-05-12  5:21 Nick Piggin
  2009-05-12  6:30 ` Américo Wang
  0 siblings, 1 reply; 2+ messages in thread
From: Nick Piggin @ 2009-05-12  5:21 UTC (permalink / raw)
  To: Andrew Morton, Linux Kernel Mailing List

This is a pretty obscure case, but it doesn't hurt to follow standards
if they're there. I can't imagine any real program relying on a 0 range
madvise call with invalid behaviour *not* failing... We have this in
SLES11 FWIW, and no problems reported.
--

mm: madvise correct return code

The posix_madvise() function succeeds (and does nothing) when called with
parameters (NULL, 0, -1); according to LSB tests, it should fail with EINVAL
because -1 is not a valid flag.

When called with a valid address and size, it correctly fails.

So perform an initial check for valid flags first.

Reported-by: Jiri Dluhos <jdluhos@novell.com>
Signed-off-by: Nick Piggin <npiggin@suse.de>
---
 mm/madvise.c |   23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

Index: linux-2.6/mm/madvise.c
===================================================================
--- linux-2.6.orig/mm/madvise.c
+++ linux-2.6/mm/madvise.c
@@ -247,12 +247,30 @@ madvise_vma(struct vm_area_struct *vma,
 		break;
 
 	default:
-		error = -EINVAL;
+		BUG();
 		break;
 	}
 	return error;
 }
 
+static int
+madvise_behavior_valid(int behavior)
+{
+	switch (behavior) {
+	case MADV_DOFORK:
+	case MADV_DONTFORK:
+	case MADV_NORMAL:
+	case MADV_SEQUENTIAL:
+	case MADV_RANDOM:
+	case MADV_REMOVE:
+	case MADV_WILLNEED:
+	case MADV_DONTNEED:
+		return 1;
+
+	default:
+		return 0;
+	}
+}
 /*
  * The madvise(2) system call.
  *
@@ -298,6 +316,9 @@ SYSCALL_DEFINE3(madvise, unsigned long,
 	int write;
 	size_t len;
 
+	if (!madvise_behavior_valid(behavior))
+		return error;
+
 	write = madvise_need_mmap_write(behavior);
 	if (write)
 		down_write(&current->mm->mmap_sem);

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [patch] mm: madvise correct return code
  2009-05-12  5:21 [patch] mm: madvise correct return code Nick Piggin
@ 2009-05-12  6:30 ` Américo Wang
  0 siblings, 0 replies; 2+ messages in thread
From: Américo Wang @ 2009-05-12  6:30 UTC (permalink / raw)
  To: Nick Piggin; +Cc: Andrew Morton, Linux Kernel Mailing List

On Tue, May 12, 2009 at 1:21 PM, Nick Piggin <npiggin@suse.de> wrote:
> This is a pretty obscure case, but it doesn't hurt to follow standards
> if they're there. I can't imagine any real program relying on a 0 range
> madvise call with invalid behaviour *not* failing... We have this in
> SLES11 FWIW, and no problems reported.
> --


But it was not consistent with the man page... we should fix it.

>
> mm: madvise correct return code
>
> The posix_madvise() function succeeds (and does nothing) when called with
> parameters (NULL, 0, -1); according to LSB tests, it should fail with EINVAL
> because -1 is not a valid flag.
>
> When called with a valid address and size, it correctly fails.
>
> So perform an initial check for valid flags first.
>
> Reported-by: Jiri Dluhos <jdluhos@novell.com>
> Signed-off-by: Nick Piggin <npiggin@suse.de>

Reviewed-and-Tested-by: WANG Cong <xiyou.wangcong@gmail.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-05-12  6:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-12  5:21 [patch] mm: madvise correct return code Nick Piggin
2009-05-12  6:30 ` Américo Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®