]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
ipc/mqueue: strengthen checks on mqueue creation
authorDoug Ledford <dledford@redhat.com>
Thu, 3 May 2012 05:44:52 +0000 (15:44 +1000)
committerStephen Rothwell <sfr@canb.auug.org.au>
Thu, 3 May 2012 05:47:02 +0000 (15:47 +1000)
We already check the mq attr struct if it's passed in, but now that the
admin can set system wide defaults separate from maximums, it's actually
possible to set the defaults to something that would overflow.  So, if
there is no attr struct passed in to the open call, check the default
values.

While we are at it, simplify mq_attr_ok() by making it return 0 or an
error condition, so that way if we add more tests to it later, we have the
option of what error should be returned instead of the calling location
having to pick a possibly inaccurate error code.

Signed-off-by: Doug Ledford <dledford@redhat.com>
Cc: Stephen Rothwell <sfr@canb.auug.org.au>
Cc: Manfred Spraul <manfred@colorfullife.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
ipc/mqueue.c

index 51fe33bae45554c0203a2d7b65213c23645fb8f5..b0461f8459b7f04886ca189f8da5f0d8a2b07522 100644 (file)
@@ -675,26 +675,26 @@ static int mq_attr_ok(struct ipc_namespace *ipc_ns, struct mq_attr *attr)
        unsigned long total_size;
 
        if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
-               return 0;
+               return -EINVAL;
        if (capable(CAP_SYS_RESOURCE)) {
                if (attr->mq_maxmsg > HARD_MSGMAX ||
                    attr->mq_msgsize > HARD_MSGSIZEMAX)
-                       return 0;
+                       return -EINVAL;
        } else {
                if (attr->mq_maxmsg > ipc_ns->mq_msg_max ||
                                attr->mq_msgsize > ipc_ns->mq_msgsize_max)
-                       return 0;
+                       return -EINVAL;
        }
        /* check for overflow */
        if (attr->mq_msgsize > ULONG_MAX/attr->mq_maxmsg)
-               return 0;
+               return -ENOMEM;
        mq_treesize = attr->mq_maxmsg * sizeof(struct msg_msg) +
                min_t(unsigned int, attr->mq_maxmsg, MQ_PRIO_MAX) *
                sizeof(struct posix_msg_tree_node);
        total_size = attr->mq_maxmsg * attr->mq_msgsize;
        if (total_size + mq_treesize < total_size)
-               return 0;
-       return 1;
+               return -ENOMEM;
+       return 0;
 }
 
 /*
@@ -709,12 +709,21 @@ static struct file *do_create(struct ipc_namespace *ipc_ns, struct dentry *dir,
        int ret;
 
        if (attr) {
-               if (!mq_attr_ok(ipc_ns, attr)) {
-                       ret = -EINVAL;
+               ret = mq_attr_ok(ipc_ns, attr);
+               if (ret)
                        goto out;
-               }
                /* store for use during create */
                dentry->d_fsdata = attr;
+       } else {
+               struct mq_attr def_attr;
+
+               def_attr.mq_maxmsg = min(ipc_ns->mq_msg_max,
+                                        ipc_ns->mq_msg_default);
+               def_attr.mq_msgsize = min(ipc_ns->mq_msgsize_max,
+                                         ipc_ns->mq_msgsize_default);
+               ret = mq_attr_ok(ipc_ns, &def_attr);
+               if (ret)
+                       goto out;
        }
 
        mode &= ~current_umask();