pipe: set lower and upper limit on max pages in the pipe page array splice
authorJens Axboe <jens.axboe@oracle.com>
Wed, 19 May 2010 19:03:16 +0000 (21:03 +0200)
committerJens Axboe <axboe@nehalem.(none)>
Thu, 20 May 2010 08:43:36 +0000 (10:43 +0200)
We need at least two to guarantee proper POSIX behaviour, so
never allow a smaller limit than that.

Also expose a /proc/sys/fs/pipe-max-pages sysctl file that allows
root to define a sane upper limit. Make it default to 16 times the
default size, which is 16 pages.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
fs/pipe.c
include/linux/pipe_fs_i.h
kernel/sysctl.c

index 054b8a6a2c7a2183547fa052565777d062340a70..d79872eba09a56a03ade25f28a9d9cc8c8b86cb8 100644 (file)
--- a/fs/pipe.c
+++ b/fs/pipe.c
 #include <linux/pagemap.h>
 #include <linux/audit.h>
 #include <linux/syscalls.h>
+#include <linux/fcntl.h>
 
 #include <asm/uaccess.h>
 #include <asm/ioctls.h>
 
+/*
+ * The max size that a non-root user is allowed to grow the pipe. Can
+ * be set by root in /proc/sys/fs/pipe-max-pages
+ */
+unsigned int pipe_max_pages = PIPE_DEF_BUFFERS * 16;
+
 /*
  * We use a start+len construction, which provides full use of the 
  * allocated memory.
@@ -1162,6 +1169,14 @@ long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
 
        switch (cmd) {
        case F_SETPIPE_SZ:
+               if (!capable(CAP_SYS_ADMIN) && arg > pipe_max_pages)
+                       return -EINVAL;
+               /*
+                * The pipe needs to be at least 2 pages large to
+                * guarantee POSIX behaviour.
+                */
+               if (arg < 2)
+                       return -EINVAL;
                ret = pipe_set_size(pipe, arg);
                break;
        case F_GETPIPE_SZ:
index 65f4282fcbaf40b293e15b3092fbb3e214c319ee..16de3933c45ed32b6c4e51a00514f00fe1667d97 100644 (file)
@@ -139,6 +139,8 @@ void pipe_lock(struct pipe_inode_info *);
 void pipe_unlock(struct pipe_inode_info *);
 void pipe_double_lock(struct pipe_inode_info *, struct pipe_inode_info *);
 
+extern unsigned int pipe_max_pages;
+
 /* Drop the inode semaphore and wait for a pipe event, atomically */
 void pipe_wait(struct pipe_inode_info *pipe);
 
index 90f536d846439439593af5f85c92bace046c611e..7742d27e08f285aeacd25e5434206ab5a0483ba9 100644 (file)
@@ -52,6 +52,7 @@
 #include <linux/slow-work.h>
 #include <linux/perf_event.h>
 #include <linux/kprobes.h>
+#include <linux/pipe_fs_i.h>
 
 #include <asm/uaccess.h>
 #include <asm/processor.h>
@@ -1423,6 +1424,14 @@ static struct ctl_table fs_table[] = {
                .child          = binfmt_misc_table,
        },
 #endif
+       {
+               .procname       = "pipe-max-pages",
+               .data           = &pipe_max_pages,
+               .maxlen         = sizeof(int),
+               .mode           = 0644,
+               .proc_handler   = &proc_dointvec_minmax,
+               .extra1         = &two,
+       },
 /*
  * NOTE: do not add new entries to this table unless you have read
  * Documentation/sysctl/ctl_unnumbered.txt