]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/msgctl.c
linux: Clear mode_t padding bits (BZ#25623)
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / msgctl.c
1 /* Copyright (C) 1995-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <sys/msg.h>
20 #include <ipc_priv.h>
21 #include <sysdep.h>
22 #include <shlib-compat.h>
23 #include <errno.h>
24
25 #ifndef DEFAULT_VERSION
26 # ifndef __ASSUME_SYSVIPC_BROKEN_MODE_T
27 # define DEFAULT_VERSION GLIBC_2_2
28 # else
29 # define DEFAULT_VERSION GLIBC_2_31
30 # endif
31 #endif
32
33 static int
34 msgctl_syscall (int msqid, int cmd, struct msqid_ds *buf)
35 {
36 #ifdef __ASSUME_DIRECT_SYSVIPC_SYSCALLS
37 return INLINE_SYSCALL_CALL (msgctl, msqid, cmd | __IPC_64, buf);
38 #else
39 return INLINE_SYSCALL_CALL (ipc, IPCOP_msgctl, msqid, cmd | __IPC_64, 0,
40 buf);
41 #endif
42 }
43
44 int
45 __new_msgctl (int msqid, int cmd, struct msqid_ds *buf)
46 {
47 /* POSIX states ipc_perm mode should have type of mode_t. */
48 _Static_assert (sizeof ((struct msqid_ds){0}.msg_perm.mode)
49 == sizeof (mode_t),
50 "sizeof (msqid_ds.msg_perm.mode) != sizeof (mode_t)");
51
52 #ifdef __ASSUME_SYSVIPC_BROKEN_MODE_T
53 struct msqid_ds tmpds;
54 if (cmd == IPC_SET)
55 {
56 tmpds = *buf;
57 tmpds.msg_perm.mode *= 0x10000U;
58 buf = &tmpds;
59 }
60 #endif
61
62 int ret = msgctl_syscall (msqid, cmd, buf);
63
64 if (ret >= 0)
65 {
66 switch (cmd)
67 {
68 case IPC_STAT:
69 case MSG_STAT:
70 case MSG_STAT_ANY:
71 #ifdef __ASSUME_SYSVIPC_BROKEN_MODE_T
72 buf->msg_perm.mode >>= 16;
73 #else
74 /* Old Linux kernel versions might not clear the mode padding. */
75 if (sizeof ((struct msqid_ds){0}.msg_perm.mode)
76 != sizeof (__kernel_mode_t))
77 buf->msg_perm.mode &= 0xFFFF;
78 #endif
79 }
80 }
81
82 return ret;
83 }
84 versioned_symbol (libc, __new_msgctl, msgctl, DEFAULT_VERSION);
85
86 #if defined __ASSUME_SYSVIPC_BROKEN_MODE_T \
87 && SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_31)
88 int
89 attribute_compat_text_section
90 __msgctl_mode16 (int msqid, int cmd, struct msqid_ds *buf)
91 {
92 return msgctl_syscall (msqid, cmd, buf);
93 }
94 compat_symbol (libc, __msgctl_mode16, msgctl, GLIBC_2_2);
95 #endif
96
97 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
98 struct __old_msqid_ds
99 {
100 struct __old_ipc_perm msg_perm; /* structure describing operation permission */
101 struct msg *__msg_first; /* pointer to first message on queue */
102 struct msg *__msg_last; /* pointer to last message on queue */
103 __time_t msg_stime; /* time of last msgsnd command */
104 __time_t msg_rtime; /* time of last msgrcv command */
105 __time_t msg_ctime; /* time of last change */
106 struct wait_queue *__wwait; /* ??? */
107 struct wait_queue *__rwait; /* ??? */
108 unsigned short int __msg_cbytes; /* current number of bytes on queue */
109 unsigned short int msg_qnum; /* number of messages currently on queue */
110 unsigned short int msg_qbytes; /* max number of bytes allowed on queue */
111 __ipc_pid_t msg_lspid; /* pid of last msgsnd() */
112 __ipc_pid_t msg_lrpid; /* pid of last msgrcv() */
113 };
114
115 int
116 attribute_compat_text_section
117 __old_msgctl (int msqid, int cmd, struct __old_msqid_ds *buf)
118 {
119 #if defined __ASSUME_DIRECT_SYSVIPC_SYSCALLS \
120 && !defined __ASSUME_SYSVIPC_DEFAULT_IPC_64
121 /* For architecture that have wire-up msgctl but also have __IPC_64 to a
122 value different than default (0x0) it means the compat symbol used the
123 __NR_ipc syscall. */
124 return INLINE_SYSCALL_CALL (msgctl, msqid, cmd, buf);
125 #else
126 return INLINE_SYSCALL_CALL (ipc, IPCOP_msgctl, msqid, cmd, 0, buf);
127 #endif
128 }
129 compat_symbol (libc, __old_msgctl, msgctl, GLIBC_2_0);
130 #endif