mq_open: add long file name check and parameter check

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao
2021-07-12 22:15:25 +08:00
committed by Gustavo Henrique Nihei
parent 40b467420f
commit 0aa14f832d
4 changed files with 38 additions and 14 deletions
+8 -3
View File
@@ -177,6 +177,12 @@ static int file_mq_vopen(FAR struct file *mq, FAR const char *mq_name,
goto errout; goto errout;
} }
if (strlen(mq_name) > NAME_MAX)
{
ret = -ENAMETOOLONG;
goto errout;
}
/* Were we asked to create it? */ /* Were we asked to create it? */
if ((oflags & O_CREAT) != 0) if ((oflags & O_CREAT) != 0)
@@ -289,10 +295,9 @@ static int file_mq_vopen(FAR struct file *mq, FAR const char *mq_name,
* be created with a reference count of zero. * be created with a reference count of zero.
*/ */
msgq = (FAR struct mqueue_inode_s *)nxmq_alloc_msgq(attr); ret = nxmq_alloc_msgq(attr, &msgq);
if (!msgq) if (ret < 0)
{ {
ret = -ENOSPC;
goto errout_with_inode; goto errout_with_inode;
} }
+1 -1
View File
@@ -39,6 +39,6 @@
/* Sizes of things */ /* Sizes of things */
#define MAX_MQUEUE_PATH 64 #define MAX_MQUEUE_PATH (sizeof(CONFIG_FS_MQUEUE_MPATH) + NAME_MAX)
#endif /* __FS_MQUEUE_MQUEUE_H */ #endif /* __FS_MQUEUE_MQUEUE_H */
+10 -3
View File
@@ -385,14 +385,21 @@ void nxmq_free_msgq(FAR struct mqueue_inode_s *msgq);
* attr - The mq_maxmsg attribute is used at the time that the message * attr - The mq_maxmsg attribute is used at the time that the message
* queue is created to determine the maximum number of * queue is created to determine the maximum number of
* messages that may be placed in the message queue. * messages that may be placed in the message queue.
* pmsgq - This parameter is a address of a pointer
* *
* Returned Value: * Returned Value:
* The allocated and initialized message queue structure or NULL in the * Zero (OK) is returned on success. Otherwise, a negated errno value is
* event of a failure. * returned to indicate the nature of the failure.
*
* EINVAL attr is NULL or either attr->mq_mqssize or attr->mq_maxmsg
* have an invalid value
* ENOSPC There is insufficient space for the creation of the new
* message queue
* *
****************************************************************************/ ****************************************************************************/
FAR struct mqueue_inode_s *nxmq_alloc_msgq(FAR struct mq_attr *attr); int nxmq_alloc_msgq(FAR struct mq_attr *attr,
FAR struct mqueue_inode_s **pmsgq);
/**************************************************************************** /****************************************************************************
* Name: nxmq_pollnotify * Name: nxmq_pollnotify
+19 -7
View File
@@ -49,14 +49,21 @@
* attr - The mq_maxmsg attribute is used at the time that the message * attr - The mq_maxmsg attribute is used at the time that the message
* queue is created to determine the maximum number of * queue is created to determine the maximum number of
* messages that may be placed in the message queue. * messages that may be placed in the message queue.
* pmsgq - This parameter is a address of a pointer
* *
* Returned Value: * Returned Value:
* The allocated and initialized message queue structure or NULL in the * Zero (OK) is returned on success. Otherwise, a negated errno value is
* event of a failure. * returned to indicate the nature of the failure.
*
* EINVAL attr is NULL or either attr->mq_mqssize or attr->mq_maxmsg
* have an invalid value
* ENOSPC There is insufficient space for the creation of the new
* message queue
* *
****************************************************************************/ ****************************************************************************/
FAR struct mqueue_inode_s *nxmq_alloc_msgq(FAR struct mq_attr *attr) int nxmq_alloc_msgq(FAR struct mq_attr *attr,
FAR struct mqueue_inode_s **pmsgq)
{ {
FAR struct mqueue_inode_s *msgq; FAR struct mqueue_inode_s *msgq;
@@ -64,10 +71,10 @@ FAR struct mqueue_inode_s *nxmq_alloc_msgq(FAR struct mq_attr *attr)
* larger than the configured maximum message size. * larger than the configured maximum message size.
*/ */
DEBUGASSERT(!attr || attr->mq_msgsize <= MQ_MAX_BYTES); DEBUGASSERT((!attr || attr->mq_msgsize <= MQ_MAX_BYTES) && pmsgq);
if (attr && attr->mq_msgsize > MQ_MAX_BYTES) if ((attr && attr->mq_msgsize > MQ_MAX_BYTES) || !pmsgq)
{ {
return NULL; return -EINVAL;
} }
/* Allocate memory for the new message queue. */ /* Allocate memory for the new message queue. */
@@ -93,6 +100,11 @@ FAR struct mqueue_inode_s *nxmq_alloc_msgq(FAR struct mq_attr *attr)
msgq->ntpid = INVALID_PROCESS_ID; msgq->ntpid = INVALID_PROCESS_ID;
} }
else
{
return -ENOSPC;
}
return msgq; *pmsgq = msgq;
return OK;
} }