Skip to content

Commit

Permalink
mm: madvise(): correct return code
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Nick Piggin <[email protected]>
Reviewed-and-Tested-by: WANG Cong <[email protected]>
Cc: Michael Kerrisk <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Nick Piggin authored and torvalds committed Jun 17, 2009
1 parent dab48da commit 75927af
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion mm/madvise.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,30 @@ madvise_vma(struct vm_area_struct *vma, struct vm_area_struct **prev,
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.
*
Expand Down Expand Up @@ -289,6 +307,9 @@ SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, behavior)
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);
Expand Down

0 comments on commit 75927af

Please sign in to comment.