mq_open: Perform check for mq name

Submitted by Qiang Huang on April 28, 2019, 9:31 a.m.

Details

Message ID 1556443913-18270-1-git-send-email-h.huangqiang@huawei.com
State New
Series "mq_open: Perform check for mq name"
Headers show

Commit Message

Qiang Huang April 28, 2019, 9:31 a.m.
According to Linux man page:
[http://man7.org/linux/man-pages/man2/mq_open.2.html]

```
C library/kernel differences
   The mq_open() library function is implemented on top of a system call
   of the same name.  The library function performs the check that the
   name starts with a slash (/), giving the EINVAL error if it does not.
   The kernel system call expects name to contain no preceding slash, so
   the C library function passes name without the preceding slash (i.e.,
   name+1) to the system call.
```

glibc performs the check but musl doesn't, add the
check so we can have consistent behavior.

Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
---
 src/mq/mq_open.c | 2 ++
 1 file changed, 2 insertions(+)

Patch hide | download patch | download mbox

diff --git a/src/mq/mq_open.c b/src/mq/mq_open.c
index aa91d58..e626228 100644
--- a/src/mq/mq_open.c
+++ b/src/mq/mq_open.c
@@ -1,12 +1,14 @@ 
 #include <mqueue.h>
 #include <fcntl.h>
 #include <stdarg.h>
+#include <errno.h>
 #include "syscall.h"
 
 mqd_t mq_open(const char *name, int flags, ...)
 {
 	mode_t mode = 0;
 	struct mq_attr *attr = 0;
+	if (name[0] != '/') return __syscall_ret(-EINVAL);
 	if (*name == '/') name++;
 	if (flags & O_CREAT) {
 		va_list ap;

Comments

Szabolcs Nagy April 28, 2019, 11:16 a.m.
* Qiang Huang <h.huangqiang@huawei.com> [2019-04-28 05:31:53 -0400]:
> According to Linux man page:
> [http://man7.org/linux/man-pages/man2/mq_open.2.html]
> 
> ```
> C library/kernel differences
>    The mq_open() library function is implemented on top of a system call
>    of the same name.  The library function performs the check that the
>    name starts with a slash (/), giving the EINVAL error if it does not.
>    The kernel system call expects name to contain no preceding slash, so
>    the C library function passes name without the preceding slash (i.e.,
>    name+1) to the system call.
> ```
> 
> glibc performs the check but musl doesn't, add the
> check so we can have consistent behavior.
> 
> Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>

posix says:

 "If name does not begin with the <slash> character,
  the effect is implementation-defined."

and on linux the documented behaviour is EINVAL, so
the patch looks ok.

> ---
>  src/mq/mq_open.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/src/mq/mq_open.c b/src/mq/mq_open.c
> index aa91d58..e626228 100644
> --- a/src/mq/mq_open.c
> +++ b/src/mq/mq_open.c
> @@ -1,12 +1,14 @@
>  #include <mqueue.h>
>  #include <fcntl.h>
>  #include <stdarg.h>
> +#include <errno.h>
>  #include "syscall.h"
>  
>  mqd_t mq_open(const char *name, int flags, ...)
>  {
>  	mode_t mode = 0;
>  	struct mq_attr *attr = 0;
> +	if (name[0] != '/') return __syscall_ret(-EINVAL);
>  	if (*name == '/') name++;
>  	if (flags & O_CREAT) {
>  		va_list ap;
> -- 
> 2.7.4