1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 1999, 2004 Manfred Spraul
7 #include <linux/spinlock.h>
8 #include <linux/init.h>
9 #include <linux/security.h>
10 #include <linux/slab.h>
11 #include <linux/ipc.h>
12 #include <linux/msg.h>
13 #include <linux/ipc_namespace.h>
14 #include <linux/utsname.h>
15 #include <linux/proc_ns.h>
16 #include <linux/uaccess.h>
17 #include <linux/sched.h>
21 DEFINE_SPINLOCK(mq_lock);
24 * The next 2 defines are here bc this is the only file
25 * compiled when either CONFIG_SYSVIPC and CONFIG_POSIX_MQUEUE
26 * and not CONFIG_IPC_NS.
28 struct ipc_namespace init_ipc_ns = {
29 .ns.count = REFCOUNT_INIT(1),
30 .user_ns = &init_user_ns,
31 .ns.inum = PROC_IPC_INIT_INO,
33 .ns.ops = &ipcns_operations,
38 struct msg_msgseg *next;
39 /* the next part of the message follows immediately */
42 #define DATALEN_MSG ((size_t)PAGE_SIZE-sizeof(struct msg_msg))
43 #define DATALEN_SEG ((size_t)PAGE_SIZE-sizeof(struct msg_msgseg))
45 static kmem_buckets *msg_buckets __ro_after_init;
47 static int __init init_msg_buckets(void)
49 msg_buckets = kmem_buckets_create("msg_msg", SLAB_ACCOUNT,
50 sizeof(struct msg_msg),
55 subsys_initcall(init_msg_buckets);
57 static struct msg_msg *alloc_msg(size_t len)
60 struct msg_msgseg **pseg;
63 alen = min(len, DATALEN_MSG);
64 msg = kmem_buckets_alloc(msg_buckets, sizeof(*msg) + alen, GFP_KERNEL);
74 struct msg_msgseg *seg;
78 alen = min(len, DATALEN_SEG);
79 seg = kmalloc(sizeof(*seg) + alen, GFP_KERNEL_ACCOUNT);
95 struct msg_msg *load_msg(const void __user *src, size_t len)
98 struct msg_msgseg *seg;
102 msg = alloc_msg(len);
104 return ERR_PTR(-ENOMEM);
106 alen = min(len, DATALEN_MSG);
107 if (copy_from_user(msg + 1, src, alen))
110 for (seg = msg->next; seg != NULL; seg = seg->next) {
112 src = (char __user *)src + alen;
113 alen = min(len, DATALEN_SEG);
114 if (copy_from_user(seg + 1, src, alen))
118 err = security_msg_msg_alloc(msg);
128 #ifdef CONFIG_CHECKPOINT_RESTORE
129 struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
131 struct msg_msgseg *dst_pseg, *src_pseg;
132 size_t len = src->m_ts;
135 if (src->m_ts > dst->m_ts)
136 return ERR_PTR(-EINVAL);
138 alen = min(len, DATALEN_MSG);
139 memcpy(dst + 1, src + 1, alen);
141 for (dst_pseg = dst->next, src_pseg = src->next;
143 dst_pseg = dst_pseg->next, src_pseg = src_pseg->next) {
146 alen = min(len, DATALEN_SEG);
147 memcpy(dst_pseg + 1, src_pseg + 1, alen);
150 dst->m_type = src->m_type;
151 dst->m_ts = src->m_ts;
156 struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst)
158 return ERR_PTR(-ENOSYS);
161 int store_msg(void __user *dest, struct msg_msg *msg, size_t len)
164 struct msg_msgseg *seg;
166 alen = min(len, DATALEN_MSG);
167 if (copy_to_user(dest, msg + 1, alen))
170 for (seg = msg->next; seg != NULL; seg = seg->next) {
172 dest = (char __user *)dest + alen;
173 alen = min(len, DATALEN_SEG);
174 if (copy_to_user(dest, seg + 1, alen))
180 void free_msg(struct msg_msg *msg)
182 struct msg_msgseg *seg;
184 security_msg_msg_free(msg);
188 while (seg != NULL) {
189 struct msg_msgseg *tmp = seg->next;